@defrex/autobuild 1.16.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 (72) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +91 -7
  3. package/docs/answering-blocked-builds.md +34 -1
  4. package/docs/architecture.md +15 -1
  5. package/docs/configuration.md +33 -4
  6. package/docs/operator-api.md +15 -5
  7. package/docs/plugin-authoring.md +1 -0
  8. package/docs/remote-store-protocol.md +13 -3
  9. package/docs/remote-ticket-protocol.md +3 -1
  10. package/docs/restacking.md +41 -1
  11. package/docs/setup.md +15 -6
  12. package/docs/troubleshooting.md +39 -2
  13. package/docs/workspaces.md +2 -0
  14. package/package.json +3 -3
  15. package/packages/core/src/cli/accounts.ts +3 -0
  16. package/packages/core/src/cli/binary-direct.ts +14 -1
  17. package/packages/core/src/cli/binary.ts +10 -7
  18. package/packages/core/src/cli/dispatch.ts +503 -334
  19. package/packages/core/src/cli/secrets.ts +2 -0
  20. package/packages/core/src/config/schema.ts +15 -2
  21. package/packages/core/src/events/payloads.ts +8 -5
  22. package/packages/core/src/kernel/engine.ts +118 -0
  23. package/packages/core/src/kernel/reconcile-restack.ts +72 -9
  24. package/packages/core/src/kernel/tracked-dirt.ts +173 -0
  25. package/packages/core/src/ontology.ts +1 -0
  26. package/packages/core/src/operator/index.ts +10 -0
  27. package/packages/core/src/operator/merged-builds.ts +51 -0
  28. package/packages/core/src/operator/registry.ts +190 -101
  29. package/packages/core/src/operator/repo-argument.ts +43 -0
  30. package/packages/core/src/ports/tickets/grants.ts +2 -0
  31. package/packages/core/src/ports/tickets/remote.ts +2 -0
  32. package/packages/core/src/ports/types.ts +15 -0
  33. package/packages/core/src/ports/workspace/vercel-command-diagnostics.ts +144 -0
  34. package/packages/core/src/ports/workspace/vercel-sandbox.ts +576 -140
  35. package/packages/core/src/processes/build-child.ts +15 -1
  36. package/packages/core/src/processes/build-runner.ts +349 -20
  37. package/packages/core/src/processes/dispatcher.ts +28 -7
  38. package/packages/core/src/processes/harvest-child.ts +21 -1
  39. package/packages/core/src/processes/harvest-runner.ts +82 -35
  40. package/packages/core/src/processes/phase-failure-budget.ts +1 -0
  41. package/packages/core/src/processes/provisioning-progress.ts +23 -0
  42. package/packages/core/src/processes/repo-view.ts +25 -4
  43. package/packages/core/src/processes/workspace-preparation.ts +1 -0
  44. package/packages/core/src/store/adapter.ts +34 -5
  45. package/packages/core/src/store/build-feed.ts +148 -22
  46. package/packages/core/src/store/build-scope.ts +12 -3
  47. package/packages/core/src/store/caller-attribution.ts +170 -0
  48. package/packages/core/src/store/contract.ts +1038 -165
  49. package/packages/core/src/store/local/schema.ts +12 -1
  50. package/packages/core/src/store/local/store.ts +260 -25
  51. package/packages/core/src/store/memory.ts +156 -9
  52. package/packages/core/src/store/phase-session.ts +23 -3
  53. package/packages/core/src/store/reducer-value-shapes.json +3184 -3175
  54. package/packages/core/src/store/remote/client.ts +90 -11
  55. package/packages/core/src/store/remote/credentials.ts +2 -0
  56. package/packages/core/src/store/remote/index.ts +31 -5
  57. package/packages/core/src/store/remote/protocol.ts +45 -8
  58. package/packages/core/src/store/remote/released-clients.json +11 -0
  59. package/packages/core/src/store/remote/released-reducer-values/1.17.0.json +36367 -0
  60. package/packages/core/src/store/remote/released-reducer-values/index.ts +18 -16
  61. package/packages/core/src/store/session-handle.ts +12 -3
  62. package/packages/core/src/store/types.ts +48 -3
  63. package/packages/core/src/testing/reduced-read-budget.ts +222 -4
  64. package/skills/guide/SKILL.md +46 -8
  65. package/skills/guide/references/operator-api.md +15 -5
  66. package/skills/guide/references/plugin-authoring.md +1 -0
  67. package/skills/guide/references/remote-store-protocol.md +13 -3
  68. package/skills/guide/references/remote-ticket-protocol.md +3 -1
  69. package/skills/guide/references/restacking.md +41 -1
  70. package/skills/guide/references/setup.md +15 -6
  71. package/skills/operate/SKILL.md +24 -3
  72. package/templates/hosted-service/package.json +2 -2
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Caller attribution for hosted store requests.
3
+ *
4
+ * Every hosted store and ticket client sends one optional, bounded
5
+ * `x-autobuild-caller` header naming who made the request: a dispatcher
6
+ * (invocation, run, pass stage), a runner (build or harvest), or an operator
7
+ * client (CLI, MCP, web). The value comes from an ambient scope that process
8
+ * entries establish, so clients need no caller parameter threaded through
9
+ * every store opener.
10
+ *
11
+ * The header is diagnostic only. The server parses it as untrusted input,
12
+ * never uses it for authorization or routing, and treats a missing header as
13
+ * an unattributed request from an older client.
14
+ *
15
+ * This module deliberately imports nothing but `node:async_hooks`, so local
16
+ * import graphs that never reach a hosted store gain nothing from it.
17
+ */
18
+ import { AsyncLocalStorage } from 'node:async_hooks'
19
+
20
+ export const STORE_CALLER_HEADER = 'x-autobuild-caller'
21
+ export const STORE_CALLER_VALUE_MAX = 96
22
+ export const STORE_CALLER_HEADER_MAX = 512
23
+ const STORE_CALLER_PARSE_MAX = 1024
24
+
25
+ export type StoreCaller =
26
+ | { kind: 'dispatcher'; invocation?: string; run?: string; stage?: string }
27
+ | {
28
+ kind: 'runner'
29
+ runner: 'build' | 'harvest'
30
+ build?: string
31
+ harvest?: string
32
+ execution?: string
33
+ session?: string
34
+ phase?: string
35
+ command?: string
36
+ }
37
+ | { kind: 'operator'; client: 'cli' | 'mcp' | 'web'; command?: string }
38
+
39
+ /** Allowlisted keys, in wire order. */
40
+ const KEYS = [
41
+ 'kind',
42
+ 'client',
43
+ 'runner',
44
+ 'invocation',
45
+ 'run',
46
+ 'stage',
47
+ 'build',
48
+ 'harvest',
49
+ 'execution',
50
+ 'session',
51
+ 'phase',
52
+ 'command',
53
+ ] as const
54
+ const ALLOWED: ReadonlySet<string> = new Set(KEYS)
55
+
56
+ /** Restricts a value to a log-safe charset and bounds its length. The
57
+ * charset excludes whitespace, quotes, `;` and `=`, so a sanitized value can
58
+ * never break the header format or a JSON log line. */
59
+ export function sanitizeCallerValue(value: string): string {
60
+ return value.replace(/[^A-Za-z0-9._:@+/-]/g, '_').slice(0, STORE_CALLER_VALUE_MAX)
61
+ }
62
+
63
+ export function encodeStoreCaller(caller: StoreCaller): string {
64
+ const fields = caller as Record<string, unknown>
65
+ let out = ''
66
+ for (const key of KEYS) {
67
+ const raw = fields[key]
68
+ if (typeof raw !== 'string' || raw === '') continue
69
+ const pair = `${key}=${sanitizeCallerValue(raw)}`
70
+ const next = out === '' ? pair : `${out};${pair}`
71
+ if (next.length > STORE_CALLER_HEADER_MAX) break
72
+ out = next
73
+ }
74
+ return out
75
+ }
76
+
77
+ /** Server-side parse of an untrusted header value. Returns only allowlisted
78
+ * keys with sanitized values, or `null` when nothing usable survives. */
79
+ export function parseStoreCaller(raw: string | null | undefined): Record<string, string> | null {
80
+ if (raw == null) return null
81
+ const parsed: Record<string, string> = {}
82
+ let any = false
83
+ for (const part of raw.slice(0, STORE_CALLER_PARSE_MAX).split(';')) {
84
+ const eq = part.indexOf('=')
85
+ if (eq < 0) continue
86
+ const key = part.slice(0, eq).trim()
87
+ if (!ALLOWED.has(key) || Object.hasOwn(parsed, key)) continue
88
+ const value = sanitizeCallerValue(part.slice(eq + 1).trim())
89
+ if (value === '') continue
90
+ parsed[key] = value
91
+ any = true
92
+ }
93
+ return any ? parsed : null
94
+ }
95
+
96
+ const scope = new AsyncLocalStorage<StoreCaller>()
97
+
98
+ export const currentStoreCaller = (): StoreCaller | undefined => scope.getStore()
99
+
100
+ export function withStoreCaller<T>(caller: StoreCaller, work: () => T): T {
101
+ return scope.run(caller, work)
102
+ }
103
+
104
+ /** Names the dispatcher pass stage for requests made inside `work`. Outside a
105
+ * dispatcher scope this is a plain call. */
106
+ export function withCallerStage<T>(stage: string, work: () => T): T {
107
+ const current = scope.getStore()
108
+ if (current?.kind !== 'dispatcher') return work()
109
+ return scope.run({ ...current, stage }, work)
110
+ }
111
+
112
+ /** Enters a dispatcher run: keeps the hosted invocation id when there is one
113
+ * and clears any setup stage. */
114
+ export function withDispatcherRun<T>(run: string, work: () => T): T {
115
+ const current = scope.getStore()
116
+ const invocation = current?.kind === 'dispatcher' ? current.invocation : undefined
117
+ return scope.run({ kind: 'dispatcher', ...(invocation ? { invocation } : {}), run }, work)
118
+ }
119
+
120
+ /** The header to send: the explicit caller when given, else the ambient one,
121
+ * else nothing. */
122
+ export function storeCallerHeaders(explicit?: StoreCaller): Record<string, string> {
123
+ const caller = explicit ?? scope.getStore()
124
+ if (!caller) return {}
125
+ const value = encodeStoreCaller(caller)
126
+ return value === '' ? {} : { [STORE_CALLER_HEADER]: value }
127
+ }
128
+
129
+ const nonblank = (value: string | undefined): string | undefined =>
130
+ value != null && value.trim() !== '' ? value : undefined
131
+
132
+ /** Who a CLI invocation is, from its command and session environment. Only
133
+ * the command name is used, never its arguments. */
134
+ export function cliStoreCaller(
135
+ argv: readonly string[],
136
+ env: Readonly<Record<string, string | undefined>>,
137
+ ): StoreCaller {
138
+ const command = argv[0]
139
+ if (command === 'dispatch') {
140
+ return argv.includes('--viewer')
141
+ ? { kind: 'operator', client: 'cli', command: 'dispatch-viewer' }
142
+ : { kind: 'dispatcher' }
143
+ }
144
+ const session = nonblank(env.AB_SESSION)
145
+ const phase = nonblank(env.AB_PHASE)
146
+ const build = nonblank(env.AB_BUILD)
147
+ if (build) {
148
+ return {
149
+ kind: 'runner',
150
+ runner: 'build',
151
+ build,
152
+ ...(session ? { session } : {}),
153
+ ...(phase ? { phase } : {}),
154
+ ...(command ? { command } : {}),
155
+ }
156
+ }
157
+ const harvest = nonblank(env.AB_HARVEST)
158
+ if (harvest) {
159
+ return {
160
+ kind: 'runner',
161
+ runner: 'harvest',
162
+ harvest,
163
+ ...(session ? { session } : {}),
164
+ ...(phase ? { phase } : {}),
165
+ ...(command ? { command } : {}),
166
+ }
167
+ }
168
+ if (command === 'mcp') return { kind: 'operator', client: 'mcp', command: 'mcp' }
169
+ return { kind: 'operator', client: 'cli', ...(command ? { command } : {}) }
170
+ }