Skip to content
Myr

audit.test.ts

This file

Path

packages/protocol/test/audit.test.ts

88 lines · 3.0 KiB · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

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 1–88why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { Option } from "effect";34import { auditUser, detectHarness, parseAuditUser } from "../src/audit.ts";56describe("which harness runs the CLI, from its env", () => {7 it.each([8 [{ AI_AGENT: "goose" }, { harness: "goose" }],9 [{ CLAUDECODE: "1" }, { harness: "claude-code" }],10 [{ CLAUDE_CODE: "1" }, { harness: "claude-code" }],11 [{ CLAUDE_CODE_ENTRYPOINT: "cli" }, { harness: "claude-code" }],12 [{ CODEX_SANDBOX: "seatbelt" }, { harness: "codex" }],13 [{ CURSOR_AGENT: "1" }, { harness: "cursor" }],14 [{ OPENCODE: "1" }, { harness: "opencode" }],15 [{ AMP_THREAD_ID: "t-1" }, { harness: "amp" }],16 [{ PI_CODING_AGENT: "true" }, { harness: "pi" }],17 [{ GEMINI_CLI: "1" }, { harness: "gemini" }],18 ] as const)("%o is %o", (env, audit) => {19 expect(detectHarness(env)).toStrictEqual(audit);20 });2122 it("takes pi's model and session from the env pi gives its children", () => {23 expect(24 detectHarness({25 AI_AGENT: "pi",26 PI_CODING_AGENT: "true",27 PI_MODEL: "kimi-k3",28 PI_PROVIDER: "moonshot",29 PI_SESSION_ID: "0193-abcd",30 })31 ).toStrictEqual({32 harness: "pi",33 model: "moonshot/kimi-k3",34 session: "0193-abcd",35 });36 });3738 it("names AI_AGENT's harness over any other marker", () => {39 expect(detectHarness({ AI_AGENT: "pi", CLAUDECODE: "1" })).toStrictEqual({40 harness: "pi",41 });42 });4344 it("finds no harness in a plain shell, or in empty markers", () => {45 expect(46 detectHarness({ HOME: "/home/someone", PATH: "/bin" })47 ).toStrictEqual({});48 expect(detectHarness({ AI_AGENT: "", CLAUDECODE: "" })).toStrictEqual({});49 });50});5152describe("the audit username", () => {53 it("carries the harness, model and session as a basic-auth username", () => {54 expect(auditUser({ harness: "pi", model: "kimi-k3" })).toBe(55 "audit;harness=pi&model=kimi-k3"56 );57 });5859 it("replaces any character a username can't carry, and stays at 200 characters or fewer", () => {60 const user = auditUser({61 harness: "my agent:v2",62 model: "x".repeat(300),63 session: "a&b=c;d",64 });6566 expect(user.startsWith("audit;harness=my_agent_v2&model=x")).toBeTruthy();67 expect(user).toContain("&session=a_b_c_d");68 expect(user.length).toBeLessThanOrEqual(200);69 expect(user).toMatch(/^audit;[A-Za-z0-9._/=&-]*$/u);70 });7172 it("reads back what it wrote", () => {73 const audit = {74 harness: "claude-code",75 model: "anthropic/claude-x",76 session: "s-1",77 };7879 expect(parseAuditUser(auditUser(audit))).toStrictEqual(Option.some(audit));80 expect(parseAuditUser(auditUser({}))).toStrictEqual(Option.some({}));81 });8283 it("is not any other username", () => {84 expect(parseAuditUser("x-access-token")).toStrictEqual(Option.none());85 expect(parseAuditUser("audit;harness")).toStrictEqual(Option.none());86 expect(parseAuditUser("audit;harness=a b")).toStrictEqual(Option.none());87 });88});