shapewall 0.2.4 → 0.3.1

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 (105) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/README.md +35 -1
  3. package/dist/agent/package-lock.json +2 -2
  4. package/dist/client/assets/BoardChat-V1BCXdDB.js +27 -0
  5. package/dist/client/assets/{BuzzDialog-CZdwIQLF.js → BuzzDialog-Ds7Thpfe.js} +1 -1
  6. package/dist/client/assets/ChecksPanel-Cm22bCdQ.js +1 -0
  7. package/dist/client/assets/History-U0bD1BjA.js +1 -0
  8. package/dist/client/assets/{LocalSettings-B5gtfKEl.js → LocalSettings-DOSuW7VC.js} +1 -1
  9. package/dist/client/assets/{Management-BxEUZAmI.js → Management-BFcKFdbp.js} +1 -1
  10. package/dist/client/assets/QuickConnect-DyfPSWbV.js +1 -0
  11. package/dist/client/assets/{ShareDialog-CdVAreaw.js → ShareDialog-BouvNraR.js} +2 -2
  12. package/dist/client/assets/{app-update-dialog-BONq9YFp.js → app-update-dialog-DH2Mgby8.js} +1 -1
  13. package/dist/client/assets/{circle-x-BmT6vHKl.js → circle-x-PRxj7kPi.js} +1 -1
  14. package/dist/client/assets/index-C9S7HL_o.js +37 -0
  15. package/dist/client/assets/index-CXtuJi1K.css +2 -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-lJjkhUcH.js +1 -0
  23. package/dist/console/assets/LocalConsole-BZHqYKnt.js +1 -0
  24. package/dist/console/assets/{app-update-dialog-o4QY_jL9.js → app-update-dialog-DCM9sYE3.js} +1 -1
  25. package/dist/console/assets/console-C2LsvEco.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/hosted.md +10 -0
  35. package/docs/pull-requests.md +193 -0
  36. package/package.json +2 -1
  37. package/src/agent/backends/acp.js +23 -6
  38. package/src/agent/batch-fixes.js +45 -0
  39. package/src/agent/batch-queue.js +204 -0
  40. package/src/agent/branch-mode.js +753 -0
  41. package/src/agent/config.js +6 -0
  42. package/src/agent/fix-prompt.js +5 -0
  43. package/src/agent/routes.js +850 -185
  44. package/src/agent/run.js +8 -1
  45. package/src/agent/settings-routes.js +69 -1
  46. package/src/agent/starter/worktree.js +286 -29
  47. package/src/agent/threads.js +2 -7
  48. package/src/agent/tools.js +51 -0
  49. package/src/agent/user-settings.js +33 -2
  50. package/src/ci/capture.js +103 -0
  51. package/src/ci/compare.js +267 -0
  52. package/src/ci/github.js +307 -0
  53. package/src/ci/images.js +76 -0
  54. package/src/ci/settings.js +211 -0
  55. package/src/ci/start.js +148 -0
  56. package/src/ci/summary.js +262 -0
  57. package/src/ci/workflow.js +249 -0
  58. package/src/cli/args.js +24 -1
  59. package/src/cli/ci.js +229 -0
  60. package/src/cli/help.js +23 -1
  61. package/src/cli/init.js +36 -1
  62. package/src/cli.js +6 -2
  63. package/src/comment-drawing.js +161 -0
  64. package/src/comment-fields.js +51 -0
  65. package/src/comment-media.js +528 -0
  66. package/src/config.js +7 -0
  67. package/src/doctor.js +23 -0
  68. package/src/export.js +8 -0
  69. package/src/feedback.js +89 -12
  70. package/src/github.js +3 -3
  71. package/src/hosted/admin.js +36 -4
  72. package/src/hosted/app.js +5 -0
  73. package/src/hosted/board-routes.js +63 -7
  74. package/src/hosted/clean-worker.js +22 -4
  75. package/src/hosted/cleaner.js +4 -1
  76. package/src/hosted/comment-files.js +200 -0
  77. package/src/hosted/comments.js +68 -15
  78. package/src/hosted/db.js +32 -0
  79. package/src/hosted/guest-view.js +9 -0
  80. package/src/hosted/import.js +213 -18
  81. package/src/hosted/operator.js +3 -1
  82. package/src/hosted/projects.js +3 -2
  83. package/src/hosted/push.js +6 -3
  84. package/src/hosted/retention.js +18 -0
  85. package/src/hosted/tenancy.js +1 -0
  86. package/src/hosted/token-api.js +110 -9
  87. package/src/image-sniff.js +11 -0
  88. package/src/mcp.js +48 -3
  89. package/src/remote/remote-mcp.js +2 -2
  90. package/src/remote/remote.js +6 -2
  91. package/src/remote/share.js +77 -7
  92. package/src/server/app.js +18 -1
  93. package/src/store.js +119 -33
  94. package/src/watch.js +4 -0
  95. package/dist/client/assets/BoardChat-C0a3DDjv.js +0 -28
  96. package/dist/client/assets/ChecksPanel-BHWgwJWO.js +0 -1
  97. package/dist/client/assets/History-C01qLFpP.js +0 -1
  98. package/dist/client/assets/QuickConnect-BzTM3UNO.js +0 -1
  99. package/dist/client/assets/index-C1IsGdGs.js +0 -29
  100. package/dist/client/assets/index-DnsWelgZ.css +0 -2
  101. package/dist/client/assets/ui-BAGBTB_P.js +0 -41
  102. package/dist/console/assets/LocalAccountsApp-DtfTDbQg.js +0 -1
  103. package/dist/console/assets/LocalConsole-BPpsxDq_.js +0 -1
  104. package/dist/console/assets/console-DeOji9Zv.css +0 -2
  105. package/dist/console/assets/console-MvFrlUCC.js +0 -51
@@ -1,7 +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: "agents" when the agent settings change (settings-events.js)
4
+ // GET /api/agent/events SSE, owner only: "agents" when the agent settings change (settings-events.js)
5
5
  // GET /api/agent/boards/:id/threads the viewer's chat history (?q= searches it)
6
6
  // GET /api/agent/boards/:id/threads/:thread one thread
7
7
  // POST /api/agent/boards/:id/threads/:thread/messages send a message (editOf: rerun from an edit; regenerate: a new answer); SSE back
@@ -35,10 +35,35 @@
35
35
  // boards, and a byte quota per board for stored threads and images.
36
36
 
37
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'
38
42
  import express from 'express'
39
- 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'
40
63
  import { createBackend } from './backends/index.js'
41
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'
42
67
  import { cleanChoices } from './backends/acp.js'
43
68
  import { createOptionsCache, withChoices } from './options-cache.js'
44
69
  import { authFiles } from '../config.js'
@@ -48,10 +73,10 @@ import { createAgentHealth } from './agent-health.js'
48
73
  import { beginAgentOperation } from './control.js'
49
74
  import { validateFiles, validateMentions } from './composer.js'
50
75
  import { profileForDefault, userAgentEntry, userSettingsFile } from './user-settings.js'
51
- import { finishFix, projectChanged, projectSnapshot } from './feedback-loop.js'
76
+ import { finishFix, projectChanged, projectSnapshot, stopReason } from './feedback-loop.js'
52
77
  import { ownerMcpServers } from './owner-mcp.js'
53
78
  import { RUN_MS_MAX, commentIdOf, runTurn } from './run.js'
54
- import { beginTurnChanges, finishTurnChanges, undoFiles, undoStarterBranch } from './turn-changes.js'
79
+ import { beginTurnChanges, finishTurnChanges, starterSummary, undoFiles, undoStarterBranch } from './turn-changes.js'
55
80
  import { latestChange, withUndoAvailability } from './undo-state.js'
56
81
  import { exportRatings, findAnswer, myRatings, raterOf, removeRating, reviewRatings, setRating } from './ratings.js'
57
82
  import { localRequest, sameOriginWrite, settingsRoutes, settingsTokenOk } from './settings-routes.js'
@@ -84,6 +109,8 @@ import {
84
109
  // body this big, and only once the message is allowed to run.
85
110
  export const BODY_LIMIT = '28mb'
86
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
87
114
 
88
115
  export const DEFAULT_LIMITS = {
89
116
  // Answers being written at once, across all boards.
@@ -296,6 +323,7 @@ export function agentRouter({
296
323
  // Server shutdown: stop agent processes (src/server/app.js stopChildren).
297
324
  router.close = () => {
298
325
  processPool.epoch += 1
326
+ batchQueue.close()
299
327
  settingsEvents.close()
300
328
  // An open events stream would hold the server open: end every one.
301
329
  eventsClosed = true
@@ -403,6 +431,8 @@ export function agentRouter({
403
431
  // meanwhile cannot tell its own edits apart, so it offers no Undo.
404
432
  let turnSerial = 0
405
433
  let undoing = false
434
+ // An agent branch being applied, discarded or opened (one at a time).
435
+ let applying = false
406
436
  // The stored summary says whether Undo was possible; this process knows
407
437
  // whether it still is, and only the thread's latest change offers it (undo-state.js).
408
438
  const withUndoState = (boardId, threadId, messages, all = messages) =>
@@ -414,6 +444,126 @@ export function agentRouter({
414
444
  : message.summary.undo.available === true,
415
445
  all,
416
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
+ }
417
567
  const buckets = new Map()
418
568
  const bucketFor = (boardId) => {
419
569
  if (!buckets.has(boardId)) buckets.set(boardId, tokenBucket(limit))
@@ -469,6 +619,7 @@ export function agentRouter({
469
619
  const admitMessage = async (req, res, next) => {
470
620
  if (connectionTesting()) return fail(res, 409, 'test_running', 'A connection test is running. Wait for it before sending.')
471
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.')
472
623
  const admission = new AbortController()
473
624
  let release
474
625
  res.once('close', () => {
@@ -506,7 +657,11 @@ export function agentRouter({
506
657
  )
507
658
  }
508
659
  const key = `${board.id}/${threadId}`
509
- 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.')
510
665
  if (runs.has(key) || selecting.has(key))
511
666
  return fail(res, 409, 'agent_running', 'An answer or profile change is still running in this thread.')
512
667
  if (runs.size >= limit.runsMax) {
@@ -521,7 +676,7 @@ export function agentRouter({
521
676
  // The slot is held from here until the response closes, whatever happens
522
677
  // (a body too large, a bad image, the board going away mid-answer).
523
678
  const controller = new AbortController()
524
- const run = { controller, current, backend, ownerWrites }
679
+ const run = { controller, current, backend, ownerWrites, branchOn }
525
680
  runs.set(key, run)
526
681
  const timer = setTimeout(() => controller.abort('timeout'), backend.runMsMax || RUN_MS_MAX)
527
682
  res.on('close', () => {
@@ -535,200 +690,505 @@ export function agentRouter({
535
690
  next()
536
691
  }
537
692
 
538
- router.post(
539
- '/boards/:id/threads/:thread/messages',
540
- findThread,
541
- admitMessage,
542
- express.json({ limit: BODY_LIMIT }),
543
- handle(async (req, res, board, threadId) => {
544
- const { controller, current, backend, ownerWrites } = res.locals.run
545
- res.locals.agentOperationRunning = true
546
- try {
547
- const fix = req.body?.fix === true
548
- // Regenerate: the same request again, the new answer in the old one's
549
- // place (threads.js replaceAnswer). Only the thread's latest answer.
550
- const regenerate = req.body?.regenerate === undefined ? null : String(req.body.regenerate)
551
- let previous = null
552
- if (regenerate !== null) {
553
- if (fix) return fail(res, 400, 'bad_regenerate', 'Regenerate and Fix with agent do not go together.')
554
- const stored = readThread(config, board.id, threadId).messages
555
- const last = stored.at(-1)
556
- if (!last || last.id !== regenerate || last.role !== 'assistant' || last.starter || stored.at(-2)?.role !== 'user')
557
- return fail(res, 409, 'cannot_regenerate', 'Only the latest answer to a message can be regenerated.')
558
- if (chatBytes(config, board.id) + Buffer.byteLength(JSON.stringify(last)) > limit.chatBytesMax)
559
- return fail(res, 413, 'chat_quota', 'Chat storage for this board is full. Clear a thread to free space.')
560
- previous = last
561
- }
562
- if (fix && runs.size > 1) return fail(res, 409, 'agent_running', 'Wait for other agent turns before starting a fix.')
563
- res.locals.run.fix = fix
564
- const commentId = commentIdOf(threadId)
565
- if (fix && (!ownerWrites || !allowCapture || !commentId || backend.id !== 'acp')) {
566
- return fail(res, 403, 'fix_needs_owner', 'Fix with agent needs an owner device, captures enabled, and a connected coding agent.')
567
- }
568
- // Images for a model that does not take them are stored, then dropped
569
- // before the request, with a note in the chat (run.js).
570
- // Composer extras: @ mentions (ids, checked against this board) and text files.
571
- const mentions = previous ? [] : validateMentions(config, board, req.body?.mentions)
572
- const files = previous ? [] : validateFiles(req.body?.files)
573
- const input = previous
574
- ? null
575
- : { ...validateUserMessage(board, req.body, { extras: mentions.length + files.length }), mentions, files }
576
- const editOf = req.body?.editOf ?? null
577
- if (editOf !== null && (typeof editOf !== 'string' || editOf.length > 64 || fix || previous))
578
- return fail(res, 400, 'bad_edit', 'editOf is the id of one of your messages in this thread.')
579
- // An edit or a new answer rewrites the thread (and moves an owner
580
- // turn's answer, with its Undo, into an earlier version): like
581
- // bringing back a version, an owner session's thread needs the owner.
582
- if ((editOf !== null || previous) && !controlsAllowed(req, board, threadId)) return controlDenied(res)
583
- // The comment's agent status before this run, to put back when the
584
- // agent never got to work (feedback-loop.js finishFix).
585
- let previousStatus = null
586
- if (fix) {
587
- const comment = getComment(config, board.id, commentId)
588
- previousStatus = comment.agentStatus ?? null
589
- if (comment.status !== 'open' || ['queued', 'working'].includes(comment.agentStatus))
590
- return fail(res, 409, 'comment_busy', 'This comment is resolved or already with an agent.')
591
- const claimed = await setAgentStatus(config, board.id, commentId, 'queued', {
592
- onlyFrom: [null, 'done', 'needs-human'],
593
- actor: 'shapewall',
594
- })
595
- if (!claimed.changed) return fail(res, 409, 'comment_busy', 'This comment is already with an agent.')
596
- }
597
- const snapshot = ownerWrites && commentId && allowCapture ? projectSnapshot(config) : null
598
- 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)
599
755
  try {
600
- userMessage = previous
601
- ? readThread(config, board.id, threadId).messages.at(-2)
602
- : await appendUserMessage(config, board.id, threadId, input, {
603
- quotaBytes: limit.chatBytesMax,
604
- viewer: res.locals.viewer,
605
- editOf,
606
- })
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
+ })
607
765
  } catch (error) {
608
- if (fix) await setAgentStatus(config, board.id, commentId, 'needs-human', { onlyFrom: ['queued'], actor: 'shapewall' })
609
- throw error
610
- }
611
- if (ownerWrites) ownerThreads.add(`${board.id}/${threadId}`)
612
- if (editOf !== null) resetSessions(`${board.id}/${threadId}`)
613
- if (controller.signal.aborted) {
614
- if (fix) await setAgentStatus(config, board.id, commentId, 'needs-human', { onlyFrom: ['queued'], actor: 'shapewall' })
615
- return
616
- }
617
- if (fix) {
618
- const working = await setAgentStatus(config, board.id, commentId, 'working', { onlyFrom: ['queued'], actor: 'shapewall' })
619
- if (!working.changed)
620
- return fail(res, 409, 'comment_changed', 'The comment changed before the agent started. Review it and try again.')
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.')
621
769
  }
622
- res.writeHead(200, {
623
- 'Content-Type': 'text/event-stream; charset=utf-8',
624
- 'Cache-Control': 'no-store',
625
- 'X-Accel-Buffering': 'no',
626
- 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',
627
775
  })
628
- res.flushHeaders?.()
629
- const emit = (event, data) => {
630
- 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
+ }
631
830
  }
632
- res.locals.run.keepalive = setInterval(() => {
633
- if (!res.writableEnded) res.write(': ping\n\n')
634
- }, limit.keepaliveMs)
635
- try {
636
- emit('user', { message: userMessage })
637
- let { messages } = readThread(config, board.id, threadId)
638
- if (previous) {
639
- messages = messages.filter((item) => item.id !== previous.id)
640
- // An ACP agent keeps its own session, which saw the old answer.
641
- if (backend.id === 'acp')
642
- messages[messages.length - 1] = {
643
- ...messages.at(-1),
644
- 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 || ''}`,
645
- }
646
- }
647
- const requestedPages = new Set()
648
- // An owner's turn: what it changes becomes the run summary, with Undo
649
- // for an ACP agent's file edits (turn-changes.js).
650
- const serial = ++turnSerial
651
- const overlappedAtStart = runs.size > 1
652
- const baseline = ownerWrites
653
- ? 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) => {
654
839
  log(`run summary: ${error.message}`)
655
840
  return null
656
- })
657
- : null
658
- const reply = await runTurn({
659
- 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,
660
875
  board,
661
876
  threadId,
662
- messages,
663
- backend,
664
- model: current.model,
665
- // The composer's model picker (an endpoint's own list), owner turns only.
666
- modelOverride: ownerWrites ? chosenModel(current, `${board.id}/${threadId}`) : null,
667
- // The owner's own feedback notes go to the owner's turns only (ratings-core.js downNotes).
668
- ownerTurn: res.locals.viewer === 'owner',
669
- // The composer's options for this chat (an ACP session's own), owner turns only.
670
- choices: ownerWrites && backend.id === 'acp' ? cleanChoices(req.body?.options) : null,
671
- emit,
672
- signal: controller.signal,
673
- ownerWrites,
674
- allowCapture,
675
- capture,
877
+ commentId,
878
+ reply,
879
+ baseline,
880
+ fix,
881
+ previousStatus,
676
882
  requestedPages,
883
+ checkoutBefore,
884
+ userText: userMessage?.text || '',
885
+ signal: controller.signal,
677
886
  })
678
- health.record(current, reply)
679
- // What the profile offers, as this turn's session saw it; a turn that
680
- // could not sign in drops what was known (the next look starts fresh).
681
- if (reply.error?.code === 'agent_auth') optionsCache.drop(current)
682
- else if (backend.id === 'acp') rememberSession(current, backend, `${board.id}/${threadId}`)
683
- if (baseline) {
684
- try {
685
- const { summary, undo } = await finishTurnChanges(config, baseline, {
686
- comments: listComments(config, board.id),
687
- board,
688
- reply,
689
- pageIds: [...requestedPages],
690
- commentId,
691
- overlapped: overlappedAtStart || serial !== turnSerial || runs.size > 1,
692
- })
693
- if (summary) reply.summary = summary
694
- if (undo) rememberUndo(`${board.id}/${threadId}/${reply.id}`, undo)
695
- } catch (error) {
696
- log(`run summary: ${error.message}`)
697
- }
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}`)
698
901
  }
699
- if (snapshot) {
700
- const changed = projectChanged(snapshot, projectSnapshot(config))
701
- if (fix || changed) {
702
- emit('status', { text: changed ? 'Recapturing the changed page' : 'Checking the fix' })
703
- await finishFix({
704
- config,
705
- board,
706
- id: commentId,
707
- reply,
708
- changed,
709
- pageIds: [...requestedPages],
710
- capture,
711
- signal: controller.signal,
712
- previousStatus,
713
- log,
714
- })
715
- }
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
+ })
716
919
  }
717
- if (previous) await replaceAnswer(config, board.id, threadId, previous.id, reply)
718
- else await appendAssistantMessage(config, board.id, threadId, reply)
719
- emit('done', { message: reply })
720
- } finally {
721
- clearInterval(res.locals.run.keepalive)
722
- if (!res.writableEnded) res.end()
723
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 })
724
924
  } finally {
725
- try {
726
- await res.locals.releaseAgentOperation()
727
- } finally {
728
- res.locals.agentOperationRunning = false
729
- 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
+ }
730
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()
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 }
731
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 })
732
1192
  }),
733
1193
  )
734
1194
 
@@ -738,8 +1198,19 @@ export function agentRouter({
738
1198
 
739
1199
  // The agent settings changed (here, in another tab, or in the file): a
740
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).
741
1206
  router.get('/events', (req, res) => {
742
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
+ }
743
1214
  res.writeHead(200, {
744
1215
  'Content-Type': 'text/event-stream; charset=utf-8',
745
1216
  'Cache-Control': 'no-store',
@@ -751,6 +1222,12 @@ export function agentRouter({
751
1222
  const stop = settingsEvents.subscribe((event) => {
752
1223
  if (!res.writableEnded) res.write(`event: ${AGENTS_EVENT}\ndata: ${JSON.stringify({ at: event.at })}\n\n`)
753
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)
754
1231
  const keepalive = setInterval(() => {
755
1232
  if (!res.writableEnded) res.write(': ping\n\n')
756
1233
  }, limit.keepaliveMs)
@@ -759,6 +1236,7 @@ export function agentRouter({
759
1236
  const done = () => {
760
1237
  clearInterval(keepalive)
761
1238
  stop()
1239
+ batchListeners.delete(batchListener)
762
1240
  eventStreams.delete(res)
763
1241
  }
764
1242
  req.on('close', done)
@@ -1072,6 +1550,7 @@ export function agentRouter({
1072
1550
  undoing = true
1073
1551
  try {
1074
1552
  let undone
1553
+ let branchState = null
1075
1554
  if (summary.undo.kind === 'files') {
1076
1555
  const record = undoRecords.get(key)
1077
1556
  if (!record) {
@@ -1095,6 +1574,22 @@ export function agentRouter({
1095
1574
  commit: summary.undo.commit,
1096
1575
  })
1097
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.')
1098
1593
  } else {
1099
1594
  return fail(
1100
1595
  res,
@@ -1107,7 +1602,10 @@ export function agentRouter({
1107
1602
  const commentId = commentIdOf(threadId)
1108
1603
  if (commentId) {
1109
1604
  try {
1110
- 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
+ })
1111
1609
  if (changed.changed) {
1112
1610
  await addReply(config, board.id, commentId, {
1113
1611
  author: 'shapewall',
@@ -1121,7 +1619,11 @@ export function agentRouter({
1121
1619
  const at = new Date().toISOString()
1122
1620
  let stored = null
1123
1621
  await changeMessage(config, board.id, threadId, messageId, (item) => {
1124
- 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
+ }
1125
1627
  stored = item
1126
1628
  })
1127
1629
  res.json({ ok: true, message: withUndoState(board.id, threadId, [stored], readThread(config, board.id, threadId).messages)[0] })
@@ -1225,6 +1727,169 @@ export function agentRouter({
1225
1727
  }),
1226
1728
  )
1227
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
+
1228
1893
  router.use((req, res) => fail(res, 404, 'not_found', 'No such API route.'))
1229
1894
  return router
1230
1895
  }