@pugi/cli 0.1.0-beta.4 → 0.1.0-beta.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/THIRD_PARTY_NOTICES.md +40 -0
- package/assets/pugi-mascot.ansi +15 -25
- package/bin/run.js +33 -1
- package/dist/commands/jobs-watch.js +201 -0
- package/dist/commands/jobs.js +15 -0
- package/dist/commands/smoke.js +133 -0
- package/dist/core/agent-progress/cleanup.js +134 -0
- package/dist/core/agent-progress/schema.js +144 -0
- package/dist/core/agent-progress/writer.js +101 -0
- package/dist/core/artifact-chain/dispatcher.js +148 -0
- package/dist/core/artifact-chain/exporter.js +164 -0
- package/dist/core/artifact-chain/state.js +243 -0
- package/dist/core/artifact-chain/steps.js +169 -0
- package/dist/core/auth/ensure-authenticated.js +129 -0
- package/dist/core/auth/env-provider.js +238 -0
- package/dist/core/auto-update/channels.js +122 -0
- package/dist/core/auto-update/checker.js +241 -0
- package/dist/core/auto-update/state.js +235 -0
- package/dist/core/bare-mode/index.js +107 -0
- package/dist/core/bash-classifier.js +108 -1
- package/dist/core/checkpoint/resumer.js +149 -0
- package/dist/core/checkpoint/rewinder.js +291 -0
- package/dist/core/codegraph/decision-store.js +248 -0
- package/dist/core/codegraph/detect-repo.js +459 -0
- package/dist/core/codegraph/install.js +134 -0
- package/dist/core/codegraph/offer-hook.js +220 -0
- package/dist/core/compact/auto-trigger.js +96 -0
- package/dist/core/compact/buffer-rewriter.js +115 -0
- package/dist/core/compact/summarizer.js +208 -0
- package/dist/core/compact/token-counter.js +108 -0
- package/dist/core/consensus/diff-capture.js +73 -0
- package/dist/core/context/index.js +7 -0
- package/dist/core/context/markdown-traverse.js +255 -0
- package/dist/core/cost/rate-card.js +129 -0
- package/dist/core/cost/tracker.js +221 -0
- package/dist/core/denial-tracking/index.js +8 -0
- package/dist/core/denial-tracking/state.js +264 -0
- package/dist/core/diagnostics/probe-runner.js +93 -0
- package/dist/core/diagnostics/probes/api.js +46 -0
- package/dist/core/diagnostics/probes/auth.js +86 -0
- package/dist/core/diagnostics/probes/bare-mode.js +42 -0
- package/dist/core/diagnostics/probes/cli-version.js +127 -0
- package/dist/core/diagnostics/probes/config.js +72 -0
- package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
- package/dist/core/diagnostics/probes/disk.js +81 -0
- package/dist/core/diagnostics/probes/git.js +65 -0
- package/dist/core/diagnostics/probes/mcp.js +75 -0
- package/dist/core/diagnostics/probes/node.js +59 -0
- package/dist/core/diagnostics/probes/pnpm.js +36 -0
- package/dist/core/diagnostics/probes/pugi-md.js +89 -0
- package/dist/core/diagnostics/probes/session.js +74 -0
- package/dist/core/diagnostics/probes/status-snapshot.js +488 -0
- package/dist/core/diagnostics/probes/workspace.js +63 -0
- package/dist/core/diagnostics/types.js +70 -0
- package/dist/core/dispatch/cache-cleanup.js +197 -0
- package/dist/core/dispatch/cache-handoff.js +295 -0
- package/dist/core/edits/dispatch.js +218 -2
- package/dist/core/edits/journal.js +199 -0
- package/dist/core/edits/layer-d-ast.js +557 -14
- package/dist/core/edits/verify-hook.js +273 -0
- package/dist/core/edits/worktree.js +322 -0
- package/dist/core/engine/anvil-client.js +115 -5
- package/dist/core/engine/budgets.js +98 -0
- package/dist/core/engine/context-prefix.js +155 -0
- package/dist/core/engine/intent.js +260 -0
- package/dist/core/engine/native-pugi.js +860 -211
- package/dist/core/engine/prompts.js +88 -2
- package/dist/core/engine/strip-internal-fields.js +124 -0
- package/dist/core/engine/tool-bridge.js +1045 -36
- package/dist/core/feedback/queue.js +177 -0
- package/dist/core/feedback/submitter.js +145 -0
- package/dist/core/file-cache.js +113 -1
- package/dist/core/hooks/events.js +44 -0
- package/dist/core/hooks/index.js +15 -0
- package/dist/core/hooks/registry.js +213 -0
- package/dist/core/hooks/runner.js +236 -0
- package/dist/core/hooks/v2/event-emitter.js +115 -0
- package/dist/core/hooks/v2/executor.js +282 -0
- package/dist/core/hooks/v2/index.js +25 -0
- package/dist/core/hooks/v2/lifecycle.js +104 -0
- package/dist/core/hooks/v2/loader.js +216 -0
- package/dist/core/hooks/v2/matcher.js +125 -0
- package/dist/core/hooks/v2/trust.js +143 -0
- package/dist/core/hooks/v2/types.js +86 -0
- package/dist/core/lsp/cache.js +105 -0
- package/dist/core/lsp/client.js +776 -0
- package/dist/core/lsp/language-detect.js +66 -0
- package/dist/core/lsp/post-edit-diagnostics.js +171 -0
- package/dist/core/mcp/client.js +75 -6
- package/dist/core/mcp/http-server.js +553 -0
- package/dist/core/mcp/orchestrator-tools.js +662 -0
- package/dist/core/mcp/permission.js +190 -0
- package/dist/core/mcp/registry.js +24 -2
- package/dist/core/mcp/server-tools.js +219 -0
- package/dist/core/mcp/server.js +397 -0
- package/dist/core/memory/dual-write.js +416 -0
- package/dist/core/memory/phase1-kinds.js +20 -0
- package/dist/core/memory-sync/queue.js +158 -0
- package/dist/core/onboarding/ensure-initialized.js +133 -0
- package/dist/core/onboarding/marker.js +111 -0
- package/dist/core/onboarding/telemetry-state.js +108 -0
- package/dist/core/output-style/presets.js +176 -0
- package/dist/core/output-style/state.js +185 -0
- package/dist/core/permissions/auto-classifier.js +124 -0
- package/dist/core/permissions/circuit-breaker.js +83 -0
- package/dist/core/permissions/gate.js +278 -0
- package/dist/core/permissions/index.js +20 -0
- package/dist/core/permissions/mode.js +174 -0
- package/dist/core/permissions/state.js +241 -0
- package/dist/core/permissions/tool-class.js +93 -0
- package/dist/core/prd-check/parser.js +215 -0
- package/dist/core/prd-check/reporter.js +127 -0
- package/dist/core/prd-check/session-review.js +557 -0
- package/dist/core/prd-check/verifiers.js +223 -0
- package/dist/core/pugi-md/context-injector.js +76 -0
- package/dist/core/pugi-md/walk-up.js +207 -0
- package/dist/core/release-notes/parser.js +241 -0
- package/dist/core/release-notes/state.js +116 -0
- package/dist/core/repl/history.js +11 -1
- package/dist/core/repl/model-pricing.js +135 -0
- package/dist/core/repl/session.js +1899 -38
- package/dist/core/repl/slash-commands.js +406 -21
- package/dist/core/repl/store/session-store.js +31 -2
- package/dist/core/repl/workspace-context.js +22 -0
- package/dist/core/repo-map/build.js +125 -0
- package/dist/core/repo-map/cache.js +185 -0
- package/dist/core/repo-map/extractor.js +254 -0
- package/dist/core/repo-map/formatter.js +145 -0
- package/dist/core/repo-map/scanner.js +211 -0
- package/dist/core/retry-budget/budget.js +284 -0
- package/dist/core/retry-budget/index.js +5 -0
- package/dist/core/session.js +92 -0
- package/dist/core/settings.js +80 -0
- package/dist/core/share/formatter.js +271 -0
- package/dist/core/share/redactor.js +221 -0
- package/dist/core/share/uploader.js +267 -0
- package/dist/core/skills/defaults.js +457 -0
- package/dist/core/smoke/headless-driver.js +174 -0
- package/dist/core/smoke/orchestrator.js +194 -0
- package/dist/core/smoke/runner.js +238 -0
- package/dist/core/smoke/scenario-parser.js +316 -0
- package/dist/core/subagents/dispatcher-real.js +600 -0
- package/dist/core/subagents/dispatcher.js +113 -24
- package/dist/core/subagents/index.js +18 -5
- package/dist/core/subagents/isolation-matrix.js +213 -0
- package/dist/core/subagents/spawn.js +19 -4
- package/dist/core/telemetry/emitter.js +229 -0
- package/dist/core/telemetry/queue.js +251 -0
- package/dist/core/theme/context.js +91 -0
- package/dist/core/theme/presets.js +228 -0
- package/dist/core/theme/state.js +181 -0
- package/dist/core/todos/invariant.js +10 -0
- package/dist/core/todos/state.js +177 -0
- package/dist/core/transport/version-interceptor.js +166 -0
- package/dist/core/vim/keymap.js +288 -0
- package/dist/core/vim/state.js +92 -0
- package/dist/index.js +28 -0
- package/dist/runtime/bootstrap.js +190 -0
- package/dist/runtime/cli.js +3073 -321
- package/dist/runtime/commands/cancel.js +231 -0
- package/dist/runtime/commands/chain.js +489 -0
- package/dist/runtime/commands/codegraph-status.js +227 -0
- package/dist/runtime/commands/compact.js +297 -0
- package/dist/runtime/commands/cost.js +199 -0
- package/dist/runtime/commands/delegate.js +242 -11
- package/dist/runtime/commands/dispatch.js +126 -0
- package/dist/runtime/commands/doctor.js +390 -0
- package/dist/runtime/commands/feedback.js +184 -0
- package/dist/runtime/commands/hooks.js +184 -0
- package/dist/runtime/commands/lsp.js +368 -0
- package/dist/runtime/commands/mcp.js +879 -0
- package/dist/runtime/commands/memory.js +508 -0
- package/dist/runtime/commands/model.js +237 -0
- package/dist/runtime/commands/onboarding.js +275 -0
- package/dist/runtime/commands/patch.js +128 -0
- package/dist/runtime/commands/permissions.js +112 -0
- package/dist/runtime/commands/plan.js +143 -0
- package/dist/runtime/commands/prd-check.js +285 -0
- package/dist/runtime/commands/redo-blob-store.js +92 -0
- package/dist/runtime/commands/redo.js +361 -0
- package/dist/runtime/commands/release-notes.js +229 -0
- package/dist/runtime/commands/repo-map.js +95 -0
- package/dist/runtime/commands/report.js +299 -0
- package/dist/runtime/commands/resume.js +118 -0
- package/dist/runtime/commands/review-consensus.js +17 -2
- package/dist/runtime/commands/rewind.js +333 -0
- package/dist/runtime/commands/sessions.js +163 -0
- package/dist/runtime/commands/share.js +316 -0
- package/dist/runtime/commands/status.js +186 -0
- package/dist/runtime/commands/stickers.js +82 -0
- package/dist/runtime/commands/style.js +194 -0
- package/dist/runtime/commands/theme.js +196 -0
- package/dist/runtime/commands/undo.js +32 -0
- package/dist/runtime/commands/update.js +289 -0
- package/dist/runtime/commands/vim.js +140 -0
- package/dist/runtime/commands/worktree.js +177 -0
- package/dist/runtime/headless-repl.js +195 -0
- package/dist/runtime/headless.js +543 -0
- package/dist/runtime/load-hooks-or-exit.js +71 -0
- package/dist/runtime/plan-decompose.js +531 -0
- package/dist/runtime/version.js +65 -0
- package/dist/tools/agent-tool.js +229 -0
- package/dist/tools/apply-patch.js +556 -0
- package/dist/tools/ask-user-question.js +213 -0
- package/dist/tools/ask-user.js +115 -0
- package/dist/tools/file-tools.js +85 -14
- package/dist/tools/lsp-tools.js +189 -0
- package/dist/tools/mcp-tool.js +260 -0
- package/dist/tools/multi-edit.js +361 -0
- package/dist/tools/powershell.js +156 -0
- package/dist/tools/registry.js +51 -0
- package/dist/tools/skill-tool.js +96 -0
- package/dist/tools/tasks.js +208 -0
- package/dist/tools/todo-write.js +184 -0
- package/dist/tools/web-fetch.js +147 -2
- package/dist/tools/web-search.js +458 -0
- package/dist/tui/agent-progress-card.js +111 -0
- package/dist/tui/agent-tree.js +10 -0
- package/dist/tui/ask-modal.js +2 -2
- package/dist/tui/ask-user-question-prompt.js +192 -0
- package/dist/tui/compact-banner.js +81 -0
- package/dist/tui/conversation-pane.js +82 -8
- package/dist/tui/cost-table.js +111 -0
- package/dist/tui/doctor-table.js +46 -0
- package/dist/tui/feedback-prompt.js +156 -0
- package/dist/tui/input-box.js +69 -2
- package/dist/tui/markdown-render.js +4 -4
- package/dist/tui/onboarding-wizard.js +240 -0
- package/dist/tui/permissions-picker.js +86 -0
- package/dist/tui/render.js +35 -0
- package/dist/tui/repl-render.js +303 -13
- package/dist/tui/repl-splash.js +2 -2
- package/dist/tui/repl.js +72 -14
- package/dist/tui/splash.js +1 -1
- package/dist/tui/status-bar.js +94 -16
- package/dist/tui/status-table.js +7 -0
- package/dist/tui/stickers-art.js +136 -0
- package/dist/tui/style-table.js +28 -0
- package/dist/tui/theme-table.js +29 -0
- package/dist/tui/tool-stream-pane.js +52 -3
- package/dist/tui/update-banner.js +20 -2
- package/dist/tui/vim-input.js +267 -0
- package/docs/examples/codegraph.mcp.json +10 -0
- package/package.json +12 -6
- package/test/scenarios/codegen-create-file.scenario.txt +13 -0
- package/test/scenarios/compact-force.scenario.txt +11 -0
- package/test/scenarios/identity.scenario.txt +11 -0
- package/test/scenarios/persona-handoff.scenario.txt +11 -0
- package/test/scenarios/walkback.scenario.txt +12 -0
- package/dist/core/engine/compaction-hook.js +0 -154
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard: discriminate a SessionEvent against the `rewind-marker`
|
|
3
|
+
* kind. The boundary check is strict — a malformed payload returns
|
|
4
|
+
* `false` so the replay layer can treat it as a regular (visible) event
|
|
5
|
+
* instead of trusting partial data.
|
|
6
|
+
*/
|
|
7
|
+
export function isRewindMarker(event) {
|
|
8
|
+
if (event.kind !== 'rewind-marker')
|
|
9
|
+
return false;
|
|
10
|
+
const p = event.payload;
|
|
11
|
+
if (p === null || typeof p !== 'object')
|
|
12
|
+
return false;
|
|
13
|
+
if (p.version !== 1)
|
|
14
|
+
return false;
|
|
15
|
+
if (p.mode !== 'rewind' && p.mode !== 'undo-rewind')
|
|
16
|
+
return false;
|
|
17
|
+
if (typeof p.toEventIndex !== 'number')
|
|
18
|
+
return false;
|
|
19
|
+
if (typeof p.fromEventIndex !== 'number')
|
|
20
|
+
return false;
|
|
21
|
+
if (typeof p.turnsRewound !== 'number')
|
|
22
|
+
return false;
|
|
23
|
+
if (p.reason !== 'manual'
|
|
24
|
+
&& p.reason !== 'to-event'
|
|
25
|
+
&& p.reason !== 'interactive'
|
|
26
|
+
&& p.reason !== 'undo') {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Append one `rewind-marker` event to the SessionStore. Returns the
|
|
33
|
+
* SessionEvent we wrote so the caller can echo it into the in-memory
|
|
34
|
+
* transcript without a re-read. Throws on store error so the caller
|
|
35
|
+
* surfaces the failure inline.
|
|
36
|
+
*/
|
|
37
|
+
export async function appendRewindMarker(input) {
|
|
38
|
+
const ts = (input.now ?? (() => Date.now()))();
|
|
39
|
+
const payload = {
|
|
40
|
+
version: 1,
|
|
41
|
+
mode: input.mode ?? 'rewind',
|
|
42
|
+
toEventIndex: input.toEventIndex,
|
|
43
|
+
fromEventIndex: input.fromEventIndex,
|
|
44
|
+
turnsRewound: input.turnsRewound,
|
|
45
|
+
reason: input.reason,
|
|
46
|
+
};
|
|
47
|
+
const event = {
|
|
48
|
+
t: ts,
|
|
49
|
+
kind: 'rewind-marker',
|
|
50
|
+
payload,
|
|
51
|
+
};
|
|
52
|
+
await input.store.appendEvent(event);
|
|
53
|
+
return event;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Apply rewind masking to a chronological event list. Pure function —
|
|
57
|
+
* returns a new array with the masked events stripped. The marker
|
|
58
|
+
* events themselves are stripped too (they are infrastructure, not
|
|
59
|
+
* conversation rows); the renderer surfaces the rewind banner via a
|
|
60
|
+
* separate informational payload, NOT by leaving the marker in the
|
|
61
|
+
* transcript.
|
|
62
|
+
*
|
|
63
|
+
* Algorithm (matched-pair cancellation):
|
|
64
|
+
*
|
|
65
|
+
* 1. Walk events newest-to-oldest. Maintain an integer `undoBalance`
|
|
66
|
+
* that counts the unmatched 'undo-rewind' markers we have seen.
|
|
67
|
+
* 2. When we hit an 'undo-rewind' marker, increment `undoBalance` —
|
|
68
|
+
* it will cancel the next older 'rewind' marker.
|
|
69
|
+
* 3. When we hit a 'rewind' marker:
|
|
70
|
+
* - If `undoBalance > 0`: decrement (the undo cancels this
|
|
71
|
+
* rewind). The marker AND the events in its masked range stay
|
|
72
|
+
* visible — the undo restored them.
|
|
73
|
+
* - Otherwise: record the marker's `[toEventIndex+1 ..
|
|
74
|
+
* fromEventIndex]` range as masked.
|
|
75
|
+
* 4. After the walk, return every event whose index is NOT inside an
|
|
76
|
+
* active masked range AND that is not itself a rewind-marker.
|
|
77
|
+
*
|
|
78
|
+
* Why not just look at the latest marker:
|
|
79
|
+
* The operator can rewind, then rewind again, then undo-rewind once.
|
|
80
|
+
* The newest rewind should still apply; only the innermost rewind is
|
|
81
|
+
* cancelled by the undo. Matched-pair walking handles every
|
|
82
|
+
* stack-depth without special-casing.
|
|
83
|
+
*/
|
|
84
|
+
export function applyRewindMask(events) {
|
|
85
|
+
// Pass 1: walk newest-to-oldest, collect active masked ranges.
|
|
86
|
+
let undoBalance = 0;
|
|
87
|
+
// Each range is half-open [start, end] inclusive on both ends so the
|
|
88
|
+
// membership check below stays a simple two-integer comparison.
|
|
89
|
+
const maskedRanges = [];
|
|
90
|
+
for (let i = events.length - 1; i >= 0; i -= 1) {
|
|
91
|
+
const ev = events[i];
|
|
92
|
+
if (!isRewindMarker(ev))
|
|
93
|
+
continue;
|
|
94
|
+
if (ev.payload.mode === 'undo-rewind') {
|
|
95
|
+
undoBalance += 1;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
// mode === 'rewind'
|
|
99
|
+
if (undoBalance > 0) {
|
|
100
|
+
undoBalance -= 1;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
// Active rewind — mask everything strictly AFTER toEventIndex and
|
|
104
|
+
// strictly BEFORE the marker itself. The marker (index === i) is
|
|
105
|
+
// dropped via the rewind-marker kind filter below.
|
|
106
|
+
const start = ev.payload.toEventIndex + 1;
|
|
107
|
+
const end = i - 1;
|
|
108
|
+
if (end >= start) {
|
|
109
|
+
maskedRanges.push({ start, end });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Pass 2: emit only events that are NOT inside any masked range and
|
|
113
|
+
// are NOT themselves rewind-markers. Linear scan with a sorted-ranges
|
|
114
|
+
// membership check would be faster for very large logs, but the
|
|
115
|
+
// typical transcript is in the hundreds of events — O(N*M) here with
|
|
116
|
+
// M = number of rewinds ever appended stays well under a millisecond.
|
|
117
|
+
const out = [];
|
|
118
|
+
for (let i = 0; i < events.length; i += 1) {
|
|
119
|
+
const ev = events[i];
|
|
120
|
+
if (isRewindMarker(ev))
|
|
121
|
+
continue;
|
|
122
|
+
let masked = false;
|
|
123
|
+
for (const range of maskedRanges) {
|
|
124
|
+
if (i >= range.start && i <= range.end) {
|
|
125
|
+
masked = true;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (!masked)
|
|
130
|
+
out.push(ev);
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Walk events oldest-to-newest and return the indices of the last `n`
|
|
136
|
+
* operator turns (`kind === 'user'`) — used by `/rewind N` to translate
|
|
137
|
+
* "drop the last 3 turns" into a concrete `toEventIndex`.
|
|
138
|
+
*
|
|
139
|
+
* Visibility is computed via `applyRewindMask`-aware indexing: the
|
|
140
|
+
* caller supplies the FULL event list (incl. existing markers) and we
|
|
141
|
+
* walk only the visible subset so a follow-up rewind on top of an
|
|
142
|
+
* existing rewind operates on what the operator currently SEES, not the
|
|
143
|
+
* full on-disk history.
|
|
144
|
+
*
|
|
145
|
+
* Returns the index of the event that should become the new
|
|
146
|
+
* `toEventIndex` — i.e. the event *immediately before* the Nth turn
|
|
147
|
+
* boundary, counting from the end. When `n` exceeds the visible turn
|
|
148
|
+
* count, returns `-1` (rewind everything).
|
|
149
|
+
*/
|
|
150
|
+
export function pickRewindTargetForTurns(events, turnsToDrop) {
|
|
151
|
+
if (turnsToDrop <= 0) {
|
|
152
|
+
// No-op — return the last index unchanged so the caller can detect
|
|
153
|
+
// the noop and emit a sensible message.
|
|
154
|
+
return { toEventIndex: events.length - 1, turnsRewound: 0 };
|
|
155
|
+
}
|
|
156
|
+
const visible = applyRewindMask(events);
|
|
157
|
+
// Walk visible newest-to-oldest, count user turns.
|
|
158
|
+
const userTurnVisibleIndices = [];
|
|
159
|
+
for (let i = visible.length - 1; i >= 0; i -= 1) {
|
|
160
|
+
if (visible[i].kind === 'user')
|
|
161
|
+
userTurnVisibleIndices.push(i);
|
|
162
|
+
if (userTurnVisibleIndices.length >= turnsToDrop + 1)
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
const turnsAvailable = userTurnVisibleIndices.length;
|
|
166
|
+
if (turnsAvailable < turnsToDrop) {
|
|
167
|
+
// Fewer turns than asked — rewind everything visible.
|
|
168
|
+
return { toEventIndex: -1, turnsRewound: turnsAvailable };
|
|
169
|
+
}
|
|
170
|
+
if (turnsAvailable === turnsToDrop) {
|
|
171
|
+
// Drop ALL visible turns — anchor at -1 inside the visible list
|
|
172
|
+
// (i.e. before the first visible event). Translate back to an
|
|
173
|
+
// index in the full event list by picking the position right
|
|
174
|
+
// before the oldest visible event.
|
|
175
|
+
const oldestVisibleIdx = visible.length > 0 ? indexInFull(events, visible[0]) : -1;
|
|
176
|
+
return { toEventIndex: oldestVisibleIdx - 1, turnsRewound: turnsToDrop };
|
|
177
|
+
}
|
|
178
|
+
// `userTurnVisibleIndices` collected user turns newest-first. The
|
|
179
|
+
// OLDEST turn the operator wants to drop sits at index
|
|
180
|
+
// `turnsToDrop - 1` of that array (the Nth most recent). The new
|
|
181
|
+
// `toEventIndex` is the visible event RIGHT BEFORE that turn — that
|
|
182
|
+
// becomes the last visible row after the rewind. The N most-recent
|
|
183
|
+
// turns + everything between them get masked.
|
|
184
|
+
const cutVisibleIdx = userTurnVisibleIndices[turnsToDrop - 1];
|
|
185
|
+
// Pick the event one slot before the cut as the new toEventIndex.
|
|
186
|
+
const anchorVisibleIdx = cutVisibleIdx - 1;
|
|
187
|
+
if (anchorVisibleIdx < 0) {
|
|
188
|
+
return { toEventIndex: -1, turnsRewound: turnsToDrop };
|
|
189
|
+
}
|
|
190
|
+
const anchorEvent = visible[anchorVisibleIdx];
|
|
191
|
+
const toEventIndex = indexInFull(events, anchorEvent);
|
|
192
|
+
return { toEventIndex, turnsRewound: turnsToDrop };
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Resolve a `--to <event-id>` argument into a concrete event index in
|
|
196
|
+
* the on-disk log. The L8 / L9 wire format does NOT mint an `id` field
|
|
197
|
+
* on individual events; the operator picks by the (1-based) line number
|
|
198
|
+
* we surface in `applyRewindMask`-aware listings. So `<event-id>` here
|
|
199
|
+
* is `"<n>"` where n is the 1-based visible index. We accept both
|
|
200
|
+
* 1-based (UI-facing) and 0-based (programmatic / tests) by checking
|
|
201
|
+
* for a leading `#`.
|
|
202
|
+
*
|
|
203
|
+
* Returns the matching index in the full event list, or null when the
|
|
204
|
+
* input is unparseable / out of range.
|
|
205
|
+
*/
|
|
206
|
+
export function resolveEventIdToIndex(events, eventId) {
|
|
207
|
+
const trimmed = eventId.trim();
|
|
208
|
+
if (trimmed.length === 0)
|
|
209
|
+
return null;
|
|
210
|
+
const zeroBased = trimmed.startsWith('#');
|
|
211
|
+
const raw = zeroBased ? trimmed.slice(1) : trimmed;
|
|
212
|
+
const parsed = Number.parseInt(raw, 10);
|
|
213
|
+
if (!Number.isFinite(parsed) || parsed < 0)
|
|
214
|
+
return null;
|
|
215
|
+
const visible = applyRewindMask(events);
|
|
216
|
+
const visibleIdx = zeroBased ? parsed : parsed - 1;
|
|
217
|
+
if (visibleIdx < 0 || visibleIdx >= visible.length)
|
|
218
|
+
return null;
|
|
219
|
+
return indexInFull(events, visible[visibleIdx]);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Locate the index of `target` inside `events` by identity-then-equality.
|
|
223
|
+
* Identity hits in O(1); the equality fallback walks the array and
|
|
224
|
+
* compares the (t, kind) discriminator, which is unique-enough for the
|
|
225
|
+
* append-only log where two events at the same millisecond with the
|
|
226
|
+
* same kind would also have identical payloads.
|
|
227
|
+
*/
|
|
228
|
+
function indexInFull(events, target) {
|
|
229
|
+
// Identity check first — `applyRewindMask` returns elements from the
|
|
230
|
+
// input array directly, so `===` succeeds in the common path.
|
|
231
|
+
for (let i = 0; i < events.length; i += 1) {
|
|
232
|
+
if (events[i] === target)
|
|
233
|
+
return i;
|
|
234
|
+
}
|
|
235
|
+
// Fallback: compare by t + kind + payload reference. Unlikely path.
|
|
236
|
+
for (let i = 0; i < events.length; i += 1) {
|
|
237
|
+
const ev = events[i];
|
|
238
|
+
if (ev.t === target.t && ev.kind === target.kind && ev.payload === target.payload) {
|
|
239
|
+
return i;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return -1;
|
|
243
|
+
}
|
|
244
|
+
export function buildRewindPickerRows(events, limit = 10) {
|
|
245
|
+
const visible = applyRewindMask(events);
|
|
246
|
+
const rows = [];
|
|
247
|
+
let turnsAgo = 0;
|
|
248
|
+
for (let i = visible.length - 1; i >= 0 && rows.length < limit; i -= 1) {
|
|
249
|
+
const ev = visible[i];
|
|
250
|
+
if (ev.kind !== 'user')
|
|
251
|
+
continue;
|
|
252
|
+
turnsAgo += 1;
|
|
253
|
+
const payload = ev.payload;
|
|
254
|
+
const preview = typeof payload?.brief === 'string'
|
|
255
|
+
? payload.brief.slice(0, 64)
|
|
256
|
+
: '(empty turn)';
|
|
257
|
+
rows.push({
|
|
258
|
+
eventIndex: indexInFull(events, ev),
|
|
259
|
+
visibleIndex: i + 1,
|
|
260
|
+
preview,
|
|
261
|
+
turnsAgo,
|
|
262
|
+
timestampEpochMs: ev.t,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
return rows;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Find the latest active 'rewind' marker — i.e. the one that an
|
|
269
|
+
* `undo-rewind` would cancel. Walks newest-to-oldest, balancing
|
|
270
|
+
* 'undo-rewind' markers against 'rewind' markers; returns the first
|
|
271
|
+
* unmatched 'rewind' or null when every rewind has already been undone.
|
|
272
|
+
*/
|
|
273
|
+
export function findLatestActiveRewind(events) {
|
|
274
|
+
let undoBalance = 0;
|
|
275
|
+
for (let i = events.length - 1; i >= 0; i -= 1) {
|
|
276
|
+
const ev = events[i];
|
|
277
|
+
if (!isRewindMarker(ev))
|
|
278
|
+
continue;
|
|
279
|
+
if (ev.payload.mode === 'undo-rewind') {
|
|
280
|
+
undoBalance += 1;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (undoBalance > 0) {
|
|
284
|
+
undoBalance -= 1;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
return { event: ev, payload: ev.payload, index: i };
|
|
288
|
+
}
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
//# sourceMappingURL=rewinder.js.map
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codegraph install-decision store — Wave 6 BIG TRACK 9 Phase 2.
|
|
3
|
+
*
|
|
4
|
+
* Persists the operator's verdict on the codegraph install prompt so we
|
|
5
|
+
* never spam them after a single decline. The 30-day reminder cadence
|
|
6
|
+
* lets us re-surface the offer on big-enough repos in case the operator
|
|
7
|
+
* said "not now" the first time and then forgot codegraph exists.
|
|
8
|
+
*
|
|
9
|
+
* Schema (workspace-scoped at `.pugi/codegraph-decision.json`):
|
|
10
|
+
*
|
|
11
|
+
* {
|
|
12
|
+
* "schema": 1,
|
|
13
|
+
* "offeredAt": "2026-05-27T00:00:00.000Z",
|
|
14
|
+
* "accepted": false,
|
|
15
|
+
* "decliningCount": 1,
|
|
16
|
+
* "remindAfter": "2026-06-26T00:00:00.000Z", // 30 days from offeredAt
|
|
17
|
+
* "lastIndexedAt": null, // ISO date string OR null
|
|
18
|
+
* "lastReindexCheckAt": null
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* The store is workspace-local (each repo gets its own decision) so
|
|
22
|
+
* declining codegraph in repo A does not suppress the prompt in repo B.
|
|
23
|
+
* `.pugi/` already exists by the time we land here (pugi init scaffolds
|
|
24
|
+
* it), so the directory creation is best-effort defence-in-depth.
|
|
25
|
+
*
|
|
26
|
+
* Concurrency: every write is `tmp + rename` so a partial write cannot
|
|
27
|
+
* surface a corrupt JSON. Reads tolerate missing files + corrupt JSON
|
|
28
|
+
* by returning `null` — the caller decides whether to fall back to
|
|
29
|
+
* "offer again" (safe default) or "do nothing" (cold-start path).
|
|
30
|
+
*
|
|
31
|
+
* Pure persistence. No telemetry, no logging. The emitter lives in the
|
|
32
|
+
* call sites so the decision store stays unit-testable in isolation.
|
|
33
|
+
*/
|
|
34
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync, } from 'node:fs';
|
|
35
|
+
import { resolve } from 'node:path';
|
|
36
|
+
/**
|
|
37
|
+
* Reminder cadence — 30 days from the last decline. Operators who said
|
|
38
|
+
* no in a small repo that grew к medium during a sprint deserve a
|
|
39
|
+
* follow-up; operators who said no last week do not. The window is
|
|
40
|
+
* exposed as a const so the spec can pin it.
|
|
41
|
+
*/
|
|
42
|
+
export const REMIND_AFTER_DAYS = 30;
|
|
43
|
+
/**
|
|
44
|
+
* Stale-index threshold for the cold-start "refresh me" reminder.
|
|
45
|
+
* Seven days is the cadence the upstream codegraph docs recommend for
|
|
46
|
+
* monorepos that ship multiple times a day; lower repos can wait
|
|
47
|
+
* longer. The spec pins it.
|
|
48
|
+
*/
|
|
49
|
+
export const STALE_INDEX_DAYS = 7;
|
|
50
|
+
/**
|
|
51
|
+
* Resolve the decision file path for a workspace root. Pure — exposed
|
|
52
|
+
* для spec parity.
|
|
53
|
+
*/
|
|
54
|
+
export function decisionPath(workspaceRoot) {
|
|
55
|
+
return resolve(workspaceRoot, '.pugi/codegraph-decision.json');
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Read the persisted decision. Returns null on missing file, malformed
|
|
59
|
+
* JSON, or wrong schema version. The caller MUST treat null as "no
|
|
60
|
+
* decision yet" — not "operator declined".
|
|
61
|
+
*/
|
|
62
|
+
export function readDecision(workspaceRoot) {
|
|
63
|
+
const path = decisionPath(workspaceRoot);
|
|
64
|
+
if (!existsSync(path))
|
|
65
|
+
return null;
|
|
66
|
+
let raw;
|
|
67
|
+
try {
|
|
68
|
+
raw = readFileSync(path, 'utf8');
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
let parsed;
|
|
74
|
+
try {
|
|
75
|
+
parsed = JSON.parse(raw);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
if (!isDecisionShape(parsed))
|
|
81
|
+
return null;
|
|
82
|
+
return parsed;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Type guard. Defensive — a future schema bump should land a migration
|
|
86
|
+
* here. For now: schema MUST be 1; required string fields MUST be
|
|
87
|
+
* strings; optional fields may be null OR string.
|
|
88
|
+
*/
|
|
89
|
+
function isDecisionShape(value) {
|
|
90
|
+
if (!value || typeof value !== 'object')
|
|
91
|
+
return false;
|
|
92
|
+
const v = value;
|
|
93
|
+
if (v.schema !== 1)
|
|
94
|
+
return false;
|
|
95
|
+
if (typeof v.offeredAt !== 'string')
|
|
96
|
+
return false;
|
|
97
|
+
if (typeof v.accepted !== 'boolean')
|
|
98
|
+
return false;
|
|
99
|
+
if (typeof v.decliningCount !== 'number' || !Number.isFinite(v.decliningCount))
|
|
100
|
+
return false;
|
|
101
|
+
if (typeof v.remindAfter !== 'string')
|
|
102
|
+
return false;
|
|
103
|
+
if (v.lastIndexedAt !== null && typeof v.lastIndexedAt !== 'string')
|
|
104
|
+
return false;
|
|
105
|
+
if (v.lastReindexCheckAt !== null && typeof v.lastReindexCheckAt !== 'string')
|
|
106
|
+
return false;
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Atomic write. Creates `.pugi/` if it does not exist (pugi init owns
|
|
111
|
+
* that surface ordinarily; we defend the rare cold-start path).
|
|
112
|
+
*/
|
|
113
|
+
export function writeDecision(workspaceRoot, decision) {
|
|
114
|
+
const path = decisionPath(workspaceRoot);
|
|
115
|
+
const dir = resolve(workspaceRoot, '.pugi');
|
|
116
|
+
if (!existsSync(dir)) {
|
|
117
|
+
mkdirSync(dir, { recursive: true });
|
|
118
|
+
}
|
|
119
|
+
const tmp = `${path}.tmp.${process.pid}`;
|
|
120
|
+
writeFileSync(tmp, `${JSON.stringify(decision, null, 2)}\n`, { mode: 0o600 });
|
|
121
|
+
try {
|
|
122
|
+
renameSync(tmp, path);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
// Rename can fail if the destination was concurrently swapped on
|
|
126
|
+
// some platforms (Windows). Fall back to unlink + rename so the
|
|
127
|
+
// best-effort write does not throw to the caller.
|
|
128
|
+
try {
|
|
129
|
+
unlinkSync(path);
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
// ignore
|
|
133
|
+
}
|
|
134
|
+
renameSync(tmp, path);
|
|
135
|
+
void error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Decide whether to surface the install prompt on init. Returns the
|
|
140
|
+
* full decision shape for callers that want to inspect the cadence;
|
|
141
|
+
* a `true` verdict means "yes, ask the operator now".
|
|
142
|
+
*/
|
|
143
|
+
export function shouldOfferOnInit(workspaceRoot, nowIso = new Date().toISOString()) {
|
|
144
|
+
const prior = readDecision(workspaceRoot);
|
|
145
|
+
if (!prior) {
|
|
146
|
+
return { shouldOffer: true, reason: 'first-run' };
|
|
147
|
+
}
|
|
148
|
+
if (prior.accepted) {
|
|
149
|
+
return { shouldOffer: false, reason: 'accepted-already' };
|
|
150
|
+
}
|
|
151
|
+
if (Date.parse(nowIso) >= Date.parse(prior.remindAfter)) {
|
|
152
|
+
return { shouldOffer: true, reason: 'reminder-due' };
|
|
153
|
+
}
|
|
154
|
+
return { shouldOffer: false, reason: 'recent-decline' };
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Record the operator's decision atomically. Mirrors the structure on
|
|
158
|
+
* disk — callers do NOT hand-craft the schema.
|
|
159
|
+
*/
|
|
160
|
+
export function recordDecision(workspaceRoot, input) {
|
|
161
|
+
const nowIso = input.nowIso ?? new Date().toISOString();
|
|
162
|
+
const prior = readDecision(workspaceRoot);
|
|
163
|
+
const decliningCount = input.accepted ? 0 : (prior?.decliningCount ?? 0) + 1;
|
|
164
|
+
const remindAfter = new Date(Date.parse(nowIso) + REMIND_AFTER_DAYS * 24 * 60 * 60 * 1000).toISOString();
|
|
165
|
+
const decision = {
|
|
166
|
+
schema: 1,
|
|
167
|
+
offeredAt: nowIso,
|
|
168
|
+
accepted: input.accepted,
|
|
169
|
+
decliningCount,
|
|
170
|
+
remindAfter,
|
|
171
|
+
lastIndexedAt: prior?.lastIndexedAt ?? null,
|
|
172
|
+
lastReindexCheckAt: prior?.lastReindexCheckAt ?? null,
|
|
173
|
+
};
|
|
174
|
+
writeDecision(workspaceRoot, decision);
|
|
175
|
+
return decision;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Stamp the last-indexed timestamp. Called by /codegraph-status when
|
|
179
|
+
* the operator triggers a reindex from inside Pugi. Updates the
|
|
180
|
+
* `accepted` decision in place — never flips the install state.
|
|
181
|
+
*/
|
|
182
|
+
export function markIndexed(workspaceRoot, nowIso = new Date().toISOString()) {
|
|
183
|
+
const prior = readDecision(workspaceRoot);
|
|
184
|
+
if (!prior)
|
|
185
|
+
return null;
|
|
186
|
+
const next = {
|
|
187
|
+
...prior,
|
|
188
|
+
lastIndexedAt: nowIso,
|
|
189
|
+
lastReindexCheckAt: nowIso,
|
|
190
|
+
};
|
|
191
|
+
writeDecision(workspaceRoot, next);
|
|
192
|
+
return next;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Stamp the last reindex-check timestamp without changing the index
|
|
196
|
+
* itself. Used by the cold-start hook so we do not show the "index is
|
|
197
|
+
* stale" hint on every keystroke once the operator has acknowledged
|
|
198
|
+
* it.
|
|
199
|
+
*/
|
|
200
|
+
export function markReindexChecked(workspaceRoot, nowIso = new Date().toISOString()) {
|
|
201
|
+
const prior = readDecision(workspaceRoot);
|
|
202
|
+
if (!prior)
|
|
203
|
+
return null;
|
|
204
|
+
const next = {
|
|
205
|
+
...prior,
|
|
206
|
+
lastReindexCheckAt: nowIso,
|
|
207
|
+
};
|
|
208
|
+
writeDecision(workspaceRoot, next);
|
|
209
|
+
return next;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Compute the staleness of the codegraph index. Pure — no IO.
|
|
213
|
+
*
|
|
214
|
+
* - returns null when `lastIndexedAt` is null (never indexed)
|
|
215
|
+
* - returns the day-delta (rounded down) otherwise
|
|
216
|
+
*/
|
|
217
|
+
export function indexAgeDays(decision, nowIso = new Date().toISOString()) {
|
|
218
|
+
if (!decision.lastIndexedAt)
|
|
219
|
+
return null;
|
|
220
|
+
const deltaMs = Date.parse(nowIso) - Date.parse(decision.lastIndexedAt);
|
|
221
|
+
if (!Number.isFinite(deltaMs) || deltaMs < 0)
|
|
222
|
+
return 0;
|
|
223
|
+
return Math.floor(deltaMs / (24 * 60 * 60 * 1000));
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Convenience predicate — should the cold-start hook show the stale-
|
|
227
|
+
* index reminder? `true` when the index is older than STALE_INDEX_DAYS
|
|
228
|
+
* AND we did NOT already remind the operator today.
|
|
229
|
+
*/
|
|
230
|
+
export function shouldNudgeStaleIndex(decision, nowIso = new Date().toISOString()) {
|
|
231
|
+
if (!decision.accepted)
|
|
232
|
+
return false;
|
|
233
|
+
const age = indexAgeDays(decision, nowIso);
|
|
234
|
+
if (age === null)
|
|
235
|
+
return false;
|
|
236
|
+
if (age < STALE_INDEX_DAYS)
|
|
237
|
+
return false;
|
|
238
|
+
// Throttle the nudge to once per day so the operator does not see it
|
|
239
|
+
// on every REPL keystroke.
|
|
240
|
+
if (decision.lastReindexCheckAt) {
|
|
241
|
+
const lastCheckDelta = Date.parse(nowIso) - Date.parse(decision.lastReindexCheckAt);
|
|
242
|
+
if (Number.isFinite(lastCheckDelta) && lastCheckDelta < 24 * 60 * 60 * 1000) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=decision-store.js.map
|