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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/THIRD_PARTY_NOTICES.md +40 -0
  2. package/assets/pugi-mascot.ansi +15 -40
  3. package/bin/run.js +33 -1
  4. package/dist/commands/jobs-watch.js +201 -0
  5. package/dist/commands/jobs.js +15 -0
  6. package/dist/core/agent-progress/cleanup.js +134 -0
  7. package/dist/core/agent-progress/schema.js +144 -0
  8. package/dist/core/agent-progress/writer.js +101 -0
  9. package/dist/core/compact/auto-trigger.js +96 -0
  10. package/dist/core/compact/buffer-rewriter.js +115 -0
  11. package/dist/core/compact/summarizer.js +196 -0
  12. package/dist/core/compact/token-counter.js +108 -0
  13. package/dist/core/consensus/diff-capture.js +73 -0
  14. package/dist/core/context/index.js +7 -0
  15. package/dist/core/context/markdown-traverse.js +255 -0
  16. package/dist/core/cost/rate-card.js +129 -0
  17. package/dist/core/cost/tracker.js +221 -0
  18. package/dist/core/denial-tracking/index.js +8 -0
  19. package/dist/core/denial-tracking/state.js +264 -0
  20. package/dist/core/diagnostics/probe-runner.js +93 -0
  21. package/dist/core/diagnostics/probes/api.js +46 -0
  22. package/dist/core/diagnostics/probes/auth.js +86 -0
  23. package/dist/core/diagnostics/probes/cli-version.js +127 -0
  24. package/dist/core/diagnostics/probes/config.js +72 -0
  25. package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
  26. package/dist/core/diagnostics/probes/disk.js +81 -0
  27. package/dist/core/diagnostics/probes/git.js +65 -0
  28. package/dist/core/diagnostics/probes/mcp.js +75 -0
  29. package/dist/core/diagnostics/probes/node.js +59 -0
  30. package/dist/core/diagnostics/probes/pnpm.js +36 -0
  31. package/dist/core/diagnostics/probes/session.js +74 -0
  32. package/dist/core/diagnostics/probes/status-snapshot.js +442 -0
  33. package/dist/core/diagnostics/probes/workspace.js +63 -0
  34. package/dist/core/diagnostics/types.js +70 -0
  35. package/dist/core/edits/dispatch.js +218 -2
  36. package/dist/core/edits/journal.js +199 -0
  37. package/dist/core/edits/layer-d-ast.js +557 -14
  38. package/dist/core/edits/verify-hook.js +273 -0
  39. package/dist/core/edits/worktree.js +111 -18
  40. package/dist/core/engine/anvil-client.js +115 -5
  41. package/dist/core/engine/budgets.js +89 -0
  42. package/dist/core/engine/context-prefix.js +155 -0
  43. package/dist/core/engine/intent.js +260 -0
  44. package/dist/core/engine/native-pugi.js +744 -210
  45. package/dist/core/engine/prompts.js +61 -6
  46. package/dist/core/engine/strip-internal-fields.js +124 -0
  47. package/dist/core/engine/tool-bridge.js +818 -31
  48. package/dist/core/file-cache.js +113 -1
  49. package/dist/core/init/scaffold.js +195 -0
  50. package/dist/core/lsp/client.js +174 -29
  51. package/dist/core/mcp/client.js +75 -6
  52. package/dist/core/mcp/http-server.js +553 -0
  53. package/dist/core/mcp/permission.js +190 -0
  54. package/dist/core/mcp/registry.js +24 -2
  55. package/dist/core/mcp/server-tools.js +219 -0
  56. package/dist/core/mcp/server.js +397 -0
  57. package/dist/core/permissions/gate.js +187 -0
  58. package/dist/core/permissions/index.js +18 -0
  59. package/dist/core/permissions/mode.js +102 -0
  60. package/dist/core/permissions/state.js +160 -0
  61. package/dist/core/permissions/tool-class.js +93 -0
  62. package/dist/core/repl/codebase-survey.js +308 -0
  63. package/dist/core/repl/history.js +11 -1
  64. package/dist/core/repl/init-interview.js +457 -0
  65. package/dist/core/repl/model-pricing.js +135 -0
  66. package/dist/core/repl/onboarding-state.js +297 -0
  67. package/dist/core/repl/session.js +719 -29
  68. package/dist/core/repl/slash-commands.js +133 -9
  69. package/dist/core/retry-budget/budget.js +284 -0
  70. package/dist/core/retry-budget/index.js +5 -0
  71. package/dist/core/settings.js +71 -0
  72. package/dist/core/skills/defaults.js +457 -0
  73. package/dist/core/subagents/dispatcher-real.js +600 -0
  74. package/dist/core/subagents/dispatcher.js +113 -24
  75. package/dist/core/subagents/index.js +18 -5
  76. package/dist/core/subagents/isolation-matrix.js +213 -0
  77. package/dist/core/subagents/spawn.js +19 -4
  78. package/dist/core/transport/version-interceptor.js +166 -0
  79. package/dist/index.js +28 -0
  80. package/dist/runtime/bootstrap.js +190 -0
  81. package/dist/runtime/cli.js +1588 -266
  82. package/dist/runtime/commands/compact.js +296 -0
  83. package/dist/runtime/commands/cost.js +199 -0
  84. package/dist/runtime/commands/delegate.js +289 -0
  85. package/dist/runtime/commands/doctor.js +369 -0
  86. package/dist/runtime/commands/lsp.js +187 -5
  87. package/dist/runtime/commands/mcp.js +824 -0
  88. package/dist/runtime/commands/patch.js +17 -0
  89. package/dist/runtime/commands/permissions.js +87 -0
  90. package/dist/runtime/commands/report.js +299 -0
  91. package/dist/runtime/commands/review-consensus.js +17 -2
  92. package/dist/runtime/commands/roster.js +117 -0
  93. package/dist/runtime/commands/status.js +178 -0
  94. package/dist/runtime/commands/worktree.js +50 -6
  95. package/dist/runtime/headless.js +543 -0
  96. package/dist/runtime/load-hooks-or-exit.js +71 -0
  97. package/dist/runtime/plan-decompose.js +531 -0
  98. package/dist/runtime/version.js +65 -0
  99. package/dist/tools/agent-tool.js +206 -0
  100. package/dist/tools/apply-patch.js +281 -39
  101. package/dist/tools/ask-user-question.js +213 -0
  102. package/dist/tools/ask-user.js +115 -0
  103. package/dist/tools/file-tools.js +85 -14
  104. package/dist/tools/mcp-tool.js +260 -0
  105. package/dist/tools/multi-edit.js +361 -0
  106. package/dist/tools/registry.js +22 -2
  107. package/dist/tools/skill-tool.js +96 -0
  108. package/dist/tools/tasks.js +208 -0
  109. package/dist/tools/web-fetch.js +147 -2
  110. package/dist/tools/web-search.js +458 -0
  111. package/dist/tui/agent-progress-card.js +111 -0
  112. package/dist/tui/agent-tree.js +10 -0
  113. package/dist/tui/ask-modal.js +2 -2
  114. package/dist/tui/ask-user-question-prompt.js +192 -0
  115. package/dist/tui/compact-banner.js +54 -0
  116. package/dist/tui/conversation-pane.js +69 -8
  117. package/dist/tui/cost-table.js +111 -0
  118. package/dist/tui/doctor-table.js +31 -0
  119. package/dist/tui/input-box.js +1 -1
  120. package/dist/tui/markdown-render.js +4 -4
  121. package/dist/tui/repl-render.js +276 -37
  122. package/dist/tui/repl-splash.js +2 -2
  123. package/dist/tui/repl.js +25 -6
  124. package/dist/tui/splash.js +1 -1
  125. package/dist/tui/status-bar.js +94 -16
  126. package/dist/tui/status-table.js +7 -0
  127. package/dist/tui/tool-stream-pane.js +7 -0
  128. package/dist/tui/update-banner.js +20 -2
  129. package/docs/examples/codegraph.mcp.json +10 -0
  130. package/package.json +9 -6
@@ -0,0 +1,74 @@
1
+ /**
2
+ * SESSION probe — reports the active session id + age when the doctor
3
+ * runs from inside the REPL OR finds a recent NDJSON session log in
4
+ * the workspace.
5
+ *
6
+ * The CLI command path has no live session context (each `pugi <cmd>`
7
+ * invocation is a fresh process), so we read `.pugi/events.jsonl` if
8
+ * present and report the most-recent event's age + total line count.
9
+ * Inside the REPL we pass an explicit `sessionId` so the probe
10
+ * surfaces the live state without re-reading disk.
11
+ *
12
+ * Absence of `.pugi/events.jsonl` is `skipped`, not an error — the
13
+ * operator may simply be running `pugi doctor` in a workspace that
14
+ * has not yet seen a dispatch.
15
+ */
16
+ export function probeSession(ctx, fs, deps) {
17
+ if (deps.liveSessionId) {
18
+ return {
19
+ name: 'SESSION',
20
+ status: 'ok',
21
+ detail: `session=${deps.liveSessionId} (live, REPL active)`,
22
+ };
23
+ }
24
+ const eventsPath = `${ctx.cwd}/.pugi/events.jsonl`;
25
+ if (!fs.existsSync(eventsPath)) {
26
+ return {
27
+ name: 'SESSION',
28
+ status: 'skipped',
29
+ detail: 'No prior dispatch logged in this workspace',
30
+ };
31
+ }
32
+ let stats;
33
+ try {
34
+ stats = fs.statSync(eventsPath);
35
+ }
36
+ catch (error) {
37
+ const message = error instanceof Error ? error.message : String(error);
38
+ return {
39
+ name: 'SESSION',
40
+ status: 'warn',
41
+ detail: `.pugi/events.jsonl present but unreadable`,
42
+ remediation: `Inspect: ${message}`,
43
+ };
44
+ }
45
+ const ageMs = Math.max(0, deps.now() - stats.mtimeMs);
46
+ const ageLabel = formatAge(ageMs);
47
+ // Counting lines is cheap on a small NDJSON file; a "huge" Pugi
48
+ // session is single-digit MB. We avoid loading binary blobs by
49
+ // simply walking the buffer count.
50
+ let lineCount = 0;
51
+ try {
52
+ const raw = fs.readFileSync(eventsPath, 'utf8');
53
+ lineCount = raw.split('\n').filter((line) => line.trim().length > 0).length;
54
+ }
55
+ catch {
56
+ lineCount = -1;
57
+ }
58
+ const linePart = lineCount >= 0 ? `, ${lineCount} event(s)` : '';
59
+ return {
60
+ name: 'SESSION',
61
+ status: 'ok',
62
+ detail: `last event ${ageLabel} ago${linePart}`,
63
+ };
64
+ }
65
+ export function formatAge(ms) {
66
+ if (ms < 60_000)
67
+ return `${Math.round(ms / 1000)}s`;
68
+ if (ms < 3_600_000)
69
+ return `${Math.round(ms / 60_000)}m`;
70
+ if (ms < 86_400_000)
71
+ return `${Math.round(ms / 3_600_000)}h`;
72
+ return `${Math.round(ms / 86_400_000)}d`;
73
+ }
74
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1,442 @@
1
+ /**
2
+ * STATUS snapshot — concise session-state probe for `pugi status` /
3
+ * in-REPL `/status` (Leak L34, 2026-05-27).
4
+ *
5
+ * Different from `pugi doctor` (which probes ENVIRONMENT health —
6
+ * Node version, disk space, auth round-trip, MCP servers, …). The
7
+ * status snapshot answers the operator question "what is THIS
8
+ * Pugi session doing right now?" — current session id + age, cwd,
9
+ * permission mode, CLI version, cumulative token usage, active
10
+ * dispatches, last command, compact boundary count, auth identity.
11
+ *
12
+ * # Fail-soft contract
13
+ *
14
+ * Every field is gathered via try/catch. A missing dep (permission
15
+ * state module not yet present, no `.pugi/agent-progress/` dir,
16
+ * no `~/.pugi/credentials.json`) is **never** an error — it
17
+ * degrades to `null` for nullable fields or a sentinel label
18
+ * (`"unknown"`, `"n/a"`, `0`) for required ones. The snapshot
19
+ * never throws; the renderer can always print a complete table.
20
+ *
21
+ * The module is intentionally dep-injected so the spec can drive
22
+ * every fail-soft branch without standing up `fs`, the credential
23
+ * store, or a real process clock. The `runStatusCommand` wrapper
24
+ * in `runtime/commands/status.ts` plugs in production deps.
25
+ *
26
+ * # Field semantics
27
+ *
28
+ * sessionId / sessionAgeMs : taken verbatim from caller when in
29
+ * REPL mode (the slash dispatcher passes the live id +
30
+ * `briefStartedAtEpochMs`); else best-effort from the on-disk
31
+ * `.pugi/events.jsonl` mtime, OR null when no prior dispatch is
32
+ * logged in the workspace.
33
+ *
34
+ * cwd : pure function of the caller's `ctx.cwd`. Never null.
35
+ *
36
+ * permissionMode : reads the optional permissions state module
37
+ * when available (L6 in the leak-parity roadmap). On any
38
+ * import failure the field degrades to "unknown" so this
39
+ * command lands before L6 without a compile-time coupling.
40
+ *
41
+ * cliVersion : injected literal so the build's PUGI_CLI_VERSION
42
+ * constant is the single source of truth.
43
+ *
44
+ * tokensUsed : injected from the caller — REPL mode passes the
45
+ * live `sessionTokensIn + sessionTokensOut` total; the
46
+ * top-level `pugi status` invocation has no REPL state so
47
+ * this field stays null and the renderer prints "n/a".
48
+ *
49
+ * activeDispatches / completedDispatches : counted by scanning
50
+ * `.pugi/agent-progress/*.json` and classifying by the
51
+ * `status` field. Missing dir → 0/0 (no crash).
52
+ *
53
+ * lastCommand : injected from the REPL when available; null on
54
+ * the top-level shell path so the renderer prints "n/a".
55
+ *
56
+ * compactBoundaries : counts lines in `.pugi/events.jsonl`
57
+ * where the event `kind === 'compact.boundary'`. Missing file
58
+ * → 0. Wired against Memory Phase 0 NDJSON; downgrade to 0 if
59
+ * the file is malformed.
60
+ *
61
+ * auth : resolved via the same credential helper `pugi whoami`
62
+ * uses. Returns either an `{ apiUrl, apiKey, label?, tier? }`
63
+ * summary or null when the operator is not authenticated. We
64
+ * do NOT make a network call here — the tier is read from
65
+ * local creds metadata; live tier lookup belongs to
66
+ * `pugi whoami --remote` (separate command).
67
+ */
68
+ /**
69
+ * Collect the full snapshot. The function is synchronous because
70
+ * every field source is local (process state, fs, injected
71
+ * resolvers) — no network calls.
72
+ */
73
+ export function collectStatusSnapshot(deps) {
74
+ const fields = [];
75
+ // Session id + age. REPL-mode caller passes both; top-level
76
+ // shell path passes neither и we fall back to the on-disk NDJSON
77
+ // tail mtime so the operator still sees a useful age figure for
78
+ // the most-recent workspace activity.
79
+ fields.push(buildSessionField(deps));
80
+ // Working directory. Pure function of caller's ctx — always
81
+ // available (sentinel only if caller passed an empty string,
82
+ // which the renderer treats as "unknown").
83
+ fields.push({
84
+ key: 'workdir',
85
+ label: 'Workdir',
86
+ value: deps.cwd.length > 0 ? deps.cwd : 'unknown',
87
+ available: deps.cwd.length > 0,
88
+ });
89
+ // Permission mode. Fail-soft — degrades к "unknown" until L6
90
+ // lands the permissions/state module.
91
+ fields.push(buildPermissionModeField(deps));
92
+ // Pugi CLI version. The build-time constant is the single
93
+ // source of truth; sanitised upstream (sanitizeSemver in
94
+ // runtime/version.ts) so we never see `workspace:*` here.
95
+ fields.push({
96
+ key: 'cli',
97
+ label: 'CLI',
98
+ value: deps.cliVersion,
99
+ available: deps.cliVersion !== '0.0.0-unknown',
100
+ });
101
+ // Token usage. REPL caller passes the live total; shell path
102
+ // degrades к "n/a" (no REPL state in a fresh process).
103
+ fields.push(buildTokenField(deps));
104
+ // Active + completed dispatches scanned from
105
+ // `.pugi/agent-progress/*.json`. Missing dir → 0 active, 0
106
+ // completed (no crash).
107
+ fields.push(buildDispatchField(deps));
108
+ // Last command. REPL caller passes the most-recent operator
109
+ // line + its timestamp; shell path degrades к "n/a".
110
+ fields.push(buildLastCommandField(deps));
111
+ // Compact boundary marker count. Read from
112
+ // `.pugi/events.jsonl` per Memory Phase 0; missing file → 0.
113
+ fields.push(buildCompactField(deps));
114
+ // Auth identity. Read from the credential resolver — local-only
115
+ // (no network call); the live tier comes from the stored
116
+ // credential metadata.
117
+ fields.push(buildAuthField(deps));
118
+ // Connection mode. Today simply mirrors the credential source
119
+ // (env vs file) so the operator can see at a glance whether
120
+ // they are in CI-env mode or a logged-in shell. Future wiring
121
+ // overlays the REPL's live connection state when invoked from
122
+ // the slash dispatcher.
123
+ fields.push(buildConnectionField(deps));
124
+ return {
125
+ command: 'status',
126
+ fields,
127
+ meta: {
128
+ cliVersion: deps.cliVersion,
129
+ nodeVersion: process.version,
130
+ cwd: deps.cwd,
131
+ capturedAt: new Date(deps.now()).toISOString(),
132
+ },
133
+ };
134
+ }
135
+ /* -------------------------- field builders -------------------------- */
136
+ function buildSessionField(deps) {
137
+ if (deps.liveSessionId && deps.liveSessionId.length > 0) {
138
+ const ageMs = typeof deps.sessionStartedAtEpochMs === 'number' && deps.sessionStartedAtEpochMs > 0
139
+ ? Math.max(0, deps.now() - deps.sessionStartedAtEpochMs)
140
+ : null;
141
+ const ageLabel = ageMs === null ? '' : ` (${formatAgeShort(ageMs)})`;
142
+ return {
143
+ key: 'session',
144
+ label: 'Session',
145
+ value: `${shortId(deps.liveSessionId)}${ageLabel}`,
146
+ available: true,
147
+ };
148
+ }
149
+ // Top-level shell path: best-effort tail of .pugi/events.jsonl.
150
+ const eventsPath = `${deps.cwd}/.pugi/events.jsonl`;
151
+ try {
152
+ if (!deps.fs.existsSync(eventsPath)) {
153
+ return {
154
+ key: 'session',
155
+ label: 'Session',
156
+ value: 'unbound (no prior dispatch in this workspace)',
157
+ available: false,
158
+ note: '.pugi/events.jsonl not found',
159
+ };
160
+ }
161
+ const stats = deps.fs.statSync(eventsPath);
162
+ const ageMs = Math.max(0, deps.now() - stats.mtimeMs);
163
+ return {
164
+ key: 'session',
165
+ label: 'Session',
166
+ value: `unbound · last activity ${formatAgeShort(ageMs)} ago`,
167
+ // Shell path never has a live id — flag as unavailable so the
168
+ // renderer can dim the row.
169
+ available: false,
170
+ note: 'shell-mode: showing last NDJSON activity instead of a live id',
171
+ };
172
+ }
173
+ catch (error) {
174
+ return {
175
+ key: 'session',
176
+ label: 'Session',
177
+ value: 'unknown',
178
+ available: false,
179
+ note: errorNote(error),
180
+ };
181
+ }
182
+ }
183
+ function buildPermissionModeField(deps) {
184
+ try {
185
+ const mode = deps.resolvePermissionMode();
186
+ if (mode && mode.length > 0) {
187
+ return {
188
+ key: 'mode',
189
+ label: 'Mode',
190
+ value: mode,
191
+ available: true,
192
+ };
193
+ }
194
+ return {
195
+ key: 'mode',
196
+ label: 'Mode',
197
+ value: 'unknown',
198
+ available: false,
199
+ note: 'permission state module not yet present (lands with L6)',
200
+ };
201
+ }
202
+ catch (error) {
203
+ return {
204
+ key: 'mode',
205
+ label: 'Mode',
206
+ value: 'unknown',
207
+ available: false,
208
+ note: errorNote(error),
209
+ };
210
+ }
211
+ }
212
+ function buildTokenField(deps) {
213
+ if (typeof deps.liveTokensUsed === 'number' && deps.liveTokensUsed >= 0) {
214
+ return {
215
+ key: 'tokens',
216
+ label: 'Tokens',
217
+ value: `~${formatThousands(deps.liveTokensUsed)} used this session`,
218
+ available: true,
219
+ };
220
+ }
221
+ return {
222
+ key: 'tokens',
223
+ label: 'Tokens',
224
+ value: 'n/a (REPL not active)',
225
+ available: false,
226
+ note: 'token counter only available inside a live REPL session',
227
+ };
228
+ }
229
+ function buildDispatchField(deps) {
230
+ const dir = `${deps.cwd}/.pugi/agent-progress`;
231
+ try {
232
+ if (!deps.fs.existsSync(dir)) {
233
+ return {
234
+ key: 'dispatches',
235
+ label: 'Dispatches',
236
+ value: '0 active, 0 completed',
237
+ available: false,
238
+ note: '.pugi/agent-progress/ not present',
239
+ };
240
+ }
241
+ const entries = deps.fs.readdirSync(dir);
242
+ let active = 0;
243
+ let completed = 0;
244
+ for (const entry of entries) {
245
+ if (!entry.endsWith('.json'))
246
+ continue;
247
+ try {
248
+ const raw = deps.fs.readFileSync(`${dir}/${entry}`, 'utf8');
249
+ const parsed = JSON.parse(raw);
250
+ const status = typeof parsed.status === 'string' ? parsed.status.toLowerCase() : null;
251
+ const completedAt = typeof parsed.completedAt === 'string' ? parsed.completedAt : null;
252
+ if (status === 'complete' || status === 'completed' || completedAt) {
253
+ completed += 1;
254
+ }
255
+ else {
256
+ active += 1;
257
+ }
258
+ }
259
+ catch {
260
+ // Malformed progress JSON shouldn't crash the snapshot.
261
+ // Count it as active so the operator sees the row + can
262
+ // open the file manually.
263
+ active += 1;
264
+ }
265
+ }
266
+ return {
267
+ key: 'dispatches',
268
+ label: 'Dispatches',
269
+ value: `${active} active, ${completed} completed`,
270
+ available: true,
271
+ };
272
+ }
273
+ catch (error) {
274
+ return {
275
+ key: 'dispatches',
276
+ label: 'Dispatches',
277
+ value: '0 active, 0 completed',
278
+ available: false,
279
+ note: errorNote(error),
280
+ };
281
+ }
282
+ }
283
+ function buildLastCommandField(deps) {
284
+ if (typeof deps.lastCommand === 'string' && deps.lastCommand.trim().length > 0) {
285
+ const cmd = truncate(deps.lastCommand.trim(), 60);
286
+ const agoLabel = typeof deps.lastCommandAtEpochMs === 'number' && deps.lastCommandAtEpochMs > 0
287
+ ? ` (${formatAgeShort(Math.max(0, deps.now() - deps.lastCommandAtEpochMs))} ago)`
288
+ : '';
289
+ return {
290
+ key: 'lastCommand',
291
+ label: 'Last cmd',
292
+ value: `${cmd}${agoLabel}`,
293
+ available: true,
294
+ };
295
+ }
296
+ return {
297
+ key: 'lastCommand',
298
+ label: 'Last cmd',
299
+ value: 'n/a',
300
+ available: false,
301
+ note: 'no command observed in this session',
302
+ };
303
+ }
304
+ function buildCompactField(deps) {
305
+ const eventsPath = `${deps.cwd}/.pugi/events.jsonl`;
306
+ try {
307
+ if (!deps.fs.existsSync(eventsPath)) {
308
+ return {
309
+ key: 'compact',
310
+ label: 'Compact',
311
+ value: '0 boundary markers',
312
+ available: false,
313
+ note: '.pugi/events.jsonl not present',
314
+ };
315
+ }
316
+ const raw = deps.fs.readFileSync(eventsPath, 'utf8');
317
+ let count = 0;
318
+ for (const line of raw.split('\n')) {
319
+ const trimmed = line.trim();
320
+ if (trimmed.length === 0)
321
+ continue;
322
+ try {
323
+ const parsed = JSON.parse(trimmed);
324
+ const kind = typeof parsed.kind === 'string' ? parsed.kind : null;
325
+ const type = typeof parsed.type === 'string' ? parsed.type : null;
326
+ if (kind === 'compact.boundary' || type === 'compact.boundary') {
327
+ count += 1;
328
+ }
329
+ }
330
+ catch {
331
+ // Malformed line in NDJSON is not a snapshot failure.
332
+ // Skip and keep counting.
333
+ }
334
+ }
335
+ return {
336
+ key: 'compact',
337
+ label: 'Compact',
338
+ value: `${count} boundary marker${count === 1 ? '' : 's'}`,
339
+ available: true,
340
+ };
341
+ }
342
+ catch (error) {
343
+ return {
344
+ key: 'compact',
345
+ label: 'Compact',
346
+ value: '0 boundary markers',
347
+ available: false,
348
+ note: errorNote(error),
349
+ };
350
+ }
351
+ }
352
+ function buildAuthField(deps) {
353
+ try {
354
+ const cred = deps.resolveCredential();
355
+ if (!cred) {
356
+ return {
357
+ key: 'auth',
358
+ label: 'Auth',
359
+ value: 'not signed in',
360
+ available: false,
361
+ note: 'run `pugi login` к authenticate',
362
+ };
363
+ }
364
+ const identity = cred.identity ?? cred.label ?? cred.apiUrl;
365
+ const tier = cred.tier ? ` (tier: ${cred.tier})` : '';
366
+ return {
367
+ key: 'auth',
368
+ label: 'Auth',
369
+ value: `${identity}${tier}`,
370
+ available: true,
371
+ };
372
+ }
373
+ catch (error) {
374
+ return {
375
+ key: 'auth',
376
+ label: 'Auth',
377
+ value: 'unknown',
378
+ available: false,
379
+ note: errorNote(error),
380
+ };
381
+ }
382
+ }
383
+ function buildConnectionField(deps) {
384
+ try {
385
+ const cred = deps.resolveCredential();
386
+ if (!cred) {
387
+ return {
388
+ key: 'connection',
389
+ label: 'Connection',
390
+ value: 'offline (no credential)',
391
+ available: false,
392
+ };
393
+ }
394
+ return {
395
+ key: 'connection',
396
+ label: 'Connection',
397
+ value: cred.apiUrl,
398
+ available: true,
399
+ };
400
+ }
401
+ catch {
402
+ return {
403
+ key: 'connection',
404
+ label: 'Connection',
405
+ value: 'unknown',
406
+ available: false,
407
+ };
408
+ }
409
+ }
410
+ /* ---------------------------- formatters ---------------------------- */
411
+ export function formatAgeShort(ms) {
412
+ if (!Number.isFinite(ms) || ms < 0)
413
+ return '0s';
414
+ if (ms < 60_000)
415
+ return `${Math.round(ms / 1000)} sec`;
416
+ if (ms < 3_600_000)
417
+ return `${Math.round(ms / 60_000)} min`;
418
+ if (ms < 86_400_000)
419
+ return `${Math.round(ms / 3_600_000)} hr`;
420
+ return `${Math.round(ms / 86_400_000)} day`;
421
+ }
422
+ export function formatThousands(value) {
423
+ if (!Number.isFinite(value) || value < 0)
424
+ return '0';
425
+ return Math.round(value).toLocaleString('en-US');
426
+ }
427
+ export function shortId(id) {
428
+ // The full ULID / UUID is awkward in a table cell. Keep the
429
+ // first 13 chars (long enough к stay collision-free across the
430
+ // recent-sessions list, short enough к share at a glance).
431
+ return id.length > 13 ? id.slice(0, 13) : id;
432
+ }
433
+ export function truncate(text, max) {
434
+ if (text.length <= max)
435
+ return text;
436
+ return `${text.slice(0, Math.max(1, max - 1))}…`;
437
+ }
438
+ function errorNote(error) {
439
+ const message = error instanceof Error ? error.message : String(error);
440
+ return `field unavailable: ${message}`;
441
+ }
442
+ //# sourceMappingURL=status-snapshot.js.map
@@ -0,0 +1,63 @@
1
+ /**
2
+ * WORKSPACE probe — verifies `.pugi/` exists, is writable, and is not
3
+ * littered with stale lock files. Optional NDJSON session log presence
4
+ * is reported as additional context but never the basis for a verdict
5
+ * change (it is created on first dispatch, not at init time).
6
+ *
7
+ * The probe owns its fs surface so the spec can run in a tmp sandbox.
8
+ */
9
+ export function probeWorkspace(ctx, fs) {
10
+ const pugiDir = `${ctx.cwd}/.pugi`;
11
+ if (!fs.existsSync(pugiDir)) {
12
+ return {
13
+ name: 'WORKSPACE',
14
+ status: 'warn',
15
+ detail: `.pugi/ not initialised in ${ctx.cwd}`,
16
+ remediation: 'Run `pugi init` to scaffold the workspace',
17
+ };
18
+ }
19
+ let isDir = false;
20
+ try {
21
+ isDir = fs.statSync(pugiDir).isDirectory();
22
+ }
23
+ catch {
24
+ return {
25
+ name: 'WORKSPACE',
26
+ status: 'error',
27
+ detail: `.pugi/ stat failed in ${ctx.cwd}`,
28
+ remediation: 'Re-create the directory: `rm -rf .pugi && pugi init`',
29
+ };
30
+ }
31
+ if (!isDir) {
32
+ return {
33
+ name: 'WORKSPACE',
34
+ status: 'error',
35
+ detail: `${pugiDir} exists but is not a directory`,
36
+ remediation: 'Remove the file at .pugi and run `pugi init`',
37
+ };
38
+ }
39
+ try {
40
+ fs.accessSync(pugiDir, fs.W_OK);
41
+ }
42
+ catch {
43
+ return {
44
+ name: 'WORKSPACE',
45
+ status: 'error',
46
+ detail: `.pugi/ is not writable for the current user`,
47
+ remediation: `chown / chmod the directory so the current user can write it`,
48
+ };
49
+ }
50
+ // Best-effort: report session log presence as detail context. Absence
51
+ // is normal (events.jsonl is created lazily) so it never moves the
52
+ // verdict.
53
+ const eventLogPresent = fs.existsSync(`${pugiDir}/events.jsonl`);
54
+ const detail = eventLogPresent
55
+ ? `.pugi/ writable, events.jsonl present`
56
+ : `.pugi/ writable (events.jsonl created on first dispatch)`;
57
+ return {
58
+ name: 'WORKSPACE',
59
+ status: 'ok',
60
+ detail,
61
+ };
62
+ }
63
+ //# sourceMappingURL=workspace.js.map
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Leak L17 — `pugi doctor` diagnostics types.
3
+ *
4
+ * The doctor command probes the local environment + remote API +
5
+ * workspace state and produces a structured health report. Each probe
6
+ * runs independently; one probe's failure NEVER cascades to another.
7
+ *
8
+ * Status semantics:
9
+ * - `ok` : probe verified the expected state.
10
+ * - `warn` : non-blocking signal (stale CLI, low-but-not-empty disk,
11
+ * missing optional config, etc.). Overall verdict still
12
+ * passes the gate.
13
+ * - `error` : a real problem the operator must fix before Pugi will
14
+ * work end-to-end (auth missing, API unreachable, .pugi/
15
+ * unwritable, Node version below floor, disk full).
16
+ * - `skipped` : prerequisite for the probe is absent (e.g. MCP probe
17
+ * when no mcp.json exists). Does NOT count against the
18
+ * overall verdict.
19
+ *
20
+ * Layered design: this module owns NO I/O. Individual probe files own
21
+ * their I/O surface. The runner orchestrates them in parallel with a
22
+ * timeout + fail-isolation wrapper. The doctor command formats the
23
+ * results for human + JSON consumers.
24
+ */
25
+ /**
26
+ * Helper for the runner to compute the overall verdict from a probe
27
+ * set without leaking the algorithm into the doctor command. Any error
28
+ * → 'error'; any warn (no errors) → 'warning'; otherwise 'healthy'.
29
+ * Skipped probes do NOT influence the verdict.
30
+ */
31
+ export function computeOverall(probes) {
32
+ let hasError = false;
33
+ let hasWarn = false;
34
+ for (const probe of probes) {
35
+ if (probe.status === 'error')
36
+ hasError = true;
37
+ else if (probe.status === 'warn')
38
+ hasWarn = true;
39
+ }
40
+ if (hasError)
41
+ return 'error';
42
+ if (hasWarn)
43
+ return 'warning';
44
+ return 'healthy';
45
+ }
46
+ /**
47
+ * Compute the per-status counts in a single pass so renderers do not
48
+ * have to re-iterate. Surfaces in both the trailer line and the JSON
49
+ * envelope so downstream consumers can render a one-line summary
50
+ * without re-walking the probe array.
51
+ */
52
+ export function countProbes(probes) {
53
+ const counts = { ok: 0, warn: 0, error: 0, skipped: 0 };
54
+ for (const probe of probes)
55
+ counts[probe.status] += 1;
56
+ return counts;
57
+ }
58
+ /**
59
+ * Exit-code map. Exposed for both the CLI handler and the spec so the
60
+ * contract stays in one place.
61
+ * 0 — healthy OR warnings only.
62
+ * 1 — internal crash (unhandled throw in the runner itself).
63
+ * 2 — at least one probe reported `error`.
64
+ */
65
+ export function exitCodeFor(overall) {
66
+ if (overall === 'error')
67
+ return 2;
68
+ return 0;
69
+ }
70
+ //# sourceMappingURL=types.js.map