@pugi/cli 0.1.0-beta.4 → 0.1.0-beta.40
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/THIRD_PARTY_NOTICES.md +40 -0
- package/assets/pugi-mascot.ansi +15 -25
- package/bin/run.js +33 -1
- package/dist/commands/jobs-watch.js +201 -0
- package/dist/commands/jobs.js +15 -0
- package/dist/commands/smoke.js +133 -0
- package/dist/core/agent-progress/cleanup.js +134 -0
- package/dist/core/agent-progress/schema.js +144 -0
- package/dist/core/agent-progress/writer.js +101 -0
- package/dist/core/artifact-chain/dispatcher.js +148 -0
- package/dist/core/artifact-chain/exporter.js +164 -0
- package/dist/core/artifact-chain/state.js +243 -0
- package/dist/core/artifact-chain/steps.js +169 -0
- package/dist/core/auth/ensure-authenticated.js +129 -0
- package/dist/core/auth/env-provider.js +238 -0
- package/dist/core/auto-update/channels.js +122 -0
- package/dist/core/auto-update/checker.js +241 -0
- package/dist/core/auto-update/state.js +235 -0
- package/dist/core/bare-mode/index.js +107 -0
- package/dist/core/bash-classifier.js +108 -1
- package/dist/core/checkpoint/resumer.js +149 -0
- package/dist/core/checkpoint/rewinder.js +291 -0
- package/dist/core/codegraph/decision-store.js +248 -0
- package/dist/core/codegraph/detect-repo.js +459 -0
- package/dist/core/codegraph/install.js +134 -0
- package/dist/core/codegraph/offer-hook.js +220 -0
- package/dist/core/compact/auto-trigger.js +96 -0
- package/dist/core/compact/buffer-rewriter.js +115 -0
- package/dist/core/compact/summarizer.js +208 -0
- package/dist/core/compact/token-counter.js +108 -0
- package/dist/core/consensus/diff-capture.js +73 -0
- package/dist/core/context/index.js +7 -0
- package/dist/core/context/markdown-traverse.js +255 -0
- package/dist/core/cost/rate-card.js +129 -0
- package/dist/core/cost/tracker.js +221 -0
- package/dist/core/denial-tracking/index.js +8 -0
- package/dist/core/denial-tracking/state.js +264 -0
- package/dist/core/diagnostics/probe-runner.js +93 -0
- package/dist/core/diagnostics/probes/api.js +46 -0
- package/dist/core/diagnostics/probes/auth.js +86 -0
- package/dist/core/diagnostics/probes/bare-mode.js +42 -0
- package/dist/core/diagnostics/probes/cli-version.js +127 -0
- package/dist/core/diagnostics/probes/config.js +72 -0
- package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
- package/dist/core/diagnostics/probes/disk.js +81 -0
- package/dist/core/diagnostics/probes/git.js +65 -0
- package/dist/core/diagnostics/probes/mcp.js +75 -0
- package/dist/core/diagnostics/probes/node.js +59 -0
- package/dist/core/diagnostics/probes/pnpm.js +36 -0
- package/dist/core/diagnostics/probes/pugi-md.js +89 -0
- package/dist/core/diagnostics/probes/session.js +74 -0
- package/dist/core/diagnostics/probes/status-snapshot.js +488 -0
- package/dist/core/diagnostics/probes/workspace.js +63 -0
- package/dist/core/diagnostics/types.js +70 -0
- package/dist/core/dispatch/cache-cleanup.js +197 -0
- package/dist/core/dispatch/cache-handoff.js +295 -0
- package/dist/core/edits/dispatch.js +218 -2
- package/dist/core/edits/journal.js +199 -0
- package/dist/core/edits/layer-d-ast.js +557 -14
- package/dist/core/edits/verify-hook.js +273 -0
- package/dist/core/edits/worktree.js +322 -0
- package/dist/core/engine/anvil-client.js +115 -5
- package/dist/core/engine/budgets.js +98 -0
- package/dist/core/engine/context-prefix.js +155 -0
- package/dist/core/engine/intent.js +260 -0
- package/dist/core/engine/native-pugi.js +860 -211
- package/dist/core/engine/prompts.js +88 -2
- package/dist/core/engine/strip-internal-fields.js +124 -0
- package/dist/core/engine/tool-bridge.js +992 -36
- package/dist/core/feedback/queue.js +177 -0
- package/dist/core/feedback/submitter.js +145 -0
- package/dist/core/file-cache.js +113 -1
- package/dist/core/hooks/events.js +44 -0
- package/dist/core/hooks/index.js +15 -0
- package/dist/core/hooks/registry.js +213 -0
- package/dist/core/hooks/runner.js +236 -0
- package/dist/core/hooks/v2/event-emitter.js +115 -0
- package/dist/core/hooks/v2/executor.js +282 -0
- package/dist/core/hooks/v2/index.js +25 -0
- package/dist/core/hooks/v2/lifecycle.js +104 -0
- package/dist/core/hooks/v2/loader.js +216 -0
- package/dist/core/hooks/v2/matcher.js +125 -0
- package/dist/core/hooks/v2/trust.js +143 -0
- package/dist/core/hooks/v2/types.js +86 -0
- package/dist/core/lsp/cache.js +105 -0
- package/dist/core/lsp/client.js +776 -0
- package/dist/core/lsp/language-detect.js +66 -0
- package/dist/core/lsp/post-edit-diagnostics.js +171 -0
- package/dist/core/mcp/client.js +75 -6
- package/dist/core/mcp/http-server.js +553 -0
- package/dist/core/mcp/orchestrator-tools.js +662 -0
- package/dist/core/mcp/permission.js +190 -0
- package/dist/core/mcp/registry.js +24 -2
- package/dist/core/mcp/server-tools.js +219 -0
- package/dist/core/mcp/server.js +397 -0
- package/dist/core/memory/dual-write.js +416 -0
- package/dist/core/memory/phase1-kinds.js +20 -0
- package/dist/core/memory-sync/queue.js +158 -0
- package/dist/core/onboarding/ensure-initialized.js +133 -0
- package/dist/core/onboarding/marker.js +111 -0
- package/dist/core/onboarding/telemetry-state.js +108 -0
- package/dist/core/output-style/presets.js +176 -0
- package/dist/core/output-style/state.js +185 -0
- package/dist/core/permissions/auto-classifier.js +124 -0
- package/dist/core/permissions/circuit-breaker.js +83 -0
- package/dist/core/permissions/gate.js +278 -0
- package/dist/core/permissions/index.js +20 -0
- package/dist/core/permissions/mode.js +174 -0
- package/dist/core/permissions/state.js +241 -0
- package/dist/core/permissions/tool-class.js +93 -0
- package/dist/core/prd-check/parser.js +215 -0
- package/dist/core/prd-check/reporter.js +127 -0
- package/dist/core/prd-check/session-review.js +557 -0
- package/dist/core/prd-check/verifiers.js +223 -0
- package/dist/core/pugi-md/context-injector.js +76 -0
- package/dist/core/pugi-md/walk-up.js +207 -0
- package/dist/core/release-notes/parser.js +241 -0
- package/dist/core/release-notes/state.js +116 -0
- package/dist/core/repl/history.js +11 -1
- package/dist/core/repl/model-pricing.js +135 -0
- package/dist/core/repl/session.js +1899 -38
- package/dist/core/repl/slash-commands.js +406 -21
- package/dist/core/repl/store/session-store.js +31 -2
- package/dist/core/repl/workspace-context.js +22 -0
- package/dist/core/repo-map/build.js +125 -0
- package/dist/core/repo-map/cache.js +185 -0
- package/dist/core/repo-map/extractor.js +254 -0
- package/dist/core/repo-map/formatter.js +145 -0
- package/dist/core/repo-map/scanner.js +211 -0
- package/dist/core/retry-budget/budget.js +284 -0
- package/dist/core/retry-budget/index.js +5 -0
- package/dist/core/session.js +92 -0
- package/dist/core/settings.js +80 -0
- package/dist/core/share/formatter.js +271 -0
- package/dist/core/share/redactor.js +221 -0
- package/dist/core/share/uploader.js +267 -0
- package/dist/core/skills/defaults.js +457 -0
- package/dist/core/smoke/headless-driver.js +174 -0
- package/dist/core/smoke/orchestrator.js +194 -0
- package/dist/core/smoke/runner.js +238 -0
- package/dist/core/smoke/scenario-parser.js +316 -0
- package/dist/core/subagents/dispatcher-real.js +600 -0
- package/dist/core/subagents/dispatcher.js +113 -24
- package/dist/core/subagents/index.js +18 -5
- package/dist/core/subagents/isolation-matrix.js +213 -0
- package/dist/core/subagents/spawn.js +19 -4
- package/dist/core/telemetry/emitter.js +229 -0
- package/dist/core/telemetry/queue.js +251 -0
- package/dist/core/theme/context.js +91 -0
- package/dist/core/theme/presets.js +228 -0
- package/dist/core/theme/state.js +181 -0
- package/dist/core/todos/invariant.js +10 -0
- package/dist/core/todos/state.js +177 -0
- package/dist/core/transport/version-interceptor.js +166 -0
- package/dist/core/vim/keymap.js +288 -0
- package/dist/core/vim/state.js +92 -0
- package/dist/index.js +28 -0
- package/dist/runtime/bootstrap.js +190 -0
- package/dist/runtime/cli.js +3073 -321
- package/dist/runtime/commands/cancel.js +231 -0
- package/dist/runtime/commands/chain.js +489 -0
- package/dist/runtime/commands/codegraph-status.js +227 -0
- package/dist/runtime/commands/compact.js +297 -0
- package/dist/runtime/commands/cost.js +199 -0
- package/dist/runtime/commands/delegate.js +242 -11
- package/dist/runtime/commands/dispatch.js +126 -0
- package/dist/runtime/commands/doctor.js +390 -0
- package/dist/runtime/commands/feedback.js +184 -0
- package/dist/runtime/commands/hooks.js +184 -0
- package/dist/runtime/commands/lsp.js +368 -0
- package/dist/runtime/commands/mcp.js +879 -0
- package/dist/runtime/commands/memory.js +508 -0
- package/dist/runtime/commands/model.js +237 -0
- package/dist/runtime/commands/onboarding.js +275 -0
- package/dist/runtime/commands/patch.js +128 -0
- package/dist/runtime/commands/permissions.js +112 -0
- package/dist/runtime/commands/plan.js +143 -0
- package/dist/runtime/commands/prd-check.js +285 -0
- package/dist/runtime/commands/redo-blob-store.js +92 -0
- package/dist/runtime/commands/redo.js +361 -0
- package/dist/runtime/commands/release-notes.js +229 -0
- package/dist/runtime/commands/repo-map.js +95 -0
- package/dist/runtime/commands/report.js +299 -0
- package/dist/runtime/commands/resume.js +118 -0
- package/dist/runtime/commands/review-consensus.js +17 -2
- package/dist/runtime/commands/rewind.js +333 -0
- package/dist/runtime/commands/sessions.js +163 -0
- package/dist/runtime/commands/share.js +316 -0
- package/dist/runtime/commands/status.js +186 -0
- package/dist/runtime/commands/stickers.js +82 -0
- package/dist/runtime/commands/style.js +194 -0
- package/dist/runtime/commands/theme.js +196 -0
- package/dist/runtime/commands/undo.js +32 -0
- package/dist/runtime/commands/update.js +289 -0
- package/dist/runtime/commands/vim.js +140 -0
- package/dist/runtime/commands/worktree.js +177 -0
- package/dist/runtime/headless-repl.js +195 -0
- package/dist/runtime/headless.js +543 -0
- package/dist/runtime/load-hooks-or-exit.js +71 -0
- package/dist/runtime/plan-decompose.js +531 -0
- package/dist/runtime/version.js +65 -0
- package/dist/tools/agent-tool.js +229 -0
- package/dist/tools/apply-patch.js +556 -0
- package/dist/tools/ask-user-question.js +213 -0
- package/dist/tools/ask-user.js +115 -0
- package/dist/tools/file-tools.js +85 -14
- package/dist/tools/lsp-tools.js +189 -0
- package/dist/tools/mcp-tool.js +260 -0
- package/dist/tools/multi-edit.js +361 -0
- package/dist/tools/registry.js +46 -0
- package/dist/tools/skill-tool.js +96 -0
- package/dist/tools/tasks.js +208 -0
- package/dist/tools/todo-write.js +184 -0
- package/dist/tools/web-fetch.js +147 -2
- package/dist/tools/web-search.js +458 -0
- package/dist/tui/agent-progress-card.js +111 -0
- package/dist/tui/agent-tree.js +10 -0
- package/dist/tui/ask-modal.js +2 -2
- package/dist/tui/ask-user-question-prompt.js +192 -0
- package/dist/tui/compact-banner.js +81 -0
- package/dist/tui/conversation-pane.js +82 -8
- package/dist/tui/cost-table.js +111 -0
- package/dist/tui/doctor-table.js +46 -0
- package/dist/tui/feedback-prompt.js +156 -0
- package/dist/tui/input-box.js +69 -2
- package/dist/tui/markdown-render.js +4 -4
- package/dist/tui/onboarding-wizard.js +240 -0
- package/dist/tui/permissions-picker.js +86 -0
- package/dist/tui/render.js +35 -0
- package/dist/tui/repl-render.js +303 -13
- package/dist/tui/repl-splash.js +2 -2
- package/dist/tui/repl.js +72 -14
- package/dist/tui/splash.js +1 -1
- package/dist/tui/status-bar.js +94 -16
- package/dist/tui/status-table.js +7 -0
- package/dist/tui/stickers-art.js +136 -0
- package/dist/tui/style-table.js +28 -0
- package/dist/tui/theme-table.js +29 -0
- package/dist/tui/tool-stream-pane.js +52 -3
- package/dist/tui/update-banner.js +20 -2
- package/dist/tui/vim-input.js +267 -0
- package/docs/examples/codegraph.mcp.json +10 -0
- package/package.json +12 -6
- package/test/scenarios/codegen-create-file.scenario.txt +13 -0
- package/test/scenarios/compact-force.scenario.txt +11 -0
- package/test/scenarios/identity.scenario.txt +11 -0
- package/test/scenarios/persona-handoff.scenario.txt +11 -0
- package/test/scenarios/walkback.scenario.txt +12 -0
- package/dist/core/engine/compaction-hook.js +0 -154
|
@@ -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
|
package/dist/tools/file-tools.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
172
|
-
|
|
173
|
-
|
|
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
|
-
|
|
178
|
-
|
|
179
|
-
throw new Error(
|
|
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 });
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { gateOnCancellation, OperatorAbortedError } from './file-tools.js';
|
|
2
|
+
import { recordToolCall, recordToolResult } from '../core/session.js';
|
|
3
|
+
/** Cap for any single LSP tool's payload size. Keeps model context lean. */
|
|
4
|
+
const LSP_PAYLOAD_CAP_BYTES = 8 * 1024;
|
|
5
|
+
export async function lspHover(ctx, lang, file, line, col) {
|
|
6
|
+
const toolCallId = recordToolCall(ctx.session, 'lsp_hover', `${lang}:${file}:${line}:${col}`);
|
|
7
|
+
return guard(ctx, 'lsp_hover', toolCallId, async () => {
|
|
8
|
+
const client = ctx.lspClients?.get(lang);
|
|
9
|
+
if (!client)
|
|
10
|
+
return unavailable(lang);
|
|
11
|
+
const result = await client.hover(file, { line, character: col }, ctx.cancellation);
|
|
12
|
+
if (!result.ok)
|
|
13
|
+
return failure(result);
|
|
14
|
+
if (!result.value) {
|
|
15
|
+
return { ok: true, value: { content: '' } };
|
|
16
|
+
}
|
|
17
|
+
const content = truncate(result.value.content);
|
|
18
|
+
return {
|
|
19
|
+
ok: true,
|
|
20
|
+
value: {
|
|
21
|
+
content: content.text,
|
|
22
|
+
...(result.value.range ? { range: result.value.range } : {}),
|
|
23
|
+
},
|
|
24
|
+
...(content.truncated ? { truncated: true } : {}),
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
export async function lspDefinition(ctx, lang, file, line, col) {
|
|
29
|
+
const toolCallId = recordToolCall(ctx.session, 'lsp_definition', `${lang}:${file}:${line}:${col}`);
|
|
30
|
+
return guard(ctx, 'lsp_definition', toolCallId, async () => {
|
|
31
|
+
const client = ctx.lspClients?.get(lang);
|
|
32
|
+
if (!client)
|
|
33
|
+
return unavailable(lang);
|
|
34
|
+
const result = await client.definition(file, { line, character: col }, ctx.cancellation);
|
|
35
|
+
if (!result.ok)
|
|
36
|
+
return failure(result);
|
|
37
|
+
const capped = capLocations(result.value);
|
|
38
|
+
return {
|
|
39
|
+
ok: true,
|
|
40
|
+
value: capped.value,
|
|
41
|
+
...(capped.truncated ? { truncated: true } : {}),
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export async function lspReferences(ctx, lang, file, line, col) {
|
|
46
|
+
const toolCallId = recordToolCall(ctx.session, 'lsp_references', `${lang}:${file}:${line}:${col}`);
|
|
47
|
+
return guard(ctx, 'lsp_references', toolCallId, async () => {
|
|
48
|
+
const client = ctx.lspClients?.get(lang);
|
|
49
|
+
if (!client)
|
|
50
|
+
return unavailable(lang);
|
|
51
|
+
const result = await client.references(file, { line, character: col }, ctx.cancellation);
|
|
52
|
+
if (!result.ok)
|
|
53
|
+
return failure(result);
|
|
54
|
+
const capped = capLocations(result.value);
|
|
55
|
+
return {
|
|
56
|
+
ok: true,
|
|
57
|
+
value: capped.value,
|
|
58
|
+
...(capped.truncated ? { truncated: true } : {}),
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export async function lspDiagnostics(ctx, lang, file) {
|
|
63
|
+
const toolCallId = recordToolCall(ctx.session, 'lsp_diagnostics', `${lang}:${file}`);
|
|
64
|
+
return guard(ctx, 'lsp_diagnostics', toolCallId, async () => {
|
|
65
|
+
const client = ctx.lspClients?.get(lang);
|
|
66
|
+
if (!client)
|
|
67
|
+
return unavailable(lang);
|
|
68
|
+
const result = await client.diagnostics(file, ctx.cancellation);
|
|
69
|
+
if (!result.ok)
|
|
70
|
+
return failure(result);
|
|
71
|
+
const capped = capDiagnostics(result.value);
|
|
72
|
+
return {
|
|
73
|
+
ok: true,
|
|
74
|
+
value: capped.value,
|
|
75
|
+
...(capped.truncated ? { truncated: true } : {}),
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
async function guard(ctx, toolName, toolCallId, op) {
|
|
80
|
+
try {
|
|
81
|
+
gateOnCancellation(ctx, toolName);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
if (error instanceof OperatorAbortedError) {
|
|
85
|
+
recordToolResult(ctx.session, toolCallId, 'cancelled', error.message);
|
|
86
|
+
return { ok: false, reason: 'operator_aborted', detail: error.message };
|
|
87
|
+
}
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const result = await op();
|
|
92
|
+
if (result.ok) {
|
|
93
|
+
recordToolResult(ctx.session, toolCallId, 'success', summarize(result.value));
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
recordToolResult(ctx.session, toolCallId, 'error', `${result.reason ?? 'error'}: ${result.detail ?? ''}`);
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
102
|
+
recordToolResult(ctx.session, toolCallId, 'error', message);
|
|
103
|
+
return { ok: false, reason: 'lsp_error', detail: message };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function unavailable(lang) {
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
reason: 'lsp_unavailable',
|
|
110
|
+
detail: `no LSP server started for ${lang}. Install the server and re-run ` +
|
|
111
|
+
`with --lsp ${lang}, or fall back to grep.`,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function failure(result) {
|
|
115
|
+
if (result.ok) {
|
|
116
|
+
// Shouldn't be hit — caller checks first.
|
|
117
|
+
return { ok: true, value: result.value };
|
|
118
|
+
}
|
|
119
|
+
return { ok: false, reason: result.reason, detail: result.detail };
|
|
120
|
+
}
|
|
121
|
+
function summarize(value) {
|
|
122
|
+
if (value === null || value === undefined)
|
|
123
|
+
return 'no result';
|
|
124
|
+
if (Array.isArray(value))
|
|
125
|
+
return `${value.length} items`;
|
|
126
|
+
if (typeof value === 'object')
|
|
127
|
+
return Object.keys(value).join(',');
|
|
128
|
+
return String(value);
|
|
129
|
+
}
|
|
130
|
+
function truncate(text) {
|
|
131
|
+
const bytes = Buffer.byteLength(text, 'utf8');
|
|
132
|
+
if (bytes <= LSP_PAYLOAD_CAP_BYTES)
|
|
133
|
+
return { text, truncated: false };
|
|
134
|
+
// Truncate to the cap byte boundary. We don't try to honor codepoint
|
|
135
|
+
// alignment — UTF-8 surrogate splits show up as a single ? at the
|
|
136
|
+
// boundary, which is acceptable for a debug surface; the dispatcher
|
|
137
|
+
// is the trust boundary for "this is what the model will see".
|
|
138
|
+
const buf = Buffer.from(text, 'utf8').subarray(0, LSP_PAYLOAD_CAP_BYTES);
|
|
139
|
+
return { text: `${buf.toString('utf8')}\n... [truncated]`, truncated: true };
|
|
140
|
+
}
|
|
141
|
+
function capLocations(locations) {
|
|
142
|
+
// Cap at 200 locations OR the byte cap, whichever hits first. The
|
|
143
|
+
// 200 number is the operator-facing "this is a hot symbol" threshold —
|
|
144
|
+
// a richer surface (paginated `pugi lsp references --offset N`) is
|
|
145
|
+
// open backlog.
|
|
146
|
+
const COUNT_CAP = 200;
|
|
147
|
+
if (locations.length === 0)
|
|
148
|
+
return { value: locations, truncated: false };
|
|
149
|
+
const trimmed = locations.slice(0, COUNT_CAP);
|
|
150
|
+
const serialized = JSON.stringify(trimmed);
|
|
151
|
+
if (Buffer.byteLength(serialized, 'utf8') <= LSP_PAYLOAD_CAP_BYTES && trimmed.length === locations.length) {
|
|
152
|
+
return { value: trimmed, truncated: false };
|
|
153
|
+
}
|
|
154
|
+
// Trim by halves until we fit the byte cap. Worst case ~10 iterations
|
|
155
|
+
// for the 200 max, fine for an interactive tool.
|
|
156
|
+
let upper = trimmed.length;
|
|
157
|
+
while (upper > 1) {
|
|
158
|
+
const half = Math.floor(upper / 2);
|
|
159
|
+
const sub = trimmed.slice(0, half);
|
|
160
|
+
if (Buffer.byteLength(JSON.stringify(sub), 'utf8') <= LSP_PAYLOAD_CAP_BYTES) {
|
|
161
|
+
return { value: sub, truncated: true };
|
|
162
|
+
}
|
|
163
|
+
upper = half;
|
|
164
|
+
}
|
|
165
|
+
return { value: trimmed.slice(0, 1), truncated: true };
|
|
166
|
+
}
|
|
167
|
+
function capDiagnostics(items) {
|
|
168
|
+
if (items.length === 0)
|
|
169
|
+
return { value: items, truncated: false };
|
|
170
|
+
const serialized = JSON.stringify(items);
|
|
171
|
+
if (Buffer.byteLength(serialized, 'utf8') <= LSP_PAYLOAD_CAP_BYTES) {
|
|
172
|
+
return { value: items, truncated: false };
|
|
173
|
+
}
|
|
174
|
+
// Diagnostics are sorted error-first in LSP convention; trim from the
|
|
175
|
+
// tail so we keep the highest-severity items.
|
|
176
|
+
let upper = items.length;
|
|
177
|
+
while (upper > 1) {
|
|
178
|
+
const half = Math.floor(upper / 2);
|
|
179
|
+
const sub = items.slice(0, half);
|
|
180
|
+
if (Buffer.byteLength(JSON.stringify(sub), 'utf8') <= LSP_PAYLOAD_CAP_BYTES) {
|
|
181
|
+
return { value: sub, truncated: true };
|
|
182
|
+
}
|
|
183
|
+
upper = half;
|
|
184
|
+
}
|
|
185
|
+
return { value: items.slice(0, 1), truncated: true };
|
|
186
|
+
}
|
|
187
|
+
/** Test-only surface so specs can poke truncation directly. */
|
|
188
|
+
export const __test__ = { truncate, capLocations, capDiagnostics, LSP_PAYLOAD_CAP_BYTES };
|
|
189
|
+
//# sourceMappingURL=lsp-tools.js.map
|