@pugi/cli 0.1.0-beta.17 → 0.1.0-beta.19
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/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/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/engine/native-pugi.js +20 -0
- package/dist/core/engine/strip-internal-fields.js +124 -0
- package/dist/core/engine/tool-bridge.js +251 -49
- package/dist/core/file-cache.js +113 -1
- package/dist/core/mcp/client.js +66 -6
- package/dist/core/mcp/registry.js +24 -2
- 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/session.js +261 -9
- package/dist/core/repl/slash-commands.js +67 -4
- package/dist/runtime/cli.js +153 -58
- package/dist/runtime/commands/compact.js +296 -0
- package/dist/runtime/commands/doctor.js +369 -0
- package/dist/runtime/commands/mcp.js +290 -3
- package/dist/runtime/commands/permissions.js +87 -0
- package/dist/runtime/commands/status.js +178 -0
- package/dist/runtime/version.js +1 -1
- package/dist/tools/agent-tool.js +18 -4
- package/dist/tools/ask-user-question.js +213 -0
- package/dist/tools/file-tools.js +57 -14
- package/dist/tools/registry.js +7 -0
- package/dist/tui/ask-user-question-prompt.js +192 -0
- package/dist/tui/compact-banner.js +54 -0
- package/dist/tui/conversation-pane.js +68 -7
- package/dist/tui/doctor-table.js +31 -0
- package/dist/tui/status-table.js +7 -0
- package/package.json +2 -2
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pugi compact` — top-level command + companion to the REPL slash.
|
|
3
|
+
*
|
|
4
|
+
* Summarises older events from the most recent SessionStore session
|
|
5
|
+
* into a single boundary marker, freeing context budget for the next
|
|
6
|
+
* REPL session that resumes the same id.
|
|
7
|
+
*
|
|
8
|
+
* Wire: writeOutput contract matches every other dispatch in cli.ts
|
|
9
|
+
* (one structured payload + one human line). Exit code:
|
|
10
|
+
* 0 — boundary marker appended
|
|
11
|
+
* 1 — summarizer transport failed / store unavailable
|
|
12
|
+
* 2 — no events to compact / nothing to do
|
|
13
|
+
*
|
|
14
|
+
* The companion REPL slash dispatches through this same runner so the
|
|
15
|
+
* surface stays single-sourced.
|
|
16
|
+
*/
|
|
17
|
+
import { homedir } from 'node:os';
|
|
18
|
+
import { loadRuntimeConfig } from '@pugi/sdk';
|
|
19
|
+
import { AnvilEngineLoopClient } from '../../core/engine/anvil-client.js';
|
|
20
|
+
import { SqliteSessionStore, resolveProjectStoreDir, } from '../../core/repl/store/session-store.js';
|
|
21
|
+
import { appendCompactBoundary, isCompactBoundary, } from '../../core/compact/buffer-rewriter.js';
|
|
22
|
+
import { summarizeEvents, SummarizerError } from '../../core/compact/summarizer.js';
|
|
23
|
+
/** Number of tail turns preserved verbatim on every compaction. */
|
|
24
|
+
export const KEPT_TAIL_TURNS = 5;
|
|
25
|
+
/** Minimum source events before we accept a compaction. */
|
|
26
|
+
const MIN_EVENTS_TO_COMPACT = 6;
|
|
27
|
+
/**
|
|
28
|
+
* Entry point reused by the slash command + the top-level dispatcher.
|
|
29
|
+
*
|
|
30
|
+
* The function is exhaustive: every early-exit branch produces a
|
|
31
|
+
* structured `CompactCommandResult` for the JSON path AND a one-line
|
|
32
|
+
* human message for the TTY path. Tests assert on the returned shape;
|
|
33
|
+
* the top-level dispatcher forwards it to `writeOutput`.
|
|
34
|
+
*/
|
|
35
|
+
export async function runCompactCommand(_args, ctx) {
|
|
36
|
+
const trigger = ctx.trigger ?? 'manual';
|
|
37
|
+
// Resolve the target session id. When the caller supplied a store
|
|
38
|
+
// (REPL slash path, tests) we trust it has the session bound; the
|
|
39
|
+
// standalone CLI path needs to discover the most-recent id via the
|
|
40
|
+
// read-only view + then open the live store on that id.
|
|
41
|
+
let store = ctx.store ?? null;
|
|
42
|
+
let sessionId = ctx.sessionId ?? null;
|
|
43
|
+
if (store === null) {
|
|
44
|
+
sessionId = sessionId ?? (await pickMostRecentSessionIdReadOnly(ctx.workspaceRoot));
|
|
45
|
+
if (!sessionId) {
|
|
46
|
+
return emit(ctx, {
|
|
47
|
+
command: 'compact',
|
|
48
|
+
status: 'failed_no_session',
|
|
49
|
+
trigger,
|
|
50
|
+
reason: 'No active session to compact. Start a REPL with `pugi`.',
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
store = await openStoreForCwd(ctx.workspaceRoot, sessionId);
|
|
54
|
+
if (!store) {
|
|
55
|
+
return emit(ctx, {
|
|
56
|
+
command: 'compact',
|
|
57
|
+
status: 'failed_no_session',
|
|
58
|
+
trigger,
|
|
59
|
+
reason: 'Local session store unavailable (lock held by REPL?).',
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else if (sessionId === null) {
|
|
64
|
+
// Caller supplied a store but no session id — try to discover one.
|
|
65
|
+
sessionId = await pickMostRecentSessionId(store, ctx.workspaceRoot);
|
|
66
|
+
if (!sessionId) {
|
|
67
|
+
return emit(ctx, {
|
|
68
|
+
command: 'compact',
|
|
69
|
+
status: 'failed_no_session',
|
|
70
|
+
trigger,
|
|
71
|
+
reason: 'No active session to compact.',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Snapshot the events. We pass `coversUntilOffset` as the count of
|
|
76
|
+
// events at the moment of the snapshot so the marker's anchor is
|
|
77
|
+
// accurate even if other writers race afterwards.
|
|
78
|
+
let events;
|
|
79
|
+
try {
|
|
80
|
+
events = await store.loadEvents(sessionId);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return emit(ctx, {
|
|
84
|
+
command: 'compact',
|
|
85
|
+
status: 'failed_store',
|
|
86
|
+
sessionId,
|
|
87
|
+
trigger,
|
|
88
|
+
reason: `Could not load events: ${errMsg(error)}`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (events.length < MIN_EVENTS_TO_COMPACT) {
|
|
92
|
+
return emit(ctx, {
|
|
93
|
+
command: 'compact',
|
|
94
|
+
status: 'noop_empty',
|
|
95
|
+
sessionId,
|
|
96
|
+
trigger,
|
|
97
|
+
reason: `Only ${events.length} events on disk; need at least ${MIN_EVENTS_TO_COMPACT}.`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
// Locate the source slice: everything strictly after the latest
|
|
101
|
+
// compact-boundary, EXCLUDING the kept-tail window. If no prior
|
|
102
|
+
// boundary exists, walk from offset 0.
|
|
103
|
+
const lastBoundaryIdx = findLastCompactBoundaryIndex(events);
|
|
104
|
+
const sliceStart = lastBoundaryIdx === -1 ? 0 : lastBoundaryIdx + 1;
|
|
105
|
+
const sliceEnd = Math.max(sliceStart, events.length - KEPT_TAIL_TURNS);
|
|
106
|
+
const sourceSlice = events.slice(sliceStart, sliceEnd);
|
|
107
|
+
if (sourceSlice.length === 0) {
|
|
108
|
+
return emit(ctx, {
|
|
109
|
+
command: 'compact',
|
|
110
|
+
status: 'noop_recent_marker',
|
|
111
|
+
sessionId,
|
|
112
|
+
trigger,
|
|
113
|
+
reason: 'Conversation already compact — nothing new to fold.',
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
// Build the engine client. Production uses Anvil; tests inject a
|
|
117
|
+
// fixture client to avoid the network round-trip.
|
|
118
|
+
const engineClient = ctx.engineClient ?? buildAnvilClient();
|
|
119
|
+
if (!engineClient) {
|
|
120
|
+
return emit(ctx, {
|
|
121
|
+
command: 'compact',
|
|
122
|
+
status: 'failed_transport',
|
|
123
|
+
sessionId,
|
|
124
|
+
trigger,
|
|
125
|
+
reason: 'Could not build engine client (run `pugi login`).',
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
// Run the summariser.
|
|
129
|
+
let summary;
|
|
130
|
+
try {
|
|
131
|
+
summary = await summarizeEvents({
|
|
132
|
+
events: sourceSlice,
|
|
133
|
+
client: engineClient,
|
|
134
|
+
personaSlug: 'mira',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
const code = error instanceof SummarizerError ? error.code : 'unknown';
|
|
139
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
140
|
+
return emit(ctx, {
|
|
141
|
+
command: 'compact',
|
|
142
|
+
status: 'failed_transport',
|
|
143
|
+
sessionId,
|
|
144
|
+
trigger,
|
|
145
|
+
reason: `Summarizer failed (${code}): ${message}`,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// Append the boundary marker. Recompute `coversUntilOffset` AFTER
|
|
149
|
+
// the summariser round-trip so any events that landed mid-flight
|
|
150
|
+
// fall into the kept-tail window — the operator does not lose work
|
|
151
|
+
// that arrived during compaction. The next event we append (the
|
|
152
|
+
// marker itself) lands at index `coversUntilOffset` in the on-disk
|
|
153
|
+
// log.
|
|
154
|
+
let postRoundTripEvents;
|
|
155
|
+
try {
|
|
156
|
+
postRoundTripEvents = await store.loadEvents(sessionId);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
return emit(ctx, {
|
|
160
|
+
command: 'compact',
|
|
161
|
+
status: 'failed_store',
|
|
162
|
+
sessionId,
|
|
163
|
+
trigger,
|
|
164
|
+
reason: `Could not refresh events: ${errMsg(error)}`,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const coversUntilOffset = postRoundTripEvents.length;
|
|
168
|
+
try {
|
|
169
|
+
await appendCompactBoundary({
|
|
170
|
+
store,
|
|
171
|
+
trigger,
|
|
172
|
+
summary: summary.summary,
|
|
173
|
+
summaryTokenCount: summary.tokensSummarised,
|
|
174
|
+
summaryTurnsBefore: summary.eventsSummarised,
|
|
175
|
+
keptTailTurns: KEPT_TAIL_TURNS,
|
|
176
|
+
coversUntilOffset,
|
|
177
|
+
...(ctx.now !== undefined ? { now: ctx.now } : {}),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
return emit(ctx, {
|
|
182
|
+
command: 'compact',
|
|
183
|
+
status: 'failed_store',
|
|
184
|
+
sessionId,
|
|
185
|
+
trigger,
|
|
186
|
+
reason: `Could not append marker: ${errMsg(error)}`,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return emit(ctx, {
|
|
190
|
+
command: 'compact',
|
|
191
|
+
status: 'compacted',
|
|
192
|
+
sessionId,
|
|
193
|
+
trigger,
|
|
194
|
+
turnsBefore: summary.eventsSummarised,
|
|
195
|
+
tailKept: KEPT_TAIL_TURNS,
|
|
196
|
+
tokensSummarised: summary.tokensSummarised,
|
|
197
|
+
}, `Compacted ${summary.eventsSummarised} events (~${summary.tokensSummarised} tokens) into 1 summary. ` +
|
|
198
|
+
`Last ${KEPT_TAIL_TURNS} turns kept verbatim.`);
|
|
199
|
+
}
|
|
200
|
+
/* ------------------------------------------------------------------ */
|
|
201
|
+
/* Helpers */
|
|
202
|
+
/* ------------------------------------------------------------------ */
|
|
203
|
+
/**
|
|
204
|
+
* Emit a payload through the context and return it for the caller. We
|
|
205
|
+
* keep the function exhaustive so every branch lands on the same
|
|
206
|
+
* writeOutput contract (JSON-mode + text-mode).
|
|
207
|
+
*/
|
|
208
|
+
function emit(ctx, payload, text) {
|
|
209
|
+
const human = text ?? payload.reason ?? `compact: ${payload.status}`;
|
|
210
|
+
ctx.writeOutput(payload, human);
|
|
211
|
+
return payload;
|
|
212
|
+
}
|
|
213
|
+
function findLastCompactBoundaryIndex(events) {
|
|
214
|
+
for (let i = events.length - 1; i >= 0; i -= 1) {
|
|
215
|
+
if (isCompactBoundary(events[i]))
|
|
216
|
+
return i;
|
|
217
|
+
}
|
|
218
|
+
return -1;
|
|
219
|
+
}
|
|
220
|
+
function errMsg(error) {
|
|
221
|
+
return error instanceof Error ? error.message : String(error);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Open the SqliteSessionStore for the workspace's project slug and
|
|
225
|
+
* bind the active log to `sessionId`. Returns null when the open
|
|
226
|
+
* fails (lock contention, IO error, missing project dir).
|
|
227
|
+
*
|
|
228
|
+
* The PID lockfile contention is the expected failure mode here — a
|
|
229
|
+
* running REPL holds the lock, and `pugi compact` from a second
|
|
230
|
+
* terminal cannot grab it. The caller surfaces a clear message rather
|
|
231
|
+
* than crashing the dispatch.
|
|
232
|
+
*/
|
|
233
|
+
async function openStoreForCwd(workspaceRoot, sessionId) {
|
|
234
|
+
try {
|
|
235
|
+
const slug = projectSlugForCwd(workspaceRoot);
|
|
236
|
+
const store = new SqliteSessionStore({ projectSlug: slug, home: homedir() });
|
|
237
|
+
await store.open({
|
|
238
|
+
id: sessionId,
|
|
239
|
+
workspaceRoot,
|
|
240
|
+
projectSlug: slug,
|
|
241
|
+
});
|
|
242
|
+
return store;
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Read-only-view variant of `pickMostRecentSessionId` for the
|
|
250
|
+
* pre-open phase. Walks `~/.pugi/projects/<slug>/session.db` without
|
|
251
|
+
* taking the lockfile so a live REPL in another terminal does not
|
|
252
|
+
* make discovery fail.
|
|
253
|
+
*/
|
|
254
|
+
async function pickMostRecentSessionIdReadOnly(workspaceRoot) {
|
|
255
|
+
try {
|
|
256
|
+
const slug = projectSlugForCwd(workspaceRoot);
|
|
257
|
+
const dir = resolveProjectStoreDir(slug, homedir());
|
|
258
|
+
const view = await SqliteSessionStore.openReadOnly(dir);
|
|
259
|
+
try {
|
|
260
|
+
const rows = await view.list({ project: slug, limit: 1, status: 'active' });
|
|
261
|
+
return rows.length > 0 ? rows[0].id : null;
|
|
262
|
+
}
|
|
263
|
+
finally {
|
|
264
|
+
await view.close();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Locate the most recent active session for the workspace. Returns
|
|
273
|
+
* null when no session row exists.
|
|
274
|
+
*/
|
|
275
|
+
async function pickMostRecentSessionId(store, workspaceRoot) {
|
|
276
|
+
const slug = projectSlugForCwd(workspaceRoot);
|
|
277
|
+
const rows = await store.listSessions({ project: slug, limit: 1, status: 'active' });
|
|
278
|
+
if (rows.length === 0)
|
|
279
|
+
return null;
|
|
280
|
+
return rows[0].id;
|
|
281
|
+
}
|
|
282
|
+
function projectSlugForCwd(workspaceRoot) {
|
|
283
|
+
const base = workspaceRoot.split('/').filter((s) => s.length > 0).pop() ?? 'workspace';
|
|
284
|
+
return base.toLowerCase().replace(/[^a-z0-9-]/g, '-').slice(0, 64);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Construct an Anvil-backed engine client from env-resolved runtime
|
|
288
|
+
* config. Returns null when the operator is not authenticated.
|
|
289
|
+
*/
|
|
290
|
+
function buildAnvilClient() {
|
|
291
|
+
const config = loadRuntimeConfig();
|
|
292
|
+
if (!config)
|
|
293
|
+
return null;
|
|
294
|
+
return new AnvilEngineLoopClient(config);
|
|
295
|
+
}
|
|
296
|
+
//# sourceMappingURL=compact.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
|