@defrex/autobuild 1.0.1 → 1.2.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 +10 -18
- package/SPEC.md +499 -238
- package/bin/ab-dispatch-generation.ts +11 -2
- package/docs/accounts.md +119 -0
- package/docs/agent-runtimes.md +4 -4
- package/docs/answering-blocked-builds.md +2 -2
- package/docs/architecture.md +262 -53
- package/docs/assets/headline-wide.png +0 -0
- package/docs/build-store.md +3 -41
- package/docs/builds.md +4 -4
- package/docs/cli.md +18 -4
- package/docs/configuration-overview.md +115 -4
- package/docs/configuration.md +488 -334
- package/docs/dispatcher-hosting.md +19 -34
- package/docs/dispatcher.md +1 -1
- package/docs/forges.md +5 -5
- package/docs/getting-started.md +2 -2
- package/docs/glossary.md +6 -6
- package/docs/intake.md +2 -0
- package/docs/integration-overview.md +15 -15
- package/docs/integration-tree.md +45 -38
- package/docs/integration.md +6 -6
- package/{skills/guide/references/landing-policy.md → docs/merge-policy.md} +40 -24
- package/docs/migration-1.0.md +17 -13
- package/docs/operating.md +41 -19
- package/docs/operator-api.md +180 -51
- package/docs/operator-surfaces.md +5 -18
- package/docs/overview.md +3 -3
- package/docs/plugin-authoring.md +21 -0
- package/docs/remote-store-protocol.md +242 -68
- package/docs/remote-ticket-protocol.md +30 -50
- package/docs/restacking.md +54 -15
- package/docs/seams.md +8 -8
- package/docs/setup.md +59 -119
- package/docs/skills.md +1 -1
- package/docs/spec-standard.md +6 -6
- package/docs/ticket-dependencies.md +7 -7
- package/docs/ticket-sources.md +18 -0
- package/docs/tickets.md +8 -2
- package/docs/troubleshooting.md +8 -12
- package/docs/upgrading.md +9 -4
- package/docs/verification.md +60 -14
- package/docs/workspaces.md +4 -4
- package/package.json +17 -6
- package/packages/core/src/accounts/chatgpt-oauth.ts +216 -0
- package/packages/core/src/accounts/chatgpt-state.ts +41 -0
- package/packages/core/src/accounts/local-credentials.ts +194 -0
- package/packages/core/src/accounts/local-inference.ts +106 -0
- package/packages/core/src/accounts/local-owner.ts +123 -0
- package/packages/core/src/accounts/sensitive.ts +2 -0
- package/packages/core/src/cli/accounts.ts +147 -0
- package/packages/core/src/cli/binary-direct.ts +13 -1
- package/packages/core/src/cli/binary-lifecycle.ts +65 -2
- package/packages/core/src/cli/binary.ts +3 -1
- package/packages/core/src/cli/build-control.ts +83 -28
- package/packages/core/src/cli/connect.ts +3 -3
- package/packages/core/src/cli/context.ts +81 -38
- package/packages/core/src/cli/dashboard/facts.ts +2 -1
- package/packages/core/src/cli/dashboard/integration-detail.ts +8 -8
- package/packages/core/src/cli/dashboard/integration-row.ts +40 -13
- package/packages/core/src/cli/dashboard/landing-display.ts +25 -0
- package/packages/core/src/cli/dashboard/model.ts +56 -33
- package/packages/core/src/cli/dashboard/poll.ts +9 -3
- package/packages/core/src/cli/dashboard/render.ts +59 -37
- package/packages/core/src/cli/dashboard/target-distance.ts +14 -0
- package/packages/core/src/cli/dashboard/tree.ts +3 -2
- package/packages/core/src/cli/dispatch-args.ts +1 -1
- package/packages/core/src/cli/dispatch-frontend.ts +6 -3
- package/packages/core/src/cli/dispatch-handoff.ts +125 -44
- package/packages/core/src/cli/dispatch-launch-contract.ts +187 -0
- package/packages/core/src/cli/dispatch-launcher.ts +54 -21
- package/packages/core/src/cli/dispatch-preflight.ts +6 -1
- package/packages/core/src/cli/dispatch.ts +106 -11
- package/packages/core/src/cli/env.ts +4 -0
- package/packages/core/src/cli/help.ts +46 -23
- package/packages/core/src/cli/init-readiness-remote.ts +22 -1
- package/packages/core/src/cli/init-readiness-shared.ts +5 -1
- package/packages/core/src/cli/init-validation.ts +160 -18
- package/packages/core/src/cli/main.ts +68 -8
- package/packages/core/src/cli/node-verification-provenance.ts +1 -97
- package/packages/core/src/cli/publication-settlement.ts +47 -21
- package/packages/core/src/cli/repository-status.ts +20 -9
- package/packages/core/src/cli/secrets.ts +173 -0
- package/packages/core/src/cli/status.ts +42 -5
- package/packages/core/src/cli/store-opening.ts +17 -2
- package/packages/core/src/cli/terminal.ts +22 -1
- package/packages/core/src/cli/terminals.ts +14 -10
- package/packages/core/src/cli/upgrade-agent.ts +9 -1
- package/packages/core/src/cli/upgrade-landing.ts +70 -30
- package/packages/core/src/cli/upgrade.ts +3 -1
- package/packages/core/src/cli/wait.ts +15 -7
- package/packages/core/src/cli/watch-lifetime.ts +111 -0
- package/packages/core/src/cli/watch.ts +635 -510
- package/packages/core/src/config/accounts.ts +101 -0
- package/packages/core/src/config/effective-artifact.fixture.ts +27 -0
- package/packages/core/src/config/effective-artifact.ts +13 -0
- package/packages/core/src/config/landing.ts +1 -1
- package/packages/core/src/config/live.ts +14 -1
- package/packages/core/src/config/load.ts +21 -1
- package/packages/core/src/config/pipeline-source.ts +28 -0
- package/packages/core/src/config/reference-metadata.ts +2 -0
- package/packages/core/src/config/reference.ts +22 -7
- package/packages/core/src/config/roles.ts +54 -3
- package/packages/core/src/config/schema.ts +146 -162
- package/packages/core/src/context-directory.ts +26 -0
- package/packages/core/src/distribution-packaging.ts +26 -5
- package/packages/core/src/events/catalog.ts +10 -2
- package/packages/core/src/events/compatibility.ts +2 -1
- package/packages/core/src/events/globs.ts +1 -1
- package/packages/core/src/events/payloads.ts +73 -1
- package/packages/core/src/events/policy.ts +7 -0
- package/packages/core/src/events/repository.ts +4 -4
- package/packages/core/src/events/sessions.ts +6 -6
- package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +574 -0
- package/packages/core/src/kernel/auto-merge.ts +2 -2
- package/packages/core/src/kernel/engine.ts +255 -26
- package/packages/core/src/kernel/generators/leaf-restack-log.ts +10 -0
- package/packages/core/src/kernel/integration-landing.ts +3 -3
- package/packages/core/src/kernel/integration-state.ts +27 -4
- package/packages/core/src/kernel/leaf-restack-state.ts +28 -3
- package/packages/core/src/kernel/log-index.ts +13 -1
- package/packages/core/src/kernel/node-landing.ts +8 -8
- package/packages/core/src/kernel/node-parent.ts +2 -2
- package/packages/core/src/kernel/node-restack-transaction.ts +1 -1
- package/packages/core/src/kernel/node-verification-context.ts +216 -0
- package/packages/core/src/kernel/phases.ts +1 -1
- package/packages/core/src/kernel/plan-verify-selection.ts +16 -0
- package/packages/core/src/kernel/publication-retry.ts +168 -0
- package/packages/core/src/kernel/reconcile-restack.ts +29 -1
- package/packages/core/src/kernel/reducer.ts +85 -5
- package/packages/core/src/kernel/target-distance.ts +45 -0
- package/packages/core/src/kernel/terminal-guard.ts +10 -1
- package/packages/core/src/kernel/verify-stage-state.ts +123 -0
- package/packages/core/src/kernel/verify-stages.ts +39 -0
- package/packages/core/src/ontology.ts +1 -0
- package/packages/core/src/operator/annotations.ts +6 -0
- package/packages/core/src/operator/control.ts +1 -1
- package/packages/core/src/operator/index.ts +3 -7
- package/packages/core/src/operator/landings.ts +5 -1
- package/packages/core/src/operator/presentation.ts +6 -1
- package/packages/core/src/operator/protocol.ts +6 -0
- package/packages/core/src/operator/query.ts +14 -18
- package/packages/core/src/operator/registry.ts +39 -7
- package/packages/core/src/operator/sandbox-host.ts +64 -40
- package/packages/core/src/operator/sandbox.ts +3 -0
- package/packages/core/src/operator/ticket-source-backend.ts +3 -12
- package/packages/core/src/operator/tickets.ts +25 -4
- package/packages/core/src/plugins/manifest.ts +2 -1
- package/packages/core/src/ports/forge/contract.ts +2 -2
- package/packages/core/src/ports/forge/git-rewrite.ts +25 -2
- package/packages/core/src/ports/forge/github-readiness.ts +2 -2
- package/packages/core/src/ports/forge/github.ts +26 -7
- package/packages/core/src/ports/forge/local-git.ts +4 -4
- package/packages/core/src/ports/forge/readiness.ts +1 -1
- package/packages/core/src/ports/forge-failure.ts +24 -0
- package/packages/core/src/ports/runner/account-env.ts +88 -0
- package/packages/core/src/ports/runner/chatgpt-provider.ts +85 -0
- package/packages/core/src/ports/runner/claude.ts +26 -2
- package/packages/core/src/ports/runner/codex.ts +63 -31
- package/packages/core/src/ports/runner/one-shot.ts +2 -0
- package/packages/core/src/ports/runner/provider-error.ts +5 -0
- package/packages/core/src/ports/runner/routing.ts +35 -4
- package/packages/core/src/ports/tickets/contract.ts +58 -0
- package/packages/core/src/ports/tickets/tree.ts +1 -1
- package/packages/core/src/ports/types.ts +28 -7
- package/packages/core/src/ports/workspace/account-policy.ts +72 -0
- package/packages/core/src/ports/workspace/build-execution.ts +9 -2
- package/packages/core/src/ports/workspace/builtin-capabilities.ts +1 -0
- package/packages/core/src/ports/workspace/create.ts +2 -0
- package/packages/core/src/ports/workspace/git-worktree.ts +6 -1
- package/packages/core/src/ports/workspace/phase-scratch.ts +4 -3
- package/packages/core/src/ports/workspace/provider-capabilities.ts +1 -0
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +182 -72
- package/packages/core/src/processes/build-child.ts +6 -8
- package/packages/core/src/processes/build-execution-state.ts +8 -5
- package/packages/core/src/processes/build-runner.ts +554 -99
- package/packages/core/src/processes/dispatcher.ts +209 -101
- package/packages/core/src/processes/execution-no-progress.ts +8 -1
- package/packages/core/src/processes/execution-settlement.ts +21 -3
- package/packages/core/src/processes/harvest-runner.ts +68 -8
- package/packages/core/src/processes/phase-failure-budget.ts +1 -0
- package/packages/core/src/processes/publication-loss.ts +8 -2
- package/packages/core/src/processes/publication-state.ts +42 -4
- package/packages/core/src/processes/repo-view.ts +14 -0
- package/packages/core/src/processes/verify-cycle-config.ts +96 -0
- package/packages/core/src/project-secrets.ts +105 -0
- package/packages/core/src/{integration → scenarios}/harness.ts +330 -317
- package/packages/core/src/secret-name.ts +41 -0
- package/packages/core/src/store/account-redaction.ts +156 -0
- package/packages/core/src/store/adapter.ts +1 -0
- package/packages/core/src/store/build-scope.ts +9 -0
- package/packages/core/src/store/contract.ts +116 -2
- package/packages/core/src/store/local/schema.ts +13 -0
- package/packages/core/src/store/local/store.ts +42 -0
- package/packages/core/src/store/memory.ts +47 -0
- package/packages/core/src/store/operator-presence.ts +21 -0
- package/packages/core/src/store/phase-session.ts +10 -0
- package/packages/core/src/store/reducer-value-shapes.json +5185 -4156
- package/packages/core/src/store/remote/client.ts +89 -48
- package/packages/core/src/store/remote/compatibility.ts +20 -0
- package/packages/core/src/store/remote/protocol.ts +4 -0
- package/packages/core/src/store/remote/version.ts +3 -1
- package/packages/core/src/store/retention.ts +2 -3
- package/packages/core/src/store/session-handle.ts +9 -0
- package/packages/core/src/store/streams/session-writer.ts +1 -1
- package/packages/core/src/store/types.ts +12 -2
- package/packages/core/src/testing/dispatch-read-budget.ts +122 -0
- package/packages/core/src/testing/index.ts +12 -1
- package/packages/core/src/testing/reduced-read-budget.ts +47 -0
- package/packages/core/src/testing/temp-directories.ts +58 -0
- package/skills/code-review/SKILL.md +23 -9
- package/skills/finalize/SKILL.md +15 -7
- package/skills/guide/SKILL.md +373 -148
- package/skills/guide/references/accounts.md +119 -0
- package/skills/guide/references/integration-tree.md +45 -38
- package/{docs/landing-policy.md → skills/guide/references/merge-policy.md} +40 -24
- package/skills/guide/references/migration-1.0.md +17 -13
- package/skills/guide/references/operator-api.md +180 -51
- package/skills/guide/references/plugin-authoring.md +21 -0
- package/skills/guide/references/remote-store-protocol.md +242 -68
- package/skills/guide/references/remote-ticket-protocol.md +30 -50
- package/skills/guide/references/restacking.md +54 -15
- package/skills/guide/references/setup.md +59 -119
- package/skills/guide/references/spec-standard.md +6 -6
- package/skills/guide/references/ticket-dependencies.md +7 -7
- package/skills/harvest/SKILL.md +3 -1
- package/skills/harvest-review/SKILL.md +3 -1
- package/skills/implement/SKILL.md +43 -17
- package/skills/operate/SKILL.md +118 -118
- package/skills/plan/SKILL.md +39 -13
- package/skills/plan-review/SKILL.md +15 -10
- package/skills/reconcile/SKILL.md +22 -3
- package/skills/spec/SKILL.md +20 -20
- package/skills/tickets/SKILL.md +12 -9
- package/templates/autobuild.toml +1 -1
- package/templates/hosted-service/package.json +2 -2
- package/docs/hosted-dispatcher-cutover-validation.md +0 -178
- package/docs/hosted-dispatcher.md +0 -275
- package/docs/hosted-service.md +0 -293
- package/docs/mcp.md +0 -149
- package/packages/core/src/orchestrator/approvals.ts +0 -115
- package/packages/core/src/orchestrator/conversation.ts +0 -167
- package/packages/core/src/orchestrator/model.ts +0 -86
- package/packages/core/src/orchestrator/prompt.ts +0 -63
- package/packages/core/src/orchestrator/tools.ts +0 -117
- package/packages/core/src/orchestrator/turn-runner.ts +0 -457
- package/packages/core/src/processes/orchestrator-tick.ts +0 -360
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { sensitiveAccountValues } from './sensitive'
|
|
2
|
+
import { randomBytes } from 'node:crypto'
|
|
3
|
+
import type { LocalCredentials } from './local-credentials'
|
|
4
|
+
|
|
5
|
+
/** A per-launch capability gates a fixed Responses endpoint. It never refreshes. */
|
|
6
|
+
export function localInference(
|
|
7
|
+
credentials: LocalCredentials,
|
|
8
|
+
name: string,
|
|
9
|
+
fetcher: (input: string, init?: RequestInit) => Promise<Response> = fetch,
|
|
10
|
+
) {
|
|
11
|
+
credentials.load(name)
|
|
12
|
+
const capability = randomBytes(32).toString('hex')
|
|
13
|
+
credentials.sensitive.add(capability)
|
|
14
|
+
sensitiveAccountValues.add(capability)
|
|
15
|
+
const server = Bun.serve({
|
|
16
|
+
hostname: '127.0.0.1',
|
|
17
|
+
port: 0,
|
|
18
|
+
maxRequestBodySize: 16000000,
|
|
19
|
+
async fetch(request) {
|
|
20
|
+
const url = new URL(request.url)
|
|
21
|
+
if (
|
|
22
|
+
request.method !== 'POST' ||
|
|
23
|
+
url.pathname !== '/v1/responses' ||
|
|
24
|
+
url.search ||
|
|
25
|
+
request.headers.get('authorization') !== `Bearer ${capability}`
|
|
26
|
+
)
|
|
27
|
+
return new Response(null, { status: 403 })
|
|
28
|
+
try {
|
|
29
|
+
const record = credentials.load(name)
|
|
30
|
+
if (!record || record.status === 'failed' || record.state.expiresAt <= Date.now())
|
|
31
|
+
return new Response(null, { status: 401 })
|
|
32
|
+
const body = await request.text()
|
|
33
|
+
if (body.length > 16000000) return new Response(null, { status: 413 })
|
|
34
|
+
const response = await fetcher('https://api.openai.com/v1/responses', {
|
|
35
|
+
method: 'POST',
|
|
36
|
+
redirect: 'error',
|
|
37
|
+
signal: request.signal,
|
|
38
|
+
headers: {
|
|
39
|
+
authorization: `Bearer ${record.state.accessToken}`,
|
|
40
|
+
'content-type': 'application/json',
|
|
41
|
+
},
|
|
42
|
+
body,
|
|
43
|
+
})
|
|
44
|
+
if (response.status === 401) {
|
|
45
|
+
await response.body?.cancel()
|
|
46
|
+
credentials.locked(name, () => {
|
|
47
|
+
const current = credentials.load(name)
|
|
48
|
+
if (
|
|
49
|
+
current?.generation === record.generation &&
|
|
50
|
+
current.state.accessToken === record.state.accessToken
|
|
51
|
+
)
|
|
52
|
+
credentials.save(name, { ...current, status: 'failed' })
|
|
53
|
+
})
|
|
54
|
+
return Response.json(
|
|
55
|
+
{
|
|
56
|
+
error: { code: 'invalid_token', message: 'ChatGPT account rejected; sign in again' },
|
|
57
|
+
},
|
|
58
|
+
{ status: 401 },
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
// Never relay a provider diagnostic containing the host bearer into the child.
|
|
62
|
+
const decoder = new TextDecoder()
|
|
63
|
+
let pending = ''
|
|
64
|
+
const needles = () =>
|
|
65
|
+
[...credentials.sensitive]
|
|
66
|
+
.flatMap((s) => [s, JSON.stringify(s).slice(1, -1)])
|
|
67
|
+
.sort((a, b) => b.length - a.length)
|
|
68
|
+
const clean = (value: string) => {
|
|
69
|
+
for (const secret of needles()) value = value.split(secret).join('[REDACTED]')
|
|
70
|
+
return value
|
|
71
|
+
}
|
|
72
|
+
const filtered = response.body?.pipeThrough(
|
|
73
|
+
new TransformStream<Uint8Array, Uint8Array>({
|
|
74
|
+
transform(chunk, controller) {
|
|
75
|
+
pending = clean(pending + decoder.decode(chunk, { stream: true }))
|
|
76
|
+
let retained = 0
|
|
77
|
+
for (const secret of needles())
|
|
78
|
+
for (let n = 1; n < secret.length && n <= pending.length; n++)
|
|
79
|
+
if (pending.endsWith(secret.slice(0, n))) retained = Math.max(retained, n)
|
|
80
|
+
const output = retained ? pending.slice(0, -retained) : pending
|
|
81
|
+
if (output) controller.enqueue(new TextEncoder().encode(output))
|
|
82
|
+
pending = retained ? pending.slice(-retained) : ''
|
|
83
|
+
},
|
|
84
|
+
flush(controller) {
|
|
85
|
+
controller.enqueue(new TextEncoder().encode(clean(pending + decoder.decode())))
|
|
86
|
+
},
|
|
87
|
+
}),
|
|
88
|
+
)
|
|
89
|
+
return new Response(filtered, {
|
|
90
|
+
status: response.status,
|
|
91
|
+
headers: {
|
|
92
|
+
'content-type': response.headers.get('content-type') ?? 'text/event-stream',
|
|
93
|
+
'cache-control': 'no-store',
|
|
94
|
+
},
|
|
95
|
+
})
|
|
96
|
+
} catch {
|
|
97
|
+
return new Response(null, { status: 401 })
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
return {
|
|
102
|
+
baseUrl: `http://127.0.0.1:${server.port}/v1`,
|
|
103
|
+
capability,
|
|
104
|
+
close: () => server.stop(true),
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import type { Accounts } from '../config/accounts'
|
|
3
|
+
import { refreshChatGPT } from './chatgpt-oauth'
|
|
4
|
+
import type { LocalCredentials } from './local-credentials'
|
|
5
|
+
import type { ChatGPTState } from './chatgpt-state'
|
|
6
|
+
|
|
7
|
+
/** One claimed dispatcher/validator owns renewal. Durable refreshing state is
|
|
8
|
+
* written before HTTP, and uncertain completion is terminal for that generation. */
|
|
9
|
+
export class LocalAccountOwner {
|
|
10
|
+
readonly id = randomUUID()
|
|
11
|
+
constructor(
|
|
12
|
+
readonly credentials: LocalCredentials,
|
|
13
|
+
private readonly refresh = refreshChatGPT,
|
|
14
|
+
) {}
|
|
15
|
+
claim(name: string): void {
|
|
16
|
+
this.credentials.locked(name, () => {
|
|
17
|
+
const r = this.credentials.load(name)
|
|
18
|
+
if (!r || (r.owner && r.owner !== this.id && !ownerProcessEnded(r.ownerPid)))
|
|
19
|
+
throw new Error('ChatGPT account already owned or missing')
|
|
20
|
+
if (r.status === 'refreshing') r.status = 'failed'
|
|
21
|
+
this.credentials.save(name, { ...r, owner: this.id, ownerPid: process.pid })
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
release(name: string): void {
|
|
25
|
+
this.credentials.locked(name, () => {
|
|
26
|
+
const r = this.credentials.load(name)
|
|
27
|
+
if (r?.owner === this.id) {
|
|
28
|
+
delete r.owner
|
|
29
|
+
delete r.ownerPid
|
|
30
|
+
this.credentials.save(name, r)
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
async tick(accounts: Accounts, now = Date.now()): Promise<Record<string, string>> {
|
|
35
|
+
const env: Record<string, string> = {}
|
|
36
|
+
for (const [name, descriptor] of Object.entries(accounts)) {
|
|
37
|
+
if (descriptor.kind !== 'chatgpt-subscription') continue
|
|
38
|
+
let r = this.credentials.load(name)
|
|
39
|
+
if (!r || r.owner !== this.id || JSON.stringify(r.descriptor) !== JSON.stringify(descriptor))
|
|
40
|
+
throw new Error('ChatGPT account ownership or descriptor changed; sign in again')
|
|
41
|
+
if (r.status !== 'ready') throw new Error('ChatGPT account failed; sign in again')
|
|
42
|
+
if (now >= Math.max(r.state.earliestRefreshAt ?? 0, r.state.expiresAt - 60000)) {
|
|
43
|
+
const claim = this.credentials.locked(name, () => {
|
|
44
|
+
const current = this.credentials.load(name)
|
|
45
|
+
if (
|
|
46
|
+
!current ||
|
|
47
|
+
current.owner !== this.id ||
|
|
48
|
+
current.generation !== r!.generation ||
|
|
49
|
+
current.status !== 'ready'
|
|
50
|
+
)
|
|
51
|
+
throw new Error('ChatGPT account changed')
|
|
52
|
+
this.credentials.save(name, { ...current, status: 'refreshing' })
|
|
53
|
+
return current
|
|
54
|
+
})
|
|
55
|
+
try {
|
|
56
|
+
const state: ChatGPTState = await this.refresh(claim.state, AbortSignal.timeout(15000))
|
|
57
|
+
this.credentials.locked(name, () => {
|
|
58
|
+
const current = this.credentials.load(name)
|
|
59
|
+
if (
|
|
60
|
+
current?.generation === claim.generation &&
|
|
61
|
+
current.owner === this.id &&
|
|
62
|
+
current.status === 'refreshing'
|
|
63
|
+
)
|
|
64
|
+
this.credentials.save(name, { ...current, state, status: 'ready' })
|
|
65
|
+
})
|
|
66
|
+
} catch {
|
|
67
|
+
this.credentials.locked(name, () => {
|
|
68
|
+
const current = this.credentials.load(name)
|
|
69
|
+
if (current?.generation === claim.generation && current.status === 'refreshing')
|
|
70
|
+
this.credentials.save(name, { ...current, status: 'failed' })
|
|
71
|
+
})
|
|
72
|
+
throw new Error('ChatGPT refresh failed; sign in again')
|
|
73
|
+
}
|
|
74
|
+
r = this.credentials.load(name)!
|
|
75
|
+
}
|
|
76
|
+
if (r.status !== 'ready' || r.state.expiresAt <= now)
|
|
77
|
+
throw new Error('ChatGPT account expired; sign in again')
|
|
78
|
+
env[descriptor.credentialEnv] = r.state.accessToken
|
|
79
|
+
}
|
|
80
|
+
return env
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Unknown ownership and permission errors remain live; only OS proof of exit
|
|
85
|
+
* permits reclaiming a record. An abandoned rotating attempt still fails closed. */
|
|
86
|
+
function ownerProcessEnded(pid: number | undefined): boolean {
|
|
87
|
+
if (!Number.isSafeInteger(pid) || pid! <= 0) return false
|
|
88
|
+
try {
|
|
89
|
+
process.kill(pid!, 0)
|
|
90
|
+
return false
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return (error as NodeJS.ErrnoException).code === 'ESRCH'
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Dispatcher renewal also recovers ready credentials after sleep or downtime.
|
|
97
|
+
* Access expiry is not refresh failure; only the owner may consume refresh state. */
|
|
98
|
+
export async function renewLocalAccounts(
|
|
99
|
+
owner: LocalAccountOwner,
|
|
100
|
+
accounts: Accounts,
|
|
101
|
+
env: Record<string, string | undefined>,
|
|
102
|
+
claimed: Set<string>,
|
|
103
|
+
now = Date.now(),
|
|
104
|
+
): Promise<void> {
|
|
105
|
+
for (const [name, descriptor] of Object.entries(accounts)) {
|
|
106
|
+
if (descriptor.kind !== 'chatgpt-subscription') continue
|
|
107
|
+
try {
|
|
108
|
+
const record = owner.credentials.load(name)
|
|
109
|
+
if (!record || record.status === 'failed') {
|
|
110
|
+
delete env[descriptor.credentialEnv]
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
owner.claim(name)
|
|
114
|
+
claimed.add(name)
|
|
115
|
+
// Only expose a still-valid access value during the rotating HTTP call.
|
|
116
|
+
if (record.state.expiresAt > now) env[descriptor.credentialEnv] = record.state.accessToken
|
|
117
|
+
else delete env[descriptor.credentialEnv]
|
|
118
|
+
Object.assign(env, await owner.tick({ [name]: descriptor }, now))
|
|
119
|
+
} catch {
|
|
120
|
+
delete env[descriptor.credentialEnv]
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
import { connectedProject } from './connect'
|
|
3
|
+
import { parseConfig } from '../config/load'
|
|
4
|
+
import { ProjectCredentials } from './project-credentials'
|
|
5
|
+
import { LocalCredentials } from '../accounts/local-credentials'
|
|
6
|
+
import { transaction, authorizationUrl, exchangeCode } from '../accounts/chatgpt-oauth'
|
|
7
|
+
import type { Exec } from '../ports/workspace/git-worktree'
|
|
8
|
+
import {
|
|
9
|
+
AUTOBUILD_VERSION,
|
|
10
|
+
AUTOBUILD_VERSION_HEADER,
|
|
11
|
+
REMOTE_STORE_PROTOCOL_VERSION,
|
|
12
|
+
REMOTE_STORE_PROTOCOL_VERSION_HEADER,
|
|
13
|
+
} from '../store/remote/version'
|
|
14
|
+
|
|
15
|
+
export async function abAccountsLogin(
|
|
16
|
+
name: string,
|
|
17
|
+
opts: {
|
|
18
|
+
cwd: string
|
|
19
|
+
env: Record<string, string | undefined>
|
|
20
|
+
exec: Exec
|
|
21
|
+
print: (s: string) => void
|
|
22
|
+
destination?: 'local' | 'project'
|
|
23
|
+
signal?: AbortSignal
|
|
24
|
+
browser?: (url: string) => Promise<void>
|
|
25
|
+
},
|
|
26
|
+
): Promise<void> {
|
|
27
|
+
const project = await connectedProject(opts)
|
|
28
|
+
const account = parseConfig(project.text).accounts[name]
|
|
29
|
+
if (account?.kind !== 'chatgpt-subscription')
|
|
30
|
+
throw new Error('Select a declared ChatGPT subscription account')
|
|
31
|
+
const destination = opts.destination ?? (project.service ? 'project' : 'local')
|
|
32
|
+
if (destination === 'project' && !project.service)
|
|
33
|
+
throw new Error('Project login requires ab connect')
|
|
34
|
+
const credential =
|
|
35
|
+
destination === 'project'
|
|
36
|
+
? new ProjectCredentials(opts.env).load(project.service!, project.repo)
|
|
37
|
+
: undefined
|
|
38
|
+
if (destination === 'project' && !credential)
|
|
39
|
+
throw new Error('Missing project credential; run ab connect')
|
|
40
|
+
const local = new LocalCredentials(join(project.checkout, '.autobuild'))
|
|
41
|
+
// Host identity is nonsecret. Project credentials themselves are never written here.
|
|
42
|
+
let hostId: string
|
|
43
|
+
if (destination === 'project') {
|
|
44
|
+
if (local.load(name))
|
|
45
|
+
throw new Error(
|
|
46
|
+
'A local credential already owns this account; use a separate account for the project',
|
|
47
|
+
)
|
|
48
|
+
const response = await fetch(
|
|
49
|
+
`${project.service}/secrets/v1/chatgpt-host?repo=${encodeURIComponent(project.repo)}`,
|
|
50
|
+
{
|
|
51
|
+
headers: {
|
|
52
|
+
authorization: `Bearer ${credential!.token}`,
|
|
53
|
+
[AUTOBUILD_VERSION_HEADER]: AUTOBUILD_VERSION,
|
|
54
|
+
[REMOTE_STORE_PROTOCOL_VERSION_HEADER]: REMOTE_STORE_PROTOCOL_VERSION,
|
|
55
|
+
},
|
|
56
|
+
signal: AbortSignal.timeout(15000),
|
|
57
|
+
redirect: 'error',
|
|
58
|
+
},
|
|
59
|
+
)
|
|
60
|
+
if (!response.ok) throw new Error('Project host identity unavailable')
|
|
61
|
+
const result = await response.json()
|
|
62
|
+
if (
|
|
63
|
+
result?.version !== 1 ||
|
|
64
|
+
typeof result.hostId !== 'string' ||
|
|
65
|
+
!/^urn:uuid:[0-9a-f-]{36}$/.test(result.hostId)
|
|
66
|
+
)
|
|
67
|
+
throw new Error('Invalid project host identity')
|
|
68
|
+
hostId = result.hostId
|
|
69
|
+
} else hostId = local.hostId()
|
|
70
|
+
const previous = destination === 'local' ? local.load(name)?.state : undefined
|
|
71
|
+
opts.print(
|
|
72
|
+
`Continue with ChatGPT: account ${JSON.stringify(name)}, destination ${destination === 'local' ? 'local dispatcher' : 'connected project'}.`,
|
|
73
|
+
)
|
|
74
|
+
const signal = AbortSignal.any([
|
|
75
|
+
AbortSignal.timeout(300000),
|
|
76
|
+
...(opts.signal ? [opts.signal] : []),
|
|
77
|
+
])
|
|
78
|
+
let resolveCallback!: (url: URL) => void
|
|
79
|
+
let rejectCallback!: (error: Error) => void
|
|
80
|
+
const received = new Promise<URL>((resolve, reject) => {
|
|
81
|
+
resolveCallback = resolve
|
|
82
|
+
rejectCallback = reject
|
|
83
|
+
})
|
|
84
|
+
void received.catch(() => {})
|
|
85
|
+
const server = Bun.serve({
|
|
86
|
+
hostname: '127.0.0.1',
|
|
87
|
+
port: 0,
|
|
88
|
+
fetch(request) {
|
|
89
|
+
const url = new URL(request.url)
|
|
90
|
+
if (url.pathname !== '/auth/callback') return new Response('Not found', { status: 404 })
|
|
91
|
+
resolveCallback(url)
|
|
92
|
+
return new Response('Return to the terminal to complete sign-in.', {
|
|
93
|
+
headers: { 'cache-control': 'no-store' },
|
|
94
|
+
})
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
const abort = () => rejectCallback(new Error('ChatGPT sign-in cancelled or timed out'))
|
|
98
|
+
signal.addEventListener('abort', abort, { once: true })
|
|
99
|
+
try {
|
|
100
|
+
if (signal.aborted) throw new Error('Sign-in cancelled')
|
|
101
|
+
const t = transaction(`http://127.0.0.1:${server.port}/auth/callback`, hostId, previous)
|
|
102
|
+
const url = authorizationUrl(t)
|
|
103
|
+
const opening = (async () => {
|
|
104
|
+
if (opts.browser) await opts.browser(url)
|
|
105
|
+
else {
|
|
106
|
+
const command = process.platform === 'darwin' ? 'open' : 'xdg-open'
|
|
107
|
+
const result = await opts.exec([command, url], { cwd: opts.cwd })
|
|
108
|
+
if (result.exitCode !== 0) throw new Error('Unable to open system browser')
|
|
109
|
+
}
|
|
110
|
+
})()
|
|
111
|
+
void opening.catch(() => {})
|
|
112
|
+
await Promise.race([opening, received.then(() => {})])
|
|
113
|
+
const state = await exchangeCode(t, await received, signal)
|
|
114
|
+
if (destination === 'local') local.install(name, account, state)
|
|
115
|
+
else {
|
|
116
|
+
const response = await fetch(`${project.service}/secrets/v1/chatgpt`, {
|
|
117
|
+
method: 'POST',
|
|
118
|
+
redirect: 'error',
|
|
119
|
+
signal,
|
|
120
|
+
headers: {
|
|
121
|
+
'content-type': 'application/json',
|
|
122
|
+
authorization: `Bearer ${credential!.token}`,
|
|
123
|
+
[AUTOBUILD_VERSION_HEADER]: AUTOBUILD_VERSION,
|
|
124
|
+
[REMOTE_STORE_PROTOCOL_VERSION_HEADER]: REMOTE_STORE_PROTOCOL_VERSION,
|
|
125
|
+
},
|
|
126
|
+
body: JSON.stringify({
|
|
127
|
+
version: 1,
|
|
128
|
+
repo: project.repo,
|
|
129
|
+
name: account.credentialEnv,
|
|
130
|
+
state,
|
|
131
|
+
}),
|
|
132
|
+
})
|
|
133
|
+
if (!response.ok) throw new Error('Project sign-in write failed')
|
|
134
|
+
const result = await response.json()
|
|
135
|
+
if (result?.version !== 1 || result?.ok !== true)
|
|
136
|
+
throw new Error('Project sign-in write not acknowledged')
|
|
137
|
+
}
|
|
138
|
+
opts.print(`ChatGPT account ${JSON.stringify(name)} saved for ${destination}.`)
|
|
139
|
+
} catch {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`ChatGPT sign-in failed for account ${JSON.stringify(name)}; run ab accounts login again`,
|
|
142
|
+
)
|
|
143
|
+
} finally {
|
|
144
|
+
signal.removeEventListener('abort', abort)
|
|
145
|
+
await server.stop(true)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -26,6 +26,7 @@ import { systemClock } from '../store/types'
|
|
|
26
26
|
import { resolveMainRepo } from './repo-state'
|
|
27
27
|
|
|
28
28
|
import {
|
|
29
|
+
installWatchTermination,
|
|
29
30
|
usesGenericSessionlessSigintHandler,
|
|
30
31
|
installSessionlessSigintHandler,
|
|
31
32
|
withDispatchTerminalRestore,
|
|
@@ -46,13 +47,21 @@ export async function runDirectBinary(
|
|
|
46
47
|
|
|
47
48
|
delete process.env.AB_DISPATCH_GENERATION
|
|
48
49
|
|
|
50
|
+
let watchTermination: ReturnType<typeof installWatchTermination> | undefined
|
|
49
51
|
const unscopedDeps = {
|
|
50
52
|
workspacePath: process.cwd(),
|
|
51
53
|
processEnv: process.env,
|
|
52
54
|
exec: spawnExec,
|
|
53
55
|
stdout: (line: string) => {
|
|
56
|
+
if (watchTermination) {
|
|
57
|
+
watchTermination.write(line)
|
|
58
|
+
return
|
|
59
|
+
}
|
|
54
60
|
if (command !== 'dispatch' || ownsDispatchPresentation()) console.log(line)
|
|
55
61
|
},
|
|
62
|
+
flushStdout: async () => {
|
|
63
|
+
await watchTermination?.flush()
|
|
64
|
+
},
|
|
56
65
|
stderr: (line: string) => {
|
|
57
66
|
if (command !== 'dispatch' || ownsDispatchPresentation()) console.error(line)
|
|
58
67
|
},
|
|
@@ -67,12 +76,13 @@ export async function runDirectBinary(
|
|
|
67
76
|
// cleanly at the next tick boundary (§15.6-C: in-flight leases expire and
|
|
68
77
|
// a future dispatch re-attaches).
|
|
69
78
|
const controller = new AbortController()
|
|
79
|
+
if (command === 'watch') watchTermination = installWatchTermination(controller)
|
|
70
80
|
const onSigint = (): void => controller.abort('SIGINT')
|
|
71
81
|
const ownsSigint = usesGenericSessionlessSigintHandler(command)
|
|
72
82
|
const terminal = processTerminal(process.stdout)
|
|
73
83
|
const removeSigint = installSessionlessSigintHandler(command, onSigint)
|
|
74
84
|
try {
|
|
75
|
-
|
|
85
|
+
const code = await withDispatchTerminalRestore(
|
|
76
86
|
command,
|
|
77
87
|
terminal,
|
|
78
88
|
() =>
|
|
@@ -92,7 +102,9 @@ export async function runDirectBinary(
|
|
|
92
102
|
process as unknown as TerminalRestoreProcess,
|
|
93
103
|
(signal) => controller.abort(signal),
|
|
94
104
|
)
|
|
105
|
+
return watchTermination ? await watchTermination.complete(code) : code
|
|
95
106
|
} finally {
|
|
107
|
+
watchTermination?.close()
|
|
96
108
|
removeSigint()
|
|
97
109
|
}
|
|
98
110
|
}
|
|
@@ -13,7 +13,7 @@ export interface SessionlessSigintBoundary {
|
|
|
13
13
|
removeListener(event: 'SIGINT', listener: () => void): unknown
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
/** Dispatch
|
|
16
|
+
/** Dispatch and watch keep their listeners through shutdown cleanup; ordinary
|
|
17
17
|
* sessionless commands preserve the historical one-shot cancellation. */
|
|
18
18
|
export function installSessionlessSigintHandler(
|
|
19
19
|
command: string | undefined,
|
|
@@ -21,7 +21,7 @@ export function installSessionlessSigintHandler(
|
|
|
21
21
|
boundary: SessionlessSigintBoundary = process,
|
|
22
22
|
): () => void {
|
|
23
23
|
if (!usesGenericSessionlessSigintHandler(command)) return () => {}
|
|
24
|
-
if (command === 'dispatch') boundary.on('SIGINT', onSigint)
|
|
24
|
+
if (command === 'dispatch' || command === 'watch') boundary.on('SIGINT', onSigint)
|
|
25
25
|
else boundary.once('SIGINT', onSigint)
|
|
26
26
|
return () => boundary.removeListener('SIGINT', onSigint)
|
|
27
27
|
}
|
|
@@ -44,3 +44,66 @@ export async function withDispatchTerminalRestore<T>(
|
|
|
44
44
|
hook?.close()
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
+
|
|
48
|
+
/** Watch alone catches termination long enough to withdraw advisory presence.
|
|
49
|
+
* SIGTERM/HUP are re-raised after cleanup to retain the shell's signal outcome. */
|
|
50
|
+
export function installWatchTermination(controller: AbortController): {
|
|
51
|
+
close: () => void
|
|
52
|
+
write: (line: string) => void
|
|
53
|
+
flush: () => Promise<void>
|
|
54
|
+
complete: (code: number) => Promise<number>
|
|
55
|
+
} {
|
|
56
|
+
const pending = new Set<Promise<void>>()
|
|
57
|
+
let broken = false
|
|
58
|
+
let signal: 'SIGTERM' | 'SIGHUP' | undefined
|
|
59
|
+
const stop = (value: 'SIGTERM' | 'SIGHUP'): void => {
|
|
60
|
+
signal ??= value
|
|
61
|
+
controller.abort(value)
|
|
62
|
+
}
|
|
63
|
+
const term = (): void => stop('SIGTERM')
|
|
64
|
+
const hup = (): void => stop('SIGHUP')
|
|
65
|
+
const pipe = (error: NodeJS.ErrnoException): void => {
|
|
66
|
+
if (error.code !== 'EPIPE') throw error
|
|
67
|
+
broken = true
|
|
68
|
+
controller.abort('EPIPE')
|
|
69
|
+
}
|
|
70
|
+
process.on('SIGTERM', term)
|
|
71
|
+
process.on('SIGHUP', hup)
|
|
72
|
+
process.stdout.on('error', pipe)
|
|
73
|
+
const close = (): void => {
|
|
74
|
+
process.removeListener('SIGTERM', term)
|
|
75
|
+
process.removeListener('SIGHUP', hup)
|
|
76
|
+
process.stdout.removeListener('error', pipe)
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
close,
|
|
80
|
+
write(line) {
|
|
81
|
+
if (broken || signal !== undefined) return
|
|
82
|
+
const written = new Promise<void>((resolve) => {
|
|
83
|
+
try {
|
|
84
|
+
process.stdout.write(`${line}\n`, (error) => {
|
|
85
|
+
if (error) pipe(error)
|
|
86
|
+
resolve()
|
|
87
|
+
})
|
|
88
|
+
} catch (error) {
|
|
89
|
+
pipe(error as NodeJS.ErrnoException)
|
|
90
|
+
resolve()
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
pending.add(written)
|
|
94
|
+
void written.then(() => pending.delete(written))
|
|
95
|
+
},
|
|
96
|
+
async flush() {
|
|
97
|
+
await Promise.all(pending)
|
|
98
|
+
},
|
|
99
|
+
async complete(code) {
|
|
100
|
+
close()
|
|
101
|
+
if (signal) {
|
|
102
|
+
process.kill(process.pid, signal)
|
|
103
|
+
// Wait for default signal delivery before the entry point can exit.
|
|
104
|
+
await new Promise<never>(() => {})
|
|
105
|
+
}
|
|
106
|
+
return broken ? 1 : code
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -3,7 +3,7 @@ import type { DashboardRendererResolver } from './dashboard/render'
|
|
|
3
3
|
import { loadDotEnv } from './dotenv'
|
|
4
4
|
import { parseDispatchArgs } from './dispatch-args'
|
|
5
5
|
import { launchDispatcher } from './dispatch-launcher'
|
|
6
|
-
import { DISPATCH_GENERATION_ENV } from './dispatch-launch-contract'
|
|
6
|
+
import { DISPATCH_GENERATION_ENV, DISPATCH_CONDITIONAL_ENV } from './dispatch-launch-contract'
|
|
7
7
|
export {
|
|
8
8
|
usesGenericSessionlessSigintHandler,
|
|
9
9
|
installSessionlessSigintHandler,
|
|
@@ -15,9 +15,11 @@ export async function runBinary(
|
|
|
15
15
|
): Promise<number> {
|
|
16
16
|
delete process.env.AB_DISPATCH_LAUNCH_CONTEXT
|
|
17
17
|
delete process.env[DISPATCH_GENERATION_ENV]
|
|
18
|
+
delete process.env[DISPATCH_CONDITIONAL_ENV]
|
|
18
19
|
if (argv[0] !== '--version') loadDotEnv(join(process.cwd(), '.env'), process.env)
|
|
19
20
|
delete process.env.AB_DISPATCH_LAUNCH_CONTEXT
|
|
20
21
|
delete process.env[DISPATCH_GENERATION_ENV]
|
|
22
|
+
delete process.env[DISPATCH_CONDITIONAL_ENV]
|
|
21
23
|
if (
|
|
22
24
|
argv[0] === 'dispatch' &&
|
|
23
25
|
!(argv.length === 2 && argv[1] === '--help') &&
|