@pugi/cli 0.1.0-beta.24 → 0.1.0-beta.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/checkpoint/resumer.js +149 -0
- package/dist/core/checkpoint/rewinder.js +291 -0
- package/dist/core/compact/summarizer.js +12 -0
- package/dist/core/dispatch/cache-cleanup.js +197 -0
- package/dist/core/dispatch/cache-handoff.js +295 -0
- package/dist/core/engine/native-pugi.js +67 -3
- package/dist/core/engine/tool-bridge.js +123 -3
- 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/lsp/cache.js +105 -0
- package/dist/core/lsp/language-detect.js +66 -0
- package/dist/core/lsp/post-edit-diagnostics.js +171 -0
- package/dist/core/memory-sync/queue.js +158 -0
- package/dist/core/memory-sync/queue.spec.js +105 -0
- package/dist/core/repl/session.js +73 -1
- package/dist/core/repl/slash-commands.js +20 -0
- package/dist/core/repl/store/session-store.js +31 -2
- 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/session.js +44 -0
- package/dist/core/settings.js +9 -0
- package/dist/core/telemetry/emitter.js +229 -0
- package/dist/core/telemetry/queue.js +251 -0
- package/dist/runtime/cli.js +216 -0
- package/dist/runtime/commands/dispatch.js +126 -0
- package/dist/runtime/commands/hooks.js +184 -0
- package/dist/runtime/commands/lsp.js +25 -23
- package/dist/runtime/commands/memory.js +508 -0
- package/dist/runtime/commands/memory.spec.js +174 -0
- package/dist/runtime/commands/repo-map.js +95 -0
- package/dist/runtime/commands/resume.js +118 -0
- package/dist/runtime/commands/rewind.js +333 -0
- package/dist/runtime/commands/sessions.js +163 -0
- package/dist/runtime/version.js +1 -1
- package/dist/tools/agent-tool.js +23 -0
- package/package.json +2 -2
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pugi repo-map` — Leak L28 (2026-05-27).
|
|
3
|
+
*
|
|
4
|
+
* Builds a compact AST-light summary of the workspace's source surface
|
|
5
|
+
* (top-level function / class / interface / type / enum declarations
|
|
6
|
+
* plus the leading JSDoc / markdown summary line) and renders it as
|
|
7
|
+
* a markdown listing. The same builder backs the engine boot-time
|
|
8
|
+
* system-prompt injection — running `pugi repo-map` shows the operator
|
|
9
|
+
* EXACTLY what the engine would inject.
|
|
10
|
+
*
|
|
11
|
+
* The command surface mirrors the L28 spec:
|
|
12
|
+
*
|
|
13
|
+
* `pugi repo-map` build + show (cache hit when fresh)
|
|
14
|
+
* `pugi repo-map --refresh` bust cache + rebuild from scratch
|
|
15
|
+
* `pugi repo-map --format=json` machine-readable envelope
|
|
16
|
+
*
|
|
17
|
+
* Exit code is always 0 — the command is informational, not a gate.
|
|
18
|
+
* The `--json` envelope carries an `ok: false` branch when the repo is
|
|
19
|
+
* too large to map (the L28 5000-file ceiling), so scripted callers
|
|
20
|
+
* can detect the skip without parsing prose.
|
|
21
|
+
*
|
|
22
|
+
* The handler is awaited even though the underlying pipeline is sync,
|
|
23
|
+
* matching the pattern of every other `run*Command` (stickers, status,
|
|
24
|
+
* doctor) so future async paths (remote cache pull) drop в without
|
|
25
|
+
* changing the call sites.
|
|
26
|
+
*/
|
|
27
|
+
import { buildAndFormatRepoMap } from '../../core/repo-map/build.js';
|
|
28
|
+
/**
|
|
29
|
+
* Run the command. Always resolves к the structured envelope so the
|
|
30
|
+
* caller can chain into a status table. Exit code is set к 0 in every
|
|
31
|
+
* branch — the command is informational.
|
|
32
|
+
*/
|
|
33
|
+
export async function runRepoMapCommand(ctx) {
|
|
34
|
+
const result = buildAndFormatRepoMap({
|
|
35
|
+
root: ctx.cwd,
|
|
36
|
+
refresh: ctx.refresh === true,
|
|
37
|
+
writeCache: ctx.writeCache !== false,
|
|
38
|
+
formatBytesCap: ctx.formatBytesCap,
|
|
39
|
+
omitHeader: false,
|
|
40
|
+
});
|
|
41
|
+
if (!result.build.ok) {
|
|
42
|
+
const payload = {
|
|
43
|
+
ok: false,
|
|
44
|
+
command: 'repo-map',
|
|
45
|
+
root: result.build.root,
|
|
46
|
+
reason: result.build.reason,
|
|
47
|
+
walked: result.build.walked,
|
|
48
|
+
};
|
|
49
|
+
const text = result.build.reason === 'too-large'
|
|
50
|
+
? `repo-map skipped: workspace exceeds the ${result.build.walked}-file ceiling. Tighten .pugiignore and re-run.`
|
|
51
|
+
: `repo-map skipped: no workspace at ${result.build.root}.`;
|
|
52
|
+
ctx.writeOutput(payload, text);
|
|
53
|
+
// Informational — never a gate.
|
|
54
|
+
process.exitCode = 0;
|
|
55
|
+
return payload;
|
|
56
|
+
}
|
|
57
|
+
const build = result.build;
|
|
58
|
+
if (!build.ok) {
|
|
59
|
+
// TS narrowing — handled above.
|
|
60
|
+
throw new Error('unreachable');
|
|
61
|
+
}
|
|
62
|
+
const format = result.format;
|
|
63
|
+
const payload = {
|
|
64
|
+
ok: true,
|
|
65
|
+
command: 'repo-map',
|
|
66
|
+
root: build.root,
|
|
67
|
+
stats: {
|
|
68
|
+
filesScanned: build.scanStats.walked,
|
|
69
|
+
filesIncluded: format.filesIncluded,
|
|
70
|
+
filesRebuilt: build.diffStats.rebuilt,
|
|
71
|
+
filesReused: build.diffStats.reused,
|
|
72
|
+
filesDropped: build.diffStats.dropped,
|
|
73
|
+
skippedLarge: build.scanStats.skippedLarge,
|
|
74
|
+
skippedIgnored: build.scanStats.skippedIgnored,
|
|
75
|
+
bytes: format.bytes,
|
|
76
|
+
truncated: format.truncated,
|
|
77
|
+
},
|
|
78
|
+
cachePath: build.cachePath,
|
|
79
|
+
cacheWritten: build.cacheWritten,
|
|
80
|
+
text: format.text,
|
|
81
|
+
};
|
|
82
|
+
// The text surface IS the formatted markdown. We append a one-line
|
|
83
|
+
// stats footer so the operator sees the cache-hit ratio at a glance
|
|
84
|
+
// (engine context cache is opaque otherwise) — kept off the JSON
|
|
85
|
+
// payload so machine consumers do not have to skip a trailer.
|
|
86
|
+
const footer = formatFooter(payload.stats);
|
|
87
|
+
ctx.writeOutput(payload, `${format.text}\n${footer}`);
|
|
88
|
+
process.exitCode = 0;
|
|
89
|
+
return payload;
|
|
90
|
+
}
|
|
91
|
+
function formatFooter(stats) {
|
|
92
|
+
const truncatedHint = stats.truncated ? ' (truncated к budget)' : '';
|
|
93
|
+
return `${stats.filesIncluded} files · ${stats.filesRebuilt} rebuilt · ${stats.filesReused} cache hit · ${stats.bytes} bytes${truncatedHint}`;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=repo-map.js.map
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/resume` runtime — leak L9 (2026-05-27).
|
|
3
|
+
*
|
|
4
|
+
* Light runner that augments the existing `pugi resume` surface in
|
|
5
|
+
* `runtime/cli.ts` with rewind-aware session listings. The full
|
|
6
|
+
* REPL-mount path stays in cli.ts (it owns the credential resolver +
|
|
7
|
+
* Ink renderer); this module exposes the data-access helpers the slash
|
|
8
|
+
* dispatcher + the cli.ts handler share.
|
|
9
|
+
*
|
|
10
|
+
* Why a separate runner rather than inlining inside cli.ts: the in-REPL
|
|
11
|
+
* `/resume` slash already holds the writer lock, so it cannot use the
|
|
12
|
+
* read-only view path the top-level command relies on. The split lets
|
|
13
|
+
* the slash code call `listResumableSessionsForRepl` (which routes
|
|
14
|
+
* through the live store) while the shell code calls
|
|
15
|
+
* `listResumableSessionsReadOnly` (which uses the no-lock view).
|
|
16
|
+
*/
|
|
17
|
+
import { homedir } from 'node:os';
|
|
18
|
+
import { slugForCwd } from '../../core/repl/history.js';
|
|
19
|
+
import { applyAllMasks, listResumableSessions, } from '../../core/checkpoint/resumer.js';
|
|
20
|
+
import { findLatestActiveRewind } from '../../core/checkpoint/rewinder.js';
|
|
21
|
+
/**
|
|
22
|
+
* Read-only `pugi resume --list` path. Uses the no-lock view so a live
|
|
23
|
+
* REPL writing in the same project does not block the listing.
|
|
24
|
+
*/
|
|
25
|
+
export async function runResumeList(ctx) {
|
|
26
|
+
const slug = slugForCwd(ctx.workspaceRoot);
|
|
27
|
+
const baseInput = {
|
|
28
|
+
projectSlug: slug,
|
|
29
|
+
limit: ctx.limit ?? 10,
|
|
30
|
+
home: ctx.home ?? homedir(),
|
|
31
|
+
};
|
|
32
|
+
const sessions = await listResumableSessions(baseInput);
|
|
33
|
+
const rows = sessions.map(toResumeListRow);
|
|
34
|
+
const text = renderResumeList(rows, slug);
|
|
35
|
+
ctx.writeOutput({
|
|
36
|
+
command: 'resume',
|
|
37
|
+
status: rows.length === 0 ? 'empty' : 'listed',
|
|
38
|
+
projectSlug: slug,
|
|
39
|
+
sessions: rows,
|
|
40
|
+
}, text);
|
|
41
|
+
return {
|
|
42
|
+
command: 'resume',
|
|
43
|
+
status: rows.length === 0 ? 'empty' : 'listed',
|
|
44
|
+
projectSlug: slug,
|
|
45
|
+
sessions: rows,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* In-REPL `/resume` slash variant. The live REPL holds the writer
|
|
50
|
+
* lockfile so we cannot reuse the read-only view path; this routes
|
|
51
|
+
* through the store the session module already opened. Returns the
|
|
52
|
+
* sessions list directly so the slash handler can render system lines.
|
|
53
|
+
*/
|
|
54
|
+
export async function runResumeListForRepl(input) {
|
|
55
|
+
const slug = slugForCwd(input.workspaceRoot);
|
|
56
|
+
const limit = input.limit ?? 10;
|
|
57
|
+
const sessionRows = await input.store.listSessions({
|
|
58
|
+
project: slug,
|
|
59
|
+
limit,
|
|
60
|
+
status: 'active+archived',
|
|
61
|
+
});
|
|
62
|
+
const out = [];
|
|
63
|
+
for (const row of sessionRows) {
|
|
64
|
+
const events = await input.store.loadEvents(row.id);
|
|
65
|
+
const visible = applyAllMasks(events);
|
|
66
|
+
const latest = findLatestActiveRewind(events);
|
|
67
|
+
out.push({
|
|
68
|
+
id: row.id,
|
|
69
|
+
title: row.title,
|
|
70
|
+
branch: row.branch,
|
|
71
|
+
turnCount: row.turnCount,
|
|
72
|
+
eventCount: row.eventCount,
|
|
73
|
+
visibleEventCount: visible.length,
|
|
74
|
+
hasActiveRewind: latest !== null,
|
|
75
|
+
updatedAt: row.updatedAt,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
function toResumeListRow(input) {
|
|
81
|
+
return {
|
|
82
|
+
id: input.row.id,
|
|
83
|
+
title: input.row.title,
|
|
84
|
+
branch: input.row.branch,
|
|
85
|
+
turnCount: input.row.turnCount,
|
|
86
|
+
eventCount: input.row.eventCount,
|
|
87
|
+
visibleEventCount: input.visibleEventCount,
|
|
88
|
+
hasActiveRewind: input.hasActiveRewind,
|
|
89
|
+
updatedAt: input.row.updatedAt,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Pretty-print a resume list. Adds a small "rewound" tag when the
|
|
94
|
+
* session carries an unfinished rewind so the operator knows the
|
|
95
|
+
* transcript will load with a masked range.
|
|
96
|
+
*/
|
|
97
|
+
export function renderResumeList(rows, projectSlug) {
|
|
98
|
+
if (rows.length === 0) {
|
|
99
|
+
return `No stored sessions for project '${projectSlug}' yet.`;
|
|
100
|
+
}
|
|
101
|
+
const lines = [
|
|
102
|
+
`Recent local sessions for '${projectSlug}' (${rows.length}):`,
|
|
103
|
+
'',
|
|
104
|
+
];
|
|
105
|
+
for (let i = 0; i < rows.length; i += 1) {
|
|
106
|
+
const row = rows[i];
|
|
107
|
+
const title = (row.title ?? '(untitled)').slice(0, 64);
|
|
108
|
+
const idShort = row.id.slice(0, 13);
|
|
109
|
+
const branch = (row.branch ?? 'no-branch').padEnd(16);
|
|
110
|
+
const turns = `${row.turnCount}t`.padStart(4);
|
|
111
|
+
const events = `${row.visibleEventCount}e`.padStart(5);
|
|
112
|
+
const tag = row.hasActiveRewind ? ' [rewound]' : '';
|
|
113
|
+
lines.push(` ${idShort} ${branch} ${turns} ${events}${tag} ${title}`);
|
|
114
|
+
}
|
|
115
|
+
lines.push('', 'Resume with: pugi resume <id>');
|
|
116
|
+
return lines.join('\n');
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=resume.js.map
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/rewind` runtime — leak L9 (2026-05-27).
|
|
3
|
+
*
|
|
4
|
+
* Three invocation modes, sharing one runner:
|
|
5
|
+
*
|
|
6
|
+
* - `/rewind N` drop the last N operator turns + every
|
|
7
|
+
* tool call that landed between.
|
|
8
|
+
* - `/rewind --to <id>` rewind to a specific event index from the
|
|
9
|
+
* visible (post-mask) transcript.
|
|
10
|
+
* - `/rewind` interactive picker — surfaces the last 10
|
|
11
|
+
* user-turn boundaries (newest-first) and
|
|
12
|
+
* returns a payload the caller can use to
|
|
13
|
+
* mount a select prompt.
|
|
14
|
+
*
|
|
15
|
+
* The rewind is APPEND-ONLY: we never delete events. `applyRewindMask`
|
|
16
|
+
* elides the masked range on read; `pugi sessions undo-rewind` appends
|
|
17
|
+
* an inverse marker that nullifies the latest rewind so operators have
|
|
18
|
+
* a reliable escape hatch.
|
|
19
|
+
*
|
|
20
|
+
* Surface contract (same shape as `runCompactCommand`):
|
|
21
|
+
*
|
|
22
|
+
* - Returns a structured result for the JSON path.
|
|
23
|
+
* - Calls `ctx.writeOutput(payload, text)` once per invocation.
|
|
24
|
+
* - Throws ONLY on programmer-error. Store failures, missing
|
|
25
|
+
* sessions, etc. are surfaced as `failed_*` statuses.
|
|
26
|
+
*
|
|
27
|
+
* Exit codes (mapped by the dispatcher in cli.ts):
|
|
28
|
+
*
|
|
29
|
+
* 0 — marker appended OR picker surfaced
|
|
30
|
+
* 1 — store unavailable / session not found
|
|
31
|
+
* 2 — noop (asked to drop 0 turns, nothing to rewind, etc.)
|
|
32
|
+
*/
|
|
33
|
+
import { homedir } from 'node:os';
|
|
34
|
+
import { slugForCwd } from '../../core/repl/history.js';
|
|
35
|
+
import { appendRewindMarker, buildRewindPickerRows, pickRewindTargetForTurns, resolveEventIdToIndex, } from '../../core/checkpoint/rewinder.js';
|
|
36
|
+
import { loadFromStore } from '../../core/checkpoint/resumer.js';
|
|
37
|
+
import { SqliteSessionStore, resolveProjectStoreDir, } from '../../core/repl/store/session-store.js';
|
|
38
|
+
/**
|
|
39
|
+
* Entry point reused by the slash command + the top-level dispatcher.
|
|
40
|
+
*
|
|
41
|
+
* `args` accepts:
|
|
42
|
+
* - `[]` picker mode
|
|
43
|
+
* - `["N"]` drop last N turns
|
|
44
|
+
* - `["--to", "<id>"]` rewind to event id
|
|
45
|
+
*
|
|
46
|
+
* Both `-N` and `--turns N` are accepted for parity with Claude Code's
|
|
47
|
+
* `--turns` flag.
|
|
48
|
+
*/
|
|
49
|
+
export async function runRewindCommand(args, ctx) {
|
|
50
|
+
const parsed = parseRewindArgs(args);
|
|
51
|
+
if (parsed.kind === 'error') {
|
|
52
|
+
return emit(ctx, {
|
|
53
|
+
command: 'rewind',
|
|
54
|
+
status: 'failed_parse',
|
|
55
|
+
reason: parsed.message,
|
|
56
|
+
}, parsed.message);
|
|
57
|
+
}
|
|
58
|
+
// Resolve session + store.
|
|
59
|
+
const slug = slugForCwd(ctx.workspaceRoot);
|
|
60
|
+
let store = ctx.store ?? null;
|
|
61
|
+
let sessionId = ctx.sessionId ?? null;
|
|
62
|
+
let storeOpenedHere = false;
|
|
63
|
+
if (store === null) {
|
|
64
|
+
sessionId = sessionId ?? (await pickMostRecentSessionIdReadOnly(slug));
|
|
65
|
+
if (!sessionId) {
|
|
66
|
+
return emit(ctx, {
|
|
67
|
+
command: 'rewind',
|
|
68
|
+
status: 'failed_no_session',
|
|
69
|
+
reason: 'No active session to rewind. Start a REPL with `pugi`.',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const opened = await openLiveStore(slug, sessionId);
|
|
73
|
+
if (!opened) {
|
|
74
|
+
return emit(ctx, {
|
|
75
|
+
command: 'rewind',
|
|
76
|
+
status: 'failed_store',
|
|
77
|
+
sessionId,
|
|
78
|
+
reason: 'Could not open local session store (lock held by another REPL?).',
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
store = opened;
|
|
82
|
+
storeOpenedHere = true;
|
|
83
|
+
}
|
|
84
|
+
else if (sessionId === null) {
|
|
85
|
+
const rows = await store.listSessions({ project: slug, limit: 1, status: 'active' });
|
|
86
|
+
if (rows.length === 0) {
|
|
87
|
+
return emit(ctx, {
|
|
88
|
+
command: 'rewind',
|
|
89
|
+
status: 'failed_no_session',
|
|
90
|
+
reason: 'No active session to rewind.',
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
sessionId = rows[0].id;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const loaded = await loadFromStore(store, sessionId);
|
|
97
|
+
if (!loaded) {
|
|
98
|
+
return emit(ctx, {
|
|
99
|
+
command: 'rewind',
|
|
100
|
+
status: 'failed_no_session',
|
|
101
|
+
sessionId,
|
|
102
|
+
reason: `Session '${sessionId}' not found.`,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
// Picker mode: surface the last 10 user-turn boundaries.
|
|
106
|
+
if (parsed.kind === 'picker') {
|
|
107
|
+
const rows = buildRewindPickerRows(loaded.rawEvents, 10);
|
|
108
|
+
if (rows.length === 0) {
|
|
109
|
+
return emit(ctx, {
|
|
110
|
+
command: 'rewind',
|
|
111
|
+
status: 'noop_empty',
|
|
112
|
+
sessionId,
|
|
113
|
+
reason: 'No operator turns to rewind to.',
|
|
114
|
+
}, 'Nothing to rewind — no operator turns yet.');
|
|
115
|
+
}
|
|
116
|
+
const pickerRows = rows.map((r) => ({
|
|
117
|
+
visibleIndex: r.visibleIndex,
|
|
118
|
+
turnsAgo: r.turnsAgo,
|
|
119
|
+
preview: r.preview,
|
|
120
|
+
timestampEpochMs: r.timestampEpochMs,
|
|
121
|
+
}));
|
|
122
|
+
const text = renderPicker(pickerRows);
|
|
123
|
+
return emit(ctx, {
|
|
124
|
+
command: 'rewind',
|
|
125
|
+
status: 'picker',
|
|
126
|
+
sessionId,
|
|
127
|
+
pickerRows,
|
|
128
|
+
}, text);
|
|
129
|
+
}
|
|
130
|
+
// Resolve target index.
|
|
131
|
+
let toEventIndex;
|
|
132
|
+
let turnsRewound;
|
|
133
|
+
if (parsed.kind === 'turns') {
|
|
134
|
+
if (parsed.n <= 0) {
|
|
135
|
+
return emit(ctx, {
|
|
136
|
+
command: 'rewind',
|
|
137
|
+
status: 'noop_zero',
|
|
138
|
+
sessionId,
|
|
139
|
+
reason: 'Asked to drop 0 turns — nothing to do.',
|
|
140
|
+
}, 'Asked to drop 0 turns — nothing to do.');
|
|
141
|
+
}
|
|
142
|
+
const target = pickRewindTargetForTurns(loaded.rawEvents, parsed.n);
|
|
143
|
+
if (target.turnsRewound === 0) {
|
|
144
|
+
return emit(ctx, {
|
|
145
|
+
command: 'rewind',
|
|
146
|
+
status: 'noop_empty',
|
|
147
|
+
sessionId,
|
|
148
|
+
reason: 'No operator turns to rewind.',
|
|
149
|
+
}, 'No operator turns to rewind.');
|
|
150
|
+
}
|
|
151
|
+
toEventIndex = target.toEventIndex;
|
|
152
|
+
turnsRewound = target.turnsRewound;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
// mode === 'to-event'
|
|
156
|
+
const resolvedIdx = resolveEventIdToIndex(loaded.rawEvents, parsed.eventId);
|
|
157
|
+
if (resolvedIdx === null) {
|
|
158
|
+
return emit(ctx, {
|
|
159
|
+
command: 'rewind',
|
|
160
|
+
status: 'failed_parse',
|
|
161
|
+
sessionId,
|
|
162
|
+
reason: `Could not resolve event id '${parsed.eventId}'. Try \`/rewind\` for the picker.`,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
toEventIndex = resolvedIdx;
|
|
166
|
+
// Count user turns in the masked range to report turnsRewound.
|
|
167
|
+
turnsRewound = countUserTurnsAfter(loaded.rawEvents, toEventIndex);
|
|
168
|
+
}
|
|
169
|
+
const fromEventIndex = loaded.rawEvents.length;
|
|
170
|
+
await appendRewindMarker({
|
|
171
|
+
store,
|
|
172
|
+
toEventIndex,
|
|
173
|
+
fromEventIndex,
|
|
174
|
+
turnsRewound,
|
|
175
|
+
reason: parsed.kind === 'turns' ? 'manual' : 'to-event',
|
|
176
|
+
...(ctx.now !== undefined ? { now: ctx.now } : {}),
|
|
177
|
+
});
|
|
178
|
+
// Reload + recompute visible count for the operator banner.
|
|
179
|
+
const after = await loadFromStore(store, sessionId);
|
|
180
|
+
const visibleAfter = after?.visibleEvents.length ?? 0;
|
|
181
|
+
const banner = `Rewound ${turnsRewound} turn${turnsRewound === 1 ? '' : 's'} ` +
|
|
182
|
+
`(to event ${toEventIndex < 0 ? 'start' : `#${toEventIndex + 1}`}). ` +
|
|
183
|
+
`${visibleAfter} event${visibleAfter === 1 ? '' : 's'} now visible. ` +
|
|
184
|
+
`Undo with \`pugi sessions undo-rewind\`.`;
|
|
185
|
+
return emit(ctx, {
|
|
186
|
+
command: 'rewind',
|
|
187
|
+
status: 'rewound',
|
|
188
|
+
sessionId,
|
|
189
|
+
turnsRewound,
|
|
190
|
+
toEventIndex,
|
|
191
|
+
fromEventIndex,
|
|
192
|
+
visibleAfter,
|
|
193
|
+
}, banner);
|
|
194
|
+
}
|
|
195
|
+
finally {
|
|
196
|
+
if (storeOpenedHere && store) {
|
|
197
|
+
try {
|
|
198
|
+
await store.close();
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
/* idempotent */
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Accepts:
|
|
208
|
+
* pugi rewind -> picker
|
|
209
|
+
* pugi rewind 3 -> drop 3 turns
|
|
210
|
+
* pugi rewind --turns 3 -> same
|
|
211
|
+
* pugi rewind --to 12 -> rewind to event index 12 (1-based visible)
|
|
212
|
+
* pugi rewind --to #12 -> rewind to event index 12 (0-based hidden)
|
|
213
|
+
*/
|
|
214
|
+
function parseRewindArgs(args) {
|
|
215
|
+
if (args.length === 0)
|
|
216
|
+
return { kind: 'picker' };
|
|
217
|
+
const head = args[0];
|
|
218
|
+
// --to <id>
|
|
219
|
+
if (head === '--to' || head === '-t') {
|
|
220
|
+
const eventId = args[1];
|
|
221
|
+
if (!eventId) {
|
|
222
|
+
return {
|
|
223
|
+
kind: 'error',
|
|
224
|
+
message: 'Usage: pugi rewind --to <event-id>',
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
return { kind: 'to-event', eventId };
|
|
228
|
+
}
|
|
229
|
+
if (head.startsWith('--to=')) {
|
|
230
|
+
const eventId = head.slice('--to='.length);
|
|
231
|
+
if (eventId.length === 0) {
|
|
232
|
+
return {
|
|
233
|
+
kind: 'error',
|
|
234
|
+
message: 'Usage: pugi rewind --to <event-id>',
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
return { kind: 'to-event', eventId };
|
|
238
|
+
}
|
|
239
|
+
// --turns N OR -N OR positional N
|
|
240
|
+
if (head === '--turns' || head === '-n') {
|
|
241
|
+
const n = Number.parseInt(args[1] ?? '', 10);
|
|
242
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
243
|
+
return {
|
|
244
|
+
kind: 'error',
|
|
245
|
+
message: 'Usage: pugi rewind --turns <N>',
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
return { kind: 'turns', n };
|
|
249
|
+
}
|
|
250
|
+
if (head.startsWith('--turns=')) {
|
|
251
|
+
const n = Number.parseInt(head.slice('--turns='.length), 10);
|
|
252
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
253
|
+
return {
|
|
254
|
+
kind: 'error',
|
|
255
|
+
message: 'Usage: pugi rewind --turns <N>',
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
return { kind: 'turns', n };
|
|
259
|
+
}
|
|
260
|
+
// Bare integer: positional turns count.
|
|
261
|
+
const positional = Number.parseInt(head, 10);
|
|
262
|
+
if (Number.isFinite(positional) && positional >= 0) {
|
|
263
|
+
return { kind: 'turns', n: positional };
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
kind: 'error',
|
|
267
|
+
message: `Unknown argument '${head}'. Try \`pugi rewind\`, \`pugi rewind <N>\`, or \`pugi rewind --to <id>\`.`,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
function emit(ctx, payload, text) {
|
|
271
|
+
const human = text ?? payload.reason ?? `rewind: ${payload.status}`;
|
|
272
|
+
ctx.writeOutput(payload, human);
|
|
273
|
+
return payload;
|
|
274
|
+
}
|
|
275
|
+
function renderPicker(rows) {
|
|
276
|
+
const lines = ['Rewind picker — pick a turn boundary:', ''];
|
|
277
|
+
for (const r of rows) {
|
|
278
|
+
const tag = `[#${r.visibleIndex.toString().padStart(3)}]`;
|
|
279
|
+
const ago = `${r.turnsAgo}t ago`.padStart(8);
|
|
280
|
+
lines.push(` ${tag} ${ago} ${r.preview}`);
|
|
281
|
+
}
|
|
282
|
+
lines.push('', 'Rewind with: pugi rewind --to <#N> (or `pugi rewind <turnsToDrop>`).');
|
|
283
|
+
return lines.join('\n');
|
|
284
|
+
}
|
|
285
|
+
function countUserTurnsAfter(events, toEventIndex) {
|
|
286
|
+
let count = 0;
|
|
287
|
+
for (let i = toEventIndex + 1; i < events.length; i += 1) {
|
|
288
|
+
if (events[i].kind === 'user')
|
|
289
|
+
count += 1;
|
|
290
|
+
}
|
|
291
|
+
return count;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Open the SqliteSessionStore for the workspace's project slug, bound
|
|
295
|
+
* to `sessionId`. Returns null when the lock is held by another REPL.
|
|
296
|
+
*/
|
|
297
|
+
async function openLiveStore(projectSlug, sessionId) {
|
|
298
|
+
try {
|
|
299
|
+
const store = new SqliteSessionStore({ projectSlug, home: homedir() });
|
|
300
|
+
await store.open({
|
|
301
|
+
id: sessionId,
|
|
302
|
+
workspaceRoot: process.cwd(),
|
|
303
|
+
projectSlug,
|
|
304
|
+
});
|
|
305
|
+
return store;
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Discover the most recent active session id for a project slug,
|
|
313
|
+
* without taking the writer lockfile. Used by the standalone CLI path
|
|
314
|
+
* (no `--session` flag) so a live REPL holding the lock does not block
|
|
315
|
+
* the lookup.
|
|
316
|
+
*/
|
|
317
|
+
async function pickMostRecentSessionIdReadOnly(projectSlug) {
|
|
318
|
+
try {
|
|
319
|
+
const dir = resolveProjectStoreDir(projectSlug, homedir());
|
|
320
|
+
const view = await SqliteSessionStore.openReadOnly(dir);
|
|
321
|
+
try {
|
|
322
|
+
const rows = await view.list({ project: projectSlug, limit: 1, status: 'active' });
|
|
323
|
+
return rows.length > 0 ? rows[0].id : null;
|
|
324
|
+
}
|
|
325
|
+
finally {
|
|
326
|
+
await view.close();
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
//# sourceMappingURL=rewind.js.map
|