klyro 1.0.0 → 1.0.1

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 (96) hide show
  1. package/dist/agent/anthropic-adapter.d.ts +36 -0
  2. package/dist/agent/anthropic-adapter.js +68 -16
  3. package/dist/agent/capabilities.d.ts +23 -0
  4. package/dist/agent/capabilities.js +46 -5
  5. package/dist/agent/child-worker.d.ts +104 -0
  6. package/dist/agent/child-worker.js +250 -0
  7. package/dist/agent/orchestrator.d.ts +106 -5
  8. package/dist/agent/orchestrator.js +378 -58
  9. package/dist/agent/provider-adapter.d.ts +8 -0
  10. package/dist/agent/provider-adapter.js +12 -3
  11. package/dist/agent/retry.d.ts +1 -1
  12. package/dist/agent/retry.js +2 -2
  13. package/dist/agent/runtime.d.ts +50 -8
  14. package/dist/agent/runtime.js +196 -33
  15. package/dist/agent/worktree-manager.d.ts +74 -0
  16. package/dist/agent/worktree-manager.js +189 -0
  17. package/dist/checkpoints/store.js +30 -5
  18. package/dist/cli/auth.js +16 -1
  19. package/dist/cli/config.d.ts +9 -3
  20. package/dist/cli/config.js +64 -3
  21. package/dist/cli/eval.d.ts +6 -1
  22. package/dist/cli/eval.js +9 -0
  23. package/dist/cli/repl.js +155 -28
  24. package/dist/cli/run.d.ts +7 -11
  25. package/dist/cli/run.js +68 -17
  26. package/dist/cli/update.d.ts +5 -0
  27. package/dist/cli/update.js +62 -10
  28. package/dist/context/import-graph.d.ts +2 -0
  29. package/dist/context/import-graph.js +31 -3
  30. package/dist/context/klyro-md.js +4 -1
  31. package/dist/context/memory.d.ts +8 -0
  32. package/dist/context/memory.js +50 -2
  33. package/dist/context/project-map.d.ts +6 -0
  34. package/dist/context/project-map.js +50 -2
  35. package/dist/context/repo-map.d.ts +2 -0
  36. package/dist/context/repo-map.js +31 -1
  37. package/dist/events/catalog.d.ts +28 -0
  38. package/dist/index.js +89 -4
  39. package/dist/mcp/client.d.ts +6 -4
  40. package/dist/mcp/client.js +83 -14
  41. package/dist/mcp/config.d.ts +10 -0
  42. package/dist/mcp/config.js +18 -1
  43. package/dist/mcp/registry.d.ts +23 -1
  44. package/dist/mcp/registry.js +78 -6
  45. package/dist/mcp/schema.d.ts +11 -4
  46. package/dist/mcp/schema.js +27 -16
  47. package/dist/mcp/trust.d.ts +20 -0
  48. package/dist/mcp/trust.js +74 -0
  49. package/dist/persistence/audit.d.ts +28 -0
  50. package/dist/persistence/audit.js +101 -1
  51. package/dist/persistence/store.d.ts +26 -2
  52. package/dist/persistence/store.js +140 -13
  53. package/dist/policy/approval.d.ts +14 -0
  54. package/dist/policy/approval.js +44 -2
  55. package/dist/policy/engine.d.ts +1 -0
  56. package/dist/policy/engine.js +88 -8
  57. package/dist/policy/secret-redactor.js +4 -0
  58. package/dist/providers/model-info.d.ts +17 -0
  59. package/dist/providers/model-info.js +35 -2
  60. package/dist/repl.d.ts +6 -0
  61. package/dist/repl.js +12 -7
  62. package/dist/tools/agent/spawn-agent.js +5 -5
  63. package/dist/tools/agent/task-apply.d.ts +4 -0
  64. package/dist/tools/agent/task-apply.js +44 -0
  65. package/dist/tools/agent/task-stop.d.ts +6 -0
  66. package/dist/tools/agent/task-stop.js +39 -0
  67. package/dist/tools/agent/task-wait.d.ts +17 -0
  68. package/dist/tools/agent/task-wait.js +79 -0
  69. package/dist/tools/fs/apply-patch.js +71 -0
  70. package/dist/tools/fs/edit-file.js +65 -0
  71. package/dist/tools/fs/multi-edit.d.ts +4 -0
  72. package/dist/tools/fs/multi-edit.js +66 -0
  73. package/dist/tools/fs/write-file.js +67 -0
  74. package/dist/tools/registry.js +6 -0
  75. package/dist/tools/shell/background.js +6 -3
  76. package/dist/tools/shell/sandbox.d.ts +51 -0
  77. package/dist/tools/shell/sandbox.js +143 -0
  78. package/dist/tools/shell/shell-exec.d.ts +1 -0
  79. package/dist/tools/shell/shell-exec.js +83 -11
  80. package/dist/tools/shell/worker-entry.d.ts +12 -0
  81. package/dist/tools/shell/worker-entry.js +43 -0
  82. package/dist/tools/types.d.ts +6 -0
  83. package/dist/tools/verify/run-verify.js +3 -1
  84. package/dist/trace/writer.d.ts +13 -0
  85. package/dist/trace/writer.js +55 -4
  86. package/dist/tui/app.js +1 -1
  87. package/dist/tui/approval.js +20 -21
  88. package/dist/util.d.ts +1 -0
  89. package/dist/util.js +1 -0
  90. package/dist/verification/baseline.js +17 -3
  91. package/dist/verification/classify.js +23 -12
  92. package/dist/verification/engine.js +3 -1
  93. package/dist/verification/registry.d.ts +2 -0
  94. package/dist/verification/registry.js +33 -0
  95. package/dist/verification/scoped.js +28 -6
  96. package/package.json +1 -1
@@ -2,14 +2,114 @@
2
2
  * Audit log — append-only JSONL of runtime events. Same shape as the
3
3
  * future durable-task scheduler will read, so the data layout is
4
4
  * forward-compatible.
5
+ *
6
+ * Durability: every record is hash-chained. Each line carries `prevHash`
7
+ * (hex sha256 of the previous line's canonical JSON, 'GENESIS' for the
8
+ * first line) and `hash` (sha256 of the record-with-prevHash canonical
9
+ * JSON). `verifyAuditChain` recomputes the chain for `klyro audit`.
5
10
  */
11
+ import * as crypto from 'node:crypto';
12
+ import * as fs from 'node:fs/promises';
13
+ import * as path from 'node:path';
6
14
  import { SessionStore } from './store.js';
15
+ export const AUDIT_GENESIS = 'GENESIS';
16
+ /** Canonical JSON: object keys sorted recursively, so hashes are stable. */
17
+ export function canonicalJson(value) {
18
+ if (value === null || typeof value !== 'object')
19
+ return JSON.stringify(value) ?? 'null';
20
+ if (Array.isArray(value))
21
+ return `[${value.map(canonicalJson).join(',')}]`;
22
+ const rec = value;
23
+ const keys = Object.keys(rec).sort();
24
+ return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalJson(rec[k])}`).join(',')}}`;
25
+ }
26
+ export function sha256Hex(s) {
27
+ return crypto.createHash('sha256').update(s, 'utf-8').digest('hex');
28
+ }
29
+ /** Hash of a chained record excluding its own `hash` field. */
30
+ export function hashAuditRecord(record) {
31
+ const { hash: _omit, ...body } = record;
32
+ void _omit;
33
+ return sha256Hex(canonicalJson(body));
34
+ }
35
+ async function readLastLine(filePath) {
36
+ try {
37
+ const raw = await fs.readFile(filePath, 'utf-8');
38
+ const lines = raw.split('\n').filter((l) => l.trim());
39
+ return lines.length > 0 ? lines[lines.length - 1] : null;
40
+ }
41
+ catch {
42
+ return null;
43
+ }
44
+ }
45
+ /** prevHash for the next record: hash of the previous line, or GENESIS. */
46
+ async function prevHashFor(filePath) {
47
+ const last = await readLastLine(filePath);
48
+ if (!last)
49
+ return AUDIT_GENESIS;
50
+ try {
51
+ const parsed = JSON.parse(last);
52
+ if (typeof parsed.hash === 'string' && parsed.hash)
53
+ return parsed.hash;
54
+ return sha256Hex(canonicalJson(parsed));
55
+ }
56
+ catch {
57
+ return sha256Hex(last);
58
+ }
59
+ }
7
60
  export class AuditLog {
8
61
  filePath;
62
+ /** Serializes chained appends so concurrent writes can't fork the chain. */
63
+ chain = Promise.resolve();
9
64
  constructor(filePath) {
10
65
  this.filePath = filePath;
11
66
  }
12
67
  async write(event) {
13
- await SessionStore.appendJsonl(this.filePath, event);
68
+ const task = this.chain.then(() => this.appendChained(event));
69
+ // Keep the chain alive across failures; the caller still sees its error.
70
+ this.chain = task.catch(() => undefined);
71
+ return task;
72
+ }
73
+ async appendChained(event) {
74
+ const prevHash = await prevHashFor(this.filePath);
75
+ const body = { ...event, prevHash };
76
+ const record = { ...body, prevHash, hash: sha256Hex(canonicalJson(body)) };
77
+ await SessionStore.appendJsonl(this.filePath, record);
78
+ }
79
+ }
80
+ /**
81
+ * Recompute the hash chain of a session JSONL file.
82
+ * Sessions live at `<sessionsDir>/<sessionId>.jsonl` (see SessionStore).
83
+ */
84
+ export async function verifyAuditChain(sessionsDir, sessionId) {
85
+ const filePath = path.join(sessionsDir, `${sessionId}.jsonl`);
86
+ let raw;
87
+ try {
88
+ raw = await fs.readFile(filePath, 'utf-8');
89
+ }
90
+ catch {
91
+ return { ok: false, events: 0, error: `audit log not found: ${filePath}` };
92
+ }
93
+ const lines = raw.split('\n').filter((l) => l.trim());
94
+ let expectedPrev = AUDIT_GENESIS;
95
+ let events = 0;
96
+ for (let i = 0; i < lines.length; i++) {
97
+ const line = lines[i];
98
+ let parsed;
99
+ try {
100
+ parsed = JSON.parse(line);
101
+ }
102
+ catch {
103
+ return { ok: false, events, error: `line ${i + 1}: unparseable JSON` };
104
+ }
105
+ if (parsed.prevHash !== expectedPrev) {
106
+ return { ok: false, events, error: `line ${i + 1}: prevHash mismatch (chain fork or truncation)` };
107
+ }
108
+ if (typeof parsed.hash !== 'string' || parsed.hash !== hashAuditRecord(parsed)) {
109
+ return { ok: false, events, error: `line ${i + 1}: hash mismatch (tampered record)` };
110
+ }
111
+ expectedPrev = parsed.hash;
112
+ events++;
14
113
  }
114
+ return { ok: true, events };
15
115
  }
@@ -50,6 +50,8 @@ export declare class SessionStore {
50
50
  private writeIndex;
51
51
  private projectHash;
52
52
  private perProjectIndexPath;
53
+ /** Atomic per-project index write (tmp + fsync + rename). */
54
+ private writePerProjectIndex;
53
55
  private titleFor;
54
56
  create(opts: {
55
57
  cwd: string;
@@ -61,7 +63,7 @@ export declare class SessionStore {
61
63
  * continues with full context instead of starting blank.
62
64
  */
63
65
  fork(id: string, taskSuffix?: string): Promise<SessionRecord>;
64
- /** Delete a session and all its artifacts (record, transcript, jsonl, indexes). */
66
+ /** Delete a session and all its artifacts (record, transcript, jsonl, lock, indexes). */
65
67
  delete(id: string): Promise<boolean>;
66
68
  private jsonlPath;
67
69
  appendJsonl(id: string, entry: unknown): Promise<void>;
@@ -77,6 +79,28 @@ export declare class SessionStore {
77
79
  status?: SessionStatus;
78
80
  }): Promise<SessionRecord[]>;
79
81
  get(id: string): Promise<SessionRecord | null>;
80
- /** Atomic append — survives crashes; suitable for audit log. */
82
+ /** Atomic append + fsync — survives crashes; suitable for audit log. */
81
83
  static appendJsonl(filePath: string, entry: unknown): Promise<void>;
82
84
  }
85
+ /**
86
+ * Redact text blocks of a stored message at the persist boundary.
87
+ * Handles string content, `{ text }` blocks, arrays of blocks, and plain
88
+ * objects (observation input/output) via a deep walk over string leaves.
89
+ * Only secret-shaped strings (key/token/password patterns, [:=-] shapes)
90
+ * are touched — normal prose passes through unchanged.
91
+ */
92
+ export declare function redactStoredContent(content: unknown): unknown;
93
+ export interface SessionLockInfo {
94
+ held: boolean;
95
+ pid?: number;
96
+ /** True when the lock holder process is (probably) still running. */
97
+ alive?: boolean;
98
+ }
99
+ /**
100
+ * Cross-process session lock probe. The lock file `<sessionsDir>/<id>.lock`
101
+ * contains the holder's PID. `alive` is resolved via `process.kill(pid, 0)`:
102
+ * EPERM means alive-but-unpermitted (→ true); ESRCH / other errors → false.
103
+ */
104
+ export declare function readSessionLock(sessionsDir: string, id: string): SessionLockInfo;
105
+ /** Best-effort release of the cross-process session lock. Never throws. */
106
+ export declare function releaseSessionLock(sessionsDir: string, id: string): Promise<void>;
@@ -10,8 +10,10 @@
10
10
  * SessionStore interface.
11
11
  */
12
12
  import * as fs from 'node:fs/promises';
13
+ import * as fsSync from 'node:fs';
13
14
  import * as path from 'node:path';
14
15
  import { randomUUID } from 'node:crypto';
16
+ import { redact } from '../policy/secret-redactor.js';
15
17
  export class SessionStore {
16
18
  dir;
17
19
  indexPath;
@@ -75,6 +77,28 @@ export class SessionStore {
75
77
  return Math.abs(h).toString(36);
76
78
  }
77
79
  perProjectIndexPath(cwd) { return path.join(this.dir, `index-${this.projectHash(cwd)}.json`); }
80
+ /** Atomic per-project index write (tmp + fsync + rename). */
81
+ async writePerProjectIndex(pp, pIdx) {
82
+ const tmp = `${pp}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`;
83
+ await fs.writeFile(tmp, JSON.stringify(pIdx, null, 2), 'utf-8');
84
+ try {
85
+ const fh = await fs.open(tmp, 'r+');
86
+ try {
87
+ await fh.sync();
88
+ }
89
+ finally {
90
+ await fh.close();
91
+ }
92
+ }
93
+ catch { /* ignore on Windows */ }
94
+ try {
95
+ await fs.rename(tmp, pp);
96
+ }
97
+ catch {
98
+ await fs.unlink(tmp).catch(() => undefined);
99
+ throw new Error('Failed to write per-project sessions index');
100
+ }
101
+ }
78
102
  titleFor(task) {
79
103
  // heuristic title via first 6 words, or model.small would be used if available
80
104
  const w = task.trim().split(/\s+/).slice(0, 6).join(' ');
@@ -84,24 +108,39 @@ export class SessionStore {
84
108
  // Locked: the index read-modify-write below races under concurrent creates.
85
109
  return this.withLock('index', async () => {
86
110
  await this.ensureDir();
87
- const id = randomUUID();
111
+ // Redact at the persist boundary — tasks routinely paste keys/tokens.
112
+ const task = redact(opts.task);
113
+ let id = randomUUID();
114
+ // Cross-process session lock: O_EXCL so a concurrent creator (another
115
+ // process resuming/creating the same id path) can detect contention.
116
+ // On collision fall back to a unique id suffix — never crash.
117
+ try {
118
+ await fs.writeFile(path.join(this.dir, `${id}.lock`), String(process.pid), { flag: 'wx' });
119
+ }
120
+ catch {
121
+ id = `${id}-${process.pid}-${Date.now().toString(36)}`;
122
+ try {
123
+ await fs.writeFile(path.join(this.dir, `${id}.lock`), String(process.pid), { flag: 'wx' });
124
+ }
125
+ catch { /* best-effort only */ }
126
+ }
88
127
  const now = Date.now();
89
128
  const record = {
90
129
  id,
91
130
  cwd: opts.cwd,
92
- task: opts.task,
131
+ task,
93
132
  status: 'open',
94
133
  createdAt: now,
95
134
  updatedAt: now,
96
135
  config: opts.config,
97
136
  };
98
- record.title = this.titleFor(opts.task);
137
+ record.title = this.titleFor(task);
99
138
  await fs.writeFile(path.join(this.dir, `${id}.json`), JSON.stringify({ record, messages: [], observations: [] }, null, 2));
100
139
  await this.appendJsonl(id, { type: 'session.create', record, ts: now });
101
140
  const idx = await this.readIndex();
102
141
  idx[id] = record;
103
142
  await this.writeIndex(idx);
104
- // per-project index
143
+ // per-project index (atomic)
105
144
  try {
106
145
  const pp = this.perProjectIndexPath(opts.cwd);
107
146
  let pIdx = {};
@@ -110,7 +149,7 @@ export class SessionStore {
110
149
  }
111
150
  catch { }
112
151
  pIdx[id] = record;
113
- await fs.writeFile(pp, JSON.stringify(pIdx, null, 2), 'utf-8');
152
+ await this.writePerProjectIndex(pp, pIdx);
114
153
  }
115
154
  catch { }
116
155
  return record;
@@ -136,13 +175,13 @@ export class SessionStore {
136
175
  await this.appendJsonl(forked.id, { type: 'session.fork', from: id, ts: Date.now() });
137
176
  return forked;
138
177
  }
139
- /** Delete a session and all its artifacts (record, transcript, jsonl, indexes). */
178
+ /** Delete a session and all its artifacts (record, transcript, jsonl, lock, indexes). */
140
179
  async delete(id) {
141
180
  return this.withLock('index', async () => {
142
181
  const rec = await this.get(id);
143
182
  if (!rec)
144
183
  return false;
145
- for (const f of [`${id}.json`, `${id}.jsonl`]) {
184
+ for (const f of [`${id}.json`, `${id}.jsonl`, `${id}.lock`]) {
146
185
  try {
147
186
  await fs.unlink(path.join(this.dir, f));
148
187
  }
@@ -156,7 +195,7 @@ export class SessionStore {
156
195
  const raw = await fs.readFile(pp, 'utf-8');
157
196
  const pIdx = JSON.parse(raw);
158
197
  delete pIdx[id];
159
- await fs.writeFile(pp, JSON.stringify(pIdx, null, 2), 'utf-8');
198
+ await this.writePerProjectIndex(pp, pIdx);
160
199
  }
161
200
  catch { /* ignore */ }
162
201
  return true;
@@ -241,14 +280,19 @@ export class SessionStore {
241
280
  async appendMessage(id, message) {
242
281
  return this.withLock(id, async () => {
243
282
  const data = await this.readSession(id);
244
- data.messages.push(message);
283
+ data.messages.push({ ...message, content: redactStoredContent(message.content) });
245
284
  await this.writeSession(id, data);
246
285
  });
247
286
  }
248
287
  async appendObservation(id, obs) {
249
288
  return this.withLock(id, async () => {
250
289
  const data = await this.readSession(id);
251
- data.observations.push(obs);
290
+ // S4-at-rest: redact observation input/output at the persist boundary.
291
+ data.observations.push({
292
+ ...obs,
293
+ input: redactStoredContent(obs.input),
294
+ output: redactStoredContent(obs.output),
295
+ });
252
296
  await this.writeSession(id, data);
253
297
  });
254
298
  }
@@ -256,9 +300,16 @@ export class SessionStore {
256
300
  return this.withLock(id, async () => {
257
301
  const data = await this.readSession(id);
258
302
  data.record.status = status;
303
+ // S4-at-rest: redact finalText before persisting.
259
304
  if (finalText !== undefined)
260
- data.record.finalText = finalText;
305
+ data.record.finalText = redact(finalText);
261
306
  await this.writeSession(id, data);
307
+ // Terminal states release the cross-process lock (no destructor exists
308
+ // to do it — the session is done, so the lock is stale by definition).
309
+ if (status === 'complete' || status === 'aborted' || status === 'max_steps' ||
310
+ status === 'verify_failed' || status === 'stuck') {
311
+ await releaseSessionLock(this.dir, id);
312
+ }
262
313
  });
263
314
  }
264
315
  async loadMessages(id) {
@@ -278,9 +329,85 @@ export class SessionStore {
278
329
  const idx = await this.readIndex();
279
330
  return idx[id] ?? null;
280
331
  }
281
- /** Atomic append — survives crashes; suitable for audit log. */
332
+ /** Atomic append + fsync — survives crashes; suitable for audit log. */
282
333
  static async appendJsonl(filePath, entry) {
283
334
  await fs.mkdir(path.dirname(filePath), { recursive: true });
284
- await fs.appendFile(filePath, JSON.stringify(entry) + '\n', 'utf-8');
335
+ const fh = await fs.open(filePath, 'a');
336
+ try {
337
+ await fh.write(JSON.stringify(entry) + '\n');
338
+ await fh.sync();
339
+ }
340
+ finally {
341
+ await fh.close();
342
+ }
343
+ }
344
+ }
345
+ /**
346
+ * Redact text blocks of a stored message at the persist boundary.
347
+ * Handles string content, `{ text }` blocks, arrays of blocks, and plain
348
+ * objects (observation input/output) via a deep walk over string leaves.
349
+ * Only secret-shaped strings (key/token/password patterns, [:=-] shapes)
350
+ * are touched — normal prose passes through unchanged.
351
+ */
352
+ export function redactStoredContent(content) {
353
+ if (typeof content === 'string')
354
+ return redact(content);
355
+ if (Array.isArray(content)) {
356
+ return content.map((b) => {
357
+ if (b && typeof b === 'object' && typeof b.text === 'string') {
358
+ return { ...b, text: redact(b.text) };
359
+ }
360
+ if (typeof b === 'string')
361
+ return redact(b);
362
+ if (b && typeof b === 'object')
363
+ return redactStoredContent(b);
364
+ return b;
365
+ });
366
+ }
367
+ if (content && typeof content === 'object') {
368
+ const rec = content;
369
+ if (typeof rec.text === 'string') {
370
+ return { ...rec, text: redact(rec.text) };
371
+ }
372
+ const out = {};
373
+ for (const [k, v] of Object.entries(rec)) {
374
+ out[k] = typeof v === 'string' ? redact(v) : (v && typeof v === 'object' ? redactStoredContent(v) : v);
375
+ }
376
+ return out;
377
+ }
378
+ return content;
379
+ }
380
+ /**
381
+ * Cross-process session lock probe. The lock file `<sessionsDir>/<id>.lock`
382
+ * contains the holder's PID. `alive` is resolved via `process.kill(pid, 0)`:
383
+ * EPERM means alive-but-unpermitted (→ true); ESRCH / other errors → false.
384
+ */
385
+ export function readSessionLock(sessionsDir, id) {
386
+ let raw;
387
+ try {
388
+ raw = fsSync.readFileSync(path.join(sessionsDir, `${id}.lock`), 'utf-8');
389
+ }
390
+ catch {
391
+ return { held: false };
392
+ }
393
+ const pid = Number(raw.trim());
394
+ if (!Number.isInteger(pid) || pid <= 0)
395
+ return { held: true };
396
+ let alive;
397
+ try {
398
+ process.kill(pid, 0);
399
+ alive = true;
400
+ }
401
+ catch (err) {
402
+ const code = err?.code;
403
+ alive = code === 'EPERM';
404
+ }
405
+ return { held: true, pid, alive };
406
+ }
407
+ /** Best-effort release of the cross-process session lock. Never throws. */
408
+ export async function releaseSessionLock(sessionsDir, id) {
409
+ try {
410
+ await fs.unlink(path.join(sessionsDir, `${id}.lock`));
285
411
  }
412
+ catch { /* ignore */ }
286
413
  }
@@ -25,6 +25,20 @@ export interface ApprovalRequest {
25
25
  export interface ApprovalPrompt {
26
26
  ask(req: ApprovalRequest): Promise<ApprovalChoice>;
27
27
  }
28
+ /**
29
+ * Sanitize untrusted tool text before showing it in an approval prompt.
30
+ * Strips ANSI escapes (CSI `ESC[...letter` and OSC `ESC]...BEL`), control
31
+ * chars except `\n`/`\t`, and truncates to 500 chars with a hidden-count
32
+ * marker so prompt-injection / terminal-escape payloads can't ride along.
33
+ */
34
+ export declare function sanitizeForPrompt(s: string): string;
35
+ /**
36
+ * Single-key approval mapping shared by the TUI modal (and documented here
37
+ * so stdin stays consistent): lowercase `a` = session-only, UPPERCASE `A` =
38
+ * persist to settings. Case MUST be checked before lowercasing — the old TUI
39
+ * code lowercased first, so `A` silently became session-only `always`.
40
+ */
41
+ export declare function approvalChoiceForKey(inputStr: string): ApprovalChoice | 'expand' | 'explain' | null;
28
42
  /** Default prompt backed by readline on stdin/stdout. */
29
43
  export declare class StdinApprovalPrompt implements ApprovalPrompt {
30
44
  ask(req: ApprovalRequest): Promise<ApprovalChoice>;
@@ -8,6 +8,45 @@
8
8
  import * as readline from 'node:readline/promises';
9
9
  import { stdin as input, stdout as output } from 'node:process';
10
10
  import { patternForCall } from './patterns.js';
11
+ /**
12
+ * Sanitize untrusted tool text before showing it in an approval prompt.
13
+ * Strips ANSI escapes (CSI `ESC[...letter` and OSC `ESC]...BEL`), control
14
+ * chars except `\n`/`\t`, and truncates to 500 chars with a hidden-count
15
+ * marker so prompt-injection / terminal-escape payloads can't ride along.
16
+ */
17
+ export function sanitizeForPrompt(s) {
18
+ const noAnsi = s
19
+ .replace(/\x1b\[[0-9;]*[A-Za-z]/g, '')
20
+ .replace(/\x1b\][^\x07]*\x07/g, '');
21
+ const clean = noAnsi.replace(/[\x00-\x08\x0b-\x0d\x0e-\x1f\x7f]/g, '');
22
+ const LIMIT = 500;
23
+ if (clean.length > LIMIT) {
24
+ return clean.slice(0, LIMIT) + `…[+${clean.length - LIMIT} hidden]`;
25
+ }
26
+ return clean;
27
+ }
28
+ /**
29
+ * Single-key approval mapping shared by the TUI modal (and documented here
30
+ * so stdin stays consistent): lowercase `a` = session-only, UPPERCASE `A` =
31
+ * persist to settings. Case MUST be checked before lowercasing — the old TUI
32
+ * code lowercased first, so `A` silently became session-only `always`.
33
+ */
34
+ export function approvalChoiceForKey(inputStr) {
35
+ if (inputStr === 'A')
36
+ return 'always-persist';
37
+ const c = inputStr.toLowerCase();
38
+ if (c === 'y')
39
+ return 'allow';
40
+ if (c === 'a')
41
+ return 'always';
42
+ if (c === 'd' || c === 'n' || c === 'e')
43
+ return 'deny';
44
+ if (c === 'f')
45
+ return 'expand';
46
+ if (c === '?')
47
+ return 'explain';
48
+ return null;
49
+ }
11
50
  /** Default prompt backed by readline on stdin/stdout. */
12
51
  export class StdinApprovalPrompt {
13
52
  async ask(req) {
@@ -15,10 +54,13 @@ export class StdinApprovalPrompt {
15
54
  return 'deny';
16
55
  const rl = readline.createInterface({ input, output });
17
56
  try {
18
- process.stdout.write(`\n[approval needed] ${req.toolName}: ${req.summary}\n reason: ${req.reason}\n allow? [y/n/a(llow)] `);
19
- const ans = (await rl.question('')).trim().toLowerCase();
57
+ process.stdout.write(`\n[approval needed] ${req.toolName}: ${sanitizeForPrompt(req.summary)}\n reason: ${sanitizeForPrompt(req.reason)}\n allow? [y/n/a/A(llow always → settings)] (y=once, n=deny, a=session, A=persist) `);
58
+ const raw = (await rl.question('')).trim();
59
+ const ans = raw.toLowerCase();
20
60
  if (ans === 'y' || ans === 'yes')
21
61
  return 'allow';
62
+ if (raw === 'A')
63
+ return 'always-persist';
22
64
  if (ans === 'a' || ans === 'allow')
23
65
  return 'always';
24
66
  return 'deny';
@@ -83,6 +83,7 @@ export declare class PolicyEngine {
83
83
  }
84
84
  /** Builtin set of rules. Order matters: first match wins. */
85
85
  export declare function builtinRules(): PolicyRule[];
86
+ export declare function matchesGlobRule(call: ToolCallLike, rule: string): boolean;
86
87
  /** Hard-deny for obviously destructive shell patterns. */
87
88
  export declare const shellDenyRule: PolicyRule;
88
89
  /** Allowlist for shell — exact or prefix. Anything not in the list asks. */
@@ -80,17 +80,33 @@ export class PolicyEngine {
80
80
  const globDecision = this.evaluateGlobRules(call);
81
81
  if (globDecision)
82
82
  return globDecision;
83
- // 3.4 — .env guard: deny writes to .env files unless explicitly allowed
84
- if ((call.name === 'write_file' || call.name === 'edit_file') && typeof call.input.path === 'string') {
85
- const p = String(call.input.path);
86
- if (/(^|\/)\.env(\.|$)/.test(p) || p.endsWith('.env')) {
87
- // Check if explicitly allowed via allow list
88
- const allowed = (ctx.config.allow ?? []).some((r) => r.includes('.env'));
89
- if (!allowed) {
83
+ // Centralized .env guard: any tool with a string `path` input pointing
84
+ // at an env file is denied unless an allow rule explicitly names that
85
+ // tool+path (exact/glob match — substring `.includes('.env')` is NOT enough).
86
+ const maybePath = typeof call.input.path === 'string' ? String(call.input.path) : null;
87
+ if (maybePath !== null && isEnvPath(maybePath)) {
88
+ const allowed = isExplicitlyAllowedForPath(call.name, maybePath, ctx.config.allow);
89
+ if (!allowed) {
90
+ return { action: 'deny', reason: 'write to .env denied by policy — add to allow list or use --yolo' };
91
+ }
92
+ }
93
+ // apply_patch carries paths inside the patch text, not `input.path`.
94
+ if (call.name === 'apply_patch' && typeof call.input.patch === 'string') {
95
+ const targets = extractPatchPaths(String(call.input.patch)).filter(isEnvPath);
96
+ if (targets.length > 0) {
97
+ const allAllowed = targets.every((t) => isExplicitlyAllowedForPath(call.name, t, ctx.config.allow));
98
+ if (!allAllowed) {
90
99
  return { action: 'deny', reason: 'write to .env denied by policy — add to allow list or use --yolo' };
91
100
  }
92
101
  }
93
102
  }
103
+ // Shell redirection into .env (e.g. `echo x > .env`, `cmd >> .env.local`).
104
+ if (call.name === 'shell_exec' && typeof call.input.command === 'string') {
105
+ const cmd = String(call.input.command);
106
+ if (/>+\s*['"]?[^'"\s]*\.env/i.test(cmd)) {
107
+ return { action: 'deny', reason: 'write to .env via shell redirection denied' };
108
+ }
109
+ }
94
110
  for (const rule of this.rules) {
95
111
  const d = rule.evaluate(call, ctx);
96
112
  if (d)
@@ -157,7 +173,32 @@ export function builtinRules() {
157
173
  function asString(v) {
158
174
  return typeof v === 'string' ? v : '';
159
175
  }
160
- function matchesGlobRule(call, rule) {
176
+ function isEnvPath(p) {
177
+ const lower = p.toLowerCase();
178
+ // Sample/template carve-out: `.env.example`-style files are documentation,
179
+ // not secrets — reads AND writes are allowed.
180
+ if (/[.](example|sample|template|dist)$/.test(lower))
181
+ return false;
182
+ return /(^|\/)\.env(\.|$)/.test(lower) || lower.endsWith('.env');
183
+ }
184
+ function extractPatchPaths(patch) {
185
+ const out = [];
186
+ for (const line of patch.split('\n')) {
187
+ if (line.startsWith('*** Update File:'))
188
+ out.push(line.replace('*** Update File:', '').trim());
189
+ else if (line.startsWith('*** Add File:'))
190
+ out.push(line.replace('*** Add File:', '').trim());
191
+ }
192
+ return out.filter(Boolean);
193
+ }
194
+ /** True when an allow rule explicitly names this tool+path (exact/glob match, not substring). */
195
+ function isExplicitlyAllowedForPath(callName, envPath, allow) {
196
+ if (!allow || allow.length === 0)
197
+ return false;
198
+ const synthetic = { name: callName, input: { path: envPath } };
199
+ return allow.some((r) => matchesGlobRule(synthetic, r));
200
+ }
201
+ export function matchesGlobRule(call, rule) {
161
202
  // Rule grammar: tool or tool(glob). e.g. "write_file", "write_file(.env)", "shell_exec(npm *)".
162
203
  // Digits are allowed so generated names (e.g. mcp__server__tool2) can be targeted.
163
204
  const m = /^([a-z0-9_]+)(?:\((.*)\))?$/.exec(rule.trim());
@@ -204,6 +245,45 @@ export const shellDenyRule = {
204
245
  if (/\b(curl|wget|fetch|iwr|Invoke-WebRequest)\b.*\|\s*(sh|bash|zsh|cmd|powershell|pwsh|node|node\s+-e)\b/i.test(cmd)) {
205
246
  return { action: 'deny', reason: 'pipe-to-shell: remote download piped into an interpreter' };
206
247
  }
248
+ // Exfiltration denylist (mirrors shell_exec DANGEROUS_PATTERNS).
249
+ if (/\bcurl\b.*(?:\s-d\b|\s--data(?:-binary|-urlencode)?\b|\s--upload-file\b)/i.test(cmd)) {
250
+ return { action: 'deny', reason: 'exfiltration: curl with --data/-d/--upload-file denied' };
251
+ }
252
+ if (/\bwget\b.*--post-(data|file)\b/i.test(cmd)) {
253
+ return { action: 'deny', reason: 'exfiltration: wget --post-data/--post-file denied' };
254
+ }
255
+ if (/\b(powershell|pwsh)\b.*-e(nc|ncodedcommand)\b/i.test(cmd)) {
256
+ return { action: 'deny', reason: 'exfiltration: powershell -enc denied' };
257
+ }
258
+ if (/\bInvoke-Expression\b/i.test(cmd) || /\biex\s*\(/i.test(cmd)) {
259
+ return { action: 'deny', reason: 'exfiltration: Invoke-Expression/iex denied' };
260
+ }
261
+ if (/\bcertutil\b.*-urlcache\b/i.test(cmd)) {
262
+ return { action: 'deny', reason: 'exfiltration: certutil -urlcache denied' };
263
+ }
264
+ if (/\bbitsadmin\b.*\/transfer\b/i.test(cmd)) {
265
+ return { action: 'deny', reason: 'exfiltration: bitsadmin /transfer denied' };
266
+ }
267
+ // .env writes via tee / PowerShell (mirrors shell_exec DANGEROUS_PATTERNS).
268
+ if (/\|\s*tee\b[^\n]*\.env/i.test(cmd)) {
269
+ return { action: 'deny', reason: 'write to .env via tee denied' };
270
+ }
271
+ if (/\b(Set-Content|Out-File)\b[^\n]*\.env/i.test(cmd)) {
272
+ return { action: 'deny', reason: 'write to .env via Set-Content/Out-File denied' };
273
+ }
274
+ // Upload-form exfiltration (mirrors shell_exec DANGEROUS_PATTERNS).
275
+ if (/\bcurl\b.*(?:\s-F\b|\s--form\b)/i.test(cmd)) {
276
+ return { action: 'deny', reason: 'exfiltration: curl -F/--form denied' };
277
+ }
278
+ if (/\bwget\b.*(?:\s--method=POST\b|\s--body-data\b)/i.test(cmd)) {
279
+ return { action: 'deny', reason: 'exfiltration: wget --method=POST/--body-data denied' };
280
+ }
281
+ if (/\bInvoke-RestMethod\b/i.test(cmd)) {
282
+ return { action: 'deny', reason: 'exfiltration: Invoke-RestMethod denied' };
283
+ }
284
+ if (/\bStart-BitsTransfer\b/i.test(cmd)) {
285
+ return { action: 'deny', reason: 'exfiltration: Start-BitsTransfer denied' };
286
+ }
207
287
  return null;
208
288
  },
209
289
  };
@@ -24,6 +24,10 @@ const PATTERNS = [
24
24
  { name: 'api-key', re: /(?:api[_-]?key|apikey)\s*[:=]\s*['"]?[A-Za-z0-9_\-]{16,}['"]?/gi },
25
25
  { name: 'password', re: /(?:password|passwd|pwd)\s*[:=]\s*['"]?[^\s'"]{4,}['"]?/gi },
26
26
  { name: 'secret-generic', re: /(?:secret|token)\s*[:=]\s*['"]?[A-Za-z0-9_\-+/=]{16,}['"]?/gi },
27
+ { name: 'discord-token', re: /mfa\.[\w-]{84}|[MN][A-Za-z\d]{23}\.[\w-]{6}\.[\w-]{27}/g },
28
+ { name: 'npm-token', re: /\bnpm_[A-Za-z0-9]{24,}/g },
29
+ { name: 'sendgrid-key', re: /\bSG\.[\w-]{22}\.[\w-]{43}/g },
30
+ { name: 'pypi-token', re: /\bpypi-[A-Za-z0-9_-]{30,}/g },
27
31
  ];
28
32
  const REPLACEMENT = '[REDACTED]';
29
33
  /** Redact a single string (or Buffer). */
@@ -15,4 +15,21 @@ export interface ModelInfo {
15
15
  }
16
16
  export declare const MODEL_REGISTRY: Record<string, ModelInfo>;
17
17
  export declare function getModelInfo(id: string): ModelInfo;
18
+ /**
19
+ * Single-source per-1K-token rate table, derived from MODEL_REGISTRY so
20
+ * prices live in exactly one place. Local/self-hosted entries are $0.
21
+ */
22
+ export declare const MODEL_RATES_PER_1K: Record<string, {
23
+ input: number;
24
+ output: number;
25
+ }>;
26
+ /**
27
+ * Resolve per-1K rates for any model id. Exact registry hits win, then the
28
+ * local-model check ($0), then hosted-family fallbacks. Unknown models are
29
+ * $0 — never invent nonzero prices for models we don't recognize.
30
+ */
31
+ export declare function ratesFor(modelId: string): {
32
+ input: number;
33
+ output: number;
34
+ };
18
35
  export declare function estimateCost(modelId: string, inputTokens: number, outputTokens: number): number;