@agent-corpus/policy-manager 0.5.0 → 0.6.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.
- package/README.md +54 -10
- package/dist/adapters.d.ts +3 -1
- package/dist/adapters.js +18 -4
- package/dist/audit.js +16 -2
- package/dist/cli.js +10 -3
- package/dist/config.js +7 -1
- package/dist/created.d.ts +44 -0
- package/dist/created.js +236 -0
- package/dist/evidence.d.ts +53 -1
- package/dist/evidence.js +201 -6
- package/dist/flow.d.ts +2 -1
- package/dist/flow.js +856 -76
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/permissive.d.ts +11 -3
- package/dist/permissive.js +648 -28
- package/dist/policy.d.ts +3 -1
- package/dist/policy.js +78 -35
- package/dist/principle.d.ts +8 -0
- package/dist/principle.js +8 -0
- package/dist/profile.d.ts +8 -0
- package/dist/profile.js +26 -0
- package/dist/recoverable.d.ts +41 -0
- package/dist/recoverable.js +391 -0
- package/dist/script.d.ts +35 -1
- package/dist/script.js +215 -17
- package/dist/service.js +2 -2
- package/dist/session.d.ts +4 -1
- package/dist/session.js +44 -15
- package/dist/types.d.ts +9 -3
- package/dist/types.js +12 -1
- package/package.json +2 -2
package/dist/script.js
CHANGED
|
@@ -163,6 +163,134 @@ function closingParen(command, start) {
|
|
|
163
163
|
}
|
|
164
164
|
return undefined;
|
|
165
165
|
}
|
|
166
|
+
/** No leading `=`: zsh's EQUALS option expands `=cmd` to a command path. */
|
|
167
|
+
const PARAM_WORD = /^(?:[A-Za-z0-9_@%+:,./-][A-Za-z0-9_@%+=:,./-]*)?$/;
|
|
168
|
+
const PARAM_PATTERN = /^[A-Za-z0-9_@%+=:,./*?-]+$/;
|
|
169
|
+
/** The name and operation inside `${...}`, or undefined for a form that is not modelled. */
|
|
170
|
+
function parameter(body) {
|
|
171
|
+
const NAME_OR_POSITION = "([A-Za-z_][A-Za-z0-9_]*|[1-9][0-9]*)";
|
|
172
|
+
let m = new RegExp(`^${NAME_OR_POSITION}$`).exec(body);
|
|
173
|
+
if (m)
|
|
174
|
+
return { name: m[1] };
|
|
175
|
+
m = new RegExp(`^#${NAME_OR_POSITION}$`).exec(body);
|
|
176
|
+
if (m)
|
|
177
|
+
return { name: m[1], op: { kind: "length" } };
|
|
178
|
+
m = new RegExp(`^${NAME_OR_POSITION}(:?)([-+])(.*)$`).exec(body);
|
|
179
|
+
if (m && PARAM_WORD.test(m[4]))
|
|
180
|
+
return {
|
|
181
|
+
name: m[1],
|
|
182
|
+
op: { kind: m[3] === "-" ? "default" : "alternate", word: m[4] },
|
|
183
|
+
};
|
|
184
|
+
m = new RegExp(`^${NAME_OR_POSITION}(##?|%%?)(.*)$`).exec(body);
|
|
185
|
+
if (m && PARAM_PATTERN.test(m[3])) {
|
|
186
|
+
const op = m[2];
|
|
187
|
+
return {
|
|
188
|
+
name: m[1],
|
|
189
|
+
op: {
|
|
190
|
+
kind: "strip",
|
|
191
|
+
suffix: op.startsWith("%"),
|
|
192
|
+
longest: op.length === 2,
|
|
193
|
+
pattern: m[3],
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
m = new RegExp(`^${NAME_OR_POSITION}:([0-9]+)(?::([0-9]+))?$`).exec(body);
|
|
198
|
+
if (m)
|
|
199
|
+
return {
|
|
200
|
+
name: m[1],
|
|
201
|
+
op: {
|
|
202
|
+
kind: "slice",
|
|
203
|
+
offset: Number(m[2]),
|
|
204
|
+
...(m[3] === undefined ? {} : { length: Number(m[3]) }),
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
function globRegex(pattern) {
|
|
210
|
+
const source = [...pattern]
|
|
211
|
+
.map((c) => (c === "*" ? ".*" : c === "?" ? "." : c.replace(/[.+^${}()|[\]\\/-]/g, "\\$&")))
|
|
212
|
+
.join("");
|
|
213
|
+
return new RegExp(`^${source}$`, "s");
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Applies a parameter operation to a value that is set and not empty, which is the only kind of
|
|
217
|
+
* value the evaluator knows.
|
|
218
|
+
*/
|
|
219
|
+
export function applyParamOp(value, op) {
|
|
220
|
+
if (!op || op.kind === "default")
|
|
221
|
+
return value;
|
|
222
|
+
if (op.kind === "alternate")
|
|
223
|
+
return op.word;
|
|
224
|
+
if (op.kind === "length")
|
|
225
|
+
return String([...value].length);
|
|
226
|
+
if (op.kind === "slice")
|
|
227
|
+
return value.slice(op.offset, op.length === undefined ? undefined : op.offset + op.length);
|
|
228
|
+
const regex = globRegex(op.pattern);
|
|
229
|
+
const lengths = [...Array(value.length + 1).keys()];
|
|
230
|
+
if (op.longest)
|
|
231
|
+
lengths.reverse();
|
|
232
|
+
for (const n of lengths) {
|
|
233
|
+
const piece = op.suffix ? value.slice(value.length - n) : value.slice(0, n);
|
|
234
|
+
if (regex.test(piece))
|
|
235
|
+
return op.suffix ? value.slice(0, value.length - n) : value.slice(n);
|
|
236
|
+
}
|
|
237
|
+
return value;
|
|
238
|
+
}
|
|
239
|
+
const ANSI_ESCAPES = {
|
|
240
|
+
a: "\x07",
|
|
241
|
+
b: "\b",
|
|
242
|
+
e: "\x1b",
|
|
243
|
+
E: "\x1b",
|
|
244
|
+
f: "\f",
|
|
245
|
+
n: "\n",
|
|
246
|
+
r: "\r",
|
|
247
|
+
t: "\t",
|
|
248
|
+
v: "\v",
|
|
249
|
+
"\\": "\\",
|
|
250
|
+
'"': '"',
|
|
251
|
+
"?": "?",
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* `$'...'` starting at `command[at] === "$"`. `\'` is refused so that the plain single-quote scan
|
|
255
|
+
* used to find the end of a substitution agrees with the shell; so are NUL, `\c`, `\u` and `\U`.
|
|
256
|
+
*/
|
|
257
|
+
function ansiString(command, at) {
|
|
258
|
+
let text = "";
|
|
259
|
+
for (let i = at + 2; i < command.length; i++) {
|
|
260
|
+
const c = command[i];
|
|
261
|
+
if (c === "'") {
|
|
262
|
+
if (text.includes(SUBSTITUTION) || text.includes("\0"))
|
|
263
|
+
return undefined;
|
|
264
|
+
return { kind: "text", text, next: i + 1, quoted: true };
|
|
265
|
+
}
|
|
266
|
+
if (c !== "\\") {
|
|
267
|
+
text += c;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
const e = command[i + 1];
|
|
271
|
+
if (e === undefined || e === "'" || e === "c" || e === "u" || e === "U")
|
|
272
|
+
return undefined;
|
|
273
|
+
const known = ANSI_ESCAPES[e];
|
|
274
|
+
if (known !== undefined) {
|
|
275
|
+
text += known;
|
|
276
|
+
i++;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
const hex = e === "x" ? /^[0-9A-Fa-f]{1,2}/.exec(command.slice(i + 2)) : null;
|
|
280
|
+
const octal = /^[0-7]{1,3}/.exec(command.slice(i + 1));
|
|
281
|
+
if (hex) {
|
|
282
|
+
text += String.fromCharCode(Number.parseInt(hex[0], 16));
|
|
283
|
+
i += 1 + hex[0].length;
|
|
284
|
+
}
|
|
285
|
+
else if (octal) {
|
|
286
|
+
text += String.fromCharCode(Number.parseInt(octal[0], 8) & 0xff);
|
|
287
|
+
i += octal[0].length;
|
|
288
|
+
}
|
|
289
|
+
else
|
|
290
|
+
text += c; // an unknown escape keeps its backslash; the next character follows
|
|
291
|
+
}
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
166
294
|
/**
|
|
167
295
|
* Reads the expansion that starts at `command[at] === "$"`, or undefined for one that is not
|
|
168
296
|
* understood. Only HOME has a value known before anything runs; `$?`, `$#`, `$$` and PIPESTATUS
|
|
@@ -191,18 +319,34 @@ function expansion(command, at, quoted = false) {
|
|
|
191
319
|
return undefined;
|
|
192
320
|
return { kind: "unknown", next: end + 1, inner: command.slice(at + 2, end) };
|
|
193
321
|
}
|
|
322
|
+
if (rest[0] === "'" && !quoted)
|
|
323
|
+
return ansiString(command, at);
|
|
194
324
|
let name;
|
|
325
|
+
let op;
|
|
195
326
|
let next;
|
|
196
327
|
if (rest[0] === "{") {
|
|
197
328
|
const close = rest.indexOf("}");
|
|
198
|
-
|
|
329
|
+
if (close <= 0)
|
|
330
|
+
return undefined;
|
|
331
|
+
const body = rest.slice(1, close);
|
|
199
332
|
next = at + close + 2;
|
|
200
|
-
if (
|
|
333
|
+
if (UNKNOWN_PARAMETER.test(body))
|
|
334
|
+
return { kind: "unknown", next };
|
|
335
|
+
const parsed = parameter(body);
|
|
336
|
+
if (!parsed)
|
|
201
337
|
return undefined;
|
|
338
|
+
({ name, op } = parsed);
|
|
339
|
+
}
|
|
340
|
+
else if (rest[0] && /[0-9]/.test(rest[0]) && /[0-9]/.test(rest[1] ?? "")) {
|
|
341
|
+
return undefined; // `$10` is `$1` then `0` in bash and sh, the tenth parameter in zsh
|
|
202
342
|
}
|
|
203
|
-
else if (rest[0] && "?#$".includes(rest[0])) {
|
|
343
|
+
else if (rest[0] && "?#$0".includes(rest[0])) {
|
|
204
344
|
return { kind: "unknown", next: at + 2 };
|
|
205
345
|
}
|
|
346
|
+
else if (rest[0] && /[1-9@*]/.test(rest[0])) {
|
|
347
|
+
name = rest[0];
|
|
348
|
+
next = at + 2;
|
|
349
|
+
}
|
|
206
350
|
else {
|
|
207
351
|
name = /^[A-Za-z_][A-Za-z0-9_]*/.exec(rest)?.[0];
|
|
208
352
|
if (!name)
|
|
@@ -213,9 +357,9 @@ function expansion(command, at, quoted = false) {
|
|
|
213
357
|
return { kind: "unknown", next };
|
|
214
358
|
if (name === "HOME") {
|
|
215
359
|
const text = home();
|
|
216
|
-
return text === undefined ? undefined : { kind: "text", text, next };
|
|
360
|
+
return text === undefined ? undefined : { kind: "text", text: applyParamOp(text, op), next };
|
|
217
361
|
}
|
|
218
|
-
return { kind: "var", name, next };
|
|
362
|
+
return op ? { kind: "var", name, next, op } : { kind: "var", name, next };
|
|
219
363
|
}
|
|
220
364
|
function tokenize(src) {
|
|
221
365
|
const tokens = [];
|
|
@@ -227,7 +371,8 @@ function tokenize(src) {
|
|
|
227
371
|
let quotedAny = false;
|
|
228
372
|
let headPlain = true;
|
|
229
373
|
let assignName;
|
|
230
|
-
let
|
|
374
|
+
let braceOpen = false;
|
|
375
|
+
let braceExpansion = false;
|
|
231
376
|
let globChar = false;
|
|
232
377
|
let quote = "";
|
|
233
378
|
let pendingRedirect;
|
|
@@ -245,8 +390,8 @@ function tokenize(src) {
|
|
|
245
390
|
if (!active)
|
|
246
391
|
return;
|
|
247
392
|
flush();
|
|
248
|
-
|
|
249
|
-
if (
|
|
393
|
+
// `{a,b}` and `{1..3}` expand to several words; `{}`, `@{u}` and a lone `{` are literal.
|
|
394
|
+
if (braceExpansion)
|
|
250
395
|
reject();
|
|
251
396
|
const word = { parts, subs, quoted: quotedAny, assign: assignName, glob: globChar };
|
|
252
397
|
parts = [];
|
|
@@ -255,7 +400,8 @@ function tokenize(src) {
|
|
|
255
400
|
quotedAny = false;
|
|
256
401
|
headPlain = true;
|
|
257
402
|
assignName = undefined;
|
|
258
|
-
|
|
403
|
+
braceOpen = false;
|
|
404
|
+
braceExpansion = false;
|
|
259
405
|
globChar = false;
|
|
260
406
|
if (pendingRedirect) {
|
|
261
407
|
tokens.push({ t: "redir", redir: { ...pendingRedirect, target: word } });
|
|
@@ -277,11 +423,14 @@ function tokenize(src) {
|
|
|
277
423
|
subs.push(inner);
|
|
278
424
|
};
|
|
279
425
|
const appendExpansion = (e, quoted) => {
|
|
280
|
-
if (e.kind === "text")
|
|
426
|
+
if (e.kind === "text") {
|
|
281
427
|
lit += e.text;
|
|
428
|
+
if (e.quoted)
|
|
429
|
+
quotedAny = true;
|
|
430
|
+
}
|
|
282
431
|
else if (e.kind === "var") {
|
|
283
432
|
flush();
|
|
284
|
-
parts.push({ name: e.name, quoted });
|
|
433
|
+
parts.push(e.op ? { name: e.name, quoted, op: e.op } : { name: e.name, quoted });
|
|
285
434
|
}
|
|
286
435
|
else
|
|
287
436
|
appendUnknown(quoted, e.inner);
|
|
@@ -382,9 +531,12 @@ function tokenize(src) {
|
|
|
382
531
|
i++;
|
|
383
532
|
continue;
|
|
384
533
|
}
|
|
534
|
+
// History expansion is off in the non-interactive shells that run these commands, but `!(`
|
|
535
|
+
// is an extended glob and `!!`/`!$` are worth no risk; `!=` is a comparison operator.
|
|
385
536
|
if (char === "!" &&
|
|
386
|
-
!(active === false && (src[i + 1] === undefined || /\s/.test(src[i + 1])))
|
|
387
|
-
|
|
537
|
+
!(active === false && (src[i + 1] === undefined || /\s/.test(src[i + 1]))) &&
|
|
538
|
+
src[i + 1] !== "=") {
|
|
539
|
+
reject();
|
|
388
540
|
}
|
|
389
541
|
if (char === " " || char === "\t") {
|
|
390
542
|
endWord();
|
|
@@ -453,7 +605,14 @@ function tokenize(src) {
|
|
|
453
605
|
continue;
|
|
454
606
|
}
|
|
455
607
|
if (char === "(") {
|
|
456
|
-
|
|
608
|
+
// `name()` starts a function definition; the parser checks the rest.
|
|
609
|
+
const definition = active &&
|
|
610
|
+
src[i + 1] === ")" &&
|
|
611
|
+
parts.length === 0 &&
|
|
612
|
+
!quotedAny &&
|
|
613
|
+
assignName === undefined &&
|
|
614
|
+
NAME.test(lit);
|
|
615
|
+
if ((active && !definition) || src[i + 1] === "(")
|
|
457
616
|
reject();
|
|
458
617
|
operator("(");
|
|
459
618
|
continue;
|
|
@@ -468,8 +627,10 @@ function tokenize(src) {
|
|
|
468
627
|
if (parts.length === 0 && /^[0-9]{2,}$/.test(lit))
|
|
469
628
|
reject();
|
|
470
629
|
}
|
|
471
|
-
if (char === "{"
|
|
472
|
-
|
|
630
|
+
if (char === "{")
|
|
631
|
+
braceOpen = true;
|
|
632
|
+
else if (braceOpen && (char === "," || (char === "." && src[i + 1] === ".")))
|
|
633
|
+
braceExpansion = true;
|
|
473
634
|
if (char === "*" || char === "?" || char === "[")
|
|
474
635
|
globChar = true;
|
|
475
636
|
if (char === ">" || char === "<") {
|
|
@@ -561,6 +722,19 @@ function tokenize(src) {
|
|
|
561
722
|
}
|
|
562
723
|
if (assignName === undefined && !/[A-Za-z0-9_]/.test(char))
|
|
563
724
|
headPlain = false;
|
|
725
|
+
// An assignment value starting with `~` or `~/` is the home directory.
|
|
726
|
+
if (char === "~" &&
|
|
727
|
+
assignName !== undefined &&
|
|
728
|
+
parts.length === 0 &&
|
|
729
|
+
lit === "" &&
|
|
730
|
+
!quotedAny &&
|
|
731
|
+
(src[i + 1] === undefined || /[/\s;&|)]/.test(src[i + 1]))) {
|
|
732
|
+
const path = homedir();
|
|
733
|
+
if (/[\s*?[\]'"$`~]/.test(path))
|
|
734
|
+
reject();
|
|
735
|
+
lit += path;
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
564
738
|
lit += char;
|
|
565
739
|
active = true;
|
|
566
740
|
}
|
|
@@ -733,6 +907,24 @@ class Parser {
|
|
|
733
907
|
return { kind: "subshell", body, redirects: this.redirects() };
|
|
734
908
|
}
|
|
735
909
|
const word = this.keyword(tok);
|
|
910
|
+
const open = this.tokens[this.pos + 1];
|
|
911
|
+
const close = this.tokens[this.pos + 2];
|
|
912
|
+
if (word === undefined &&
|
|
913
|
+
tok?.t === "word" &&
|
|
914
|
+
open?.t === "op" &&
|
|
915
|
+
open.op === "(" &&
|
|
916
|
+
close?.t === "op" &&
|
|
917
|
+
close.op === ")") {
|
|
918
|
+
const name = plainWord(tok.w);
|
|
919
|
+
if (name === undefined || !NAME.test(name) || depth >= MAX_DEPTH)
|
|
920
|
+
reject();
|
|
921
|
+
this.pos += 3;
|
|
922
|
+
this.skipNewlines();
|
|
923
|
+
const body = this.command(depth + 1);
|
|
924
|
+
if ((body.kind !== "group" && body.kind !== "subshell") || body.redirects.length)
|
|
925
|
+
reject();
|
|
926
|
+
return { kind: "function", name: name, body: body };
|
|
927
|
+
}
|
|
736
928
|
if (word === undefined)
|
|
737
929
|
return this.simple();
|
|
738
930
|
if (depth >= MAX_DEPTH)
|
|
@@ -774,7 +966,13 @@ class Parser {
|
|
|
774
966
|
this.expectKeyword("do");
|
|
775
967
|
const body = this.list(new Set(["done"]), depth + 1);
|
|
776
968
|
this.expectKeyword("done");
|
|
777
|
-
return {
|
|
969
|
+
return {
|
|
970
|
+
kind: "while",
|
|
971
|
+
cond,
|
|
972
|
+
body,
|
|
973
|
+
redirects: this.redirects(),
|
|
974
|
+
...(word === "until" ? { until: true } : {}),
|
|
975
|
+
};
|
|
778
976
|
}
|
|
779
977
|
case "if": {
|
|
780
978
|
const clauses = [];
|
package/dist/service.js
CHANGED
|
@@ -3,7 +3,7 @@ import { chmodSync, existsSync } from "node:fs";
|
|
|
3
3
|
import { createConnection, createServer } from "node:net";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { audit, sessionDirectory, sessionDirectoryPath, sessionToken } from "./audit.js";
|
|
6
|
-
import { hash } from "./policy.js";
|
|
6
|
+
import { hash, requestHash } from "./policy.js";
|
|
7
7
|
import { loadProfile, strictProfile } from "./profile.js";
|
|
8
8
|
import { PolicySession } from "./session.js";
|
|
9
9
|
function endpoint(directory) {
|
|
@@ -44,7 +44,7 @@ export async function serve(harness, sessionId) {
|
|
|
44
44
|
event: "request-envelope",
|
|
45
45
|
payloadHash: hash(data.slice(0, end)),
|
|
46
46
|
...(message.request
|
|
47
|
-
? { requestHash:
|
|
47
|
+
? { requestHash: requestHash(message.request) }
|
|
48
48
|
: message.classification
|
|
49
49
|
? { requestHash: message.classification.requestHash }
|
|
50
50
|
: message.report
|
package/dist/session.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Profile } from "./profile.js";
|
|
2
|
-
import type
|
|
2
|
+
import { type Classification, type Decision, type OutcomeReport, type Request } from "./types.js";
|
|
3
3
|
/** AI approvals per session; beyond this, ambiguous requests go to a human. */
|
|
4
4
|
export declare const MAX_AI_APPROVALS = 50;
|
|
5
5
|
export declare class PolicySession {
|
|
@@ -12,6 +12,9 @@ export declare class PolicySession {
|
|
|
12
12
|
private states;
|
|
13
13
|
/** Requests the AI approved, counted once each however often they are retried. */
|
|
14
14
|
private approvedByAi;
|
|
15
|
+
/** Files this session created, and the paths each approved request may create. */
|
|
16
|
+
private created;
|
|
17
|
+
private creating;
|
|
15
18
|
constructor(directory: string, harness: string, sessionId: string, profile?: Profile);
|
|
16
19
|
private state;
|
|
17
20
|
private remember;
|
package/dist/session.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { audit, requestSummary } from "./audit.js";
|
|
2
|
-
import {
|
|
2
|
+
import { creationTargets, pathKey, settle, withCreated, } from "./created.js";
|
|
3
|
+
import { evaluate, hash, requestHash as requestHashOf } from "./policy.js";
|
|
3
4
|
import { strictProfile } from "./profile.js";
|
|
4
5
|
import { redactText } from "./redact.js";
|
|
6
|
+
import { CATEGORIES, } from "./types.js";
|
|
5
7
|
/** How long a classification outcome for one exact request is remembered. */
|
|
6
8
|
const CLASSIFICATION_TTL_MS = 5 * 60_000;
|
|
7
9
|
/** AI approvals per session; beyond this, ambiguous requests go to a human. */
|
|
8
10
|
export const MAX_AI_APPROVALS = 50;
|
|
11
|
+
/** How long after its approval an execution report may still record the files a tool created. */
|
|
12
|
+
const CREATION_REPORT_MS = 15 * 60_000;
|
|
9
13
|
export class PolicySession {
|
|
10
14
|
directory;
|
|
11
15
|
harness;
|
|
@@ -16,6 +20,9 @@ export class PolicySession {
|
|
|
16
20
|
states = new Map();
|
|
17
21
|
/** Requests the AI approved, counted once each however often they are retried. */
|
|
18
22
|
approvedByAi = new Set();
|
|
23
|
+
/** Files this session created, and the paths each approved request may create. */
|
|
24
|
+
created = new Map();
|
|
25
|
+
creating = new Map();
|
|
19
26
|
constructor(directory, harness, sessionId, profile = strictProfile) {
|
|
20
27
|
this.directory = directory;
|
|
21
28
|
this.harness = harness;
|
|
@@ -53,16 +60,7 @@ export class PolicySession {
|
|
|
53
60
|
throw new Error("Session mismatch");
|
|
54
61
|
audit(this.directory, { event: "request", ...requestSummary(request) });
|
|
55
62
|
if (ai &&
|
|
56
|
-
(!
|
|
57
|
-
"code-read",
|
|
58
|
-
"worktree-write",
|
|
59
|
-
"cloud-read",
|
|
60
|
-
"git-read",
|
|
61
|
-
"impactful-write",
|
|
62
|
-
"destructive",
|
|
63
|
-
"sensitive",
|
|
64
|
-
"unknown",
|
|
65
|
-
].includes(ai.category) ||
|
|
63
|
+
(!CATEGORIES.includes(ai.category) ||
|
|
66
64
|
typeof ai.confidence !== "number" ||
|
|
67
65
|
!Number.isFinite(ai.confidence) ||
|
|
68
66
|
(ai.recoverable !== undefined && typeof ai.recoverable !== "boolean") ||
|
|
@@ -73,14 +71,18 @@ export class PolicySession {
|
|
|
73
71
|
decision: "human",
|
|
74
72
|
category: "unknown",
|
|
75
73
|
reason: "Malformed AI classification",
|
|
76
|
-
requestHash:
|
|
74
|
+
requestHash: requestHashOf(request),
|
|
77
75
|
});
|
|
78
76
|
throw new Error("Malformed AI classification");
|
|
79
77
|
}
|
|
80
|
-
const key = hash([
|
|
78
|
+
const key = hash([
|
|
79
|
+
requestHashOf(request),
|
|
80
|
+
"policy-v1",
|
|
81
|
+
this.profile.digest ?? this.profile.mode,
|
|
82
|
+
]);
|
|
81
83
|
const entry = this.cache.get(key);
|
|
82
84
|
const cachedAi = entry && entry.expires > Date.now() ? entry.ai : undefined;
|
|
83
|
-
const requestHash =
|
|
85
|
+
const requestHash = requestHashOf(request);
|
|
84
86
|
const known = ai ? undefined : this.state(requestHash);
|
|
85
87
|
if (ai)
|
|
86
88
|
audit(this.directory, {
|
|
@@ -91,7 +93,7 @@ export class PolicySession {
|
|
|
91
93
|
...(ai.recoverable === undefined ? {} : { recoverable: ai.recoverable }),
|
|
92
94
|
...(ai.rationale ? { rationale: redactText(ai.rationale, 200) } : {}),
|
|
93
95
|
});
|
|
94
|
-
let decision = evaluate(request, ai ?? cachedAi ?? known?.ai, this.profile);
|
|
96
|
+
let decision = withCreated(this.created, () => evaluate(request, ai ?? cachedAi ?? known?.ai, this.profile));
|
|
95
97
|
if (decision.tier === "ambiguous") {
|
|
96
98
|
if (ai && !decision.classification)
|
|
97
99
|
this.remember(requestHash, { invalid: true });
|
|
@@ -169,6 +171,17 @@ export class PolicySession {
|
|
|
169
171
|
this.pending.delete(this.pending.keys().next().value ?? "");
|
|
170
172
|
this.pending.set(decision.requestHash, request);
|
|
171
173
|
}
|
|
174
|
+
// Every decision replaces the pending creations for this request, so a report can only ever
|
|
175
|
+
// settle the most recent approval: an earlier approval whose report never came is forgotten.
|
|
176
|
+
this.creating.delete(decision.requestHash);
|
|
177
|
+
if (decision.decision === "approve") {
|
|
178
|
+
const targets = creationTargets(request, this.created);
|
|
179
|
+
if (targets.length) {
|
|
180
|
+
if (this.creating.size >= 256)
|
|
181
|
+
this.creating.delete(this.creating.keys().next().value ?? "");
|
|
182
|
+
this.creating.set(decision.requestHash, { targets, approvedAt: Date.now() });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
172
185
|
const { ai: classification, ...safe } = decision;
|
|
173
186
|
audit(this.directory, {
|
|
174
187
|
event: "decision",
|
|
@@ -215,6 +228,22 @@ export class PolicySession {
|
|
|
215
228
|
...(report.durationMs === undefined ? {} : { durationMs: report.durationMs }),
|
|
216
229
|
summary: "Harness-reported execution result",
|
|
217
230
|
});
|
|
231
|
+
// A target is the session's only if `settle` proves the file there is its own inode or a new
|
|
232
|
+
// one born after the approval; otherwise any earlier claim on the path is dropped.
|
|
233
|
+
// A report that comes too long after its approval records nothing.
|
|
234
|
+
const pending = this.creating.get(report.requestHash);
|
|
235
|
+
const fresh = pending && Date.now() - pending.approvedAt <= CREATION_REPORT_MS ? pending : undefined;
|
|
236
|
+
for (const target of fresh?.targets ?? []) {
|
|
237
|
+
const now = settle(target, fresh?.approvedAt ?? Number.POSITIVE_INFINITY);
|
|
238
|
+
if (!now)
|
|
239
|
+
this.created.delete(pathKey(target.path));
|
|
240
|
+
else {
|
|
241
|
+
if (this.created.size >= 4096)
|
|
242
|
+
this.created.clear();
|
|
243
|
+
this.created.set(pathKey(target.path), now);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
this.creating.delete(report.requestHash);
|
|
218
247
|
}
|
|
219
248
|
else
|
|
220
249
|
throw new Error("Invalid outcome event");
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
export
|
|
1
|
+
export declare const CATEGORIES: readonly ["code-read", "worktree-write",
|
|
2
|
+
/** A recoverable write outside any repository, such as the system temp directory. */
|
|
3
|
+
"temp-write", "cloud-read", "git-read", "impactful-write", "destructive", "sensitive", "unknown"];
|
|
4
|
+
export type Category = (typeof CATEGORIES)[number];
|
|
2
5
|
export interface Request {
|
|
3
6
|
sessionId: string;
|
|
4
7
|
harness: "claude" | "codex" | "copilot";
|
|
@@ -18,8 +21,11 @@ export interface Classification {
|
|
|
18
21
|
/** Where an ambiguous request stands in the AI classification flow. */
|
|
19
22
|
export type ClassificationStage = "requested" | "approved" | "denied" | "fallback";
|
|
20
23
|
export interface Decision {
|
|
21
|
-
/**
|
|
22
|
-
|
|
24
|
+
/**
|
|
25
|
+
* `deny` is a decision the model sees and works around; `human` asks the person; `native` makes
|
|
26
|
+
* no decision and leaves the request to the harness's own permission flow.
|
|
27
|
+
*/
|
|
28
|
+
decision: "approve" | "human" | "deny" | "native";
|
|
23
29
|
category: Category;
|
|
24
30
|
reason: string;
|
|
25
31
|
requestHash: string;
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1,13 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const CATEGORIES = [
|
|
2
|
+
"code-read",
|
|
3
|
+
"worktree-write",
|
|
4
|
+
/** A recoverable write outside any repository, such as the system temp directory. */
|
|
5
|
+
"temp-write",
|
|
6
|
+
"cloud-read",
|
|
7
|
+
"git-read",
|
|
8
|
+
"impactful-write",
|
|
9
|
+
"destructive",
|
|
10
|
+
"sensitive",
|
|
11
|
+
"unknown",
|
|
12
|
+
];
|
|
2
13
|
//# sourceMappingURL=types.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-corpus/policy-manager",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Deterministic, session-scoped permission decisions with audit logs and bounded AI classification.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"coding-agent",
|
|
@@ -50,6 +50,6 @@
|
|
|
50
50
|
"postpack": "node -e \"require('fs').rmSync('LICENSE',{force:true})\""
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@agent-corpus/audit-log": "^0.1.
|
|
53
|
+
"@agent-corpus/audit-log": "^0.1.1"
|
|
54
54
|
}
|
|
55
55
|
}
|