audit.ts
This file
Path
packages/protocol/src/audit.ts
124 lines · 3.4 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
lines 1–2why, one step back1// The harness travels as the basic-auth username: audit metadata, never2// identity. The token in the password slot is the only identity.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 3–87why, one step back3import { Option, Schema } from "effect";4import type { Types } from "effect";56export const HarnessAudit = Schema.Struct({7 harness: Schema.optionalKey(Schema.NonEmptyString),8 model: Schema.optionalKey(Schema.NonEmptyString),9 session: Schema.optionalKey(Schema.NonEmptyString),10});1112export type HarnessAudit = typeof HarnessAudit.Type;1314const named =15 (...names: readonly string[]) =>16 (key: string) =>17 names.includes(key);1819const prefixed = (prefix: string) => (key: string) => key.startsWith(prefix);2021const markers = [22 {23 harness: "claude-code",24 marks: named("CLAUDECODE", "CLAUDE_CODE", "CLAUDE_CODE_ENTRYPOINT"),25 },26 { harness: "codex", marks: prefixed("CODEX_") },27 { harness: "cursor", marks: named("CURSOR_AGENT") },28 { harness: "opencode", marks: named("OPENCODE") },29 { harness: "amp", marks: prefixed("AMP_") },30 { harness: "pi", marks: prefixed("PI_") },31 { harness: "gemini", marks: named("GEMINI_CLI") },32] as const satisfies readonly {33 readonly harness: string;34 readonly marks: (key: string) => boolean;35}[];3637const given = (value: string | undefined) =>38 value === undefined || value === "" ? undefined : value;3940const piModel = (env: Readonly<Record<string, string | undefined>>) => {41 const model = given(env.PI_MODEL);42 const provider = given(env.PI_PROVIDER);4344 return model === undefined || provider === undefined45 ? model46 : `${provider}/${model}`;47};4849export const detectHarness = (50 env: Readonly<Record<string, string | undefined>>51): HarnessAudit => {52 const set = Object.keys(env).filter((key) => given(env[key]) !== undefined);5354 const harness =55 given(env.AI_AGENT) ??56 markers.find(({ marks }) => set.some(marks))?.harness;5758 const audit: Types.Mutable<HarnessAudit> = {};5960 if (harness === undefined) {61 return audit;62 }6364 audit.harness = harness;6566 if (harness !== "pi") {67 return audit;68 }6970 const model = piModel(env);71 const session = given(env.PI_SESSION_ID);7273 if (model !== undefined) {74 audit.model = model;75 }7677 if (session !== undefined) {78 audit.session = session;79 }8081 return audit;82};8384const fields = ["harness", "model", "session"] as const;8586const PREFIX = "audit;";87Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 88why, one step back88// Three fields with the prefix and keys stay within 200 characters (30 + 3 × 56 = 198).Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 89–124why, one step back89const VALUE_LIMIT = 56;9091const clean = (value: string) =>92 value.replaceAll(/[^A-Za-z0-9._/-]/gu, "_").slice(0, VALUE_LIMIT);9394export const auditUser = (audit: HarnessAudit): string =>95 `${PREFIX}${fields96 .flatMap((field) => {97 const value = audit[field];9899 return value === undefined ? [] : [`${field}=${clean(value)}`];100 })101 .join("&")}`;102103const Pair = /^(?<field>harness|model|session)=(?<value>[A-Za-z0-9._/-]+)$/u;104105export const parseAuditUser = (user: string): Option.Option<HarnessAudit> => {106 if (!user.startsWith(PREFIX)) {107 return Option.none();108 }109110 const rest = user.slice(PREFIX.length);111112 const pairs =113 rest === "" ? [] : rest.split("&").map((pair) => Pair.exec(pair));114115 const entries = pairs.flatMap((match) =>116 match?.groups?.field === undefined || match.groups.value === undefined117 ? []118 : [[match.groups.field, match.groups.value] as const]119 );120121 return entries.length === pairs.length122 ? Schema.decodeOption(HarnessAudit)(Object.fromEntries(entries))123 : Option.none();124};