@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,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,199 @@
1
+ /**
2
+ * `pugi cost` / `pugi usage` command handler — L19 sprint (2026-05-27).
3
+ *
4
+ * Shared backend for three operator surfaces:
5
+ *
6
+ * - `pugi cost` current session (default)
7
+ * - `pugi cost --all-sessions` 30-day rolling aggregate
8
+ * - `pugi cost --reset --yes` wipe current session counter (operator-only)
9
+ * - `pugi usage` alias of `pugi cost`
10
+ * - `/cost` REPL slash same handler, in-REPL output
11
+ * - `/usage` REPL slash same handler, alias of /cost
12
+ *
13
+ * Why a separate command from the existing `pugi budget`:
14
+ *
15
+ * - `pugi budget` walks `.pugi/events.jsonl` and bills against the
16
+ * event-log heuristic (per-command / per-persona attribution). It
17
+ * is the right surface for "what did this brief / this persona
18
+ * spend?". It does not break down by model and it does not persist
19
+ * a cross-session aggregate.
20
+ *
21
+ * - `pugi cost` (this command) reads the persisted `.pugi/cost.json`
22
+ * written by the `CostTracker`. It is the right surface for "what
23
+ * did this model spend?" and "what did I spend across the last 30
24
+ * days?". Token + USD figures are sourced from the rate card, which
25
+ * distinguishes hosted Claude (per-token billed) from open-weight
26
+ * Qwen / Kimi / DeepSeek (infra cost only).
27
+ *
28
+ * Both commands intentionally coexist — they answer adjacent but distinct
29
+ * operator questions. The L19 spec calls out `/cost` and `/usage` by
30
+ * name; the budget surface is unaffected.
31
+ */
32
+ import { existsSync, readFileSync } from 'node:fs';
33
+ import { resolve } from 'node:path';
34
+ import { createCostTracker, totalTokens, totalUsd, } from '../../core/cost/tracker.js';
35
+ import { buildCostView, renderCostTableText } from '../../tui/cost-table.js';
36
+ /**
37
+ * Parsed flag bundle. Exported for the test surface; production callers
38
+ * never touch it directly — `runCostCommand` owns parsing.
39
+ */
40
+ export function parseCostFlags(args) {
41
+ const flags = {
42
+ allSessions: false,
43
+ reset: false,
44
+ yes: false,
45
+ json: false,
46
+ windowDays: 30,
47
+ };
48
+ for (let i = 0; i < args.length; i += 1) {
49
+ const arg = args[i] ?? '';
50
+ if (arg === '--all-sessions')
51
+ flags.allSessions = true;
52
+ else if (arg === '--reset')
53
+ flags.reset = true;
54
+ else if (arg === '--yes' || arg === '-y')
55
+ flags.yes = true;
56
+ else if (arg === '--json')
57
+ flags.json = true;
58
+ else if (arg.startsWith('--window=')) {
59
+ const raw = Number.parseInt(arg.slice('--window='.length), 10);
60
+ if (Number.isFinite(raw) && raw > 0 && raw <= 365)
61
+ flags.windowDays = raw;
62
+ }
63
+ }
64
+ return flags;
65
+ }
66
+ export async function runCostCommand(args, ctx) {
67
+ const flags = parseCostFlags(args);
68
+ const sessionId = ctx.sessionId ?? deriveSessionIdFromEvents(ctx.workspaceRoot) ?? 'no-session';
69
+ const tracker = createCostTracker({
70
+ workspaceRoot: ctx.workspaceRoot,
71
+ sessionIdProvider: () => sessionId,
72
+ now: ctx.now,
73
+ });
74
+ // --reset: clear the current session counter. Operator-only — refuses
75
+ // without `--yes` so a typo / shell completion never wipes the meter.
76
+ if (flags.reset) {
77
+ if (!flags.yes) {
78
+ ctx.writeOutput({ command: 'cost', status: 'reset_pending_confirmation' }, 'pugi cost --reset clears the current session counter. Re-run with --yes to confirm.');
79
+ return;
80
+ }
81
+ const wiped = tracker.resetCurrent();
82
+ const payload = {
83
+ command: 'cost',
84
+ status: 'reset_ok',
85
+ wiped: wiped ?? null,
86
+ };
87
+ ctx.writeOutput(payload, wiped
88
+ ? `Cleared session ${wiped.sessionId} (${Object.keys(wiped.models).length} model(s) wiped).`
89
+ : 'No current session counter to clear.');
90
+ return;
91
+ }
92
+ const aggregate = flags.allSessions ? tracker.aggregateWithin(flags.windowDays) : (tracker.current() ?? emptyAggregate(sessionId, ctx.now ?? Date.now));
93
+ const tier = ctx.resolveTier ? await safeResolveTier(ctx.resolveTier) : null;
94
+ const heading = flags.allSessions
95
+ ? `Pugi cost / usage — aggregate (last ${flags.windowDays} days)`
96
+ : buildSessionHeading(aggregate, ctx.now ?? Date.now);
97
+ const view = buildCostView({ aggregate, heading, tier: tier ?? undefined });
98
+ const text = renderCostTableText(view);
99
+ ctx.writeOutput({
100
+ command: flags.allSessions ? 'cost.aggregate' : 'cost.session',
101
+ status: 'ok',
102
+ window: flags.allSessions ? `${flags.windowDays}d` : 'current',
103
+ tokens: {
104
+ input: view.totalInputTokens,
105
+ output: view.totalOutputTokens,
106
+ },
107
+ dollars: view.totalUsd,
108
+ perModel: view.rows.map((row) => ({
109
+ model: row.model,
110
+ input: row.inputTokens,
111
+ output: row.outputTokens,
112
+ usd: row.usd,
113
+ note: row.note ?? null,
114
+ })),
115
+ tier: tier ?? null,
116
+ }, text);
117
+ }
118
+ /**
119
+ * Render-only helper for the REPL slash. The slash dispatcher inside
120
+ * `session.ts` owns the side-effect of pushing system lines; this
121
+ * function builds the view and the text rendition so the slash handler
122
+ * can fan the lines into the existing `appendSystemLine` queue.
123
+ *
124
+ * Exposed here (not in the Ink module) so the slash path never imports
125
+ * Ink/React — keeps the REPL bundle slim and the slash handler async-free.
126
+ */
127
+ export function renderCostForSlash(input) {
128
+ const aggregate = input.allSessions
129
+ ? input.tracker.aggregateWithin(input.windowDays)
130
+ : (input.tracker.current() ?? emptyAggregate('no-session', input.now));
131
+ const heading = input.allSessions
132
+ ? `Pugi cost / usage — aggregate (last ${input.windowDays} days)`
133
+ : buildSessionHeading(aggregate, input.now);
134
+ const view = buildCostView({ aggregate, heading, tier: input.tier ?? undefined });
135
+ return { view, lines: renderCostTableText(view).split('\n') };
136
+ }
137
+ /**
138
+ * Derive a session id from `.pugi/events.jsonl` when the caller does not
139
+ * pass one. Walks the file once and picks the most recent `session.start`
140
+ * event's id. Falls back to `null` when the file is missing / corrupted
141
+ * — the caller substitutes a `'no-session'` placeholder so the table
142
+ * still renders an empty state instead of crashing.
143
+ */
144
+ function deriveSessionIdFromEvents(workspaceRoot) {
145
+ const path = resolve(workspaceRoot, '.pugi/events.jsonl');
146
+ if (!existsSync(path))
147
+ return null;
148
+ try {
149
+ const raw = readFileSync(path, 'utf8');
150
+ const lines = raw.split('\n').filter((line) => line.trim().length > 0);
151
+ // Walk from newest to oldest — `session.start` is rare, no reason to
152
+ // scan the whole file when the answer is at the tail.
153
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
154
+ try {
155
+ const parsed = JSON.parse(lines[i]);
156
+ if (parsed.type === 'session' && parsed.name === 'start' && typeof parsed.sessionId === 'string') {
157
+ return parsed.sessionId;
158
+ }
159
+ }
160
+ catch {
161
+ // partial-write lines are ignored
162
+ }
163
+ }
164
+ }
165
+ catch {
166
+ // best-effort; absent events.jsonl is a normal first-boot state
167
+ }
168
+ return null;
169
+ }
170
+ function buildSessionHeading(aggregate, now) {
171
+ if (!aggregate || aggregate.sessionId === 'no-session' || aggregate.sessionId === 'aggregate') {
172
+ return 'Pugi cost / usage — no active session';
173
+ }
174
+ const start = Date.parse(aggregate.startedAt);
175
+ if (!Number.isFinite(start)) {
176
+ return `Pugi cost / usage — session ${aggregate.sessionId}`;
177
+ }
178
+ const elapsedMin = Math.max(0, Math.floor((now() - start) / 60_000));
179
+ return `Pugi cost / usage — session ${aggregate.sessionId} (${elapsedMin} min)`;
180
+ }
181
+ function emptyAggregate(sessionId, now) {
182
+ return {
183
+ sessionId,
184
+ startedAt: new Date(now()).toISOString(),
185
+ models: {},
186
+ };
187
+ }
188
+ async function safeResolveTier(resolver) {
189
+ try {
190
+ return await resolver();
191
+ }
192
+ catch {
193
+ return null;
194
+ }
195
+ }
196
+ // Re-export aggregate helpers so the cli.ts wire-up can read totals
197
+ // without reaching into the tracker module directly.
198
+ export { totalUsd, totalTokens };
199
+ //# sourceMappingURL=cost.js.map