@pugi/cli 0.1.0-beta.18 → 0.1.0-beta.19
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/dist/core/compact/auto-trigger.js +96 -0
- package/dist/core/compact/buffer-rewriter.js +115 -0
- package/dist/core/compact/summarizer.js +196 -0
- package/dist/core/compact/token-counter.js +108 -0
- package/dist/core/denial-tracking/index.js +8 -0
- package/dist/core/denial-tracking/state.js +264 -0
- package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
- package/dist/core/diagnostics/probes/status-snapshot.js +442 -0
- package/dist/core/engine/native-pugi.js +20 -0
- package/dist/core/engine/tool-bridge.js +153 -14
- package/dist/core/permissions/gate.js +187 -0
- package/dist/core/permissions/index.js +18 -0
- package/dist/core/permissions/mode.js +102 -0
- package/dist/core/permissions/state.js +160 -0
- package/dist/core/permissions/tool-class.js +93 -0
- package/dist/core/repl/session.js +227 -9
- package/dist/core/repl/slash-commands.js +58 -4
- package/dist/runtime/cli.js +129 -0
- package/dist/runtime/commands/compact.js +296 -0
- package/dist/runtime/commands/doctor.js +12 -0
- package/dist/runtime/commands/permissions.js +87 -0
- package/dist/runtime/commands/status.js +178 -0
- package/dist/runtime/version.js +1 -1
- package/dist/tui/compact-banner.js +54 -0
- package/dist/tui/status-table.js +7 -0
- package/package.json +2 -2
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* α7 L11 (2026-05-27) — DenialTrackingState surface (Claude Code parity).
|
|
3
|
+
*
|
|
4
|
+
* Per the openclaude / anarchic-CC leak research (`gap-analysis-3-repos`
|
|
5
|
+
* §5.2): Claude Code's `QueryEngine.ts` maintains a per-session
|
|
6
|
+
* `DenialTrackingState` that records every tool-dispatch denial.
|
|
7
|
+
* Subsequent turns receive a compact reminder so the model does not
|
|
8
|
+
* loop on the same refused operation, the operator can audit pressure
|
|
9
|
+
* points via `/permissions denials` / `/doctor`, and hooks can react
|
|
10
|
+
* to recurring patterns.
|
|
11
|
+
*
|
|
12
|
+
* Pugi pre-L11 surfaced denials only as a one-line sentinel inside the
|
|
13
|
+
* tool error (`HOOK_BLOCKED:`, `PLAN_MODE_REFUSED:`, `STALE_READ:`).
|
|
14
|
+
* The model saw each refusal in isolation; no aggregate, no count, no
|
|
15
|
+
* "do not retry" context across turns. This module closes that gap.
|
|
16
|
+
*
|
|
17
|
+
* Design contract:
|
|
18
|
+
*
|
|
19
|
+
* - Per-session in-memory state. The engine adapter creates one
|
|
20
|
+
* instance at session open and threads it through `buildExecutor`
|
|
21
|
+
* + `runEngineLoop`. No disk persistence — denial history is a
|
|
22
|
+
* turn-scoped signal, not a forensic log (the audit ledger at
|
|
23
|
+
* `.pugi/events.jsonl` already carries every refused dispatch).
|
|
24
|
+
*
|
|
25
|
+
* - Pure: no I/O, no logging. Inputs are passed by caller, output
|
|
26
|
+
* is in-memory only. The diagnostics probe + system-reminder
|
|
27
|
+
* splice are pure functions over the snapshot.
|
|
28
|
+
*
|
|
29
|
+
* - Canonical argHash: sha256 of `canonicalize(args)`. Same call
|
|
30
|
+
* to same tool with same args = same key. Reordering JSON object
|
|
31
|
+
* keys does NOT change the hash (we sort keys recursively before
|
|
32
|
+
* stringification). This makes "repeated denial" detection
|
|
33
|
+
* deterministic across model retries.
|
|
34
|
+
*
|
|
35
|
+
* - Bounded: the map caps at 256 entries per session. Once full,
|
|
36
|
+
* `recordDenial` evicts the oldest entry (lowest `lastDeniedAt`).
|
|
37
|
+
* A session with > 256 distinct denials is already pathological;
|
|
38
|
+
* the cap is defensive insurance against pathological loops
|
|
39
|
+
* blowing memory.
|
|
40
|
+
*
|
|
41
|
+
* - Reminder threshold: the reminder splice only injects when at
|
|
42
|
+
* least one record has `count >= 2`. One-off denials are normal
|
|
43
|
+
* (the operator is shaping the session); repeated denials = the
|
|
44
|
+
* model is failing to learn from the refusal and we should
|
|
45
|
+
* reinforce it.
|
|
46
|
+
*/
|
|
47
|
+
import { createHash } from 'node:crypto';
|
|
48
|
+
/** Bound on the per-session map. See module header. */
|
|
49
|
+
export const DENIAL_TRACKING_MAX_ENTRIES = 256;
|
|
50
|
+
/** Threshold above which `buildDenialContext` injects a system reminder. */
|
|
51
|
+
export const DENIAL_REMINDER_THRESHOLD = 2;
|
|
52
|
+
/** Per-denial-args summary cap surfaced to the model. */
|
|
53
|
+
export const DENIAL_ARGS_SUMMARY_BYTES = 240;
|
|
54
|
+
/**
|
|
55
|
+
* Per-session denial tracker. Instances are mutable but threaded
|
|
56
|
+
* through `buildExecutor` so every dispatcher in the loop sees the
|
|
57
|
+
* same view.
|
|
58
|
+
*/
|
|
59
|
+
export class DenialTrackingState {
|
|
60
|
+
/** Insertion-ordered map. Eviction walks oldest `lastDeniedAt`. */
|
|
61
|
+
records = new Map();
|
|
62
|
+
/**
|
|
63
|
+
* Wall clock. Defaults to `Date.now`. Injected so tests can drive
|
|
64
|
+
* deterministic timestamps.
|
|
65
|
+
*/
|
|
66
|
+
clock;
|
|
67
|
+
constructor(options = {}) {
|
|
68
|
+
this.clock = options.clock ?? Date.now;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Record a denial. Increments the count when the (tool, args) pair
|
|
72
|
+
* has been denied before; otherwise inserts a new record. The reason
|
|
73
|
+
* is always overwritten with the latest value so the system reminder
|
|
74
|
+
* surfaces the most-recent context.
|
|
75
|
+
*/
|
|
76
|
+
recordDenial(toolName, args, reason) {
|
|
77
|
+
const argHash = canonicalArgHash(args);
|
|
78
|
+
const key = `${toolName}:${argHash}`;
|
|
79
|
+
const now = new Date(this.clock());
|
|
80
|
+
const existing = this.records.get(key);
|
|
81
|
+
if (existing) {
|
|
82
|
+
existing.count += 1;
|
|
83
|
+
existing.lastDeniedAt = now;
|
|
84
|
+
existing.lastReason = truncateReason(reason);
|
|
85
|
+
// Re-insert so the iteration order tracks lastDeniedAt (LRU-ish).
|
|
86
|
+
this.records.delete(key);
|
|
87
|
+
this.records.set(key, existing);
|
|
88
|
+
return existing;
|
|
89
|
+
}
|
|
90
|
+
if (this.records.size >= DENIAL_TRACKING_MAX_ENTRIES) {
|
|
91
|
+
// Evict the oldest entry (insertion order = LRU after our delete
|
|
92
|
+
// + set pattern above). Map iteration order is insertion order
|
|
93
|
+
// in JS; the first key is the LRU one.
|
|
94
|
+
const oldestKey = this.records.keys().next().value;
|
|
95
|
+
if (oldestKey !== undefined) {
|
|
96
|
+
this.records.delete(oldestKey);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const record = {
|
|
100
|
+
key,
|
|
101
|
+
toolName,
|
|
102
|
+
argsSummary: summariseArgs(args),
|
|
103
|
+
firstDeniedAt: now,
|
|
104
|
+
lastDeniedAt: now,
|
|
105
|
+
count: 1,
|
|
106
|
+
lastReason: truncateReason(reason),
|
|
107
|
+
};
|
|
108
|
+
this.records.set(key, record);
|
|
109
|
+
return record;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Snapshot of every recorded denial. Returned in insertion order
|
|
113
|
+
* (LRU-ish: most-recently-touched at the tail). The caller MUST
|
|
114
|
+
* treat the array as read-only — mutating it does not affect state.
|
|
115
|
+
*/
|
|
116
|
+
getDenialHistory() {
|
|
117
|
+
return Array.from(this.records.values()).map((r) => ({ ...r }));
|
|
118
|
+
}
|
|
119
|
+
/** Lookup the record for a specific (tool, args) pair, or undefined. */
|
|
120
|
+
getPatternFor(toolName, args) {
|
|
121
|
+
const argHash = canonicalArgHash(args);
|
|
122
|
+
const key = `${toolName}:${argHash}`;
|
|
123
|
+
const record = this.records.get(key);
|
|
124
|
+
return record ? { ...record } : undefined;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Aggregate counts — handy for `/doctor`, `/permissions denials`,
|
|
128
|
+
* and the system-reminder threshold check.
|
|
129
|
+
*/
|
|
130
|
+
summary() {
|
|
131
|
+
let totalDenials = 0;
|
|
132
|
+
let repeatedPatterns = 0;
|
|
133
|
+
for (const record of this.records.values()) {
|
|
134
|
+
totalDenials += record.count;
|
|
135
|
+
if (record.count >= DENIAL_REMINDER_THRESHOLD)
|
|
136
|
+
repeatedPatterns += 1;
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
totalDenials,
|
|
140
|
+
uniquePatterns: this.records.size,
|
|
141
|
+
repeatedPatterns,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/** Drop every recorded denial. Called on session end. */
|
|
145
|
+
clear() {
|
|
146
|
+
this.records.clear();
|
|
147
|
+
}
|
|
148
|
+
/** Number of distinct (tool, args) pairs currently tracked. */
|
|
149
|
+
size() {
|
|
150
|
+
return this.records.size;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Build the operator-facing system-reminder block. Returns an empty
|
|
155
|
+
* string when no record meets the threshold — the caller short-circuits
|
|
156
|
+
* the splice in that case.
|
|
157
|
+
*
|
|
158
|
+
* Rendered shape:
|
|
159
|
+
*
|
|
160
|
+
* <denial-context>
|
|
161
|
+
* - Tool `bash` denied 3 times in this session.
|
|
162
|
+
* Last reason: HOOK_BLOCKED: PreToolUse refused (exit=1).
|
|
163
|
+
* Args: {"command":"rm -rf node_modules"}
|
|
164
|
+
* Do not retry without operator intervention.
|
|
165
|
+
* - Tool `edit` denied 2 times in this session.
|
|
166
|
+
* Last reason: STALE_READ: file changed since last read.
|
|
167
|
+
* Args: {"path":"src/index.ts","oldString":"..."}
|
|
168
|
+
* Do not retry without operator intervention.
|
|
169
|
+
* </denial-context>
|
|
170
|
+
*
|
|
171
|
+
* Determinism: same snapshot always produces the same block. We sort
|
|
172
|
+
* entries by `count` descending then `lastDeniedAt` descending so the
|
|
173
|
+
* most-pressing patterns surface first.
|
|
174
|
+
*/
|
|
175
|
+
export function buildDenialContext(state) {
|
|
176
|
+
const records = state.getDenialHistory()
|
|
177
|
+
.filter((r) => r.count >= DENIAL_REMINDER_THRESHOLD)
|
|
178
|
+
.sort((a, b) => {
|
|
179
|
+
if (b.count !== a.count)
|
|
180
|
+
return b.count - a.count;
|
|
181
|
+
return b.lastDeniedAt.getTime() - a.lastDeniedAt.getTime();
|
|
182
|
+
});
|
|
183
|
+
if (records.length === 0)
|
|
184
|
+
return '';
|
|
185
|
+
const lines = ['<denial-context>'];
|
|
186
|
+
for (const record of records) {
|
|
187
|
+
lines.push(` - Tool \`${record.toolName}\` denied ${record.count} times in this session.`);
|
|
188
|
+
lines.push(` Last reason: ${record.lastReason}`);
|
|
189
|
+
lines.push(` Args: ${record.argsSummary}`);
|
|
190
|
+
lines.push(' Do not retry without operator intervention.');
|
|
191
|
+
}
|
|
192
|
+
lines.push('</denial-context>');
|
|
193
|
+
return lines.join('\n');
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Canonical sha256 of the args. Object keys are sorted recursively
|
|
197
|
+
* before stringification so `{a:1,b:2}` and `{b:2,a:1}` hash to the
|
|
198
|
+
* same value. Undefined values + functions are stripped (JSON.stringify
|
|
199
|
+
* already does this); circular refs throw — the caller is expected to
|
|
200
|
+
* pass plain JSON objects (every Pugi tool argument set is a JSON
|
|
201
|
+
* object by contract).
|
|
202
|
+
*
|
|
203
|
+
* Returns the full 64-char hex digest. The denial key prepends the
|
|
204
|
+
* tool name (`<tool>:<hash>`) so a hash collision across tools cannot
|
|
205
|
+
* confuse the matcher.
|
|
206
|
+
*/
|
|
207
|
+
export function canonicalArgHash(args) {
|
|
208
|
+
const canonical = canonicalize(args);
|
|
209
|
+
const hash = createHash('sha256');
|
|
210
|
+
hash.update(canonical);
|
|
211
|
+
return hash.digest('hex');
|
|
212
|
+
}
|
|
213
|
+
function canonicalize(value) {
|
|
214
|
+
if (value === null)
|
|
215
|
+
return 'null';
|
|
216
|
+
if (value === undefined)
|
|
217
|
+
return 'null';
|
|
218
|
+
const t = typeof value;
|
|
219
|
+
if (t === 'string')
|
|
220
|
+
return JSON.stringify(value);
|
|
221
|
+
if (t === 'number' || t === 'boolean')
|
|
222
|
+
return JSON.stringify(value);
|
|
223
|
+
if (t === 'bigint')
|
|
224
|
+
return JSON.stringify(value.toString());
|
|
225
|
+
if (Array.isArray(value)) {
|
|
226
|
+
return `[${value.map((entry) => canonicalize(entry)).join(',')}]`;
|
|
227
|
+
}
|
|
228
|
+
if (t === 'object') {
|
|
229
|
+
const obj = value;
|
|
230
|
+
const keys = Object.keys(obj).sort();
|
|
231
|
+
const parts = keys.map((k) => `${JSON.stringify(k)}:${canonicalize(obj[k])}`);
|
|
232
|
+
return `{${parts.join(',')}}`;
|
|
233
|
+
}
|
|
234
|
+
// Functions / symbols — neither should reach here for tool args.
|
|
235
|
+
// Stringify defensively so the hash is stable.
|
|
236
|
+
return JSON.stringify(String(value));
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Short, operator-readable preview of the args object. Cap so a
|
|
240
|
+
* 32 KB write payload does not blow up the reminder block. Falls back
|
|
241
|
+
* к `[non-JSON args]` when stringification fails (e.g. a circular ref
|
|
242
|
+
* snuck through — unlikely for tool args by contract).
|
|
243
|
+
*/
|
|
244
|
+
function summariseArgs(args) {
|
|
245
|
+
let stringified;
|
|
246
|
+
try {
|
|
247
|
+
stringified = JSON.stringify(args);
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
return '[non-JSON args]';
|
|
251
|
+
}
|
|
252
|
+
if (stringified === undefined)
|
|
253
|
+
return '{}';
|
|
254
|
+
if (stringified.length <= DENIAL_ARGS_SUMMARY_BYTES)
|
|
255
|
+
return stringified;
|
|
256
|
+
return `${stringified.slice(0, DENIAL_ARGS_SUMMARY_BYTES)}…`;
|
|
257
|
+
}
|
|
258
|
+
/** Cap reason at a sane length so a hook script's stdout cannot flood the reminder. */
|
|
259
|
+
function truncateReason(reason, cap = 240) {
|
|
260
|
+
if (reason.length <= cap)
|
|
261
|
+
return reason;
|
|
262
|
+
return `${reason.slice(0, cap)}…`;
|
|
263
|
+
}
|
|
264
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* α7 L11 (2026-05-27) — DENIAL TRACKING probe for `pugi doctor`.
|
|
3
|
+
*
|
|
4
|
+
* Reports the current session's denial pressure: total denial count,
|
|
5
|
+
* unique (tool, args) patterns, and how many patterns have repeated
|
|
6
|
+
* past the reminder threshold. Operators read this to spot:
|
|
7
|
+
*
|
|
8
|
+
* - A hook script refusing more dispatches than expected (mis-
|
|
9
|
+
* configured `.pugi/hooks.json`).
|
|
10
|
+
* - Plan-mode runs where the model keeps trying mutating tools
|
|
11
|
+
* (a sign the prompt is not anchoring it correctly).
|
|
12
|
+
* - Stale-read loops indicating concurrent multi-agent writes.
|
|
13
|
+
*
|
|
14
|
+
* Status semantics:
|
|
15
|
+
*
|
|
16
|
+
* - `ok` when the tracker is empty OR carries denials but none have
|
|
17
|
+
* repeated past the threshold. Single denials are normal session
|
|
18
|
+
* hygiene; repeats are the signal.
|
|
19
|
+
* - `warn` when one or more patterns repeated >= the reminder
|
|
20
|
+
* threshold. The probe surfaces the count so the operator can act.
|
|
21
|
+
* - `skipped` when no tracker is wired (e.g. doctor invoked outside
|
|
22
|
+
* a live REPL session, top-level `pugi doctor`).
|
|
23
|
+
*
|
|
24
|
+
* Pure: takes a tracker snapshot — no I/O, no module-level state.
|
|
25
|
+
*/
|
|
26
|
+
import { DENIAL_REMINDER_THRESHOLD, } from '../../denial-tracking/state.js';
|
|
27
|
+
export function probeDenialTracking(deps) {
|
|
28
|
+
if (!deps.tracker) {
|
|
29
|
+
return {
|
|
30
|
+
name: 'DENIAL TRACKING',
|
|
31
|
+
status: 'skipped',
|
|
32
|
+
detail: 'No live session — run `pugi doctor` from inside the REPL to see denials.',
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const summary = deps.tracker.summary();
|
|
36
|
+
if (summary.totalDenials === 0) {
|
|
37
|
+
return {
|
|
38
|
+
name: 'DENIAL TRACKING',
|
|
39
|
+
status: 'ok',
|
|
40
|
+
detail: 'No tool denials this session.',
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (summary.repeatedPatterns === 0) {
|
|
44
|
+
return {
|
|
45
|
+
name: 'DENIAL TRACKING',
|
|
46
|
+
status: 'ok',
|
|
47
|
+
detail: `${summary.totalDenials} denial(s), ${summary.uniquePatterns} unique pattern(s), none repeated.`,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
name: 'DENIAL TRACKING',
|
|
52
|
+
status: 'warn',
|
|
53
|
+
detail: `${summary.totalDenials} denial(s), ${summary.repeatedPatterns} pattern(s) repeated >= ${DENIAL_REMINDER_THRESHOLD}.`,
|
|
54
|
+
remediation: 'Inspect via `/permissions denials` (when L6 lands) or check `.pugi/events.jsonl` for the latest refusals.',
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=denial-tracking.js.map
|