@pugi/cli 0.1.0-beta.15 → 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
  };