shapewall 0.2.3 → 0.3.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.
Files changed (110) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/README.md +35 -1
  3. package/dist/agent/package-lock.json +2 -2
  4. package/dist/client/assets/BoardChat-CupOxioV.js +27 -0
  5. package/dist/client/assets/{BuzzDialog-CZdwIQLF.js → BuzzDialog-Ds7Thpfe.js} +1 -1
  6. package/dist/client/assets/ChecksPanel-BqrS02Ih.js +1 -0
  7. package/dist/client/assets/History-CQG3NQOC.js +1 -0
  8. package/dist/client/assets/{LocalSettings-CvlVtNY_.js → LocalSettings-CSNMhNyV.js} +1 -1
  9. package/dist/client/assets/{Management-DOJkFY_o.js → Management-CJVNAqAW.js} +1 -1
  10. package/dist/client/assets/QuickConnect-B-4pUgXH.js +1 -0
  11. package/dist/client/assets/{ShareDialog-CdVAreaw.js → ShareDialog-BouvNraR.js} +2 -2
  12. package/dist/client/assets/{app-update-dialog-Dox9-LWv.js → app-update-dialog-rhaw0MRk.js} +1 -1
  13. package/dist/client/assets/{circle-x-BmT6vHKl.js → circle-x-PRxj7kPi.js} +1 -1
  14. package/dist/client/assets/index-CXtuJi1K.css +2 -0
  15. package/dist/client/assets/index-DKOCIQiq.js +37 -0
  16. package/dist/client/assets/{refresh-cw-ugt3cWKH.js → refresh-cw-aeOBDuGQ.js} +1 -1
  17. package/dist/client/assets/{square-_KG6tM5A.js → shield-check-DuPBJ6H_.js} +1 -1
  18. package/dist/client/assets/ui-Cg4TTrec.js +41 -0
  19. package/dist/client/build-info.json +1 -1
  20. package/dist/client/index.html +3 -3
  21. package/dist/client/sw.js +2 -2
  22. package/dist/console/assets/LocalAccountsApp-DOKZI6Lt.js +1 -0
  23. package/dist/console/assets/LocalConsole-DXzldLW9.js +1 -0
  24. package/dist/console/assets/app-update-dialog-CrqIMEwN.js +1 -0
  25. package/dist/console/assets/console-76WovHlh.js +55 -0
  26. package/dist/console/assets/console-Fp4k9iyx.css +2 -0
  27. package/dist/console/build-info.json +1 -1
  28. package/dist/console/console.html +2 -2
  29. package/docs/agent-hardening.md +19 -0
  30. package/docs/agent-workflow.md +33 -0
  31. package/docs/agents.md +112 -0
  32. package/docs/config.md +41 -0
  33. package/docs/deployment.md +3 -0
  34. package/docs/design-system.md +13 -5
  35. package/docs/hosted.md +10 -0
  36. package/docs/journeys.md +13 -1
  37. package/docs/pull-requests.md +192 -0
  38. package/package.json +2 -1
  39. package/src/agent/backends/acp.js +35 -6
  40. package/src/agent/batch-fixes.js +45 -0
  41. package/src/agent/batch-queue.js +204 -0
  42. package/src/agent/branch-mode.js +753 -0
  43. package/src/agent/config.js +6 -0
  44. package/src/agent/fix-prompt.js +5 -0
  45. package/src/agent/manage.js +1 -1
  46. package/src/agent/routes.js +901 -186
  47. package/src/agent/run.js +8 -1
  48. package/src/agent/settings-events.js +103 -0
  49. package/src/agent/settings-routes.js +92 -2
  50. package/src/agent/starter/worktree.js +254 -25
  51. package/src/agent/threads.js +2 -7
  52. package/src/agent/tools.js +51 -0
  53. package/src/agent/user-settings.js +33 -2
  54. package/src/ci/capture.js +103 -0
  55. package/src/ci/compare.js +267 -0
  56. package/src/ci/github.js +307 -0
  57. package/src/ci/images.js +76 -0
  58. package/src/ci/settings.js +211 -0
  59. package/src/ci/start.js +148 -0
  60. package/src/ci/summary.js +262 -0
  61. package/src/ci/workflow.js +238 -0
  62. package/src/cli/args.js +24 -1
  63. package/src/cli/ci.js +229 -0
  64. package/src/cli/help.js +23 -1
  65. package/src/cli/init.js +33 -1
  66. package/src/cli.js +6 -2
  67. package/src/comment-drawing.js +161 -0
  68. package/src/comment-fields.js +51 -0
  69. package/src/comment-media.js +528 -0
  70. package/src/config.js +7 -0
  71. package/src/doctor.js +23 -0
  72. package/src/export.js +8 -0
  73. package/src/feedback.js +89 -12
  74. package/src/github.js +3 -3
  75. package/src/hosted/admin.js +36 -4
  76. package/src/hosted/app.js +5 -0
  77. package/src/hosted/board-routes.js +63 -7
  78. package/src/hosted/clean-worker.js +22 -4
  79. package/src/hosted/cleaner.js +4 -1
  80. package/src/hosted/comment-files.js +200 -0
  81. package/src/hosted/comments.js +68 -15
  82. package/src/hosted/db.js +32 -0
  83. package/src/hosted/guest-view.js +9 -0
  84. package/src/hosted/import.js +213 -18
  85. package/src/hosted/operator.js +3 -1
  86. package/src/hosted/projects.js +3 -2
  87. package/src/hosted/push.js +6 -3
  88. package/src/hosted/retention.js +18 -0
  89. package/src/hosted/tenancy.js +1 -0
  90. package/src/hosted/token-api.js +110 -9
  91. package/src/image-sniff.js +11 -0
  92. package/src/mcp.js +48 -3
  93. package/src/remote/remote-mcp.js +2 -2
  94. package/src/remote/remote.js +6 -2
  95. package/src/remote/share.js +77 -7
  96. package/src/server/app.js +18 -1
  97. package/src/store.js +119 -33
  98. package/src/watch.js +4 -0
  99. package/dist/client/assets/BoardChat-C88k4s-W.js +0 -28
  100. package/dist/client/assets/ChecksPanel-B54chcLA.js +0 -1
  101. package/dist/client/assets/History-CVVuuxUK.js +0 -1
  102. package/dist/client/assets/QuickConnect-D40mj4Ry.js +0 -1
  103. package/dist/client/assets/index-CwvFfwU0.js +0 -29
  104. package/dist/client/assets/index-UIYSPHoh.css +0 -2
  105. package/dist/client/assets/ui-BAGBTB_P.js +0 -41
  106. package/dist/console/assets/LocalAccountsApp-4MtW1QlZ.js +0 -1
  107. package/dist/console/assets/LocalConsole-CgKqYa8i.js +0 -1
  108. package/dist/console/assets/app-update-dialog-S2Yyc7Pw.js +0 -1
  109. package/dist/console/assets/console-BPmBRIoz.js +0 -51
  110. package/dist/console/assets/console-DgVrGRMD.css +0 -2
@@ -1,6 +1,7 @@
1
1
  // /api/agent/*: the chat panel's API. JSON in, server-sent events out.
2
2
  //
3
3
  // GET /api/agent/status what chat is connected to (no secrets)
4
+ // GET /api/agent/events SSE, owner only: "agents" when the agent settings change (settings-events.js)
4
5
  // GET /api/agent/boards/:id/threads the viewer's chat history (?q= searches it)
5
6
  // GET /api/agent/boards/:id/threads/:thread one thread
6
7
  // POST /api/agent/boards/:id/threads/:thread/messages send a message (editOf: rerun from an edit; regenerate: a new answer); SSE back
@@ -34,9 +35,35 @@
34
35
  // boards, and a byte quota per board for stored threads and images.
35
36
 
36
37
  import { createHash, randomBytes } from 'node:crypto'
38
+ import { spawn } from 'node:child_process'
39
+ import { EventEmitter } from 'node:events'
40
+ import fs from 'node:fs'
41
+ import path from 'node:path'
37
42
  import express from 'express'
38
- import { addReply, getComment, listComments, setAgentStatus } from '../store.js'
43
+ import { BRANCH_READY, addReply, boardDir, getComment, listComments, setAgentStatus } from '../store.js'
44
+ import { acquireLock, releaseLock } from '../lock.js'
45
+ import { boardUrlFor, commentLink } from '../notify/messages.js'
46
+ import { absoluteProgram } from '../which.js'
47
+ import {
48
+ BranchError,
49
+ agentWorktreeRoot,
50
+ applyAgentBranch,
51
+ branchModeSettings,
52
+ checkoutSignature,
53
+ commitAgentBranch,
54
+ createAgentBranch,
55
+ discardAgentBranch,
56
+ listAgentBranches,
57
+ readMeta,
58
+ revertAppliedBranch,
59
+ sweepAgentBranches,
60
+ worktreeAgentEnv,
61
+ writeMeta,
62
+ } from './branch-mode.js'
39
63
  import { createBackend } from './backends/index.js'
64
+ import { AGENTS_EVENT, createSettingsEvents } from './settings-events.js'
65
+ import { batchFixSettings, createBatchQueue } from './batch-fixes.js'
66
+ import { FIX_PROMPT as BATCH_FIX_PROMPT } from './fix-prompt.js'
40
67
  import { cleanChoices } from './backends/acp.js'
41
68
  import { createOptionsCache, withChoices } from './options-cache.js'
42
69
  import { authFiles } from '../config.js'
@@ -45,11 +72,11 @@ import { AgentError } from './errors.js'
45
72
  import { createAgentHealth } from './agent-health.js'
46
73
  import { beginAgentOperation } from './control.js'
47
74
  import { validateFiles, validateMentions } from './composer.js'
48
- import { profileForDefault, userAgentEntry } from './user-settings.js'
49
- import { finishFix, projectChanged, projectSnapshot } from './feedback-loop.js'
75
+ import { profileForDefault, userAgentEntry, userSettingsFile } from './user-settings.js'
76
+ import { finishFix, projectChanged, projectSnapshot, stopReason } from './feedback-loop.js'
50
77
  import { ownerMcpServers } from './owner-mcp.js'
51
78
  import { RUN_MS_MAX, commentIdOf, runTurn } from './run.js'
52
- import { beginTurnChanges, finishTurnChanges, undoFiles, undoStarterBranch } from './turn-changes.js'
79
+ import { beginTurnChanges, finishTurnChanges, starterSummary, undoFiles, undoStarterBranch } from './turn-changes.js'
53
80
  import { latestChange, withUndoAvailability } from './undo-state.js'
54
81
  import { exportRatings, findAnswer, myRatings, raterOf, removeRating, reviewRatings, setRating } from './ratings.js'
55
82
  import { localRequest, sameOriginWrite, settingsRoutes, settingsTokenOk } from './settings-routes.js'
@@ -82,6 +109,8 @@ import {
82
109
  // body this big, and only once the message is allowed to run.
83
110
  export const BODY_LIMIT = '28mb'
84
111
  const SMALL_BODY_LIMIT = '4kb'
112
+ // Open GET /events streams at once; a new one ends the oldest.
113
+ export const EVENT_STREAMS_MAX = 32
85
114
 
86
115
  export const DEFAULT_LIMITS = {
87
116
  // Answers being written at once, across all boards.
@@ -178,6 +207,13 @@ export function agentRouter({
178
207
  }) {
179
208
  const limit = { ...DEFAULT_LIMITS, ...limits }
180
209
  const router = express.Router()
210
+ // "Agents changed" for every open view: this server's settings routes and
211
+ // edits of the agents file from outside (settings-events.js).
212
+ const settingsEvents = createSettingsEvents({ file: userSettingsFile(env), watch: limit.watchSettings !== false })
213
+ router.settingsEvents = settingsEvents
214
+ // The open GET /events responses, ended on close.
215
+ const eventStreams = new Set()
216
+ let eventsClosed = false
181
217
  const settings = (profileId = null) => {
182
218
  const current = agentSettings(config, env, { acp, profileId })
183
219
  if (shared && !allowRemoteAgent && current.backend === 'acp' && current.configured) {
@@ -195,7 +231,14 @@ export function agentRouter({
195
231
  // processes) while the settings stay the same.
196
232
  let cached = null
197
233
  const backends = new Map()
198
- const processPool = { epoch: 0, agents: new Set(), max: Math.min(limit.runsMax, limit.acp?.processesMax ?? limit.runsMax) }
234
+ // `changed`: an agent process started, became ready or went away, which
235
+ // moves an agent's state ("Starting", "Ready"): the open views hear it.
236
+ const processPool = {
237
+ epoch: 0,
238
+ agents: new Set(),
239
+ max: Math.min(limit.runsMax, limit.acp?.processesMax ?? limit.runsMax),
240
+ changed: () => settingsEvents.changed('state'),
241
+ }
199
242
  // How each agent is doing (agent-health.js): from the last turn or test.
200
243
  const health = createAgentHealth()
201
244
  const agentStatus = (current) => health.status(current, processPool)
@@ -280,6 +323,12 @@ export function agentRouter({
280
323
  // Server shutdown: stop agent processes (src/server/app.js stopChildren).
281
324
  router.close = () => {
282
325
  processPool.epoch += 1
326
+ batchQueue.close()
327
+ settingsEvents.close()
328
+ // An open events stream would hold the server open: end every one.
329
+ eventsClosed = true
330
+ for (const res of eventStreams) res.end()
331
+ eventStreams.clear()
283
332
  const settingsStopped = settingsApi.close()
284
333
  const owned = [...processPool.agents]
285
334
  for (const agent of owned) agent.kill('shutdown')
@@ -382,6 +431,8 @@ export function agentRouter({
382
431
  // meanwhile cannot tell its own edits apart, so it offers no Undo.
383
432
  let turnSerial = 0
384
433
  let undoing = false
434
+ // An agent branch being applied, discarded or opened (one at a time).
435
+ let applying = false
385
436
  // The stored summary says whether Undo was possible; this process knows
386
437
  // whether it still is, and only the thread's latest change offers it (undo-state.js).
387
438
  const withUndoState = (boardId, threadId, messages, all = messages) =>
@@ -393,6 +444,126 @@ export function agentRouter({
393
444
  : message.summary.undo.available === true,
394
445
  all,
395
446
  )
447
+ // ---- Agent branch mode (branch-mode.js) ------------------------------------
448
+ //
449
+ // What dead servers left: worktrees without unapplied commits.
450
+ Promise.resolve()
451
+ .then(() => sweepAgentBranches(config))
452
+ .catch(() => {})
453
+ // The folder a branch-mode turn's agent works in, for acp.js: the project
454
+ // inside the worktree, its own store protected, the files capture runs
455
+ // (auth.loginScript, storageState) still asking before a write there.
456
+ const worktreeWorkdir = (work) => {
457
+ const inWorktree = (file) => {
458
+ const rel = path.relative(config.dir, path.resolve(file))
459
+ return rel && !rel.startsWith('..') && !path.isAbsolute(rel) ? path.join(work.tree.projectDir, rel) : null
460
+ }
461
+ return {
462
+ root: work.tree.projectDir,
463
+ env: worktreeAgentEnv(work.root),
464
+ protect: [path.join(work.tree.projectDir, '.shapewall')],
465
+ sensitivePaths: () => authFiles(config).map(inWorktree).filter(Boolean),
466
+ }
467
+ }
468
+ const noteCard = (reply, id, text) => {
469
+ reply.cards.push({ id, kind: 'note', name: 'note', label: String(text).slice(0, 200) })
470
+ }
471
+ // The end of a branch-mode turn: the agent's work committed on its branch
472
+ // (as the repository's author), the run summary from that commit, and the
473
+ // comment's status: branch-ready when a fix is waiting to be applied.
474
+ const finishBranchTurn = async ({
475
+ work,
476
+ board,
477
+ threadId,
478
+ commentId,
479
+ reply,
480
+ baseline,
481
+ fix,
482
+ previousStatus,
483
+ requestedPages,
484
+ checkoutBefore,
485
+ userText,
486
+ signal,
487
+ }) => {
488
+ const comment = commentId ? getComment(config, board.id, commentId) : null
489
+ const firstLine = (text) =>
490
+ String(text || '')
491
+ .split('\n')
492
+ .map((line) => line.trim())
493
+ .find(Boolean)
494
+ ?.slice(0, 72) || ''
495
+ const message = comment
496
+ ? `Fix comment #${commentId}: ${firstLine(comment.body) || 'agent change'}\n\n${commentLink(config, boardUrlFor(config, { env }), board.id, comment)}\n\nshapewall:${board.id}/${commentId}\n`
497
+ : `Agent change from chat: ${firstLine(userText) || threadId}\n\nshapewall:${board.id}/${threadId}\n`
498
+ let commit = null
499
+ try {
500
+ commit = await commitAgentBranch(config, { ...work, message })
501
+ } catch (error) {
502
+ log(`branch mode: commit: ${error.message}`)
503
+ noteCard(
504
+ reply,
505
+ 'note-branch-commit',
506
+ error instanceof BranchError ? error.message : 'The agent’s work could not be committed on its branch. See the server log.',
507
+ )
508
+ }
509
+ if (commit?.committed && commit.ignoreChanged?.length)
510
+ noteCard(
511
+ reply,
512
+ 'note-branch-gitignore',
513
+ `The agent changed ${commit.ignoreChanged.slice(0, 2).join(', ')}. Files your old ignore rules left out (such as .env) were kept out of its commit anyway.`,
514
+ )
515
+ if (checkoutBefore && (await checkoutSignature(config)) !== checkoutBefore)
516
+ noteCard(
517
+ reply,
518
+ 'note-branch-checkout',
519
+ 'Files in your own checkout changed during this turn. The agent’s own tools can write outside its worktree: check git status.',
520
+ )
521
+ const knownPages = new Set(board.pages.map((page) => page.id))
522
+ const recapture = [...new Set([comment?.pageId, ...requestedPages])].filter((id) => knownPages.has(id)).slice(0, 20)
523
+ let resolved = []
524
+ if (baseline) {
525
+ const done = await finishTurnChanges(config, baseline, { comments: listComments(config, board.id), board, reply, commentId }).catch(
526
+ () => null,
527
+ )
528
+ resolved = done?.summary?.resolved || []
529
+ if (!commit?.committed && done?.summary) reply.summary = done.summary
530
+ }
531
+ if (commit?.committed) {
532
+ work.kept = true
533
+ work.meta.messageId = reply.id
534
+ writeMeta(work.root, work.meta)
535
+ const summary = await starterSummary({ tree: work.tree, commit, branch: work.meta.branch })
536
+ reply.summary = {
537
+ ...summary,
538
+ kind: 'branch',
539
+ resolved,
540
+ recapture,
541
+ branch: { id: work.meta.id, name: work.meta.branch, commit: commit.commit, base: work.meta.base, state: 'ready' },
542
+ undo: { kind: 'agent-branch', available: true },
543
+ }
544
+ }
545
+ if (!fix) return
546
+ if (commit?.committed && reply.status === 'done' && !signal.aborted) {
547
+ const ready = await setAgentStatus(config, board.id, commentId, BRANCH_READY, {
548
+ onlyFrom: ['queued', 'working'],
549
+ actor: 'shapewall',
550
+ branch: work.meta.branch,
551
+ })
552
+ if (ready.changed)
553
+ await addReply(config, board.id, commentId, {
554
+ author: 'shapewall',
555
+ body: `The agent’s fix is ready on branch ${work.meta.branch}. Your checkout is unchanged: apply the branch from the chat to see it on the board, with before and after.`,
556
+ })
557
+ } else if (commit?.committed) {
558
+ await addReply(config, board.id, commentId, {
559
+ author: 'shapewall',
560
+ body: `The agent stopped before it finished. ${stopReason(reply, signal)} What it changed is on branch ${work.meta.branch}: apply or discard it from the chat.`,
561
+ })
562
+ await setAgentStatus(config, board.id, commentId, 'needs-human', { onlyFrom: ['queued', 'working'], actor: 'shapewall' })
563
+ } else {
564
+ await finishFix({ config, board, id: commentId, reply, changed: false, capture, signal, previousStatus, log })
565
+ }
566
+ }
396
567
  const buckets = new Map()
397
568
  const bucketFor = (boardId) => {
398
569
  if (!buckets.has(boardId)) buckets.set(boardId, tokenBucket(limit))
@@ -448,6 +619,7 @@ export function agentRouter({
448
619
  const admitMessage = async (req, res, next) => {
449
620
  if (connectionTesting()) return fail(res, 409, 'test_running', 'A connection test is running. Wait for it before sending.')
450
621
  if (undoing) return fail(res, 409, 'agent_running', 'An undo is running. Try again in a moment.')
622
+ if (applying) return fail(res, 409, 'agent_running', 'An agent branch is being applied. Try again in a moment.')
451
623
  const admission = new AbortController()
452
624
  let release
453
625
  res.once('close', () => {
@@ -485,7 +657,11 @@ export function agentRouter({
485
657
  )
486
658
  }
487
659
  const key = `${board.id}/${threadId}`
488
- if ([...runs.values()].some((run) => run.fix)) return fail(res, 409, 'agent_running', 'A fix is already running in this project.')
660
+ // Branch mode: every ACP turn works in a worktree of its own, so a fix
661
+ // does not have to wait for other turns (nor they for it).
662
+ const branchOn = backend.id === 'acp' && branchModeSettings(config, env).enabled
663
+ if (!branchOn && [...runs.values()].some((run) => run.fix))
664
+ return fail(res, 409, 'agent_running', 'A fix is already running in this project.')
489
665
  if (runs.has(key) || selecting.has(key))
490
666
  return fail(res, 409, 'agent_running', 'An answer or profile change is still running in this thread.')
491
667
  if (runs.size >= limit.runsMax) {
@@ -500,7 +676,7 @@ export function agentRouter({
500
676
  // The slot is held from here until the response closes, whatever happens
501
677
  // (a body too large, a bad image, the board going away mid-answer).
502
678
  const controller = new AbortController()
503
- const run = { controller, current, backend, ownerWrites }
679
+ const run = { controller, current, backend, ownerWrites, branchOn }
504
680
  runs.set(key, run)
505
681
  const timer = setTimeout(() => controller.abort('timeout'), backend.runMsMax || RUN_MS_MAX)
506
682
  res.on('close', () => {
@@ -514,200 +690,505 @@ export function agentRouter({
514
690
  next()
515
691
  }
516
692
 
517
- router.post(
518
- '/boards/:id/threads/:thread/messages',
519
- findThread,
520
- admitMessage,
521
- express.json({ limit: BODY_LIMIT }),
522
- handle(async (req, res, board, threadId) => {
523
- const { controller, current, backend, ownerWrites } = res.locals.run
524
- res.locals.agentOperationRunning = true
525
- try {
526
- const fix = req.body?.fix === true
527
- // Regenerate: the same request again, the new answer in the old one's
528
- // place (threads.js replaceAnswer). Only the thread's latest answer.
529
- const regenerate = req.body?.regenerate === undefined ? null : String(req.body.regenerate)
530
- let previous = null
531
- if (regenerate !== null) {
532
- if (fix) return fail(res, 400, 'bad_regenerate', 'Regenerate and Fix with agent do not go together.')
533
- const stored = readThread(config, board.id, threadId).messages
534
- const last = stored.at(-1)
535
- if (!last || last.id !== regenerate || last.role !== 'assistant' || last.starter || stored.at(-2)?.role !== 'user')
536
- return fail(res, 409, 'cannot_regenerate', 'Only the latest answer to a message can be regenerated.')
537
- if (chatBytes(config, board.id) + Buffer.byteLength(JSON.stringify(last)) > limit.chatBytesMax)
538
- return fail(res, 413, 'chat_quota', 'Chat storage for this board is full. Clear a thread to free space.')
539
- previous = last
540
- }
541
- if (fix && runs.size > 1) return fail(res, 409, 'agent_running', 'Wait for other agent turns before starting a fix.')
542
- res.locals.run.fix = fix
543
- const commentId = commentIdOf(threadId)
544
- if (fix && (!ownerWrites || !allowCapture || !commentId || backend.id !== 'acp')) {
545
- return fail(res, 403, 'fix_needs_owner', 'Fix with agent needs an owner device, captures enabled, and a connected coding agent.')
546
- }
547
- // Images for a model that does not take them are stored, then dropped
548
- // before the request, with a note in the chat (run.js).
549
- // Composer extras: @ mentions (ids, checked against this board) and text files.
550
- const mentions = previous ? [] : validateMentions(config, board, req.body?.mentions)
551
- const files = previous ? [] : validateFiles(req.body?.files)
552
- const input = previous
553
- ? null
554
- : { ...validateUserMessage(board, req.body, { extras: mentions.length + files.length }), mentions, files }
555
- const editOf = req.body?.editOf ?? null
556
- if (editOf !== null && (typeof editOf !== 'string' || editOf.length > 64 || fix || previous))
557
- return fail(res, 400, 'bad_edit', 'editOf is the id of one of your messages in this thread.')
558
- // An edit or a new answer rewrites the thread (and moves an owner
559
- // turn's answer, with its Undo, into an earlier version): like
560
- // bringing back a version, an owner session's thread needs the owner.
561
- if ((editOf !== null || previous) && !controlsAllowed(req, board, threadId)) return controlDenied(res)
562
- // The comment's agent status before this run, to put back when the
563
- // agent never got to work (feedback-loop.js finishFix).
564
- let previousStatus = null
565
- if (fix) {
566
- const comment = getComment(config, board.id, commentId)
567
- previousStatus = comment.agentStatus ?? null
568
- if (comment.status !== 'open' || ['queued', 'working'].includes(comment.agentStatus))
569
- return fail(res, 409, 'comment_busy', 'This comment is resolved or already with an agent.')
570
- const claimed = await setAgentStatus(config, board.id, commentId, 'queued', {
571
- onlyFrom: [null, 'done', 'needs-human'],
572
- actor: 'shapewall',
573
- })
574
- if (!claimed.changed) return fail(res, 409, 'comment_busy', 'This comment is already with an agent.')
575
- }
576
- const snapshot = ownerWrites && commentId && allowCapture ? projectSnapshot(config) : null
577
- let userMessage
693
+ // Sending a message, a fix included. Batch fixes (batch-fixes.js) run each
694
+ // of their fixes through this same chain (runBatchItem below).
695
+ const sendMessage = handle(async (req, res, board, threadId) => {
696
+ const { controller, current, backend, ownerWrites, branchOn } = res.locals.run
697
+ res.locals.agentOperationRunning = true
698
+ // Branch mode: this turn's worktree ({ tree, meta, root }); removed at
699
+ // the end unless the agent's work was committed on its branch.
700
+ let work = null
701
+ try {
702
+ const fix = req.body?.fix === true
703
+ // Regenerate: the same request again, the new answer in the old one's
704
+ // place (threads.js replaceAnswer). Only the thread's latest answer.
705
+ const regenerate = req.body?.regenerate === undefined ? null : String(req.body.regenerate)
706
+ let previous = null
707
+ if (regenerate !== null) {
708
+ if (fix) return fail(res, 400, 'bad_regenerate', 'Regenerate and Fix with agent do not go together.')
709
+ const stored = readThread(config, board.id, threadId).messages
710
+ const last = stored.at(-1)
711
+ if (!last || last.id !== regenerate || last.role !== 'assistant' || last.starter || stored.at(-2)?.role !== 'user')
712
+ return fail(res, 409, 'cannot_regenerate', 'Only the latest answer to a message can be regenerated.')
713
+ if (chatBytes(config, board.id) + Buffer.byteLength(JSON.stringify(last)) > limit.chatBytesMax)
714
+ return fail(res, 413, 'chat_quota', 'Chat storage for this board is full. Clear a thread to free space.')
715
+ previous = last
716
+ }
717
+ if (fix && runs.size > 1 && !branchOn) return fail(res, 409, 'agent_running', 'Wait for other agent turns before starting a fix.')
718
+ res.locals.run.fix = fix
719
+ const commentId = commentIdOf(threadId)
720
+ if (fix && (!ownerWrites || !allowCapture || !commentId || backend.id !== 'acp')) {
721
+ return fail(res, 403, 'fix_needs_owner', 'Fix with agent needs an owner device, captures enabled, and a connected coding agent.')
722
+ }
723
+ // Images for a model that does not take them are stored, then dropped
724
+ // before the request, with a note in the chat (run.js).
725
+ // Composer extras: @ mentions (ids, checked against this board) and text files.
726
+ const mentions = previous ? [] : validateMentions(config, board, req.body?.mentions)
727
+ const files = previous ? [] : validateFiles(req.body?.files)
728
+ const input = previous
729
+ ? null
730
+ : { ...validateUserMessage(board, req.body, { extras: mentions.length + files.length }), mentions, files }
731
+ const editOf = req.body?.editOf ?? null
732
+ if (editOf !== null && (typeof editOf !== 'string' || editOf.length > 64 || fix || previous))
733
+ return fail(res, 400, 'bad_edit', 'editOf is the id of one of your messages in this thread.')
734
+ // An edit or a new answer rewrites the thread (and moves an owner
735
+ // turn's answer, with its Undo, into an earlier version): like
736
+ // bringing back a version, an owner session's thread needs the owner.
737
+ if ((editOf !== null || previous) && !controlsAllowed(req, board, threadId)) return controlDenied(res)
738
+ // The comment's agent status before this run, to put back when the
739
+ // agent never got to work (feedback-loop.js finishFix).
740
+ let previousStatus = null
741
+ if (fix) {
742
+ const comment = getComment(config, board.id, commentId)
743
+ previousStatus = comment.agentStatus ?? null
744
+ if (comment.status !== 'open' || ['queued', 'working'].includes(comment.agentStatus))
745
+ return fail(res, 409, 'comment_busy', 'This comment is resolved or already with an agent.')
746
+ if (comment.agentStatus === BRANCH_READY)
747
+ return fail(res, 409, 'comment_busy', 'A fix for this comment is waiting on a branch. Apply or discard it first.')
748
+ }
749
+ if (branchOn) {
750
+ // The worktree comes first: when the agent cannot work on a branch
751
+ // here (not a repository, no commit yet, a merge in progress), the
752
+ // turn does not start and the comment stays as it is.
753
+ const stored = readThread(config, board.id, threadId).messages
754
+ const asked = stored.filter((item) => item.role === 'user').length + (previous ? 0 : 1)
578
755
  try {
579
- userMessage = previous
580
- ? readThread(config, board.id, threadId).messages.at(-2)
581
- : await appendUserMessage(config, board.id, threadId, input, {
582
- quotaBytes: limit.chatBytesMax,
583
- viewer: res.locals.viewer,
584
- editOf,
585
- })
756
+ work = await createAgentBranch(config, {
757
+ prefix: branchModeSettings(config, env).prefix,
758
+ kind: commentId ? 'comment' : 'chat',
759
+ boardId: board.id,
760
+ threadId,
761
+ commentId,
762
+ text: commentId ? getComment(config, board.id, commentId).body : '',
763
+ n: asked,
764
+ })
586
765
  } catch (error) {
587
- if (fix) await setAgentStatus(config, board.id, commentId, 'needs-human', { onlyFrom: ['queued'], actor: 'shapewall' })
588
- throw error
589
- }
590
- if (ownerWrites) ownerThreads.add(`${board.id}/${threadId}`)
591
- if (editOf !== null) resetSessions(`${board.id}/${threadId}`)
592
- if (controller.signal.aborted) {
593
- if (fix) await setAgentStatus(config, board.id, commentId, 'needs-human', { onlyFrom: ['queued'], actor: 'shapewall' })
594
- return
766
+ if (error instanceof BranchError) return fail(res, error.status, error.code, error.message)
767
+ log(`branch mode: ${error.message}`)
768
+ return fail(res, 500, 'branch_mode', 'The agent’s worktree could not be made. See the server log.')
595
769
  }
596
- if (fix) {
597
- const working = await setAgentStatus(config, board.id, commentId, 'working', { onlyFrom: ['queued'], actor: 'shapewall' })
598
- if (!working.changed)
599
- return fail(res, 409, 'comment_changed', 'The comment changed before the agent started. Review it and try again.')
600
- }
601
- res.writeHead(200, {
602
- 'Content-Type': 'text/event-stream; charset=utf-8',
603
- 'Cache-Control': 'no-store',
604
- 'X-Accel-Buffering': 'no',
605
- Connection: 'keep-alive',
770
+ }
771
+ if (fix) {
772
+ const claimed = await setAgentStatus(config, board.id, commentId, 'queued', {
773
+ onlyFrom: [null, 'done', 'needs-human'],
774
+ actor: 'shapewall',
606
775
  })
607
- res.flushHeaders?.()
608
- const emit = (event, data) => {
609
- if (!res.writableEnded) res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`)
776
+ if (!claimed.changed) return fail(res, 409, 'comment_busy', 'This comment is already with an agent.')
777
+ }
778
+ const snapshot = ownerWrites && commentId && allowCapture && !work ? projectSnapshot(config) : null
779
+ // Branch mode: the checkout should not change. An agent's own tools
780
+ // (its shell) are not sandboxed, so the chat says when it did.
781
+ const checkoutBefore = work ? await checkoutSignature(config) : null
782
+ let userMessage
783
+ try {
784
+ userMessage = previous
785
+ ? readThread(config, board.id, threadId).messages.at(-2)
786
+ : await appendUserMessage(config, board.id, threadId, input, {
787
+ quotaBytes: limit.chatBytesMax,
788
+ viewer: res.locals.viewer,
789
+ editOf,
790
+ })
791
+ } catch (error) {
792
+ if (fix) await setAgentStatus(config, board.id, commentId, 'needs-human', { onlyFrom: ['queued'], actor: 'shapewall' })
793
+ throw error
794
+ }
795
+ if (ownerWrites) ownerThreads.add(`${board.id}/${threadId}`)
796
+ if (editOf !== null) resetSessions(`${board.id}/${threadId}`)
797
+ if (controller.signal.aborted) {
798
+ if (fix) await setAgentStatus(config, board.id, commentId, 'needs-human', { onlyFrom: ['queued'], actor: 'shapewall' })
799
+ return
800
+ }
801
+ if (fix) {
802
+ const working = await setAgentStatus(config, board.id, commentId, 'working', { onlyFrom: ['queued'], actor: 'shapewall' })
803
+ if (!working.changed)
804
+ return fail(res, 409, 'comment_changed', 'The comment changed before the agent started. Review it and try again.')
805
+ }
806
+ res.writeHead(200, {
807
+ 'Content-Type': 'text/event-stream; charset=utf-8',
808
+ 'Cache-Control': 'no-store',
809
+ 'X-Accel-Buffering': 'no',
810
+ Connection: 'keep-alive',
811
+ })
812
+ res.flushHeaders?.()
813
+ const emit = (event, data) => {
814
+ if (!res.writableEnded) res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`)
815
+ }
816
+ res.locals.run.keepalive = setInterval(() => {
817
+ if (!res.writableEnded) res.write(': ping\n\n')
818
+ }, limit.keepaliveMs)
819
+ try {
820
+ emit('user', { message: userMessage })
821
+ let { messages } = readThread(config, board.id, threadId)
822
+ if (previous) {
823
+ messages = messages.filter((item) => item.id !== previous.id)
824
+ // An ACP agent keeps its own session, which saw the old answer.
825
+ if (backend.id === 'acp')
826
+ messages[messages.length - 1] = {
827
+ ...messages.at(-1),
828
+ text: `[Shapewall] The person asked for a new answer to this same message. Answer it again from the start.\n\n${messages.at(-1).text || ''}`,
829
+ }
610
830
  }
611
- res.locals.run.keepalive = setInterval(() => {
612
- if (!res.writableEnded) res.write(': ping\n\n')
613
- }, limit.keepaliveMs)
614
- try {
615
- emit('user', { message: userMessage })
616
- let { messages } = readThread(config, board.id, threadId)
617
- if (previous) {
618
- messages = messages.filter((item) => item.id !== previous.id)
619
- // An ACP agent keeps its own session, which saw the old answer.
620
- if (backend.id === 'acp')
621
- messages[messages.length - 1] = {
622
- ...messages.at(-1),
623
- text: `[Shapewall] The person asked for a new answer to this same message. Answer it again from the start.\n\n${messages.at(-1).text || ''}`,
624
- }
625
- }
626
- const requestedPages = new Set()
627
- // An owner's turn: what it changes becomes the run summary, with Undo
628
- // for an ACP agent's file edits (turn-changes.js).
629
- const serial = ++turnSerial
630
- const overlappedAtStart = runs.size > 1
631
- const baseline = ownerWrites
632
- ? await beginTurnChanges(config, { comments: listComments(config, board.id), files: backend.id === 'acp' }).catch((error) => {
831
+ const requestedPages = new Set()
832
+ // An owner's turn: what it changes becomes the run summary, with Undo
833
+ // for an ACP agent's file edits (turn-changes.js).
834
+ const serial = ++turnSerial
835
+ const overlappedAtStart = runs.size > 1
836
+ const baseline = ownerWrites
837
+ ? await beginTurnChanges(config, { comments: listComments(config, board.id), files: backend.id === 'acp' && !work }).catch(
838
+ (error) => {
633
839
  log(`run summary: ${error.message}`)
634
840
  return null
635
- })
636
- : null
637
- const reply = await runTurn({
638
- config,
841
+ },
842
+ )
843
+ : null
844
+ const reply = await runTurn({
845
+ config,
846
+ board,
847
+ threadId,
848
+ messages,
849
+ backend,
850
+ model: current.model,
851
+ // The composer's model picker (an endpoint's own list), owner turns only.
852
+ modelOverride: ownerWrites ? chosenModel(current, `${board.id}/${threadId}`) : null,
853
+ // The owner's own feedback notes go to the owner's turns only (ratings-core.js downNotes).
854
+ ownerTurn: res.locals.viewer === 'owner',
855
+ // The composer's options for this chat (an ACP session's own), owner turns only.
856
+ choices: ownerWrites && backend.id === 'acp' ? cleanChoices(req.body?.options) : null,
857
+ emit,
858
+ signal: controller.signal,
859
+ ownerWrites,
860
+ allowCapture,
861
+ capture,
862
+ requestedPages,
863
+ // Branch mode: the agent's cwd, file sandbox and commands are the worktree.
864
+ workdir: work ? worktreeWorkdir(work) : null,
865
+ })
866
+ health.record(current, reply)
867
+ // What the profile offers, as this turn's session saw it; a turn that
868
+ // could not sign in drops what was known (the next look starts fresh).
869
+ if (reply.error?.code === 'agent_auth') optionsCache.drop(current)
870
+ else if (backend.id === 'acp') rememberSession(current, backend, `${board.id}/${threadId}`)
871
+ if (work) {
872
+ emit('status', { text: 'Committing the agent’s work on its branch' })
873
+ await finishBranchTurn({
874
+ work,
639
875
  board,
640
876
  threadId,
641
- messages,
642
- backend,
643
- model: current.model,
644
- // The composer's model picker (an endpoint's own list), owner turns only.
645
- modelOverride: ownerWrites ? chosenModel(current, `${board.id}/${threadId}`) : null,
646
- // The owner's own feedback notes go to the owner's turns only (ratings-core.js downNotes).
647
- ownerTurn: res.locals.viewer === 'owner',
648
- // The composer's options for this chat (an ACP session's own), owner turns only.
649
- choices: ownerWrites && backend.id === 'acp' ? cleanChoices(req.body?.options) : null,
650
- emit,
651
- signal: controller.signal,
652
- ownerWrites,
653
- allowCapture,
654
- capture,
877
+ commentId,
878
+ reply,
879
+ baseline,
880
+ fix,
881
+ previousStatus,
655
882
  requestedPages,
883
+ checkoutBefore,
884
+ userText: userMessage?.text || '',
885
+ signal: controller.signal,
656
886
  })
657
- health.record(current, reply)
658
- // What the profile offers, as this turn's session saw it; a turn that
659
- // could not sign in drops what was known (the next look starts fresh).
660
- if (reply.error?.code === 'agent_auth') optionsCache.drop(current)
661
- else if (backend.id === 'acp') rememberSession(current, backend, `${board.id}/${threadId}`)
662
- if (baseline) {
663
- try {
664
- const { summary, undo } = await finishTurnChanges(config, baseline, {
665
- comments: listComments(config, board.id),
666
- board,
667
- reply,
668
- pageIds: [...requestedPages],
669
- commentId,
670
- overlapped: overlappedAtStart || serial !== turnSerial || runs.size > 1,
671
- })
672
- if (summary) reply.summary = summary
673
- if (undo) rememberUndo(`${board.id}/${threadId}/${reply.id}`, undo)
674
- } catch (error) {
675
- log(`run summary: ${error.message}`)
676
- }
887
+ } else if (baseline) {
888
+ try {
889
+ const { summary, undo } = await finishTurnChanges(config, baseline, {
890
+ comments: listComments(config, board.id),
891
+ board,
892
+ reply,
893
+ pageIds: [...requestedPages],
894
+ commentId,
895
+ overlapped: overlappedAtStart || serial !== turnSerial || runs.size > 1,
896
+ })
897
+ if (summary) reply.summary = summary
898
+ if (undo) rememberUndo(`${board.id}/${threadId}/${reply.id}`, undo)
899
+ } catch (error) {
900
+ log(`run summary: ${error.message}`)
677
901
  }
678
- if (snapshot) {
679
- const changed = projectChanged(snapshot, projectSnapshot(config))
680
- if (fix || changed) {
681
- emit('status', { text: changed ? 'Recapturing the changed page' : 'Checking the fix' })
682
- await finishFix({
683
- config,
684
- board,
685
- id: commentId,
686
- reply,
687
- changed,
688
- pageIds: [...requestedPages],
689
- capture,
690
- signal: controller.signal,
691
- previousStatus,
692
- log,
693
- })
694
- }
902
+ }
903
+ if (snapshot) {
904
+ const changed = projectChanged(snapshot, projectSnapshot(config))
905
+ if (fix || changed) {
906
+ emit('status', { text: changed ? 'Recapturing the changed page' : 'Checking the fix' })
907
+ await finishFix({
908
+ config,
909
+ board,
910
+ id: commentId,
911
+ reply,
912
+ changed,
913
+ pageIds: [...requestedPages],
914
+ capture,
915
+ signal: controller.signal,
916
+ previousStatus,
917
+ log,
918
+ })
695
919
  }
696
- if (previous) await replaceAnswer(config, board.id, threadId, previous.id, reply)
697
- else await appendAssistantMessage(config, board.id, threadId, reply)
698
- emit('done', { message: reply })
699
- } finally {
700
- clearInterval(res.locals.run.keepalive)
701
- if (!res.writableEnded) res.end()
702
920
  }
921
+ if (previous) await replaceAnswer(config, board.id, threadId, previous.id, reply)
922
+ else await appendAssistantMessage(config, board.id, threadId, reply)
923
+ emit('done', { message: reply })
703
924
  } finally {
704
- try {
705
- await res.locals.releaseAgentOperation()
706
- } finally {
707
- res.locals.agentOperationRunning = false
708
- if (runs.get(`${board.id}/${threadId}`) === res.locals.run) runs.delete(`${board.id}/${threadId}`)
925
+ clearInterval(res.locals.run.keepalive)
926
+ if (!res.writableEnded) res.end()
927
+ }
928
+ } finally {
929
+ // A worktree that holds no committed work (nothing changed, the turn
930
+ // never started, an error): removed now, not left for the sweep.
931
+ if (work && !work.kept) await discardAgentBranch(config, work.meta).catch((error) => log(`branch mode: ${error.message}`))
932
+ try {
933
+ await res.locals.releaseAgentOperation()
934
+ } finally {
935
+ res.locals.agentOperationRunning = false
936
+ if (runs.get(`${board.id}/${threadId}`) === res.locals.run) runs.delete(`${board.id}/${threadId}`)
937
+ }
938
+ }
939
+ })
940
+ router.post('/boards/:id/threads/:thread/messages', findThread, admitMessage, express.json({ limit: BODY_LIMIT }), sendMessage)
941
+
942
+ // ---- Batch fixes (batch-fixes.js) -------------------------------------------
943
+ //
944
+ // One fix of a batch: the request that started the batch, replayed in this
945
+ // process for one comment thread (its owner headers, never stored), through
946
+ // findThread, admitMessage and sendMessage like a click on Fix with agent.
947
+ // The answer streams into a response object here instead of a browser;
948
+ // the thread keeps it like any other.
949
+ const batchRequest = (startReq, boardId, commentId) => ({
950
+ method: 'POST',
951
+ url: `/boards/${boardId}/threads/comment-${commentId}/messages`,
952
+ headers: Object.fromEntries(
953
+ Object.entries(startReq.headers).filter(([name]) => !['content-length', 'content-type', 'transfer-encoding'].includes(name)),
954
+ ),
955
+ socket: { remoteAddress: startReq.socket?.remoteAddress },
956
+ secure: startReq.secure,
957
+ shapewallAccess: startReq.shapewallAccess,
958
+ localAccountChecked: startReq.localAccountChecked,
959
+ localAccount: startReq.localAccount,
960
+ aborted: false,
961
+ params: { id: boardId, thread: `comment-${commentId}` },
962
+ query: {},
963
+ body: { text: BATCH_FIX_PROMPT, fix: true },
964
+ })
965
+ const batchResponse = ({ onEvent }) => {
966
+ const res = new EventEmitter()
967
+ let buffer = ''
968
+ Object.assign(res, {
969
+ locals: {},
970
+ statusCode: 200,
971
+ headersSent: false,
972
+ writableEnded: false,
973
+ destroyed: false,
974
+ body: null,
975
+ status(code) {
976
+ res.statusCode = code
977
+ return res
978
+ },
979
+ json(value) {
980
+ res.body = value
981
+ res.headersSent = true
982
+ res.end()
983
+ return res
984
+ },
985
+ setHeader() {},
986
+ append() {},
987
+ writeHead(code) {
988
+ res.statusCode = code
989
+ res.headersSent = true
990
+ },
991
+ flushHeaders() {},
992
+ write(chunk) {
993
+ buffer += String(chunk)
994
+ let end
995
+ while ((end = buffer.indexOf('\n\n')) >= 0) {
996
+ const block = buffer.slice(0, end)
997
+ buffer = buffer.slice(end + 2)
998
+ const name = /^event: (.+)$/m.exec(block)?.[1]
999
+ const data = /^data: (.+)$/m.exec(block)?.[1]
1000
+ if (!name || !data) continue
1001
+ try {
1002
+ onEvent(name, JSON.parse(data))
1003
+ } catch {
1004
+ // A malformed event changes nothing about the fix itself.
1005
+ }
709
1006
  }
1007
+ return true
1008
+ },
1009
+ end() {
1010
+ if (res.writableEnded) return
1011
+ res.writableEnded = true
1012
+ res.emit('finish')
1013
+ res.emit('close')
1014
+ },
1015
+ // The batch was cancelled before this fix took its slot.
1016
+ destroy() {
1017
+ if (res.destroyed || res.writableEnded) return
1018
+ res.destroyed = true
1019
+ res.emit('close')
1020
+ },
1021
+ })
1022
+ return res
1023
+ }
1024
+ // Starts one fix; { done, cancel } for the queue. Busy (another answer,
1025
+ // the rate limit) waits and tries again for up to a minute.
1026
+ const runBatchItem =
1027
+ (startReq) =>
1028
+ async (boardId, commentId, { onApproval }) => {
1029
+ const key = `${boardId}/comment-${commentId}`
1030
+ let cancelled = false
1031
+ let res = null
1032
+ const cancel = () => {
1033
+ cancelled = true
1034
+ const run = runs.get(key)
1035
+ if (run && run === res?.locals.run) run.controller.abort('stopped')
1036
+ else res?.destroy()
710
1037
  }
1038
+ const once = async () => {
1039
+ const cards = new Map()
1040
+ let answer = null
1041
+ res = batchResponse({
1042
+ onEvent: (name, data) => {
1043
+ if (name === 'tool' && data?.id) {
1044
+ cards.set(data.id, data)
1045
+ const pending = [...cards.values()].find((card) => card.kind === 'permission' && card.state === 'pending')
1046
+ // Everything the card shows the person, so the batch's card can be answered as fully informed.
1047
+ onApproval(
1048
+ pending
1049
+ ? {
1050
+ id: pending.id,
1051
+ label: pending.label,
1052
+ name: pending.name,
1053
+ detail: pending.detail || null,
1054
+ diff: pending.diff || null,
1055
+ options: pending.options || [],
1056
+ expiresAt: pending.expiresAt || null,
1057
+ }
1058
+ : null,
1059
+ )
1060
+ }
1061
+ if (name === 'done') answer = data?.message || null
1062
+ },
1063
+ })
1064
+ const req = batchRequest(startReq, boardId, commentId)
1065
+ const closed = new Promise((resolve) => res.once('close', () => resolve(false)))
1066
+ const passed = (middleware) => Promise.race([new Promise((resolve) => middleware(req, res, () => resolve(true))), closed])
1067
+ if ((await passed(findThread)) && (await passed(admitMessage))) await sendMessage(req, res)
1068
+ return { status: res.statusCode, error: res.body?.error || null, answer }
1069
+ }
1070
+ const done = (async () => {
1071
+ for (let attempt = 0; ; attempt++) {
1072
+ if (cancelled) return { skipped: true, message: 'Stopped when the batch was cancelled.' }
1073
+ const result = await once()
1074
+ const busy = [429].includes(result.status) || ['agent_running', 'test_running'].includes(result.error?.code)
1075
+ if (busy && attempt < 30 && !cancelled) {
1076
+ await new Promise((resolve) => setTimeout(resolve, 2000).unref?.())
1077
+ continue
1078
+ }
1079
+ if (result.error) {
1080
+ const skip = ['comment_busy', 'comment_changed', 'unknown_thread'].includes(result.error.code)
1081
+ return { fixed: false, skipped: skip, message: result.error.message }
1082
+ }
1083
+ let comment = null
1084
+ try {
1085
+ comment = getComment(config, boardId, commentId)
1086
+ } catch {
1087
+ return { skipped: true, message: 'The comment was deleted.' }
1088
+ }
1089
+ const fixed = ['done', BRANCH_READY].includes(comment.agentStatus)
1090
+ return {
1091
+ fixed,
1092
+ message: fixed ? null : stopReason(result.answer, cancelled ? { aborted: true } : null),
1093
+ }
1094
+ }
1095
+ })()
1096
+ return { done, cancel }
1097
+ }
1098
+ // The queue's runItem for the batch being started (the queue runs one batch
1099
+ // per board, so the start request in effect is that batch's).
1100
+ const batchStarts = new Map()
1101
+ const batchQueue = createBatchQueue({
1102
+ runItem: (boardId, commentId, hooks) => runBatchItem(batchStarts.get(boardId))(boardId, commentId, hooks),
1103
+ onChange: (boardId) => batchEvents.changed(boardId),
1104
+ log,
1105
+ })
1106
+ router.batchQueue = batchQueue
1107
+ // Who hears it: the owner's open GET /events streams.
1108
+ const batchListeners = new Set()
1109
+ const batchEvents = {
1110
+ changed: (boardId) => {
1111
+ for (const listener of [...batchListeners]) listener({ boardId, at: new Date().toISOString() })
1112
+ },
1113
+ }
1114
+ // Owner only, the same as Fix with agent: an owner device, captures on, a
1115
+ // coding agent over ACP. Hosted boards do not mount this router.
1116
+ const batchAllowed = (req, res) => {
1117
+ if (!ownerWritesFor(req) || !sameOriginWrite(req) || !agentOwner(req)) {
1118
+ controlDenied(res)
1119
+ return false
1120
+ }
1121
+ const current = settings()
1122
+ if (!allowCapture || current.backend !== 'acp' || !current.configured) {
1123
+ fail(res, 403, 'fix_needs_owner', 'Fix with agent needs an owner device, captures enabled, and a connected coding agent.')
1124
+ return false
1125
+ }
1126
+ return true
1127
+ }
1128
+ router.get(
1129
+ '/boards/:id/batch',
1130
+ findThread,
1131
+ handle((req, res, board) => {
1132
+ if (!ownerWritesFor(req)) return controlDenied(res)
1133
+ res.json({ settings: batchFixSettings(config, env), batch: batchQueue.state(board.id) })
1134
+ }),
1135
+ )
1136
+ router.post(
1137
+ '/boards/:id/batch',
1138
+ findThread,
1139
+ express.json({ limit: SMALL_BODY_LIMIT }),
1140
+ handle((req, res, board) => {
1141
+ if (!batchAllowed(req, res)) return
1142
+ const options = batchFixSettings(config, env)
1143
+ if (!options.enabled) return fail(res, 403, 'batch_off', 'Batch fixes are off. Turn them on in Agents first.')
1144
+ if (batchQueue.running(board.id))
1145
+ return fail(res, 409, 'batch_running', 'A batch of fixes is already running on this board. Wait for it or cancel it first.')
1146
+ const given = req.body?.commentIds
1147
+ if (!Array.isArray(given) || !given.length || given.length > 500)
1148
+ return fail(res, 400, 'bad_batch', 'commentIds: the ids of the open comments to fix, in order.')
1149
+ if (given.some((id) => !Number.isSafeInteger(id) || id < 1)) return fail(res, 400, 'bad_batch', 'commentIds: whole numbers.')
1150
+ const comments = new Map(listComments(config, board.id).map((comment) => [comment.id, comment]))
1151
+ const ids = [...new Set(given)]
1152
+ for (const id of ids) {
1153
+ const comment = comments.get(id)
1154
+ if (!comment) return fail(res, 404, 'unknown_comment', `No comment #${id} on this board.`)
1155
+ if (comment.status !== 'open') return fail(res, 409, 'comment_busy', `Comment #${id} is resolved.`)
1156
+ if (['queued', 'working', BRANCH_READY].includes(comment.agentStatus))
1157
+ return fail(res, 409, 'comment_busy', `Comment #${id} is already with an agent.`)
1158
+ }
1159
+ // What the replayed requests need from this one (owner headers, kept in memory only).
1160
+ batchStarts.set(board.id, {
1161
+ headers: { ...req.headers },
1162
+ socket: { remoteAddress: req.socket?.remoteAddress },
1163
+ secure: req.secure,
1164
+ shapewallAccess: req.shapewallAccess,
1165
+ localAccountChecked: req.localAccountChecked,
1166
+ localAccount: req.localAccount,
1167
+ })
1168
+ const batch = batchQueue.start({
1169
+ boardId: board.id,
1170
+ commentIds: ids,
1171
+ max: options.max,
1172
+ agent: publicAgentInfo(settings()).label || null,
1173
+ })
1174
+ res.json({ batch })
1175
+ }),
1176
+ )
1177
+ router.post(
1178
+ '/boards/:id/batch/cancel',
1179
+ findThread,
1180
+ handle((req, res, board) => {
1181
+ if (!ownerWritesFor(req) || !sameOriginWrite(req)) return controlDenied(res)
1182
+ res.json({ batch: batchQueue.cancel(board.id) })
1183
+ }),
1184
+ )
1185
+ router.delete(
1186
+ '/boards/:id/batch',
1187
+ findThread,
1188
+ handle((req, res, board) => {
1189
+ if (!ownerWritesFor(req) || !sameOriginWrite(req)) return controlDenied(res)
1190
+ batchQueue.clear(board.id)
1191
+ res.json({ batch: null })
711
1192
  }),
712
1193
  )
713
1194
 
@@ -715,6 +1196,52 @@ export function agentRouter({
715
1196
 
716
1197
  router.use(express.json({ limit: SMALL_BODY_LIMIT }))
717
1198
 
1199
+ // The agent settings changed (here, in another tab, or in the file): a
1200
+ // nudge only, no settings in it; each view refetches what it shows.
1201
+ // The owner only (the settings token in its header, a paired device or the
1202
+ // signed-in account): the page reads it with fetch, which can send the
1203
+ // header, not with EventSource. At most EVENT_STREAMS_MAX open at once: a
1204
+ // new one ends the oldest (a reloaded page leaves its old stream behind
1205
+ // until the socket closes).
1206
+ router.get('/events', (req, res) => {
1207
+ if (eventsClosed) return res.status(503).json({ error: { code: 'stopping', message: 'The server is stopping.' } })
1208
+ if (!agentOwner(req)) return controlDenied(res)
1209
+ while (eventStreams.size >= EVENT_STREAMS_MAX) {
1210
+ const oldest = eventStreams.values().next().value
1211
+ eventStreams.delete(oldest)
1212
+ oldest.end()
1213
+ }
1214
+ res.writeHead(200, {
1215
+ 'Content-Type': 'text/event-stream; charset=utf-8',
1216
+ 'Cache-Control': 'no-store',
1217
+ 'X-Accel-Buffering': 'no',
1218
+ Connection: 'keep-alive',
1219
+ })
1220
+ res.flushHeaders?.()
1221
+ res.write(': connected\n\n')
1222
+ const stop = settingsEvents.subscribe((event) => {
1223
+ if (!res.writableEnded) res.write(`event: ${AGENTS_EVENT}\ndata: ${JSON.stringify({ at: event.at })}\n\n`)
1224
+ })
1225
+ // A board's batch of fixes moved on: { board, at }, a nudge like "agents".
1226
+ // What the batch holds is only on GET /boards/:id/batch.
1227
+ const batchListener = (event) => {
1228
+ if (!res.writableEnded) res.write(`event: batch\ndata: ${JSON.stringify({ board: event.boardId, at: event.at })}\n\n`)
1229
+ }
1230
+ batchListeners.add(batchListener)
1231
+ const keepalive = setInterval(() => {
1232
+ if (!res.writableEnded) res.write(': ping\n\n')
1233
+ }, limit.keepaliveMs)
1234
+ keepalive.unref?.()
1235
+ eventStreams.add(res)
1236
+ const done = () => {
1237
+ clearInterval(keepalive)
1238
+ stop()
1239
+ batchListeners.delete(batchListener)
1240
+ eventStreams.delete(res)
1241
+ }
1242
+ req.on('close', done)
1243
+ res.on('finish', done)
1244
+ })
718
1245
  router.get('/status', (req, res) =>
719
1246
  res.json({
720
1247
  ...publicAgentInfo(settings()),
@@ -727,6 +1254,7 @@ export function agentRouter({
727
1254
  }),
728
1255
  )
729
1256
  const settingsApi = settingsRoutes({
1257
+ events: settingsEvents,
730
1258
  config,
731
1259
  env,
732
1260
  fetchImpl,
@@ -1022,6 +1550,7 @@ export function agentRouter({
1022
1550
  undoing = true
1023
1551
  try {
1024
1552
  let undone
1553
+ let branchState = null
1025
1554
  if (summary.undo.kind === 'files') {
1026
1555
  const record = undoRecords.get(key)
1027
1556
  if (!record) {
@@ -1045,6 +1574,22 @@ export function agentRouter({
1045
1574
  commit: summary.undo.commit,
1046
1575
  })
1047
1576
  undone = { kind: 'branch', branch: result.branch }
1577
+ } else if (summary.undo.kind === 'agent-branch' && summary.undo.available && summary.branch) {
1578
+ // Branch mode: before Apply, Undo is Discard; after it, a revert
1579
+ // of what Apply brought in, only while that is still the branch tip.
1580
+ if (applying) return fail(res, 409, 'agent_running', 'An apply is running. Try again in a moment.')
1581
+ if (summary.branch.state === 'ready') {
1582
+ const meta = readMeta(config, summary.branch.id)
1583
+ if (!meta || meta.messageId !== messageId)
1584
+ return fail(res, 410, 'undo_unavailable', 'That branch is no longer here: it was applied or removed outside the chat.')
1585
+ await discardAgentBranch(config, meta)
1586
+ undone = { kind: 'branch', branch: meta.branch }
1587
+ branchState = 'discarded'
1588
+ } else if (summary.branch.state === 'applied' && summary.branch.applied) {
1589
+ const reverted = await revertAppliedBranch(config, summary.branch.applied)
1590
+ undone = { kind: 'revert', branch: summary.branch.name, commit: reverted.head }
1591
+ branchState = 'reverted'
1592
+ } else return fail(res, 409, 'undo_unavailable', 'Undo is not available for this branch any more.')
1048
1593
  } else {
1049
1594
  return fail(
1050
1595
  res,
@@ -1057,7 +1602,10 @@ export function agentRouter({
1057
1602
  const commentId = commentIdOf(threadId)
1058
1603
  if (commentId) {
1059
1604
  try {
1060
- const changed = await setAgentStatus(config, board.id, commentId, 'needs-human', { onlyFrom: ['done'], actor: 'shapewall' })
1605
+ const changed = await setAgentStatus(config, board.id, commentId, 'needs-human', {
1606
+ onlyFrom: ['done', BRANCH_READY],
1607
+ actor: 'shapewall',
1608
+ })
1061
1609
  if (changed.changed) {
1062
1610
  await addReply(config, board.id, commentId, {
1063
1611
  author: 'shapewall',
@@ -1071,7 +1619,11 @@ export function agentRouter({
1071
1619
  const at = new Date().toISOString()
1072
1620
  let stored = null
1073
1621
  await changeMessage(config, board.id, threadId, messageId, (item) => {
1074
- item.summary = { ...item.summary, undone: { ...undone, at, by: req.shapewallAccess?.name || null } }
1622
+ item.summary = {
1623
+ ...item.summary,
1624
+ ...(branchState ? { branch: { ...item.summary.branch, state: branchState } } : {}),
1625
+ undone: { ...undone, at, by: req.shapewallAccess?.name || null },
1626
+ }
1075
1627
  stored = item
1076
1628
  })
1077
1629
  res.json({ ok: true, message: withUndoState(board.id, threadId, [stored], readThread(config, board.id, threadId).messages)[0] })
@@ -1175,6 +1727,169 @@ export function agentRouter({
1175
1727
  }),
1176
1728
  )
1177
1729
 
1730
+ // ---- Agent branches (branch mode, branch-mode.js) ----------------------------
1731
+ //
1732
+ // GET /api/agent/branches the agent's branches waiting here
1733
+ // POST /api/agent/branches/:id/apply bring one into the checkout, then recapture
1734
+ // POST /api/agent/branches/:id/discard delete its worktree and branch
1735
+ // POST /api/agent/branches/:id/open open its folder (this machine only)
1736
+ //
1737
+ // Owner device only; writes from this board's page. The folder's path is
1738
+ // given only to a direct local request (the person at this machine).
1739
+ // The project's folder in the worktree (a subfolder when the project is one in its repository).
1740
+ const branchFolder = (meta) => path.join(agentWorktreeRoot(config), meta.id, ...(meta.sub ? meta.sub.split('/') : []))
1741
+ const publicBranch = (item, local) => ({
1742
+ id: item.id,
1743
+ branch: item.branch,
1744
+ boardId: item.boardId,
1745
+ threadId: item.threadId,
1746
+ commentId: item.commentId,
1747
+ messageId: item.messageId,
1748
+ commit: item.commit,
1749
+ files: item.files,
1750
+ createdAt: item.createdAt,
1751
+ state: item.state,
1752
+ ...(local && item.worktree ? { folder: branchFolder(item) } : {}),
1753
+ })
1754
+ const branchOwner = (req, res, next) => {
1755
+ if (!ownerWritesFor(req)) return controlDenied(res)
1756
+ if (req.method !== 'GET' && !sameOriginWrite(req)) return controlDenied(res)
1757
+ next()
1758
+ }
1759
+ const branchMessage = async (meta, change) => {
1760
+ if (!meta.messageId) return null
1761
+ let stored = null
1762
+ await changeMessage(config, meta.boardId, meta.threadId, meta.messageId, (item) => {
1763
+ if (item.summary?.branch?.id !== meta.id) return
1764
+ change(item)
1765
+ stored = item
1766
+ }).catch((error) => log(`branch mode: ${error.message}`))
1767
+ if (!stored) return null
1768
+ return withUndoState(meta.boardId, meta.threadId, [stored], readThread(config, meta.boardId, meta.threadId).messages)[0]
1769
+ }
1770
+ const branchAction = (action) =>
1771
+ handle(async (req, res) => {
1772
+ if (runs.size || selecting.size || undoing || applying)
1773
+ return fail(res, 409, 'agent_running', 'Wait for the agent to finish, then try again.')
1774
+ const meta = readMeta(config, req.params.branch)
1775
+ if (!meta) return fail(res, 404, 'unknown_branch', 'No such agent branch. It may have been applied or discarded already.')
1776
+ if (!meta.commit) return fail(res, 409, 'branch_pending', 'The agent has not committed anything on this branch yet.')
1777
+ const board = config.boards.find((item) => item.id === meta.boardId)
1778
+ if (!board) return fail(res, 404, 'unknown_board', 'The board of this branch is gone.')
1779
+ applying = true
1780
+ try {
1781
+ await action(req, res, meta, board)
1782
+ } finally {
1783
+ applying = false
1784
+ }
1785
+ })
1786
+ router.get(
1787
+ '/branches',
1788
+ branchOwner,
1789
+ handle(async (req, res) => {
1790
+ const local = localRequest(req) && settingsTokenOk(req, settingsToken)
1791
+ const items = await listAgentBranches(config)
1792
+ res.json({ branches: items.filter((item) => item.commit).map((item) => publicBranch(item, local)) })
1793
+ }),
1794
+ )
1795
+ router.post(
1796
+ '/branches/:branch/apply',
1797
+ branchOwner,
1798
+ branchAction(async (req, res, meta, board) => {
1799
+ // Serialized with captures of this board: no capture reads the
1800
+ // checkout while Apply changes it.
1801
+ const dir = boardDir(config, board.id)
1802
+ fs.mkdirSync(dir, { recursive: true })
1803
+ const lockFile = path.join(dir, 'capture.lock')
1804
+ if (!acquireLock(lockFile))
1805
+ return fail(res, 409, 'capture_running', 'A capture of this board is running. Apply again when it is done.')
1806
+ let applied
1807
+ try {
1808
+ applied = await applyAgentBranch(config, meta, { message: `Apply ${meta.branch} (Shapewall agent)` })
1809
+ } finally {
1810
+ releaseLock(lockFile)
1811
+ }
1812
+ const at = new Date().toISOString()
1813
+ const message = await branchMessage(meta, (item) => {
1814
+ item.summary = { ...item.summary, branch: { ...item.summary.branch, state: 'applied', applied: { ...applied, at } } }
1815
+ })
1816
+ // The comment: recaptured from the dev server, with before and after.
1817
+ let comment = null
1818
+ if (meta.commentId) {
1819
+ try {
1820
+ const moved = await setAgentStatus(config, board.id, meta.commentId, 'working', { onlyFrom: [BRANCH_READY], actor: 'shapewall' })
1821
+ if (moved.changed && allowCapture) {
1822
+ await finishFix({
1823
+ config,
1824
+ board,
1825
+ id: meta.commentId,
1826
+ reply: { status: 'done' },
1827
+ changed: true,
1828
+ pageIds: message?.summary?.recapture || [],
1829
+ capture,
1830
+ signal: null,
1831
+ log,
1832
+ })
1833
+ } else if (moved.changed)
1834
+ await setAgentStatus(config, board.id, meta.commentId, 'done', { onlyFrom: ['working'], actor: 'shapewall' })
1835
+ comment = getComment(config, board.id, meta.commentId)
1836
+ } catch (error) {
1837
+ log(`branch mode: after apply: ${error.message}`)
1838
+ }
1839
+ }
1840
+ res.json({ ok: true, applied, message, comment: comment ? { id: comment.id, agentStatus: comment.agentStatus } : null })
1841
+ }),
1842
+ )
1843
+ router.post(
1844
+ '/branches/:branch/discard',
1845
+ branchOwner,
1846
+ branchAction(async (req, res, meta, board) => {
1847
+ const result = await discardAgentBranch(config, meta)
1848
+ const at = new Date().toISOString()
1849
+ const message = await branchMessage(meta, (item) => {
1850
+ item.summary = {
1851
+ ...item.summary,
1852
+ branch: { ...item.summary.branch, state: 'discarded' },
1853
+ undone: { kind: 'branch', branch: meta.branch, at, by: req.shapewallAccess?.name || null },
1854
+ }
1855
+ })
1856
+ if (meta.commentId) {
1857
+ try {
1858
+ const changed = await setAgentStatus(config, board.id, meta.commentId, 'needs-human', {
1859
+ onlyFrom: [BRANCH_READY],
1860
+ actor: 'shapewall',
1861
+ })
1862
+ if (changed.changed)
1863
+ await addReply(config, board.id, meta.commentId, {
1864
+ author: 'shapewall',
1865
+ body: `The agent’s branch ${meta.branch} was discarded. Your checkout was not touched.`,
1866
+ })
1867
+ } catch (error) {
1868
+ log(`branch mode: after discard: ${error.message}`)
1869
+ }
1870
+ }
1871
+ res.json({ ok: true, ...result, message })
1872
+ }),
1873
+ )
1874
+ router.post(
1875
+ '/branches/:branch/open',
1876
+ branchOwner,
1877
+ branchAction(async (req, res, meta) => {
1878
+ if (!(localRequest(req) && settingsTokenOk(req, settingsToken)))
1879
+ return fail(res, 403, 'open_needs_local', 'Open the folder on the computer running Shapewall.')
1880
+ const folder = branchFolder(meta)
1881
+ if (!fs.existsSync(folder)) return fail(res, 404, 'unknown_branch', 'The branch’s folder is gone. Its branch is still in Git.')
1882
+ const name = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'explorer' : 'xdg-open'
1883
+ const program = absoluteProgram(name, env)
1884
+ if (!program) return res.json({ opened: false, folder })
1885
+ // The folder is an absolute path Shapewall made (never an option), with no shell.
1886
+ const child = spawn(program, [folder], { detached: true, stdio: 'ignore', shell: false })
1887
+ child.on('error', () => {})
1888
+ child.unref()
1889
+ res.json({ opened: true, folder })
1890
+ }),
1891
+ )
1892
+
1178
1893
  router.use((req, res) => fail(res, 404, 'not_found', 'No such API route.'))
1179
1894
  return router
1180
1895
  }