flowviant 0.47.2 → 0.48.0

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.
@@ -191,7 +191,13 @@ const oneLine = (s, n = 160) => String(s).replace(/\s+/g, ' ').trim().slice(0, n
191
191
  // bursts before/between tools; emitting only tools left long silent gaps).
192
192
  // Assistant text is also folded into `out` so the WIKI_DONE/REGROUND_DONE
193
193
  // sentinels still match. A non-JSON line (a stray warning) is kept as raw text.
194
- function handleStreamLine(line, { cwd, emit, onActivity, appendText }) {
194
+ //
195
+ // `answerFromResult` narrows that last part for callers whose `out` IS the
196
+ // answer rather than a haystack to match sentinels in (a Workbench tab's turn):
197
+ // every intermediate text block still NARRATES, but only the final `result`
198
+ // event contributes text — otherwise the same sentences arrive twice, once as
199
+ // they stream and once in the result, and the tab posts the duplicate.
200
+ function handleStreamLine(line, { cwd, emit, onActivity, appendText, answerFromResult }) {
195
201
  let ev;
196
202
  try {
197
203
  ev = JSON.parse(line);
@@ -212,15 +218,23 @@ function handleStreamLine(line, { cwd, emit, onActivity, appendText }) {
212
218
  // marker — enough to show Claude is actively reasoning, not hung.
213
219
  push({ kind: 'think', label: b.thinking ? `thinking: ${oneLine(b.thinking)}` : 'thinking…' });
214
220
  } else if (b.type === 'text' && b.text?.trim()) {
215
- appendText(b.text + '\n');
221
+ if (!answerFromResult) appendText(b.text + '\n');
216
222
  push({ kind: 'say', label: oneLine(b.text) });
217
223
  } else if (b.type === 'tool_use') {
218
224
  push(humanizeToolUse(b.name, b.input || {}, cwd));
219
225
  }
220
226
  }
221
- } else if (ev.type === 'result' && typeof ev.result === 'string') {
227
+ } else if (ev.type === 'result') {
222
228
  // The final assistant text (carries WIKI_DONE / REGROUND_DONE).
223
- appendText(ev.result + '\n');
229
+ if (typeof ev.result === 'string') appendText(ev.result + '\n');
230
+ else if (ev.is_error || ev.subtype) {
231
+ // A result that carries no text is a FAILED turn (a limit, a refused
232
+ // permission, an aborted run). Under `answerFromResult` this is the only
233
+ // stdout that would have said so, and a caller whose `out` is the answer
234
+ // must not report "no output" for a turn that explained itself.
235
+ const msg = ev.error?.message ?? ev.error ?? ev.subtype;
236
+ appendText(`${typeof msg === 'string' ? msg : JSON.stringify(msg)}\n`);
237
+ }
224
238
  }
225
239
  }
226
240
 
@@ -233,7 +247,7 @@ function handleStreamLine(line, { cwd, emit, onActivity, appendText }) {
233
247
  // returned string for sentinel detection, and each activity is handed to
234
248
  // `onActivity` so the caller can forward progress. Build-agent turns leave it
235
249
  // off and keep the raw text passthrough + line sentinels.
236
- export function runTurn({ prompt, resume, system, cwd, mcpConfig, mcpArgs, mcpEnv, runtime = 'claude', label, onSpawn, streamJson, onActivity, onThreadId, wikiPerm, readOnly, planPerm, vaultDir, resultSchemaArgs, model, effort, adoptResumeId, resumeThreadId, resumeConversationId }) {
250
+ export function runTurn({ prompt, resume, system, cwd, mcpConfig, mcpArgs, mcpEnv, runtime = 'claude', label, onSpawn, streamJson, answerFromResult, onActivity, onThreadId, wikiPerm, readOnly, planPerm, vaultDir, resultSchemaArgs, model, effort, adoptResumeId, resumeThreadId, resumeConversationId }) {
237
251
  return new Promise((resolve) => {
238
252
  const rt = runtimeById(runtime);
239
253
  if (!rt.args) {
@@ -344,7 +358,8 @@ export function runTurn({ prompt, resume, system, cwd, mcpConfig, mcpArgs, mcpEn
344
358
  };
345
359
  /** One line of the child's stdout, in whichever dialect it speaks. */
346
360
  const onLine = (line) => {
347
- if (!rt.parse) return handleStreamLine(line, { cwd, emit, onActivity, appendText });
361
+ if (!rt.parse)
362
+ return handleStreamLine(line, { cwd, emit, onActivity, appendText, answerFromResult });
348
363
  const ev = rt.parse(line, cwd);
349
364
  if (!ev) return;
350
365
  // The conversation id, when the runtime announces one (codex's
package/bin/lib/fleet.mjs CHANGED
@@ -1213,6 +1213,7 @@ export async function runFleetDaemon() {
1213
1213
  processWorkTurns,
1214
1214
  processShipJobs,
1215
1215
  retireWorkSessions,
1216
+ reportWorktrees,
1216
1217
  shutdownWork,
1217
1218
  } = createWorkManager({
1218
1219
  repoRoot,
@@ -1831,6 +1832,11 @@ export async function runFleetDaemon() {
1831
1832
  // sessions are LIVE, and the guards above (chains, shipping) are populated
1832
1833
  // by the intake this same tick.
1833
1834
  retireWorkSessions(roster.activeWorkSessions);
1835
+ // …and what the SURVIVING ones hold: branch, ahead-of-base, diffstat.
1836
+ // Throttled inside, never awaited — a `git status` the human cannot run
1837
+ // themselves from a browser, relayed. After retirement so a directory that
1838
+ // just went away is not reported as a place.
1839
+ reportWorktrees(roster.activeWorkSessions);
1834
1840
  // Terminal-session presence, throttled + dedup'd inside; never awaited —
1835
1841
  // the daemon's own worktrees are carved out (a session the daemon spawned
1836
1842
  // is already a tab, not something to offer adopting).
package/bin/lib/work.mjs CHANGED
@@ -43,6 +43,7 @@ import {
43
43
  import { materializeInto, scrub as envScrub } from './env.mjs';
44
44
  import { detectRuntimes, canRun, RUNTIMES } from './runtimes.mjs';
45
45
  import { isTerminalSessionLive, isAgyConversationLive } from './localSessions.mjs';
46
+ import { worktreeDiff } from './worktreeDiff.mjs';
46
47
  import { homedir } from 'node:os';
47
48
 
48
49
  /**
@@ -88,6 +89,8 @@ export function createWorkManager({ repoRoot, baseDir, baseRef, getMcpUrl, getLe
88
89
  const WORK_TOKEN_URL = FLEET_URL.replace(/\/agents\/?$/, '/work-token');
89
90
  const WORK_DONE_URL = FLEET_URL.replace(/\/agents\/?$/, '/work-turn-done');
90
91
  const SHIP_DONE_URL = FLEET_URL.replace(/\/agents\/?$/, '/ship-done');
92
+ const ACTIVITY_URL = FLEET_URL.replace(/\/agents\/?$/, '/session-activity');
93
+ const WORKTREES_URL = FLEET_URL.replace(/\/agents\/?$/, '/session-worktrees');
91
94
  const workAnswering = new Set(); // turn ids currently queued/running here
92
95
  const workAttempts = new Map(); // turn id -> completed runTurn attempts
93
96
  const MAX_WORK_TRIES = 3;
@@ -168,6 +171,144 @@ export function createWorkManager({ repoRoot, baseDir, baseRef, getMcpUrl, getLe
168
171
  else pendingShipReports.delete(sessionId);
169
172
  return r;
170
173
  };
174
+ /**
175
+ * THE TAB'S LIVE NARRATION — the terminal's own stdout, relayed.
176
+ *
177
+ * A turn used to be a spinner: the tab said "working…" for minutes and the
178
+ * only thing that ever appeared was the finished reply. The CLI is printing
179
+ * the whole time (thinking, reads, greps, commands), so the honest fix is to
180
+ * FORWARD that, not to invent a progress model on the server. Flowviant
181
+ * relays; it does not narrate on its own behalf.
182
+ *
183
+ * Best-effort by construction: throttled to one POST per window (a turn can
184
+ * emit hundreds of lines), never awaited by the turn, and every failure is
185
+ * swallowed. A spinner must never be able to fail a build. The server clears
186
+ * the line at settle, so a daemon killed mid-turn cannot leave one stuck.
187
+ */
188
+ const ACTIVITY_MIN_MS = 1_500;
189
+ const ACTIVITY_KEEP = 4; // the last few lines — a tail, not a log
190
+ const makeNarrator = (sessionId) => {
191
+ const recent = [];
192
+ let lastSent = 0;
193
+ let dirty = false;
194
+ let timer = null;
195
+ let sending = false;
196
+ let stopped = false;
197
+ const send = async () => {
198
+ if (sending || stopped) return;
199
+ sending = true;
200
+ dirty = false;
201
+ lastSent = Date.now();
202
+ const lines = recent.slice(-ACTIVITY_KEEP);
203
+ try {
204
+ await fetch(ACTIVITY_URL, {
205
+ method: 'POST',
206
+ headers: {
207
+ Authorization: `Bearer ${FLEET_TOKEN}`,
208
+ 'User-Agent': USER_AGENT,
209
+ 'Content-Type': 'application/json',
210
+ },
211
+ signal: AbortSignal.timeout(10_000),
212
+ body: JSON.stringify({ sessionId, lines }),
213
+ });
214
+ } catch {
215
+ /* narration is decoration — a dropped line is not an incident */
216
+ }
217
+ sending = false;
218
+ if (dirty && !stopped) schedule();
219
+ };
220
+ const schedule = () => {
221
+ if (timer || stopped) return;
222
+ const wait = Math.max(0, ACTIVITY_MIN_MS - (Date.now() - lastSent));
223
+ timer = setTimeout(() => {
224
+ timer = null;
225
+ void send();
226
+ }, wait);
227
+ timer.unref?.(); // never hold the process open for a spinner
228
+ };
229
+ return {
230
+ line(label) {
231
+ const s = String(label ?? '')
232
+ .replace(/\s+/g, ' ')
233
+ .trim()
234
+ .slice(0, 200);
235
+ if (!s || stopped) return;
236
+ recent.push(s);
237
+ if (recent.length > ACTIVITY_KEEP * 2) recent.shift();
238
+ dirty = true;
239
+ schedule();
240
+ },
241
+ stop() {
242
+ stopped = true;
243
+ if (timer) {
244
+ clearTimeout(timer);
245
+ timer = null;
246
+ }
247
+ },
248
+ };
249
+ };
250
+
251
+ /**
252
+ * WHERE EACH TAB IS STANDING, and what it holds — the readout a human would
253
+ * get by running `git status` in the session's directory, which is the one
254
+ * thing they cannot do from a browser.
255
+ *
256
+ * Two triggers, both cheap: right after a turn settles (the moment the diff
257
+ * changed) and a throttled sweep over every live session (a human editing in
258
+ * the worktree, a build writing files, a ship landing). Best-effort like the
259
+ * narrator: never awaited by a turn, every failure swallowed.
260
+ */
261
+ const WORKTREE_SWEEP_MS = 60_000;
262
+ let lastWorktreeSweep = 0;
263
+ let sweepingWorktrees = false;
264
+ const postWorktrees = async (reports) => {
265
+ if (!reports.length) return;
266
+ try {
267
+ await fetch(WORKTREES_URL, {
268
+ method: 'POST',
269
+ headers: {
270
+ Authorization: `Bearer ${FLEET_TOKEN}`,
271
+ 'User-Agent': USER_AGENT,
272
+ 'Content-Type': 'application/json',
273
+ },
274
+ signal: AbortSignal.timeout(20_000),
275
+ body: JSON.stringify({ reports }),
276
+ });
277
+ } catch {
278
+ /* a readout — the next sweep carries it */
279
+ }
280
+ };
281
+ const sessionWorktreeReport = (sessionId) => {
282
+ if (!isSafePathSegment(sessionId)) return null;
283
+ const d = worktreeDiff(join(baseDir, 'sessions', sessionId), baseRef);
284
+ return d ? { sessionId, ...d } : null;
285
+ };
286
+ /** One session, now — called after its turn settles. */
287
+ const reportSessionWorktree = async (sessionId) => {
288
+ const r = sessionWorktreeReport(sessionId);
289
+ if (r) await postWorktrees([r]);
290
+ };
291
+ /** Every live session, throttled — called from the reconcile loop. */
292
+ const reportWorktrees = (activeIds) => {
293
+ if (!Array.isArray(activeIds) || activeIds.length === 0) return;
294
+ if (sweepingWorktrees) return;
295
+ if (Date.now() - lastWorktreeSweep < WORKTREE_SWEEP_MS) return;
296
+ sweepingWorktrees = true;
297
+ lastWorktreeSweep = Date.now();
298
+ void (async () => {
299
+ try {
300
+ const reports = [];
301
+ for (const id of activeIds.slice(0, 20)) {
302
+ const r = sessionWorktreeReport(id);
303
+ if (r) reports.push(r);
304
+ }
305
+ await postWorktrees(reports);
306
+ } finally {
307
+ sweepingWorktrees = false;
308
+ }
309
+ })();
310
+ };
311
+
171
312
  let flushingReports = false;
172
313
  const flushWorkReports = async () => {
173
314
  if (flushingReports) return;
@@ -928,6 +1069,7 @@ export function createWorkManager({ repoRoot, baseDir, baseRef, getMcpUrl, getLe
928
1069
  let out;
929
1070
  let seenThreadId = null; // codex's conversation id, off thread.started
930
1071
  const spawned = []; // this turn's children, for the teardown registry
1072
+ const narrator = makeNarrator(job.sessionId);
931
1073
  try {
932
1074
  const message = [job.body, adoptNote, carryNote].filter(Boolean).join('\n\n');
933
1075
  const turnArgs = {
@@ -953,6 +1095,14 @@ export function createWorkManager({ repoRoot, baseDir, baseRef, getMcpUrl, getLe
953
1095
  system: plainTab ? SYSTEM_WORK_PLAIN : SYSTEM_WORK,
954
1096
  // Present only when the tab named one — see brainFor.
955
1097
  ...brain,
1098
+ // The tab watches the CLI work. Claude needs the flag to speak
1099
+ // events at all (codex and agy always do); `answerFromResult`
1100
+ // keeps `out` — which IS the reply posted to the transcript — to
1101
+ // the final result, so streamed prose is narrated once and
1102
+ // posted once. Every line goes to the narrator above, throttled.
1103
+ streamJson: true,
1104
+ answerFromResult: true,
1105
+ onActivity: (a) => narrator.line(a?.label),
956
1106
  cwd: dir.wt,
957
1107
  mcpArgs: mcp.args,
958
1108
  mcpEnv: mcp.env,
@@ -998,6 +1148,10 @@ export function createWorkManager({ repoRoot, baseDir, baseRef, getMcpUrl, getLe
998
1148
  if (!adopting && resume && !(out || '').trim())
999
1149
  out = await runTurn({ ...turnArgs, resume: false });
1000
1150
  } finally {
1151
+ // The CLI has stopped printing, so stop relaying. The LINE itself
1152
+ // is cleared server-side at settle — clearing it here would race
1153
+ // the settle and blank the tab a beat before the reply lands.
1154
+ narrator.stop();
1001
1155
  for (const ch of spawned) workChildren.delete(ch);
1002
1156
  if (lockPath) {
1003
1157
  try {
@@ -1083,6 +1237,12 @@ export function createWorkManager({ repoRoot, baseDir, baseRef, getMcpUrl, getLe
1083
1237
  warn(`session turn failed: ${e?.message ?? e}`);
1084
1238
  } finally {
1085
1239
  workAnswering.delete(job.id);
1240
+ // The turn just changed the directory — say what it looks like now,
1241
+ // whether it succeeded or blew up (a failed turn can still have
1242
+ // written half a file, and the tab should show that honestly). NOT
1243
+ // awaited: this runs inside the session's chain, and a slow POST
1244
+ // would delay the next turn of that tab behind a readout.
1245
+ void reportSessionWorktree(job.sessionId).catch(() => {});
1086
1246
  }
1087
1247
  });
1088
1248
  }
@@ -1420,5 +1580,12 @@ export function createWorkManager({ repoRoot, baseDir, baseRef, getMcpUrl, getLe
1420
1580
  }
1421
1581
  };
1422
1582
 
1423
- return { flushWorkReports, processWorkTurns, processShipJobs, retireWorkSessions, shutdownWork };
1583
+ return {
1584
+ flushWorkReports,
1585
+ processWorkTurns,
1586
+ processShipJobs,
1587
+ retireWorkSessions,
1588
+ reportWorktrees,
1589
+ shutdownWork,
1590
+ };
1424
1591
  }
@@ -0,0 +1,147 @@
1
+ /**
2
+ * What a session's worktree actually holds, measured — not guessed.
3
+ *
4
+ * A Workbench tab IS a directory on this machine, on its own `session/<id>`
5
+ * branch, and the human driving it from a browser cannot run `git status` in
6
+ * it. So the daemon runs it for them: the branch, how far ahead of base it is,
7
+ * and the per-file diffstat — the same numbers `git diff --stat` prints in that
8
+ * directory, relayed rather than interpreted.
9
+ *
10
+ * Measured against the MERGE-BASE with the project's base ref, and against the
11
+ * WORKING TREE rather than HEAD, so one number answers the question a human
12
+ * actually asks ("what has this session changed?") with committed and
13
+ * uncommitted work in the same total. Untracked files count too: git calls them
14
+ * nothing until they are added, and a human calls them new work.
15
+ *
16
+ * Everything here is best-effort and read-only. A worktree mid-rebase, a
17
+ * deleted directory, a file that vanished between listing and reading — each
18
+ * degrades to a smaller answer, never to a thrown error. Nothing about a
19
+ * readout is worth failing a turn over.
20
+ */
21
+
22
+ import { existsSync, statSync, readFileSync } from 'node:fs';
23
+ import { join } from 'node:path';
24
+ import { git } from './git.mjs';
25
+
26
+ /** Rows reported. The rail shows a handful; the totals below cover the rest. */
27
+ const MAX_FILES = 20;
28
+ /** Untracked paths we are willing to open. A stray build directory that isn't
29
+ * gitignored must not turn a 60-second sweep into a disk crawl. */
30
+ const MAX_UNTRACKED_SCAN = 200;
31
+ /** Past this we call a file binary rather than counting its lines. */
32
+ const MAX_COUNT_BYTES = 512 * 1024;
33
+
34
+ /** Lines in a buffer, the way a diff counts them: a trailing newline does not
35
+ * add a line, and a NUL byte anywhere means we are not looking at text. */
36
+ function countLines(buf) {
37
+ if (buf.includes(0)) return null; // binary — git's own heuristic
38
+ const s = buf.toString('utf8');
39
+ if (s === '') return 0;
40
+ const n = s.split('\n').length;
41
+ return s.endsWith('\n') ? n - 1 : n;
42
+ }
43
+
44
+ /**
45
+ * @param {string} wt the worktree directory
46
+ * @param {string} baseRef the project's base ref (e.g. `origin/main`)
47
+ * @returns {null | {branch:string, path:string, ahead:number, dirty:boolean,
48
+ * additions:number, deletions:number, fileCount:number, truncated:number,
49
+ * files:{path:string, added:number, deleted:number, binary?:boolean}[]}}
50
+ */
51
+ export function worktreeDiff(wt, baseRef) {
52
+ if (!wt || !existsSync(wt)) return null;
53
+ let branch = '';
54
+ try {
55
+ branch = git(['rev-parse', '--abbrev-ref', 'HEAD'], wt);
56
+ } catch {
57
+ return null; // not a worktree (or not readable) — report nothing, not zeros
58
+ }
59
+ let base = '';
60
+ try {
61
+ base = git(['merge-base', 'HEAD', baseRef], wt);
62
+ } catch {
63
+ /* a branch with no common ancestor (or an unfetched base) — fall back to
64
+ HEAD below, which still reports the uncommitted half honestly */
65
+ }
66
+
67
+ const files = [];
68
+ let additions = 0;
69
+ let deletions = 0;
70
+ const push = (path, added, deleted, binary = false) => {
71
+ if (!path) return;
72
+ files.push(binary ? { path, added, deleted, binary } : { path, added, deleted });
73
+ additions += added;
74
+ deletions += deleted;
75
+ };
76
+
77
+ // Tracked: working tree vs base. `git diff <base>` (no --cached, no second
78
+ // ref) is exactly "everything this session did", committed or not.
79
+ try {
80
+ const raw = git(['diff', '--numstat', base || 'HEAD'], wt);
81
+ for (const line of raw.split('\n')) {
82
+ if (!line.trim()) continue;
83
+ const [a, d, ...rest] = line.split('\t');
84
+ const path = rest.join('\t');
85
+ const binary = a === '-' || d === '-';
86
+ push(path, binary ? 0 : Number(a) || 0, binary ? 0 : Number(d) || 0, binary);
87
+ }
88
+ } catch {
89
+ /* report what we have */
90
+ }
91
+
92
+ // Untracked, minus everything gitignored — new files are the most visible
93
+ // work a session does and they would otherwise show as nothing at all.
94
+ try {
95
+ const others = git(['ls-files', '--others', '--exclude-standard'], wt)
96
+ .split('\n')
97
+ .filter(Boolean);
98
+ for (const path of others.slice(0, MAX_UNTRACKED_SCAN)) {
99
+ try {
100
+ const full = join(wt, path);
101
+ const st = statSync(full);
102
+ if (!st.isFile()) continue;
103
+ if (st.size > MAX_COUNT_BYTES) {
104
+ push(path, 0, 0, true);
105
+ continue;
106
+ }
107
+ const lines = countLines(readFileSync(full));
108
+ if (lines === null) push(path, 0, 0, true);
109
+ else push(path, lines, 0);
110
+ } catch {
111
+ /* vanished between listing and reading — it wasn't there to report */
112
+ }
113
+ }
114
+ } catch {
115
+ /* report what we have */
116
+ }
117
+
118
+ let ahead = 0;
119
+ try {
120
+ if (base) ahead = Number(git(['rev-list', '--count', `${base}..HEAD`], wt)) || 0;
121
+ } catch {
122
+ /* leave at 0 */
123
+ }
124
+ let dirty = false;
125
+ try {
126
+ dirty = git(['status', '--porcelain'], wt) !== '';
127
+ } catch {
128
+ /* leave at false */
129
+ }
130
+
131
+ // Biggest first: with a 20-row cap, the rows that survive should be the ones
132
+ // worth looking at. Ties break by path so the list doesn't shuffle per sweep.
133
+ files.sort(
134
+ (x, y) => y.added + y.deleted - (x.added + x.deleted) || (x.path < y.path ? -1 : 1)
135
+ );
136
+ return {
137
+ branch,
138
+ path: wt,
139
+ ahead,
140
+ dirty,
141
+ additions,
142
+ deletions,
143
+ fileCount: files.length,
144
+ truncated: Math.max(0, files.length - MAX_FILES),
145
+ files: files.slice(0, MAX_FILES),
146
+ };
147
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flowviant",
3
- "version": "0.47.2",
3
+ "version": "0.48.0",
4
4
  "description": "Run your own coding CLIs as headless build agents for Flowviant — Claude Code or Codex, on your own credentials. Claims dispatched work, opens PRs, captures review evidence, and routes questions back to you.",
5
5
  "type": "module",
6
6
  "bin": {