@duke-dsh-plugins/dsh-agent-approval 1.3.5 → 1.4.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 (4) hide show
  1. package/client.js +176 -11
  2. package/index.js +236 -15
  3. package/package.json +1 -1
  4. package/typert.host.js +123 -3
package/client.js CHANGED
@@ -6,7 +6,8 @@
6
6
  * 1. A "Agent 审批" page in the Settings panel (`settings.section`):
7
7
  * - approval model picker (provider + model, or the harness default),
8
8
  * - judge timeout setting (fail-closed),
9
- * - the list of sessions with the mode enabled,
9
+ * - the list of sessions with the mode enabled (session-list title +
10
+ * workspace, so each chip is recognizable),
10
11
  * - the latest approval audit records (verdict, risk, model, duration,
11
12
  * rationale; hover for the full rationale + tool arguments).
12
13
  *
@@ -38,7 +39,9 @@ window.__ModuleLoader__.load({
38
39
  .aapr-muted{color:var(--dsw-alias-label-secondary);line-height:1.5}
39
40
  .aapr-row{display:flex;align-items:center;gap:8px;flex-wrap:wrap}
40
41
  .aapr-select,.aapr-input{background:var(--dsw-alias-bg-layer-2);color:var(--dsw-alias-label-primary);border:1px solid var(--dsw-alias-border-l2);border-radius:6px;padding:4px 8px;font-size:12px;max-width:340px}
41
- .aapr-chip{display:inline-flex;align-items:center;gap:6px;border:1px solid var(--dsw-alias-border-l2);border-radius:999px;padding:2px 10px;background:var(--dsw-alias-bg-layer-2);font-family:monospace}
42
+ .aapr-chip{display:inline-flex;align-items:center;gap:6px;border:1px solid var(--dsw-alias-border-l2);border-radius:999px;padding:2px 10px;background:var(--dsw-alias-bg-layer-2);max-width:100%}
43
+ .aapr-chip-title{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:320px}
44
+ .aapr-chip-id{color:var(--dsw-alias-label-secondary);font-family:monospace;font-size:11px}
42
45
  .aapr-chip button{background:none;border:none;color:var(--dsw-alias-state-error-primary);cursor:pointer;font-size:12px;padding:0 2px}
43
46
  .aapr-wrap{overflow-x:auto}
44
47
  .aapr-table{width:100%;border-collapse:collapse;font-size:12px}
@@ -47,6 +50,12 @@ window.__ModuleLoader__.load({
47
50
  .aapr-cell{max-width:280px;overflow:hidden;text-overflow:ellipsis;white-space:normal}
48
51
  .aapr-ok{color:var(--dsw-alias-state-success-primary);white-space:nowrap}
49
52
  .aapr-no{color:var(--dsw-alias-state-error-primary);white-space:nowrap}
53
+ .aapr-input-wide{flex:1;min-width:220px;max-width:none}
54
+ .aapr-rule-match{font-family:monospace;font-size:11px;color:var(--dsw-alias-label-secondary)}
55
+ .aapr-rule-del,.aapr-whitelist{background:none;border:none;cursor:pointer;font-size:12px;padding:0 2px}
56
+ .aapr-rule-del{color:var(--dsw-alias-state-error-primary)}
57
+ .aapr-whitelist{color:var(--dsw-alias-label-secondary)}
58
+ .aapr-whitelist:hover{color:var(--dsw-alias-label-primary)}
50
59
 
51
60
  /* Settings nav icon: DSH 0.1.x settings.section only projects id/order/
52
61
  label, and the settings shell paints a generic gear for every external
@@ -249,6 +258,24 @@ window.__ModuleLoader__.load({
249
258
  parameters: param("dsh-agent-approval#AgentApprovalToggleRequest"),
250
259
  result: result("dsh-agent-approval#AgentApprovalToggleResult"),
251
260
  },
261
+ {
262
+ id: "dsh-agent-approval#agentApproval/addRule",
263
+ service: "agentApproval",
264
+ namespace: "agentApproval",
265
+ method: "addRule",
266
+ invocation: { kind: "direct" },
267
+ parameters: param("dsh-agent-approval#AgentApprovalAddRuleRequest"),
268
+ result: result("dsh-agent-approval#AgentApprovalRulesResult"),
269
+ },
270
+ {
271
+ id: "dsh-agent-approval#agentApproval/removeRule",
272
+ service: "agentApproval",
273
+ namespace: "agentApproval",
274
+ method: "removeRule",
275
+ invocation: { kind: "direct" },
276
+ parameters: param("dsh-agent-approval#AgentApprovalRemoveRuleRequest"),
277
+ result: result("dsh-agent-approval#AgentApprovalRulesResult"),
278
+ },
252
279
  {
253
280
  id: "dsh-agent-approval#agentApproval/clearRecords",
254
281
  service: "agentApproval",
@@ -360,6 +387,11 @@ window.__ModuleLoader__.load({
360
387
  const noteSlot = React.useState("");
361
388
  const note = noteSlot[0];
362
389
  const setNote = noteSlot[1];
390
+ // Rule-add form drafts (the table itself comes from getState).
391
+ const ruleEffectSlot = React.useState("allow");
392
+ const ruleToolSlot = React.useState("");
393
+ const ruleMatchSlot = React.useState("");
394
+ const ruleNoteSlot = React.useState("");
363
395
 
364
396
  const refresh = () => {
365
397
  remote
@@ -425,6 +457,58 @@ window.__ModuleLoader__.load({
425
457
  .catch(() => {});
426
458
  };
427
459
 
460
+ const ruleEffect = ruleEffectSlot[0];
461
+ const ruleTool = ruleToolSlot[0];
462
+ const ruleMatch = ruleMatchSlot[0];
463
+ const ruleNote = ruleNoteSlot[0];
464
+ const setRuleEffect = ruleEffectSlot[1];
465
+ const setRuleTool = ruleToolSlot[1];
466
+ const setRuleMatch = ruleMatchSlot[1];
467
+ const setRuleNote = ruleNoteSlot[1];
468
+ const rules = state !== null && Array.isArray(state.rules) ? state.rules : [];
469
+
470
+ const addRule = (draft) => {
471
+ remote
472
+ .addRule(draft)
473
+ .then((res) => {
474
+ const v = pick(res);
475
+ setState((prev) => (prev ? Object.assign({}, prev, { rules: v.rules }) : prev));
476
+ setRuleTool("");
477
+ setRuleMatch("");
478
+ setRuleNote("");
479
+ setNote("规则已保存(deny 优先于 allow;命中即不再经过审批模型)");
480
+ })
481
+ .catch((e) => setNote("保存失败:" + (e && e.message ? e.message : String(e))));
482
+ };
483
+ const submitRule = () => {
484
+ if (ruleTool.trim() === "") {
485
+ setNote("工具名必填(* 匹配所有工具)");
486
+ return;
487
+ }
488
+ addRule({ effect: ruleEffect, tool: ruleTool.trim(), match: ruleMatch, note: ruleNote.trim() });
489
+ };
490
+ const removeRule = (id) => {
491
+ remote
492
+ .removeRule({ id: id })
493
+ .then((res) => {
494
+ const v = pick(res);
495
+ setState((prev) => (prev ? Object.assign({}, prev, { rules: v.rules }) : prev));
496
+ })
497
+ .catch(() => {});
498
+ };
499
+ // One-click whitelist from an audit row: the recorded args are a
500
+ // PREFIX of the real arguments JSON (the Host truncates at 2000
501
+ // chars), so stripping the truncation marker keeps a valid substring.
502
+ const whitelistRecord = (r) => {
503
+ const args = String(r.args || "").replace(/…\[truncated\]$/, "");
504
+ addRule({
505
+ effect: "allow",
506
+ tool: String(r.toolName),
507
+ match: args,
508
+ note: "来自审计 " + fmtTime(r.at),
509
+ });
510
+ };
511
+
428
512
  const providerOptions = [{ id: "", name: "默认(Harness 默认模型)" }].concat(
429
513
  dir ? dir.providers : [],
430
514
  );
@@ -529,15 +613,81 @@ window.__ModuleLoader__.load({
529
613
  : h(
530
614
  "div",
531
615
  { className: "aapr-row" },
532
- state.enabledSessions.map((sid) =>
533
- h(
616
+ state.enabledSessions.map((raw) => {
617
+ // New hosts send { id, title, cwd }; a not-yet-restarted
618
+ // old host still sends bare id strings — render both.
619
+ const info =
620
+ typeof raw === "string" ? { id: raw, title: "", cwd: "" } : raw;
621
+ const sid = String(info.id);
622
+ const title =
623
+ typeof info.title === "string" && info.title !== "" ? info.title : "";
624
+ const cwd = typeof info.cwd === "string" && info.cwd !== "" ? info.cwd : "";
625
+ const tip =
626
+ "会话 ID:" + sid + (cwd !== "" ? "\n工作区:" + cwd : "");
627
+ return h(
534
628
  "span",
535
- { key: sid, className: "aapr-chip" },
536
- String(sid).slice(0, 8),
537
- h("button", { onClick: () => disableSession(sid), title: "关闭该会话的 Agent 审批" }, "✕"),
538
- ),
629
+ { key: sid, className: "aapr-chip", title: tip },
630
+ h(
631
+ "span",
632
+ { className: "aapr-chip-title" },
633
+ title !== "" ? title : sid.slice(0, 8),
634
+ ),
635
+ title !== ""
636
+ ? h("span", { className: "aapr-chip-id" }, sid.slice(0, 8))
637
+ : null,
638
+ h(
639
+ "button",
640
+ { onClick: () => disableSession(sid), title: "关闭该会话的 Agent 审批" },
641
+ "✕",
642
+ ),
643
+ );
644
+ }),
645
+ ),
646
+ ),
647
+ h(
648
+ "div",
649
+ { className: "aapr-card" },
650
+ h("h3", null, "放行 / 拒绝规则"),
651
+ h(
652
+ "div",
653
+ { className: "aapr-muted" },
654
+ "命中规则的提权不再经过审批模型:拒绝规则直接拒、放行规则直接过(拒绝优先于放行)。match 留空 = 该工具全部调用;否则是参数 JSON 的子串,或 /正则/flags 形式。另:模型批准后,同一会话内参数完全相同的再次提权直接放行(会话内信任,不跨会话、不泛化)。",
655
+ ),
656
+ rules.length === 0
657
+ ? h("div", { className: "aapr-muted" }, "暂无规则。")
658
+ : rules.map((rule) =>
659
+ h(
660
+ "div",
661
+ { key: rule.id, className: "aapr-row" },
662
+ h(
663
+ "span",
664
+ { className: rule.effect === "allow" ? "aapr-ok" : "aapr-no" },
665
+ rule.effect === "allow" ? "放行" : "拒绝",
539
666
  ),
667
+ h("span", { className: "aapr-chip-id" }, String(rule.tool)),
668
+ rule.match !== ""
669
+ ? h("span", { className: "aapr-rule-match", title: String(rule.match) }, truncText(rule.match, 60))
670
+ : h("span", { className: "aapr-muted" }, "(全部调用)"),
671
+ rule.note !== ""
672
+ ? h("span", { className: "aapr-muted" }, truncText(rule.note, 40))
673
+ : null,
674
+ h("button", { className: "aapr-rule-del", onClick: () => removeRule(rule.id), title: "删除该规则" }, "✕"),
540
675
  ),
676
+ ),
677
+ h(
678
+ "div",
679
+ { className: "aapr-row" },
680
+ h(
681
+ "select",
682
+ { className: "aapr-select", value: ruleEffect, onChange: (e) => setRuleEffect(e.target.value) },
683
+ h("option", { value: "allow" }, "放行"),
684
+ h("option", { value: "deny" }, "拒绝"),
685
+ ),
686
+ h("input", { className: "aapr-input", placeholder: "工具名(* = 所有工具)", value: ruleTool, onChange: (e) => setRuleTool(e.target.value) }),
687
+ h("input", { className: "aapr-input aapr-input-wide", placeholder: "match:留空 = 全部;子串或 /正则/flags", value: ruleMatch, onChange: (e) => setRuleMatch(e.target.value) }),
688
+ h("input", { className: "aapr-input", placeholder: "备注(可选)", value: ruleNote, onChange: (e) => setRuleNote(e.target.value) }),
689
+ h(ui.Button, { variant: "primary", size: "sm", onClick: submitRule }, "添加"),
690
+ ),
541
691
  ),
542
692
  h(
543
693
  "div",
@@ -562,7 +712,7 @@ window.__ModuleLoader__.load({
562
712
  h(
563
713
  "tr",
564
714
  null,
565
- ["时间", "会话", "工具", "结果", "风险", "模型", "耗时", "审批理由"].map((t) => h("th", { key: t }, t)),
715
+ ["时间", "会话", "工具", "结果", "风险", "模型", "耗时", "审批理由", "规则"].map((t) => h("th", { key: t }, t)),
566
716
  ),
567
717
  ),
568
718
  h(
@@ -591,16 +741,31 @@ window.__ModuleLoader__.load({
591
741
  },
592
742
  truncText(r.rationale, 110),
593
743
  ),
744
+ h(
745
+ "td",
746
+ null,
747
+ r.outcome === "allowed-once" && r.args && r.model !== "rule"
748
+ ? h(
749
+ "button",
750
+ {
751
+ className: "aapr-whitelist",
752
+ onClick: () => whitelistRecord(r),
753
+ title: "把该操作存为放行规则(工具 + 参数子串):今后直接放行,不再经过审批模型",
754
+ },
755
+ "加白",
756
+ )
757
+ : null,
758
+ ),
594
759
  ),
595
760
  )
596
- : h("tr", null, h("td", { colSpan: 8 }, h("span", { className: "aapr-muted" }, "暂无记录"))),
761
+ : h("tr", null, h("td", { colSpan: 9 }, h("span", { className: "aapr-muted" }, "暂无记录"))),
597
762
  ),
598
763
  ),
599
764
  ),
600
765
  h(
601
766
  "div",
602
767
  { className: "aapr-muted" },
603
- "悬停“审批理由”可查看完整理由与工具参数;“审批会话”前缀可在会话列表中找到审批 Agent 的完整会话记录。",
768
+ "悬停“审批理由”可查看完整理由与工具参数;“审批会话”前缀可在会话列表中找到审批 Agent 的完整会话记录。「加白」把一条已批准的操作存为放行规则(模型列显示 rule/trust 的行分别来自规则命中与会话内信任缓存)。",
604
769
  ),
605
770
  ),
606
771
  );
package/index.js CHANGED
@@ -18,15 +18,19 @@
18
18
  * 2. JUDGE — this service claims the `approval/request` waterfall with
19
19
  * `{ prepend: true }`, so it runs BEFORE the interactive UI answerer:
20
20
  * an enabled session never pops a human prompt. Every escalation ask is
21
- * routed to a ONE-SHOT `spawn` subagent (own session, zero parent
21
+ * first checked against the deterministic rule table (persisted
22
+ * allow/deny rules; deny wins) and then the per-session trust cache (a
23
+ * judge-approved, byte-identical call is not re-judged) — both
24
+ * short-circuit with zero model cost. Only a miss on both is routed to
25
+ * a ONE-SHOT `spawn` subagent (own session, zero parent
22
26
  * context, approval policy pinned to `never` by the delegation itself,
23
27
  * every global tool blanked via `toolFilter: { allow: [] }`) that must
24
28
  * answer through a structured-output schema:
25
29
  * { decision: approve|reject, riskLevel, rationale }
26
30
  * The judge sees the exact tool arguments (read from the session log by
27
- * `callId`) plus the asker's stated reason, and is instructed to fail
28
- * closed: destructive / irreversible / out-of-scope / dishonest requests
29
- * are rejected.
31
+ * `callId`) plus the asker's stated reason. A rejection must name the
32
+ * concrete, credible risk the operation creates (destructive /
33
+ * irreversible / out-of-scope / dishonest); vague unease is approved.
30
34
  *
31
35
  * 3. FAIL CLOSED — any infrastructure fault, timeout, malformed verdict, or
32
36
  * cancellation maps to the fail-closed approval outcomes
@@ -105,7 +109,7 @@ const VERDICT_SCHEMA = {
105
109
  const APPROVER_PERSONA = [
106
110
  "You are an independent security approval agent inside a coding harness.",
107
111
  "Your only job is to judge ONE request for wider sandbox access and report the verdict through the structured_output tool.",
108
- "You are conservative and fail closed: when uncertain, when the operation is destructive or irreversible, when it reaches outside its stated purpose, or when the stated justification does not match the actual arguments, you REJECT.",
112
+ "You reject what is concretely dangerous — destructive or irreversible operations, ones that reach outside their stated purpose, or requests whose stated justification does not match the actual arguments. Mere uncertainty, an unfamiliar command, or a terse justification is never enough: every rejection must name the concrete risk the operation creates.",
109
113
  "Your own judging session is deliberately sandboxed: approvals are disabled for YOU and your permission scope is fixed. That describes only your own environment — never cite your own constraints (or anything your runtime context says about YOUR permissions) as a property of the requesting session or as grounds for rejection.",
110
114
  "You never ask questions, never attempt the operation yourself, and never finish with a plain-text answer.",
111
115
  ].join(" ");
@@ -184,6 +188,8 @@ export class AgentApprovalService extends TypertRemoteService {
184
188
  markRemoteMethod(this, "setModel", "setModel");
185
189
  markRemoteMethod(this, "setApprovalTimeout", "setApprovalTimeout");
186
190
  markRemoteMethod(this, "toggle", "toggle");
191
+ markRemoteMethod(this, "addRule", "addRule");
192
+ markRemoteMethod(this, "removeRule", "removeRule");
187
193
  markRemoteMethod(this, "clearRecords", "clearRecords");
188
194
  markRemoteMethod(this, "directory", "directory");
189
195
 
@@ -195,6 +201,19 @@ export class AgentApprovalService extends TypertRemoteService {
195
201
  this._enabled = new Map();
196
202
  /** Audit records, oldest first, capped at MAX_RECORDS. */
197
203
  this._records = [];
204
+ /**
205
+ * Deterministic rules judged BEFORE the model (persisted in config.json):
206
+ * [{ id, effect: "allow"|"deny", tool, match, note, createdAt }]. A hit
207
+ * short-circuits the judge entirely — no subagent, no latency.
208
+ */
209
+ this._rules = [];
210
+ /**
211
+ * sessionId -> Set of "toolName\nargsJson" fingerprints the judge already
212
+ * approved in that session. Re-judging a byte-identical call is pure
213
+ * latency; the cache never crosses sessions and never generalizes to
214
+ * merely-similar arguments. Dropped with the session's enable entry.
215
+ */
216
+ this._trusted = new Map();
198
217
 
199
218
  // Claim escalations BEFORE the interactive answerer. The host apiproxy
200
219
  // answerer registered earlier (composition load order); `{ prepend: true }`
@@ -222,6 +241,7 @@ export class AgentApprovalService extends TypertRemoteService {
222
241
  this._enableCore(session, agent);
223
242
  } else if (this._enabled.has(session.id)) {
224
243
  this._enabled.delete(session.id);
244
+ this._trusted.delete(session.id);
225
245
  }
226
246
  } catch (e) {
227
247
  /* an emit listener must never throw */
@@ -248,7 +268,10 @@ export class AgentApprovalService extends TypertRemoteService {
248
268
  // A disposed session's bookkeeping entry is dead weight — drop it.
249
269
  this.ctx.on("session/disposed", (session) => {
250
270
  try {
251
- if (session) this._enabled.delete(session.id);
271
+ if (session) {
272
+ this._enabled.delete(session.id);
273
+ this._trusted.delete(session.id);
274
+ }
252
275
  } catch (e) {
253
276
  /* cleanup only */
254
277
  }
@@ -410,6 +433,7 @@ export class AgentApprovalService extends TypertRemoteService {
410
433
  const prev = this._enabled.get(session.id);
411
434
  if (prev === undefined) return "agent-approval is not ON for this session";
412
435
  this._enabled.delete(session.id);
436
+ this._trusted.delete(session.id);
413
437
  if (!restoreKnobs) return "agent-approval OFF: previous permission knobs restored";
414
438
  if (
415
439
  typeof prev.prevSandbox === "string" &&
@@ -457,6 +481,64 @@ export class AgentApprovalService extends TypertRemoteService {
457
481
  }
458
482
  }
459
483
 
484
+ // ---- deterministic rules + session trust ----------------------------------
485
+
486
+ /**
487
+ * Compile a rule's `match`: "" matches every call of the tool;
488
+ * "/pattern/flags" is a regex; anything else is a plain substring tested
489
+ * against the raw arguments JSON. Returns undefined for the substring form,
490
+ * null for an invalid regex (rejected at add time; a corrupt persisted rule
491
+ * simply never matches).
492
+ */
493
+ _ruleRegex(match) {
494
+ if (match.length < 2 || match[0] !== "/") return undefined;
495
+ const last = match.lastIndexOf("/");
496
+ if (last <= 0) return undefined;
497
+ try {
498
+ return new RegExp(match.slice(1, last), match.slice(last + 1));
499
+ } catch (e) {
500
+ return null;
501
+ }
502
+ }
503
+
504
+ /** Whether one rule hits this exact call. */
505
+ _ruleMatches(rule, toolName, argsRaw) {
506
+ if (rule.tool !== "*" && rule.tool !== toolName) return false;
507
+ if (rule.match === "") return true;
508
+ const args = typeof argsRaw === "string" ? argsRaw : "";
509
+ const re = this._ruleRegex(rule.match);
510
+ if (re === null) return false;
511
+ if (re !== undefined) return re.test(args);
512
+ return args.indexOf(rule.match) !== -1;
513
+ }
514
+
515
+ /**
516
+ * First hitting rule — every deny rule is evaluated before any allow rule
517
+ * may win, so a later-added deny always overrides an earlier allow.
518
+ * Undefined when nothing hits.
519
+ */
520
+ _matchRules(toolName, argsRaw) {
521
+ let allowHit;
522
+ for (const rule of this._rules) {
523
+ if (!this._ruleMatches(rule, toolName, argsRaw)) continue;
524
+ if (rule.effect === "deny") return rule;
525
+ if (allowHit === undefined) allowHit = rule;
526
+ }
527
+ return allowHit;
528
+ }
529
+
530
+ /** Owned plain copies for the wire (strict result schema). */
531
+ _rulesSnapshot() {
532
+ return this._rules.map((r) => ({
533
+ id: String(r.id),
534
+ effect: r.effect === "deny" ? "deny" : "allow",
535
+ tool: String(r.tool),
536
+ match: String(r.match),
537
+ note: String(r.note),
538
+ createdAt: String(r.createdAt),
539
+ }));
540
+ }
541
+
460
542
  // ---- audit ----------------------------------------------------------------
461
543
 
462
544
  /** Coerce one entry to the strict wire shape (typert result schema). */
@@ -501,11 +583,12 @@ export class AgentApprovalService extends TypertRemoteService {
501
583
  }
502
584
  }
503
585
 
504
- /** Persist the judge settings (model override + timeout) to config.json. */
586
+ /** Persist the judge settings (model override + timeout + rules) to config.json. */
505
587
  _persistConfig() {
506
588
  const body = JSON.stringify({
507
589
  model: { provider: this._model.provider, model: this._model.model },
508
590
  timeoutMs: this._timeoutMs,
591
+ rules: this._rules,
509
592
  });
510
593
  mkdir(DATA_DIR, { recursive: true })
511
594
  .then(() => writeFile(CONFIG_FILE, body, "utf8"))
@@ -533,6 +616,27 @@ export class AgentApprovalService extends TypertRemoteService {
533
616
  if (typeof cfg.timeoutMs === "number" && Number.isFinite(cfg.timeoutMs)) {
534
617
  this._timeoutMs = Math.min(MAX_TIMEOUT_MS, Math.max(MIN_TIMEOUT_MS, Math.floor(cfg.timeoutMs)));
535
618
  }
619
+ if (Array.isArray(cfg.rules)) {
620
+ const rules = [];
621
+ for (const raw of cfg.rules) {
622
+ if (!raw || typeof raw !== "object") continue;
623
+ if (raw.effect !== "allow" && raw.effect !== "deny") continue;
624
+ if (typeof raw.tool !== "string" || raw.tool === "") continue;
625
+ if (typeof raw.match !== "string") continue;
626
+ rules.push({
627
+ id:
628
+ typeof raw.id === "string" && raw.id !== ""
629
+ ? raw.id
630
+ : Date.now().toString(36) + Math.random().toString(36).slice(2, 8),
631
+ effect: raw.effect,
632
+ tool: raw.tool,
633
+ match: raw.match,
634
+ note: typeof raw.note === "string" ? raw.note : "",
635
+ createdAt: typeof raw.createdAt === "string" ? raw.createdAt : new Date().toISOString(),
636
+ });
637
+ }
638
+ this._rules = rules;
639
+ }
536
640
  }
537
641
  } catch (e) {
538
642
  /* first run or unreadable config — keep the defaults */
@@ -651,7 +755,7 @@ export class AgentApprovalService extends TypertRemoteService {
651
755
  "- reading tool-owned config or logs needed to debug the task at hand.",
652
756
  "REJECT when the operation is destructive (mass deletion, disk formatting, registry/service/system-wide changes), exfiltrates credentials or secrets, touches resources unrelated to the task, modifies the operating system or OTHER applications' data, hides intent behind encoded or obfuscated content, or the reason does not match the arguments.",
653
757
  "Your own judging session is deliberately sandboxed: approvals are disabled for YOU and your permission scope is fixed by design. Anything your own runtime context says about YOUR permissions describes only you — it says nothing about the requesting session, and must never be cited as a property of that session or as grounds for rejection.",
654
- "When uncertain, REJECT. Report the verdict via the structured_output tool only.",
758
+ "REJECT only when you can name a concrete, credible risk THIS specific operation creates — what it would destroy, leak, or change beyond the user's task. Vague unease, an unfamiliar command, or a terse stated reason is NOT a concrete risk: when no concrete risk exists and the operation fits the task, APPROVE. Report the verdict via the structured_output tool only.",
655
759
  );
656
760
  return lines.join("\n");
657
761
  }
@@ -733,18 +837,47 @@ export class AgentApprovalService extends TypertRemoteService {
733
837
  const startedAt = new Date().toISOString();
734
838
  const t0 = Date.now();
735
839
  const argsRaw = this._callArgsOf(session, req.callId);
736
- const route = this._judgeRoute();
840
+ const toolName = String(req.toolName);
737
841
  const base = {
738
842
  at: startedAt,
739
843
  sessionId: shortId(session.id),
740
- toolName: String(req.toolName),
844
+ toolName: toolName,
741
845
  reason: trunc(req.reason, 300),
742
846
  args: trunc(argsRaw, 2000),
743
- model: route.label,
744
847
  durationMs: 0,
745
848
  childSessionId: "",
746
849
  };
747
850
 
851
+ // 1. Deterministic rules run BEFORE the model — zero latency, zero cost.
852
+ // Deny beats allow (see _matchRules); both are recorded for audit.
853
+ const rule = this._matchRules(toolName, argsRaw);
854
+ if (rule !== undefined) {
855
+ const text =
856
+ (rule.effect === "deny" ? "matched deny rule" : "matched allow rule") +
857
+ " [tool=" + rule.tool + (rule.match !== "" ? " match=" + rule.match : "") + "]" +
858
+ (rule.note !== "" ? " — " + rule.note : "");
859
+ base.durationMs = Date.now() - t0;
860
+ if (rule.effect === "deny") {
861
+ this._record({ ...base, outcome: "rejected", riskLevel: "-", model: "rule", rationale: trunc(text, 600) });
862
+ return "rejected";
863
+ }
864
+ this._record({ ...base, outcome: "allowed-once", riskLevel: "-", model: "rule", rationale: trunc(text, 600) });
865
+ return "allowed-once";
866
+ }
867
+
868
+ // 2. Session trust: a byte-identical call (same tool, same arguments JSON)
869
+ // the judge already approved in this session is not re-judged.
870
+ const trustKey = typeof argsRaw === "string" ? toolName + "\n" + argsRaw : undefined;
871
+ const trusted = this._trusted.get(session.id);
872
+ if (trustKey !== undefined && trusted !== undefined && trusted.has(trustKey)) {
873
+ base.durationMs = Date.now() - t0;
874
+ this._record({ ...base, outcome: "allowed-once", riskLevel: "-", model: "trust", rationale: "trusted: an identical operation was already approved in this session" });
875
+ return "allowed-once";
876
+ }
877
+
878
+ // 3. The model judge.
879
+ const route = this._judgeRoute();
880
+
748
881
  let run;
749
882
  try {
750
883
  run = await this.ctx.subagents.start("spawn", {
@@ -760,7 +893,7 @@ export class AgentApprovalService extends TypertRemoteService {
760
893
  persona: APPROVER_PERSONA,
761
894
  });
762
895
  } catch (error) {
763
- this._record({ ...base, outcome: "unavailable", riskLevel: "-", rationale: "approval agent failed to start: " + errText(error) });
896
+ this._record({ ...base, outcome: "unavailable", riskLevel: "-", model: route.label, rationale: "approval agent failed to start: " + errText(error) });
764
897
  return "unavailable";
765
898
  }
766
899
  base.childSessionId = shortId(run.id);
@@ -801,21 +934,33 @@ export class AgentApprovalService extends TypertRemoteService {
801
934
  ...base,
802
935
  outcome: approved ? "allowed-once" : "rejected",
803
936
  riskLevel: String(verdict.riskLevel || "-"),
937
+ model: route.label,
804
938
  rationale: trunc(verdict.rationale, 600),
805
939
  });
940
+ // Trust one approved fingerprint for the rest of the session: the
941
+ // next byte-identical call short-circuits before the model.
942
+ if (approved && trustKey !== undefined) {
943
+ let set = this._trusted.get(session.id);
944
+ if (set === undefined) {
945
+ set = new Set();
946
+ this._trusted.set(session.id, set);
947
+ }
948
+ set.add(trustKey);
949
+ }
806
950
  return approved ? "allowed-once" : "rejected";
807
951
  }
808
952
  this._record({
809
953
  ...base,
810
954
  outcome: "unavailable",
811
955
  riskLevel: "-",
956
+ model: route.label,
812
957
  rationale:
813
958
  "approval agent returned no valid verdict (stopReason: " + String(result.stopReason) + ")",
814
959
  });
815
960
  return "unavailable";
816
961
  }
817
962
  if (winner.kind === "aborted") {
818
- this._record({ ...base, outcome: "cancelled", riskLevel: "-", rationale: "request cancelled while the approval agent was judging" });
963
+ this._record({ ...base, outcome: "cancelled", riskLevel: "-", model: route.label, rationale: "request cancelled while the approval agent was judging" });
819
964
  return "cancelled";
820
965
  }
821
966
  if (winner.kind === "timeout") {
@@ -823,16 +968,50 @@ export class AgentApprovalService extends TypertRemoteService {
823
968
  ...base,
824
969
  outcome: "unavailable",
825
970
  riskLevel: "-",
971
+ model: route.label,
826
972
  rationale: "approval agent timed out after " + String(this._timeoutMs) + "ms (fail closed)",
827
973
  });
828
974
  return "unavailable";
829
975
  }
830
- this._record({ ...base, outcome: "unavailable", riskLevel: "-", rationale: "approval agent infrastructure fault: " + errText(winner.error) });
976
+ this._record({ ...base, outcome: "unavailable", riskLevel: "-", model: route.label, rationale: "approval agent infrastructure fault: " + errText(winner.error) });
831
977
  return "unavailable";
832
978
  }
833
979
 
834
980
  // ---- Remote API ------------------------------------------------------------
835
981
 
982
+ /**
983
+ * Display info for every enabled session: the same log-backed title the
984
+ * session list shows (via the optional `sessionTitle` service) plus the
985
+ * workspace cwd, so the Settings chips are recognizable. Every read is a
986
+ * best-effort leaf read on owned plain objects; a disposed agent, an absent
987
+ * title service, or a missing header field degrades to "".
988
+ */
989
+ _sessionInfos() {
990
+ const titles = this.ctx.get("sessionTitle");
991
+ const out = [];
992
+ for (const sid of this._enabled.keys()) {
993
+ let title = "";
994
+ let cwd = "";
995
+ const agent = this.ctx.agents.get(sid);
996
+ const session = agent === undefined ? undefined : agent.session;
997
+ if (session !== undefined) {
998
+ try {
999
+ const snap = titles === undefined ? undefined : titles.get(session);
1000
+ if (snap && typeof snap.title === "string") title = snap.title;
1001
+ } catch (e) {
1002
+ /* title read is best-effort */
1003
+ }
1004
+ try {
1005
+ if (session.header && typeof session.header.cwd === "string") cwd = session.header.cwd;
1006
+ } catch (e) {
1007
+ /* header access is best-effort */
1008
+ }
1009
+ }
1010
+ out.push({ id: String(sid), title: title, cwd: cwd });
1011
+ }
1012
+ return out;
1013
+ }
1014
+
836
1015
  /** Snapshot for the Settings page. */
837
1016
  async getState() {
838
1017
  return {
@@ -840,7 +1019,8 @@ export class AgentApprovalService extends TypertRemoteService {
840
1019
  value: {
841
1020
  model: { provider: this._model.provider, model: this._model.model },
842
1021
  timeoutMs: this._timeoutMs,
843
- enabledSessions: Array.from(this._enabled.keys()).map(String),
1022
+ enabledSessions: this._sessionInfos(),
1023
+ rules: this._rulesSnapshot(),
844
1024
  records: this._records.slice(-50).reverse(),
845
1025
  },
846
1026
  };
@@ -885,6 +1065,47 @@ export class AgentApprovalService extends TypertRemoteService {
885
1065
  return { ok: true, value: { message: this._setEnabled(agent, want) } };
886
1066
  }
887
1067
 
1068
+ /**
1069
+ * Add one deterministic rule and persist the table. `tool` is an exact tool
1070
+ * name or "*" for every tool; `match` is "" (every call of that tool), a
1071
+ * plain substring, or "/pattern/flags" tested against the raw arguments
1072
+ * JSON. Returns the full table.
1073
+ */
1074
+ async addRule(request) {
1075
+ const effect = request && request.effect === "deny" ? "deny" : "allow";
1076
+ const tool = request && typeof request.tool === "string" ? request.tool.trim() : "";
1077
+ const match = request && typeof request.match === "string" ? request.match : "";
1078
+ const note = request && typeof request.note === "string" ? trunc(request.note, 200) : "";
1079
+ if (tool === "") {
1080
+ return { ok: false, error: { code: "invalid-rule", message: 'tool is required ("*" matches every tool)' } };
1081
+ }
1082
+ if (this._ruleRegex(match) === null) {
1083
+ return { ok: false, error: { code: "invalid-rule", message: "invalid /regex/flags match expression" } };
1084
+ }
1085
+ this._rules.push({
1086
+ id: Date.now().toString(36) + Math.random().toString(36).slice(2, 8),
1087
+ effect: effect,
1088
+ tool: tool,
1089
+ match: match,
1090
+ note: note,
1091
+ createdAt: new Date().toISOString(),
1092
+ });
1093
+ this._persistConfig();
1094
+ return { ok: true, value: { rules: this._rulesSnapshot() } };
1095
+ }
1096
+
1097
+ /** Remove one rule by id and persist the table. Returns the full table. */
1098
+ async removeRule(request) {
1099
+ const id = request && typeof request.id === "string" ? request.id : "";
1100
+ const before = this._rules.length;
1101
+ this._rules = this._rules.filter((r) => r.id !== id);
1102
+ if (this._rules.length === before) {
1103
+ return { ok: false, error: { code: "rule-not-found", message: "no rule with that id" } };
1104
+ }
1105
+ this._persistConfig();
1106
+ return { ok: true, value: { rules: this._rulesSnapshot() } };
1107
+ }
1108
+
888
1109
  /** Clear the audit records (memory + persisted file). */
889
1110
  async clearRecords() {
890
1111
  this._records = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duke-dsh-plugins/dsh-agent-approval",
3
- "version": "1.3.5",
3
+ "version": "1.4.0",
4
4
  "description": "Agent-decided approvals for DeepSeek Harness: a workspace-write base permission mode where an independent approval subagent judges every sandbox escalation (risky operations are rejected), with a configurable approval model and an audit log in Settings.",
5
5
  "repository": {
6
6
  "type": "git",
package/typert.host.js CHANGED
@@ -41,11 +41,31 @@ const modelRouteSchema = z
41
41
  })
42
42
  .readonly();
43
43
 
44
+ const enabledSessionSchema = z
45
+ .object({
46
+ id: z.string(),
47
+ title: z.string(),
48
+ cwd: z.string(),
49
+ })
50
+ .readonly();
51
+
52
+ const ruleSchema = z
53
+ .object({
54
+ id: z.string(),
55
+ effect: z.enum(["allow", "deny"]),
56
+ tool: z.string(),
57
+ match: z.string(),
58
+ note: z.string(),
59
+ createdAt: z.string(),
60
+ })
61
+ .readonly();
62
+
44
63
  const stateValueSchema = z
45
64
  .object({
46
65
  model: modelRouteSchema,
47
66
  timeoutMs: z.number(),
48
- enabledSessions: z.array(z.string()).readonly(),
67
+ enabledSessions: z.array(enabledSessionSchema).readonly(),
68
+ rules: z.array(ruleSchema).readonly(),
49
69
  records: z.array(recordSchema).readonly(),
50
70
  })
51
71
  .readonly();
@@ -68,6 +88,12 @@ const toggleValueSchema = z
68
88
  })
69
89
  .readonly();
70
90
 
91
+ const rulesValueSchema = z
92
+ .object({
93
+ rules: z.array(ruleSchema).readonly(),
94
+ })
95
+ .readonly();
96
+
71
97
  const clearRecordsValueSchema = z
72
98
  .object({
73
99
  cleared: z.boolean(),
@@ -128,6 +154,8 @@ const stateResultSchema = okResult(stateValueSchema);
128
154
  const setModelResultSchema = okResult(setModelValueSchema);
129
155
  const setTimeoutResultSchema = okResult(setTimeoutValueSchema);
130
156
  const toggleResultSchema = okResult(toggleValueSchema);
157
+ const addRuleResultSchema = okResult(rulesValueSchema);
158
+ const removeRuleResultSchema = okResult(rulesValueSchema);
131
159
  const clearRecordsResultSchema = okResult(clearRecordsValueSchema);
132
160
  const directoryResultSchema = okResult(directoryValueSchema);
133
161
 
@@ -147,6 +175,17 @@ const _agentApproval_toggle_parameter_0$schema = z.object({
147
175
  on: z.boolean(),
148
176
  });
149
177
 
178
+ const _agentApproval_addRule_parameter_0$schema = z.object({
179
+ effect: z.enum(["allow", "deny"]),
180
+ tool: z.string(),
181
+ match: z.string(),
182
+ note: z.string(),
183
+ });
184
+
185
+ const _agentApproval_removeRule_parameter_0$schema = z.object({
186
+ id: z.string(),
187
+ });
188
+
150
189
  export const TYPERT = {
151
190
  package: "@duke-dsh-plugins/dsh-agent-approval",
152
191
  face: "host",
@@ -241,6 +280,56 @@ export const TYPERT = {
241
280
  },
242
281
  sourceLocation: { file: "index.js", line: 1, column: 1 },
243
282
  },
283
+ {
284
+ id: "dsh-agent-approval#agentApproval/addRule",
285
+ service: "agentApproval",
286
+ namespace: "agentApproval",
287
+ method: "addRule",
288
+ invocation: { kind: "direct" },
289
+ parameters: [
290
+ {
291
+ name: "request",
292
+ wire: "request",
293
+ source: "json",
294
+ codec: {
295
+ mode: "strict",
296
+ typeSymbol: "dsh-agent-approval#AgentApprovalAddRuleRequest",
297
+ schema: _agentApproval_addRule_parameter_0$schema,
298
+ },
299
+ },
300
+ ],
301
+ result: {
302
+ mode: "strict",
303
+ typeSymbol: "dsh-agent-approval#AgentApprovalRulesResult",
304
+ schema: addRuleResultSchema,
305
+ },
306
+ sourceLocation: { file: "index.js", line: 1, column: 1 },
307
+ },
308
+ {
309
+ id: "dsh-agent-approval#agentApproval/removeRule",
310
+ service: "agentApproval",
311
+ namespace: "agentApproval",
312
+ method: "removeRule",
313
+ invocation: { kind: "direct" },
314
+ parameters: [
315
+ {
316
+ name: "request",
317
+ wire: "request",
318
+ source: "json",
319
+ codec: {
320
+ mode: "strict",
321
+ typeSymbol: "dsh-agent-approval#AgentApprovalRemoveRuleRequest",
322
+ schema: _agentApproval_removeRule_parameter_0$schema,
323
+ },
324
+ },
325
+ ],
326
+ result: {
327
+ mode: "strict",
328
+ typeSymbol: "dsh-agent-approval#AgentApprovalRulesResult",
329
+ schema: removeRuleResultSchema,
330
+ },
331
+ sourceLocation: { file: "index.js", line: 1, column: 1 },
332
+ },
244
333
  {
245
334
  id: "dsh-agent-approval#agentApproval/clearRecords",
246
335
  service: "agentApproval",
@@ -288,7 +377,7 @@ export const TYPERT = {
288
377
  signature: "@Remote('getState') async getState(): Promise<AgentApprovalStateResult>",
289
378
  summary: "Snapshot for the Settings page and the composer toggle.",
290
379
  jsDoc:
291
- "/**\n * Return the judge model route, timeout, enabled session ids, and the latest audit records.\n * @returns success or a business failure.\n */",
380
+ "/**\n * Return the judge model route, timeout, enabled sessions (id + session-list title + workspace cwd), and the latest audit records.\n * @returns success or a business failure.\n */",
292
381
  },
293
382
  {
294
383
  kind: "method",
@@ -314,6 +403,22 @@ export const TYPERT = {
314
403
  jsDoc:
315
404
  "/**\n * Enable (workspace-write base + judged escalations) or disable (restore prior knobs) for one live session.\n * @param request - { sessionId, on }.\n * @returns a human-readable message, or session-not-live.\n */",
316
405
  },
406
+ {
407
+ kind: "method",
408
+ name: "addRule",
409
+ signature: "@Remote('addRule') async addRule(request: AgentApprovalAddRuleRequest): Promise<AgentApprovalRulesResult>",
410
+ summary: "Add a deterministic allow/deny rule judged before the model (persisted).",
411
+ jsDoc:
412
+ "/**\n * Add one rule: tool = exact name or \"*\"; match = \"\" (every call), substring, or /regex/flags over the raw arguments JSON. Deny is evaluated before allow.\n * @param request - { effect, tool, match, note }.\n * @returns the full rule table.\n */",
413
+ },
414
+ {
415
+ kind: "method",
416
+ name: "removeRule",
417
+ signature: "@Remote('removeRule') async removeRule(request: AgentApprovalRemoveRuleRequest): Promise<AgentApprovalRulesResult>",
418
+ summary: "Remove one rule by id (persisted).",
419
+ jsDoc:
420
+ "/**\n * Remove one rule by id.\n * @param request - { id }.\n * @returns the full rule table.\n */",
421
+ },
317
422
  {
318
423
  kind: "method",
319
424
  name: "clearRecords",
@@ -342,10 +447,25 @@ export const TYPERT = {
342
447
  declaration:
343
448
  "export interface AgentApprovalModelRoute {\n readonly provider: string;\n readonly model: string;\n}",
344
449
  },
450
+ {
451
+ name: "AgentApprovalEnabledSession",
452
+ declaration:
453
+ "export interface AgentApprovalEnabledSession {\n readonly id: string;\n readonly title: string;\n readonly cwd: string;\n}",
454
+ },
455
+ {
456
+ name: "AgentApprovalRule",
457
+ declaration:
458
+ "export interface AgentApprovalRule {\n readonly id: string;\n readonly effect: 'allow' | 'deny';\n readonly tool: string;\n readonly match: string;\n readonly note: string;\n readonly createdAt: string;\n}",
459
+ },
460
+ {
461
+ name: "AgentApprovalRulesResult",
462
+ declaration:
463
+ "export type AgentApprovalRulesResult = { ok: true; value: { readonly rules: readonly AgentApprovalRule[] } } | { ok: false; error: { code: string; message?: string } };",
464
+ },
345
465
  {
346
466
  name: "AgentApprovalStateValue",
347
467
  declaration:
348
- "export interface AgentApprovalStateValue {\n readonly model: AgentApprovalModelRoute;\n readonly timeoutMs: number;\n readonly enabledSessions: readonly string[];\n readonly records: readonly AgentApprovalRecord[];\n}",
468
+ "export interface AgentApprovalStateValue {\n readonly model: AgentApprovalModelRoute;\n readonly timeoutMs: number;\n readonly enabledSessions: readonly AgentApprovalEnabledSession[];\n readonly rules: readonly AgentApprovalRule[];\n readonly records: readonly AgentApprovalRecord[];\n}",
349
469
  },
350
470
  {
351
471
  name: "AgentApprovalStateResult",