@pugi/cli 0.1.0-beta.54 → 0.1.0-beta.56

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.
@@ -43,6 +43,7 @@ import { appendFileSync, mkdirSync } from 'node:fs';
43
43
  import { createHash } from 'node:crypto';
44
44
  import { homedir } from 'node:os';
45
45
  import { basename, dirname, join, resolve } from 'node:path';
46
+ import { collectStrings, scanForInjection, summarizeFindings, } from '../security/injection-scanner.js';
46
47
  /**
47
48
  * Opt-out env var. Mirrors the convention every other Pugi feature uses
48
49
  * (`PUGI_BARE`, `PUGI_AGENTMEMORY_RECALL_ENABLED=false`, etc.).
@@ -183,6 +184,32 @@ export function writeAuditEvent(input) {
183
184
  encoding: 'utf8',
184
185
  mode: 0o600,
185
186
  });
187
+ // Injection scan (ported KeiSeiKit `injection_patterns.rs`,
188
+ // Apache-2.0). Wrap the OUTBOUND `data` payload through the
189
+ // scanner. Findings emit a SECOND audit line of type
190
+ // `injection_detected` so an operator (or SOC pipeline) sees a
191
+ // structured, append-only record without losing the original
192
+ // event. Never blocks the write — hard-block requires a separate
193
+ // CEO-signed PR.
194
+ //
195
+ // Recursion guard: the `injection_detected` event itself carries
196
+ // matched substrings (intentional — they are the evidence). We
197
+ // skip scanning it to avoid an infinite loop of self-detections.
198
+ if (input.event !== 'injection_detected') {
199
+ const findings = scanAuditPayload(input.data);
200
+ if (findings.length > 0) {
201
+ emitInjectionDetected({
202
+ findings,
203
+ triggeringEvent: input.event,
204
+ sessionId: input.sessionId,
205
+ workspaceRoot: input.workspaceRoot,
206
+ tenant: input.tenant,
207
+ env: input.env,
208
+ home: input.home,
209
+ now: input.now,
210
+ });
211
+ }
212
+ }
186
213
  }
187
214
  catch {
188
215
  // Audit failures must NEVER break a dispatch. The session log + the
@@ -191,4 +218,58 @@ export function writeAuditEvent(input) {
191
218
  // via the doctor probe; for now silent no-op is the contract.
192
219
  }
193
220
  }
221
+ /**
222
+ * Fold the audit `data` payload into a single string and scan it for
223
+ * prompt-injection / invisible-unicode / secret markers. Returns the
224
+ * empty array on clean payloads.
225
+ *
226
+ * Exported for the spec — the scanner module owns the algorithm, this
227
+ * helper owns the payload-walking glue.
228
+ */
229
+ export function scanAuditPayload(data) {
230
+ // Fold every string anywhere in the payload (keys included) into a
231
+ // single buffer separated by NULs. NUL keeps regex anchors honest
232
+ // (no accidental cross-field match for a `^system:` pattern) without
233
+ // adding bytes that themselves could become a pattern.
234
+ const fragments = collectStrings(data);
235
+ if (fragments.length === 0)
236
+ return [];
237
+ const joined = fragments.join('\0');
238
+ return scanForInjection(joined);
239
+ }
240
+ /**
241
+ * Build the `injection_detected` envelope payload and recurse into
242
+ * `writeAuditEvent` to append it. The recursion is bounded — the
243
+ * recursion guard in `writeAuditEvent` short-circuits on the
244
+ * `injection_detected` event so we never re-scan ourselves.
245
+ */
246
+ function emitInjectionDetected(input) {
247
+ const summary = summarizeFindings(input.findings);
248
+ // Cap the findings array in the audit line so a payload with
249
+ // hundreds of invisible-unicode hits does not bloat the JSONL row.
250
+ // The summary still carries `total` so operators see the real count.
251
+ const MAX_FINDINGS_PER_EVENT = 32;
252
+ const truncated = input.findings.length > MAX_FINDINGS_PER_EVENT;
253
+ const capped = truncated
254
+ ? input.findings.slice(0, MAX_FINDINGS_PER_EVENT)
255
+ : [...input.findings];
256
+ writeAuditEvent({
257
+ event: 'injection_detected',
258
+ sessionId: input.sessionId,
259
+ workspaceRoot: input.workspaceRoot,
260
+ tenant: input.tenant,
261
+ env: input.env,
262
+ home: input.home,
263
+ now: input.now,
264
+ data: {
265
+ triggeringEvent: input.triggeringEvent,
266
+ summary,
267
+ findings: capped,
268
+ truncated,
269
+ // KeiSeiKit attribution is recorded inline so a SOC pipeline
270
+ // grepping for the upstream project name lands here.
271
+ detector: 'keiseikit-injection-patterns',
272
+ },
273
+ });
274
+ }
194
275
  //# sourceMappingURL=audit-trail.js.map
@@ -0,0 +1,367 @@
1
+ /**
2
+ * Prompt-injection scanner — TypeScript port of KeiSeiKit's
3
+ * `injection_patterns.rs` (Apache-2.0, KeiSeiLab).
4
+ *
5
+ * Upstream source:
6
+ * `_primitives/_rust/kei-memory/src/injection_patterns.rs`
7
+ * from https://github.com/Pugi-dev/KeiSeiKit (private mirror).
8
+ *
9
+ * Scope of the port:
10
+ * - Pattern TABLES are ported verbatim (regex + invisible-codepoint
11
+ * set + ChatML tags + role-prefix patterns). The substring/secret
12
+ * rows (curl-with-bearer, aws_secret keyword, api_key URL, openssh
13
+ * PEM markers, long-base64 blob heuristic) are KEPT in this port —
14
+ * they harden writes through memory/audit paths against accidental
15
+ * credential pasting.
16
+ * - Detection logic is rewritten in TypeScript. The Rust upstream
17
+ * uses `regex::Regex` + a separate `injection_guard.rs` that owns
18
+ * the "should I block?" decision. Pugi's port collapses both
19
+ * responsibilities into a single function (`scanForInjection`)
20
+ * because the caller surfaces (audit-trail, file-tools) only need
21
+ * the findings list — they do not block writes today (CEO sign-off
22
+ * gate, separate PR).
23
+ *
24
+ * Severity model:
25
+ * The upstream `Block` / `Warn` enum is mirrored as a Pugi field on
26
+ * each finding so a future PR can wire hard-block behavior without
27
+ * re-shaping the call sites.
28
+ *
29
+ * What this is NOT:
30
+ * - An LLM-output safety filter. This scans CONTENT BOUND FOR DISK
31
+ * (audit payloads + file writes / edits) for accidental or
32
+ * adversarial prompt-injection markers.
33
+ * - A secrets scanner. Real secrets detection lives in
34
+ * `scripts/secret-scanner.mjs` (release gate). The few credential
35
+ * heuristics here exist because the upstream Rust treats memory
36
+ * persistence as a credential-exfil surface too.
37
+ *
38
+ * See `licenses/keiseikit-LICENSE-NOTICE.md` for Apache-2.0 attribution.
39
+ */
40
+ /**
41
+ * Maximum captured-match length recorded in a finding. Bounds the
42
+ * worst-case row size in the audit JSONL stream. Set to 128 because
43
+ * the longest legitimate pattern match (`long_base64_line`) would be
44
+ * 1024+ bytes — the operator can re-scan the source content for the
45
+ * full blob if they need it; we only need enough context to triage.
46
+ */
47
+ export const MAX_MATCH_CAPTURE = 128;
48
+ function clampMatch(matched) {
49
+ if (matched.length <= MAX_MATCH_CAPTURE)
50
+ return matched;
51
+ return `${matched.slice(0, MAX_MATCH_CAPTURE)}…`;
52
+ }
53
+ /**
54
+ * Invisible / bidi / zero-width unicode codepoints ported verbatim
55
+ * from `INVISIBLE_CHARS` in the upstream Rust. Each one is a known
56
+ * vehicle for hiding prompt-override text from a casual reader.
57
+ */
58
+ export const INVISIBLE_CHARS = [
59
+ '​', // ZERO WIDTH SPACE
60
+ '‌', // ZERO WIDTH NON-JOINER
61
+ '‍', // ZERO WIDTH JOINER
62
+ '‎', // LEFT-TO-RIGHT MARK
63
+ '‏', // RIGHT-TO-LEFT MARK
64
+ '‪', // LEFT-TO-RIGHT EMBEDDING
65
+ '‫', // RIGHT-TO-LEFT EMBEDDING
66
+ '‬', // POP DIRECTIONAL FORMATTING
67
+ '‭', // LEFT-TO-RIGHT OVERRIDE
68
+ '‮', // RIGHT-TO-LEFT OVERRIDE
69
+ '⁠', // WORD JOINER
70
+ '', // BYTE ORDER MARK / ZERO WIDTH NO-BREAK SPACE
71
+ ];
72
+ /**
73
+ * Pre-built Set for O(1) codepoint membership tests. The scanner walks
74
+ * the input once and probes this set per character — cheaper than a
75
+ * regex with 12 alternation branches.
76
+ */
77
+ const INVISIBLE_CHAR_SET = new Set(INVISIBLE_CHARS);
78
+ /**
79
+ * Threshold above which a single base64-looking line is flagged.
80
+ * Matches the upstream `BASE64_BLOB_BYTES` constant so the heuristic
81
+ * stays aligned with the Rust spec. The regex below hardcodes the
82
+ * same value for compile-time clarity.
83
+ */
84
+ export const BASE64_BLOB_BYTES = 1024;
85
+ /**
86
+ * PEM begin marker built at runtime so the literal dashes do not
87
+ * trigger over-eager secret-scanners in this very source file (same
88
+ * concern as the upstream `pem_dashes()` helper).
89
+ */
90
+ function pemMarker(label) {
91
+ const d = '-'.repeat(5);
92
+ return `${d}BEGIN ${label}${d}`;
93
+ }
94
+ /**
95
+ * Escape regex metachars in a literal string. We avoid pulling a
96
+ * dependency just for this — the set of metachars is small and
97
+ * well-known.
98
+ */
99
+ function escapeRegex(literal) {
100
+ return literal.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
101
+ }
102
+ /**
103
+ * Prompt-override patterns. Ported verbatim from
104
+ * `prompt_override_patterns()` in the upstream Rust. The regex
105
+ * strings are the same modulo Rust's `(?im)` inline flags being
106
+ * expressed as `i` + `m` on the TS `RegExp`.
107
+ */
108
+ const PROMPT_OVERRIDE_PATTERNS = [
109
+ {
110
+ id: 'prompt_override_ignore_previous',
111
+ kind: 'override-prompt',
112
+ re: /ignore\s+previous\s+instructions/gi,
113
+ severity: 'block',
114
+ source: 'promptguard:override',
115
+ },
116
+ {
117
+ id: 'prompt_override_you_are_now',
118
+ kind: 'override-prompt',
119
+ re: /you\s+are\s+now\b/gi,
120
+ severity: 'block',
121
+ source: 'promptguard:roleplay',
122
+ },
123
+ {
124
+ id: 'prompt_override_disregard',
125
+ kind: 'override-prompt',
126
+ re: /disregard\s+(all|prior|above)/gi,
127
+ severity: 'block',
128
+ source: 'promptguard:override',
129
+ },
130
+ {
131
+ id: 'system_role_prefix',
132
+ kind: 'override-prompt',
133
+ re: /^\s*system\s*:/gim,
134
+ severity: 'block',
135
+ source: 'promptguard:role-prefix',
136
+ },
137
+ {
138
+ id: 'chatml_im_start',
139
+ kind: 'tag-injection',
140
+ re: /<\|im_start\|>/g,
141
+ severity: 'block',
142
+ source: 'chatml:tag',
143
+ },
144
+ {
145
+ id: 'chatml_endoftext',
146
+ kind: 'tag-injection',
147
+ re: /<\|endoftext\|>/g,
148
+ severity: 'block',
149
+ source: 'chatml:tag',
150
+ },
151
+ ];
152
+ /**
153
+ * Secret-shaped patterns. Ported from `secret_patterns()`. The PEM
154
+ * markers are built at runtime so they do not show up verbatim in
155
+ * this file's bytes (anti-self-trigger).
156
+ */
157
+ function buildSecretPatterns() {
158
+ const openssh = escapeRegex(pemMarker('OPENSSH PRIVATE KEY'));
159
+ const rsa = escapeRegex(pemMarker('RSA PRIVATE KEY'));
160
+ return [
161
+ {
162
+ id: 'ssh_openssh_private',
163
+ kind: 'secret-marker',
164
+ re: new RegExp(openssh, 'g'),
165
+ severity: 'block',
166
+ source: 'secret:openssh',
167
+ },
168
+ {
169
+ id: 'ssh_rsa_private',
170
+ kind: 'secret-marker',
171
+ re: new RegExp(rsa, 'g'),
172
+ severity: 'block',
173
+ source: 'secret:rsa',
174
+ },
175
+ {
176
+ // Upstream P2.1.b audit upgraded this to Block tier — long
177
+ // base64 blobs on a memory-write path are a direct exfil
178
+ // surface for attestation / key blobs pasted into transcripts.
179
+ id: 'long_base64_line',
180
+ kind: 'secret-marker',
181
+ re: new RegExp(`^[A-Za-z0-9+/=]{${BASE64_BLOB_BYTES},}$`, 'gm'),
182
+ severity: 'block',
183
+ source: 'heuristic:base64-blob',
184
+ },
185
+ ];
186
+ }
187
+ /**
188
+ * Substring/heuristic patterns. Ported from `build_substring_table()`.
189
+ * Each row demands ALL needles be present in the LOWERCASED copy of
190
+ * the input (AND semantics) — keeps false-positives low.
191
+ */
192
+ const SUBSTRING_PATTERNS = [
193
+ {
194
+ id: 'curl_with_bearer',
195
+ kind: 'secret-marker',
196
+ needles: ['bearer ', '://'],
197
+ severity: 'block',
198
+ source: 'exfil:curl-bearer',
199
+ },
200
+ {
201
+ id: 'aws_secret_keyword',
202
+ kind: 'secret-marker',
203
+ needles: ['aws_secret'],
204
+ severity: 'block',
205
+ source: 'secret:aws',
206
+ },
207
+ {
208
+ id: 'api_key_url',
209
+ kind: 'secret-marker',
210
+ needles: ['api_key=', '://'],
211
+ severity: 'block',
212
+ source: 'exfil:api-key-url',
213
+ },
214
+ ];
215
+ let REGEX_TABLE = null;
216
+ function regexPatterns() {
217
+ if (REGEX_TABLE === null) {
218
+ REGEX_TABLE = [...PROMPT_OVERRIDE_PATTERNS, ...buildSecretPatterns()];
219
+ }
220
+ return REGEX_TABLE;
221
+ }
222
+ /**
223
+ * Maximum input size we scan. Above this we sample the first
224
+ * MAX_SCAN_BYTES bytes and tag the result as `truncated: true`. This
225
+ * keeps a 10 MB log payload from stalling the audit append path.
226
+ *
227
+ * The threshold is deliberately generous (256 KB) — the typical audit
228
+ * `data` payload is a few hundred bytes (a single `tool_call` envelope)
229
+ * and a file write of an HTML page is well under the cap. The cutoff
230
+ * exists only for pathological cases.
231
+ */
232
+ export const MAX_SCAN_BYTES = 256 * 1024;
233
+ /**
234
+ * Scan a string for prompt-injection / invisible-unicode / secret
235
+ * markers. Returns the empty array when clean. Never throws —
236
+ * malformed input (e.g. lone surrogates) falls through to the regex
237
+ * engine and produces zero or more findings, never an exception.
238
+ *
239
+ * Pure function. Safe to call from a hot path (audit-trail append,
240
+ * file-tools writeTool) without worrying about side effects.
241
+ */
242
+ export function scanForInjection(text) {
243
+ if (typeof text !== 'string' || text.length === 0)
244
+ return [];
245
+ const findings = [];
246
+ const scanText = text.length > MAX_SCAN_BYTES ? text.slice(0, MAX_SCAN_BYTES) : text;
247
+ // 1. Invisible unicode scan: O(n) single pass with a Set lookup.
248
+ // We collect per-codepoint hits rather than collapsing them so
249
+ // the operator can see how many bidi marks are present (high
250
+ // counts strongly suggest adversarial intent).
251
+ for (let i = 0; i < scanText.length; i += 1) {
252
+ const ch = scanText[i];
253
+ if (ch === undefined)
254
+ continue;
255
+ if (INVISIBLE_CHAR_SET.has(ch)) {
256
+ const code = ch.charCodeAt(0).toString(16).toUpperCase().padStart(4, '0');
257
+ findings.push({
258
+ kind: 'invisible-unicode',
259
+ id: `invisible_unicode_U+${code}`,
260
+ severity: 'warn',
261
+ matched: ch,
262
+ offset: i,
263
+ source: `unicode:invisible:U+${code}`,
264
+ });
265
+ }
266
+ }
267
+ // 2. Regex table scan. Each pattern uses the `g` flag so we walk
268
+ // every occurrence — a single text can carry multiple ChatML
269
+ // tags or override phrases and the operator needs to see all of
270
+ // them, not just the first.
271
+ for (const pattern of regexPatterns()) {
272
+ // Re-set lastIndex defensively in case a prior call left the
273
+ // regex's stateful cursor mid-string.
274
+ pattern.re.lastIndex = 0;
275
+ let match;
276
+ while ((match = pattern.re.exec(scanText)) !== null) {
277
+ findings.push({
278
+ kind: pattern.kind,
279
+ id: pattern.id,
280
+ severity: pattern.severity,
281
+ matched: clampMatch(match[0]),
282
+ offset: match.index,
283
+ source: pattern.source,
284
+ });
285
+ // Guard against zero-width matches infinite-looping (e.g. a
286
+ // regex that matches the empty string would never advance).
287
+ if (match.index === pattern.re.lastIndex) {
288
+ pattern.re.lastIndex += 1;
289
+ }
290
+ }
291
+ }
292
+ // 3. Substring/heuristic scan. AND semantics: every needle must
293
+ // appear in the lowercased copy. We record the FIRST needle's
294
+ // offset because that is the most actionable index for the
295
+ // operator (the others may be hundreds of bytes away).
296
+ const lower = scanText.toLowerCase();
297
+ for (const pattern of SUBSTRING_PATTERNS) {
298
+ const offsets = pattern.needles.map((n) => lower.indexOf(n));
299
+ if (offsets.every((o) => o >= 0)) {
300
+ const firstOffset = Math.min(...offsets);
301
+ // Reconstruct a useful matched snippet — the needles can be
302
+ // far apart so we cap at the first needle plus a window.
303
+ const snippetEnd = Math.min(firstOffset + MAX_MATCH_CAPTURE, scanText.length);
304
+ findings.push({
305
+ kind: pattern.kind,
306
+ id: pattern.id,
307
+ severity: pattern.severity,
308
+ matched: clampMatch(scanText.slice(firstOffset, snippetEnd)),
309
+ offset: firstOffset,
310
+ source: pattern.source,
311
+ });
312
+ }
313
+ }
314
+ return findings;
315
+ }
316
+ export function summarizeFindings(findings) {
317
+ let score = 0;
318
+ const kindSet = new Set();
319
+ for (const f of findings) {
320
+ if (f.severity === 'block')
321
+ score += 1;
322
+ kindSet.add(f.kind);
323
+ }
324
+ return {
325
+ score,
326
+ total: findings.length,
327
+ kinds: Array.from(kindSet).sort(),
328
+ };
329
+ }
330
+ /**
331
+ * Recursively walk a JSON-shaped value and concatenate every string
332
+ * found. Used by audit-trail to fold the entire `data` payload into a
333
+ * single scannable surface — a tool_result with a deeply nested error
334
+ * object could otherwise hide an override prompt one level deep.
335
+ *
336
+ * Cycles are broken by a WeakSet — a payload that round-trips through
337
+ * a session struct is safe to scan even when it has back-references.
338
+ */
339
+ export function collectStrings(value, seen = new WeakSet()) {
340
+ if (value === null || value === undefined)
341
+ return [];
342
+ if (typeof value === 'string')
343
+ return [value];
344
+ if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') {
345
+ return [];
346
+ }
347
+ if (typeof value !== 'object')
348
+ return [];
349
+ if (seen.has(value))
350
+ return [];
351
+ seen.add(value);
352
+ const out = [];
353
+ if (Array.isArray(value)) {
354
+ for (const item of value) {
355
+ out.push(...collectStrings(item, seen));
356
+ }
357
+ return out;
358
+ }
359
+ for (const key of Object.keys(value)) {
360
+ // Scan the KEY too — a deliberately-crafted payload could hide
361
+ // an override phrase as an object key.
362
+ out.push(key);
363
+ out.push(...collectStrings(value[key], seen));
364
+ }
365
+ return out;
366
+ }
367
+ //# sourceMappingURL=injection-scanner.js.map
@@ -44,7 +44,7 @@ export function sanitizeSemver(raw) {
44
44
  * during import). When bumping the CLI version BOTH literals must be
45
45
  * updated; the release smoke-test (`pack:smoke`) verifies they agree.
46
46
  */
47
- export const PUGI_CLI_VERSION = sanitizeSemver('0.1.0-beta.54');
47
+ export const PUGI_CLI_VERSION = sanitizeSemver('0.1.0-beta.56');
48
48
  /**
49
49
  * Outbound: the CLI's installed semver. Read at request time by
50
50
  * `version-interceptor.ts` and injected on every `fetch` call.
@@ -33,6 +33,7 @@ import { globSync } from 'node:fs';
33
33
  import { decidePermission } from '../core/permission.js';
34
34
  import { StaleReadError, createReadRecord, hashContent, } from '../core/file-cache.js';
35
35
  import { resolveWorkspacePath } from '../core/path-security.js';
36
+ import { scanForInjection, summarizeFindings } from '../core/security/injection-scanner.js';
36
37
  import { recordFileMutation, recordToolCall, recordToolResult } from '../core/session.js';
37
38
  /**
38
39
  * α6.9 WriteGate marker — thrown by `gateOnCancellation` when the
@@ -184,6 +185,14 @@ export function writeTool(ctx, path, content) {
184
185
  const tmp = `${resolved}.pugi-tmp-${Date.now()}`;
185
186
  writeFileSync(tmp, content, { encoding: 'utf8', mode: 0o600 });
186
187
  renameSync(tmp, resolved);
188
+ // Injection scan (ported KeiSeiKit `injection_patterns.rs`,
189
+ // Apache-2.0). Scan the BODY (never the path — path security is
190
+ // owned by `path-security.ts`). Findings are SURFACED as an extra
191
+ // line on the session tool-result, never block the write. Hard-
192
+ // block requires a separate CEO-signed PR. Failure here must NOT
193
+ // throw: a buggy scanner cannot rugpull the write that already
194
+ // landed on disk above.
195
+ surfaceInjectionWarning(ctx, toolCallId, 'write', path, content);
187
196
  // Refresh the cache with the post-write content so the model can
188
197
  // chain a follow-up read+edit on the same file without an extra
189
198
  // round-trip. Same pattern editTool uses below.
@@ -197,6 +206,36 @@ export function writeTool(ctx, path, content) {
197
206
  });
198
207
  recordToolResult(ctx.session, toolCallId, 'success', `${existed ? 'Updated' : 'Created'} ${path}`);
199
208
  }
209
+ /**
210
+ * Surface an injection-scan warning on a file write/edit BODY. The
211
+ * scan never blocks — it folds findings into the session as a
212
+ * `tool_result` with status `warn` so an operator (or SOC pipeline
213
+ * tailing `<workspace>/.pugi/events.jsonl`) sees the signal without a
214
+ * mid-dispatch rollback.
215
+ *
216
+ * Wrapped in try/catch so a malformed scanner never crashes the tool
217
+ * loop — the write itself has already landed on disk by the time we
218
+ * call this.
219
+ */
220
+ function surfaceInjectionWarning(ctx, triggeringToolCallId, tool, path, body) {
221
+ try {
222
+ const findings = scanForInjection(body);
223
+ if (findings.length === 0)
224
+ return;
225
+ const summary = summarizeFindings(findings);
226
+ const warnCallId = recordToolCall(ctx.session, 'injection_warning', path);
227
+ const message = `injection_warning: ${tool} ${path} — ${summary.total} pattern(s) ` +
228
+ `(score=${summary.score}, kinds=${summary.kinds.join('|')}). ` +
229
+ `Triggering call: ${triggeringToolCallId}. ` +
230
+ `Detector: keiseikit-injection-patterns. Write was NOT blocked.`;
231
+ recordToolResult(ctx.session, warnCallId, 'success', message);
232
+ }
233
+ catch {
234
+ // Scanner failure must NEVER throw — the write has already
235
+ // landed and the tool loop has to continue. Silent no-op
236
+ // mirrors the audit-trail contract.
237
+ }
238
+ }
200
239
  export function editTool(ctx, path, oldString, newString) {
201
240
  const toolCallId = recordToolCall(ctx.session, 'edit', path);
202
241
  // α6.9 WriteGate: refuse the edit when the operator has cancelled
@@ -252,6 +291,13 @@ export function editTool(ctx, path, oldString, newString) {
252
291
  const tmp = `${resolved}.pugi-tmp-${Date.now()}`;
253
292
  writeFileSync(tmp, after, { encoding: 'utf8', mode: 0o600 });
254
293
  renameSync(tmp, resolved);
294
+ // Injection scan (ported KeiSeiKit `injection_patterns.rs`,
295
+ // Apache-2.0). We scan the NEW SUBSTRING the model is inserting,
296
+ // not the full post-edit file — the rest of the file is operator-
297
+ // owned content that pre-dates this dispatch. False-positive on
298
+ // legitimate prose that mentions banned phrases is the worst
299
+ // outcome and the warn-only contract bounds the cost.
300
+ surfaceInjectionWarning(ctx, toolCallId, 'edit', path, newString);
255
301
  ctx.readCache.set(createReadRecord(ctx.root, path, after, 'read_tool'));
256
302
  recordFileMutation(ctx.session, {
257
303
  toolCallId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pugi/cli",
3
- "version": "0.1.0-beta.54",
3
+ "version": "0.1.0-beta.56",
4
4
  "description": "Pugi CLI - terminal-native software execution system",
5
5
  "homepage": "https://pugi.io",
6
6
  "repository": {
@@ -55,7 +55,7 @@
55
55
  "undici": "^8.3.0",
56
56
  "zod": "^3.23.0",
57
57
  "@pugi/personas": "0.1.2",
58
- "@pugi/sdk": "0.1.0-beta.54"
58
+ "@pugi/sdk": "0.1.0-beta.56"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/node": "^22.0.0",