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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125// The harness travels as the basic-auth username: audit metadata, never// identity. The token in the password slot is the only identity.import { Option, Schema } from "effect";import type { Types } from "effect";
export const HarnessAudit = Schema.Struct({ harness: Schema.optionalKey(Schema.NonEmptyString), model: Schema.optionalKey(Schema.NonEmptyString), session: Schema.optionalKey(Schema.NonEmptyString),});
export type HarnessAudit = typeof HarnessAudit.Type;
const named = (...names: readonly string[]) => (key: string) => names.includes(key);
const prefixed = (prefix: string) => (key: string) => key.startsWith(prefix);
const markers = [ { harness: "claude-code", marks: named("CLAUDECODE", "CLAUDE_CODE", "CLAUDE_CODE_ENTRYPOINT"), }, { harness: "codex", marks: prefixed("CODEX_") }, { harness: "cursor", marks: named("CURSOR_AGENT") }, { harness: "opencode", marks: named("OPENCODE") }, { harness: "amp", marks: prefixed("AMP_") }, { harness: "pi", marks: prefixed("PI_") }, { harness: "gemini", marks: named("GEMINI_CLI") },] as const satisfies readonly { readonly harness: string; readonly marks: (key: string) => boolean;}[];
const given = (value: string | undefined) => value === undefined || value === "" ? undefined : value;
const piModel = (env: Readonly<Record<string, string | undefined>>) => { const model = given(env.PI_MODEL); const provider = given(env.PI_PROVIDER);
return model === undefined || provider === undefined ? model : `${provider}/${model}`;};
export const detectHarness = ( env: Readonly<Record<string, string | undefined>>): HarnessAudit => { const set = Object.keys(env).filter((key) => given(env[key]) !== undefined);
const harness = given(env.AI_AGENT) ?? markers.find(({ marks }) => set.some(marks))?.harness;
const audit: Types.Mutable<HarnessAudit> = {};
if (harness === undefined) { return audit; }
audit.harness = harness;
if (harness !== "pi") { return audit; }
const model = piModel(env); const session = given(env.PI_SESSION_ID);
if (model !== undefined) { audit.model = model; }
if (session !== undefined) { audit.session = session; }
return audit;};
const fields = ["harness", "model", "session"] as const;
const PREFIX = "audit;";
// Three fields with the prefix and keys stay within 200 characters (30 + 3 × 56 = 198).const VALUE_LIMIT = 56;
const clean = (value: string) => value.replaceAll(/[^A-Za-z0-9._/-]/gu, "_").slice(0, VALUE_LIMIT);
export const auditUser = (audit: HarnessAudit): string => `${PREFIX}${fields .flatMap((field) => { const value = audit[field];
return value === undefined ? [] : [`${field}=${clean(value)}`]; }) .join("&")}`;
const Pair = /^(?<field>harness|model|session)=(?<value>[A-Za-z0-9._/-]+)$/u;
export const parseAuditUser = (user: string): Option.Option<HarnessAudit> => { if (!user.startsWith(PREFIX)) { return Option.none(); }
const rest = user.slice(PREFIX.length);
const pairs = rest === "" ? [] : rest.split("&").map((pair) => Pair.exec(pair));
const entries = pairs.flatMap((match) => match?.groups?.field === undefined || match.groups.value === undefined ? [] : [[match.groups.field, match.groups.value] as const] );
return entries.length === pairs.length ? Schema.decodeOption(HarnessAudit)(Object.fromEntries(entries)) : Option.none();};