@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.
- package/THIRD_PARTY_NOTICES.md +40 -0
- package/assets/pugi-mascot.ansi +15 -40
- package/bin/run.js +33 -1
- package/dist/commands/jobs-watch.js +201 -0
- package/dist/commands/jobs.js +15 -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/compact/auto-trigger.js +96 -0
- package/dist/core/compact/buffer-rewriter.js +115 -0
- package/dist/core/compact/summarizer.js +196 -0
- package/dist/core/compact/token-counter.js +108 -0
- package/dist/core/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/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/session.js +74 -0
- package/dist/core/diagnostics/probes/status-snapshot.js +442 -0
- package/dist/core/diagnostics/probes/workspace.js +63 -0
- package/dist/core/diagnostics/types.js +70 -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 +111 -18
- package/dist/core/engine/anvil-client.js +115 -5
- package/dist/core/engine/budgets.js +89 -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 +744 -210
- package/dist/core/engine/prompts.js +61 -6
- package/dist/core/engine/strip-internal-fields.js +124 -0
- package/dist/core/engine/tool-bridge.js +818 -31
- package/dist/core/file-cache.js +113 -1
- package/dist/core/init/scaffold.js +195 -0
- package/dist/core/lsp/client.js +174 -29
- package/dist/core/mcp/client.js +75 -6
- package/dist/core/mcp/http-server.js +553 -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/permissions/gate.js +187 -0
- package/dist/core/permissions/index.js +18 -0
- package/dist/core/permissions/mode.js +102 -0
- package/dist/core/permissions/state.js +160 -0
- package/dist/core/permissions/tool-class.js +93 -0
- package/dist/core/repl/codebase-survey.js +308 -0
- package/dist/core/repl/history.js +11 -1
- package/dist/core/repl/init-interview.js +457 -0
- package/dist/core/repl/model-pricing.js +135 -0
- package/dist/core/repl/onboarding-state.js +297 -0
- package/dist/core/repl/session.js +719 -29
- package/dist/core/repl/slash-commands.js +133 -9
- package/dist/core/retry-budget/budget.js +284 -0
- package/dist/core/retry-budget/index.js +5 -0
- package/dist/core/settings.js +71 -0
- package/dist/core/skills/defaults.js +457 -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/transport/version-interceptor.js +166 -0
- package/dist/index.js +28 -0
- package/dist/runtime/bootstrap.js +190 -0
- package/dist/runtime/cli.js +1588 -266
- package/dist/runtime/commands/compact.js +296 -0
- package/dist/runtime/commands/cost.js +199 -0
- package/dist/runtime/commands/delegate.js +289 -0
- package/dist/runtime/commands/doctor.js +369 -0
- package/dist/runtime/commands/lsp.js +187 -5
- package/dist/runtime/commands/mcp.js +824 -0
- package/dist/runtime/commands/patch.js +17 -0
- package/dist/runtime/commands/permissions.js +87 -0
- package/dist/runtime/commands/report.js +299 -0
- package/dist/runtime/commands/review-consensus.js +17 -2
- package/dist/runtime/commands/roster.js +117 -0
- package/dist/runtime/commands/status.js +178 -0
- package/dist/runtime/commands/worktree.js +50 -6
- 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 +206 -0
- package/dist/tools/apply-patch.js +281 -39
- 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/mcp-tool.js +260 -0
- package/dist/tools/multi-edit.js +361 -0
- package/dist/tools/registry.js +22 -2
- package/dist/tools/skill-tool.js +96 -0
- package/dist/tools/tasks.js +208 -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 +54 -0
- package/dist/tui/conversation-pane.js +69 -8
- package/dist/tui/cost-table.js +111 -0
- package/dist/tui/doctor-table.js +31 -0
- package/dist/tui/input-box.js +1 -1
- package/dist/tui/markdown-render.js +4 -4
- package/dist/tui/repl-render.js +276 -37
- package/dist/tui/repl-splash.js +2 -2
- package/dist/tui/repl.js +25 -6
- 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/tool-stream-pane.js +7 -0
- package/dist/tui/update-banner.js +20 -2
- package/docs/examples/codegraph.mcp.json +10 -0
- package/package.json +9 -6
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run the delegate command. Parses args, validates the brief, opens a
|
|
3
|
+
* fresh session, and POSTs the delegate brief.
|
|
4
|
+
*
|
|
5
|
+
* Sets `process.exitCode` instead of throwing so the caller (cli.ts
|
|
6
|
+
* handlers table) does not need a try/catch wrapper.
|
|
7
|
+
*/
|
|
8
|
+
export async function runDelegateCommand(args, ctx) {
|
|
9
|
+
// Extract the optional `--wait` flag from positional args. Accept it
|
|
10
|
+
// in any position so `pugi delegate --wait dev "..."` and
|
|
11
|
+
// `pugi delegate dev "..." --wait` both work; positional ordering of
|
|
12
|
+
// slug + brief is preserved by filtering the flag out before the
|
|
13
|
+
// slug/brief split (Claude P2 fix 2026-05-25).
|
|
14
|
+
const wait = args.some((a) => a === '--wait');
|
|
15
|
+
const positional = args.filter((a) => a !== '--wait');
|
|
16
|
+
const slug = positional[0];
|
|
17
|
+
const brief = positional.slice(1).join(' ').trim();
|
|
18
|
+
if (!slug || !brief) {
|
|
19
|
+
ctx.writeOutput({
|
|
20
|
+
ok: false,
|
|
21
|
+
error: 'Usage: pugi delegate [--wait] <persona-slug> "<one-sentence brief>"',
|
|
22
|
+
}, 'Usage: pugi delegate [--wait] <persona-slug> "<one-sentence brief>"');
|
|
23
|
+
process.exitCode = 2;
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
// Brief size cap mirrors the server-side DTO (8000 chars). The local
|
|
27
|
+
// gate lets the operator know about the truncation before we waste a
|
|
28
|
+
// round-trip on a too-large brief.
|
|
29
|
+
if (brief.length > 8000) {
|
|
30
|
+
ctx.writeOutput({
|
|
31
|
+
ok: false,
|
|
32
|
+
error: `brief is ${brief.length} chars; max 8000`,
|
|
33
|
+
}, `pugi delegate: brief is ${brief.length} chars; max 8000.`);
|
|
34
|
+
process.exitCode = 2;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const config = ctx.resolveConfig();
|
|
38
|
+
if (!config) {
|
|
39
|
+
ctx.writeOutput({
|
|
40
|
+
ok: false,
|
|
41
|
+
error: 'no Pugi credential configured; run `pugi login` first',
|
|
42
|
+
}, 'pugi delegate: no credential configured. Run `pugi login` first.');
|
|
43
|
+
process.exitCode = 1;
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const opened = await ctx.openSession(config, ctx.workspaceCwd);
|
|
47
|
+
if ('error' in opened) {
|
|
48
|
+
ctx.writeOutput({ ok: false, error: opened.error }, `pugi delegate: session open failed: ${opened.error}`);
|
|
49
|
+
process.exitCode = 1;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const result = await ctx.submitDelegate(config, opened.sessionId, {
|
|
53
|
+
persona: slug,
|
|
54
|
+
brief,
|
|
55
|
+
});
|
|
56
|
+
switch (result.status) {
|
|
57
|
+
case 'ok': {
|
|
58
|
+
const ok = result.response;
|
|
59
|
+
if (!wait) {
|
|
60
|
+
ctx.writeOutput({
|
|
61
|
+
ok: true,
|
|
62
|
+
sessionId: opened.sessionId,
|
|
63
|
+
dispatchId: ok.dispatchId,
|
|
64
|
+
personaSlug: ok.personaSlug,
|
|
65
|
+
}, `dispatched ${ok.personaSlug} (dispatchId=${ok.dispatchId}); stream via GET /sessions/${opened.sessionId}/stream.`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
// --wait: subscribe to SSE until the persona reaches a terminal
|
|
69
|
+
// event. The waiter contract is provider-shaped so tests can pass
|
|
70
|
+
// a fake without standing up fetch.
|
|
71
|
+
const waiter = ctx.waitForTerminal ?? waitForDelegateTerminal;
|
|
72
|
+
const outcome = await waiter(config, opened.sessionId, ok.personaSlug);
|
|
73
|
+
switch (outcome.kind) {
|
|
74
|
+
case 'completed':
|
|
75
|
+
ctx.writeOutput({
|
|
76
|
+
ok: true,
|
|
77
|
+
sessionId: opened.sessionId,
|
|
78
|
+
dispatchId: ok.dispatchId,
|
|
79
|
+
personaSlug: outcome.personaSlug,
|
|
80
|
+
status: 'completed',
|
|
81
|
+
}, `completed ${outcome.personaSlug} (dispatchId=${ok.dispatchId}).`);
|
|
82
|
+
return;
|
|
83
|
+
case 'blocked':
|
|
84
|
+
ctx.writeOutput({
|
|
85
|
+
ok: false,
|
|
86
|
+
sessionId: opened.sessionId,
|
|
87
|
+
dispatchId: ok.dispatchId,
|
|
88
|
+
personaSlug: outcome.personaSlug,
|
|
89
|
+
status: 'blocked',
|
|
90
|
+
detail: outcome.detail,
|
|
91
|
+
}, `blocked ${outcome.personaSlug}: ${outcome.detail}`);
|
|
92
|
+
process.exitCode = 5;
|
|
93
|
+
return;
|
|
94
|
+
case 'failed':
|
|
95
|
+
ctx.writeOutput({
|
|
96
|
+
ok: false,
|
|
97
|
+
sessionId: opened.sessionId,
|
|
98
|
+
dispatchId: ok.dispatchId,
|
|
99
|
+
personaSlug: outcome.personaSlug,
|
|
100
|
+
status: 'failed',
|
|
101
|
+
error: outcome.error,
|
|
102
|
+
}, `failed ${outcome.personaSlug}: ${outcome.error}`);
|
|
103
|
+
process.exitCode = 5;
|
|
104
|
+
return;
|
|
105
|
+
case 'stream_error':
|
|
106
|
+
ctx.writeOutput({
|
|
107
|
+
ok: false,
|
|
108
|
+
sessionId: opened.sessionId,
|
|
109
|
+
dispatchId: ok.dispatchId,
|
|
110
|
+
error: outcome.error,
|
|
111
|
+
}, `pugi delegate --wait: ${outcome.error}`);
|
|
112
|
+
process.exitCode = 1;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
case 'unknown_persona':
|
|
118
|
+
ctx.writeOutput({ ok: false, error: result.message, code: result.code }, `pugi delegate: ${result.message}`);
|
|
119
|
+
process.exitCode = 3;
|
|
120
|
+
return;
|
|
121
|
+
case 'quota_exceeded':
|
|
122
|
+
ctx.writeOutput({ ok: false, error: result.message, code: result.code }, `pugi delegate: ${result.message}`);
|
|
123
|
+
process.exitCode = 4;
|
|
124
|
+
return;
|
|
125
|
+
case 'endpoint_missing':
|
|
126
|
+
ctx.writeOutput({
|
|
127
|
+
ok: false,
|
|
128
|
+
error: 'runtime does not expose POST /api/pugi/sessions/:id/delegate (upgrade admin-api to α7.5+)',
|
|
129
|
+
code: result.code,
|
|
130
|
+
}, 'pugi delegate: runtime does not expose the delegate endpoint. Upgrade admin-api to α7.5+.');
|
|
131
|
+
process.exitCode = 1;
|
|
132
|
+
return;
|
|
133
|
+
case 'unauthenticated':
|
|
134
|
+
case 'failed':
|
|
135
|
+
ctx.writeOutput({ ok: false, error: result.message, code: result.code }, `pugi delegate: ${result.message}`);
|
|
136
|
+
process.exitCode = 1;
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/* ------------------------------------------------------------------ */
|
|
141
|
+
/* --wait waiter */
|
|
142
|
+
/* ------------------------------------------------------------------ */
|
|
143
|
+
/**
|
|
144
|
+
* Subscribe to the session SSE stream and resolve when the named
|
|
145
|
+
* persona reaches a terminal event (agent.completed / agent.blocked /
|
|
146
|
+
* agent.failed). Falls back to a `stream_error` outcome when the
|
|
147
|
+
* stream closes before any terminal event arrives or when the
|
|
148
|
+
* underlying fetch fails.
|
|
149
|
+
*
|
|
150
|
+
* The waiter is intentionally a single-call helper (not a long-lived
|
|
151
|
+
* subscriber) - the scripted `pugi delegate --wait` caller wants to
|
|
152
|
+
* exit on the first terminal event for THIS persona, not maintain a
|
|
153
|
+
* persistent connection.
|
|
154
|
+
*
|
|
155
|
+
* Why we parse the lifecycle in this file (not via the repl-render
|
|
156
|
+
* subscribe helper): repl-render carries Ink/React deps the
|
|
157
|
+
* non-interactive delegate path does not want to load. Duplicating the
|
|
158
|
+
* minimal SSE-line parser here keeps the command lean (this is the same
|
|
159
|
+
* shape `apps/admin-api/src/pugi/sessions.controller.ts` writes to the
|
|
160
|
+
* wire).
|
|
161
|
+
*/
|
|
162
|
+
export async function waitForDelegateTerminal(config, sessionId, targetPersonaSlug) {
|
|
163
|
+
const url = `${config.apiUrl.replace(/\/+$/, '')}/api/pugi/sessions/${encodeURIComponent(sessionId)}/stream`;
|
|
164
|
+
const controller = new AbortController();
|
|
165
|
+
// Hard-cap the waiter at 5 minutes so a stalled server cannot keep
|
|
166
|
+
// a scripted caller hanging forever; the dispatcher's per-turn budget
|
|
167
|
+
// is well under this.
|
|
168
|
+
const timeout = setTimeout(() => controller.abort(), 5 * 60 * 1000);
|
|
169
|
+
try {
|
|
170
|
+
const response = await fetch(url, {
|
|
171
|
+
method: 'GET',
|
|
172
|
+
headers: {
|
|
173
|
+
Accept: 'text/event-stream',
|
|
174
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
175
|
+
},
|
|
176
|
+
signal: controller.signal,
|
|
177
|
+
});
|
|
178
|
+
if (!response.ok) {
|
|
179
|
+
return { kind: 'stream_error', error: `HTTP ${response.status}` };
|
|
180
|
+
}
|
|
181
|
+
if (!response.body) {
|
|
182
|
+
return { kind: 'stream_error', error: 'no response body' };
|
|
183
|
+
}
|
|
184
|
+
// Track the most-recent persona slug seen on agent.spawned so the
|
|
185
|
+
// terminal event (which carries only taskId) can be matched to the
|
|
186
|
+
// delegate target without parsing the random nonce suffix.
|
|
187
|
+
const taskToPersona = new Map();
|
|
188
|
+
const reader = response.body.getReader();
|
|
189
|
+
const decoder = new TextDecoder('utf-8');
|
|
190
|
+
let buffer = '';
|
|
191
|
+
let currentData = '';
|
|
192
|
+
while (true) {
|
|
193
|
+
const { value, done } = await reader.read();
|
|
194
|
+
if (done) {
|
|
195
|
+
return { kind: 'stream_error', error: 'stream ended without terminal event' };
|
|
196
|
+
}
|
|
197
|
+
buffer += decoder.decode(value, { stream: true });
|
|
198
|
+
let newlineIndex;
|
|
199
|
+
while ((newlineIndex = buffer.indexOf('\n')) !== -1) {
|
|
200
|
+
const rawLine = buffer.slice(0, newlineIndex).replace(/\r$/, '');
|
|
201
|
+
buffer = buffer.slice(newlineIndex + 1);
|
|
202
|
+
if (rawLine.length === 0) {
|
|
203
|
+
if (currentData.length > 0) {
|
|
204
|
+
const parsed = parseDelegateFrame(currentData);
|
|
205
|
+
if (parsed) {
|
|
206
|
+
if (parsed.type === 'agent.spawned') {
|
|
207
|
+
taskToPersona.set(parsed.taskId, parsed.personaSlug);
|
|
208
|
+
}
|
|
209
|
+
else if (parsed.type === 'agent.completed' ||
|
|
210
|
+
parsed.type === 'agent.blocked' ||
|
|
211
|
+
parsed.type === 'agent.failed') {
|
|
212
|
+
const slug = taskToPersona.get(parsed.taskId) ?? targetPersonaSlug;
|
|
213
|
+
if (slug === targetPersonaSlug) {
|
|
214
|
+
controller.abort();
|
|
215
|
+
if (parsed.type === 'agent.completed') {
|
|
216
|
+
return {
|
|
217
|
+
kind: 'completed',
|
|
218
|
+
personaSlug: slug,
|
|
219
|
+
taskId: parsed.taskId,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (parsed.type === 'agent.blocked') {
|
|
223
|
+
return {
|
|
224
|
+
kind: 'blocked',
|
|
225
|
+
personaSlug: slug,
|
|
226
|
+
taskId: parsed.taskId,
|
|
227
|
+
detail: parsed.detail,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
kind: 'failed',
|
|
232
|
+
personaSlug: slug,
|
|
233
|
+
taskId: parsed.taskId,
|
|
234
|
+
error: parsed.error,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
currentData = '';
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (rawLine.startsWith(':'))
|
|
244
|
+
continue;
|
|
245
|
+
const colonIndex = rawLine.indexOf(':');
|
|
246
|
+
const field = colonIndex === -1 ? rawLine : rawLine.slice(0, colonIndex);
|
|
247
|
+
const value = colonIndex === -1 ? '' : rawLine.slice(colonIndex + 1).replace(/^ /, '');
|
|
248
|
+
if (field === 'data') {
|
|
249
|
+
currentData = currentData.length === 0 ? value : `${currentData}\n${value}`;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
catch (err) {
|
|
255
|
+
if (controller.signal.aborted) {
|
|
256
|
+
return { kind: 'stream_error', error: 'wait timed out' };
|
|
257
|
+
}
|
|
258
|
+
return { kind: 'stream_error', error: err.message };
|
|
259
|
+
}
|
|
260
|
+
finally {
|
|
261
|
+
clearTimeout(timeout);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function parseDelegateFrame(data) {
|
|
265
|
+
try {
|
|
266
|
+
const raw = JSON.parse(data);
|
|
267
|
+
const type = raw.type;
|
|
268
|
+
const taskId = typeof raw.taskId === 'string' ? raw.taskId : null;
|
|
269
|
+
if (!taskId)
|
|
270
|
+
return null;
|
|
271
|
+
if (type === 'agent.spawned' && typeof raw.personaSlug === 'string') {
|
|
272
|
+
return { type, taskId, personaSlug: raw.personaSlug };
|
|
273
|
+
}
|
|
274
|
+
if (type === 'agent.completed') {
|
|
275
|
+
return { type, taskId };
|
|
276
|
+
}
|
|
277
|
+
if (type === 'agent.blocked') {
|
|
278
|
+
return { type, taskId, detail: typeof raw.detail === 'string' ? raw.detail : '' };
|
|
279
|
+
}
|
|
280
|
+
if (type === 'agent.failed') {
|
|
281
|
+
return { type, taskId, error: typeof raw.error === 'string' ? raw.error : '' };
|
|
282
|
+
}
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
//# sourceMappingURL=delegate.js.map
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pugi doctor` — environment health report (Leak L17, 2026-05-27).
|
|
3
|
+
*
|
|
4
|
+
* Parity command with Claude Code's `/doctor` (gap doc:
|
|
5
|
+
* docs/research/2026-05-27-pugi-gap-analysis-3-repos.md §6). Probes
|
|
6
|
+
* auth, API reachability, CLI version, workspace state, disk space,
|
|
7
|
+
* Node version, pnpm, git, MCP servers, config file, and session
|
|
8
|
+
* activity. Emits either a human-readable table OR a structured JSON
|
|
9
|
+
* envelope depending on `--json`.
|
|
10
|
+
*
|
|
11
|
+
* Module contract:
|
|
12
|
+
*
|
|
13
|
+
* - This file owns the WIRING from CLI flags + workspace context to
|
|
14
|
+
* the probe runner. The probes themselves live in
|
|
15
|
+
* `core/diagnostics/probes/*.ts` and have NO module-level coupling
|
|
16
|
+
* to the CLI dispatch surface.
|
|
17
|
+
*
|
|
18
|
+
* - `runDoctorCommand` is the single entry point. Both the top-level
|
|
19
|
+
* `pugi doctor` handler in `runtime/cli.ts` AND the in-REPL
|
|
20
|
+
* `/doctor` slash command call it. The function returns the
|
|
21
|
+
* `DoctorReport` so the REPL can render via the Ink table without
|
|
22
|
+
* re-running the probes.
|
|
23
|
+
*
|
|
24
|
+
* - Exit codes are derived from `exitCodeFor(overall)` in
|
|
25
|
+
* `core/diagnostics/types.ts` and bubble up via `process.exitCode`
|
|
26
|
+
* (matches the convention of every other CLI handler in cli.ts).
|
|
27
|
+
*
|
|
28
|
+
* - The MCP probe is opportunistic: if `core/mcp/registry.js` is
|
|
29
|
+
* unavailable for any reason (e.g. sibling L13 not yet landed,
|
|
30
|
+
* unexpected schema change), the probe degrades to a graceful
|
|
31
|
+
* `skipped` result so the rest of the table still renders.
|
|
32
|
+
*/
|
|
33
|
+
import { execFileSync } from 'node:child_process';
|
|
34
|
+
import { constants as fsConstants, existsSync, accessSync, readFileSync, statSync } from 'node:fs';
|
|
35
|
+
import { homedir } from 'node:os';
|
|
36
|
+
import { resolveActiveCredential } from '../../core/credentials.js';
|
|
37
|
+
import { PUGI_CLI_VERSION } from '../version.js';
|
|
38
|
+
import { runProbes, } from '../../core/diagnostics/probe-runner.js';
|
|
39
|
+
import { computeOverall, countProbes, exitCodeFor, } from '../../core/diagnostics/types.js';
|
|
40
|
+
import { probeAuth } from '../../core/diagnostics/probes/auth.js';
|
|
41
|
+
import { probeApi } from '../../core/diagnostics/probes/api.js';
|
|
42
|
+
import { probeCliVersion } from '../../core/diagnostics/probes/cli-version.js';
|
|
43
|
+
import { probeWorkspace } from '../../core/diagnostics/probes/workspace.js';
|
|
44
|
+
import { probeDisk } from '../../core/diagnostics/probes/disk.js';
|
|
45
|
+
import { probeNode } from '../../core/diagnostics/probes/node.js';
|
|
46
|
+
import { probePnpm } from '../../core/diagnostics/probes/pnpm.js';
|
|
47
|
+
import { probeGit } from '../../core/diagnostics/probes/git.js';
|
|
48
|
+
import { probeMcp } from '../../core/diagnostics/probes/mcp.js';
|
|
49
|
+
import { probeConfig } from '../../core/diagnostics/probes/config.js';
|
|
50
|
+
import { probeSession } from '../../core/diagnostics/probes/session.js';
|
|
51
|
+
import { probeDenialTracking } from '../../core/diagnostics/probes/denial-tracking.js';
|
|
52
|
+
/**
|
|
53
|
+
* Default API URL when no PUGI_API_URL env override is set. Mirrors
|
|
54
|
+
* the constant in `core/credentials.ts` (kept local to avoid an
|
|
55
|
+
* extra named export from that module).
|
|
56
|
+
*/
|
|
57
|
+
const DEFAULT_API_URL = 'https://api.pugi.io';
|
|
58
|
+
/**
|
|
59
|
+
* Build the standard probe set with production dependencies. Exported
|
|
60
|
+
* for the spec so the test can construct the same suite with stub
|
|
61
|
+
* deps + assert per-probe ordering + fail-isolation in isolation.
|
|
62
|
+
*/
|
|
63
|
+
export function buildDefaultProbes(ctx, options = {}) {
|
|
64
|
+
const fetchImpl = ctx.fetchImpl ?? globalThis.fetch.bind(globalThis);
|
|
65
|
+
const now = Date.now;
|
|
66
|
+
const probes = [
|
|
67
|
+
{
|
|
68
|
+
name: 'AUTH',
|
|
69
|
+
run: () => probeAuth(ctx, {
|
|
70
|
+
resolveCredential: (env, home) => {
|
|
71
|
+
const credential = resolveActiveCredential(env, home);
|
|
72
|
+
if (!credential)
|
|
73
|
+
return null;
|
|
74
|
+
return { apiUrl: credential.apiUrl, apiKey: credential.apiKey };
|
|
75
|
+
},
|
|
76
|
+
fetchImpl,
|
|
77
|
+
now,
|
|
78
|
+
}),
|
|
79
|
+
timeoutMs: 4_000,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: 'API',
|
|
83
|
+
run: () => probeApi(ctx, {
|
|
84
|
+
resolveApiUrl: (env) => {
|
|
85
|
+
return env.PUGI_API_URL ?? DEFAULT_API_URL;
|
|
86
|
+
},
|
|
87
|
+
fetchImpl,
|
|
88
|
+
now,
|
|
89
|
+
}),
|
|
90
|
+
timeoutMs: 4_000,
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: 'CLI VERSION',
|
|
94
|
+
run: () => probeCliVersion({
|
|
95
|
+
localVersion: options.localCliVersion ?? PUGI_CLI_VERSION,
|
|
96
|
+
fetchImpl,
|
|
97
|
+
now,
|
|
98
|
+
}),
|
|
99
|
+
timeoutMs: 4_000,
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: 'WORKSPACE',
|
|
103
|
+
run: async () => probeWorkspace(ctx, {
|
|
104
|
+
existsSync,
|
|
105
|
+
statSync,
|
|
106
|
+
accessSync,
|
|
107
|
+
W_OK: fsConstants.W_OK,
|
|
108
|
+
}),
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: 'DISK',
|
|
112
|
+
run: async () => probeDisk(ctx, {
|
|
113
|
+
getFreeBytes: (home) => getFreeBytesViaDf(home),
|
|
114
|
+
}),
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: 'NODE',
|
|
118
|
+
run: async () => probeNode({ version: process.version }),
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: 'PNPM',
|
|
122
|
+
run: async () => probePnpm({
|
|
123
|
+
resolveVersion: () => execFileSync('pnpm', ['--version'], {
|
|
124
|
+
encoding: 'utf8',
|
|
125
|
+
timeout: 2_000,
|
|
126
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
127
|
+
}).trim(),
|
|
128
|
+
}),
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: 'GIT',
|
|
132
|
+
run: async () => probeGit(ctx, {
|
|
133
|
+
resolveVersion: () => execFileSync('git', ['--version'], {
|
|
134
|
+
encoding: 'utf8',
|
|
135
|
+
timeout: 2_000,
|
|
136
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
137
|
+
}).trim(),
|
|
138
|
+
isInWorkTree: (cwd) => {
|
|
139
|
+
try {
|
|
140
|
+
const result = execFileSync('git', ['-C', cwd, 'rev-parse', '--is-inside-work-tree'], {
|
|
141
|
+
encoding: 'utf8',
|
|
142
|
+
timeout: 2_000,
|
|
143
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
144
|
+
}).trim();
|
|
145
|
+
return result === 'true';
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
resolveHeadSha: (cwd) => {
|
|
152
|
+
try {
|
|
153
|
+
return execFileSync('git', ['-C', cwd, 'rev-parse', 'HEAD'], {
|
|
154
|
+
encoding: 'utf8',
|
|
155
|
+
timeout: 2_000,
|
|
156
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
157
|
+
}).trim();
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
resolveRoot: (cwd) => {
|
|
164
|
+
try {
|
|
165
|
+
return execFileSync('git', ['-C', cwd, 'rev-parse', '--show-toplevel'], {
|
|
166
|
+
encoding: 'utf8',
|
|
167
|
+
timeout: 2_000,
|
|
168
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
169
|
+
}).trim();
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
}),
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
name: 'MCP SERVERS',
|
|
179
|
+
run: async () => probeMcpSafely(ctx),
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: 'CONFIG',
|
|
183
|
+
run: async () => probeConfig(ctx, {
|
|
184
|
+
existsSync,
|
|
185
|
+
readFileSync: (p, encoding) => readFileSync(p, encoding),
|
|
186
|
+
}),
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
name: 'SESSION',
|
|
190
|
+
run: async () => probeSession(ctx, {
|
|
191
|
+
existsSync,
|
|
192
|
+
statSync,
|
|
193
|
+
readFileSync: (p, encoding) => readFileSync(p, encoding),
|
|
194
|
+
}, {
|
|
195
|
+
now,
|
|
196
|
+
...(options.liveSessionId ? { liveSessionId: options.liveSessionId } : {}),
|
|
197
|
+
}),
|
|
198
|
+
},
|
|
199
|
+
// α7 L11 (2026-05-27): DENIAL TRACKING probe. Reports the live
|
|
200
|
+
// session's denial pressure when the REPL adapter wired the
|
|
201
|
+
// tracker through `runDoctorCommand`; degrades к `skipped` for
|
|
202
|
+
// top-level `pugi doctor` calls outside the REPL.
|
|
203
|
+
{
|
|
204
|
+
name: 'DENIAL TRACKING',
|
|
205
|
+
run: async () => probeDenialTracking({
|
|
206
|
+
...(options.denialTracking ? { tracker: options.denialTracking } : {}),
|
|
207
|
+
}),
|
|
208
|
+
},
|
|
209
|
+
];
|
|
210
|
+
return probes;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Run the full doctor sweep + emit the output via the supplied
|
|
214
|
+
* writeOutput sink. Returns the report so REPL callers can route it
|
|
215
|
+
* к the Ink renderer instead of the plain-text fallback.
|
|
216
|
+
*/
|
|
217
|
+
export async function runDoctorCommand(ctx) {
|
|
218
|
+
const probeCtx = {
|
|
219
|
+
cwd: ctx.cwd,
|
|
220
|
+
home: ctx.home,
|
|
221
|
+
env: ctx.env,
|
|
222
|
+
};
|
|
223
|
+
const probes = buildDefaultProbes(probeCtx, {
|
|
224
|
+
...(ctx.liveSessionId ? { liveSessionId: ctx.liveSessionId } : {}),
|
|
225
|
+
...(ctx.denialTracking ? { denialTracking: ctx.denialTracking } : {}),
|
|
226
|
+
});
|
|
227
|
+
const report = await runProbes(probes);
|
|
228
|
+
// Defensive recompute: even though runProbes already computed the
|
|
229
|
+
// overall + counts, recomputing here documents the invariant for the
|
|
230
|
+
// reader and gives the JSON envelope a single source of truth.
|
|
231
|
+
const overall = computeOverall(report.probes);
|
|
232
|
+
const counts = countProbes(report.probes);
|
|
233
|
+
const envelope = {
|
|
234
|
+
command: 'doctor',
|
|
235
|
+
overall,
|
|
236
|
+
counts,
|
|
237
|
+
durationMs: report.durationMs,
|
|
238
|
+
probes: report.probes,
|
|
239
|
+
meta: {
|
|
240
|
+
cliVersion: PUGI_CLI_VERSION,
|
|
241
|
+
nodeVersion: process.version,
|
|
242
|
+
cwd: ctx.cwd,
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
const text = renderDoctorTable(envelope);
|
|
246
|
+
ctx.writeOutput(envelope, text);
|
|
247
|
+
process.exitCode = exitCodeFor(overall);
|
|
248
|
+
return { ...report, overall, counts };
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Plain-text table renderer. Mirrors the layout from the leak-parity
|
|
252
|
+
* spec but is intentionally column-light (3 columns: NAME / STATUS /
|
|
253
|
+
* DETAIL) so it composes well in narrow terminals without dragging
|
|
254
|
+
* a layout library into the CLI hot path. The Ink TUI renderer in
|
|
255
|
+
* `tui/doctor-table.tsx` is the colour-aware variant used inside the
|
|
256
|
+
* REPL.
|
|
257
|
+
*/
|
|
258
|
+
export function renderDoctorTable(envelope) {
|
|
259
|
+
const NAME_WIDTH = Math.max('NAME'.length, ...envelope.probes.map((row) => row.name.length));
|
|
260
|
+
const STATUS_WIDTH = Math.max('STATUS'.length, ...envelope.probes.map((row) => row.status.length));
|
|
261
|
+
const lines = [];
|
|
262
|
+
lines.push('Pugi Doctor — environment health report');
|
|
263
|
+
lines.push('='.repeat(50));
|
|
264
|
+
lines.push('');
|
|
265
|
+
for (const row of envelope.probes) {
|
|
266
|
+
const namePart = row.name.padEnd(NAME_WIDTH, ' ');
|
|
267
|
+
const statusPart = row.status.toUpperCase().padEnd(STATUS_WIDTH, ' ');
|
|
268
|
+
const latencyPart = typeof row.latencyMs === 'number' ? ` (${row.latencyMs}ms)` : '';
|
|
269
|
+
lines.push(`${namePart} ${statusPart} ${row.detail}${latencyPart}`);
|
|
270
|
+
if (row.remediation && (row.status === 'warn' || row.status === 'error')) {
|
|
271
|
+
lines.push(`${' '.repeat(NAME_WIDTH + STATUS_WIDTH + 4)}→ ${row.remediation}`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
lines.push('');
|
|
275
|
+
const { ok, warn, error: errorCount, skipped } = envelope.counts;
|
|
276
|
+
const summary = envelope.overall === 'healthy'
|
|
277
|
+
? 'HEALTHY'
|
|
278
|
+
: envelope.overall === 'warning'
|
|
279
|
+
? 'WARNINGS'
|
|
280
|
+
: 'ERRORS';
|
|
281
|
+
lines.push(`${errorCount} error(s), ${warn} warning(s), ${ok} ok, ${skipped} skipped. Overall: ${summary}`);
|
|
282
|
+
lines.push(`CLI ${envelope.meta.cliVersion} Node ${envelope.meta.nodeVersion} cwd ${envelope.meta.cwd}`);
|
|
283
|
+
return lines.join('\n');
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Wrap the MCP probe in a dynamic import + try/catch so a missing
|
|
287
|
+
* sibling L13 surface (or a schema mismatch in `core/mcp/registry`)
|
|
288
|
+
* degrades the row к `skipped` instead of breaking the entire sweep.
|
|
289
|
+
* The probe-runner already isolates throws into `error` rows; this
|
|
290
|
+
* wrapper additionally distinguishes "feature not available" from
|
|
291
|
+
* "feature crashed".
|
|
292
|
+
*/
|
|
293
|
+
async function probeMcpSafely(ctx) {
|
|
294
|
+
try {
|
|
295
|
+
const mod = await import('../../core/mcp/registry.js');
|
|
296
|
+
if (typeof mod.loadMcpRegistry !== 'function') {
|
|
297
|
+
return {
|
|
298
|
+
name: 'MCP SERVERS',
|
|
299
|
+
status: 'skipped',
|
|
300
|
+
detail: 'MCP integration not exported by this build',
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
return await probeMcp(ctx, {
|
|
304
|
+
loadRegistry: (cwd, options) => mod.loadMcpRegistry(cwd, { connect: options.connect ?? false }),
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
309
|
+
return {
|
|
310
|
+
name: 'MCP SERVERS',
|
|
311
|
+
status: 'skipped',
|
|
312
|
+
detail: 'MCP integration not available',
|
|
313
|
+
remediation: `Inspection failed: ${message}`,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Best-effort free-bytes lookup via `df -k <home>`. Parses the second
|
|
319
|
+
* line (header + one data row) and returns the `Available` column ×
|
|
320
|
+
* 1024. Throws on parse failure so the probe surfaces a `warn`
|
|
321
|
+
* instead of a misleading 0-bytes-free verdict.
|
|
322
|
+
*
|
|
323
|
+
* Exported for the spec so we can drive it through a stubbed
|
|
324
|
+
* execFileSync without spawning a real subprocess.
|
|
325
|
+
*/
|
|
326
|
+
export function getFreeBytesViaDf(home) {
|
|
327
|
+
const out = execFileSync('df', ['-k', home], {
|
|
328
|
+
encoding: 'utf8',
|
|
329
|
+
timeout: 2_000,
|
|
330
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
331
|
+
});
|
|
332
|
+
return parseDfOutput(out);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Parse the textual output of `df -k`. Handles both BSD and GNU
|
|
336
|
+
* variants — both emit a `Available` column at index 3 of the data
|
|
337
|
+
* row, with one quirk: long device names wrap к the next line on
|
|
338
|
+
* GNU, so we collapse whitespace + tab newlines first.
|
|
339
|
+
*/
|
|
340
|
+
export function parseDfOutput(out) {
|
|
341
|
+
// Collapse multi-line device-name wraps into a single logical row.
|
|
342
|
+
const collapsed = out.replace(/\n\s+/g, ' ');
|
|
343
|
+
const lines = collapsed
|
|
344
|
+
.split('\n')
|
|
345
|
+
.map((line) => line.trim())
|
|
346
|
+
.filter((line) => line.length > 0);
|
|
347
|
+
if (lines.length < 2) {
|
|
348
|
+
throw new Error(`df output too short: ${JSON.stringify(out.slice(0, 64))}`);
|
|
349
|
+
}
|
|
350
|
+
const data = lines[1].split(/\s+/);
|
|
351
|
+
// Schema: Filesystem 1K-blocks Used Available Capacity Mounted-on
|
|
352
|
+
const availableField = data[3];
|
|
353
|
+
if (!availableField) {
|
|
354
|
+
throw new Error(`df output missing Available column: ${JSON.stringify(lines[1])}`);
|
|
355
|
+
}
|
|
356
|
+
const value = Number(availableField);
|
|
357
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
358
|
+
throw new Error(`df Available column not numeric: ${availableField}`);
|
|
359
|
+
}
|
|
360
|
+
return value * 1024;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Default home dir resolver. Centralised so the CLI handler can call
|
|
364
|
+
* `runDoctorCommand` without re-importing `os.homedir` everywhere.
|
|
365
|
+
*/
|
|
366
|
+
export function defaultHome() {
|
|
367
|
+
return homedir();
|
|
368
|
+
}
|
|
369
|
+
//# sourceMappingURL=doctor.js.map
|