pi-permission-classifier 0.6.0 → 0.7.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/src/reviewer.ts CHANGED
@@ -5,12 +5,11 @@
5
5
  * The decision runs top to bottom, deferring at the first miss so the cheap
6
6
  * gates short-circuit before any model call:
7
7
  * 1. a config is loaded (else defer, recorded),
8
- * 2. the gate-authoritative surface is determinable and outside the `path`
9
- * and `external_directory` families — the bare name or any directional
10
- * member such as `path_read` or `external_directory_write` (else defer —
11
- * the engine caps any allow on those families anyway, so the classifier
12
- * never attempts them); every other surface, whatever its name, is
13
- * judged,
8
+ * 2. the gate-authoritative surface is determinable (else defer); every
9
+ * surface is judged, the `path` and `external_directory` families
10
+ * included. The permission system caps an allow on those families to
11
+ * defer unless the operator trusts this link with the family
12
+ * (`authorizerTrust`); a deny stands either way,
14
13
  * 3. the extracted full-command context fits `contextBudgetBytes` (else
15
14
  * defer, recorded — over-budget context is never rendered),
16
15
  * 4. the judge model and its auth resolve (else defer, recorded),
@@ -42,6 +41,14 @@ import { extractFullCommandContext, type FullCommandContext } from "./context";
42
41
  import type { GuidanceSelection } from "./guidance";
43
42
  import type { HealthOutcome } from "./health";
44
43
  import { type CompleteFn, type ReviewOutcome, reviewAsk } from "./model-review";
44
+ import {
45
+ type TargetContextDeps,
46
+ type TargetReading,
47
+ extractTargetContext,
48
+ resolveTargetDeps,
49
+ sessionCwdOf,
50
+ tempDirectoryOf,
51
+ } from "./target-context";
45
52
  import type { UserIntent } from "./user-intent";
46
53
  import {
47
54
  applyVerdictPolicy,
@@ -51,11 +58,13 @@ import {
51
58
  } from "./verdict-policy";
52
59
 
53
60
  /**
54
- * Surface families the engine caps to defer; the classifier never even asks.
55
- * A family covers its bare name and every `<name>_*` member, the shape the
56
- * engine's delegation envelope uses (`path_read`, `path_write`,
57
- * `external_directory_read`, `external_directory_write`); an unknown
58
- * member is excluded too, which only ever defers.
61
+ * Surface families the permission system caps an allow on unless the
62
+ * operator trusts the link with them. The LLM judge reviews them; the quick
63
+ * judge never allows them and passes them on as uncertain. A family covers
64
+ * its bare name and every `<name>_*` member, the shape the engine's
65
+ * delegation envelope uses (`path_read`, `path_write`,
66
+ * `external_directory_read`, `external_directory_write`); an unknown member
67
+ * is covered too.
59
68
  */
60
69
  const EXCLUDED_SURFACE_FAMILIES = ["path", "external_directory"] as const;
61
70
 
@@ -148,6 +157,18 @@ export interface ClassifierReviewerDeps {
148
157
  * nothing about what listens; the extension folds it into session health.
149
158
  */
150
159
  onOutcome?: (outcome: HealthOutcome) => void;
160
+ /**
161
+ * The session's working directory, which the target facts measure
162
+ * containment against. Absent leaves the target's location unknown; the
163
+ * path itself is still read and reported.
164
+ */
165
+ getCwd?: () => string;
166
+ /**
167
+ * The target-read seams (filesystem, temporary directory, cwd). Tests
168
+ * answer them in memory; production leaves them unset and takes the real
169
+ * ones, built from `getCwd`.
170
+ */
171
+ targetDeps?: TargetContextDeps;
151
172
  }
152
173
 
153
174
  /**
@@ -200,6 +221,8 @@ export function createClassifierReviewer(
200
221
  deferReason: "internal-error",
201
222
  ...contextFields(false, context),
202
223
  ...guidanceFields(trail.guidance),
224
+ // Target fields omitted: reading them needs the seams that may
225
+ // themselves have thrown, and this entry must be written.
203
226
  });
204
227
  } catch {
205
228
  // The log failed too; there is nothing left to record on.
@@ -243,6 +266,8 @@ async function decide(
243
266
  const surface = gateSurfaceOf(details);
244
267
  const value = details.payload.request.value;
245
268
  const context = extractFullCommandContext(details.payload);
269
+ const targetSeams = resolveTargetDeps(deps.targetDeps, deps.getCwd);
270
+ const target = extractTargetContext(details, targetSeams);
246
271
 
247
272
  const config = deps.getConfig();
248
273
  if (!config) {
@@ -253,6 +278,7 @@ async function decide(
253
278
  modelId: null,
254
279
  deferReason: "no-config",
255
280
  context,
281
+ target,
256
282
  });
257
283
  }
258
284
 
@@ -266,15 +292,6 @@ async function decide(
266
292
  });
267
293
  return { kind: "defer" };
268
294
  }
269
- if (isExcludedSurface(surface)) {
270
- log.debug(SHORT_CIRCUIT_EVENT, {
271
- judge: JUDGE_TAG,
272
- requestId,
273
- surface,
274
- reason: "excluded-surface",
275
- });
276
- return { kind: "defer" };
277
- }
278
295
  // The budget gate: an over-budget full command is never rendered,
279
296
  // not even truncated. The ask is uncertain under the policy, with the
280
297
  // measurements on record. Decided before the breaker and model stages: it
@@ -287,6 +304,7 @@ async function decide(
287
304
  modelId: null,
288
305
  deferReason: "context-over-budget",
289
306
  context,
307
+ target,
290
308
  });
291
309
  }
292
310
 
@@ -298,6 +316,7 @@ async function decide(
298
316
  modelId: null,
299
317
  deferReason: "breaker-open",
300
318
  context,
319
+ target,
301
320
  });
302
321
  }
303
322
 
@@ -317,6 +336,7 @@ async function decide(
317
336
  : null,
318
337
  deferReason: "model-unresolved",
319
338
  context,
339
+ target,
320
340
  });
321
341
  }
322
342
  const modelId = `${model.provider}/${model.id}`;
@@ -330,6 +350,7 @@ async function decide(
330
350
  modelId,
331
351
  deferReason: "auth-failed",
332
352
  context,
353
+ target,
333
354
  });
334
355
  }
335
356
 
@@ -349,6 +370,7 @@ async function decide(
349
370
  modelId,
350
371
  deferReason: "guidance-load-failed",
351
372
  context,
373
+ target,
352
374
  });
353
375
  }
354
376
 
@@ -363,6 +385,11 @@ async function decide(
363
385
  context,
364
386
  guidance: guidance.included,
365
387
  ...(userIntent === undefined ? {} : { userIntent }),
388
+ promptFacts: {
389
+ ...(target.included ? { target: target.target } : {}),
390
+ cwd: sessionCwdOf(targetSeams),
391
+ tempDirectory: tempDirectoryOf(targetSeams),
392
+ },
366
393
  });
367
394
  if (
368
395
  outcome.deferReason !== undefined &&
@@ -403,6 +430,7 @@ async function decide(
403
430
  verdict: verdict.kind,
404
431
  deferReason,
405
432
  ...contextFields(context !== null, context),
433
+ ...targetFields(target),
406
434
  ...guidanceFields(guidance),
407
435
  });
408
436
  // Health folds in what the judge did, not what the policy made of it, so
@@ -447,6 +475,11 @@ function linkOutcomeOf(outcome: ReviewOutcome): LinkOutcome {
447
475
  return outcome.deferReason === "timeout" || outcome.deferReason === "call-failed"
448
476
  ? { kind: "failure", failure: outcome.deferReason }
449
477
  : { kind: "uncertain" };
478
+ case "allow_units":
479
+ // A partial allow over a bash chain is a verdict kind the permission
480
+ // system accepts from a link; the model review never produces one. Treat
481
+ // it as uncertain so it can never become an allow by accident.
482
+ return { kind: "uncertain" };
450
483
  }
451
484
  }
452
485
 
@@ -467,6 +500,7 @@ function recordPreModel(
467
500
  modelId: string | null;
468
501
  deferReason: PreModelDeferReason;
469
502
  context: FullCommandContext | null;
503
+ target: TargetReading;
470
504
  },
471
505
  ): AuthorizerVerdict {
472
506
  const verdict: AuthorizerVerdict =
@@ -484,6 +518,7 @@ function recordPreModel(
484
518
  verdict: verdict.kind,
485
519
  deferReason: entry.deferReason,
486
520
  ...contextFields(false, entry.context),
521
+ ...targetFields(entry.target),
487
522
  ...guidanceFields(EMPTY_GUIDANCE),
488
523
  });
489
524
  report(deps, { verdict: "defer", deferReason: entry.deferReason });
@@ -519,6 +554,43 @@ export function contextFields(
519
554
  };
520
555
  }
521
556
 
557
+ /**
558
+ * The seven target fields every decision entry carries. The path is logged
559
+ * outright: the review log already carries it in the tool-input preview.
560
+ * `targetOmitted` names why a target is absent.
561
+ */
562
+ export function targetFields(reading: TargetReading): {
563
+ targetIncluded: boolean;
564
+ targetPath: string | null;
565
+ targetResolved: string | null;
566
+ targetDirectory: string | null;
567
+ targetState: string | null;
568
+ targetLocation: string | null;
569
+ targetOmitted: string | null;
570
+ } {
571
+ if (reading.included === false) {
572
+ return {
573
+ targetIncluded: false,
574
+ targetPath: null,
575
+ targetResolved: null,
576
+ targetDirectory: null,
577
+ targetState: null,
578
+ targetLocation: null,
579
+ targetOmitted: reading.reason,
580
+ };
581
+ }
582
+ const { target } = reading;
583
+ return {
584
+ targetIncluded: true,
585
+ targetPath: target.path,
586
+ targetResolved: target.resolved,
587
+ targetDirectory: target.directory,
588
+ targetState: target.state,
589
+ targetLocation: target.location,
590
+ targetOmitted: null,
591
+ };
592
+ }
593
+
522
594
  /** No guidance: the render before the seam runs, and after it fails. */
523
595
  export const EMPTY_GUIDANCE: GuidanceSelection = { included: [], dropped: [] };
524
596
 
@@ -4,12 +4,13 @@
4
4
  * answers become a link outcome.
5
5
  *
6
6
  * Pure: no I/O, no clock, no model call. The question text lives here and
7
- * nowhere in config. The state carries the ask facts only: the gated value,
8
- * surface, tool and cwd, the full command when within budget, the selected
9
- * guidance files when the caller passes them, and the user's own messages
10
- * when intent is available. Tool results, file contents, assistant text,
11
- * payload evidence, annotations and the tool-input preview never reach the
12
- * model.
7
+ * nowhere in config. The state carries the ask facts only: the file the action
8
+ * targets when the gate resolved one, then the gated value, surface, tool and
9
+ * cwd, the full command when within budget, the selected guidance files when
10
+ * the caller passes them, and the user's own messages when intent is available.
11
+ * The target keys lead, because for a path-bearing tool the gated value is only
12
+ * the tool name. Tool results, file contents, assistant text, payload evidence,
13
+ * annotations and the tool-input preview never reach the model.
13
14
  */
14
15
 
15
16
  import type {
@@ -23,6 +24,7 @@ import type { PromptPermissionDetails } from "@gotgenes/pi-permission-system";
23
24
 
24
25
  import type { FullCommandContext } from "./context";
25
26
  import type { IncludedGuidance } from "./guidance";
27
+ import type { TargetContext } from "./target-context";
26
28
  import type { UserIntent } from "./user-intent";
27
29
  import type { LinkOutcome } from "./verdict-policy";
28
30
 
@@ -147,6 +149,8 @@ export interface SystemOneContextInputs {
147
149
  surface: string;
148
150
  /** The session cwd the action runs in. */
149
151
  cwd: string;
152
+ /** The file the action targets, when the gate resolved one. */
153
+ target?: TargetContext | null;
150
154
  /** The extracted full command, or null when the ask has none. */
151
155
  fullCommand: FullCommandContext | null;
152
156
  /** A full command larger than this is left out of the state. */
@@ -160,12 +164,19 @@ export interface SystemOneContextInputs {
160
164
  /** Build the classify context for one ask. */
161
165
  export function buildSystemOneContext(inputs: SystemOneContextInputs): ClassifierContext {
162
166
  const request = inputs.details.payload.request;
163
- const state: JsonObject = {
164
- value: request.value,
165
- surface: inputs.surface,
166
- tool: request.toolName,
167
- cwd: inputs.cwd,
168
- };
167
+ const target = inputs.target ?? null;
168
+ const state: JsonObject = {};
169
+ if (target !== null) {
170
+ state.targetPath = target.path;
171
+ if (target.resolved !== null) state.targetResolved = target.resolved;
172
+ state.targetDirectory = target.directory;
173
+ state.targetState = target.state;
174
+ state.targetLocation = target.location;
175
+ }
176
+ state.value = request.value;
177
+ state.surface = inputs.surface;
178
+ state.tool = request.toolName;
179
+ state.cwd = inputs.cwd;
169
180
  if (inputs.fullCommand !== null && inputs.fullCommand.bytes <= inputs.contextBudgetBytes) {
170
181
  state.fullCommand = inputs.fullCommand.text;
171
182
  }