@pugi/cli 0.1.0-beta.16 → 0.1.0-beta.17

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.
@@ -0,0 +1,201 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * `pugi jobs --watch` Ink TUI — α7 live progress (2026-05-27).
4
+ *
5
+ * Tails `~/.pugi/agent-progress/*.json` via chokidar and re-renders a
6
+ * grid of <AgentProgressCard> components. Mirrors the Claude Code
7
+ * `/compact` layout pattern — one card per agent, sorted by status
8
+ * (running → completed → failed) then by lastUpdate descending so the
9
+ * freshest activity floats к the top.
10
+ *
11
+ * Lifecycle:
12
+ * 1. Resolve dir via resolveProgressDir() (env-aware).
13
+ * 2. Initial scan with readdirSync — populate the map.
14
+ * 3. chokidar.watch(dir, { ignoreInitial: true }) for add/change/unlink.
15
+ * 4. setInterval(1000) tick re-renders to keep elapsed labels live
16
+ * WITHOUT re-reading files.
17
+ * 5. SIGINT (Ctrl+C) triggers `app.unmount()` + process.exit(0).
18
+ *
19
+ * Layout: single column under 100 cols, two columns at 100+ cols
20
+ * (Ink's flexbox computes the responsive split automatically — we just
21
+ * set `flexDirection="row" flexWrap="wrap"` and bound each card к 50%
22
+ * width when stdout is wide enough).
23
+ *
24
+ * The TUI is opt-in (operator runs `pugi jobs --watch`); the bare
25
+ * `pugi jobs` continues to print the legacy job-registry table.
26
+ */
27
+ import { existsSync, readFileSync, readdirSync } from 'node:fs';
28
+ import { join } from 'node:path';
29
+ import chokidar from 'chokidar';
30
+ import { useEffect, useMemo, useState } from 'react';
31
+ import { Box, Text, render, useApp, useInput, useStdout } from 'ink';
32
+ import { resolveProgressDir } from '../core/agent-progress/writer.js';
33
+ import { runCleanup } from '../core/agent-progress/cleanup.js';
34
+ import { validateAgentProgress, } from '../core/agent-progress/schema.js';
35
+ import { AgentProgressCard } from '../tui/agent-progress-card.js';
36
+ /** Cleanup pass cadence inside the watcher. Cheap (readdir on a small
37
+ * dir); 60s is the operator-tested sweet spot — fast enough to feel
38
+ * live, slow enough к never burn CPU. */
39
+ const CLEANUP_INTERVAL_MS = 60_000;
40
+ const STATUS_RANK = {
41
+ running: 0,
42
+ completed: 1,
43
+ failed: 2,
44
+ };
45
+ /**
46
+ * Read + validate one progress file. Returns undefined on any failure
47
+ * — the watcher prefers к skip a malformed entry rather than crash.
48
+ * Exported for the spec.
49
+ */
50
+ export function readProgressFile(path) {
51
+ if (!existsSync(path))
52
+ return undefined;
53
+ try {
54
+ const body = readFileSync(path, 'utf8');
55
+ const parsed = JSON.parse(body);
56
+ const result = validateAgentProgress(parsed);
57
+ return result.ok ? result.value : undefined;
58
+ }
59
+ catch {
60
+ return undefined;
61
+ }
62
+ }
63
+ /**
64
+ * Sort progress entries for stable, operator-friendly display:
65
+ * 1. Running first.
66
+ * 2. Then by lastUpdate (most recent first).
67
+ * 3. Tiebreak on agentId for snapshot stability.
68
+ */
69
+ export function sortProgressEntries(entries) {
70
+ return [...entries].sort((a, b) => {
71
+ const rank = STATUS_RANK[a.status] - STATUS_RANK[b.status];
72
+ if (rank !== 0)
73
+ return rank;
74
+ const tsDiff = Date.parse(b.lastUpdate) - Date.parse(a.lastUpdate);
75
+ if (tsDiff !== 0)
76
+ return tsDiff;
77
+ return a.agentId.localeCompare(b.agentId);
78
+ });
79
+ }
80
+ export function JobsWatch(props) {
81
+ const { dir, nowEpochMs, staticMode = false } = props;
82
+ const resolvedDir = useMemo(() => resolveProgressDir(dir), [dir]);
83
+ const [agents, setAgents] = useState(() => initialScan(resolvedDir));
84
+ const [tick, setTick] = useState(0);
85
+ const { exit } = useApp();
86
+ const { stdout } = useStdout();
87
+ const cols = stdout?.columns ?? 80;
88
+ const twoColumns = cols >= 100;
89
+ useInput((_input, key) => {
90
+ if (key.escape || key.ctrl) {
91
+ // Ink's useInput passes ctrl=true for Ctrl+<x>; we exit on Ctrl+C
92
+ // (key.ctrl alone is too broad — actual key char arrives in `input`).
93
+ // Practically, raw mode passes Ctrl+C as SIGINT to the process so
94
+ // this is a belt-and-suspenders catch.
95
+ if (key.escape)
96
+ exit();
97
+ }
98
+ });
99
+ useEffect(() => {
100
+ if (staticMode)
101
+ return;
102
+ let watcher;
103
+ try {
104
+ watcher = chokidar.watch(resolvedDir, {
105
+ ignoreInitial: true,
106
+ depth: 0,
107
+ // The agent writer uses atomic rename — chokidar reports `add`
108
+ // when the final filename appears, never the *.tmp-* intermediate.
109
+ ignored: /\.tmp-/,
110
+ });
111
+ const onChange = (path) => {
112
+ if (!/\.json$/.test(path))
113
+ return;
114
+ const progress = readProgressFile(path);
115
+ if (!progress)
116
+ return;
117
+ setAgents((prev) => ({ ...prev, [progress.agentId]: progress }));
118
+ };
119
+ const onRemove = (path) => {
120
+ const match = /([^/\\]+)\.json$/.exec(path);
121
+ if (!match)
122
+ return;
123
+ const id = match[1];
124
+ if (!id)
125
+ return;
126
+ setAgents((prev) => {
127
+ if (!(id in prev))
128
+ return prev;
129
+ const next = { ...prev };
130
+ delete next[id];
131
+ return next;
132
+ });
133
+ };
134
+ watcher.on('add', onChange);
135
+ watcher.on('change', onChange);
136
+ watcher.on('unlink', onRemove);
137
+ }
138
+ catch {
139
+ // Watcher failures degrade gracefully — initial scan still rendered.
140
+ }
141
+ const interval = setInterval(() => setTick((t) => t + 1), 1000);
142
+ // Auto-cleanup completed/failed entries older than the TTL. The
143
+ // sweep moves them к `<dir>/archive/` so operators can still
144
+ // forensic; the watcher's chokidar `unlink` handler drops them
145
+ // from the live grid as the rename fires.
146
+ const cleanupInterval = setInterval(() => {
147
+ try {
148
+ runCleanup({ dir: resolvedDir });
149
+ }
150
+ catch {
151
+ // best-effort housekeeping; never crash the TUI
152
+ }
153
+ }, CLEANUP_INTERVAL_MS);
154
+ return () => {
155
+ clearInterval(interval);
156
+ clearInterval(cleanupInterval);
157
+ if (watcher) {
158
+ void watcher.close();
159
+ }
160
+ };
161
+ }, [resolvedDir, staticMode]);
162
+ const sorted = useMemo(() => sortProgressEntries(Object.values(agents)), [agents]);
163
+ const effectiveNow = nowEpochMs ?? Date.now() + tick * 0; // tick triggers re-render only
164
+ if (sorted.length === 0) {
165
+ return (_jsxs(Box, { flexDirection: "column", padding: 1, children: [_jsxs(Text, { dimColor: true, children: ["No agents broadcasting progress yet. Watching", ' ', _jsx(Text, { bold: true, children: resolvedDir }), " for `<id>.json` files\u2026"] }), _jsx(Text, { dimColor: true, children: "(Press Esc / Ctrl+C \u043A exit.)" })] }));
166
+ }
167
+ return (_jsxs(Box, { flexDirection: "column", padding: 1, children: [_jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { bold: true, children: "pugi jobs --watch" }), _jsxs(Text, { dimColor: true, children: [' ', "\u00B7 ", sorted.length, " ", sorted.length === 1 ? 'agent' : 'agents', " \u00B7", ' ', resolvedDir] })] }), _jsx(Box, { flexDirection: "row", flexWrap: "wrap", children: sorted.map((progress) => (_jsx(Box, { width: twoColumns ? '50%' : '100%', flexDirection: "column", children: _jsx(AgentProgressCard, { progress: progress, nowEpochMs: effectiveNow }) }, progress.agentId))) }), _jsx(Text, { dimColor: true, children: "(Esc / Ctrl+C \u043A exit.)" })] }));
168
+ }
169
+ function initialScan(dir) {
170
+ const map = {};
171
+ if (!existsSync(dir))
172
+ return map;
173
+ let entries;
174
+ try {
175
+ entries = readdirSync(dir);
176
+ }
177
+ catch {
178
+ return map;
179
+ }
180
+ for (const name of entries) {
181
+ if (!name.endsWith('.json'))
182
+ continue;
183
+ if (/\.tmp-/.test(name))
184
+ continue;
185
+ const progress = readProgressFile(join(dir, name));
186
+ if (progress)
187
+ map[progress.agentId] = progress;
188
+ }
189
+ return map;
190
+ }
191
+ /**
192
+ * Entry-point for `pugi jobs --watch`. Mounts the Ink app and returns
193
+ * a Promise that resolves when the user exits (Ctrl+C / Esc).
194
+ */
195
+ export async function runJobsWatch(options = {}) {
196
+ return new Promise((resolve) => {
197
+ const app = render(_jsx(JobsWatch, { dir: options.dir }), { exitOnCtrlC: true });
198
+ void app.waitUntilExit().then(() => resolve(0));
199
+ });
200
+ }
201
+ //# sourceMappingURL=jobs-watch.js.map
@@ -16,6 +16,7 @@
16
16
  */
17
17
  import { existsSync, readFileSync } from 'node:fs';
18
18
  import { formatDuration, getJobRegistry, relativeAge, } from '../core/jobs/registry.js';
19
+ import { runJobsWatch } from './jobs-watch.js';
19
20
  const HUMAN_STATUS = {
20
21
  running: 'on watch',
21
22
  finished: 'shipped',
@@ -24,6 +25,19 @@ const HUMAN_STATUS = {
24
25
  abandoned: 'lost',
25
26
  };
26
27
  export async function runJobsCommand(args, flags, io, sessionId) {
28
+ // α7 live progress (2026-05-27): `pugi jobs --watch` mounts the live
29
+ // agent-progress TUI. The flag may sit anywhere in the arg list
30
+ // (`pugi jobs --watch` or `pugi jobs watch`) — both forms route к
31
+ // the watcher. JSON mode is incompatible (it would never return); we
32
+ // bail with a helpful error instead of silently swallowing the flag.
33
+ const wantsWatch = args.includes('--watch') || args[0] === 'watch';
34
+ if (wantsWatch) {
35
+ if (flags.json) {
36
+ io.writeError('pugi jobs --watch is interactive; --json is not supported.');
37
+ return 2;
38
+ }
39
+ return runJobsWatch();
40
+ }
27
41
  const sub = args[0] ?? 'list';
28
42
  switch (sub) {
29
43
  case 'list':
@@ -48,6 +62,7 @@ function usage() {
48
62
  ' pugi jobs tail <id> Stream the captured artifact.',
49
63
  ' pugi jobs kill <id> [--json] SIGTERM, escalate to SIGKILL.',
50
64
  ' pugi jobs kill --all [--json] Stand down every running job.',
65
+ ' pugi jobs --watch Live Ink TUI of agent progress.',
51
66
  ].join('\n');
52
67
  }
53
68
  async function runList(flags, io) {
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Agent-progress auto-cleanup — α7 live progress (2026-05-27).
3
+ *
4
+ * Rule: a progress file whose status === 'completed' OR 'failed' AND
5
+ * whose `lastUpdate` is older than COMPLETION_TTL_MS (default 5 min)
6
+ * gets MOVED into `<dir>/archive/<id>-<ts>.json` rather than deleted.
7
+ * Operators sometimes want to inspect a finished agent's last state;
8
+ * the archive keeps that affordance while preventing the live watcher
9
+ * from cluttering up with stale rows.
10
+ *
11
+ * The function is pure-ish:
12
+ * - All clock reads go through the injected `now` (defaults Date.now).
13
+ * - Returns a structured report so the caller (the watcher or a CLI
14
+ * cron) can log what got swept.
15
+ * - File-system errors degrade silently — the cleanup is best-effort
16
+ * housekeeping, never a hot path.
17
+ *
18
+ * Hook-in point: `pugi jobs --watch` calls `runCleanup()` once per
19
+ * 60-second tick (cheap — readdir on a small directory). A separate
20
+ * cron entry can call it standalone via the (future) `pugi jobs
21
+ * gc` subcommand if the operator never runs --watch.
22
+ */
23
+ import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, } from 'node:fs';
24
+ import { join } from 'node:path';
25
+ import { validateAgentProgress, } from './schema.js';
26
+ import { resolveProgressDir } from './writer.js';
27
+ /** Default time a completed/failed entry sits before getting swept. */
28
+ export const COMPLETION_TTL_MS = 5 * 60 * 1000;
29
+ /** Archive subdirectory under the resolved progress dir. */
30
+ export const ARCHIVE_SUBDIR = 'archive';
31
+ /**
32
+ * Run a single cleanup pass. Returns the report for telemetry / tests.
33
+ */
34
+ export function runCleanup(options = {}) {
35
+ const dir = resolveProgressDir(options.dir);
36
+ const ttl = options.ttlMs ?? COMPLETION_TTL_MS;
37
+ const now = options.now ?? Date.now;
38
+ const report = { dir, archived: [], skipped: [] };
39
+ if (!existsSync(dir))
40
+ return report;
41
+ const archiveDir = join(dir, ARCHIVE_SUBDIR);
42
+ let entries;
43
+ try {
44
+ entries = readdirSync(dir);
45
+ }
46
+ catch (err) {
47
+ report.skipped.push({ path: dir, reason: `readdir failed: ${err.message}` });
48
+ return report;
49
+ }
50
+ for (const name of entries) {
51
+ if (!name.endsWith('.json'))
52
+ continue;
53
+ if (/\.tmp-/.test(name))
54
+ continue;
55
+ const path = join(dir, name);
56
+ let body;
57
+ try {
58
+ body = readFileSync(path, 'utf8');
59
+ }
60
+ catch {
61
+ report.skipped.push({ path, reason: 'read failed' });
62
+ continue;
63
+ }
64
+ let parsed;
65
+ try {
66
+ parsed = JSON.parse(body);
67
+ }
68
+ catch {
69
+ report.skipped.push({ path, reason: 'malformed JSON' });
70
+ continue;
71
+ }
72
+ const validation = validateAgentProgress(parsed);
73
+ if (!validation.ok) {
74
+ report.skipped.push({ path, reason: `invalid: ${validation.error}` });
75
+ continue;
76
+ }
77
+ const progress = validation.value;
78
+ if (!isExpired(progress, now(), ttl)) {
79
+ continue;
80
+ }
81
+ if (!existsSync(archiveDir)) {
82
+ try {
83
+ mkdirSync(archiveDir, { recursive: true });
84
+ }
85
+ catch (err) {
86
+ report.skipped.push({
87
+ path,
88
+ reason: `archive mkdir failed: ${err.message}`,
89
+ });
90
+ continue;
91
+ }
92
+ }
93
+ const target = join(archiveDir, `${progress.agentId}-${safeStamp(progress.lastUpdate)}.json`);
94
+ try {
95
+ renameSync(path, target);
96
+ report.archived.push({ agentId: progress.agentId, from: path, to: target });
97
+ }
98
+ catch (err) {
99
+ report.skipped.push({ path, reason: `rename failed: ${err.message}` });
100
+ }
101
+ }
102
+ return report;
103
+ }
104
+ /**
105
+ * Decide whether a single progress doc has aged out. Exported for the
106
+ * spec — kept pure so tests can probe edges (running entries never
107
+ * expire, completed entries before TTL stay, etc).
108
+ */
109
+ export function isExpired(progress, nowEpochMs, ttlMs = COMPLETION_TTL_MS) {
110
+ if (progress.status === 'running')
111
+ return false;
112
+ const lastTs = Date.parse(progress.lastUpdate);
113
+ if (Number.isNaN(lastTs))
114
+ return false;
115
+ return nowEpochMs - lastTs >= ttlMs;
116
+ }
117
+ // 2026-05-27 Claude review followup: `pruneArchive` was removed.
118
+ // Rationale (P1 in the Claude review batch on PR #512):
119
+ // - hardcoded `/tmp` path was POSIX-only (Windows would break),
120
+ // - `renameSync` across volumes throws EXDEV,
121
+ // - collision risk between concurrent hosts/sessions sharing `/tmp`,
122
+ // - and crucially, the function had ZERO call-sites in the codebase.
123
+ // Deleting it is strictly safer than leaving a broken-on-Windows
124
+ // dead helper that future refactors might accidentally wire up.
125
+ // If/when an archive GC is needed, the right shape is:
126
+ // - `os.tmpdir()` (not '/tmp'),
127
+ // - `rmSync(path, {force: true})` for cross-volume safe delete,
128
+ // - exposed as `pugi jobs gc` so we have an explicit caller.
129
+ function safeStamp(iso) {
130
+ // Build a filename-safe slug — strip colons/dots which are friendly
131
+ // в ISO timestamps but hostile к some filesystems.
132
+ return iso.replace(/[:.]/g, '-');
133
+ }
134
+ //# sourceMappingURL=cleanup.js.map
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Agent progress JSON schema — α7 live-progress sprint (2026-05-27).
3
+ *
4
+ * Long-running agents (spawned externally OR via `pugi /agent`) emit a
5
+ * single JSON document per agent to `~/.pugi/agent-progress/<id>.json`.
6
+ * `pugi jobs --watch` tails the directory via chokidar and re-renders
7
+ * an Ink TUI that mirrors the Claude Code `/compact` visual pattern
8
+ * (header with elapsed + token counter, unicode progress bar, milestone
9
+ * list with done/active/pending status icons).
10
+ *
11
+ * Schema is intentionally optimistic — every numeric field is clamped
12
+ * by the writer/reader so a malformed document degrades to a partial
13
+ * card instead of crashing the watcher. The `pendingCount` /
14
+ * `completedCount` fields are pre-computed by the agent for the
15
+ * "… +N pending, M completed" footer; the renderer never re-counts
16
+ * (the agent may have collapsed milestone history to save bytes).
17
+ */
18
+ /**
19
+ * Validate an unknown payload as an `AgentProgress` document. Returns
20
+ * the typed value on success and a string error otherwise. We deliberately
21
+ * keep this hand-rolled (no zod) — every field is checked exactly once,
22
+ * the error message is human-readable, and zero runtime deps.
23
+ */
24
+ export function validateAgentProgress(value) {
25
+ if (typeof value !== 'object' || value === null) {
26
+ return { ok: false, error: 'progress payload must be a JSON object' };
27
+ }
28
+ const raw = value;
29
+ const requiredString = (field) => {
30
+ const v = raw[field];
31
+ return typeof v === 'string' && v.length > 0 ? v : null;
32
+ };
33
+ const agentId = requiredString('agentId');
34
+ if (!agentId)
35
+ return { ok: false, error: 'agentId required (non-empty string)' };
36
+ if (!/^[a-zA-Z0-9_-]+$/.test(agentId)) {
37
+ return { ok: false, error: 'agentId must match [a-zA-Z0-9_-]+ (filename-safe)' };
38
+ }
39
+ const agentType = requiredString('agentType');
40
+ if (!agentType)
41
+ return { ok: false, error: 'agentType required (non-empty string)' };
42
+ const task = requiredString('task');
43
+ if (!task)
44
+ return { ok: false, error: 'task required (non-empty string)' };
45
+ const startedAt = requiredString('startedAt');
46
+ if (!startedAt)
47
+ return { ok: false, error: 'startedAt required (ISO string)' };
48
+ if (Number.isNaN(Date.parse(startedAt))) {
49
+ return { ok: false, error: 'startedAt must be a parseable ISO timestamp' };
50
+ }
51
+ const lastUpdate = requiredString('lastUpdate');
52
+ if (!lastUpdate)
53
+ return { ok: false, error: 'lastUpdate required (ISO string)' };
54
+ if (Number.isNaN(Date.parse(lastUpdate))) {
55
+ return { ok: false, error: 'lastUpdate must be a parseable ISO timestamp' };
56
+ }
57
+ const elapsedMs = raw.elapsedMs;
58
+ if (typeof elapsedMs !== 'number' || !Number.isFinite(elapsedMs) || elapsedMs < 0) {
59
+ return { ok: false, error: 'elapsedMs required (non-negative number)' };
60
+ }
61
+ const percentComplete = raw.percentComplete;
62
+ if (typeof percentComplete !== 'number' ||
63
+ !Number.isFinite(percentComplete)) {
64
+ return { ok: false, error: 'percentComplete required (number 0..100)' };
65
+ }
66
+ const clampedPercent = Math.max(0, Math.min(100, percentComplete));
67
+ const status = raw.status;
68
+ if (status !== 'running' && status !== 'completed' && status !== 'failed') {
69
+ return { ok: false, error: 'status must be running | completed | failed' };
70
+ }
71
+ const currentStep = raw.currentStep;
72
+ if (typeof currentStep !== 'number' || currentStep < 0) {
73
+ return { ok: false, error: 'currentStep required (non-negative number)' };
74
+ }
75
+ const totalSteps = raw.totalSteps;
76
+ if (typeof totalSteps !== 'number' || totalSteps < 0) {
77
+ return { ok: false, error: 'totalSteps required (non-negative number)' };
78
+ }
79
+ const stepDescription = typeof raw.stepDescription === 'string'
80
+ ? raw.stepDescription
81
+ : '';
82
+ const milestonesRaw = raw.milestones;
83
+ if (!Array.isArray(milestonesRaw)) {
84
+ return { ok: false, error: 'milestones required (array, may be empty)' };
85
+ }
86
+ const milestones = [];
87
+ for (let i = 0; i < milestonesRaw.length; i += 1) {
88
+ const m = milestonesRaw[i];
89
+ if (typeof m !== 'object' || m === null) {
90
+ return { ok: false, error: `milestones[${i}] must be an object` };
91
+ }
92
+ const mr = m;
93
+ const label = typeof mr.label === 'string' ? mr.label : '';
94
+ if (!label) {
95
+ return { ok: false, error: `milestones[${i}].label required` };
96
+ }
97
+ const mStatus = mr.status;
98
+ if (mStatus !== 'done' && mStatus !== 'active' && mStatus !== 'pending') {
99
+ return {
100
+ ok: false,
101
+ error: `milestones[${i}].status must be done | active | pending`,
102
+ };
103
+ }
104
+ const ts = typeof mr.ts === 'string' ? mr.ts : undefined;
105
+ milestones.push({ label, status: mStatus, ts });
106
+ }
107
+ const tokensUsed = typeof raw.tokensUsed === 'number' && Number.isFinite(raw.tokensUsed)
108
+ ? Math.max(0, raw.tokensUsed)
109
+ : undefined;
110
+ const pendingCount = typeof raw.pendingCount === 'number' && Number.isFinite(raw.pendingCount)
111
+ ? Math.max(0, Math.floor(raw.pendingCount))
112
+ : undefined;
113
+ const completedCount = typeof raw.completedCount === 'number' && Number.isFinite(raw.completedCount)
114
+ ? Math.max(0, Math.floor(raw.completedCount))
115
+ : undefined;
116
+ return {
117
+ ok: true,
118
+ value: {
119
+ agentId,
120
+ agentType,
121
+ task,
122
+ startedAt,
123
+ lastUpdate,
124
+ elapsedMs,
125
+ tokensUsed,
126
+ percentComplete: clampedPercent,
127
+ status,
128
+ currentStep,
129
+ totalSteps,
130
+ stepDescription,
131
+ milestones,
132
+ pendingCount,
133
+ completedCount,
134
+ },
135
+ };
136
+ }
137
+ /**
138
+ * Default directory the writer and watcher share. Lives under the
139
+ * project workspace by convention so worktree-isolated agents can emit
140
+ * progress without crossing tenant boundaries. Operators can override
141
+ * via `PUGI_AGENT_PROGRESS_DIR` env var.
142
+ */
143
+ export const DEFAULT_AGENT_PROGRESS_DIRNAME = '.pugi/agent-progress';
144
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Agent progress writer — atomic file ops so the chokidar watcher
3
+ * never reads a half-written JSON document.
4
+ *
5
+ * Pattern (POSIX-portable):
6
+ * 1. Build the canonical JSON body.
7
+ * 2. Write to `<dir>/<agentId>.json.tmp-<pid>-<seq>`.
8
+ * 3. Rename (atomic on the same filesystem) to `<dir>/<agentId>.json`.
9
+ * 4. Cleanup is a no-op on success; on failure the caller bubbles the
10
+ * error and the .tmp file is removed best-effort.
11
+ *
12
+ * The auto-cleanup of completed entries lives in `cleanup.ts` (commit 4);
13
+ * the writer here is the minimal surface the agent prompt boilerplate
14
+ * needs to copy + paste.
15
+ */
16
+ import { existsSync, mkdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
17
+ import { homedir } from 'node:os';
18
+ import { dirname, join, resolve } from 'node:path';
19
+ import { DEFAULT_AGENT_PROGRESS_DIRNAME, validateAgentProgress, } from './schema.js';
20
+ let writeSequence = 0;
21
+ /**
22
+ * Resolve the effective progress directory. Public so the watcher can
23
+ * share the same resolution rules without duplicating env-var logic.
24
+ */
25
+ export function resolveProgressDir(override) {
26
+ const candidate = override
27
+ ?? process.env.PUGI_AGENT_PROGRESS_DIR
28
+ ?? join(process.cwd(), DEFAULT_AGENT_PROGRESS_DIRNAME);
29
+ if (candidate.startsWith('~/')) {
30
+ return join(homedir(), candidate.slice(2));
31
+ }
32
+ return resolve(candidate);
33
+ }
34
+ /**
35
+ * Write a progress document atomically.
36
+ *
37
+ * Throws on validation failure so the caller is loud about a schema
38
+ * regression. File-system errors propagate untouched.
39
+ */
40
+ export function writeProgress(progress, options = {}) {
41
+ const validation = validateAgentProgress(progress);
42
+ if (!validation.ok) {
43
+ throw new Error(`invalid agent-progress payload: ${validation.error}`);
44
+ }
45
+ const dir = resolveProgressDir(options.dir);
46
+ if (!existsSync(dir)) {
47
+ mkdirSync(dir, { recursive: true });
48
+ }
49
+ const finalPath = join(dir, `${progress.agentId}.json`);
50
+ writeSequence += 1;
51
+ const tmpPath = `${finalPath}.tmp-${process.pid}-${writeSequence}`;
52
+ const body = `${JSON.stringify(progress, null, 2)}\n`;
53
+ try {
54
+ // mkdirSync above may have created `dir`, but a parallel agent could
55
+ // delete it between the existsSync and writeFile — ensure the parent
56
+ // of the tmp file exists right before the write.
57
+ const parent = dirname(tmpPath);
58
+ if (!existsSync(parent)) {
59
+ mkdirSync(parent, { recursive: true });
60
+ }
61
+ writeFileSync(tmpPath, body, 'utf8');
62
+ renameSync(tmpPath, finalPath);
63
+ }
64
+ catch (err) {
65
+ try {
66
+ rmSync(tmpPath, { force: true });
67
+ }
68
+ catch {
69
+ // best-effort cleanup; surface the original error
70
+ }
71
+ throw err;
72
+ }
73
+ return { path: finalPath };
74
+ }
75
+ /**
76
+ * Build a fresh progress document with sensible defaults. Mostly a
77
+ * convenience for tests + the agent prompt boilerplate — the writer
78
+ * does NOT call this implicitly because agents have richer context
79
+ * about their own milestone state.
80
+ */
81
+ export function makeInitialProgress(input) {
82
+ const now = input.now ?? Date.now;
83
+ const iso = new Date(now()).toISOString();
84
+ return {
85
+ agentId: input.agentId,
86
+ agentType: input.agentType,
87
+ task: input.task,
88
+ startedAt: iso,
89
+ lastUpdate: iso,
90
+ elapsedMs: 0,
91
+ percentComplete: 0,
92
+ status: 'running',
93
+ currentStep: 0,
94
+ totalSteps: Math.max(0, Math.floor(input.totalSteps)),
95
+ stepDescription: '',
96
+ milestones: input.milestones ?? [],
97
+ pendingCount: input.milestones?.filter((m) => m.status === 'pending').length,
98
+ completedCount: input.milestones?.filter((m) => m.status === 'done').length,
99
+ };
100
+ }
101
+ //# sourceMappingURL=writer.js.map
@@ -433,13 +433,19 @@ function parseArgs(raw) {
433
433
  throw new Error(`invalid JSON in tool arguments: ${error.message}`);
434
434
  }
435
435
  }
436
- function requireString(obj, key) {
437
- const v = obj[key];
438
- if (typeof v !== 'string') {
439
- throw new Error(`tool argument "${key}" must be a string`);
436
+ function requireString(obj, key, aliases = []) {
437
+ // 2026-05-27 CEO live smoke: model passed `file: "index.html"` to write tool
438
+ // but spec required `path:`. Tool rejected → silent no-op → file не созданы.
439
+ // Accept common aliases so models trained на various tool schemas just work.
440
+ const tryKeys = [key, ...aliases];
441
+ for (const k of tryKeys) {
442
+ const v = obj[k];
443
+ if (typeof v === 'string')
444
+ return v;
440
445
  }
441
- return v;
446
+ throw new Error(`tool argument "${key}" must be a string`);
442
447
  }
448
+ const PATH_ALIASES = ['file', 'filename', 'filepath', 'file_path'];
443
449
  export function buildExecutor(input) {
444
450
  const { kind, ctx, hooks, sessionId, askUserBridge, interactive, allowFetch, allowSearch, agentDispatch, mcpRegistry } = input;
445
451
  const mcpPrompt = input.mcpPrompt ?? defaultNonInteractiveMcpPrompt;
@@ -637,7 +643,7 @@ function extractToolPath(name, argsRaw) {
637
643
  function dispatchTool(name, args, ctx) {
638
644
  switch (name) {
639
645
  case 'read': {
640
- const { path } = { path: requireString(args, 'path') };
646
+ const { path } = { path: requireString(args, 'path', PATH_ALIASES) };
641
647
  const content = readTool(ctx, path);
642
648
  // Cap the content surfaced back to the model so a 10MB file
643
649
  // does not blow the context window. The model sees the head
@@ -650,7 +656,7 @@ function dispatchTool(name, args, ctx) {
650
656
  }
651
657
  case 'write': {
652
658
  const wargs = {
653
- path: requireString(args, 'path'),
659
+ path: requireString(args, 'path', PATH_ALIASES),
654
660
  content: requireString(args, 'content'),
655
661
  };
656
662
  writeTool(ctx, wargs.path, wargs.content);
@@ -658,7 +664,7 @@ function dispatchTool(name, args, ctx) {
658
664
  }
659
665
  case 'edit': {
660
666
  const eargs = {
661
- path: requireString(args, 'path'),
667
+ path: requireString(args, 'path', PATH_ALIASES),
662
668
  oldString: requireString(args, 'oldString'),
663
669
  newString: requireString(args, 'newString'),
664
670
  };
@@ -2604,7 +2604,7 @@ export function synthesiseToolCall(input) {
2604
2604
  // Pattern: ToolName(args) optionally suffixed with a result hint.
2605
2605
  // We allow the canonical Claude Code casing AND the snake_case
2606
2606
  // alias `web_fetch` so the synthesiser matches what personas write.
2607
- const match = /^(Read|Edit|Bash|Grep|Glob|WebFetch|web_fetch)\s*\(\s*([^)]*)\s*\)\s*(.*)$/i
2607
+ const match = /^(Read|Write|Edit|Bash|Grep|Glob|WebFetch|web_fetch)\s*\(\s*([^)]*)\s*\)\s*(.*)$/i
2608
2608
  .exec(detail);
2609
2609
  if (!match)
2610
2610
  return null;
@@ -2628,6 +2628,8 @@ function normaliseToolName(raw) {
2628
2628
  return 'web_fetch';
2629
2629
  if (lower === 'read')
2630
2630
  return 'read';
2631
+ if (lower === 'write')
2632
+ return 'write';
2631
2633
  if (lower === 'edit')
2632
2634
  return 'edit';
2633
2635
  if (lower === 'bash')
@@ -2853,7 +2855,22 @@ export function stripPersonaPrefixEcho(personaSlug, text) {
2853
2855
  // Escape regex specials in the display name even though THE_TEN
2854
2856
  // names are alpha-only today (forward-defense).
2855
2857
  const escaped = display.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
2858
+ // Match `<DisplayName>` (case-insensitive) followed by EITHER:
2859
+ // - an end-of-string, OR
2860
+ // - a separator (whitespace / comma / colon / dash / period+space).
2861
+ // The `i` flag is needed so a model writing "PUGI:" or "pugi," still
2862
+ // strips. After this match the post-fix `noSepUppercaseRe` handles
2863
+ // the "PugiПринял" / "PugiHello" no-separator emission pattern
2864
+ // (CEO red-alert 2026-05-27) using a SEPARATE regex without the `i`
2865
+ // flag so the lookahead is case-strict (Pugineous must NOT strip).
2856
2866
  const re = new RegExp(`^${escaped}(?:[\\s,:;\\-—–]+|$)`, 'i');
2867
+ // No-separator case-strict matcher. Display name in either of its
2868
+ // canonical casings ("Pugi" / "PUGI") immediately followed by an
2869
+ // uppercase Cyrillic or Latin letter. The strip is intentionally
2870
+ // narrower than the case-insensitive `re` above because a lowercase
2871
+ // continuation ("Pugineous") is a single word, not a display-name
2872
+ // echo - we must not eat real content.
2873
+ const noSepUppercaseRe = new RegExp(`^(?:${escaped}|${escaped.toUpperCase()})(?=[А-ЯЁA-Z])`);
2857
2874
  // Loop the strip so cascading echoes ("Pugi Pugi Pugi, координатор ...")
2858
2875
  // collapse to a single name. The model occasionally emits the display
2859
2876
  // name two or three times back-to-back when the pane prefix also
@@ -2865,10 +2882,18 @@ export function stripPersonaPrefixEcho(personaSlug, text) {
2865
2882
  // matches an empty string (defence-in-depth even though the current
2866
2883
  // pattern guarantees at least one consumed char).
2867
2884
  for (let i = 0; i < 3; i += 1) {
2868
- const m = re.exec(working);
2869
- if (!m || m[0].length === 0)
2870
- break;
2871
- working = working.slice(m[0].length).trimStart();
2885
+ let m = re.exec(working);
2886
+ if (m && m[0].length > 0) {
2887
+ working = working.slice(m[0].length).trimStart();
2888
+ continue;
2889
+ }
2890
+ // Fallback: no-separator match for "PugiПринял" / "PugiHello" shape.
2891
+ m = noSepUppercaseRe.exec(working);
2892
+ if (m && m[0].length > 0) {
2893
+ working = working.slice(m[0].length);
2894
+ continue;
2895
+ }
2896
+ break;
2872
2897
  }
2873
2898
  return working;
2874
2899
  }
@@ -29,6 +29,7 @@ import { runDeployCommand } from '../commands/deploy.js';
29
29
  import { runJobsCommand } from '../commands/jobs.js';
30
30
  import { runConfigCommand } from './commands/config.js';
31
31
  import { runPrivacyCommand } from './commands/privacy.js';
32
+ import { runReport } from './commands/report.js';
32
33
  import { runUndoCommand } from './commands/undo.js';
33
34
  import { runBudgetCommand } from './commands/budget.js';
34
35
  import { runSkillsCommand } from './commands/skills.js';
@@ -90,6 +91,10 @@ const handlers = {
90
91
  plan: runEngineTask('plan'),
91
92
  'plan-review': dispatchPlanReview,
92
93
  privacy: dispatchPrivacy,
94
+ // PAVF-7 (2026-05-27): `pugi report --from-error` captures the
95
+ // most-recent failed session as a redacted bundle so operators can
96
+ // file clean bug reports without manual log-grepping.
97
+ report: dispatchReport,
93
98
  review,
94
99
  resume,
95
100
  roster: dispatchRoster,
@@ -271,6 +276,25 @@ async function dispatchPrivacy(args, flags, _session) {
271
276
  writeOutput: (payload, text) => writeOutput(flags, payload, text),
272
277
  });
273
278
  }
279
+ /**
280
+ * PAVF-7 (2026-05-27): `pugi report --from-error` — bundle the most-
281
+ * recent failed session into a redacted local report so operators can
282
+ * file clean bug tickets without manual log-grepping. v1 is local-only
283
+ * (no auto-upload — see commands/report.ts header for the rationale).
284
+ */
285
+ async function dispatchReport(args, flags, _session) {
286
+ const rc = runReport(args, {
287
+ cwd: process.cwd(),
288
+ json: flags.json,
289
+ emit: (line) => {
290
+ if (!flags.json)
291
+ process.stdout.write(line);
292
+ },
293
+ writeOutput: (payload, text) => writeOutput(flags, payload, text),
294
+ });
295
+ if (rc !== 0)
296
+ process.exitCode = rc;
297
+ }
274
298
  /**
275
299
  * `pugi roster` - α7.5 Phase 1.
276
300
  *
@@ -973,6 +997,16 @@ const COMMAND_HELP_BODIES = {
973
997
  'event log, settings), permission mode, and the capability matrix per',
974
998
  'engine adapter. Safe to run anywhere; no network calls.',
975
999
  ],
1000
+ report: [
1001
+ 'pugi report — capture a bug report from the most-recent session.',
1002
+ '',
1003
+ ' --from-error Bundle the most-recent failed session as a',
1004
+ ' redacted local report (default + only mode in v1).',
1005
+ '',
1006
+ 'Output: writes .pugi/reports/<timestamp>-<session-id>/{report.json, report.md}.',
1007
+ 'Secrets (bearer tokens, JWTs, named env values) are stripped before disk write.',
1008
+ 'Auto-upload to api.pugi.io planned for a follow-up; v1 keeps everything local.',
1009
+ ],
976
1010
  ask: [
977
1011
  'pugi ask "<question>" — surface a yes/no question modal locally.',
978
1012
  '',
@@ -2140,12 +2174,45 @@ async function performTripleProviderReview(root, session, flags, prompt) {
2140
2174
  `Refusing to submit an empty diff for review.`);
2141
2175
  }
2142
2176
  const resolvedCommit = safeGit(root, ['rev-parse', '--short', commitRef]).trim() || commitRef;
2143
- const mergeBase = safeGit(root, ['merge-base', baseRef, commitRef]).trim() || '';
2177
+ // merge-base is intentionally a PROBE: an empty result is a valid
2178
+ // signal (orphan branch, shallow clone, moved tag) that the dispatch
2179
+ // path handles by falling back к range-notation. Use the legacy
2180
+ // `safeGit` (probe semantics) explicitly rather than the strict
2181
+ // variant.
2182
+ const mergeBase = safeGitProbe(root, ['merge-base', baseRef, commitRef]).trim() || '';
2183
+ // 2026-05-27 (Claude review followup #489): when merge-base returns empty
2184
+ // (orphan branch, shallow clone, moved tag), we MUST NOT pass the
2185
+ // `<range> <commitRef>` two-arg form to `git diff` — that combo is
2186
+ // invalid syntax, git exits 129, `safeGit` swallows the error, and the
2187
+ // diff payload ships empty. An empty diff is then classified as
2188
+ // `'code'` server-side, dispatched to reviewers who emit a trivial
2189
+ // `VERDICT: PASS` over zero lines — a SILENT GREEN REVIEW on a commit
2190
+ // nobody actually examined. Branch on `mergeBase` так что:
2191
+ // - mergeBase present → `git diff <mergeBase> <commitRef> --`
2192
+ // (both endpoints explicit, only-uncommitted-against-base ignored
2193
+ // because commitRef is a SHA, not HEAD).
2194
+ // - mergeBase empty → `git diff <baseRef>..<commitRef> --`
2195
+ // (range form encodes both endpoints; do NOT append commitRef
2196
+ // again or git rejects the args).
2144
2197
  const diffRange = mergeBase || `${baseRef}..${commitRef}`;
2145
- const diffArgs = ['diff', diffRange, commitRef, '--', '.', ...PROTECTED_DIFF_EXCLUDES];
2146
- const diffStatArgs = ['diff', '--shortstat', diffRange, commitRef, '--', '.', ...PROTECTED_DIFF_EXCLUDES];
2147
- const diffPatch = safeGit(root, diffArgs);
2148
- const diffStats = parseDiffStats(safeGit(root, diffStatArgs));
2198
+ const diffArgs = mergeBase
2199
+ ? ['diff', mergeBase, commitRef, '--', '.', ...PROTECTED_DIFF_EXCLUDES]
2200
+ : ['diff', diffRange, '--', '.', ...PROTECTED_DIFF_EXCLUDES];
2201
+ const diffStatArgs = mergeBase
2202
+ ? ['diff', '--shortstat', mergeBase, commitRef, '--', '.', ...PROTECTED_DIFF_EXCLUDES]
2203
+ : ['diff', '--shortstat', diffRange, '--', '.', ...PROTECTED_DIFF_EXCLUDES];
2204
+ // Use the strict variant — a non-empty diffPatch is load-bearing for
2205
+ // the review gate. If git fails for ANY reason (bad ref, ENOBUFS, FS
2206
+ // permission), we'd rather surface a hard error than ship a green
2207
+ // review on nothing. The `--shortstat` companion uses the same
2208
+ // helper so the throw is symmetric.
2209
+ const diffPatch = safeGitRequired(root, diffArgs, 'triple-providers diff');
2210
+ const diffStats = parseDiffStats(safeGitRequired(root, diffStatArgs, 'triple-providers diff --shortstat'));
2211
+ if (diffPatch.trim() === '') {
2212
+ throw new Error(`pugi review --triple: empty diff between '${baseRef}' and '${commitRef}'. ` +
2213
+ `Refusing to dispatch a review for zero changes — check the refs ` +
2214
+ `or commit your changes before running.`);
2215
+ }
2149
2216
  const requestBody = pugiTripleReviewRequestSchema.parse({
2150
2217
  schema: 1,
2151
2218
  workspace: {
@@ -5039,7 +5106,31 @@ function fileBytes(path) {
5039
5106
  return 0;
5040
5107
  }
5041
5108
  }
5042
- function safeGit(root, args) {
5109
+ /**
5110
+ * Git invocation helpers — probe vs required semantics.
5111
+ *
5112
+ * 2026-05-27 (Claude review followup #489): the historical `safeGit`
5113
+ * collapsed BOTH "tell me the branch name if you can" probes AND
5114
+ * "give me the diff or fail" hard requirements into a single helper
5115
+ * that swallowed every error as an empty string. That's the correct
5116
+ * shape for the probe case (branch / status / dirty flag — empty
5117
+ * result is a valid signal) but catastrophically wrong for the diff
5118
+ * case (empty result === false PASS on a commit nobody reviewed).
5119
+ *
5120
+ * The split:
5121
+ * - `safeGitProbe` — best-effort. Returns '' on any error. Use for
5122
+ * branch name lookups, status probes, opt-in dirty detection.
5123
+ * - `safeGitRequired` — throws on non-zero exit / ENOBUFS / bad ref.
5124
+ * Use for diff, merge-base resolution, anything whose empty
5125
+ * output would silently corrupt downstream behaviour.
5126
+ *
5127
+ * Legacy `safeGit` is kept as a deprecated alias of `safeGitProbe`
5128
+ * so existing call-sites (branch detection, status, etc.) keep their
5129
+ * tolerant semantics until they are individually migrated. Diff /
5130
+ * merge-base / rev-parse-verify call-sites are migrated к
5131
+ * `safeGitRequired` in this same patch.
5132
+ */
5133
+ export function safeGitProbe(root, args) {
5043
5134
  try {
5044
5135
  return execFileSync('git', args, {
5045
5136
  cwd: root,
@@ -5057,6 +5148,38 @@ function safeGit(root, args) {
5057
5148
  return '';
5058
5149
  }
5059
5150
  }
5151
+ /**
5152
+ * Strict variant — throws on non-zero exit, ENOBUFS, or any git-side
5153
+ * failure. The thrown error carries the operation context so the
5154
+ * caller (triple-review dispatch, etc.) can fail loud rather than
5155
+ * ship an empty diff to a remote reviewer.
5156
+ */
5157
+ export function safeGitRequired(root, args, context) {
5158
+ try {
5159
+ return execFileSync('git', args, {
5160
+ cwd: root,
5161
+ encoding: 'utf8',
5162
+ stdio: ['ignore', 'pipe', 'pipe'],
5163
+ maxBuffer: 64 * 1024 * 1024,
5164
+ });
5165
+ }
5166
+ catch (err) {
5167
+ const cause = err instanceof Error ? err.message : String(err);
5168
+ throw new Error(`git ${args.slice(0, 2).join(' ')} failed (${context}): ${cause}. ` +
5169
+ `Refusing to proceed — empty git output here would corrupt downstream behaviour.`);
5170
+ }
5171
+ }
5172
+ /**
5173
+ * Deprecated alias preserved for diff / status / branch probes that
5174
+ * legitimately want a tolerant empty-string-on-error shape. New call
5175
+ * sites should pick `safeGitProbe` or `safeGitRequired` explicitly.
5176
+ *
5177
+ * @deprecated 2026-05-27 — prefer `safeGitProbe` (tolerant) or
5178
+ * `safeGitRequired` (strict, throws).
5179
+ */
5180
+ function safeGit(root, args) {
5181
+ return safeGitProbe(root, args);
5182
+ }
5060
5183
  /**
5061
5184
  * Glob patterns excluded from triple-review `diffPatch` before egress.
5062
5185
  *
@@ -44,7 +44,7 @@ export function sanitizeSemver(raw) {
44
44
  * during import). When bumping the CLI version BOTH literals must be
45
45
  * updated; the release smoke-test (`pack:smoke`) verifies they agree.
46
46
  */
47
- export const PUGI_CLI_VERSION = sanitizeSemver('0.1.0-beta.16');
47
+ export const PUGI_CLI_VERSION = sanitizeSemver('0.1.0-beta.17');
48
48
  /**
49
49
  * Outbound: the CLI's installed semver. Read at request time by
50
50
  * `version-interceptor.ts` and injected on every `fetch` call.
@@ -1,3 +1,31 @@
1
+ /**
2
+ * file-tools - Pugi CLI file/bash/glob/grep tool surface.
3
+ *
4
+ * Workspace-binding contract (CEO red-alert 2026-05-27 follow-up):
5
+ *
6
+ * Every tool dispatch path threads `ctx.root` from the operator's
7
+ * `process.cwd()` through `EngineTask.workspaceRoot` ->
8
+ * `native-pugi.run()` -> `toolCtx.root` -> here. Tools call
9
+ * `resolveWorkspacePath(ctx.root, path)` for every on-disk operation
10
+ * so a dispatched specialist (e.g. Hiroshi writing tic-tac-toe HTML)
11
+ * produces files in the OPERATOR'S cwd, never in a server-side temp
12
+ * space. The path-security gate refuses traversal (`../etc/passwd`,
13
+ * URL-encoded variants, symlink escapes at the target).
14
+ *
15
+ * Wiring chain:
16
+ * 1. runtime/cli.ts: workspaceRoot = process.cwd()
17
+ * 2. EngineTask.workspaceRoot threads through to native-pugi.run().
18
+ * 3. native-pugi: const root = task.workspaceRoot
19
+ * 4. tool-bridge: passes ctx.root to file-tools / bash.
20
+ * 5. file-tools: resolveWorkspacePath(ctx.root, path).
21
+ *
22
+ * The contract is locked by `test/tools-write-to-workspace.spec.ts`
23
+ * (6 cases covering relative + nested + absolute paths + traversal
24
+ * refusal). If any layer of the chain regressed silently, dispatched
25
+ * files would land in `/tmp` instead of the operator's repo, which
26
+ * is the same failure surface as the menu-mode anti-pattern the
27
+ * sibling commits close.
28
+ */
1
29
  import { spawnSync } from 'node:child_process';
2
30
  import { existsSync, readFileSync, realpathSync, renameSync, writeFileSync } from 'node:fs';
3
31
  import { dirname, isAbsolute, relative } from 'node:path';
@@ -0,0 +1,111 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Box, Text } from 'ink';
3
+ /** Width of the progress bar in display cells. Tuned to fit comfortably
4
+ * inside an 80-col terminal alongside the percent label. */
5
+ export const PROGRESS_BAR_WIDTH = 24;
6
+ /** Max milestone rows the card renders before collapsing to the footer
7
+ * summary. Matches the CC `/compact` cutoff. */
8
+ export const MAX_VISIBLE_MILESTONES = 5;
9
+ const STATUS_GLYPH = {
10
+ done: '◼',
11
+ active: '▸',
12
+ pending: '◻',
13
+ };
14
+ const STATUS_COLOR = {
15
+ done: 'green',
16
+ active: 'yellow',
17
+ pending: 'gray',
18
+ };
19
+ const HEADER_DOT_COLOR = {
20
+ running: 'cyan',
21
+ completed: 'green',
22
+ failed: 'red',
23
+ };
24
+ /**
25
+ * Build the unicode progress bar. Exported для тесты — guarantees the
26
+ * filled/empty counts match the percent under all rounding edges.
27
+ */
28
+ export function renderProgressBarCells(percent, width = PROGRESS_BAR_WIDTH) {
29
+ const safePercent = Math.max(0, Math.min(100, percent));
30
+ const cells = Math.round((safePercent / 100) * width);
31
+ const clamped = Math.max(0, Math.min(width, cells));
32
+ return {
33
+ filled: '▰'.repeat(clamped),
34
+ empty: '▱'.repeat(width - clamped),
35
+ cells: clamped,
36
+ };
37
+ }
38
+ /**
39
+ * Format milliseconds as the CC-style `Hh Mm Ss` / `Mm Ss` / `Ss` label.
40
+ * Mirrors the rule used by status-bar elapsed slot.
41
+ */
42
+ export function formatElapsed(ms) {
43
+ const total = Math.max(0, Math.floor(ms / 1000));
44
+ const h = Math.floor(total / 3600);
45
+ const m = Math.floor((total % 3600) / 60);
46
+ const s = total % 60;
47
+ if (h > 0)
48
+ return `${h}h ${m}m ${s}s`;
49
+ if (m > 0)
50
+ return `${m}m ${s}s`;
51
+ return `${s}s`;
52
+ }
53
+ /**
54
+ * Format a raw token count as `21.7k` / `3.4M` / `812`. Mirrors the
55
+ * formatter in `core/repl/model-pricing.ts` so both surfaces stay
56
+ * visually consistent without coupling.
57
+ */
58
+ export function formatTokenCount(n) {
59
+ if (n === undefined)
60
+ return undefined;
61
+ if (n < 1_000)
62
+ return `${n}`;
63
+ if (n < 1_000_000) {
64
+ const k = n / 1_000;
65
+ return `${k >= 10 ? k.toFixed(1).replace(/\.0$/, '') : k.toFixed(1)}k`;
66
+ }
67
+ const m = n / 1_000_000;
68
+ return `${m >= 10 ? m.toFixed(1).replace(/\.0$/, '') : m.toFixed(1)}M`;
69
+ }
70
+ /**
71
+ * Compute the "… +N pending, M completed" footer counts. When the
72
+ * agent supplied rollups they win; otherwise we derive from the
73
+ * milestone array.
74
+ */
75
+ export function computeFooterCounts(milestones, visibleCount, rollup) {
76
+ const pending = rollup.pendingCount
77
+ ?? milestones.filter((m) => m.status === 'pending').length;
78
+ const completed = rollup.completedCount
79
+ ?? milestones.filter((m) => m.status === 'done').length;
80
+ const hidden = Math.max(0, milestones.length - visibleCount);
81
+ return { pending, completed, hidden };
82
+ }
83
+ function MilestoneRow({ milestone }) {
84
+ const glyph = STATUS_GLYPH[milestone.status];
85
+ const color = STATUS_COLOR[milestone.status];
86
+ // Truncate to 64 chars so a verbose label can't wrap and break the
87
+ // grid layout in the watcher.
88
+ const label = milestone.label.length > 64
89
+ ? `${milestone.label.slice(0, 63)}…`
90
+ : milestone.label;
91
+ return (_jsxs(Box, { children: [_jsx(Text, { children: ' ' }), _jsx(Text, { color: color, children: glyph }), _jsx(Text, { children: " " }), _jsx(Text, { color: color === 'gray' ? 'gray' : undefined, dimColor: milestone.status === 'pending', children: label })] }));
92
+ }
93
+ export function AgentProgressCard({ progress, nowEpochMs, }) {
94
+ // Re-derive elapsed from the wall clock when the parent supplied it;
95
+ // this is what makes the card tick once a second without the writer
96
+ // re-emitting JSON every tick.
97
+ const elapsed = nowEpochMs !== undefined
98
+ ? Math.max(progress.elapsedMs, nowEpochMs - Date.parse(progress.startedAt))
99
+ : progress.elapsedMs;
100
+ const bar = renderProgressBarCells(progress.percentComplete);
101
+ const percentLabel = `${Math.round(Math.max(0, Math.min(100, progress.percentComplete)))}%`;
102
+ const tokensLabel = formatTokenCount(progress.tokensUsed);
103
+ const dotColor = HEADER_DOT_COLOR[progress.status];
104
+ const visibleMilestones = progress.milestones.slice(0, MAX_VISIBLE_MILESTONES);
105
+ const footer = computeFooterCounts(progress.milestones, visibleMilestones.length, { pendingCount: progress.pendingCount, completedCount: progress.completedCount });
106
+ // CC compact pattern: header has a leading `· ` glyph + the task label.
107
+ // We append `…` only while running (matches CC's "Compacting…" verb form).
108
+ const headerVerb = progress.status === 'running' ? '…' : '';
109
+ return (_jsxs(Box, { flexDirection: "column", paddingX: 1, paddingY: 0, marginBottom: 1, children: [_jsxs(Box, { children: [_jsx(Text, { color: dotColor, children: '· ' }), _jsx(Text, { bold: true, children: progress.agentType }), _jsx(Text, { children: " " }), _jsxs(Text, { children: [progress.task, headerVerb] }), _jsxs(Text, { dimColor: true, children: [' (', formatElapsed(elapsed), tokensLabel ? ` · ↑ ${tokensLabel} tokens` : '', ')'] })] }), _jsxs(Box, { children: [_jsx(Text, { children: ' ' }), _jsx(Text, { color: "cyan", children: bar.filled }), _jsx(Text, { dimColor: true, children: bar.empty }), _jsxs(Text, { children: [' ', percentLabel] })] }), progress.stepDescription ? (_jsxs(Box, { children: [_jsx(Text, { children: ' ' }), _jsxs(Text, { dimColor: true, children: ["step ", progress.currentStep, "/", progress.totalSteps, ": ", progress.stepDescription] })] })) : null, visibleMilestones.length > 0 ? (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { children: [_jsx(Text, { children: ' ' }), _jsx(Text, { dimColor: true, children: "\u23BF" })] }), visibleMilestones.map((m, i) => (_jsx(MilestoneRow, { milestone: m }, `${m.label}-${i}`))), footer.hidden > 0 ? (_jsxs(Box, { children: [_jsx(Text, { children: ' ' }), _jsxs(Text, { dimColor: true, children: ["\u2026 +", footer.pending, " pending, ", footer.completed, " completed"] })] })) : null] })) : null] }));
110
+ }
111
+ //# sourceMappingURL=agent-progress-card.js.map
@@ -64,6 +64,13 @@ function toolDisplayName(tool) {
64
64
  switch (tool) {
65
65
  case 'read':
66
66
  return 'Read';
67
+ case 'write':
68
+ // 2026-05-27 — Write is the most operator-visible tool for the
69
+ // codegen-dispatch surface (Hiroshi writing index.html / style.css
70
+ // / script.js for a tic-tac-toe brief). Add the display name so
71
+ // the tool stream pane renders ✓ Write(index.html) instead of an
72
+ // unlabeled placeholder. Mirrors the Claude Code Write rendering.
73
+ return 'Write';
67
74
  case 'edit':
68
75
  return 'Edit';
69
76
  case 'bash':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pugi/cli",
3
- "version": "0.1.0-beta.16",
3
+ "version": "0.1.0-beta.17",
4
4
  "description": "Pugi CLI - terminal-native software execution system",
5
5
  "homepage": "https://pugi.io",
6
6
  "repository": {
@@ -54,7 +54,7 @@
54
54
  "undici": "^8.3.0",
55
55
  "zod": "^3.23.0",
56
56
  "@pugi/personas": "0.1.2",
57
- "@pugi/sdk": "0.1.0-beta.16"
57
+ "@pugi/sdk": "0.1.0-beta.17"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@types/node": "^22.0.0",