@cat-factory/executor-harness 1.132.3 → 1.134.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/README.md +47 -0
- package/dist/agent-env.d.ts +17 -0
- package/dist/agent-env.js +47 -0
- package/dist/agent-runner.d.ts +11 -2
- package/dist/agent-runner.js +3 -48
- package/dist/agent.d.ts +0 -11
- package/dist/agent.js +7 -132
- package/dist/captured-command.d.ts +1 -1
- package/dist/captured-command.js +3 -2
- package/dist/coding-agent.d.ts +35 -0
- package/dist/coding-agent.js +213 -41
- package/dist/docker-status.d.ts +89 -0
- package/dist/docker-status.js +147 -0
- package/dist/frontend-infra.js +4 -3
- package/dist/git.d.ts +48 -5
- package/dist/git.js +93 -26
- package/dist/guard-driver.d.ts +71 -0
- package/dist/guard-driver.js +171 -0
- package/dist/harness-server.js +13 -0
- package/dist/infra-standup.d.ts +69 -0
- package/dist/infra-standup.js +182 -0
- package/dist/job.d.ts +10 -0
- package/dist/multi-repo-coding.d.ts +17 -0
- package/dist/multi-repo-coding.js +55 -8
- package/dist/pi-workspace.d.ts +11 -0
- package/dist/pi-workspace.js +47 -0
- package/dist/pi.d.ts +8 -0
- package/dist/pi.js +16 -9
- package/dist/progress-guard.d.ts +56 -10
- package/dist/progress-guard.js +84 -22
- package/dist/runner.d.ts +1 -1
- package/dist/salvage.d.ts +180 -0
- package/dist/salvage.js +289 -0
- package/dist/workspace-probe.d.ts +85 -0
- package/dist/workspace-probe.js +124 -0
- package/package.json +4 -4
- package/src/agent-env.ts +49 -0
- package/src/agent-runner.ts +14 -53
- package/src/agent.ts +7 -158
- package/src/captured-command.ts +3 -2
- package/src/coding-agent.ts +252 -44
- package/src/docker-status.ts +201 -0
- package/src/frontend-infra.ts +4 -3
- package/src/git.ts +104 -26
- package/src/guard-driver.ts +203 -0
- package/src/harness-server.ts +13 -0
- package/src/infra-standup.ts +218 -0
- package/src/job.ts +10 -0
- package/src/multi-repo-coding.ts +59 -8
- package/src/pi-workspace.ts +72 -0
- package/src/pi.ts +27 -12
- package/src/progress-guard.ts +110 -34
- package/src/runner.ts +1 -1
- package/src/salvage.ts +407 -0
- package/src/workspace-probe.ts +155 -0
package/src/salvage.ts
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { stat } from 'node:fs/promises'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { commitPaths, listUntrackedFiles } from './git.js'
|
|
4
|
+
import { HARNESS_SENTINEL_FILES } from './workspace-probe.js'
|
|
5
|
+
import type { Logger } from './logger.js'
|
|
6
|
+
|
|
7
|
+
// Recovering the work an aborted run left in the tree. `commitTrackedEdits` is a safety net for
|
|
8
|
+
// forgotten edits to files git ALREADY tracks, so a NEW file the agent created and never committed
|
|
9
|
+
// was found, warned about, and dropped. On a greenfield task every file is new, which made that
|
|
10
|
+
// warning the whole deliverable going in the bin: a run that built, tested and verified a service
|
|
11
|
+
// through `bash` heredocs was killed by the progress guard and lost all of it.
|
|
12
|
+
//
|
|
13
|
+
// Observable is not recovered. This makes the salvage real, under guardrails, and MARKED — a
|
|
14
|
+
// salvage commit is evidence from an interrupted run, never work anyone should read as reviewed.
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Directory and file names never salvaged. A greenfield checkout may not have a `.gitignore` yet
|
|
18
|
+
* (the agent had not written one when it was killed), and git only excludes what a `.gitignore`
|
|
19
|
+
* tells it to, so without this a blanket salvage would commit `node_modules` into the PR.
|
|
20
|
+
*
|
|
21
|
+
* Matched against every SEGMENT of a path, so `packages/api/node_modules/x` is caught as surely as
|
|
22
|
+
* a root-level one. Deliberately a short list of the unambiguous ones: a cleverer heuristic starts
|
|
23
|
+
* discarding the deliverable, and a `dist/` that genuinely belonged in a commit is a far cheaper
|
|
24
|
+
* miss than a `node_modules/` that did not.
|
|
25
|
+
*/
|
|
26
|
+
export const SALVAGE_DENIED_SEGMENTS: readonly string[] = [
|
|
27
|
+
'node_modules',
|
|
28
|
+
'dist',
|
|
29
|
+
'build',
|
|
30
|
+
'coverage',
|
|
31
|
+
'.venv',
|
|
32
|
+
'__pycache__',
|
|
33
|
+
'target',
|
|
34
|
+
'vendor',
|
|
35
|
+
'.git',
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
/** Suffixes never salvaged: run output, not source. */
|
|
39
|
+
export const SALVAGE_DENIED_SUFFIXES: readonly string[] = ['.log']
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Basenames and suffixes that carry CREDENTIALS, withheld from every salvage.
|
|
43
|
+
*
|
|
44
|
+
* The deny-list above trades a cheap miss (a `dist/` that belonged in a commit) against an
|
|
45
|
+
* expensive one (`node_modules/` in a PR). For a secret that trade INVERTS: a private key or a
|
|
46
|
+
* populated `.env` pushed to a branch is a disclosure that outlives the run, cannot be taken back
|
|
47
|
+
* by deleting the commit, and forces a rotation. Missing a file is recoverable; leaking one is not.
|
|
48
|
+
*
|
|
49
|
+
* This exists for the same reason the deny-list does: on the greenfield case the salvage was
|
|
50
|
+
* written for, the agent was killed before it wrote a `.gitignore`, so git excludes nothing and
|
|
51
|
+
* the harness is the only thing standing between an agent-authored key and the pull request.
|
|
52
|
+
*
|
|
53
|
+
* Unlike a junk path, a withheld secret is REPORTED (see {@link SalvageReport.withheld}): the file
|
|
54
|
+
* is real work that did not land, and whoever reads the run has to decide whether to re-create it
|
|
55
|
+
* or, if it holds a live credential, to rotate it.
|
|
56
|
+
*/
|
|
57
|
+
export const SALVAGE_SECRET_BASENAMES: readonly string[] = [
|
|
58
|
+
'.netrc',
|
|
59
|
+
'.npmrc',
|
|
60
|
+
'.pypirc',
|
|
61
|
+
'credentials',
|
|
62
|
+
'id_dsa',
|
|
63
|
+
'id_ecdsa',
|
|
64
|
+
'id_ed25519',
|
|
65
|
+
'id_rsa',
|
|
66
|
+
'secrets.json',
|
|
67
|
+
'secrets.yaml',
|
|
68
|
+
'secrets.yml',
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Suffixes that mark a key store or an environment file, withheld for the reason above.
|
|
73
|
+
*
|
|
74
|
+
* `.env` is here as well as in {@link isSecretBearingName}'s own `.env` / `.env.*` test, so that
|
|
75
|
+
* `prod.env` and `local.env` are caught alongside `.env` and `.env.production`. The sample
|
|
76
|
+
* allow-list is unaffected: `.env.example` ends in `.example`, not in `.env`.
|
|
77
|
+
*/
|
|
78
|
+
export const SALVAGE_SECRET_SUFFIXES: readonly string[] = [
|
|
79
|
+
'.env',
|
|
80
|
+
'.jks',
|
|
81
|
+
'.key',
|
|
82
|
+
'.keystore',
|
|
83
|
+
'.p12',
|
|
84
|
+
'.pem',
|
|
85
|
+
'.pfx',
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
/** Path segments that are credential or state stores rather than source. */
|
|
89
|
+
export const SALVAGE_SECRET_SEGMENTS: readonly string[] = ['.aws', '.gnupg', '.ssh', '.terraform']
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The `.env` files that carry no secret and ARE the deliverable: the checked-in sample every
|
|
93
|
+
* scaffold ships so a reader knows which variables the service wants.
|
|
94
|
+
*
|
|
95
|
+
* An allow-list rather than a cleverer rule, because the two are the same shape and only the
|
|
96
|
+
* convention tells them apart. `.env` and every other `.env.<something>` is withheld: a scaffolded
|
|
97
|
+
* `.env.local` or `.env.production` is exactly where a real key ends up.
|
|
98
|
+
*/
|
|
99
|
+
export const SALVAGE_ENV_SAMPLE_BASENAMES: readonly string[] = [
|
|
100
|
+
'.env.defaults',
|
|
101
|
+
'.env.dist',
|
|
102
|
+
'.env.example',
|
|
103
|
+
'.env.sample',
|
|
104
|
+
'.env.template',
|
|
105
|
+
]
|
|
106
|
+
|
|
107
|
+
/** Whether `basename` is a credential-bearing file the salvage must never commit. */
|
|
108
|
+
function isSecretBearingName(basename: string): boolean {
|
|
109
|
+
const lower = basename.toLowerCase()
|
|
110
|
+
if (SALVAGE_ENV_SAMPLE_BASENAMES.includes(lower)) return false
|
|
111
|
+
if (lower === '.env' || lower.startsWith('.env.')) return true
|
|
112
|
+
if (SALVAGE_SECRET_BASENAMES.includes(lower)) return true
|
|
113
|
+
return SALVAGE_SECRET_SUFFIXES.some((suffix) => lower.endsWith(suffix))
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** How much may be salvaged before the whole salvage is refused. */
|
|
117
|
+
export interface SalvageBounds {
|
|
118
|
+
maxFiles: number
|
|
119
|
+
maxBytes: number
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The default bounds. Generous enough for a scaffolded service (the run this was written for left
|
|
124
|
+
* about twenty source files) and far below anything that looks like a build output or a dependency
|
|
125
|
+
* tree that slipped past the deny-list.
|
|
126
|
+
*/
|
|
127
|
+
export const DEFAULT_SALVAGE_BOUNDS: SalvageBounds = { maxFiles: 200, maxBytes: 5_000_000 }
|
|
128
|
+
|
|
129
|
+
/** What the salvage did, carried onto the run outcome so a human is told rather than left to infer. */
|
|
130
|
+
export interface SalvageReport {
|
|
131
|
+
/**
|
|
132
|
+
* `none`: nothing was left uncommitted. `committed`: the files below are in `commitSha`.
|
|
133
|
+
* `refused`: there was work but it exceeded the bounds, so NOTHING was committed — a truncated
|
|
134
|
+
* salvage is worse than none, because a half-committed tree reads as a complete one.
|
|
135
|
+
* `failed`: the commit itself could not be made; the paths are named so the loss is on the record.
|
|
136
|
+
*/
|
|
137
|
+
status: 'none' | 'committed' | 'refused' | 'failed'
|
|
138
|
+
/** The salvaged (or would-be salvaged) paths, capped for the log/wire; `fileCount` is the truth. */
|
|
139
|
+
files: string[]
|
|
140
|
+
fileCount: number
|
|
141
|
+
totalBytes: number
|
|
142
|
+
commitSha?: string
|
|
143
|
+
/** Why a `refused`/`failed` salvage did not land. */
|
|
144
|
+
reason?: string
|
|
145
|
+
/**
|
|
146
|
+
* Secret-bearing paths the salvage refused to commit, whatever its `status` (a run with nothing
|
|
147
|
+
* else to salvage still reports them, as `none`). Named rather than counted: the point is that
|
|
148
|
+
* someone can look at the file and decide whether it held a live credential.
|
|
149
|
+
*/
|
|
150
|
+
withheld?: string[]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** How many paths a report quotes. The count carries the rest; a report is a summary, not a manifest. */
|
|
154
|
+
const REPORTED_PATHS = 20
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* What the salvage does with one path.
|
|
158
|
+
*
|
|
159
|
+
* Three outcomes, not two, because the reasons for withholding a file are not the same fact. A
|
|
160
|
+
* `skip` is expected and uninteresting: nobody wants `node_modules` in a PR, and saying so would
|
|
161
|
+
* be noise on every run. A `secret` is a decision someone has to know about — the file was real
|
|
162
|
+
* work, it did not land, and it may hold a live credential that now needs rotating.
|
|
163
|
+
*/
|
|
164
|
+
export type SalvageDisposition = 'salvage' | 'skip' | 'secret'
|
|
165
|
+
|
|
166
|
+
/** What the salvage would do with `path`: keep it, drop it quietly, or withhold it as a secret. */
|
|
167
|
+
export function classifySalvagePath(path: string): SalvageDisposition {
|
|
168
|
+
const segments = path.split('/')
|
|
169
|
+
const basename = segments[segments.length - 1] ?? ''
|
|
170
|
+
if (isSecretBearingName(basename)) return 'secret'
|
|
171
|
+
if (segments.some((segment) => SALVAGE_SECRET_SEGMENTS.includes(segment))) return 'secret'
|
|
172
|
+
if (HARNESS_SENTINEL_FILES.includes(basename)) return 'skip'
|
|
173
|
+
if (SALVAGE_DENIED_SUFFIXES.some((suffix) => basename.endsWith(suffix))) return 'skip'
|
|
174
|
+
if (segments.some((segment) => SALVAGE_DENIED_SEGMENTS.includes(segment))) return 'skip'
|
|
175
|
+
return 'salvage'
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Split the untracked paths into what the salvage commits and what it withholds as secret-bearing.
|
|
180
|
+
*
|
|
181
|
+
* The secret check runs BEFORE the junk one, so a key under a denied directory is still counted as
|
|
182
|
+
* withheld rather than swallowed as junk: the point of the count is telling someone a credential
|
|
183
|
+
* may have been created, and where it happened to sit does not change that.
|
|
184
|
+
*/
|
|
185
|
+
export function partitionSalvageCandidates(paths: readonly string[]): {
|
|
186
|
+
candidates: string[]
|
|
187
|
+
withheld: string[]
|
|
188
|
+
} {
|
|
189
|
+
const candidates: string[] = []
|
|
190
|
+
const withheld: string[] = []
|
|
191
|
+
for (const path of paths) {
|
|
192
|
+
const disposition = classifySalvagePath(path)
|
|
193
|
+
if (disposition === 'salvage') candidates.push(path)
|
|
194
|
+
else if (disposition === 'secret') withheld.push(path)
|
|
195
|
+
}
|
|
196
|
+
return { candidates, withheld }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Commit the new, untracked, non-ignored files the agent left behind in `dir`.
|
|
201
|
+
*
|
|
202
|
+
* Bounded by FILE COUNT and TOTAL BYTES, and over either bound it salvages NOTHING and says so:
|
|
203
|
+
* committing a prefix would produce a tree that looks complete and is not, which is the one
|
|
204
|
+
* outcome worse than the loss this exists to prevent.
|
|
205
|
+
*
|
|
206
|
+
* The message names the salvage as a salvage. A commit that arrives on a branch with no
|
|
207
|
+
* explanation is indistinguishable from work the agent chose to make, and this work was chosen by
|
|
208
|
+
* nobody — the run was killed with it still on the floor.
|
|
209
|
+
*
|
|
210
|
+
* CODING MODE ONLY: the caller decides that. A read-only kind has no branch to carry a commit and
|
|
211
|
+
* must never be given one.
|
|
212
|
+
*/
|
|
213
|
+
export async function salvageUntrackedWork(args: {
|
|
214
|
+
dir: string
|
|
215
|
+
/** How the run ended, which is what the commit message has to state. */
|
|
216
|
+
occasion: SalvageOccasion
|
|
217
|
+
logger: Logger
|
|
218
|
+
signal?: AbortSignal
|
|
219
|
+
bounds?: SalvageBounds
|
|
220
|
+
}): Promise<SalvageReport> {
|
|
221
|
+
const bounds = args.bounds ?? DEFAULT_SALVAGE_BOUNDS
|
|
222
|
+
const { candidates, withheld } = partitionSalvageCandidates(
|
|
223
|
+
await listUntrackedFiles(args.dir, args.signal),
|
|
224
|
+
)
|
|
225
|
+
if (withheld.length > 0) {
|
|
226
|
+
args.logger.warn('salvage: withheld secret-bearing files from the commit', { withheld })
|
|
227
|
+
}
|
|
228
|
+
const secrets = withheld.length > 0 ? { withheld } : {}
|
|
229
|
+
if (candidates.length === 0) {
|
|
230
|
+
return { status: 'none', files: [], fileCount: 0, totalBytes: 0, ...secrets }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const totalBytes = await measure(args.dir, candidates)
|
|
234
|
+
const report = {
|
|
235
|
+
files: candidates.slice(0, REPORTED_PATHS),
|
|
236
|
+
fileCount: candidates.length,
|
|
237
|
+
totalBytes,
|
|
238
|
+
...secrets,
|
|
239
|
+
}
|
|
240
|
+
if (candidates.length > bounds.maxFiles || totalBytes > bounds.maxBytes) {
|
|
241
|
+
const reason =
|
|
242
|
+
`${candidates.length} uncommitted new files totalling ${totalBytes} bytes exceed the salvage ` +
|
|
243
|
+
`bounds (${bounds.maxFiles} files / ${bounds.maxBytes} bytes), so none were committed — a ` +
|
|
244
|
+
`partial salvage would read as a complete change.`
|
|
245
|
+
args.logger.warn('salvage: refused, over bounds', { ...report, reason })
|
|
246
|
+
return { status: 'refused', ...report, reason }
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
const commitSha = await commitPaths(
|
|
251
|
+
args.dir,
|
|
252
|
+
candidates,
|
|
253
|
+
salvageCommitMessage(candidates.length, args.occasion),
|
|
254
|
+
args.signal,
|
|
255
|
+
)
|
|
256
|
+
if (!commitSha) return { status: 'none', files: [], fileCount: 0, totalBytes: 0, ...secrets }
|
|
257
|
+
args.logger.warn('salvage: committed the new files the agent left untracked', {
|
|
258
|
+
...report,
|
|
259
|
+
commitSha,
|
|
260
|
+
})
|
|
261
|
+
return { status: 'committed', ...report, commitSha }
|
|
262
|
+
} catch (error) {
|
|
263
|
+
const reason = error instanceof Error ? error.message : String(error)
|
|
264
|
+
args.logger.error('salvage: could not commit the files the agent left behind', {
|
|
265
|
+
...report,
|
|
266
|
+
reason,
|
|
267
|
+
})
|
|
268
|
+
return { status: 'failed', ...report, reason }
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* How the run that left these files behind ended. It decides what the commit message SAYS, which
|
|
274
|
+
* is the whole point of marking a salvage: a commit arriving on a branch with no explanation is
|
|
275
|
+
* indistinguishable from work the agent chose to make and someone chose to keep.
|
|
276
|
+
*/
|
|
277
|
+
export type SalvageOccasion =
|
|
278
|
+
/** The run was killed mid-flight (guard, watchdog, eviction); `cause` is what killed it. */
|
|
279
|
+
| { kind: 'aborted'; cause: string }
|
|
280
|
+
/** The agent finished but never added its own new files. */
|
|
281
|
+
| { kind: 'settled' }
|
|
282
|
+
|
|
283
|
+
/** The salvage commit's message: what it is, why it exists, and how much to trust it. */
|
|
284
|
+
export function salvageCommitMessage(fileCount: number, occasion: SalvageOccasion): string {
|
|
285
|
+
const noun = fileCount === 1 ? 'file' : 'files'
|
|
286
|
+
if (occasion.kind === 'settled') {
|
|
287
|
+
return (
|
|
288
|
+
`chore: commit ${fileCount} new ${noun} the agent left untracked\n\n` +
|
|
289
|
+
`The agent created these files and finished without committing them. The harness committed ` +
|
|
290
|
+
`them so they reach the pull request rather than being discarded with the container.`
|
|
291
|
+
)
|
|
292
|
+
}
|
|
293
|
+
return (
|
|
294
|
+
`chore: salvage ${fileCount} uncommitted ${noun} from an aborted agent run\n\n` +
|
|
295
|
+
`This run was ABORTED (${occasion.cause}) with these files created and never committed. The ` +
|
|
296
|
+
`harness committed them so the work is not lost. They are NOT a reviewed change: nothing ` +
|
|
297
|
+
`checked that they are complete or consistent, and the run had not said it was finished.`
|
|
298
|
+
)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* The banner for a pull request whose ENTIRE content is a salvage.
|
|
303
|
+
*
|
|
304
|
+
* A branch the agent never committed to, which exists only because the harness swept up the
|
|
305
|
+
* untracked files left in that checkout, is not a change anyone proposed. It is still worth
|
|
306
|
+
* opening (dropping it is the loss this whole module exists to prevent, and a peer repository in a
|
|
307
|
+
* multi-repo run is where a cross-service change most easily goes missing), but its reviewer has
|
|
308
|
+
* to be told that before reading it as a considered contribution: the agent may have been building
|
|
309
|
+
* there, or it may have left scratch work behind while working on a sibling repository, and
|
|
310
|
+
* nothing in the diff distinguishes the two.
|
|
311
|
+
*
|
|
312
|
+
* Lives here with {@link salvageCommitMessage} and {@link describeSalvage} because all three are
|
|
313
|
+
* the same job — saying what a salvage is to whoever finds it — and the three had better not drift
|
|
314
|
+
* into describing it differently. The caller decides WHERE it goes.
|
|
315
|
+
*/
|
|
316
|
+
export function salvageOnlyNotice(): string {
|
|
317
|
+
return (
|
|
318
|
+
`> **This branch is a salvage.** The agent committed nothing to this repository; everything ` +
|
|
319
|
+
`here is new files it left uncommitted in the checkout, swept up by the harness so they would ` +
|
|
320
|
+
`not be discarded with the container. Nothing has reviewed them for completeness or ` +
|
|
321
|
+
`relevance, and some may be scratch work from the agent's task in a sibling repository.`
|
|
322
|
+
)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** Total size of `paths` under `dir`; a file that cannot be stat'd counts as zero rather than failing. */
|
|
326
|
+
async function measure(dir: string, paths: string[]): Promise<number> {
|
|
327
|
+
const sizes = await Promise.all(
|
|
328
|
+
paths.map((path) =>
|
|
329
|
+
stat(join(dir, path)).then(
|
|
330
|
+
(info) => info.size,
|
|
331
|
+
() => 0,
|
|
332
|
+
),
|
|
333
|
+
),
|
|
334
|
+
)
|
|
335
|
+
return sizes.reduce((total, size) => total + size, 0)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Where a salvage commit ENDED UP, which the salvage itself cannot know: it commits, and someone
|
|
340
|
+
* else pushes. A commit that was not pushed dies with the container exactly as the uncommitted
|
|
341
|
+
* files would have, so a note that does not say so describes a rescue that did not happen.
|
|
342
|
+
*/
|
|
343
|
+
export interface SalvageDelivery {
|
|
344
|
+
pushed: boolean
|
|
345
|
+
/** Why the push did not land, when it did not. */
|
|
346
|
+
reason?: string
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* What a human can act on, in one or two sentences. Joined onto the failure an aborted run
|
|
351
|
+
* reports, so the person reading "the run was killed" is told in the same breath what became of
|
|
352
|
+
* its work: on the branch and reviewed by nobody, still in the container, or never committed.
|
|
353
|
+
*
|
|
354
|
+
* `delivery` is supplied by whoever pushed. Absent means the caller is on a path where the
|
|
355
|
+
* ordinary push follows (the settle path), so there is nothing extra to say.
|
|
356
|
+
*/
|
|
357
|
+
export function describeSalvage(
|
|
358
|
+
report: SalvageReport,
|
|
359
|
+
delivery?: SalvageDelivery,
|
|
360
|
+
): string | undefined {
|
|
361
|
+
const parts = [describeOutcome(report, delivery), describeWithheld(report)].filter(
|
|
362
|
+
(part): part is string => part !== undefined,
|
|
363
|
+
)
|
|
364
|
+
return parts.length > 0 ? parts.join(' ') : undefined
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** The fate of the files the salvage DID try to keep. */
|
|
368
|
+
function describeOutcome(
|
|
369
|
+
report: SalvageReport,
|
|
370
|
+
delivery: SalvageDelivery | undefined,
|
|
371
|
+
): string | undefined {
|
|
372
|
+
switch (report.status) {
|
|
373
|
+
case 'none':
|
|
374
|
+
return undefined
|
|
375
|
+
case 'committed': {
|
|
376
|
+
const landed =
|
|
377
|
+
delivery && !delivery.pushed
|
|
378
|
+
? `commit ${report.commitSha ?? 'unknown'}, which could NOT be pushed ` +
|
|
379
|
+
`(${delivery.reason ?? 'the push failed'}) and so is lost with the container`
|
|
380
|
+
: `commit ${report.commitSha ?? 'unknown'}`
|
|
381
|
+
return (
|
|
382
|
+
`${report.fileCount} uncommitted new file(s) the agent left behind were salvaged into ` +
|
|
383
|
+
`${landed}; this run was aborted, so review them before trusting them.`
|
|
384
|
+
)
|
|
385
|
+
}
|
|
386
|
+
case 'refused':
|
|
387
|
+
return `Uncommitted new files were NOT salvaged: ${report.reason ?? 'over the salvage bounds'}`
|
|
388
|
+
case 'failed':
|
|
389
|
+
return (
|
|
390
|
+
`${report.fileCount} uncommitted new file(s) were left behind and could NOT be salvaged: ` +
|
|
391
|
+
`${report.reason ?? 'the commit failed'}`
|
|
392
|
+
)
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** The secret-bearing files the salvage refused, named so a live credential can be rotated. */
|
|
397
|
+
function describeWithheld(report: SalvageReport): string | undefined {
|
|
398
|
+
const withheld = report.withheld ?? []
|
|
399
|
+
if (withheld.length === 0) return undefined
|
|
400
|
+
const shown = withheld.slice(0, REPORTED_PATHS).join(', ')
|
|
401
|
+
const rest =
|
|
402
|
+
withheld.length > REPORTED_PATHS ? ` (and ${withheld.length - REPORTED_PATHS} more)` : ''
|
|
403
|
+
return (
|
|
404
|
+
`${withheld.length} file(s) that look credential-bearing were withheld from the salvage and ` +
|
|
405
|
+
`are NOT on the branch: ${shown}${rest}. Re-create them, and rotate anything real they held.`
|
|
406
|
+
)
|
|
407
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { changedPathsFromPorcelain, headCommit, workingTreeStatus } from './git.js'
|
|
2
|
+
import { EFFORT_REPORT_FILE } from './effort.js'
|
|
3
|
+
import { FOLLOW_UPS_FILENAME } from './follow-ups.js'
|
|
4
|
+
import { PR_DESCRIPTION_FILE } from './pr-description.js'
|
|
5
|
+
|
|
6
|
+
// The working-tree answer to "has this run actually changed the repository". The no-progress
|
|
7
|
+
// guard's no-edit bound used to answer it from TOOL NAMES, which is a fact about which tool the
|
|
8
|
+
// model happened to pick, not about the repo: an agent writing every file through `bash`
|
|
9
|
+
// (heredocs, `sed -i`, `node -e`) read as forty calls and not one edit however much it had built,
|
|
10
|
+
// and the guard killed it. This module is the evidence the guard now decides on instead.
|
|
11
|
+
//
|
|
12
|
+
// Kept OFF the hot path. The bound only matters at the instant it is about to abort, so the probe
|
|
13
|
+
// runs there and at most once per run (see `guard-driver.ts`), never per tool call.
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The harness's own side-channel sentinels, written INTO the checkout by the platform rather than
|
|
17
|
+
* by the agent. Excluded from the dirty check, or a run that wrote nothing but its effort report
|
|
18
|
+
* reads as productive and the guard it is meant to satisfy never fires again.
|
|
19
|
+
*
|
|
20
|
+
* Deliberately just this list. A cleverer rule (anything dotted, anything the harness has ever
|
|
21
|
+
* touched) would start excluding the agent's own work — a `.github/workflows/ci.yml` or an
|
|
22
|
+
* `eslint.config.js` is exactly the greenfield deliverable this whole change exists to keep.
|
|
23
|
+
*/
|
|
24
|
+
export const HARNESS_SENTINEL_FILES: readonly string[] = [
|
|
25
|
+
EFFORT_REPORT_FILE,
|
|
26
|
+
FOLLOW_UPS_FILENAME,
|
|
27
|
+
PR_DESCRIPTION_FILE,
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
/** What a workspace probe found. Never `undefined`: a probe that cannot answer THROWS. */
|
|
31
|
+
export interface WorkspaceEvidence {
|
|
32
|
+
/** Whether the repository changed: a dirty working tree, or HEAD moved off the pass's base. */
|
|
33
|
+
mutated: boolean
|
|
34
|
+
/** HEAD at probe time, quoted in the guard's diagnostic so the evidence is on the record. */
|
|
35
|
+
headSha: string
|
|
36
|
+
/** Whether HEAD moved off the sha the pass started from (the agent committed). */
|
|
37
|
+
headMoved: boolean
|
|
38
|
+
/** How many non-sentinel paths the working tree reports as changed. */
|
|
39
|
+
dirtyPathCount: number
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Probes the working tree for evidence the agent changed the repository. Throws if it cannot. */
|
|
43
|
+
export type WorkspaceProbe = () => Promise<WorkspaceEvidence>
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The non-sentinel paths in a porcelain status — the working-tree half of the evidence.
|
|
47
|
+
*
|
|
48
|
+
* Pure, so the sentinel rule is unit-testable without a repository. A sentinel matches by BASENAME
|
|
49
|
+
* as well as by exact path: the agent's cwd is a service subdirectory in a monorepo, so its effort
|
|
50
|
+
* report lands at `services/api/.cat-effort.json`, and a root-anchored comparison would miss it.
|
|
51
|
+
*/
|
|
52
|
+
export function agentChangedPaths(status: string): string[] {
|
|
53
|
+
const sentinels = new Set<string>(HARNESS_SENTINEL_FILES)
|
|
54
|
+
return changedPathsFromPorcelain(status).filter((path) => {
|
|
55
|
+
const basename = path.slice(path.lastIndexOf('/') + 1)
|
|
56
|
+
return !sentinels.has(path) && !sentinels.has(basename)
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Build the probe for one pass: the working tree at `dir`, judged against the sha the pass
|
|
62
|
+
* started from.
|
|
63
|
+
*
|
|
64
|
+
* The repository changed if the tree is dirty or HEAD has moved off `baseSha`. Both are the agent
|
|
65
|
+
* changing the repo, and between them they cover the two shapes the tool-name proxy missed: files
|
|
66
|
+
* written through `bash` and left in the tree, and files written and then committed. Gitignored
|
|
67
|
+
* paths are excluded by git itself, so an `npm install` still reads as nothing.
|
|
68
|
+
*
|
|
69
|
+
* ORDER MATTERS, and carries the "is this a repository at all" question. The status runs FIRST and
|
|
70
|
+
* is never caught: a `dir` that is no git repository fails there, and the driver treats a throw as
|
|
71
|
+
* inconclusive (re-arm and warn, never abort). HEAD is read second and a failure to read it is
|
|
72
|
+
* NOT a failed probe: a scaffold-from-scratch checkout has no commit yet, so `rev-parse HEAD`
|
|
73
|
+
* errors in exactly the case the dirty-tree half was written for. It reads as the empty sha, which
|
|
74
|
+
* is what the pass baselined against too, so the two agree that HEAD has not moved and the tree
|
|
75
|
+
* decides. Catching it around the status instead would turn "not a repository" into "no evidence
|
|
76
|
+
* of change" and hand the guard a clean verdict it has no business acting on.
|
|
77
|
+
*
|
|
78
|
+
* INJECTED, never imported by the guard: the guard stays pure and synchronous so it can be driven
|
|
79
|
+
* over a fixed event sequence in a unit test, and the git access lives out here where a test
|
|
80
|
+
* substitutes a stub.
|
|
81
|
+
*/
|
|
82
|
+
export function createWorkspaceProbe(deps: {
|
|
83
|
+
dir: string
|
|
84
|
+
/** HEAD when this pass began — a repair round's base is its own start, not the clone's. */
|
|
85
|
+
baseSha: string
|
|
86
|
+
signal?: AbortSignal
|
|
87
|
+
}): WorkspaceProbe {
|
|
88
|
+
return async () => {
|
|
89
|
+
const status = await workingTreeStatus(deps.dir, deps.signal)
|
|
90
|
+
const dirty = agentChangedPaths(status)
|
|
91
|
+
const headSha = await readHeadOrEmpty(deps.dir, deps.signal)
|
|
92
|
+
const headMoved = headSha !== deps.baseSha
|
|
93
|
+
return {
|
|
94
|
+
mutated: dirty.length > 0 || headMoved,
|
|
95
|
+
headSha,
|
|
96
|
+
headMoved,
|
|
97
|
+
dirtyPathCount: dirty.length,
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* HEAD at `dir`, or the empty sha where there is no commit to read.
|
|
104
|
+
*
|
|
105
|
+
* The one shared reader for both the pass BASELINE and the probe, so the two can never disagree
|
|
106
|
+
* about what a commit-less checkout is worth: if the baseline tolerates a missing HEAD and the
|
|
107
|
+
* probe throws on it, a from-scratch build has no working bound at all.
|
|
108
|
+
*/
|
|
109
|
+
export async function readHeadOrEmpty(dir: string, signal?: AbortSignal): Promise<string> {
|
|
110
|
+
return headCommit(dir, signal).catch(() => '')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* One probe over SEVERAL checkouts, for a run whose cwd is not itself a repository.
|
|
115
|
+
*
|
|
116
|
+
* A multi-repo run works at a WORKSPACE ROOT holding sibling checkouts, so probing the cwd asks
|
|
117
|
+
* git about a directory that is no repository: every probe throws, the driver re-arms forever and
|
|
118
|
+
* the no-edit bound is permanently unenforceable. The honest question there is "did the run change
|
|
119
|
+
* ANY of the repositories it was given", which is this.
|
|
120
|
+
*
|
|
121
|
+
* Mutation is a disjunction and inconclusiveness WINS OVER cleanliness. A leg that answers
|
|
122
|
+
* `mutated` settles it, since one changed repository is the run making progress. But a leg that
|
|
123
|
+
* THREW might have been the changed one, so `mutated: false` is only reported when every leg
|
|
124
|
+
* actually answered; otherwise this throws and the driver re-arms, which is the same fail-open
|
|
125
|
+
* disposition a single failing probe already gets. Killing a productive run is the expensive error.
|
|
126
|
+
*
|
|
127
|
+
* `headSha` joins the answering legs' shas, because a workspace has no single HEAD and quoting one
|
|
128
|
+
* leg's would put a sha in the abort diagnostic that says nothing about where the run actually was.
|
|
129
|
+
*/
|
|
130
|
+
export function composeWorkspaceProbes(probes: readonly WorkspaceProbe[]): WorkspaceProbe {
|
|
131
|
+
if (probes.length === 1) return probes[0] as WorkspaceProbe
|
|
132
|
+
return async () => {
|
|
133
|
+
const settled = await Promise.allSettled(probes.map((probe) => probe()))
|
|
134
|
+
const answered = settled.filter(
|
|
135
|
+
(result): result is PromiseFulfilledResult<WorkspaceEvidence> =>
|
|
136
|
+
result.status === 'fulfilled',
|
|
137
|
+
)
|
|
138
|
+
const evidence = answered.map((result) => result.value)
|
|
139
|
+
const mutated = evidence.some((one) => one.mutated)
|
|
140
|
+
if (!mutated && answered.length < probes.length) {
|
|
141
|
+
const first = settled.find((result) => result.status === 'rejected')
|
|
142
|
+
throw new Error(
|
|
143
|
+
`${probes.length - answered.length} of ${probes.length} checkouts could not be probed and ` +
|
|
144
|
+
`none of the rest had changed, so whether this run changed anything is unknown`,
|
|
145
|
+
{ cause: first?.status === 'rejected' ? first.reason : undefined },
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
mutated,
|
|
150
|
+
headSha: evidence.map((one) => one.headSha).join(', '),
|
|
151
|
+
headMoved: evidence.some((one) => one.headMoved),
|
|
152
|
+
dirtyPathCount: evidence.reduce((total, one) => total + one.dirtyPathCount, 0),
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|