@pugi/cli 0.1.0-alpha.9 → 0.1.0-beta.2

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 (68) hide show
  1. package/README.md +33 -0
  2. package/assets/pugi-mascot.ansi +41 -0
  3. package/dist/commands/deploy.js +439 -0
  4. package/dist/core/agents/loader.js +104 -0
  5. package/dist/core/agents/registry.js +1 -1
  6. package/dist/core/consensus/anvil-fanout.js +276 -0
  7. package/dist/core/consensus/diff-capture.js +382 -0
  8. package/dist/core/consensus/rubric.js +233 -0
  9. package/dist/core/context/index.js +21 -0
  10. package/dist/core/context/pugiignore.js +316 -0
  11. package/dist/core/context/repo-skeleton.js +533 -0
  12. package/dist/core/context/watcher.js +342 -0
  13. package/dist/core/context/working-set.js +165 -0
  14. package/dist/core/edits/dispatch.js +185 -0
  15. package/dist/core/edits/index.js +15 -0
  16. package/dist/core/edits/layer-a-apply.js +217 -0
  17. package/dist/core/edits/layer-b-apply.js +211 -0
  18. package/dist/core/edits/layer-c-apply.js +160 -0
  19. package/dist/core/edits/layer-d-ast.js +29 -0
  20. package/dist/core/edits/marker-parser.js +401 -0
  21. package/dist/core/edits/security-gate.js +223 -0
  22. package/dist/core/edits/worktree.js +229 -0
  23. package/dist/core/engine/native-pugi.js +6 -1
  24. package/dist/core/engine/prompts.js +4 -1
  25. package/dist/core/engine/tool-bridge.js +33 -1
  26. package/dist/core/lsp/client.js +631 -0
  27. package/dist/core/repl/ask.js +512 -0
  28. package/dist/core/repl/cancellation.js +98 -0
  29. package/dist/core/repl/dispatch-fsm.js +220 -0
  30. package/dist/core/repl/privacy-banner.js +71 -0
  31. package/dist/core/repl/session.js +1896 -13
  32. package/dist/core/repl/slash-commands.js +59 -32
  33. package/dist/core/repl/store/index.js +12 -0
  34. package/dist/core/repl/store/jsonl-log.js +321 -0
  35. package/dist/core/repl/store/lockfile.js +155 -0
  36. package/dist/core/repl/store/session-store.js +792 -0
  37. package/dist/core/repl/store/types.js +44 -0
  38. package/dist/core/repl/store/uuid-v7.js +68 -0
  39. package/dist/core/repl/workspace-context.js +72 -1
  40. package/dist/core/skills/loader.js +454 -0
  41. package/dist/core/skills/sources.js +480 -0
  42. package/dist/core/skills/trust.js +172 -0
  43. package/dist/runtime/cli.js +767 -10
  44. package/dist/runtime/commands/agents.js +385 -0
  45. package/dist/runtime/commands/config.js +338 -8
  46. package/dist/runtime/commands/lsp.js +184 -0
  47. package/dist/runtime/commands/patch.js +111 -0
  48. package/dist/runtime/commands/review-consensus.js +399 -0
  49. package/dist/runtime/commands/skills.js +401 -0
  50. package/dist/runtime/commands/worktree.js +133 -0
  51. package/dist/tools/apply-patch.js +314 -0
  52. package/dist/tools/file-tools.js +90 -0
  53. package/dist/tools/lsp-tools.js +189 -0
  54. package/dist/tools/registry.js +18 -0
  55. package/dist/tools/web-fetch.js +1 -1
  56. package/dist/tui/agent-tree-pane.js +9 -0
  57. package/dist/tui/ask-cli.js +52 -0
  58. package/dist/tui/ask-modal.js +211 -0
  59. package/dist/tui/conversation-pane.js +48 -3
  60. package/dist/tui/input-box.js +48 -5
  61. package/dist/tui/markdown-render.js +266 -0
  62. package/dist/tui/repl-render.js +185 -0
  63. package/dist/tui/repl-splash-mascot.js +130 -0
  64. package/dist/tui/repl-splash.js +7 -1
  65. package/dist/tui/repl.js +82 -11
  66. package/dist/tui/status-bar.js +63 -3
  67. package/dist/tui/tool-stream-pane.js +91 -0
  68. package/package.json +11 -5
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Dispatch FSM — Sprint α6.9 Phase 1 (agent loop FSM + cancellation).
3
+ *
4
+ * Tracks the lifecycle of a single operator-issued brief from the moment
5
+ * `POST /api/pugi/sessions/:id/brief` fires until the persona returns a
6
+ * final reply (or the operator aborts). The Ctrl+C handler and the
7
+ * status-bar surface both subscribe to state transitions so the UX is
8
+ * predictable: the operator can always tell where the dispatch is in
9
+ * its lifecycle and the abort signal lands at a deterministic seam.
10
+ *
11
+ * State graph:
12
+ *
13
+ * idle
14
+ * │ (brief posted)
15
+ * ▼
16
+ * awaiting_response ←──────────┐
17
+ * │ │ (next turn)
18
+ * ▼ │
19
+ * tool_running ────────────────┘
20
+ * │
21
+ * ▼
22
+ * completed
23
+ *
24
+ * Terminal states: `completed`, `failed`, `aborted`. The FSM does not
25
+ * transition out of a terminal state; a fresh brief mints a new FSM
26
+ * instance. `aborting` is transient — any non-terminal state can
27
+ * transition to `aborting`, which then transitions to `aborted` once
28
+ * the SSE stream + in-flight tool acknowledge the abort.
29
+ *
30
+ * Illegal transitions are rejected by throwing a typed error. This is
31
+ * load-bearing: a bug that tries to walk `completed → tool_running`
32
+ * would otherwise silently corrupt the agent tree. Throwing fails
33
+ * loud at the call site so the integration bug surfaces immediately.
34
+ *
35
+ * onEnter listeners fire AFTER the state transition is committed.
36
+ * Subscribers can call `transition()` from inside a listener; the FSM
37
+ * uses a small re-entrancy guard so nested transitions are processed
38
+ * in order (the inner transition fires its own listeners before the
39
+ * outer transition returns).
40
+ *
41
+ * Brand voice: no forbidden words. ASCII only. No emoji.
42
+ */
43
+ /**
44
+ * Legal transitions per state. Building the matrix at module scope
45
+ * keeps the FSM declarative and exhaustive — adding a new state forces
46
+ * the matrix update at typecheck time.
47
+ *
48
+ * `idle` is the start state. Posting a brief moves to
49
+ * `awaiting_response`. The model's first turn either returns a final
50
+ * text (`completed`), requests tools (`tool_running`), or fails
51
+ * (`failed`). After tools execute the model gets another turn
52
+ * (`awaiting_response`); the cycle repeats until a final text or a
53
+ * terminal outcome.
54
+ *
55
+ * `aborting` is reachable from every non-terminal state. From
56
+ * `aborting` we always end at `aborted` (no rollback — once the
57
+ * operator aborts, the dispatch is dead). Terminal states have no
58
+ * outgoing transitions.
59
+ */
60
+ const LEGAL_TRANSITIONS = {
61
+ idle: new Set(['awaiting_response', 'aborting']),
62
+ awaiting_response: new Set([
63
+ 'tool_running',
64
+ 'completed',
65
+ 'failed',
66
+ 'aborting',
67
+ ]),
68
+ tool_running: new Set([
69
+ 'awaiting_response',
70
+ 'completed',
71
+ 'failed',
72
+ 'aborting',
73
+ ]),
74
+ aborting: new Set(['aborted']),
75
+ aborted: new Set(),
76
+ completed: new Set(),
77
+ failed: new Set(),
78
+ };
79
+ /**
80
+ * Thrown when `transition()` is called with a state that is not in the
81
+ * current state's legal outgoing set. Carries enough context that a log
82
+ * line can be reconstructed at the call site.
83
+ */
84
+ export class IllegalDispatchTransitionError extends Error {
85
+ from;
86
+ to;
87
+ constructor(from, to) {
88
+ super(`Illegal dispatch FSM transition: ${from} -> ${to}`);
89
+ this.name = 'IllegalDispatchTransitionError';
90
+ this.from = from;
91
+ this.to = to;
92
+ }
93
+ }
94
+ export class DispatchFSM {
95
+ state = 'idle';
96
+ listeners = new Map();
97
+ /**
98
+ * Re-entrancy guard. While `firing === true` we are draining the
99
+ * listener fan-out for an in-progress transition. A nested
100
+ * `transition()` call from inside a listener queues into `pending`
101
+ * instead of recursing, so the legality check + state mutation +
102
+ * listener drain happen sequentially in commit order. Without this,
103
+ * a listener that immediately fired another transition would mutate
104
+ * `this.state` mid-iteration of the outer listener snapshot, which
105
+ * (a) could turn a legal outer transition into a thrown illegal
106
+ * inner one (the legality check reads `this.state` already mutated),
107
+ * (b) could ship `onEnter(B)` reasons under state A's snapshot.
108
+ * Queueing keeps the transition log linear.
109
+ */
110
+ firing = false;
111
+ pending = [];
112
+ /**
113
+ * Current state. Read-only — mutate via `transition()`.
114
+ */
115
+ get current() {
116
+ return this.state;
117
+ }
118
+ /**
119
+ * True when the current state is one of `completed`, `failed`,
120
+ * `aborted`. Terminal states cannot transition further.
121
+ */
122
+ get isTerminal() {
123
+ return this.state === 'completed' || this.state === 'failed' || this.state === 'aborted';
124
+ }
125
+ /**
126
+ * Transition the FSM to `next`, optionally carrying a `reason` string
127
+ * that is forwarded to `onEnter` listeners. Throws
128
+ * `IllegalDispatchTransitionError` when the transition is not in the
129
+ * current state's legal outgoing set.
130
+ *
131
+ * Listener errors do NOT block the transition. A throwing listener
132
+ * stops itself but the transition is already committed; the next
133
+ * listener still fires.
134
+ *
135
+ * Re-entrant calls (a listener calls `transition()` again) are
136
+ * queued - the inner transition runs after the outer listener drain
137
+ * completes, in FIFO order. See `firing` field comment.
138
+ */
139
+ transition(next, reason) {
140
+ if (this.firing) {
141
+ this.pending.push({ next, reason });
142
+ return;
143
+ }
144
+ this.firing = true;
145
+ try {
146
+ // R2 P2 fix (Codex triple-review 2026-05-25): if `commit` throws -
147
+ // either the first move or any queued nested move - we MUST flush
148
+ // the pending queue before propagating the error. Otherwise a
149
+ // later `transition()` call would drain stale entries left over
150
+ // from the failed turn, firing onEnter listeners under a wholly
151
+ // different state context. The queue is "poisoned" the moment any
152
+ // entry throws; the safest move is to drop the rest and surface
153
+ // the failure to the caller.
154
+ try {
155
+ this.commit(next, reason);
156
+ while (this.pending.length > 0) {
157
+ const queued = this.pending.shift();
158
+ if (!queued)
159
+ break;
160
+ this.commit(queued.next, queued.reason);
161
+ }
162
+ }
163
+ catch (err) {
164
+ // Drop every still-queued entry so the next external transition
165
+ // starts on a clean queue. See the inline rationale above.
166
+ this.pending.length = 0;
167
+ throw err;
168
+ }
169
+ }
170
+ finally {
171
+ this.firing = false;
172
+ }
173
+ }
174
+ /**
175
+ * Inner commit step - legality check + state mutation + listener
176
+ * drain. Called by `transition()` directly for the first move and
177
+ * for every queued nested move thereafter. Errors propagate to the
178
+ * outer `transition()` caller (illegal transitions still throw).
179
+ */
180
+ commit(next, reason) {
181
+ const legal = LEGAL_TRANSITIONS[this.state];
182
+ if (!legal.has(next)) {
183
+ throw new IllegalDispatchTransitionError(this.state, next);
184
+ }
185
+ this.state = next;
186
+ const set = this.listeners.get(next);
187
+ if (!set || set.size === 0)
188
+ return;
189
+ // Snapshot the listener set so a listener that detaches itself
190
+ // (via the returned unsubscribe handle) does not corrupt iteration.
191
+ const snapshot = Array.from(set);
192
+ for (const listener of snapshot) {
193
+ try {
194
+ listener(reason);
195
+ }
196
+ catch {
197
+ // Swallow listener errors — see header comment for rationale.
198
+ }
199
+ }
200
+ }
201
+ /**
202
+ * Register a listener that fires whenever the FSM ENTERS `state`. The
203
+ * listener does NOT fire if the FSM is already in `state` at
204
+ * registration time — onEnter is edge-triggered, not level-triggered.
205
+ *
206
+ * Returns an unsubscribe handle.
207
+ */
208
+ onEnter(state, listener) {
209
+ let set = this.listeners.get(state);
210
+ if (!set) {
211
+ set = new Set();
212
+ this.listeners.set(state, set);
213
+ }
214
+ set.add(listener);
215
+ return () => {
216
+ set?.delete(listener);
217
+ };
218
+ }
219
+ }
220
+ //# sourceMappingURL=dispatch-fsm.js.map
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Privacy mode REPL surface - alpha 6.13 (Phase 1).
3
+ *
4
+ * Two surfaces:
5
+ *
6
+ * 1. `renderPrivacyBanner(mode)` - one-line banner shown on REPL
7
+ * bootstrap (mirrors `apps/admin-api/src/privacy/privacy-mode.ts`
8
+ * `PRIVACY_MODE_BANNER` verbatim - keep in sync, the unit spec
9
+ * asserts they match).
10
+ *
11
+ * 2. `renderPrivacyContractDoc(mode)` - multi-line contract doc the
12
+ * `/privacy` slash command prints. Shows the active mode header
13
+ * + the full 3-mode contract so the operator can compare their
14
+ * current posture to the alternatives without leaving the REPL.
15
+ *
16
+ * The strings are pinned client-side so the contract doc is
17
+ * available even when the operator is offline / has not authenticated
18
+ * yet. The mode value itself is server-side authoritative (resolved
19
+ * via /api/admin/privacy/mode); the banner falls back to "(unknown)"
20
+ * when the round-trip fails.
21
+ *
22
+ * Brand voice: ASCII hyphens only, no em-dashes, no emoji decoration.
23
+ */
24
+ export const PRIVACY_MODES = ['strict', 'balanced', 'permissive'];
25
+ export function isPrivacyMode(value) {
26
+ return (typeof value === 'string' && PRIVACY_MODES.includes(value));
27
+ }
28
+ /**
29
+ * One-line banner. Mirrors `PRIVACY_MODE_BANNER` on the server.
30
+ */
31
+ const BANNERS = Object.freeze({
32
+ strict: 'Privacy: strict (no upstream LLM, no external tool egress)',
33
+ balanced: 'Privacy: balanced (PII scrubbed before upstream LLM)',
34
+ permissive: 'Privacy: permissive (raw prompts forwarded upstream)',
35
+ });
36
+ export function renderPrivacyBanner(mode) {
37
+ if (!mode || !isPrivacyMode(mode)) {
38
+ return 'Privacy: (unknown - mode lookup pending)';
39
+ }
40
+ return BANNERS[mode];
41
+ }
42
+ /**
43
+ * Full mode contract doc. Printed by the `/privacy` slash command.
44
+ * Keep in sync with `PRIVACY_MODE_CONTRACT_DOC` on the server.
45
+ */
46
+ const CONTRACT_DOC = `
47
+ Pugi privacy mode contract (alpha 6.13):
48
+
49
+ strict Maximum privacy. Nothing leaves tenant infra.
50
+ - Upstream LLM calls REFUSED (use self-hosted Ollama / llama.cpp).
51
+ - External tool calls require per-call confirm.
52
+ - Session.db, blobs, transcripts stay on operator disk.
53
+ - No telemetry. No error reporting.
54
+
55
+ balanced Default. PII-scrubbed content goes to the upstream LLM.
56
+ - 3-layer PII scrubber runs before egress (regex + NER + LLM).
57
+ - Tool calls to external services allowed (logged).
58
+ - Anonymized telemetry only (counts + error categories).
59
+
60
+ permissive Verbatim. Power-user mode.
61
+ - Raw prompts forwarded to upstream provider (no scrubbing).
62
+ - Tool calls to external services allowed.
63
+ - Full error reporting (may include prompt fragments).
64
+ - Accepts the upstream provider's data retention policy.
65
+
66
+ Switch with: pugi config set privacy=strict|balanced|permissive
67
+ `.trim();
68
+ export function renderPrivacyContractDoc(currentMode) {
69
+ return `${renderPrivacyBanner(currentMode)}\n\n${CONTRACT_DOC}`;
70
+ }
71
+ //# sourceMappingURL=privacy-banner.js.map