@defrex/autobuild 1.15.0 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +110 -10
  3. package/docs/answering-blocked-builds.md +34 -1
  4. package/docs/architecture.md +41 -7
  5. package/docs/configuration.md +41 -7
  6. package/docs/dispatcher-hosting.md +3 -0
  7. package/docs/operator-api.md +15 -5
  8. package/docs/plugin-authoring.md +1 -0
  9. package/docs/remote-store-protocol.md +13 -3
  10. package/docs/remote-ticket-protocol.md +3 -1
  11. package/docs/restacking.md +41 -1
  12. package/docs/setup.md +15 -6
  13. package/docs/troubleshooting.md +51 -3
  14. package/docs/workspaces.md +4 -1
  15. package/package.json +3 -3
  16. package/packages/core/src/cli/accounts.ts +3 -0
  17. package/packages/core/src/cli/binary-direct.ts +14 -1
  18. package/packages/core/src/cli/binary.ts +10 -7
  19. package/packages/core/src/cli/dispatch.ts +610 -356
  20. package/packages/core/src/cli/secrets.ts +2 -0
  21. package/packages/core/src/config/schema.ts +15 -2
  22. package/packages/core/src/events/payloads.ts +8 -5
  23. package/packages/core/src/events/repository.ts +12 -6
  24. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/e28c68e63cbd265f.json +108 -0
  25. package/packages/core/src/kernel/engine.ts +118 -0
  26. package/packages/core/src/kernel/reconcile-restack.ts +72 -9
  27. package/packages/core/src/kernel/reducer-revisions.ts +1 -0
  28. package/packages/core/src/kernel/tracked-dirt.ts +173 -0
  29. package/packages/core/src/ontology.ts +1 -0
  30. package/packages/core/src/operator/index.ts +10 -0
  31. package/packages/core/src/operator/merged-builds.ts +51 -0
  32. package/packages/core/src/operator/registry.ts +190 -101
  33. package/packages/core/src/operator/repo-argument.ts +43 -0
  34. package/packages/core/src/ports/tickets/grants.ts +2 -0
  35. package/packages/core/src/ports/tickets/remote.ts +2 -0
  36. package/packages/core/src/ports/types.ts +25 -4
  37. package/packages/core/src/ports/workspace/vercel-command-diagnostics.ts +144 -0
  38. package/packages/core/src/ports/workspace/vercel-sandbox.ts +811 -169
  39. package/packages/core/src/processes/build-child.ts +15 -1
  40. package/packages/core/src/processes/build-runner.ts +349 -20
  41. package/packages/core/src/processes/dispatcher.ts +509 -104
  42. package/packages/core/src/processes/harvest-child.ts +21 -1
  43. package/packages/core/src/processes/harvest-runner.ts +82 -35
  44. package/packages/core/src/processes/invocation-budget.ts +6 -0
  45. package/packages/core/src/processes/launch-cut.ts +37 -0
  46. package/packages/core/src/processes/phase-failure-budget.ts +1 -0
  47. package/packages/core/src/processes/provisioning-progress.ts +23 -0
  48. package/packages/core/src/processes/repo-view.ts +25 -4
  49. package/packages/core/src/processes/workspace-preparation.ts +1 -0
  50. package/packages/core/src/store/adapter.ts +34 -5
  51. package/packages/core/src/store/build-feed.ts +148 -22
  52. package/packages/core/src/store/build-scope.ts +12 -3
  53. package/packages/core/src/store/caller-attribution.ts +170 -0
  54. package/packages/core/src/store/contract.ts +1038 -165
  55. package/packages/core/src/store/local/schema.ts +12 -1
  56. package/packages/core/src/store/local/store.ts +260 -25
  57. package/packages/core/src/store/memory.ts +156 -9
  58. package/packages/core/src/store/phase-session.ts +23 -3
  59. package/packages/core/src/store/reducer-value-shapes.json +5351 -5338
  60. package/packages/core/src/store/remote/client.ts +90 -11
  61. package/packages/core/src/store/remote/credentials.ts +2 -0
  62. package/packages/core/src/store/remote/index.ts +31 -5
  63. package/packages/core/src/store/remote/protocol.ts +45 -8
  64. package/packages/core/src/store/remote/released-clients.json +22 -0
  65. package/packages/core/src/store/remote/released-reducer-values/1.16.0.json +36358 -0
  66. package/packages/core/src/store/remote/released-reducer-values/1.17.0.json +36367 -0
  67. package/packages/core/src/store/remote/released-reducer-values/index.ts +20 -16
  68. package/packages/core/src/store/remote/released-value-policy.ts +7 -0
  69. package/packages/core/src/store/session-handle.ts +12 -3
  70. package/packages/core/src/store/types.ts +48 -3
  71. package/packages/core/src/testing/reduced-read-budget.ts +310 -12
  72. package/skills/guide/SKILL.md +58 -9
  73. package/skills/guide/references/operator-api.md +15 -5
  74. package/skills/guide/references/plugin-authoring.md +1 -0
  75. package/skills/guide/references/remote-store-protocol.md +13 -3
  76. package/skills/guide/references/remote-ticket-protocol.md +3 -1
  77. package/skills/guide/references/restacking.md +41 -1
  78. package/skills/guide/references/setup.md +15 -6
  79. package/skills/operate/SKILL.md +31 -7
  80. package/templates/hosted-service/package.json +2 -2
@@ -0,0 +1,173 @@
1
+ import { compilePathSelectors } from '../config/path-selectors'
2
+ import type { GeneratedEntry } from '../config/schema'
3
+ import type { Exec } from '../ports/workspace/git-worktree'
4
+
5
+ /**
6
+ * Tracked worktree dirt: uncommitted changes to files Git tracks. Declared
7
+ * `[[generated]]` outputs are disposable and discarded before a replay;
8
+ * anything else is work that must reach an agent, never a silent discard.
9
+ */
10
+
11
+ /** Paths with uncommitted tracked changes, sorted and unique. Both sides of a
12
+ * staged rename or copy are included, so restoring them restores the pair. */
13
+ export async function listTrackedDirt(exec: Exec, cwd: string): Promise<string[]> {
14
+ const result = await exec(['git', 'status', '--porcelain=v1', '-z', '--untracked-files=no'], {
15
+ cwd,
16
+ })
17
+ if (result.exitCode !== 0) throw new Error(`git status failed: ${result.stderr || result.stdout}`)
18
+ return parsePorcelainZ(result.stdout)
19
+ }
20
+
21
+ /** Parse `git status --porcelain=v1 -z` records. A rename or copy record is
22
+ * followed by its source path as a separate NUL-terminated field. */
23
+ export function parsePorcelainZ(output: string): string[] {
24
+ const fields = output.split('\0')
25
+ const paths = new Set<string>()
26
+ for (let index = 0; index < fields.length; index++) {
27
+ const record = fields[index]!
28
+ if (record.length < 4) continue
29
+ const status = record.slice(0, 2)
30
+ paths.add(record.slice(3))
31
+ if (status.includes('R') || status.includes('C')) {
32
+ const source = fields[++index]
33
+ if (source) paths.add(source)
34
+ }
35
+ }
36
+ return [...paths].sort()
37
+ }
38
+
39
+ export interface TrackedDirtClassification {
40
+ /** Paths some `[[generated]]` selector owns. */
41
+ generated: string[]
42
+ /** Paths no declaration owns. */
43
+ undeclared: string[]
44
+ }
45
+
46
+ export function classifyTrackedDirt(
47
+ paths: readonly string[],
48
+ declarations: readonly GeneratedEntry[],
49
+ ): TrackedDirtClassification {
50
+ const matches = compilePathSelectors(declarations.flatMap((entry) => entry.paths))
51
+ const generated: string[] = []
52
+ const undeclared: string[] = []
53
+ for (const path of paths) (matches(path) ? generated : undeclared).push(path)
54
+ return { generated, undeclared }
55
+ }
56
+
57
+ /** A bounded, human-readable path list: at most `limit`, then `(+N more)`. */
58
+ export function formatPathList(paths: readonly string[], limit = 20): string {
59
+ const shown = paths.slice(0, limit).join(', ')
60
+ return paths.length > limit ? `${shown} (+${paths.length - limit} more)` : shown
61
+ }
62
+
63
+ /** Reset `paths` to HEAD. A staged add absent from HEAD is only unstaged and
64
+ * left untracked, never deleted. Throws unless the paths end clean. */
65
+ export async function restoreTrackedPaths(
66
+ exec: Exec,
67
+ cwd: string,
68
+ paths: readonly string[],
69
+ ): Promise<void> {
70
+ if (paths.length === 0) return
71
+ const git = async (args: string[], allowFailure = false) => {
72
+ const result = await exec(['git', ...args], { cwd })
73
+ if (result.exitCode !== 0 && !allowFailure)
74
+ throw new Error(`git ${args.join(' ')} failed: ${result.stderr || result.stdout}`)
75
+ return result
76
+ }
77
+ await git(['reset', '-q', 'HEAD', '--', ...paths])
78
+ const present: string[] = []
79
+ for (const path of paths)
80
+ if ((await git(['cat-file', '-e', `HEAD:${path}`], true)).exitCode === 0) present.push(path)
81
+ if (present.length > 0) await git(['checkout', '-q', 'HEAD', '--', ...present])
82
+ const remaining = new Set(await listTrackedDirt(exec, cwd))
83
+ const left = paths.filter((path) => remaining.has(path))
84
+ if (left.length > 0) throw new Error(`could not restore tracked paths: ${formatPathList(left)}`)
85
+ }
86
+
87
+ /** Preparation found tracked changes outside `[[generated]]`; nothing was
88
+ * discarded. `paths` are the undeclared paths. */
89
+ export class TrackedDirtRefusal extends Error {
90
+ constructor(
91
+ readonly prefix: string,
92
+ readonly paths: readonly string[],
93
+ ) {
94
+ super(`${prefix}: uncommitted tracked changes outside [[generated]]: ${formatPathList(paths)}`)
95
+ this.name = 'TrackedDirtRefusal'
96
+ }
97
+ }
98
+
99
+ export type DirtyWorktreeOrigin =
100
+ | { origin: 'setup'; command: string }
101
+ | { origin: 'restack' }
102
+ | { origin: 'reconcile' }
103
+
104
+ /** The `policy/dirty-worktree` escalation question. */
105
+ export function dirtyWorktreeQuestion(
106
+ input: DirtyWorktreeOrigin & { paths: readonly string[] },
107
+ ): string {
108
+ const where =
109
+ input.origin === 'setup'
110
+ ? `[commands].setup ${JSON.stringify(input.command)} modified tracked files outside [[generated]]`
111
+ : `${input.origin} preparation found uncommitted tracked changes outside [[generated]]`
112
+ return (
113
+ `${where}: ${formatPathList(input.paths)}. ` +
114
+ 'Autobuild never commits or discards them on its own. Answer with guidance or a bare retry ' +
115
+ 'to return the build to implement, which commits, reverts or regenerates them (or fixes ' +
116
+ 'setup or [[generated]]); a frozen preparation is superseded, or settled first with the ' +
117
+ 'changes set aside as a build artifact.'
118
+ )
119
+ }
120
+
121
+ /** Artifact kind holding the binary patch of set-aside dirt. */
122
+ export const dirtyWorktreeArtifact = (escalation: string) => `dirty-worktree-${escalation}`
123
+ /** Deterministic observation id recording a set-aside. */
124
+ export const dirtyWorktreeSetAsideObservation = (escalation: string) =>
125
+ `dirty-worktree:${escalation}:set-aside`
126
+
127
+ /** Split a `git diff` patch into its per-file sections, keyed by header. */
128
+ function patchSections(patch: string): Map<string, string> {
129
+ const sections = new Map<string, string>()
130
+ for (const section of patch.split(/^(?=diff --git )/m)) {
131
+ if (!section.startsWith('diff --git ')) continue
132
+ sections.set(section.slice(0, section.indexOf('\n') + 1 || section.length), section)
133
+ }
134
+ return sections
135
+ }
136
+
137
+ /**
138
+ * The set-aside patch after a later capture: every file the current capture
139
+ * shows takes its current section, and every file only the earlier capture
140
+ * held keeps its earlier section, so no captured change is dropped.
141
+ * Base85 binary hunks never start a line with `diff --git `.
142
+ */
143
+ export function mergePatches(previous: string, current: string): string {
144
+ const merged = patchSections(previous)
145
+ for (const [header, section] of patchSections(current)) merged.set(header, section)
146
+ return [...merged.values()].join('')
147
+ }
148
+
149
+ /** The recovery implement round's feedback: question, answer and, when the
150
+ * dirt was set aside, how to get it back. */
151
+ export function dirtyWorktreeGuidance(input: {
152
+ question: string
153
+ /** The operator's guidance; absent for a bare retry. */
154
+ answer?: string
155
+ setAside?: string
156
+ }): string {
157
+ const parts = [`Dirty tracked worktree: ${input.question}`]
158
+ parts.push(
159
+ input.answer === undefined ? 'Operator answer: retry.' : `Operator answer: ${input.answer}`,
160
+ )
161
+ if (input.setAside !== undefined)
162
+ parts.push(
163
+ `The changes were set aside before the kernel moved the workspace. Restore them with ` +
164
+ `\`ab artifact download "$AB_BUILD" ${input.setAside} --output .ab/${input.setAside}.patch\` and ` +
165
+ `\`git apply --binary --3way .ab/${input.setAside}.patch\`, then decide what to keep.`,
166
+ )
167
+ else parts.push('The changes are still in the worktree.')
168
+ parts.push(
169
+ 'Commit, revert or regenerate each listed file, or fix [commands].setup or [[generated]] ' +
170
+ 'so setup stops modifying tracked files, then finish the round.',
171
+ )
172
+ return parts.join('\n\n')
173
+ }
@@ -119,6 +119,7 @@ export const policyEscalationCauseSchema = z.enum([
119
119
  'wait-unresolvable',
120
120
  'measurement-failed',
121
121
  'target-rewritten',
122
+ 'dirty-worktree',
122
123
  ])
123
124
  export type PolicyEscalationCause = z.infer<typeof policyEscalationCauseSchema>
124
125
 
@@ -47,6 +47,16 @@ export {
47
47
  toggleHarvestGate,
48
48
  toggleRepositorySetting,
49
49
  } from './control'
50
+ export {
51
+ listOperatorMergedBuilds,
52
+ type OperatorMergedBuild,
53
+ type OperatorMergedBuildPage,
54
+ } from './merged-builds'
55
+ export {
56
+ REPO_ARGUMENT_CONTRACT,
57
+ REPOSITORY_ARGUMENT_MESSAGE,
58
+ hasRepositoryArgument,
59
+ } from './repo-argument'
50
60
  export {
51
61
  getHarvestStatus,
52
62
  getOperatorBuild,
@@ -0,0 +1,51 @@
1
+ /**
2
+ * `builds.list` with scope `merged`: a repository's merged builds, most
3
+ * recently merged first, paged.
4
+ *
5
+ * Read cost: exactly one `store.listMergedBuildsPage` call per request, at most
6
+ * `limit + 1` (≤ 101) listing-index entries joined to build records. Every
7
+ * field below comes from that page; nothing reads events, reduced values or
8
+ * another build record, so the cost is the same at 10 builds or 10,000 and at
9
+ * 100 events or 100,000.
10
+ */
11
+ import type { TicketRef } from '../ontology'
12
+ import type { MergedBuildPageOptions, MergedBuildTarget } from '../store/build-feed'
13
+ import type { BuildStore } from '../store/types'
14
+
15
+ /** One merged build as `builds.list` scope `merged` reports it. */
16
+ export interface OperatorMergedBuild {
17
+ slug: string
18
+ /** When the build merged: its merging `build.completed`, never a later
19
+ * cleanup write. */
20
+ mergedAt: string
21
+ kind: 'leaf' | 'node'
22
+ ticket?: TicketRef
23
+ pr?: { number: number; url: string }
24
+ /** The node build a child build targets; absent for root-targeting builds. */
25
+ target?: MergedBuildTarget
26
+ }
27
+
28
+ export interface OperatorMergedBuildPage {
29
+ builds: OperatorMergedBuild[]
30
+ /** Pass back as `cursor`, with the same window, for the next page; null on
31
+ * the last page. */
32
+ next: string | null
33
+ }
34
+
35
+ export async function listOperatorMergedBuilds(
36
+ opts: { store: BuildStore; repo: string } & MergedBuildPageOptions,
37
+ ): Promise<OperatorMergedBuildPage> {
38
+ const { store, repo, ...page } = opts
39
+ const answer = await store.listMergedBuildsPage(repo, page)
40
+ return {
41
+ builds: answer.builds.map((build) => ({
42
+ slug: build.record.slug,
43
+ mergedAt: build.mergedAt,
44
+ kind: build.kind,
45
+ ...(build.record.ticket ? { ticket: build.record.ticket } : {}),
46
+ ...(build.pr ? { pr: build.pr } : {}),
47
+ ...(build.target ? { target: build.target } : {}),
48
+ })),
49
+ next: answer.next,
50
+ }
51
+ }