@pugi/cli 0.1.0-beta.2 → 0.1.0-beta.20

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 (130) hide show
  1. package/THIRD_PARTY_NOTICES.md +40 -0
  2. package/assets/pugi-mascot.ansi +15 -40
  3. package/bin/run.js +33 -1
  4. package/dist/commands/jobs-watch.js +201 -0
  5. package/dist/commands/jobs.js +15 -0
  6. package/dist/core/agent-progress/cleanup.js +134 -0
  7. package/dist/core/agent-progress/schema.js +144 -0
  8. package/dist/core/agent-progress/writer.js +101 -0
  9. package/dist/core/compact/auto-trigger.js +96 -0
  10. package/dist/core/compact/buffer-rewriter.js +115 -0
  11. package/dist/core/compact/summarizer.js +196 -0
  12. package/dist/core/compact/token-counter.js +108 -0
  13. package/dist/core/consensus/diff-capture.js +73 -0
  14. package/dist/core/context/index.js +7 -0
  15. package/dist/core/context/markdown-traverse.js +255 -0
  16. package/dist/core/cost/rate-card.js +129 -0
  17. package/dist/core/cost/tracker.js +221 -0
  18. package/dist/core/denial-tracking/index.js +8 -0
  19. package/dist/core/denial-tracking/state.js +264 -0
  20. package/dist/core/diagnostics/probe-runner.js +93 -0
  21. package/dist/core/diagnostics/probes/api.js +46 -0
  22. package/dist/core/diagnostics/probes/auth.js +86 -0
  23. package/dist/core/diagnostics/probes/cli-version.js +127 -0
  24. package/dist/core/diagnostics/probes/config.js +72 -0
  25. package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
  26. package/dist/core/diagnostics/probes/disk.js +81 -0
  27. package/dist/core/diagnostics/probes/git.js +65 -0
  28. package/dist/core/diagnostics/probes/mcp.js +75 -0
  29. package/dist/core/diagnostics/probes/node.js +59 -0
  30. package/dist/core/diagnostics/probes/pnpm.js +36 -0
  31. package/dist/core/diagnostics/probes/session.js +74 -0
  32. package/dist/core/diagnostics/probes/status-snapshot.js +442 -0
  33. package/dist/core/diagnostics/probes/workspace.js +63 -0
  34. package/dist/core/diagnostics/types.js +70 -0
  35. package/dist/core/edits/dispatch.js +218 -2
  36. package/dist/core/edits/journal.js +199 -0
  37. package/dist/core/edits/layer-d-ast.js +557 -14
  38. package/dist/core/edits/verify-hook.js +273 -0
  39. package/dist/core/edits/worktree.js +111 -18
  40. package/dist/core/engine/anvil-client.js +115 -5
  41. package/dist/core/engine/budgets.js +89 -0
  42. package/dist/core/engine/context-prefix.js +155 -0
  43. package/dist/core/engine/intent.js +260 -0
  44. package/dist/core/engine/native-pugi.js +744 -210
  45. package/dist/core/engine/prompts.js +61 -6
  46. package/dist/core/engine/strip-internal-fields.js +124 -0
  47. package/dist/core/engine/tool-bridge.js +818 -31
  48. package/dist/core/file-cache.js +113 -1
  49. package/dist/core/init/scaffold.js +195 -0
  50. package/dist/core/lsp/client.js +174 -29
  51. package/dist/core/mcp/client.js +75 -6
  52. package/dist/core/mcp/http-server.js +553 -0
  53. package/dist/core/mcp/permission.js +190 -0
  54. package/dist/core/mcp/registry.js +24 -2
  55. package/dist/core/mcp/server-tools.js +219 -0
  56. package/dist/core/mcp/server.js +397 -0
  57. package/dist/core/permissions/gate.js +187 -0
  58. package/dist/core/permissions/index.js +18 -0
  59. package/dist/core/permissions/mode.js +102 -0
  60. package/dist/core/permissions/state.js +160 -0
  61. package/dist/core/permissions/tool-class.js +93 -0
  62. package/dist/core/repl/codebase-survey.js +308 -0
  63. package/dist/core/repl/history.js +11 -1
  64. package/dist/core/repl/init-interview.js +457 -0
  65. package/dist/core/repl/model-pricing.js +135 -0
  66. package/dist/core/repl/onboarding-state.js +297 -0
  67. package/dist/core/repl/session.js +719 -29
  68. package/dist/core/repl/slash-commands.js +133 -9
  69. package/dist/core/retry-budget/budget.js +284 -0
  70. package/dist/core/retry-budget/index.js +5 -0
  71. package/dist/core/settings.js +71 -0
  72. package/dist/core/skills/defaults.js +457 -0
  73. package/dist/core/subagents/dispatcher-real.js +600 -0
  74. package/dist/core/subagents/dispatcher.js +113 -24
  75. package/dist/core/subagents/index.js +18 -5
  76. package/dist/core/subagents/isolation-matrix.js +213 -0
  77. package/dist/core/subagents/spawn.js +19 -4
  78. package/dist/core/transport/version-interceptor.js +166 -0
  79. package/dist/index.js +28 -0
  80. package/dist/runtime/bootstrap.js +190 -0
  81. package/dist/runtime/cli.js +1588 -266
  82. package/dist/runtime/commands/compact.js +296 -0
  83. package/dist/runtime/commands/cost.js +199 -0
  84. package/dist/runtime/commands/delegate.js +289 -0
  85. package/dist/runtime/commands/doctor.js +369 -0
  86. package/dist/runtime/commands/lsp.js +187 -5
  87. package/dist/runtime/commands/mcp.js +824 -0
  88. package/dist/runtime/commands/patch.js +17 -0
  89. package/dist/runtime/commands/permissions.js +87 -0
  90. package/dist/runtime/commands/report.js +299 -0
  91. package/dist/runtime/commands/review-consensus.js +17 -2
  92. package/dist/runtime/commands/roster.js +117 -0
  93. package/dist/runtime/commands/status.js +178 -0
  94. package/dist/runtime/commands/worktree.js +50 -6
  95. package/dist/runtime/headless.js +543 -0
  96. package/dist/runtime/load-hooks-or-exit.js +71 -0
  97. package/dist/runtime/plan-decompose.js +531 -0
  98. package/dist/runtime/version.js +65 -0
  99. package/dist/tools/agent-tool.js +206 -0
  100. package/dist/tools/apply-patch.js +281 -39
  101. package/dist/tools/ask-user-question.js +213 -0
  102. package/dist/tools/ask-user.js +115 -0
  103. package/dist/tools/file-tools.js +85 -14
  104. package/dist/tools/mcp-tool.js +260 -0
  105. package/dist/tools/multi-edit.js +361 -0
  106. package/dist/tools/registry.js +22 -2
  107. package/dist/tools/skill-tool.js +96 -0
  108. package/dist/tools/tasks.js +208 -0
  109. package/dist/tools/web-fetch.js +147 -2
  110. package/dist/tools/web-search.js +458 -0
  111. package/dist/tui/agent-progress-card.js +111 -0
  112. package/dist/tui/agent-tree.js +10 -0
  113. package/dist/tui/ask-modal.js +2 -2
  114. package/dist/tui/ask-user-question-prompt.js +192 -0
  115. package/dist/tui/compact-banner.js +54 -0
  116. package/dist/tui/conversation-pane.js +69 -8
  117. package/dist/tui/cost-table.js +111 -0
  118. package/dist/tui/doctor-table.js +31 -0
  119. package/dist/tui/input-box.js +1 -1
  120. package/dist/tui/markdown-render.js +4 -4
  121. package/dist/tui/repl-render.js +276 -37
  122. package/dist/tui/repl-splash.js +2 -2
  123. package/dist/tui/repl.js +25 -6
  124. package/dist/tui/splash.js +1 -1
  125. package/dist/tui/status-bar.js +94 -16
  126. package/dist/tui/status-table.js +7 -0
  127. package/dist/tui/tool-stream-pane.js +7 -0
  128. package/dist/tui/update-banner.js +20 -2
  129. package/docs/examples/codegraph.mcp.json +10 -0
  130. package/package.json +9 -6
@@ -0,0 +1,213 @@
1
+ /**
2
+ * AskUserQuestion structured tool — leak L5 (research memo §2.5).
3
+ *
4
+ * Mirrors openclaude's `src/tools/AskUserQuestionTool/prompt.ts`
5
+ * pattern: clarifying questions go through a structured multi-choice
6
+ * tool, NOT free-text prose. The model dispatches the tool with a
7
+ * `question` + a `header` chip + 2-4 `options` (each with `label` +
8
+ * `description`). The UI renders the modal, auto-appends an "Other"
9
+ * fallback for custom text, and surfaces the operator's pick back to
10
+ * the model as a tool_result frame.
11
+ *
12
+ * Why P0 leverage: the structured form forecloses Pugi's recurring
13
+ * "agent rambles instead of dispatching" failure mode at the schema
14
+ * level. When the model is uncertain, the cheapest legal output is
15
+ * `ask_user_question` — not a prose menu, not a fake "Шипану через
16
+ * 8 минут" dispatch promise.
17
+ *
18
+ * Relationship to ask-user.ts (β1 T2):
19
+ * - ask-user.ts is the LEGACY string-array form (`options: string[]`).
20
+ * Kept for back-compat; the existing prompt envelope `<pugi-ask>`
21
+ * and the persona prompts still emit that grammar.
22
+ * - ask-user-question.ts is the STRUCTURED form layered on top. It
23
+ * normalises a {label, description} option into the legacy string
24
+ * before delegating to `askUser`, so the Ink modal does not need
25
+ * two render paths and the abort/timeout race logic is shared.
26
+ *
27
+ * Hard rules (enforced by Zod):
28
+ * - question: 5-500 chars, must end with "?". Plain English.
29
+ * - header: 2-12 chars (short chip label, e.g. "Auth method").
30
+ * - options: 2-4 strict (no more, no less). Mutually exclusive.
31
+ * UI auto-adds "Other" — the model NEVER emits it.
32
+ * - multiSelect: default false.
33
+ */
34
+ import { z } from 'zod';
35
+ import { askUser } from './ask-user.js';
36
+ /** Cap matches the Ink modal layout: 12 chars fits the header chip. */
37
+ export const ASK_USER_QUESTION_HEADER_MIN = 2;
38
+ export const ASK_USER_QUESTION_HEADER_MAX = 12;
39
+ /** Question must be a real question (ends with ?). 5-500 chars. */
40
+ export const ASK_USER_QUESTION_MIN = 5;
41
+ export const ASK_USER_QUESTION_MAX = 500;
42
+ /** Each option label: 2-40 chars (1-5 words). */
43
+ export const ASK_USER_QUESTION_OPTION_LABEL_MIN = 2;
44
+ export const ASK_USER_QUESTION_OPTION_LABEL_MAX = 40;
45
+ /** Each option description: 10-200 chars (one short sentence). */
46
+ export const ASK_USER_QUESTION_OPTION_DESC_MIN = 10;
47
+ export const ASK_USER_QUESTION_OPTION_DESC_MAX = 200;
48
+ /** Option count: 2-4 strict. UI adds "Other" automatically. */
49
+ export const ASK_USER_QUESTION_OPTIONS_MIN = 2;
50
+ export const ASK_USER_QUESTION_OPTIONS_MAX = 4;
51
+ /**
52
+ * Structured option. `label` is the display text; `description` is the
53
+ * implication line shown dim below it. Both are required — the model
54
+ * cannot ship a label-only option (forces it to think about why each
55
+ * choice exists).
56
+ */
57
+ export const askUserQuestionOptionSchema = z.strictObject({
58
+ label: z
59
+ .string()
60
+ .min(ASK_USER_QUESTION_OPTION_LABEL_MIN)
61
+ .max(ASK_USER_QUESTION_OPTION_LABEL_MAX)
62
+ .describe('Display text. Concise (1-5 words).'),
63
+ description: z
64
+ .string()
65
+ .min(ASK_USER_QUESTION_OPTION_DESC_MIN)
66
+ .max(ASK_USER_QUESTION_OPTION_DESC_MAX)
67
+ .describe('What this option means / implications.'),
68
+ });
69
+ export const askUserQuestionSchema = z.strictObject({
70
+ question: z
71
+ .string()
72
+ .min(ASK_USER_QUESTION_MIN)
73
+ .max(ASK_USER_QUESTION_MAX)
74
+ .refine((q) => q.trim().endsWith('?'), {
75
+ message: 'question must end with "?"',
76
+ })
77
+ .describe('The complete question. Must end with "?". Plain English, no jargon.'),
78
+ header: z
79
+ .string()
80
+ .min(ASK_USER_QUESTION_HEADER_MIN)
81
+ .max(ASK_USER_QUESTION_HEADER_MAX)
82
+ .describe('Short chip label (max 12 chars). E.g. "Auth method".'),
83
+ options: z
84
+ .array(askUserQuestionOptionSchema)
85
+ .min(ASK_USER_QUESTION_OPTIONS_MIN)
86
+ .max(ASK_USER_QUESTION_OPTIONS_MAX)
87
+ .describe('2-4 mutually-exclusive options. NEVER add "Other" — UI auto-adds.'),
88
+ multiSelect: z
89
+ .boolean()
90
+ .optional()
91
+ .default(false)
92
+ .describe('Allow multiple selections. Default false.'),
93
+ });
94
+ /**
95
+ * Dispatch the structured tool: validate args via Zod, then route
96
+ * through the shared `askUser` primitive so abort/timeout/non-TTY
97
+ * envelope behaviour is identical to the legacy form.
98
+ *
99
+ * The bridge surface is the same `AskUserBridge` signature — the
100
+ * structured form just gives the Ink modal richer metadata to render
101
+ * (header chip + per-option description). The bridge sees the legacy
102
+ * `{question, options: string[]}` shape because all production bridges
103
+ * (Ink modal + non-TTY envelope emitter) already consume that shape.
104
+ * Per-option descriptions and the header chip are surfaced separately
105
+ * via `enrich` — the modal layer reads them off the dispatched payload
106
+ * stash, NOT off the bridge input, so structured callers can layer on
107
+ * top of the legacy interface without touching the modal contract.
108
+ *
109
+ * Return contract:
110
+ * - Interactive + bridge present + operator picks N options →
111
+ * `[ask_user_question:answered] <labels joined by ", ">`.
112
+ * - Interactive + bridge present + operator cancels →
113
+ * `[ask_user_question:cancelled]`.
114
+ * - Interactive + bridge present + timeout →
115
+ * `[ask_user_question:timeout]`.
116
+ * - Non-TTY or no bridge → `[user_input_required]<json>[/...]`
117
+ * envelope identical to the legacy form. Includes `header` +
118
+ * structured options so a scripted caller can parse the full shape.
119
+ */
120
+ export async function dispatchAskUserQuestion(ctx, rawArgs) {
121
+ const parsed = askUserQuestionSchema.parse(rawArgs);
122
+ // Schema-level guard against the "Other" leak: the prompt rules tell
123
+ // the model NEVER to include "Other" in `options`, but we still reject
124
+ // it defensively in case a future model misreads the spec. The Ink
125
+ // modal auto-appends "Other" itself; a model-supplied duplicate would
126
+ // render two "Other" rows.
127
+ for (const opt of parsed.options) {
128
+ const trimmed = opt.label.trim().toLowerCase();
129
+ if (trimmed === 'other' || trimmed === 'другое') {
130
+ throw new Error('ask_user_question: do NOT include "Other" in options — UI auto-adds it');
131
+ }
132
+ }
133
+ const legacyOptions = parsed.options.map((opt) => opt.label);
134
+ const result = await askUser(ctx, {
135
+ question: parsed.question,
136
+ options: legacyOptions,
137
+ multiSelect: parsed.multiSelect ?? false,
138
+ });
139
+ if (result.answers && result.answers.length > 0) {
140
+ return {
141
+ answers: result.answers,
142
+ envelope: `[ask_user_question:answered] ${result.answers.join(', ')}`,
143
+ };
144
+ }
145
+ // Non-TTY / cancelled / timeout. Re-wrap the envelope so callers can
146
+ // grep for the structured tool name even when the underlying primitive
147
+ // surfaced its legacy `[user_input_required]` envelope.
148
+ if (result.envelope.includes('"reason":"timeout"')) {
149
+ return { envelope: '[ask_user_question:timeout]' };
150
+ }
151
+ if (result.envelope.includes('"reason":"cancelled"')) {
152
+ return { envelope: '[ask_user_question:cancelled]' };
153
+ }
154
+ // Default to the legacy envelope verbatim — it is still
155
+ // grep-friendly and includes the structured payload above.
156
+ return { envelope: result.envelope };
157
+ }
158
+ /**
159
+ * JSON-Schema fragment surfaced to the model via the tool-bridge
160
+ * `parameters` field. Mirrors the Zod schema 1:1 — kept hand-written
161
+ * because the runtime engine wires OpenAI-compatible JSON Schema and
162
+ * the Zod-to-JSON-Schema converter pulls in a transitive dep we have
163
+ * not greenlit. If the Zod schema above changes, mirror the change here.
164
+ */
165
+ export const askUserQuestionJsonSchema = {
166
+ type: 'object',
167
+ additionalProperties: false,
168
+ required: ['question', 'header', 'options'],
169
+ properties: {
170
+ question: {
171
+ type: 'string',
172
+ minLength: ASK_USER_QUESTION_MIN,
173
+ maxLength: ASK_USER_QUESTION_MAX,
174
+ description: 'The complete question. Must end with "?". Plain English, no jargon.',
175
+ },
176
+ header: {
177
+ type: 'string',
178
+ minLength: ASK_USER_QUESTION_HEADER_MIN,
179
+ maxLength: ASK_USER_QUESTION_HEADER_MAX,
180
+ description: 'Short chip label (max 12 chars). E.g. "Auth method".',
181
+ },
182
+ options: {
183
+ type: 'array',
184
+ minItems: ASK_USER_QUESTION_OPTIONS_MIN,
185
+ maxItems: ASK_USER_QUESTION_OPTIONS_MAX,
186
+ items: {
187
+ type: 'object',
188
+ additionalProperties: false,
189
+ required: ['label', 'description'],
190
+ properties: {
191
+ label: {
192
+ type: 'string',
193
+ minLength: ASK_USER_QUESTION_OPTION_LABEL_MIN,
194
+ maxLength: ASK_USER_QUESTION_OPTION_LABEL_MAX,
195
+ description: 'Display text. Concise (1-5 words).',
196
+ },
197
+ description: {
198
+ type: 'string',
199
+ minLength: ASK_USER_QUESTION_OPTION_DESC_MIN,
200
+ maxLength: ASK_USER_QUESTION_OPTION_DESC_MAX,
201
+ description: 'What this option means / implications.',
202
+ },
203
+ },
204
+ },
205
+ description: '2-4 mutually-exclusive options. NEVER add "Other" — UI auto-adds.',
206
+ },
207
+ multiSelect: {
208
+ type: 'boolean',
209
+ description: 'Allow multiple selections. Default false.',
210
+ },
211
+ },
212
+ };
213
+ //# sourceMappingURL=ask-user-question.js.map
@@ -0,0 +1,115 @@
1
+ export const ASK_USER_DEFAULT_TIMEOUT_MS = 5 * 60 * 1_000;
2
+ /**
3
+ * Schema cap: keep the option count tight so the modal stays scannable.
4
+ * Mirrors `ASK_MAX_OPTIONS` in `core/repl/ask.ts` (4).
5
+ */
6
+ export const ASK_USER_MAX_OPTIONS = 4;
7
+ export const ASK_USER_MAX_QUESTION_LEN = 1_000;
8
+ export const ASK_USER_MAX_OPTION_LEN = 200;
9
+ export async function askUser(ctx, input) {
10
+ validate(input);
11
+ if (ctx.interactive && ctx.bridge) {
12
+ // β1a r1 (2026-05-26): wrap the bridge in an abort-aware race so a
13
+ // pending modal cannot block the engine loop forever. Two signals
14
+ // can interrupt:
15
+ // 1. `ctx.signal` — the operator cancelled the parent task via
16
+ // Ctrl-C; the engine forwards the loop's AbortSignal here.
17
+ // 2. `ctx.timeoutMs` (default 5 minutes) — operator walked away;
18
+ // the modal stays renderable but the tool surface returns the
19
+ // `cancelled` envelope so the model can make progress.
20
+ // The bridge receives the same `signal` so an Ink-based modal can
21
+ // tear down its render loop and free its keyboard handlers on
22
+ // abort. Bridges that ignore the signal still get pre-empted by
23
+ // the race — they just leak a render until the next operator
24
+ // keystroke.
25
+ const timeoutMs = ctx.timeoutMs ?? ASK_USER_DEFAULT_TIMEOUT_MS;
26
+ // Pre-flight: short-circuit when the caller's signal is already
27
+ // aborted. Avoids constructing a bridge promise that races against
28
+ // an already-resolved abort sentinel — the race ordering is
29
+ // unspecified for promises that have all settled by the time
30
+ // Promise.race is called, which would non-deterministically let
31
+ // the bridge's answer leak through after an explicit cancel.
32
+ if (ctx.signal?.aborted) {
33
+ return { envelope: formatEnvelope(input, 'cancelled') };
34
+ }
35
+ const controller = new AbortController();
36
+ if (ctx.signal) {
37
+ ctx.signal.addEventListener('abort', () => controller.abort(), { once: true });
38
+ }
39
+ let timeoutHandle;
40
+ const timeoutPromise = new Promise((resolve) => {
41
+ timeoutHandle = setTimeout(() => resolve('timeout'), timeoutMs);
42
+ });
43
+ const abortPromise = new Promise((resolve) => {
44
+ controller.signal.addEventListener('abort', () => resolve('aborted'), { once: true });
45
+ });
46
+ let picked;
47
+ try {
48
+ picked = await Promise.race([
49
+ ctx.bridge(input, { signal: controller.signal }),
50
+ timeoutPromise,
51
+ abortPromise,
52
+ ]);
53
+ }
54
+ finally {
55
+ if (timeoutHandle)
56
+ clearTimeout(timeoutHandle);
57
+ }
58
+ if (picked === 'timeout') {
59
+ controller.abort();
60
+ return { envelope: formatEnvelope(input, 'timeout') };
61
+ }
62
+ if (picked === 'aborted') {
63
+ return { envelope: formatEnvelope(input, 'cancelled') };
64
+ }
65
+ if (!Array.isArray(picked) || picked.length === 0) {
66
+ // Operator declined / closed the modal — surface a structured
67
+ // "no answer" envelope so the model can decide whether to retry.
68
+ const envelope = formatEnvelope(input, 'cancelled');
69
+ return { envelope };
70
+ }
71
+ return {
72
+ answers: picked,
73
+ envelope: formatAnswer(picked),
74
+ };
75
+ }
76
+ // Non-TTY or no bridge — surface the envelope. Caller parses it and
77
+ // either pipes an answer back on a follow-up turn or aborts.
78
+ const envelope = formatEnvelope(input, 'no_tty');
79
+ return { envelope };
80
+ }
81
+ function validate(input) {
82
+ const question = input.question?.trim();
83
+ if (!question)
84
+ throw new Error('ask_user: question is required');
85
+ if (question.length > ASK_USER_MAX_QUESTION_LEN) {
86
+ throw new Error(`ask_user: question exceeds ${ASK_USER_MAX_QUESTION_LEN} char cap`);
87
+ }
88
+ if (!Array.isArray(input.options) || input.options.length < 2) {
89
+ throw new Error('ask_user: at least 2 options required');
90
+ }
91
+ if (input.options.length > ASK_USER_MAX_OPTIONS) {
92
+ throw new Error(`ask_user: at most ${ASK_USER_MAX_OPTIONS} options allowed`);
93
+ }
94
+ for (const opt of input.options) {
95
+ if (typeof opt !== 'string' || !opt.trim()) {
96
+ throw new Error('ask_user: every option must be a non-empty string');
97
+ }
98
+ if (opt.length > ASK_USER_MAX_OPTION_LEN) {
99
+ throw new Error(`ask_user: option exceeds ${ASK_USER_MAX_OPTION_LEN} char cap`);
100
+ }
101
+ }
102
+ }
103
+ function formatEnvelope(input, reason) {
104
+ const payload = {
105
+ question: input.question,
106
+ options: input.options,
107
+ multiSelect: input.multiSelect === true,
108
+ reason,
109
+ };
110
+ return `[user_input_required]${JSON.stringify(payload)}[/user_input_required]`;
111
+ }
112
+ function formatAnswer(answers) {
113
+ return answers.join(', ');
114
+ }
115
+ //# sourceMappingURL=ask-user.js.map
@@ -1,9 +1,37 @@
1
+ /**
2
+ * file-tools - Pugi CLI file/bash/glob/grep tool surface.
3
+ *
4
+ * Workspace-binding contract (CEO red-alert 2026-05-27 follow-up):
5
+ *
6
+ * Every tool dispatch path threads `ctx.root` from the operator's
7
+ * `process.cwd()` through `EngineTask.workspaceRoot` ->
8
+ * `native-pugi.run()` -> `toolCtx.root` -> here. Tools call
9
+ * `resolveWorkspacePath(ctx.root, path)` for every on-disk operation
10
+ * so a dispatched specialist (e.g. Hiroshi writing tic-tac-toe HTML)
11
+ * produces files in the OPERATOR'S cwd, never in a server-side temp
12
+ * space. The path-security gate refuses traversal (`../etc/passwd`,
13
+ * URL-encoded variants, symlink escapes at the target).
14
+ *
15
+ * Wiring chain:
16
+ * 1. runtime/cli.ts: workspaceRoot = process.cwd()
17
+ * 2. EngineTask.workspaceRoot threads through to native-pugi.run().
18
+ * 3. native-pugi: const root = task.workspaceRoot
19
+ * 4. tool-bridge: passes ctx.root to file-tools / bash.
20
+ * 5. file-tools: resolveWorkspacePath(ctx.root, path).
21
+ *
22
+ * The contract is locked by `test/tools-write-to-workspace.spec.ts`
23
+ * (6 cases covering relative + nested + absolute paths + traversal
24
+ * refusal). If any layer of the chain regressed silently, dispatched
25
+ * files would land in `/tmp` instead of the operator's repo, which
26
+ * is the same failure surface as the menu-mode anti-pattern the
27
+ * sibling commits close.
28
+ */
1
29
  import { spawnSync } from 'node:child_process';
2
- import { existsSync, readFileSync, realpathSync, renameSync, writeFileSync } from 'node:fs';
30
+ import { existsSync, readFileSync, realpathSync, renameSync, statSync, writeFileSync } from 'node:fs';
3
31
  import { dirname, isAbsolute, relative } from 'node:path';
4
32
  import { globSync } from 'node:fs';
5
33
  import { decidePermission } from '../core/permission.js';
6
- import { createReadRecord, hashContent } from '../core/file-cache.js';
34
+ import { StaleReadError, createReadRecord, hashContent, } from '../core/file-cache.js';
7
35
  import { resolveWorkspacePath } from '../core/path-security.js';
8
36
  import { recordFileMutation, recordToolCall, recordToolResult } from '../core/session.js';
9
37
  /**
@@ -19,6 +47,11 @@ export class OperatorAbortedError extends Error {
19
47
  this.name = 'OperatorAbortedError';
20
48
  }
21
49
  }
50
+ // Re-export StaleReadError so tool-bridge / test consumers can import
51
+ // the typed error from a single file-tools surface alongside
52
+ // OperatorAbortedError. Same shape as the existing OperatorAbortedError
53
+ // re-surface pattern.
54
+ export { StaleReadError } from '../core/file-cache.js';
22
55
  /**
23
56
  * α6.9 WriteGate: refuse the tool dispatch when the active
24
57
  * cancellation token has aborted. Idempotent (the token's `isAborted`
@@ -124,10 +157,37 @@ export function writeTool(ctx, path, content) {
124
157
  throw error;
125
158
  }
126
159
  const existed = existsSync(resolved);
127
- const before = existed ? readFileSync(resolved, 'utf8') : undefined;
160
+ // Leak L1 stale-read gate for writeTool's update-existing path. The
161
+ // model uses writeTool for two distinct intents:
162
+ //
163
+ // - create-new: path does not exist on disk. There is no prior
164
+ // read to validate against; skip the gate. This is the
165
+ // intentional escape hatch the leak spec also calls out.
166
+ // - overwrite-existing: path exists. Without the gate the model
167
+ // could blind-clobber an externally-modified file, losing the
168
+ // concurrent change silently. Force the model to re-read first.
169
+ //
170
+ // We deliberately apply the SAME stale-validation primitive editTool
171
+ // uses so the two write surfaces stay symmetric and a future fix to
172
+ // either one cannot accidentally weaken the other.
173
+ let before;
174
+ if (existed) {
175
+ before = readFileSync(resolved, 'utf8');
176
+ const currentStat = statSync(resolved);
177
+ const validation = ctx.readCache.validate(ctx.root, path, currentStat.mtimeMs, before);
178
+ if (validation.stale) {
179
+ const reason = `stale_read: write ${path} refused — ${validation.detail}`;
180
+ recordToolResult(ctx.session, toolCallId, 'error', reason);
181
+ throw new StaleReadError(path, validation.reason, validation.detail);
182
+ }
183
+ }
128
184
  const tmp = `${resolved}.pugi-tmp-${Date.now()}`;
129
185
  writeFileSync(tmp, content, { encoding: 'utf8', mode: 0o600 });
130
186
  renameSync(tmp, resolved);
187
+ // Refresh the cache with the post-write content so the model can
188
+ // chain a follow-up read+edit on the same file without an extra
189
+ // round-trip. Same pattern editTool uses below.
190
+ ctx.readCache.set(createReadRecord(ctx.root, path, content, 'read_tool'));
131
191
  recordFileMutation(ctx.session, {
132
192
  toolCallId,
133
193
  path,
@@ -154,10 +214,6 @@ export function editTool(ctx, path, oldString, newString) {
154
214
  recordToolResult(ctx.session, toolCallId, 'error', reason);
155
215
  throw new Error(reason);
156
216
  }
157
- const readRecord = ctx.readCache.get(ctx.root, path);
158
- if (!readRecord) {
159
- throw new Error(`Cannot edit ${path}: file must be read first`);
160
- }
161
217
  let resolved;
162
218
  try {
163
219
  resolved = permissionGatedResolve(ctx, path, 'edit', 'edit');
@@ -167,16 +223,31 @@ export function editTool(ctx, path, oldString, newString) {
167
223
  recordToolResult(ctx.session, toolCallId, 'error', reason);
168
224
  throw error;
169
225
  }
226
+ // Leak L1 stale-read gate. Validate the model's read-time view of
227
+ // the file against the on-disk state BEFORE applying the mutation.
228
+ // We read disk content once and feed it to the validator so a single
229
+ // syscall covers both the gate decision AND the oldString/newString
230
+ // replacement below.
170
231
  const before = readFileSync(resolved, 'utf8');
171
- const currentHash = hashContent(before);
172
- if (currentHash !== readRecord.sha256) {
173
- throw new Error(`Cannot edit ${path}: file changed since last read`);
232
+ const currentStat = statSync(resolved);
233
+ const validation = ctx.readCache.validate(ctx.root, path, currentStat.mtimeMs, before);
234
+ if (validation.stale) {
235
+ const reason = `stale_read: edit ${path} refused — ${validation.detail}`;
236
+ recordToolResult(ctx.session, toolCallId, 'error', reason);
237
+ throw new StaleReadError(path, validation.reason, validation.detail);
174
238
  }
239
+ const currentHash = hashContent(before);
175
240
  const matches = before.split(oldString).length - 1;
176
- if (matches === 0)
177
- throw new Error(`Cannot edit ${path}: oldString not found`);
178
- if (matches > 1)
179
- throw new Error(`Cannot edit ${path}: oldString is not unique`);
241
+ if (matches === 0) {
242
+ const reason = `Cannot edit ${path}: oldString not found`;
243
+ recordToolResult(ctx.session, toolCallId, 'error', reason);
244
+ throw new Error(reason);
245
+ }
246
+ if (matches > 1) {
247
+ const reason = `Cannot edit ${path}: oldString is not unique`;
248
+ recordToolResult(ctx.session, toolCallId, 'error', reason);
249
+ throw new Error(reason);
250
+ }
180
251
  const after = before.replace(oldString, newString);
181
252
  const tmp = `${resolved}.pugi-tmp-${Date.now()}`;
182
253
  writeFileSync(tmp, after, { encoding: 'utf8', mode: 0o600 });