@agent-corpus/policy-manager 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +126 -29
  2. package/dist/adapters.d.ts +3 -1
  3. package/dist/adapters.js +37 -18
  4. package/dist/classifier.d.ts +98 -0
  5. package/dist/classifier.js +343 -0
  6. package/dist/cli.js +66 -7
  7. package/dist/config.d.ts +2 -0
  8. package/dist/config.js +6 -2
  9. package/dist/evidence.d.ts +1 -0
  10. package/dist/evidence.js +60 -0
  11. package/dist/flow.d.ts +4 -0
  12. package/dist/flow.js +91 -18
  13. package/dist/gitalias.d.ts +13 -0
  14. package/dist/gitalias.js +33 -0
  15. package/dist/grants.d.ts +9 -9
  16. package/dist/grants.js +46 -30
  17. package/dist/hard.d.ts +63 -0
  18. package/dist/hard.js +1637 -0
  19. package/dist/helpers.d.ts +59 -0
  20. package/dist/helpers.js +304 -0
  21. package/dist/merged.d.ts +6 -0
  22. package/dist/merged.js +23 -0
  23. package/dist/nativerules.d.ts +44 -0
  24. package/dist/nativerules.js +653 -0
  25. package/dist/permissive.js +179 -7
  26. package/dist/policy.d.ts +12 -0
  27. package/dist/policy.js +228 -45
  28. package/dist/profile.d.ts +7 -0
  29. package/dist/profile.js +32 -1
  30. package/dist/prompt.d.ts +32 -0
  31. package/dist/prompt.js +334 -0
  32. package/dist/reads.d.ts +46 -0
  33. package/dist/reads.js +812 -0
  34. package/dist/replay.d.ts +42 -0
  35. package/dist/replay.js +174 -1
  36. package/dist/service.d.ts +12 -1
  37. package/dist/service.js +23 -6
  38. package/dist/session.d.ts +76 -13
  39. package/dist/session.js +373 -116
  40. package/dist/shell.d.ts +2 -0
  41. package/dist/shell.js +1 -1
  42. package/dist/shellscan.d.ts +58 -0
  43. package/dist/shellscan.js +395 -0
  44. package/dist/statepaths.d.ts +41 -0
  45. package/dist/statepaths.js +706 -0
  46. package/dist/types.d.ts +11 -1
  47. package/package.json +1 -1
@@ -0,0 +1,343 @@
1
+ import { execFile } from "node:child_process";
2
+ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { CATEGORIES } from "./types.js";
6
+ /** The JSON schema every classifier CLI is given for its answer. */
7
+ export const VERDICT_SCHEMA = {
8
+ type: "object",
9
+ properties: {
10
+ category: { type: "string", enum: [...CATEGORIES] },
11
+ confidence: { type: "number" },
12
+ recoverable: { type: "boolean" },
13
+ rationale: { type: "string" },
14
+ },
15
+ required: ["category", "confidence", "recoverable", "rationale"],
16
+ additionalProperties: false,
17
+ };
18
+ export const DEFAULT_CLASSIFIER = {
19
+ enabled: true,
20
+ claude: { model: "haiku" },
21
+ codex: { model: "gpt-6-luna" },
22
+ copilot: { model: "auto" },
23
+ };
24
+ /** How long each CLI may take, in milliseconds. */
25
+ export const CLASSIFIER_TIMEOUT_MS = {
26
+ claude: 15_000,
27
+ codex: 15_000,
28
+ copilot: 20_000,
29
+ };
30
+ /** Set in the classifier's environment, so a policy hook that still runs there refuses tools. */
31
+ export const CLASSIFIER_ENV = "AGENT_CORPUS_POLICY_CLASSIFIER";
32
+ /** `execFile`, no shell, standard input closed, output capped, killed at the timeout. */
33
+ export const execRunner = (file, args, options) => new Promise((resolve) => {
34
+ const child = execFile(file, args, {
35
+ cwd: options.cwd,
36
+ env: options.env,
37
+ timeout: options.timeoutMs,
38
+ killSignal: "SIGKILL",
39
+ maxBuffer: 4 * 1024 * 1024,
40
+ windowsHide: true,
41
+ encoding: "utf8",
42
+ }, (error, stdout, stderr) => {
43
+ const failure = error;
44
+ resolve({
45
+ code: failure ? (typeof failure.code === "number" ? failure.code : null) : 0,
46
+ stdout: String(stdout ?? ""),
47
+ stderr: String(stderr ?? ""),
48
+ ...(failure
49
+ ? {
50
+ error: failure.code === "ENOENT"
51
+ ? "ENOENT"
52
+ : failure.killed || failure.signal
53
+ ? "timeout"
54
+ : typeof failure.code === "number"
55
+ ? undefined
56
+ : "other",
57
+ }
58
+ : {}),
59
+ });
60
+ });
61
+ child.stdin?.end();
62
+ });
63
+ const LOGIN = /log ?in|logged|auth|credential|api key|unauthori[sz]ed|\b401\b|forbidden|\b403\b/i;
64
+ /** A verdict with exactly the schema's shape, or undefined. */
65
+ export function parseVerdict(value) {
66
+ if (!value || typeof value !== "object" || Array.isArray(value))
67
+ return undefined;
68
+ const v = value;
69
+ if (!CATEGORIES.includes(v.category) ||
70
+ typeof v.confidence !== "number" ||
71
+ !Number.isFinite(v.confidence) ||
72
+ v.confidence < 0 ||
73
+ v.confidence > 1 ||
74
+ typeof v.recoverable !== "boolean" ||
75
+ typeof v.rationale !== "string")
76
+ return undefined;
77
+ return {
78
+ category: v.category,
79
+ confidence: v.confidence,
80
+ recoverable: v.recoverable,
81
+ rationale: v.rationale.slice(0, 1000),
82
+ };
83
+ }
84
+ class Failure extends Error {
85
+ reason;
86
+ constructor(reason) {
87
+ super(reason);
88
+ this.reason = reason;
89
+ }
90
+ }
91
+ function json(text) {
92
+ try {
93
+ return JSON.parse(text);
94
+ }
95
+ catch {
96
+ throw new Failure("bad-json");
97
+ }
98
+ }
99
+ function failed(run) {
100
+ if (run.error === "ENOENT")
101
+ throw new Failure("missing");
102
+ if (run.error === "timeout")
103
+ throw new Failure("timeout");
104
+ if (run.code !== 0) {
105
+ // Structured output (a Claude result, Copilot events) holds usage fields and model text that
106
+ // can mention "auth"; only an error message decides that the CLI is not logged in.
107
+ let text = run.stdout;
108
+ try {
109
+ const value = JSON.parse(run.stdout.split("\n").find((line) => line.trim()) ?? "");
110
+ text =
111
+ value &&
112
+ typeof value === "object" &&
113
+ typeof value.result === "string" &&
114
+ value.is_error === true
115
+ ? value.result
116
+ : "";
117
+ }
118
+ catch {
119
+ /* plain text output */
120
+ }
121
+ throw new Failure(LOGIN.test(`${run.stderr}\n${text}`) ? "not-logged-in" : "exit");
122
+ }
123
+ }
124
+ /**
125
+ * The exact command line for each harness. Claude: `-p` with a JSON schema; the answer is in
126
+ * `.structured_output`. Codex: `exec` with an output schema and an output file, in an empty
127
+ * directory. Copilot: `-p` with JSON events, its home moved to a private directory whose settings
128
+ * turn every hook off; authentication still comes from the user's stored login (the system
129
+ * keychain, or COPILOT_GITHUB_TOKEN/GH_TOKEN/GITHUB_TOKEN), which lives outside that directory.
130
+ */
131
+ function invocation(cli, model, prompt, dir, empty) {
132
+ const schema = JSON.stringify(VERDICT_SCHEMA);
133
+ if (cli === "claude")
134
+ return {
135
+ file: "claude",
136
+ args: [
137
+ "-p",
138
+ "--model",
139
+ model,
140
+ "--tools",
141
+ "",
142
+ "--setting-sources",
143
+ "",
144
+ "--settings",
145
+ JSON.stringify({ disableAllHooks: true, autoMemoryEnabled: false }),
146
+ "--no-session-persistence",
147
+ "--strict-mcp-config",
148
+ "--disable-slash-commands",
149
+ "--permission-mode",
150
+ "dontAsk",
151
+ // The schema answer is itself a tool call; a model that first reaches for a tool it does
152
+ // not have needs a few turns to return it (at 2, some runs stopped at the turn limit).
153
+ "--max-turns",
154
+ "5",
155
+ "--output-format",
156
+ "json",
157
+ "--json-schema",
158
+ schema,
159
+ prompt,
160
+ ],
161
+ env: {},
162
+ parse: (run) => {
163
+ const out = json(run.stdout);
164
+ if (!out || typeof out !== "object")
165
+ throw new Failure("bad-json");
166
+ if (out.is_error === true)
167
+ throw new Failure(LOGIN.test(String(out.result ?? "")) ? "not-logged-in" : "exit");
168
+ if (out.structured_output === undefined)
169
+ throw new Failure("schema");
170
+ return out.structured_output;
171
+ },
172
+ };
173
+ if (cli === "codex") {
174
+ const schemaFile = join(dir, "schema.json");
175
+ const outFile = join(dir, "out.json");
176
+ writeFileSync(schemaFile, schema, { mode: 0o600 });
177
+ return {
178
+ file: "codex",
179
+ args: [
180
+ "exec",
181
+ "--ephemeral",
182
+ "--ignore-user-config",
183
+ "--ignore-rules",
184
+ "--disable",
185
+ "hooks",
186
+ "--skip-git-repo-check",
187
+ "-s",
188
+ "read-only",
189
+ "-c",
190
+ 'approval_policy="never"',
191
+ "-c",
192
+ 'model_reasoning_effort="low"',
193
+ "-m",
194
+ model,
195
+ "-C",
196
+ empty,
197
+ "--output-schema",
198
+ schemaFile,
199
+ "-o",
200
+ outFile,
201
+ prompt,
202
+ ],
203
+ env: {},
204
+ parse: () => {
205
+ let text;
206
+ try {
207
+ text = readFileSync(outFile, "utf8");
208
+ }
209
+ catch {
210
+ throw new Failure("bad-json");
211
+ }
212
+ return json(text);
213
+ },
214
+ };
215
+ }
216
+ const home = join(dir, "copilot-home");
217
+ mkdirSync(home, { mode: 0o700 });
218
+ writeFileSync(join(home, "settings.json"), JSON.stringify({ disableAllHooks: true }), {
219
+ mode: 0o600,
220
+ });
221
+ return {
222
+ file: "copilot",
223
+ args: [
224
+ "-p",
225
+ prompt,
226
+ "--model",
227
+ model,
228
+ "--available-tools",
229
+ "",
230
+ "--no-custom-instructions",
231
+ "--disable-builtin-mcps",
232
+ "--no-ask-user",
233
+ "--no-auto-update",
234
+ "--disable-client-telemetry",
235
+ "--output-format",
236
+ "json",
237
+ ],
238
+ env: { COPILOT_HOME: home },
239
+ parse: (run) => {
240
+ const events = run.stdout
241
+ .split("\n")
242
+ .filter(Boolean)
243
+ .map((line) => {
244
+ try {
245
+ return JSON.parse(line);
246
+ }
247
+ catch {
248
+ return {};
249
+ }
250
+ });
251
+ const error = events.find((event) => event.type === "session.error");
252
+ if (error)
253
+ throw new Failure(LOGIN.test(String(error.data?.message ?? "")) ? "not-logged-in" : "exit");
254
+ const message = events
255
+ .filter((event) => event.type === "assistant.message")
256
+ .map((event) => event.data?.content)
257
+ .filter((content) => typeof content === "string")
258
+ .at(-1);
259
+ if (message === undefined)
260
+ throw new Failure("bad-json");
261
+ const start = message.indexOf("{");
262
+ const end = message.lastIndexOf("}");
263
+ if (start < 0 || end < start)
264
+ throw new Failure("bad-json");
265
+ return json(message.slice(start, end + 1));
266
+ },
267
+ };
268
+ }
269
+ /**
270
+ * Classifies one prompt with `cli`'s own CLI. Every failure (not installed, not logged in, a
271
+ * nonzero exit, a timeout, output that is not JSON or not the schema) is a result with `ok: false`,
272
+ * never an exception and never an approval.
273
+ */
274
+ export async function classifyWithCli(cli, prompt, config = DEFAULT_CLASSIFIER, options = {}) {
275
+ const model = config[cli].model;
276
+ const started = Date.now();
277
+ const fail = (reason) => ({
278
+ ok: false,
279
+ reason,
280
+ cli,
281
+ model,
282
+ latencyMs: Date.now() - started,
283
+ });
284
+ if (!config.enabled)
285
+ return fail("disabled");
286
+ if (prompt === undefined)
287
+ return fail("too-long");
288
+ let dir;
289
+ try {
290
+ dir = mkdtempSync(join(tmpdir(), "acp-classifier-"));
291
+ const empty = join(dir, "empty");
292
+ mkdirSync(empty, { mode: 0o700 });
293
+ const call = invocation(cli, model, prompt, dir, empty);
294
+ const run = await (options.runner ?? execRunner)(call.file, call.args, {
295
+ cwd: empty,
296
+ env: { ...process.env, ...call.env, [CLASSIFIER_ENV]: "1" },
297
+ timeoutMs: options.timeoutMs ?? CLASSIFIER_TIMEOUT_MS[cli],
298
+ });
299
+ failed(run);
300
+ const verdict = parseVerdict(call.parse(run));
301
+ if (!verdict)
302
+ return fail("schema");
303
+ return { ok: true, verdict, cli, model, latencyMs: Date.now() - started };
304
+ }
305
+ catch (error) {
306
+ return fail(error instanceof Failure ? error.reason : "exit");
307
+ }
308
+ finally {
309
+ if (dir)
310
+ rmSync(dir, { recursive: true, force: true });
311
+ }
312
+ }
313
+ const MODEL = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,79}$/;
314
+ /** Parses the profile's `classifier` key. Throws a message for anything invalid. */
315
+ export function parseClassifierConfig(raw) {
316
+ if (raw === undefined)
317
+ return structuredClone(DEFAULT_CLASSIFIER);
318
+ if (!raw || typeof raw !== "object" || Array.isArray(raw))
319
+ throw new Error("classifier must be an object");
320
+ const value = raw;
321
+ const result = structuredClone(DEFAULT_CLASSIFIER);
322
+ for (const [key, entry] of Object.entries(value)) {
323
+ if (key === "enabled") {
324
+ if (typeof entry !== "boolean")
325
+ throw new Error("classifier.enabled must be true or false");
326
+ result.enabled = entry;
327
+ continue;
328
+ }
329
+ if (key !== "claude" && key !== "codex" && key !== "copilot")
330
+ throw new Error(`classifier has an unknown key: ${key}`);
331
+ if (!entry || typeof entry !== "object" || Array.isArray(entry))
332
+ throw new Error(`classifier.${key} must be an object`);
333
+ for (const [field, model] of Object.entries(entry)) {
334
+ if (field !== "model")
335
+ throw new Error(`classifier.${key} has an unknown key: ${field}`);
336
+ if (typeof model !== "string" || !MODEL.test(model))
337
+ throw new Error(`classifier.${key}.model must be a model name`);
338
+ result[key].model = model;
339
+ }
340
+ }
341
+ return result;
342
+ }
343
+ //# sourceMappingURL=classifier.js.map
package/dist/cli.js CHANGED
@@ -6,10 +6,11 @@ import { fileURLToPath } from "node:url";
6
6
  import { parseArgs } from "node:util";
7
7
  import { executionReport, hookOutput, normalize } from "./adapters.js";
8
8
  import { sessionDirectoryPath } from "./audit.js";
9
+ import { CLASSIFIER_ENV } from "./classifier.js";
9
10
  import { hookConfig } from "./config.js";
10
- import { HUMAN_REVIEW_PRINCIPLE } from "./principle.js";
11
+ import { HUMAN_ONLY_FOR } from "./principle.js";
11
12
  import { defaultProfilePath, loadProfile, parseProfile } from "./profile.js";
12
- import { replayTranscripts } from "./replay.js";
13
+ import { replayClassified, replayTranscripts } from "./replay.js";
13
14
  import { formatReview, reviewClassifications, sessionDirectories } from "./review.js";
14
15
  import { latestHumanDecision, send, sendTo, serve, start, stop } from "./service.js";
15
16
  import { SetupError, setupHook } from "./setup.js";
@@ -26,11 +27,11 @@ policy-manager setup-hook-claude|setup-hook-codex|setup-hook-copilot [--dry-run]
26
27
  policy-manager config <claude|codex|copilot> <absolute-installed-cli-path>
27
28
  policy-manager profile validate <file> # check a policy profile without installing it
28
29
  policy-manager profile install <file> # install it to the user config directory outside Git
29
- policy-manager replay <transcript-dir-or-file...> [--profile <file>] [--files <n>] # approval rate over recorded Bash commands; nothing is executed
30
+ policy-manager replay <transcript-dir-or-file...> [--profile <file>] [--files <n>] [--classify [--limit <n>] [--json]] # approval rate over recorded Bash commands; --classify runs the real AI classifier on the newest escalations; nothing is executed
30
31
  policy-manager review-classifications [--since <iso-time>] [--dir <session-dir>...] [--json] # risk review of AI-classified ambiguous requests
31
32
  policy-manager stop <claude|codex|copilot> <session-id>
32
33
  A per-session background service checks policy and writes private temporary audit.jsonl logs.
33
- AI classification comes from the session's policy-manager agent, never an external API.
34
+ What the engine cannot approve is classified by the harness CLI the user is logged in to (claude, codex or copilot), run inside the hook.
34
35
  Unknown effects require human approval. Exit 0: protocol response; exit 1: malformed input or service failure.
35
36
  `;
36
37
  /** An `approve` failure whose message is meant for the user. */
@@ -93,6 +94,7 @@ try {
93
94
  cloudWrites: profile.cloudWrites,
94
95
  mcpReadOnly: profile.mcpReadOnly.length,
95
96
  toolsDeny: Object.values(profile.toolsDeny).reduce((n, list) => n + list.length, 0),
97
+ classifier: profile.classifier,
96
98
  digest: profile.digest,
97
99
  };
98
100
  if (harnessArg === "install") {
@@ -109,12 +111,38 @@ try {
109
111
  else if (action === "replay") {
110
112
  const { values, positionals } = parseArgs({
111
113
  args: args.slice(1),
112
- options: { profile: { type: "string" }, files: { type: "string" } },
114
+ options: {
115
+ profile: { type: "string" },
116
+ files: { type: "string" },
117
+ classify: { type: "boolean" },
118
+ limit: { type: "string" },
119
+ json: { type: "boolean" },
120
+ },
113
121
  allowPositionals: true,
114
122
  });
115
123
  const profile = values.profile
116
124
  ? parseProfile(readFileSync(values.profile, "utf8"))
117
125
  : loadProfile();
126
+ if (values.classify) {
127
+ const report = await replayClassified(positionals, profile, {
128
+ limit: Number(values.limit ?? 300),
129
+ files: Number(values.files ?? 200),
130
+ concurrency: 4,
131
+ });
132
+ if (values.json)
133
+ console.log(JSON.stringify(report, null, 2));
134
+ else {
135
+ console.log(`${report.commands} unique commands read; engine approved ${report.engineApproved}; ${report.escalations} escalations replayed`);
136
+ console.log(`AI approved ${report.aiApproved}; user allow rule approved ${report.userAllowApproved}; user deny/ask rule (left to the harness) ${report.userRuleNative}; other no-decision ${report.otherNative}; still ask ${report.stillAsk}`);
137
+ console.log(`classifier runs ${report.classifierRuns}; errors ${JSON.stringify(report.classifierErrors)}; latency p50 ${report.latencyMs.p50} ms, p95 ${report.latencyMs.p95} ms, max ${report.latencyMs.max} ms`);
138
+ for (const row of report.askReasons.slice(0, 15)) {
139
+ console.log(`${row.count}\t${row.reason}`);
140
+ for (const example of row.examples)
141
+ console.log(`\t ${example}`);
142
+ }
143
+ }
144
+ process.exit(0);
145
+ }
118
146
  const report = replayTranscripts(positionals, profile, Number(values.files ?? 50));
119
147
  const pct = (n) => `${((100 * n) / Math.max(report.commands, 1)).toFixed(1)}%`;
120
148
  console.log(`${report.commands} unique commands; approved ${pct(report.approved)} (strict ${pct(report.strictApproved)})`);
@@ -217,14 +245,30 @@ try {
217
245
  else if (action === "hook") {
218
246
  const payload = await stdin();
219
247
  const request = normalize(harness, payload);
220
- if (phase === "SessionEnd") {
248
+ const toolUseId = payload.tool_use_id ?? payload.toolUseId;
249
+ if (process.env[CLASSIFIER_ENV] === "1") {
250
+ // Inside the classifier's own CLI run, which has no tools and no hooks: refuse anything that
251
+ // still reaches a policy hook, and start no service.
252
+ if (phase === "PreToolUse")
253
+ console.log(hookOutput(harness, phase, {
254
+ decision: "deny",
255
+ category: "unknown",
256
+ reason: "The policy classifier runs without tools",
257
+ requestHash: "",
258
+ cache: "none",
259
+ cached: false,
260
+ }));
261
+ else
262
+ console.log(harness === "codex" ? "" : "{}");
263
+ }
264
+ else if (phase === "SessionEnd") {
221
265
  await stop(harness, request.sessionId);
222
266
  console.log("{}");
223
267
  }
224
268
  else {
225
269
  const directory = await start(harness, request.sessionId, cliPath);
226
270
  if (phase === "SessionStart") {
227
- const context = `Policy service started. Audit log: ${join(directory, "audit.jsonl")}. Native hooks route tool and permission requests directly to the deterministic service. An ambiguous request is blocked once with instructions to have the policy-manager agent classify it and then retry; the person is asked only if classification fails. ${HUMAN_REVIEW_PRINCIPLE} If .agent-config/agents/policy-manager.md exists, read it before creating a read-only policy-manager agent. Never ask an agent to approve its own tools.`;
271
+ const context = `Policy service started. Audit log: ${join(directory, "audit.jsonl")}. Tool requests are checked by a deterministic policy engine; what it cannot approve is classified by an AI model through the user's own logged-in CLI, inside the hook. What neither approves goes to the user${harness === "codex" ? ", who approves it from their own terminal" : " in the normal permission prompt"}. A person is needed only for ${HUMAN_ONLY_FOR}. Never ask an agent to approve its own tools.`;
228
272
  console.log(JSON.stringify(harness === "copilot"
229
273
  ? { additionalContext: context }
230
274
  : {
@@ -235,9 +279,20 @@ try {
235
279
  }));
236
280
  }
237
281
  else if (phase === "PostToolUse" || phase === "PostToolUseFailure") {
282
+ const error = payload.error ?? payload.tool_error;
238
283
  await send(harness, request.sessionId, {
239
284
  action: "record",
240
285
  report: executionReport(request, phase, payload),
286
+ phase,
287
+ ...(typeof toolUseId === "string" ? { toolUseId } : {}),
288
+ ...(phase === "PostToolUseFailure"
289
+ ? {
290
+ failure: {
291
+ ...(typeof error === "string" ? { error } : {}),
292
+ interrupted: payload.is_interrupt === true,
293
+ },
294
+ }
295
+ : {}),
241
296
  });
242
297
  console.log("{}");
243
298
  }
@@ -246,6 +301,10 @@ try {
246
301
  action: "review",
247
302
  request,
248
303
  phase,
304
+ ...(typeof toolUseId === "string" ? { toolUseId } : {}),
305
+ ...(typeof payload.permission_mode === "string"
306
+ ? { permissionMode: payload.permission_mode }
307
+ : {}),
249
308
  }));
250
309
  const output = hookOutput(harness, phase, decision, {
251
310
  cliPath,
package/dist/config.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import type { Request } from "./types.js";
2
2
  export declare const managedHookFlag = "--agent-corpus-policy-manager-hook";
3
+ /** Seconds each managed hook may run: above the service's review wait, which covers the classifier. */
4
+ export declare const HOOK_TIMEOUT_SECONDS = 45;
3
5
  export declare function hookConfig(harness: Request["harness"], cliPath: string): Record<string, unknown>;
4
6
  //# sourceMappingURL=config.d.ts.map
package/dist/config.js CHANGED
@@ -1,12 +1,14 @@
1
1
  import { isAbsolute } from "node:path";
2
2
  export const managedHookFlag = "--agent-corpus-policy-manager-hook";
3
+ /** Seconds each managed hook may run: above the service's review wait, which covers the classifier. */
4
+ export const HOOK_TIMEOUT_SECONDS = 45;
3
5
  export function hookConfig(harness, cliPath) {
4
6
  if (![cliPath, process.execPath].every((path) => isAbsolute(path) && !/["\r\n$`%]/.test(path)))
5
7
  throw new Error("Use a safe absolute path to the reviewed CLI installation");
6
8
  if (!["claude", "codex", "copilot"].includes(harness))
7
9
  throw new Error("Unknown harness");
8
10
  const hooks = {};
9
- const matcher = "Bash|Read|Edit|Write|apply_patch|bash|powershell|view|read_file|write_file|edit|create|exec_command|shell";
11
+ const matcher = "Bash|Read|Edit|Write|MultiEdit|NotebookEdit|apply_patch|bash|powershell|view|read_file|write_file|edit|create|exec_command|shell";
10
12
  // Copilot documents no MCP tool name format for hooks, so only Claude and Codex see MCP calls.
11
13
  const preMatcher = harness === "copilot" ? matcher : `${matcher}|mcp__.*`;
12
14
  const phases = [
@@ -18,8 +20,10 @@ export function hookConfig(harness, cliPath) {
18
20
  "SessionEnd",
19
21
  ];
20
22
  for (const phase of phases) {
23
+ // Every managed hook gets an explicit timeout above the AI classifier's (at most 20 s) plus a
24
+ // margin: a review can wait for the classifier, and Copilot's default of 30 s fails open.
21
25
  // Codex limits SessionEnd to 1 to 3 seconds and warns about anything longer.
22
- const timeout = harness === "codex" && phase === "SessionEnd" ? 3 : 8;
26
+ const timeout = harness === "codex" && phase === "SessionEnd" ? 3 : HOOK_TIMEOUT_SECONDS;
23
27
  if (harness === "copilot") {
24
28
  const event = phase[0]?.toLowerCase() + phase.slice(1);
25
29
  hooks[event] = [
@@ -11,6 +11,7 @@ export declare const UNKNOWN_CWD: string;
11
11
  * `x` beside the link's target, not beside the link.
12
12
  */
13
13
  export declare function resolveWord(cwd: string, word: string): string;
14
+ export declare function withEvidence<T>(run: () => T): T;
14
15
  export declare function git(cwd: string, args: string[]): string;
15
16
  export declare function canonical(path: string, links?: number): string;
16
17
  export declare function inside(root: string, path: string): boolean;
package/dist/evidence.js CHANGED
@@ -50,7 +50,49 @@ export function resolveWord(cwd, word) {
50
50
  }
51
51
  return current;
52
52
  }
53
+ /**
54
+ * Git answers memoized for the length of one decision (`withEvidence`): a script that names the
55
+ * same checkout many times asks Git once per question. Outside a decision nothing is cached, so a
56
+ * long-running service never reuses an answer across requests.
57
+ */
58
+ let gitMemo;
59
+ let gitDepth = 0;
60
+ /** `canonical` answers for the length of one decision, like `gitMemo`. */
61
+ let realMemo;
62
+ export function withEvidence(run) {
63
+ if (gitDepth++ === 0) {
64
+ gitMemo = new Map();
65
+ realMemo = new Map();
66
+ }
67
+ try {
68
+ return run();
69
+ }
70
+ finally {
71
+ if (--gitDepth === 0) {
72
+ gitMemo = undefined;
73
+ realMemo = undefined;
74
+ }
75
+ }
76
+ }
53
77
  export function git(cwd, args) {
78
+ if (!gitMemo)
79
+ return gitOnce(cwd, args);
80
+ const key = JSON.stringify([cwd, args]);
81
+ let hit = gitMemo.get(key);
82
+ if (!hit) {
83
+ try {
84
+ hit = { out: gitOnce(cwd, args) };
85
+ }
86
+ catch (error) {
87
+ hit = { error };
88
+ }
89
+ gitMemo.set(key, hit);
90
+ }
91
+ if ("error" in hit)
92
+ throw hit.error;
93
+ return hit.out;
94
+ }
95
+ function gitOnce(cwd, args) {
54
96
  return execFileSync("git", ["-c", "core.fsmonitor=false", "-C", cwd, ...args], {
55
97
  encoding: "utf8",
56
98
  timeout: 3000,
@@ -60,6 +102,24 @@ export function git(cwd, args) {
60
102
  }).trim();
61
103
  }
62
104
  export function canonical(path, links = 0) {
105
+ if (!realMemo || links)
106
+ return canonicalOnce(path, links);
107
+ let hit = realMemo.get(path);
108
+ if (!hit) {
109
+ try {
110
+ hit = { out: canonicalOnce(path, 0) };
111
+ }
112
+ catch (error) {
113
+ hit = { error };
114
+ }
115
+ if (realMemo.size < 20_000)
116
+ realMemo.set(path, hit);
117
+ }
118
+ if ("error" in hit)
119
+ throw hit.error;
120
+ return hit.out;
121
+ }
122
+ function canonicalOnce(path, links) {
63
123
  // Native resolution expands Windows 8.3 aliases as well as symlinks, so
64
124
  // paths supplied by Node and Git describe the same repository boundaries.
65
125
  if (existsSync(path))
package/dist/flow.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import { type Fill } from "./evidence.js";
2
2
  import type { Profile } from "./profile.js";
3
3
  import type { Decision, Request } from "./types.js";
4
+ /** With `outcome`, also reports the state when the list's exit status is success. */
5
+ /** How long one evaluation may take before it asks instead. */
6
+ export declare const TIME_BUDGET_MS = 5000;
7
+ export declare const TIME_BUDGET_REASON = "Command took too long to check";
4
8
  /**
5
9
  * Evaluates a compound command. `cd <literal>` is tracked: every later command is judged in each
6
10
  * directory it could run in (before and after the `cd`, since `cd x || ...` may fail), so a command