shapewall 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/README.md +35 -1
- package/dist/agent/package-lock.json +2 -2
- package/dist/client/assets/BoardChat-CupOxioV.js +27 -0
- package/dist/client/assets/{BuzzDialog-CZdwIQLF.js → BuzzDialog-Ds7Thpfe.js} +1 -1
- package/dist/client/assets/ChecksPanel-BqrS02Ih.js +1 -0
- package/dist/client/assets/History-CQG3NQOC.js +1 -0
- package/dist/client/assets/{LocalSettings-CvlVtNY_.js → LocalSettings-CSNMhNyV.js} +1 -1
- package/dist/client/assets/{Management-DOJkFY_o.js → Management-CJVNAqAW.js} +1 -1
- package/dist/client/assets/QuickConnect-B-4pUgXH.js +1 -0
- package/dist/client/assets/{ShareDialog-CdVAreaw.js → ShareDialog-BouvNraR.js} +2 -2
- package/dist/client/assets/{app-update-dialog-Dox9-LWv.js → app-update-dialog-rhaw0MRk.js} +1 -1
- package/dist/client/assets/{circle-x-BmT6vHKl.js → circle-x-PRxj7kPi.js} +1 -1
- package/dist/client/assets/index-CXtuJi1K.css +2 -0
- package/dist/client/assets/index-DKOCIQiq.js +37 -0
- package/dist/client/assets/{refresh-cw-ugt3cWKH.js → refresh-cw-aeOBDuGQ.js} +1 -1
- package/dist/client/assets/{square-_KG6tM5A.js → shield-check-DuPBJ6H_.js} +1 -1
- package/dist/client/assets/ui-Cg4TTrec.js +41 -0
- package/dist/client/build-info.json +1 -1
- package/dist/client/index.html +3 -3
- package/dist/client/sw.js +2 -2
- package/dist/console/assets/LocalAccountsApp-DOKZI6Lt.js +1 -0
- package/dist/console/assets/LocalConsole-DXzldLW9.js +1 -0
- package/dist/console/assets/app-update-dialog-CrqIMEwN.js +1 -0
- package/dist/console/assets/console-76WovHlh.js +55 -0
- package/dist/console/assets/console-Fp4k9iyx.css +2 -0
- package/dist/console/build-info.json +1 -1
- package/dist/console/console.html +2 -2
- package/docs/agent-hardening.md +19 -0
- package/docs/agent-workflow.md +33 -0
- package/docs/agents.md +112 -0
- package/docs/config.md +41 -0
- package/docs/deployment.md +3 -0
- package/docs/design-system.md +13 -5
- package/docs/hosted.md +10 -0
- package/docs/journeys.md +13 -1
- package/docs/pull-requests.md +192 -0
- package/package.json +2 -1
- package/src/agent/backends/acp.js +35 -6
- package/src/agent/batch-fixes.js +45 -0
- package/src/agent/batch-queue.js +204 -0
- package/src/agent/branch-mode.js +753 -0
- package/src/agent/config.js +6 -0
- package/src/agent/fix-prompt.js +5 -0
- package/src/agent/manage.js +1 -1
- package/src/agent/routes.js +901 -186
- package/src/agent/run.js +8 -1
- package/src/agent/settings-events.js +103 -0
- package/src/agent/settings-routes.js +92 -2
- package/src/agent/starter/worktree.js +254 -25
- package/src/agent/threads.js +2 -7
- package/src/agent/tools.js +51 -0
- package/src/agent/user-settings.js +33 -2
- package/src/ci/capture.js +103 -0
- package/src/ci/compare.js +267 -0
- package/src/ci/github.js +307 -0
- package/src/ci/images.js +76 -0
- package/src/ci/settings.js +211 -0
- package/src/ci/start.js +148 -0
- package/src/ci/summary.js +262 -0
- package/src/ci/workflow.js +238 -0
- package/src/cli/args.js +24 -1
- package/src/cli/ci.js +229 -0
- package/src/cli/help.js +23 -1
- package/src/cli/init.js +33 -1
- package/src/cli.js +6 -2
- package/src/comment-drawing.js +161 -0
- package/src/comment-fields.js +51 -0
- package/src/comment-media.js +528 -0
- package/src/config.js +7 -0
- package/src/doctor.js +23 -0
- package/src/export.js +8 -0
- package/src/feedback.js +89 -12
- package/src/github.js +3 -3
- package/src/hosted/admin.js +36 -4
- package/src/hosted/app.js +5 -0
- package/src/hosted/board-routes.js +63 -7
- package/src/hosted/clean-worker.js +22 -4
- package/src/hosted/cleaner.js +4 -1
- package/src/hosted/comment-files.js +200 -0
- package/src/hosted/comments.js +68 -15
- package/src/hosted/db.js +32 -0
- package/src/hosted/guest-view.js +9 -0
- package/src/hosted/import.js +213 -18
- package/src/hosted/operator.js +3 -1
- package/src/hosted/projects.js +3 -2
- package/src/hosted/push.js +6 -3
- package/src/hosted/retention.js +18 -0
- package/src/hosted/tenancy.js +1 -0
- package/src/hosted/token-api.js +110 -9
- package/src/image-sniff.js +11 -0
- package/src/mcp.js +48 -3
- package/src/remote/remote-mcp.js +2 -2
- package/src/remote/remote.js +6 -2
- package/src/remote/share.js +77 -7
- package/src/server/app.js +18 -1
- package/src/store.js +119 -33
- package/src/watch.js +4 -0
- package/dist/client/assets/BoardChat-C88k4s-W.js +0 -28
- package/dist/client/assets/ChecksPanel-B54chcLA.js +0 -1
- package/dist/client/assets/History-CVVuuxUK.js +0 -1
- package/dist/client/assets/QuickConnect-D40mj4Ry.js +0 -1
- package/dist/client/assets/index-CwvFfwU0.js +0 -29
- package/dist/client/assets/index-UIYSPHoh.css +0 -2
- package/dist/client/assets/ui-BAGBTB_P.js +0 -41
- package/dist/console/assets/LocalAccountsApp-4MtW1QlZ.js +0 -1
- package/dist/console/assets/LocalConsole-CgKqYa8i.js +0 -1
- package/dist/console/assets/app-update-dialog-S2Yyc7Pw.js +0 -1
- package/dist/console/assets/console-BPmBRIoz.js +0 -51
- package/dist/console/assets/console-DgVrGRMD.css +0 -2
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
|
})) {
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// "Agents changed": one event for every open view when the agent settings
|
|
2
|
+
// change, whoever changed them. Two sources:
|
|
3
|
+
// - this server's own settings routes (connect, remove, default, save):
|
|
4
|
+
// changed('settings') after each one;
|
|
5
|
+
// - the saved agents file itself (src/agent/user-settings.js), watched with
|
|
6
|
+
// fs.watch on its folder, for edits made outside this process
|
|
7
|
+
// (`shapewall settings`, another `shapewall serve`, a person or an agent
|
|
8
|
+
// editing the file).
|
|
9
|
+
// Changes inside `debounceMs` of each other become one event, so a route's
|
|
10
|
+
// own write and the watcher noticing it say "changed" once.
|
|
11
|
+
//
|
|
12
|
+
// Browsers hear it on GET /api/agent/events (server-sent events, routes.js),
|
|
13
|
+
// which carries no settings, only that they changed: each view refetches
|
|
14
|
+
// what it shows through its usual, authorized routes.
|
|
15
|
+
|
|
16
|
+
import fs from 'node:fs'
|
|
17
|
+
import path from 'node:path'
|
|
18
|
+
|
|
19
|
+
export const AGENTS_EVENT = 'agents'
|
|
20
|
+
|
|
21
|
+
export function createSettingsEvents({ file = null, debounceMs = 150, watch = true, now = () => Date.now() } = {}) {
|
|
22
|
+
const listeners = new Set()
|
|
23
|
+
let timer = null
|
|
24
|
+
let reasons = new Set()
|
|
25
|
+
let watcher = null
|
|
26
|
+
let closed = false
|
|
27
|
+
|
|
28
|
+
const flush = () => {
|
|
29
|
+
timer = null
|
|
30
|
+
const event = { at: new Date(now()).toISOString(), reasons: [...reasons] }
|
|
31
|
+
reasons = new Set()
|
|
32
|
+
for (const listener of [...listeners]) {
|
|
33
|
+
try {
|
|
34
|
+
listener(event)
|
|
35
|
+
} catch {
|
|
36
|
+
// One listener failing never stops the others.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const changed = (reason = 'settings') => {
|
|
41
|
+
if (closed) return
|
|
42
|
+
reasons.add(reason)
|
|
43
|
+
if (!timer) {
|
|
44
|
+
timer = setTimeout(flush, debounceMs)
|
|
45
|
+
timer.unref?.()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The folder, not the file: saves replace the file (write, then rename),
|
|
50
|
+
// which a watch on the file itself would lose. A folder that does not
|
|
51
|
+
// exist yet (no settings saved) is watched once it appears, via its parent.
|
|
52
|
+
const start = () => {
|
|
53
|
+
if (!watch || !file || closed || watcher) return
|
|
54
|
+
const dir = path.dirname(file)
|
|
55
|
+
const name = path.basename(file)
|
|
56
|
+
try {
|
|
57
|
+
watcher = fs.watch(dir, (event, changedName) => {
|
|
58
|
+
if (!changedName || changedName === name || String(changedName).startsWith(`${name}.`)) changed('file')
|
|
59
|
+
})
|
|
60
|
+
watcher.on('error', () => {
|
|
61
|
+
watcher?.close()
|
|
62
|
+
watcher = null
|
|
63
|
+
})
|
|
64
|
+
watcher.unref?.()
|
|
65
|
+
} catch {
|
|
66
|
+
watcher = null
|
|
67
|
+
// The folder is not there yet: wait for it in the parent.
|
|
68
|
+
try {
|
|
69
|
+
const parent = fs.watch(path.dirname(dir), (event, changedName) => {
|
|
70
|
+
if (changedName !== path.basename(dir) || !fs.existsSync(dir)) return
|
|
71
|
+
parent.close()
|
|
72
|
+
watcher = null
|
|
73
|
+
start()
|
|
74
|
+
changed('file')
|
|
75
|
+
})
|
|
76
|
+
parent.on('error', () => parent.close())
|
|
77
|
+
parent.unref?.()
|
|
78
|
+
// Replaced by the real watcher once the folder appears.
|
|
79
|
+
watcher = { close: () => parent.close() }
|
|
80
|
+
} catch {
|
|
81
|
+
// No parent either: only this server's own changes are announced.
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
start()
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
changed,
|
|
89
|
+
subscribe(listener) {
|
|
90
|
+
listeners.add(listener)
|
|
91
|
+
return () => listeners.delete(listener)
|
|
92
|
+
},
|
|
93
|
+
listenerCount: () => listeners.size,
|
|
94
|
+
close() {
|
|
95
|
+
closed = true
|
|
96
|
+
if (timer) clearTimeout(timer)
|
|
97
|
+
timer = null
|
|
98
|
+
watcher?.close()
|
|
99
|
+
watcher = null
|
|
100
|
+
listeners.clear()
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -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
|
|
@@ -328,8 +330,30 @@ export async function testConnection(settings, { env = process.env, fetchImpl =
|
|
|
328
330
|
// `repo` is the config's `agent`. `busy()` is true while an answer is being
|
|
329
331
|
// written, `reload()` switches the chat over to new settings.
|
|
330
332
|
// `token` is the per-process settings token (null: the routes are off).
|
|
331
|
-
export function settingsRoutes({
|
|
333
|
+
export function settingsRoutes({
|
|
334
|
+
config,
|
|
335
|
+
env,
|
|
336
|
+
fetchImpl,
|
|
337
|
+
settings,
|
|
338
|
+
shared,
|
|
339
|
+
token,
|
|
340
|
+
busy,
|
|
341
|
+
reload,
|
|
342
|
+
processPool,
|
|
343
|
+
health = null,
|
|
344
|
+
events = null,
|
|
345
|
+
}) {
|
|
332
346
|
const router = express.Router()
|
|
347
|
+
// Every change that went through (and a test, which updates an agent's
|
|
348
|
+
// state) tells the open views (settings-events.js).
|
|
349
|
+
router.use((req, res, next) => {
|
|
350
|
+
if (events && !['GET', 'HEAD', 'OPTIONS'].includes(req.method)) {
|
|
351
|
+
res.on('finish', () => {
|
|
352
|
+
if (res.statusCode < 400) events.changed('settings')
|
|
353
|
+
})
|
|
354
|
+
}
|
|
355
|
+
next()
|
|
356
|
+
})
|
|
333
357
|
const repo = () => config.agent || {}
|
|
334
358
|
let testing = false
|
|
335
359
|
let closing = false
|
|
@@ -547,7 +571,15 @@ export function settingsRoutes({ config, env, fetchImpl, settings, shared, token
|
|
|
547
571
|
return fail(res, 400, 'invalid_settings', 'The settings must be an object.')
|
|
548
572
|
// Old clients save only the default connection. Keep their saved profiles.
|
|
549
573
|
const preserved = input && !Object.hasOwn(input, 'profiles') ? userAgentEntry(config.dir, env).entry?.profiles : undefined
|
|
550
|
-
|
|
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 })
|
|
551
583
|
if (!entry) return fail(res, 400, 'invalid_settings', problems.join('; '))
|
|
552
584
|
// A Connect is testing what it saved and may put the old settings back:
|
|
553
585
|
// a save now would be lost.
|
|
@@ -680,6 +712,64 @@ export function settingsRoutes({ config, env, fetchImpl, settings, shared, token
|
|
|
680
712
|
if (saveEntry(req, res, entry)) res.json(view())
|
|
681
713
|
})
|
|
682
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
|
+
|
|
683
773
|
router.post('/test', guard, async (req, res) => {
|
|
684
774
|
if (testing) return fail(res, 409, 'test_running', 'A connection test is already running.')
|
|
685
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
|
-
|
|
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
|
-
|
|
146
|
-
//
|
|
147
|
-
await
|
|
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)
|
|
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
|
-
|
|
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
|
|
248
|
-
//
|
|
249
|
-
|
|
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)
|
|
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:
|
|
294
|
-
GIT_AUTHOR_EMAIL:
|
|
295
|
-
GIT_COMMITTER_NAME:
|
|
296
|
-
GIT_COMMITTER_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
|
-
|
|
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())
|