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
package/src/agent/run.js CHANGED
@@ -11,7 +11,7 @@ import { AgentError } from './errors.js'
11
11
  import { cleanCardDiff } from './text-diff.js'
12
12
  import { attachmentData, newId } from './threads.js'
13
13
  import { recentDownNotes } from './ratings.js'
14
- import { commentForModel, runTool, toolSpecs } from './tools.js'
14
+ import { commentForModel, commentMediaParts, runTool, toolSpecs } from './tools.js'
15
15
 
16
16
  export const HISTORY_MAX = 40
17
17
  // Images are sent again only for the last few person messages; older ones
@@ -64,6 +64,10 @@ export function modelMessages({ config, board, threadId, messages, comments = fa
64
64
  }
65
65
  content.push(...composerParts(config, board, message, { full: withImages.has(index) }))
66
66
  if (message.text) content.push({ type: 'text', text: message.text })
67
+ // A comment's thread (Fix with agent too): the reviewer's drawing on its
68
+ // screenshot and their images go with the newest message, as images,
69
+ // each after a quoted note saying what it is.
70
+ if (comment && index === recent.length - 1) content.push(...commentMediaParts(config, board, comment))
67
71
  for (const attachment of message.attachments || []) {
68
72
  const data = withImages.has(index) ? attachmentData(config, board.id, attachment) : null
69
73
  if (data) content.push({ type: 'image', mediaType: attachment.mediaType, data })
@@ -111,6 +115,8 @@ export async function runTurn({
111
115
  // The composer's options for this chat ({ configId: value }), owner turns
112
116
  // of an agent with its own session options (an ACP agent).
113
117
  choices = null,
118
+ // Agent branch mode: the folder this turn's agent works in (acp.js stream).
119
+ workdir = null,
114
120
  }) {
115
121
  const thread = `${board.id}/${threadId}`
116
122
  const runMs = backend.runMsMax || RUN_MS_MAX
@@ -186,6 +192,7 @@ export async function runTurn({
186
192
  tools: round < TOOL_ROUNDS_MAX ? tools : [],
187
193
  ...(modelOverride ? { model: modelOverride } : {}),
188
194
  ...(choices ? { choices } : {}),
195
+ ...(workdir ? { workdir } : {}),
189
196
  signal,
190
197
  thread,
191
198
  })) {
@@ -47,6 +47,8 @@ import {
47
47
  publicAgentInfo,
48
48
  } from './config.js'
49
49
  import { legacyProfile, profileForDefault, projectKey, saveUserAgentEntry, userAgentEntry, validateUserEntry } from './user-settings.js'
50
+ import { DEFAULT_BRANCH_PREFIX, branchModeSettings, cleanBranchPrefix, repoFilters } from './branch-mode.js'
51
+ import { batchFixSettings, cleanBatchFixes } from './batch-fixes.js'
50
52
  import { tokenEquals } from '../tokens.js'
51
53
 
52
54
  const TEST_TIMEOUT_MS = 15000
@@ -569,7 +571,15 @@ export function settingsRoutes({
569
571
  return fail(res, 400, 'invalid_settings', 'The settings must be an object.')
570
572
  // Old clients save only the default connection. Keep their saved profiles.
571
573
  const preserved = input && !Object.hasOwn(input, 'profiles') ? userAgentEntry(config.dir, env).entry?.profiles : undefined
572
- const { entry, problems } = validateUserEntry({ ...input, ...(preserved ? { profiles: preserved } : {}) })
574
+ // Branch mode and batch fixes have their own routes (/branch-mode,
575
+ // /batch-fixes): a save of the connection keeps them.
576
+ const savedEntry = userAgentEntry(config.dir, env).entry || {}
577
+ const branch = Object.fromEntries(
578
+ ['branchMode', 'branchPrefix', 'batchFixes']
579
+ .filter((key) => !Object.hasOwn(input, key) && savedEntry[key] !== undefined)
580
+ .map((key) => [key, savedEntry[key]]),
581
+ )
582
+ const { entry, problems } = validateUserEntry({ ...input, ...(preserved ? { profiles: preserved } : {}), ...branch })
573
583
  if (!entry) return fail(res, 400, 'invalid_settings', problems.join('; '))
574
584
  // A Connect is testing what it saved and may put the old settings back:
575
585
  // a save now would be lost.
@@ -702,6 +712,64 @@ export function settingsRoutes({
702
712
  if (saveEntry(req, res, entry)) res.json(view())
703
713
  })
704
714
 
715
+ // Agent branch mode (branch-mode.js): GET what is in effect, POST
716
+ // { enabled, prefix? } to save it for this project. No confirmation: it only
717
+ // narrows where the agent writes. The environment still wins.
718
+ const branchView = async () => {
719
+ const current = branchModeSettings(config, env)
720
+ // acp: whether it applies now (only a coding agent over ACP runs in a worktree).
721
+ // filters: the Git filters the repository's attributes name (['lfs']):
722
+ // the agent's checkout holds those files in their stored form.
723
+ return {
724
+ enabled: current.enabled,
725
+ prefix: current.prefix,
726
+ sources: current.sources,
727
+ defaultPrefix: DEFAULT_BRANCH_PREFIX,
728
+ acp: settings().backend === 'acp',
729
+ filters: await repoFilters(config),
730
+ }
731
+ }
732
+ router.get('/branch-mode', guard, async (req, res) => res.json(await branchView()))
733
+ router.post('/branch-mode', guard, async (req, res) => {
734
+ const { enabled, prefix } = req.body || {}
735
+ if (typeof enabled !== 'boolean') return fail(res, 400, 'invalid_settings', 'enabled: true or false.')
736
+ if (prefix !== undefined && prefix !== null && prefix !== '' && !cleanBranchPrefix(prefix))
737
+ return fail(res, 400, 'invalid_settings', 'prefix: letters, digits, "-", "_" and "/", at most 40 characters, such as "shapewall/".')
738
+ if (busy()) return fail(res, 409, 'agent_running', 'An answer is still being written. Stop it, then change this.')
739
+ const saved = userAgentEntry(config.dir, env)
740
+ if (saved.problem) return fail(res, 409, 'settings_file_invalid', saved.problem)
741
+ const entry = { ...(saved.entry || {}), branchMode: enabled }
742
+ delete entry.updatedAt
743
+ if (prefix === null || prefix === '') delete entry.branchPrefix
744
+ else if (prefix !== undefined) entry.branchPrefix = cleanBranchPrefix(prefix)
745
+ try {
746
+ saveUserAgentEntry(config.dir, entry, env)
747
+ } catch (error) {
748
+ return fail(res, 500, 'settings_not_saved', `The setting was not saved: ${error.message}`)
749
+ }
750
+ res.json(await branchView())
751
+ })
752
+
753
+ // Batch fixes (batch-fixes.js): GET what is in effect, POST { enabled, max? }
754
+ // to save it for this project. Off by default; max at most 20.
755
+ const batchView = () => ({ ...batchFixSettings(config, env), acp: settings().backend === 'acp' })
756
+ router.get('/batch-fixes', guard, (req, res) => res.json(batchView()))
757
+ router.post('/batch-fixes', guard, (req, res) => {
758
+ const { enabled, max } = req.body || {}
759
+ const { value, problems } = cleanBatchFixes({ enabled, ...(max === undefined ? {} : { max }) })
760
+ if (!value || typeof enabled !== 'boolean') return fail(res, 400, 'invalid_settings', problems.join('; ') || 'enabled: true or false.')
761
+ const saved = userAgentEntry(config.dir, env)
762
+ if (saved.problem) return fail(res, 409, 'settings_file_invalid', saved.problem)
763
+ const entry = { ...(saved.entry || {}), batchFixes: { ...(saved.entry?.batchFixes || {}), ...value } }
764
+ delete entry.updatedAt
765
+ try {
766
+ saveUserAgentEntry(config.dir, entry, env)
767
+ } catch (error) {
768
+ return fail(res, 500, 'settings_not_saved', `The setting was not saved: ${error.message}`)
769
+ }
770
+ res.json(batchView())
771
+ })
772
+
705
773
  router.post('/test', guard, async (req, res) => {
706
774
  if (testing) return fail(res, 409, 'test_running', 'A connection test is already running.')
707
775
  if (busy()) return fail(res, 409, 'agent_running', 'Stop the answer or wait for the profile change before testing.')
@@ -7,6 +7,9 @@
7
7
  // there can point git anywhere. Git on the host never runs with that folder
8
8
  // as its repository or (except to list untracked files) as its work tree:
9
9
  //
10
+ // - the folder is written by Shapewall from the blobs git lists (ls-tree,
11
+ // cat-file --batch): no checkout, so no smudge filter runs. Files a filter
12
+ // handles (Git LFS) come out in their stored form (a pointer file);
10
13
  // - every git call names the repository explicitly (--git-dir, the real
11
14
  // common directory found at the start of the run), uses a temporary index
12
15
  // kept outside the mount, ignores the system config (GIT_CONFIG_NOSYSTEM),
@@ -15,6 +18,8 @@
15
18
  // by path or as a parent folder, and hard-linked files are refused) and
16
19
  // writing blobs with `hash-object --no-filters --stdin`, so no clean or
17
20
  // smudge filter, no attribute and no config from the agent's files runs;
21
+ // - untracked files the BASE commit's ignore rules ignore are never
22
+ // committed, whatever the agent wrote to .gitignore;
18
23
  // - .gitattributes and .gitmodules keep the base's version whatever the
19
24
  // agent did to them, and submodules (gitlinks) are left as they were;
20
25
  // - the branch is created with update-ref, only if it does not exist.
@@ -26,7 +31,7 @@
26
31
  import fs from 'node:fs'
27
32
  import os from 'node:os'
28
33
  import path from 'node:path'
29
- import { execFile } from 'node:child_process'
34
+ import { execFile, spawn } from 'node:child_process'
30
35
  import { createHash, randomBytes } from 'node:crypto'
31
36
  import { absoluteProgram } from '../../which.js'
32
37
 
@@ -51,7 +56,7 @@ const SKIP_PARTS = new Set(['.git', 'node_modules'])
51
56
  export const FILE_MAX = 100 * 1024 * 1024
52
57
  // The default bound on the bytes of changed files one commit reads in.
53
58
  export const COMMIT_MAX = 2 * 1024 ** 3
54
- const RUN_DIR_RE = /^\d{14}-[a-f0-9]{6}$/
59
+ export const RUN_DIR_RE = /^\d{14}-[a-f0-9]{6}$/
55
60
 
56
61
  // The environment for git: the person's, without anything that would point
57
62
  // git somewhere else, and without the system config.
@@ -64,7 +69,9 @@ function gitEnv(env, extra = {}) {
64
69
  return { ...out, GIT_TERMINAL_PROMPT: '0', GIT_CONFIG_NOSYSTEM: '1', ...extra }
65
70
  }
66
71
 
67
- export function git(args, { cwd, env = process.env, extraEnv = {}, input = null, timeoutMs = 120000 } = {}) {
72
+ // `status`: resolve { code, stdout } when git exits with an error code,
73
+ // instead of rejecting (a timeout or a spawn failure still rejects).
74
+ export function git(args, { cwd, env = process.env, extraEnv = {}, input = null, timeoutMs = 120000, status = false } = {}) {
68
75
  // An absolute path from absolute PATH entries only: a relative entry would
69
76
  // let a `git` in the folder (the agent's checkout) decide what runs.
70
77
  const program = absoluteProgram('git', env)
@@ -75,14 +82,16 @@ export function git(args, { cwd, env = process.env, extraEnv = {}, input = null,
75
82
  [...SAFE_GIT_FLAGS, ...args],
76
83
  { cwd, env: gitEnv(env, extraEnv), timeout: timeoutMs, maxBuffer: 256 * 1024 * 1024, encoding: 'buffer' },
77
84
  (error, stdout, stderr) => {
78
- if (error) {
85
+ if (error && status && typeof error.code === 'number' && !error.killed) {
86
+ resolve({ code: error.code, stdout: stdout.toString('utf8'), stderr: String(stderr || '') })
87
+ } else if (error) {
79
88
  const message = String(stderr || error.message)
80
89
  .trim()
81
90
  .split('\n')
82
91
  .slice(-3)
83
92
  .join(' ')
84
93
  reject(new Error(`git ${args.find((arg) => !arg.startsWith('-')) || args[0]}: ${message.slice(0, 400)}`))
85
- } else resolve(stdout.toString('utf8'))
94
+ } else resolve(status ? { code: 0, stdout: stdout.toString('utf8'), stderr: String(stderr || '') } : stdout.toString('utf8'))
86
95
  },
87
96
  )
88
97
  if (input !== null) {
@@ -93,6 +102,190 @@ export function git(args, { cwd, env = process.env, extraEnv = {}, input = null,
93
102
  })
94
103
  }
95
104
 
105
+ // The repository's configured filter, merge and diff drivers, each overridden
106
+ // with an empty command (-c filter.<name>.process= and so on), so a git call
107
+ // that would check files out, merge or compare them runs none of the
108
+ // repository's or the person's programs. The `.gitattributes` files and
109
+ // $GIT_DIR/info/attributes only name drivers: a name with no command
110
+ // configured runs nothing, so overriding every configured command is enough.
111
+ // core.attributesFile is /dev/null in SAFE_GIT_FLAGS already.
112
+ const DRIVER_RE = '^(filter|merge|diff)\\..*\\.(smudge|clean|process|required|driver|textconv|command)$'
113
+ export async function inertDriverFlags({ gitDir, cwd }) {
114
+ const listed = await git(['--git-dir', gitDir, 'config', '-z', '--get-regexp', DRIVER_RE], { cwd, status: true, timeoutMs: 20000 })
115
+ if (listed.code !== 0 && listed.code !== 1) throw new Error(`git config: ${listed.stderr.trim().slice(0, 200)}`)
116
+ const flags = new Set(['diff.external='])
117
+ for (const item of listed.stdout.split('\0')) {
118
+ if (!item) continue
119
+ const key = item.split('\n')[0]
120
+ const match = /^(filter|merge|diff)\.(.+)\.([a-z]+)$/s.exec(key)
121
+ if (!match) continue
122
+ const [, section, name] = match
123
+ // `-c` splits at the first "=": such a name cannot be overridden safely.
124
+ if (/[=\n\0]/.test(name)) throw new Error(`refusing the git ${section} driver "${name.slice(0, 40)}": its name has "=" or a line break`)
125
+ if (section === 'filter') for (const key of ['smudge', 'clean', 'process']) flags.add(`filter.${name}.${key}=`)
126
+ if (section === 'filter') flags.add(`filter.${name}.required=false`)
127
+ if (section === 'merge') flags.add(`merge.${name}.driver=`)
128
+ if (section === 'diff') for (const key of ['textconv', 'command']) flags.add(`diff.${name}.${key}=`)
129
+ }
130
+ return [...flags].flatMap((flag) => ['-c', flag])
131
+ }
132
+
133
+ // The filter names the attributes give to files in `commit` (its
134
+ // .gitattributes files and $GIT_DIR/info/attributes): ['lfs'] for a
135
+ // repository that uses Git LFS. Reads attribute files as text only.
136
+ export async function filterNames({ gitDir, cwd, commit }) {
137
+ const names = new Set()
138
+ const scan = (text) => {
139
+ for (const match of String(text).matchAll(/(?:^|\s)filter=([^\s]+)/g)) names.add(match[1].slice(0, 60))
140
+ }
141
+ const listed = await git(['--git-dir', gitDir, 'ls-tree', '-r', '-z', '--full-tree', commit], { cwd })
142
+ for (const line of listed.split('\0')) {
143
+ const tab = line.indexOf('\t')
144
+ if (tab < 0 || path.posix.basename(line.slice(tab + 1)) !== '.gitattributes') continue
145
+ const [, type, oid] = line.slice(0, tab).split(' ')
146
+ if (type === 'blob') scan(await git(['--git-dir', gitDir, 'cat-file', 'blob', oid], { cwd }))
147
+ }
148
+ try {
149
+ const info = (await git(['--git-dir', gitDir, 'rev-parse', '--git-path', 'info/attributes'], { cwd })).trim()
150
+ scan(fs.readFileSync(path.resolve(cwd, info), 'utf8'))
151
+ } catch {
152
+ /* none */
153
+ }
154
+ return [...names].sort()
155
+ }
156
+
157
+ // The entries of a tree, recursively: [{ mode, type, oid, path }].
158
+ export async function treeEntries({ gitDir, cwd, commit, extraEnv = {} }) {
159
+ const out = []
160
+ for (const line of (await git(['--git-dir', gitDir, 'ls-tree', '-r', '-z', '--full-tree', commit], { cwd, extraEnv })).split('\0')) {
161
+ if (!line) continue
162
+ const tab = line.indexOf('\t')
163
+ const [mode, type, oid] = line.slice(0, tab).split(' ')
164
+ out.push({ mode, type, oid, path: line.slice(tab + 1) })
165
+ }
166
+ return out
167
+ }
168
+
169
+ // Reads blobs with one `git cat-file --batch`, calling `onBlob(index, data)`
170
+ // in the order of `oids`.
171
+ function readBlobs({ gitDir, cwd, oids, onBlob }) {
172
+ if (!oids.length) return Promise.resolve()
173
+ const program = absoluteProgram('git', process.env)
174
+ if (!program) return Promise.reject(new Error('git: not found on PATH'))
175
+ return new Promise((resolve, reject) => {
176
+ const child = spawn(program, [...SAFE_GIT_FLAGS, '--git-dir', gitDir, 'cat-file', '--batch'], {
177
+ cwd,
178
+ env: gitEnv(process.env),
179
+ stdio: ['pipe', 'pipe', 'pipe'],
180
+ })
181
+ let buffer = Buffer.alloc(0)
182
+ let index = 0
183
+ let need = null
184
+ let failed = null
185
+ let stderr = ''
186
+ const fail = (error) => {
187
+ if (failed) return
188
+ failed = error
189
+ child.kill()
190
+ }
191
+ child.stdout.on('data', (chunk) => {
192
+ buffer = buffer.length ? Buffer.concat([buffer, chunk]) : chunk
193
+ try {
194
+ for (;;) {
195
+ if (need === null) {
196
+ const end = buffer.indexOf(10)
197
+ if (end < 0) break
198
+ const header = buffer.subarray(0, end).toString('utf8').split(' ')
199
+ buffer = buffer.subarray(end + 1)
200
+ if (header[1] !== 'blob' || header[0] !== oids[index])
201
+ throw new Error(`git cat-file: unexpected "${header.join(' ').slice(0, 120)}"`)
202
+ need = Number(header[2])
203
+ }
204
+ if (buffer.length < need + 1) break
205
+ onBlob(index, buffer.subarray(0, need))
206
+ buffer = buffer.subarray(need + 1)
207
+ need = null
208
+ index += 1
209
+ }
210
+ } catch (error) {
211
+ fail(error)
212
+ }
213
+ })
214
+ child.stderr.on('data', (chunk) => {
215
+ stderr = `${stderr}${chunk}`.slice(-2000)
216
+ })
217
+ child.on('error', fail)
218
+ child.on('close', (code) => {
219
+ if (failed) reject(failed)
220
+ else if (code !== 0 || index !== oids.length) reject(new Error(`git cat-file: ${stderr.trim().slice(0, 300) || `exit ${code}`}`))
221
+ else resolve()
222
+ })
223
+ child.stdin.on('error', () => {})
224
+ child.stdin.end(`${oids.join('\n')}\n`)
225
+ })
226
+ }
227
+
228
+ const BAD_PART = (part) => !part || part === '.' || part === '..' || part.toLowerCase() === '.git' || part.includes('\0')
229
+
230
+ // Writes the files of `commit` into the empty folder `dir` ourselves, with
231
+ // no checkout: git only lists the tree and hands over blob contents, so no
232
+ // smudge filter, attribute or other configured program runs. Modes and
233
+ // symlinks as recorded; a submodule (gitlink) becomes an empty folder.
234
+ // Files tracked by Git LFS (or any filter) come out as their stored form
235
+ // (an LFS pointer file).
236
+ export async function materializeTree({ gitDir, cwd, commit, dir }) {
237
+ const entries = await treeEntries({ gitDir, cwd, commit })
238
+ const made = new Set([''])
239
+ const parentOf = (rel) => {
240
+ const parts = rel.split('/')
241
+ if (parts.some(BAD_PART)) throw new Error(`refusing the path "${rel.slice(0, 120)}" in the tree`)
242
+ let current = ''
243
+ for (const part of parts.slice(0, -1)) {
244
+ current = current ? `${current}/${part}` : part
245
+ if (made.has(current)) continue
246
+ const full = path.join(dir, current)
247
+ let stat = null
248
+ try {
249
+ stat = fs.lstatSync(full)
250
+ } catch {
251
+ fs.mkdirSync(full, { mode: 0o755 })
252
+ stat = fs.lstatSync(full)
253
+ }
254
+ if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error(`refusing to write under "${current.slice(0, 120)}": not a folder`)
255
+ made.add(current)
256
+ }
257
+ return path.join(dir, rel)
258
+ }
259
+ const blobs = []
260
+ for (const entry of entries) {
261
+ const full = parentOf(entry.path)
262
+ if (entry.mode === '160000') {
263
+ fs.mkdirSync(full, { mode: 0o755 })
264
+ made.add(entry.path)
265
+ } else if (entry.type === 'blob') blobs.push({ ...entry, full })
266
+ }
267
+ await readBlobs({
268
+ gitDir,
269
+ cwd,
270
+ oids: blobs.map((entry) => entry.oid),
271
+ onBlob: (index, data) => {
272
+ const entry = blobs[index]
273
+ if (entry.mode === '120000') {
274
+ fs.symlinkSync(data.toString('utf8'), entry.full)
275
+ return
276
+ }
277
+ const mode = entry.mode === '100755' ? 0o755 : 0o644
278
+ const fd = fs.openSync(entry.full, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_NOFOLLOW, mode)
279
+ try {
280
+ fs.writeSync(fd, data)
281
+ } finally {
282
+ fs.closeSync(fd)
283
+ }
284
+ },
285
+ })
286
+ return entries.length
287
+ }
288
+
96
289
  // Where run folders go: --work-dir, else the cache folder.
97
290
  export function workRoot(projectDir, { flag = null, env = process.env } = {}) {
98
291
  if (flag) return path.resolve(flag)
@@ -136,22 +329,18 @@ export async function createWorktree({ projectDir, root, runId, branch, base = '
136
329
  fs.mkdirSync(root, { recursive: true, mode: 0o700 })
137
330
  fs.writeFileSync(ownerFile(root, runId), `${process.pid}\n`, { mode: 0o600 })
138
331
  const dir = path.join(root, runId)
139
- const index = indexFile(root, runId, 'checkout')
140
- const extraEnv = { GIT_INDEX_FILE: index }
141
332
  let made = false
142
333
  try {
143
334
  fs.mkdirSync(dir, { mode: 0o700 })
144
335
  made = true
145
- await git(['--git-dir', gitDir, 'read-tree', commit], { cwd: root, extraEnv })
146
- // The base is the person's own commit: checking it out is what `git worktree add` would do.
147
- await git(['--git-dir', gitDir, '--work-tree', dir, 'checkout-index', '--all', '--force'], { cwd: dir, extraEnv })
336
+ // Written by Shapewall from the blobs, not checked out by git: no smudge
337
+ // filter or other program from the repository's config runs.
338
+ await materializeTree({ gitDir, cwd: root, commit, dir })
148
339
  } catch (error) {
149
340
  // A half-made checkout is not left for the sweep: remove it and the owner file now.
150
- if (made) fs.rmSync(dir, { recursive: true, force: true })
341
+ if (made) removeTree(dir)
151
342
  fs.rmSync(ownerFile(root, runId), { force: true })
152
343
  throw error
153
- } finally {
154
- fs.rmSync(index, { force: true })
155
344
  }
156
345
  const real = fs.realpathSync(dir)
157
346
  const sub = path.relative(top, fs.realpathSync(projectDir))
@@ -218,6 +407,38 @@ export function linkLeaves(rel, target) {
218
407
  return resolved === '..' || resolved.startsWith('../')
219
408
  }
220
409
 
410
+ // The untracked files in the run folder that the base commit's ignore rules
411
+ // (its .gitignore files, plus the repository's info/exclude and the person's
412
+ // excludes file) do not ignore. The rules are read from a folder holding only
413
+ // the base's .gitignore files, never from the agent's edited ones.
414
+ async function untrackedByBaseRules({ run, root, runId, dir, baseEntries }) {
415
+ const all = (await run(['--work-tree', dir, 'ls-files', '--others', '-z', '--exclude=node_modules', '--exclude=.git']))
416
+ .split('\0')
417
+ .filter((rel) => rel && !rel.endsWith('/'))
418
+ if (!all.length) return []
419
+ const shadow = path.join(root, `${runId}.ignore`)
420
+ removeTree(shadow)
421
+ fs.mkdirSync(shadow, { mode: 0o700 })
422
+ try {
423
+ for (const [rel, entry] of baseEntries) {
424
+ if (rel.split('/').pop() !== '.gitignore' || entry.type !== 'blob' || entry.mode === '120000') continue
425
+ if (rel.split('/').some(BAD_PART)) continue
426
+ fs.mkdirSync(path.join(shadow, path.dirname(rel)), { recursive: true, mode: 0o700 })
427
+ fs.writeFileSync(path.join(shadow, rel), await run(['cat-file', 'blob', entry.oid]), { mode: 0o600 })
428
+ }
429
+ const checked = await run(['--work-tree', shadow, 'check-ignore', '-z', '--stdin', '--no-index'], {
430
+ cwd: shadow,
431
+ input: `${all.join('\0')}\0`,
432
+ status: true,
433
+ })
434
+ if (checked.code > 1) throw new Error(`git check-ignore: ${checked.stderr.trim().slice(0, 200)}`)
435
+ const ignored = new Set(checked.stdout.split('\0').filter(Boolean))
436
+ return all.filter((rel) => !ignored.has(rel))
437
+ } finally {
438
+ removeTree(shadow)
439
+ }
440
+ }
441
+
221
442
  const skipped = (rel, exclude) => {
222
443
  const parts = rel.split('/')
223
444
  if (parts.some((part) => SKIP_PARTS.has(part))) return true
@@ -230,7 +451,8 @@ const skipped = (rel, exclude) => {
230
451
  // deleted, refused } (files is all of added, modified and deleted).
231
452
  // `maxBytes` bounds what is read in for changed files: `du` sees a sparse file
232
453
  // as small, and a folder of them could keep the host reading for hours.
233
- export async function commitWorktree({ tree, message, exclude = [], maxBytes = COMMIT_MAX }) {
454
+ // `author`: { name, email } for the commit (the starter agent's own by default).
455
+ export async function commitWorktree({ tree, message, exclude = [], maxBytes = COMMIT_MAX, author = AGENT_AUTHOR }) {
234
456
  const { gitDir, dir, base, branch, format, root, runId } = tree
235
457
  const index = indexFile(root, runId, 'commit')
236
458
  const extraEnv = { GIT_INDEX_FILE: index }
@@ -244,9 +466,11 @@ export async function commitWorktree({ tree, message, exclude = [], maxBytes = C
244
466
  const [mode, type, oid] = line.slice(0, tab).split(' ')
245
467
  baseEntries.set(line.slice(tab + 1), { mode, type, oid })
246
468
  }
247
- // Untracked files the repository's ignore rules let through (git lists
248
- // names here; it reads no file content and follows no symlink).
249
- const untracked = (await run(['--work-tree', dir, 'ls-files', '--others', '--exclude-standard', '-z'])).split('\0').filter(Boolean)
469
+ // Untracked files the BASE commit's ignore rules let through: the agent's
470
+ // own edits to .gitignore never let an ignored file (.env) into the
471
+ // commit. Git lists names here; it reads no file content and follows no
472
+ // symlink.
473
+ const untracked = await untrackedByBaseRules({ run, root, runId, dir, baseEntries })
250
474
  const zero = '0'.repeat(format === 'sha256' ? 64 : 40)
251
475
  const updates = []
252
476
  const files = []
@@ -285,15 +509,16 @@ export async function commitWorktree({ tree, message, exclude = [], maxBytes = C
285
509
  if (!before) added.push(rel)
286
510
  if (now.mode === '120000' && linkLeaves(rel, now.data.toString('utf8'))) links.push(rel)
287
511
  }
288
- if (!files.length) return { committed: false, commit: null, files: [], added: [], modified: [], deleted: [], links: [], refused }
512
+ if (!files.length)
513
+ return { committed: false, commit: null, files: [], added: [], modified: [], deleted: [], links: [], ignoreChanged: [], refused }
289
514
  await run(['update-index', '-z', '--index-info'], { input: updates.join('') })
290
515
  const treeId = (await run(['write-tree'])).trim()
291
516
  const env = {
292
517
  ...process.env,
293
- GIT_AUTHOR_NAME: AGENT_AUTHOR.name,
294
- GIT_AUTHOR_EMAIL: AGENT_AUTHOR.email,
295
- GIT_COMMITTER_NAME: AGENT_AUTHOR.name,
296
- GIT_COMMITTER_EMAIL: AGENT_AUTHOR.email,
518
+ GIT_AUTHOR_NAME: author.name,
519
+ GIT_AUTHOR_EMAIL: author.email,
520
+ GIT_COMMITTER_NAME: author.name,
521
+ GIT_COMMITTER_EMAIL: author.email,
297
522
  }
298
523
  const commit = (await run(['commit-tree', treeId, '-p', base, '-F', '-'], { env, input: message })).trim()
299
524
  // Only a new branch: the zero old value makes update-ref refuse an existing one.
@@ -307,6 +532,8 @@ export async function commitWorktree({ tree, message, exclude = [], maxBytes = C
307
532
  modified,
308
533
  deleted: deleted.sort(),
309
534
  links: links.sort(),
535
+ // .gitignore files the agent changed: what they ignore now did not decide the commit.
536
+ ignoreChanged: files.filter((rel) => rel.split('/').pop() === '.gitignore').sort(),
310
537
  refused,
311
538
  }
312
539
  } finally {
@@ -359,8 +586,10 @@ const alive = (pid) => {
359
586
  }
360
587
 
361
588
  // Removes run folders whose run is gone (its owner process ended, or no
362
- // owner file and older than a day). Returns the run ids removed.
363
- export function sweepWorkRoot(root, { now = Date.now(), isAlive = alive } = {}) {
589
+ // owner file and older than a day). Returns the run ids removed. `keep(id)`:
590
+ // true for a folder that must stay whatever its owner (agent branch mode keeps
591
+ // a worktree whose commit is not applied yet).
592
+ export function sweepWorkRoot(root, { now = Date.now(), isAlive = alive, keep = () => false } = {}) {
364
593
  let names
365
594
  try {
366
595
  names = fs.readdirSync(root)
@@ -369,7 +598,7 @@ export function sweepWorkRoot(root, { now = Date.now(), isAlive = alive } = {})
369
598
  }
370
599
  const removed = []
371
600
  for (const name of names) {
372
- if (!RUN_DIR_RE.test(name)) continue
601
+ if (!RUN_DIR_RE.test(name) || keep(name)) continue
373
602
  let pid = null
374
603
  try {
375
604
  pid = Number(fs.readFileSync(ownerFile(root, name), 'utf8').trim())
@@ -392,18 +621,46 @@ export function sweepWorkRoot(root, { now = Date.now(), isAlive = alive } = {})
392
621
  return removed
393
622
  }
394
623
 
624
+ // What a push may travel over: https, http, ssh and a local path. Everything
625
+ // else is off, so a remote URL (or a url.*.insteadOf rewrite) in the
626
+ // repository's config cannot pick a transport that runs a program
627
+ // ("ext::sh -c ...", or any other git-remote-<name> helper). This is the
628
+ // environment variable, not `-c protocol.allow=never`: a protocol.<name>.allow
629
+ // in the repository's config would win over that, and never over this.
630
+ const PUSH_PROTOCOLS = 'https:http:ssh:file'
631
+
395
632
  // Pushes the agent's branch, and only it, to the same name on the remote.
633
+ //
634
+ // The push uses the person's own credentials on purpose (their credential
635
+ // helper, their ssh setup): that is how it reaches their remote at all. What
636
+ // it does not take from the repository's config is anything that would run a
637
+ // different program or send something else: the transport is limited (above),
638
+ // the receiving program is git's own whatever remote.<name>.receivepack says,
639
+ // and the push is not signed, so no gpg program runs.
396
640
  export async function pushBranch({ tree, remote }) {
397
641
  const { branch } = tree
398
642
  const last = branch.split('/').pop()
399
643
  if (PROTECTED_BRANCHES.has(branch) || PROTECTED_BRANCHES.has(last)) throw new Error(`refusing to push "${branch}"`)
644
+ // A remote name starting with "-" would be read as an option.
645
+ if (typeof remote !== 'string' || !remote || remote.startsWith('-')) throw new Error('refusing to push: not a remote name')
400
646
  // No tags (push.followTags) and no submodule pushes (push.recurseSubmodules)
401
647
  // from the person's config: the branch is all that goes.
402
648
  const refspec = `refs/heads/${branch}:refs/heads/${branch}`
403
- await git(['--git-dir', tree.gitDir, 'push', '--no-verify', '--no-follow-tags', '--recurse-submodules=no', remote, refspec], {
404
- cwd: tree.repo,
405
- timeoutMs: 180000,
406
- })
649
+ await git(
650
+ [
651
+ '--git-dir',
652
+ tree.gitDir,
653
+ 'push',
654
+ '--no-verify',
655
+ '--no-follow-tags',
656
+ '--no-signed',
657
+ '--recurse-submodules=no',
658
+ '--receive-pack=git-receive-pack',
659
+ remote,
660
+ refspec,
661
+ ],
662
+ { cwd: tree.repo, timeoutMs: 180000, extraEnv: { GIT_ALLOW_PROTOCOL: PUSH_PROTOCOLS } },
663
+ )
407
664
  }
408
665
 
409
666
  export { alive as processAlive }
@@ -23,6 +23,7 @@ import { randomBytes } from 'node:crypto'
23
23
  import { withFileLock } from '../lock.js'
24
24
  import { boardDir, readJson, writeFileAtomic, writeJsonAtomic } from '../store.js'
25
25
  import { AgentError } from './errors.js'
26
+ import { sniffImage } from '../image-sniff.js'
26
27
  import {
27
28
  CHAT_RE,
28
29
  THREAD_RE,
@@ -121,13 +122,7 @@ export function listThreads(config, boardId, { viewer = null, query = '' } = {})
121
122
  }
122
123
 
123
124
  // The file's real type from its first bytes; the declared type must match.
124
- export function sniffImage(buffer) {
125
- if (buffer.length >= 8 && buffer.subarray(0, 8).equals(Buffer.from('89504e470d0a1a0a', 'hex'))) return 'image/png'
126
- if (buffer.length >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) return 'image/jpeg'
127
- if (buffer.length >= 12 && buffer.toString('latin1', 0, 4) === 'RIFF' && buffer.toString('latin1', 8, 12) === 'WEBP') return 'image/webp'
128
- if (buffer.length >= 6 && /^GIF8[79]a$/.test(buffer.toString('latin1', 0, 6))) return 'image/gif'
129
- return null
130
- }
125
+ export { sniffImage }
131
126
 
132
127
  const cleanName = (value, fallback) =>
133
128
  String(value || '')