@defrex/autobuild 1.3.0 → 1.4.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 (142) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +302 -26
  3. package/docs/accounts.md +38 -0
  4. package/docs/architecture.md +187 -19
  5. package/docs/build-store.md +18 -0
  6. package/docs/builds.md +5 -1
  7. package/docs/cli.md +1 -1
  8. package/docs/configuration.md +38 -19
  9. package/docs/dispatcher-hosting.md +42 -0
  10. package/docs/dispatcher.md +41 -0
  11. package/docs/forges.md +5 -0
  12. package/docs/integration-tree.md +8 -0
  13. package/docs/operating.md +11 -3
  14. package/docs/operator-api.md +37 -4
  15. package/docs/plugin-authoring.md +45 -1
  16. package/docs/remote-store-protocol.md +258 -42
  17. package/docs/remote-ticket-protocol.md +12 -10
  18. package/docs/restacking.md +60 -3
  19. package/docs/setup.md +37 -8
  20. package/docs/skills.md +1 -1
  21. package/docs/spec-standard.md +37 -3
  22. package/docs/troubleshooting.md +9 -0
  23. package/docs/workspaces.md +1 -1
  24. package/package.json +4 -3
  25. package/packages/core/src/accounts/availability.ts +96 -0
  26. package/packages/core/src/accounts/local-owner.ts +49 -2
  27. package/packages/core/src/cli/attention.ts +2 -0
  28. package/packages/core/src/cli/binary.ts +17 -1
  29. package/packages/core/src/cli/context.ts +62 -0
  30. package/packages/core/src/cli/dashboard/model.ts +12 -3
  31. package/packages/core/src/cli/dashboard/poll.ts +65 -8
  32. package/packages/core/src/cli/dashboard/refresh.ts +2 -0
  33. package/packages/core/src/cli/dashboard/render.ts +46 -5
  34. package/packages/core/src/cli/dispatch-args.ts +27 -3
  35. package/packages/core/src/cli/dispatch-frontend.ts +178 -52
  36. package/packages/core/src/cli/dispatch-mode.ts +94 -0
  37. package/packages/core/src/cli/dispatch-preflight.ts +43 -0
  38. package/packages/core/src/cli/dispatch-repository-feed.ts +45 -9
  39. package/packages/core/src/cli/dispatch.ts +145 -11
  40. package/packages/core/src/cli/harvest.ts +2 -1
  41. package/packages/core/src/cli/help.ts +16 -3
  42. package/packages/core/src/cli/historical-build-state.ts +6 -0
  43. package/packages/core/src/cli/main.ts +15 -2
  44. package/packages/core/src/cli/publication-settlement.ts +17 -0
  45. package/packages/core/src/cli/remote-poll.ts +11 -1
  46. package/packages/core/src/cli/repository-status.ts +11 -0
  47. package/packages/core/src/cli/status.ts +7 -1
  48. package/packages/core/src/cli/wait.ts +9 -2
  49. package/packages/core/src/cli/watch.ts +38 -3
  50. package/packages/core/src/config/roles.ts +16 -1
  51. package/packages/core/src/events/catalog.ts +7 -0
  52. package/packages/core/src/events/payloads.ts +85 -2
  53. package/packages/core/src/events/policy.ts +10 -0
  54. package/packages/core/src/events/repository.ts +35 -0
  55. package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +189 -0
  56. package/packages/core/src/kernel/dispatch-placement.ts +30 -0
  57. package/packages/core/src/kernel/engine.ts +19 -12
  58. package/packages/core/src/kernel/generators/leaf-restack-log.ts +17 -1
  59. package/packages/core/src/kernel/leaf-restack-state.ts +24 -2
  60. package/packages/core/src/kernel/log-index.ts +1 -1
  61. package/packages/core/src/kernel/node-landing.ts +27 -10
  62. package/packages/core/src/kernel/node-restack-demand.ts +1 -0
  63. package/packages/core/src/kernel/node-restack-state.ts +16 -1
  64. package/packages/core/src/kernel/node-restack-transaction.ts +14 -0
  65. package/packages/core/src/kernel/read-facts.ts +114 -9
  66. package/packages/core/src/kernel/reconcile-head.ts +34 -11
  67. package/packages/core/src/kernel/reconcile-restack.ts +106 -72
  68. package/packages/core/src/kernel/reducer-registry.ts +3 -0
  69. package/packages/core/src/kernel/reducer.ts +20 -2
  70. package/packages/core/src/kernel/replay-retention.ts +176 -0
  71. package/packages/core/src/kernel/target-lineage.ts +65 -0
  72. package/packages/core/src/kernel/target-observation.ts +117 -0
  73. package/packages/core/src/operator/registry.ts +5 -13
  74. package/packages/core/src/operator/tickets.ts +8 -2
  75. package/packages/core/src/plugin-sdk/index.ts +1 -0
  76. package/packages/core/src/ports/forge/fake.ts +22 -0
  77. package/packages/core/src/ports/forge/git-rewrite.ts +49 -12
  78. package/packages/core/src/ports/forge/github.ts +42 -10
  79. package/packages/core/src/ports/forge/local-git.ts +16 -1
  80. package/packages/core/src/ports/types.ts +58 -4
  81. package/packages/core/src/ports/workspace/account-policy.ts +2 -1
  82. package/packages/core/src/ports/workspace/commit-preservation.ts +175 -0
  83. package/packages/core/src/ports/workspace/create.ts +2 -0
  84. package/packages/core/src/ports/workspace/fake.ts +4 -0
  85. package/packages/core/src/ports/workspace/git-worktree.ts +22 -1
  86. package/packages/core/src/ports/workspace/vercel-sandbox.ts +304 -56
  87. package/packages/core/src/processes/build-runner.ts +240 -111
  88. package/packages/core/src/processes/dispatcher-selectors.ts +3 -2
  89. package/packages/core/src/processes/dispatcher.ts +262 -80
  90. package/packages/core/src/processes/execution-no-progress.ts +7 -0
  91. package/packages/core/src/processes/harvest-runner.ts +6 -7
  92. package/packages/core/src/processes/publication-state.ts +2 -1
  93. package/packages/core/src/processes/repo-view.ts +22 -0
  94. package/packages/core/src/processes/workspace-preservation.ts +164 -0
  95. package/packages/core/src/scenarios/harness.ts +7 -0
  96. package/packages/core/src/store/adapter.ts +9 -0
  97. package/packages/core/src/store/build-scope.ts +28 -0
  98. package/packages/core/src/store/contract.ts +125 -0
  99. package/packages/core/src/store/event-reads.ts +156 -0
  100. package/packages/core/src/store/local/schema.ts +11 -0
  101. package/packages/core/src/store/local/store.ts +117 -2
  102. package/packages/core/src/store/memory.ts +237 -0
  103. package/packages/core/src/store/phase-session.ts +47 -0
  104. package/packages/core/src/store/reduced-reads.ts +8 -0
  105. package/packages/core/src/store/reducer-value-shapes.json +6262 -5227
  106. package/packages/core/src/store/reducer-value-validation.ts +3 -0
  107. package/packages/core/src/store/reducer-values.ts +62 -2
  108. package/packages/core/src/store/remote/client.ts +161 -16
  109. package/packages/core/src/store/remote/compatibility.ts +177 -46
  110. package/packages/core/src/store/remote/errors.ts +16 -0
  111. package/packages/core/src/store/remote/index.ts +11 -0
  112. package/packages/core/src/store/remote/protocol.ts +22 -0
  113. package/packages/core/src/store/remote/released-clients.json +37 -0
  114. package/packages/core/src/store/remote/released-clients.ts +88 -0
  115. package/packages/core/src/store/remote/released-reducer-values/1.3.0.json +25093 -0
  116. package/packages/core/src/store/remote/released-reducer-values/1.4.0.json +26128 -0
  117. package/packages/core/src/store/remote/released-reducer-values/index.ts +13 -0
  118. package/packages/core/src/store/remote/version.ts +5 -2
  119. package/packages/core/src/store/repo-state-events.ts +4 -3
  120. package/packages/core/src/store/scan-cadence.ts +15 -0
  121. package/packages/core/src/store/session-handle.ts +18 -0
  122. package/packages/core/src/store/types.ts +27 -6
  123. package/packages/core/src/testing/dispatch-read-budget.ts +313 -2
  124. package/packages/core/src/testing/index.ts +5 -0
  125. package/packages/core/src/testing/reduced-read-budget.ts +728 -5
  126. package/skills/guide/SKILL.md +177 -16
  127. package/skills/guide/references/accounts.md +38 -0
  128. package/skills/guide/references/integration-tree.md +8 -0
  129. package/skills/guide/references/operator-api.md +37 -4
  130. package/skills/guide/references/plugin-authoring.md +45 -1
  131. package/skills/guide/references/remote-store-protocol.md +258 -42
  132. package/skills/guide/references/remote-ticket-protocol.md +12 -10
  133. package/skills/guide/references/restacking.md +60 -3
  134. package/skills/guide/references/setup.md +37 -8
  135. package/skills/guide/references/spec-standard.md +37 -3
  136. package/skills/implement/SKILL.md +12 -3
  137. package/skills/operate/SKILL.md +64 -7
  138. package/skills/plan/SKILL.md +27 -4
  139. package/skills/plan-review/SKILL.md +22 -2
  140. package/skills/reconcile/SKILL.md +10 -1
  141. package/skills/spec/SKILL.md +7 -4
  142. package/templates/hosted-service/package.json +2 -2
@@ -0,0 +1,175 @@
1
+ import type { Exec } from './git-worktree'
2
+ import type { ForgeRewrite, WorkspacePreservationHead } from '../types'
3
+
4
+ /** Boundary-only Git inspection. Inventory traverses the unpublished change,
5
+ * while immutable archive acknowledgement proves the tip and its ancestry. */
6
+ export async function preserveGitCommits(input: {
7
+ exec: Exec
8
+ workspacePath: string
9
+ branch: string
10
+ slug: string
11
+ base: string
12
+ candidates: string[]
13
+ archive: ForgeRewrite['archive']
14
+ }): Promise<WorkspacePreservationHead[]> {
15
+ const git = async (args: string[], optional = false) => {
16
+ const result = await input.exec(['git', ...args], { cwd: input.workspacePath })
17
+ if (result.exitCode !== 0 && !(optional && result.exitCode === 1))
18
+ throw new Error(`workspace preservation git ${args.join(' ')} failed: ${result.stderr}`)
19
+ return result
20
+ }
21
+ const branch = await git(
22
+ ['rev-parse', '--verify', '--quiet', `refs/heads/${input.branch}^{commit}`],
23
+ true,
24
+ )
25
+ const head = await git(['rev-parse', '--verify', 'HEAD^{commit}'])
26
+ const heads: WorkspacePreservationHead[] = []
27
+ const branchSha = branch.exitCode === 0 ? branch.stdout.trim() : undefined
28
+ const candidates = [
29
+ ...new Set([...(branchSha ? [branchSha] : []), head.stdout.trim(), ...input.candidates]),
30
+ ]
31
+ // The base is the authoritative branch (or recorded immutable initial base),
32
+ // never an arbitrary local remote-tracking cache.
33
+ await git(['cat-file', '-e', `${input.base}^{commit}`])
34
+ for (const sha of candidates) {
35
+ if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(sha))
36
+ throw new Error('invalid preservation candidate')
37
+ await git(['cat-file', '-e', `${sha}^{commit}`])
38
+ const commits = (await git(['rev-list', sha, `^${input.base}`])).stdout
39
+ .trim()
40
+ .split('\n')
41
+ .filter(Boolean)
42
+ if (commits.length === 0) continue
43
+ const remoteRef = `refs/autobuild/archive/${input.slug}/${sha}`
44
+ await input.archive({ workspacePath: input.workspacePath, ref: remoteRef, sha })
45
+ heads.push({ sha, remoteRef, role: sha === branchSha ? 'branch' : 'producer', commits })
46
+ }
47
+ return heads
48
+ }
49
+
50
+ /** Fetch only recorded immutable refs. Attaching an unpublished branch is safe
51
+ * only over an ancestor; detached producer objects never move the branch. */
52
+ export async function restoreGitCommits(input: {
53
+ exec: Exec
54
+ workspacePath: string
55
+ branch: string
56
+ heads: WorkspacePreservationHead[]
57
+ local?: boolean
58
+ }): Promise<void> {
59
+ const git = async (args: string[], optional = false) => {
60
+ const result = await input.exec(['git', ...args], { cwd: input.workspacePath })
61
+ if (result.exitCode !== 0 && !optional)
62
+ throw new Error(`workspace restoration git ${args.join(' ')} failed: ${result.stderr}`)
63
+ return result
64
+ }
65
+ for (const head of input.heads) {
66
+ if (
67
+ !/^refs\/autobuild\/archive\/[a-z0-9-]+\/[0-9a-f]+$/.test(head.remoteRef) ||
68
+ !head.remoteRef.endsWith(`/${head.sha}`)
69
+ )
70
+ throw new Error('invalid workspace restoration ref')
71
+ if (!input.local)
72
+ await git([
73
+ 'fetch',
74
+ '--no-tags',
75
+ '--no-write-fetch-head',
76
+ '--refmap=',
77
+ 'origin',
78
+ `+${head.remoteRef}:${head.remoteRef}`,
79
+ ])
80
+ const actual = (
81
+ await git(['rev-parse', '--verify', `${head.remoteRef}^{commit}`])
82
+ ).stdout.trim()
83
+ if (actual !== head.sha) throw new Error(`workspace restoration ref ${head.remoteRef} changed`)
84
+ for (const sha of [head.sha, ...head.commits]) await git(['cat-file', '-e', `${sha}^{commit}`])
85
+ if (head.role !== 'branch') continue
86
+ const ref = `refs/heads/${input.branch}`
87
+ const current = (await git(['rev-parse', '--verify', ref])).stdout.trim()
88
+ if (current === head.sha) continue
89
+ const preservedAncestor = await git(['merge-base', '--is-ancestor', head.sha, current], true)
90
+ if (preservedAncestor.exitCode === 0) continue
91
+ const currentAncestor = await git(['merge-base', '--is-ancestor', current, head.sha], true)
92
+ if (currentAncestor.exitCode !== 0)
93
+ throw new Error('workspace restoration would overwrite divergent branch work')
94
+ const status = await git(['status', '--porcelain'])
95
+ if (status.stdout.trim()) throw new Error('workspace restoration requires a clean checkout')
96
+ await git(['update-ref', ref, head.sha, current])
97
+ const attached = (await git(['symbolic-ref', '--quiet', 'HEAD'], true)).stdout.trim()
98
+ if (attached === ref) await git(['reset', '--hard', head.sha])
99
+ }
100
+ }
101
+
102
+ /** Trusted exact-resource probe shared by hosted and local recovery. */
103
+ export async function inspectGitCandidate(input: {
104
+ exec: Exec
105
+ workspacePath: string
106
+ sha: string
107
+ branch: string
108
+ archiveRef: string
109
+ local?: boolean
110
+ replayPack?: string
111
+ }): Promise<boolean> {
112
+ if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(input.sha)) throw new Error('invalid candidate SHA')
113
+ const git = async (args: string[], optional = false) => {
114
+ const r = await input.exec(['git', ...args], { cwd: input.workspacePath })
115
+ if (r.exitCode !== 0 && !optional) throw new Error(`candidate probe failed: ${r.stderr}`)
116
+ return r
117
+ }
118
+ if ((await git(['cat-file', '-e', `${input.sha}^{commit}`], true)).exitCode === 0) return true
119
+ for (const ref of [`refs/heads/${input.branch}`, input.archiveRef]) {
120
+ const r = await git(
121
+ input.local
122
+ ? ['rev-parse', '--verify', '--quiet', ref]
123
+ : ['ls-remote', '--refs', 'origin', ref],
124
+ true,
125
+ )
126
+ if (r.exitCode !== 0) {
127
+ if (input.local && r.exitCode === 1) continue
128
+ throw new Error(`candidate ref inspection failed: ${r.stderr}`)
129
+ }
130
+ const actual = r.stdout.trim().split(/\s+/)[0]
131
+ if (actual !== input.sha) continue
132
+ if (!input.local)
133
+ await git(['fetch', '--no-tags', '--no-write-fetch-head', '--refmap=', 'origin', ref])
134
+ await git(['cat-file', '-e', `${input.sha}^{commit}`])
135
+ return true
136
+ }
137
+ if (!input.replayPack) return false
138
+ if (!/^[A-Za-z0-9+/=]+$/.test(input.replayPack)) throw new Error('invalid retained replay pack')
139
+ const temporary = await input.exec(['mktemp'], { cwd: input.workspacePath })
140
+ const path = temporary.stdout.trim()
141
+ if (temporary.exitCode !== 0 || !path) throw new Error('cannot stage replay pack')
142
+ try {
143
+ for (let offset = 0; offset < input.replayPack.length; offset += 16384) {
144
+ const r = await input.exec(
145
+ [
146
+ 'sh',
147
+ '-c',
148
+ 'printf %s "$1" >> "$2"',
149
+ 'replay-chunk',
150
+ input.replayPack.slice(offset, offset + 16384),
151
+ path,
152
+ ],
153
+ { cwd: input.workspacePath },
154
+ )
155
+ if (r.exitCode !== 0) throw new Error('cannot stage replay pack')
156
+ }
157
+ const r = await input.exec(
158
+ [
159
+ 'bash',
160
+ '-o',
161
+ 'pipefail',
162
+ '-c',
163
+ 'base64 -d "$1" | git unpack-objects -q',
164
+ 'restore-replay',
165
+ path,
166
+ ],
167
+ { cwd: input.workspacePath },
168
+ )
169
+ if (r.exitCode !== 0) throw new Error('cannot restore replay pack')
170
+ await git(['cat-file', '-e', `${input.sha}^{commit}`])
171
+ return true
172
+ } finally {
173
+ await input.exec(['rm', '-f', path], { cwd: input.workspacePath })
174
+ }
175
+ }
@@ -26,6 +26,7 @@ export interface CreateWorkspaceProviderOptions {
26
26
  storeToken?: string
27
27
  /** Host-derived effective routes; consumed only by the built-in sandbox. */
28
28
  accounts?: () => import('../../config/accounts').Accounts
29
+ executionAccounts?: () => import('../../config/accounts').Accounts
29
30
  runtimeReferences?: RuntimeReferencesSource
30
31
  /** Checkout-less seams for the built-in sandbox: the repository's HTTPS
31
32
  * origin, and a remote branch-head reader returning `undefined` for an
@@ -150,6 +151,7 @@ export async function createWorkspaceProvider(
150
151
  }
151
152
  : {}),
152
153
  ...(opts.accounts !== undefined ? { accounts: opts.accounts } : {}),
154
+ ...(opts.executionAccounts ? { executionAccounts: opts.executionAccounts } : {}),
153
155
  ...(opts.runtimeReferences !== undefined
154
156
  ? { runtimeReferences: opts.runtimeReferences }
155
157
  : {}),
@@ -55,6 +55,10 @@ async function pathExists(path: string): Promise<boolean> {
55
55
 
56
56
  export class FakeWorkspaceProvider implements WorkspaceProvider {
57
57
  readonly name = 'fake'
58
+ readonly preservation: NonNullable<WorkspacePublication['preserve']> = async () => ({
59
+ outcome: 'preserved',
60
+ heads: [],
61
+ })
58
62
  readonly orchestratorSandbox: OperatorSandboxExecution
59
63
  readonly sandboxPublication: WorkspacePublication
60
64
 
@@ -1,3 +1,4 @@
1
+ import { restoreGitCommits } from './commit-preservation'
1
2
  import { publishBranch, remoteBranchHead, refreshBranchCache } from '../forge/git-rewrite'
2
3
  /**
3
4
  * Workspace adapter: git worktrees (SPEC §3.2). The worktree is disposable
@@ -322,7 +323,27 @@ export class GitWorktreeProvider implements WorkspaceProvider {
322
323
  return { source: 'local', sha, remoteError }
323
324
  }
324
325
 
325
- async provision(opts: {
326
+ async provision(
327
+ opts: Parameters<WorkspaceProvider['provision']>[0],
328
+ ): Promise<WorkspaceProvisionResult> {
329
+ const handle = await this.provisionCore(opts)
330
+ if (opts.restoration?.length) {
331
+ await restoreGitCommits({
332
+ exec: this.exec,
333
+ workspacePath: handle.path,
334
+ branch: opts.branch,
335
+ heads: opts.restoration,
336
+ local: true,
337
+ })
338
+ handle.base = {
339
+ source: 'existing',
340
+ sha: await this.resolveCommit(handle.path, `refs/heads/${opts.branch}`),
341
+ }
342
+ }
343
+ return handle
344
+ }
345
+
346
+ private async provisionCore(opts: {
326
347
  repo: string
327
348
  baseBranch: string
328
349
  branch: string