@pugi/cli 0.1.0-beta.12 → 0.1.0-beta.14

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 (58) hide show
  1. package/dist/core/consensus/diff-capture.js +73 -0
  2. package/dist/core/context/index.js +7 -0
  3. package/dist/core/context/markdown-traverse.js +255 -0
  4. package/dist/core/edits/dispatch.js +218 -2
  5. package/dist/core/edits/journal.js +199 -0
  6. package/dist/core/edits/layer-d-ast.js +557 -14
  7. package/dist/core/edits/verify-hook.js +273 -0
  8. package/dist/core/engine/anvil-client.js +99 -5
  9. package/dist/core/engine/context-prefix.js +155 -0
  10. package/dist/core/engine/intent.js +260 -0
  11. package/dist/core/engine/native-pugi.js +663 -249
  12. package/dist/core/engine/prompts.js +52 -2
  13. package/dist/core/engine/tool-bridge.js +311 -9
  14. package/dist/core/lsp/client.js +57 -0
  15. package/dist/core/mcp/client.js +9 -0
  16. package/dist/core/mcp/http-server.js +553 -0
  17. package/dist/core/mcp/permission.js +190 -0
  18. package/dist/core/mcp/server-tools.js +219 -0
  19. package/dist/core/mcp/server.js +397 -0
  20. package/dist/core/repl/history.js +11 -1
  21. package/dist/core/repl/model-pricing.js +135 -0
  22. package/dist/core/repl/session.js +328 -12
  23. package/dist/core/repl/slash-commands.js +18 -4
  24. package/dist/core/settings.js +43 -0
  25. package/dist/core/subagents/dispatcher-real.js +600 -0
  26. package/dist/core/subagents/dispatcher.js +113 -24
  27. package/dist/core/subagents/index.js +18 -5
  28. package/dist/core/subagents/isolation-matrix.js +213 -0
  29. package/dist/core/subagents/spawn.js +19 -4
  30. package/dist/core/transport/version-interceptor.js +166 -0
  31. package/dist/index.js +28 -0
  32. package/dist/runtime/bootstrap.js +190 -0
  33. package/dist/runtime/cli.js +859 -269
  34. package/dist/runtime/commands/lsp.js +165 -5
  35. package/dist/runtime/commands/mcp.js +537 -0
  36. package/dist/runtime/commands/review-consensus.js +17 -2
  37. package/dist/runtime/headless.js +543 -0
  38. package/dist/runtime/load-hooks-or-exit.js +71 -0
  39. package/dist/runtime/version.js +65 -0
  40. package/dist/tools/agent-tool.js +192 -0
  41. package/dist/tools/apply-patch.js +62 -1
  42. package/dist/tools/mcp-tool.js +260 -0
  43. package/dist/tools/multi-edit.js +361 -0
  44. package/dist/tools/registry.js +5 -0
  45. package/dist/tools/web-fetch.js +147 -2
  46. package/dist/tools/web-search.js +458 -0
  47. package/dist/tui/agent-tree.js +10 -0
  48. package/dist/tui/ask-modal.js +2 -2
  49. package/dist/tui/conversation-pane.js +1 -1
  50. package/dist/tui/input-box.js +1 -1
  51. package/dist/tui/markdown-render.js +4 -4
  52. package/dist/tui/repl-render.js +105 -15
  53. package/dist/tui/repl-splash.js +2 -2
  54. package/dist/tui/repl.js +10 -4
  55. package/dist/tui/splash.js +1 -1
  56. package/dist/tui/status-bar.js +94 -16
  57. package/dist/tui/update-banner.js +20 -2
  58. package/package.json +5 -4
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Edit journal — β1b Pl8 (2026-05-26).
3
+ *
4
+ * Per-session append-only log of file-mutation INTENT before the
5
+ * dispatcher runs. Lives at `<root>/.pugi/sessions/<sessionId>/journal.jsonl`,
6
+ * one JSON entry per multi-file dispatch. On a non-zero exit / budget
7
+ * kill / OOM crash, the journal lets `git restore` / `fs.unlink` revert
8
+ * the workspace to its pre-dispatch state.
9
+ *
10
+ * Shape:
11
+ *
12
+ * ```ts
13
+ * {
14
+ * ts: number, // ms epoch — opens chronological replay
15
+ * taskId: string, // engine task id (`<kind>-<ts>`) for correlation
16
+ * files: [{
17
+ * path: string, // workspace-relative
18
+ * existed: boolean, // existed BEFORE the dispatch
19
+ * sha256_before?: string, // present iff existed === true
20
+ * }],
21
+ * }
22
+ * ```
23
+ *
24
+ * The journal records ONLY intent: it does not enumerate the post-state
25
+ * (a per-edit `applied` event already lives in `events.jsonl`). On
26
+ * rollback we need the pre-state to know:
27
+ * - whether to `git restore` (the file existed before and is git-
28
+ * tracked) OR `fs.unlink` (the file did NOT exist before — it was
29
+ * created by the failed dispatch).
30
+ * - whether to fall back to writing the cached `sha256_before` source
31
+ * when neither git nor fs can recover (untracked file that existed
32
+ * before — the dispatcher MUST have cached the pre-content; we
33
+ * surface a `partial_rollback` reason and let the operator decide).
34
+ *
35
+ * Why a separate journal vs reusing `events.jsonl`:
36
+ * - events.jsonl mixes status + tool_call + tool_result + outcome
37
+ * records from the engine loop. A crash mid-dispatch leaves the
38
+ * file in a state where reconstructing "files we MIGHT have
39
+ * touched" requires correlating multiple event types. The journal
40
+ * hoists the rollback-relevant subset into one line per dispatch
41
+ * so the recovery code is grep-able + audit-readable.
42
+ * - The journal is also forward-compatible with a future
43
+ * `pugi resume --rollback <taskId>` operator command — single
44
+ * source of truth for "what would I undo".
45
+ *
46
+ * Best-effort writes: a journal failure (disk full, .pugi unwritable)
47
+ * must NEVER block the actual edit. The dispatcher proceeds with
48
+ * `journalEntryId: null` and surfaces a warning in the session's
49
+ * status events. Rollback then degrades to apply-patch-style
50
+ * pre-existing snapshot (the dispatcher keeps an in-memory copy too).
51
+ *
52
+ * Brand voice: ASCII only, no banned words. Operator-facing tail
53
+ * (jq friendly).
54
+ */
55
+ import { appendFileSync, existsSync, mkdirSync, readFileSync, statSync, } from 'node:fs';
56
+ import { createHash } from 'node:crypto';
57
+ import { join, resolve } from 'node:path';
58
+ /**
59
+ * Compute the on-disk journal path for a session. The directory is
60
+ * created lazily by `appendEntry` so a read against a missing session
61
+ * does not silently mkdir.
62
+ */
63
+ export function journalPath(workspaceRoot, sessionId) {
64
+ return resolve(workspaceRoot, '.pugi', 'sessions', sessionId, 'journal.jsonl');
65
+ }
66
+ /**
67
+ * sha256 of a file's bytes. Used by `snapshotForDispatch` to capture
68
+ * the pre-content fingerprint so rollback can detect "file was changed
69
+ * between intent + rollback" cases (rare but real — a concurrent
70
+ * external editor + the dispatcher hitting the same path).
71
+ *
72
+ * Returns null when the file cannot be hashed (missing / unreadable);
73
+ * caller treats absent fingerprint as `existed=false`.
74
+ */
75
+ export function sha256File(absPath) {
76
+ try {
77
+ const buf = readFileSync(absPath);
78
+ const h = createHash('sha256');
79
+ h.update(buf);
80
+ return h.digest('hex');
81
+ }
82
+ catch {
83
+ return null;
84
+ }
85
+ }
86
+ /**
87
+ * Build a snapshot of the pre-dispatch state for every workspace-relative
88
+ * path the dispatcher is about to touch. Pure read; no writes.
89
+ *
90
+ * `paths` are workspace-relative. The journal records the SAME shape so
91
+ * the rollback path can re-resolve against the same `workspaceRoot`
92
+ * without ambiguity.
93
+ */
94
+ export function snapshotForDispatch(workspaceRoot, paths) {
95
+ const out = [];
96
+ for (const rel of paths) {
97
+ const abs = resolve(workspaceRoot, rel);
98
+ if (existsSync(abs)) {
99
+ // Hash only on regular files; sha256File returns null for
100
+ // directories or unreadable inodes and we degrade cleanly.
101
+ try {
102
+ const st = statSync(abs);
103
+ if (st.isFile()) {
104
+ const sha = sha256File(abs);
105
+ out.push({ path: rel, existed: true, ...(sha ? { sha256_before: sha } : {}) });
106
+ continue;
107
+ }
108
+ }
109
+ catch {
110
+ /* fall through — treat as non-existent for rollback */
111
+ }
112
+ }
113
+ out.push({ path: rel, existed: false });
114
+ }
115
+ return out;
116
+ }
117
+ /**
118
+ * Append one journal entry. Best-effort: any I/O failure returns false
119
+ * and the dispatcher proceeds without journal-backed rollback. The
120
+ * in-memory `JournalFileEntry[]` snapshot still drives the immediate
121
+ * post-crash rollback inside the same process.
122
+ */
123
+ export function appendEntry(workspaceRoot, sessionId, entry) {
124
+ const path = journalPath(workspaceRoot, sessionId);
125
+ try {
126
+ mkdirSync(join(workspaceRoot, '.pugi', 'sessions', sessionId), {
127
+ recursive: true,
128
+ mode: 0o700,
129
+ });
130
+ }
131
+ catch {
132
+ return false;
133
+ }
134
+ try {
135
+ appendFileSync(path, `${JSON.stringify(entry)}\n`, { encoding: 'utf8', mode: 0o600 });
136
+ return true;
137
+ }
138
+ catch {
139
+ return false;
140
+ }
141
+ }
142
+ /**
143
+ * Read every journal entry for a session, oldest-first. Malformed
144
+ * lines are dropped silently — a single bad line should not nuke
145
+ * recovery. Returns `[]` when the file is missing.
146
+ *
147
+ * Synchronous (mirrors `recordToolCall` etc) because the operator-
148
+ * facing rollback path needs to be deterministic; a partially-
149
+ * streamed read would race the very crash recovery it is supporting.
150
+ */
151
+ export function readEntries(workspaceRoot, sessionId) {
152
+ const path = journalPath(workspaceRoot, sessionId);
153
+ if (!existsSync(path))
154
+ return [];
155
+ let raw;
156
+ try {
157
+ raw = readFileSync(path, 'utf8');
158
+ }
159
+ catch {
160
+ return [];
161
+ }
162
+ const out = [];
163
+ for (const line of raw.split('\n')) {
164
+ const trimmed = line.trim();
165
+ if (trimmed.length === 0)
166
+ continue;
167
+ try {
168
+ const parsed = JSON.parse(trimmed);
169
+ if (!parsed ||
170
+ typeof parsed !== 'object' ||
171
+ typeof parsed.taskId !== 'string' ||
172
+ typeof parsed.ts !== 'number' ||
173
+ !Array.isArray(parsed.files)) {
174
+ continue;
175
+ }
176
+ // β1b r1 defense-in-depth: validate every `files[]` entry too.
177
+ // A malformed file entry (missing `path`, wrong `existed` type)
178
+ // would silently feed garbage into the rollback path; better to
179
+ // drop the whole journal line than to attempt restore against a
180
+ // bogus snapshot.
181
+ const candidate = parsed;
182
+ const allFilesValid = candidate.files.every((f) => f !== null &&
183
+ typeof f === 'object' &&
184
+ typeof f.path === 'string' &&
185
+ f.path.length > 0 &&
186
+ typeof f.existed === 'boolean' &&
187
+ (f.sha256_before === undefined ||
188
+ typeof f.sha256_before === 'string'));
189
+ if (!allFilesValid)
190
+ continue;
191
+ out.push(candidate);
192
+ }
193
+ catch {
194
+ /* drop malformed line */
195
+ }
196
+ }
197
+ return out;
198
+ }
199
+ //# sourceMappingURL=journal.js.map