@venturewild/workspace 0.6.15 → 0.6.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@venturewild/workspace",
3
- "version": "0.6.15",
3
+ "version": "0.6.17",
4
4
  "description": "Claude Code Web — Replit/Lovable-style chat-first browser UI that wraps the AI agent already installed on your machine.",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -0,0 +1,322 @@
1
+ // Background-task registry — the "what's running in the background" surface.
2
+ //
3
+ // WHY this exists: the agent can launch a long job with the Bash tool's
4
+ // `run_in_background:true` (e.g. "run the scored-verdict audit in the background").
5
+ // We verified empirically how `claude` behaves here:
6
+ // - The launch tool_result is text: `Command running in background with ID:
7
+ // <shellId>. Output is being written to: <file>. You will be notified…`.
8
+ // - The task OUTLIVES the `claude -p` turn that spawned it (it's detached) — the
9
+ // output file keeps growing on disk after the process exits.
10
+ // - BUT in our one-process-per-turn model there is NO completion event delivered
11
+ // back into the stream, and a resumed session can't re-poll the old shell. So
12
+ // "I'll be notified when it finishes" never actually fires for the user.
13
+ // - Completion IS visible IF the agent polls in the same turn: the poll tool
14
+ // (newer `TaskOutput` w/ `task_id`, older `BashOutput` w/ `bash_id`) returns
15
+ // `<status>completed</status>` + `<exit_code>N</exit_code>`.
16
+ //
17
+ // So this registry fuses two signals, both of which the server already sees or can
18
+ // reach: (1) the agent's tool-use/tool-result chunk stream (launch + any polls) and
19
+ // (2) the on-disk output file's size/mtime (process-independent liveness). It is the
20
+ // single source of truth behind the chat's background-task card, the topbar activity
21
+ // tray, and the canvas Tasks block.
22
+ //
23
+ // HONESTY (CLAUDE.md: never fake): we only assert `completed`/`failed` from an
24
+ // authoritative poll result or an exit-code we actually observed. Without that, a
25
+ // task is `running`, optionally flagged `quiet` when the output file has been
26
+ // silent past a threshold — surfaced as "no output for Xm", never a false "done".
27
+
28
+ import fs from 'node:fs';
29
+
30
+ const DEFAULT_POLL_MS = 3000; // how often we stat each live task's output file
31
+ const DEFAULT_QUIET_MS = 90_000; // silence past this → flag `quiet` (maybe stuck / maybe just slow)
32
+ const DEFAULT_MAX_PER_WS = 40; // retained tasks per workspace (terminal ones pruned oldest-first)
33
+ const TAIL_BYTES = 16 * 1024; // how much of the output file the tail endpoint returns
34
+
35
+ const TERMINAL = new Set(['completed', 'failed', 'killed']);
36
+
37
+ /** Parse a background-launch tool_result into { shellId, outputFile }. */
38
+ export function parseLaunchResult(text) {
39
+ if (typeof text !== 'string') return null;
40
+ const idM = text.match(/background with ID:\s*([A-Za-z0-9_-]+)/i);
41
+ if (!idM) return null;
42
+ const fileM = text.match(/written to:\s*(\S+)/i);
43
+ let outputFile = fileM ? fileM[1] : null;
44
+ // The path is a contiguous no-space token; the sentence's trailing punctuation
45
+ // ("…/x.output. You will be notified") clings to it — strip it (output files end
46
+ // in letters, so this never eats a real filename char).
47
+ if (outputFile) outputFile = outputFile.replace(/[.,;:]+$/, '');
48
+ return { shellId: idM[1], outputFile: outputFile || null };
49
+ }
50
+
51
+ /** Parse a poll tool_result (`TaskOutput`/`BashOutput`) into { status, exitCode }. */
52
+ export function parsePollResult(text) {
53
+ if (typeof text !== 'string') return null;
54
+ const sM = text.match(/<status>\s*([a-z_]+)\s*<\/status>/i);
55
+ if (!sM) return null;
56
+ const raw = sM[1].toLowerCase();
57
+ const eM = text.match(/<exit_code>\s*(-?\d+)\s*<\/exit_code>/i);
58
+ const exitCode = eM ? Number(eM[1]) : null;
59
+ let status = 'running';
60
+ if (raw === 'completed' || raw === 'done' || raw === 'finished') {
61
+ status = exitCode !== null && exitCode !== 0 ? 'failed' : 'completed';
62
+ } else if (raw === 'killed' || raw === 'stopped') {
63
+ status = 'killed';
64
+ } else if (raw === 'failed' || raw === 'error') {
65
+ status = 'failed';
66
+ }
67
+ return { status, exitCode };
68
+ }
69
+
70
+ // The shell id a poll/kill tool call targets, across the new + old vocabularies.
71
+ function targetShellId(input) {
72
+ if (!input || typeof input !== 'object') return null;
73
+ return input.task_id || input.bash_id || input.shell_id || null;
74
+ }
75
+
76
+ const POLL_TOOLS = new Set(['TaskOutput', 'BashOutput']);
77
+ const KILL_TOOLS = new Set(['TaskStop', 'KillShell', 'KillBash']);
78
+
79
+ /**
80
+ * @param {object} opts
81
+ * @param {(task:object)=>void} [opts.broadcast] called with the public task view on every change
82
+ * @param {()=>number} [opts.now]
83
+ * @param {number} [opts.pollMs]
84
+ * @param {number} [opts.quietMs]
85
+ * @param {number} [opts.maxPerWorkspace]
86
+ * @param {object} [opts.fsImpl] injectable fs (tests)
87
+ */
88
+ export function createBackgroundTasks(opts = {}) {
89
+ const broadcast = typeof opts.broadcast === 'function' ? opts.broadcast : () => {};
90
+ const now = opts.now || Date.now;
91
+ const pollMs = opts.pollMs || DEFAULT_POLL_MS;
92
+ const quietMs = opts.quietMs || DEFAULT_QUIET_MS;
93
+ const maxPerWs = opts.maxPerWorkspace || DEFAULT_MAX_PER_WS;
94
+ const fsi = opts.fsImpl || fs;
95
+
96
+ // taskId (= the LAUNCH tool_use id) → task. Keying by the launch id (not the
97
+ // shellId) means the chat card, which is created at launch time and only knows the
98
+ // tool_use id, can correlate to live updates without a round-trip.
99
+ const tasks = new Map();
100
+ const byShell = new Map(); // shellId → taskId (for poll/kill correlation)
101
+ const pollTargets = new Map(); // poll tool_use id → shellId (result arrives later, idless)
102
+
103
+ let timer = null;
104
+
105
+ function publicView(t) {
106
+ return {
107
+ id: t.id,
108
+ shellId: t.shellId,
109
+ command: t.command,
110
+ description: t.description,
111
+ status: t.status,
112
+ exitCode: t.exitCode,
113
+ quiet: t.quiet,
114
+ startedAt: t.startedAt,
115
+ endedAt: t.endedAt,
116
+ lastOutputAt: t.lastOutputAt,
117
+ hasOutput: t.everExisted,
118
+ workspaceId: t.workspaceId,
119
+ threadId: t.threadId,
120
+ // NB: the absolute outputFile path is deliberately NOT exposed (it leaks the
121
+ // home dir — same rule the Logs route follows). Tail it via the by-id endpoint.
122
+ };
123
+ }
124
+
125
+ function emit(t) {
126
+ broadcast(publicView(t));
127
+ }
128
+
129
+ function prune(workspaceId) {
130
+ const ours = [...tasks.values()].filter((t) => t.workspaceId === workspaceId);
131
+ if (ours.length <= maxPerWs) return;
132
+ // Drop oldest TERMINAL tasks first; never evict something still running.
133
+ const terminal = ours.filter((t) => TERMINAL.has(t.status)).sort((a, b) => a.startedAt - b.startedAt);
134
+ let over = ours.length - maxPerWs;
135
+ for (const t of terminal) {
136
+ if (over <= 0) break;
137
+ tasks.delete(t.id);
138
+ if (t.shellId) byShell.delete(t.shellId);
139
+ over -= 1;
140
+ }
141
+ }
142
+
143
+ function startLaunch(workspaceId, threadId, chunk) {
144
+ const id = chunk.id;
145
+ if (!id || tasks.has(id)) return;
146
+ const input = chunk.input || {};
147
+ const t = {
148
+ id,
149
+ shellId: null,
150
+ outputFile: null,
151
+ command: String(input.command || '').slice(0, 2000),
152
+ description: String(input.description || '').slice(0, 300),
153
+ status: 'running',
154
+ exitCode: null,
155
+ quiet: false,
156
+ startedAt: now(),
157
+ endedAt: null,
158
+ lastOutputAt: null,
159
+ lastSize: 0,
160
+ everExisted: false,
161
+ workspaceId,
162
+ threadId: threadId || null,
163
+ };
164
+ tasks.set(id, t);
165
+ prune(workspaceId);
166
+ emit(t);
167
+ ensureTimer();
168
+ }
169
+
170
+ function fillLaunchResult(id, content) {
171
+ const t = tasks.get(id);
172
+ if (!t || t.shellId) return; // not a launch, or already resolved
173
+ const parsed = parseLaunchResult(content);
174
+ if (!parsed) return;
175
+ t.shellId = parsed.shellId;
176
+ t.outputFile = parsed.outputFile;
177
+ byShell.set(parsed.shellId, id);
178
+ emit(t);
179
+ }
180
+
181
+ function applyPoll(shellId, parsed) {
182
+ const id = byShell.get(shellId);
183
+ if (!id) return;
184
+ const t = tasks.get(id);
185
+ if (!t || TERMINAL.has(t.status)) return;
186
+ if (parsed.status === 'running') {
187
+ // A live poll proves it's alive right now — clears any `quiet` flag.
188
+ t.lastOutputAt = now();
189
+ if (t.quiet) { t.quiet = false; emit(t); }
190
+ return;
191
+ }
192
+ t.status = parsed.status;
193
+ t.exitCode = parsed.exitCode;
194
+ t.endedAt = now();
195
+ t.quiet = false;
196
+ emit(t);
197
+ }
198
+
199
+ function applyKill(shellId) {
200
+ const id = byShell.get(shellId);
201
+ if (!id) return;
202
+ const t = tasks.get(id);
203
+ if (!t || TERMINAL.has(t.status)) return;
204
+ t.status = 'killed';
205
+ t.endedAt = now();
206
+ t.quiet = false;
207
+ emit(t);
208
+ }
209
+
210
+ /**
211
+ * Fold one normalized chunk (from session.on('chunk')) into the registry. Only
212
+ * tool-use/tool-result chunks matter; everything else is ignored cheaply.
213
+ */
214
+ function observe(workspaceId, threadId, chunk) {
215
+ if (!chunk || !workspaceId) return;
216
+ if (chunk.type === 'tool-use') {
217
+ const name = chunk.name;
218
+ const input = chunk.input || {};
219
+ if (name === 'Bash' && input.run_in_background === true) {
220
+ startLaunch(workspaceId, threadId, chunk);
221
+ } else if (POLL_TOOLS.has(name)) {
222
+ const sid = targetShellId(input);
223
+ if (sid && chunk.id) pollTargets.set(chunk.id, sid);
224
+ } else if (KILL_TOOLS.has(name)) {
225
+ const sid = targetShellId(input);
226
+ if (sid) applyKill(sid);
227
+ }
228
+ return;
229
+ }
230
+ if (chunk.type === 'tool-result') {
231
+ const id = chunk.id;
232
+ if (!id) return;
233
+ if (tasks.has(id)) {
234
+ // The launching Bash's own result — carries the shell id + output path.
235
+ fillLaunchResult(id, chunk.content);
236
+ } else if (pollTargets.has(id)) {
237
+ const sid = pollTargets.get(id);
238
+ pollTargets.delete(id);
239
+ const parsed = parsePollResult(chunk.content);
240
+ if (parsed) applyPoll(sid, parsed);
241
+ }
242
+ }
243
+ }
244
+
245
+ // --- file-watcher: process-independent liveness for the gap between launch and
246
+ // the agent's next poll (which, in the per-turn model, may never come). -------
247
+ function tick() {
248
+ const t = now();
249
+ let anyLive = false;
250
+ for (const task of tasks.values()) {
251
+ if (TERMINAL.has(task.status)) continue;
252
+ anyLive = true;
253
+ if (!task.outputFile) continue;
254
+ let size = null;
255
+ try {
256
+ size = fsi.statSync(task.outputFile).size;
257
+ } catch {
258
+ size = null; // not created yet, or cleaned up after finishing
259
+ }
260
+ if (size !== null) {
261
+ if (!task.everExisted) task.everExisted = true;
262
+ if (size > task.lastSize) {
263
+ task.lastSize = size;
264
+ task.lastOutputAt = t;
265
+ if (task.quiet) { task.quiet = false; emit(task); }
266
+ continue;
267
+ }
268
+ }
269
+ // No new bytes this tick. Flag (or clear) the `quiet` hint honestly.
270
+ const idle = t - (task.lastOutputAt || task.startedAt);
271
+ const quiet = idle >= quietMs;
272
+ if (quiet !== task.quiet) { task.quiet = quiet; emit(task); }
273
+ }
274
+ if (!anyLive) stopTimer(); // nothing live → stop polling; observe() restarts it
275
+ }
276
+
277
+ function ensureTimer() {
278
+ if (timer) return;
279
+ timer = setInterval(tick, pollMs);
280
+ if (typeof timer.unref === 'function') timer.unref(); // never hold the process open
281
+ }
282
+ function stopTimer() {
283
+ if (timer) { clearInterval(timer); timer = null; }
284
+ }
285
+
286
+ return {
287
+ observe,
288
+ /** All tasks for a workspace, newest first. */
289
+ list(workspaceId) {
290
+ return [...tasks.values()]
291
+ .filter((t) => t.workspaceId === workspaceId)
292
+ .sort((a, b) => b.startedAt - a.startedAt)
293
+ .map(publicView);
294
+ },
295
+ /** Tail the output file for one task (best-effort; null if unavailable). */
296
+ tail(workspaceId, taskId, bytes = TAIL_BYTES) {
297
+ const t = tasks.get(taskId);
298
+ if (!t || t.workspaceId !== workspaceId || !t.outputFile) return null;
299
+ try {
300
+ const stat = fsi.statSync(t.outputFile);
301
+ const start = Math.max(0, stat.size - bytes);
302
+ const fd = fsi.openSync(t.outputFile, 'r');
303
+ try {
304
+ const len = stat.size - start;
305
+ const buf = Buffer.alloc(len);
306
+ fsi.readSync(fd, buf, 0, len, start);
307
+ return buf.toString('utf8');
308
+ } finally {
309
+ fsi.closeSync(fd);
310
+ }
311
+ } catch {
312
+ return null;
313
+ }
314
+ },
315
+ /** Test/diagnostic: the internal task (incl. outputFile). */
316
+ _get(taskId) {
317
+ return tasks.get(taskId) || null;
318
+ },
319
+ _tick: tick,
320
+ dispose: stopTimer,
321
+ };
322
+ }
@@ -84,6 +84,7 @@ import { loadAccount } from './account.mjs';
84
84
  import { getOperatorToken } from './operator.mjs';
85
85
  import { runDoctor } from './doctor.mjs';
86
86
  import { appendLine, tailFile, logFile, listLogs, TAILABLE, globalDir } from './logpaths.mjs';
87
+ import { createBackgroundTasks } from './background-tasks.mjs';
87
88
  import { SessionReporter } from './session-reporter.mjs';
88
89
  import { TranscriptRecorder } from './transcript.mjs';
89
90
  import { loadObservabilityConsent, setObservabilityConsent } from './observability.mjs';
@@ -578,6 +579,23 @@ export async function createServer(overrides = {}) {
578
579
  }
579
580
  }
580
581
 
582
+ // Background-task frames are WORKSPACE-scoped but THREAD-agnostic: a job the agent
583
+ // launched in chat A is still "running in this workspace", so the topbar tray and
584
+ // the canvas Tasks block (which span threads) must see it. Sent to every socket on
585
+ // the workspace, regardless of which chat thread it's bound to.
586
+ function broadcastTask(task) {
587
+ if (!task || !task.workspaceId) return;
588
+ const data = JSON.stringify({ type: 'task', task });
589
+ for (const ws of chatClients) {
590
+ if (ws.readyState !== ws.OPEN) continue;
591
+ if (ws._wsWorkspaceId !== task.workspaceId) continue;
592
+ ws.send(data);
593
+ }
594
+ }
595
+ // The registry persists ACROSS turns (a background job outlives the `claude -p`
596
+ // process that spawned it), so it lives here in server scope — never per-session.
597
+ const bgTasks = createBackgroundTasks({ broadcast: broadcastTask });
598
+
581
599
  /**
582
600
  * Run one chat turn: spawn the agent, stream every chunk to every chat
583
601
  * client, and persist the resulting session id so the next turn resumes it.
@@ -644,6 +662,10 @@ export async function createServer(overrides = {}) {
644
662
  return;
645
663
  }
646
664
  if (chunk.type === 'error') sawError = true;
665
+ // Track background jobs (Bash run_in_background:true + any TaskOutput/BashOutput
666
+ // polls) so the chat card, topbar tray, and Tasks block can show live status.
667
+ // Cheap no-op for every other chunk type.
668
+ bgTasks.observe(workspace.id, threadId, chunk);
647
669
  broadcastChat({ type: 'chunk', messageId: id, chunk }, workspace.id, threadId);
648
670
  activityBus.publish({ type: 'chat-stream', messageId: id, chunk });
649
671
  // Surface the turn's token/cost totals so the activity bar can show
@@ -2554,6 +2576,27 @@ export async function createServer(overrides = {}) {
2554
2576
  return c.json({ name, lines, body: tailFile(logFile(name), lines) });
2555
2577
  });
2556
2578
 
2579
+ // Background tasks (the agent's run_in_background jobs) — what's running, finished,
2580
+ // or gone quiet. Owner-only (fileTree): these expose the commands the agent ran on
2581
+ // the host, which a share-link viewer must not see (same posture as Logs). The
2582
+ // absolute output-file path is never returned (it would leak the home dir); tail it
2583
+ // by id instead.
2584
+ app.get('/api/workspace/tasks', (c) => {
2585
+ const forbidden = require(c, 'fileTree');
2586
+ if (forbidden) return forbidden;
2587
+ return c.json({ tasks: bgTasks.list(workspaceFor(c).id) });
2588
+ });
2589
+
2590
+ app.get('/api/workspace/tasks/:id/output', (c) => {
2591
+ const forbidden = require(c, 'fileTree');
2592
+ if (forbidden) return forbidden;
2593
+ const id = c.req.param('id');
2594
+ const bytes = Math.min(Number(c.req.query('bytes')) || 16384, 200_000);
2595
+ const output = bgTasks.tail(workspaceFor(c).id, id, bytes);
2596
+ if (output === null) return c.json({ error: 'no-output', id }, 404);
2597
+ return c.json({ id, output });
2598
+ });
2599
+
2557
2600
  // --- component inbox ---
2558
2601
  app.get('/api/inbox', async (c) => {
2559
2602
  // Enforce the `inbox` capability (partner-only). It existed in the matrix