@defrex/autobuild 1.1.0 → 1.3.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 +5 -13
- package/SPEC.md +249 -15
- package/bin/ab-dispatch-generation.ts +11 -2
- package/docs/accounts.md +130 -0
- package/docs/agent-runtimes.md +4 -4
- package/docs/answering-blocked-builds.md +4 -1
- package/docs/architecture.md +224 -12
- package/docs/build-store.md +3 -41
- package/docs/builds.md +2 -0
- package/docs/cli.md +14 -0
- package/docs/configuration-overview.md +117 -0
- package/docs/configuration.md +266 -141
- package/docs/dispatcher-hosting.md +18 -33
- package/docs/intake.md +2 -0
- package/docs/integration-tree.md +16 -5
- package/docs/merge-policy.md +1 -3
- package/docs/operating.md +1 -2
- package/docs/operator-api.md +144 -53
- package/docs/operator-surfaces.md +32 -20
- package/docs/overview.md +1 -1
- package/docs/plugin-authoring.md +27 -1
- package/docs/remote-store-protocol.md +259 -91
- package/docs/remote-ticket-protocol.md +29 -49
- package/docs/restacking.md +45 -0
- package/docs/seams.md +8 -8
- package/docs/setup.md +34 -129
- package/docs/spec-standard.md +4 -2
- package/docs/ticket-dependencies.md +8 -0
- package/docs/ticket-sources.md +18 -0
- package/docs/troubleshooting.md +7 -11
- package/docs/upgrading.md +1 -1
- package/docs/workspaces.md +4 -4
- package/package.json +14 -5
- 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.ts +3 -1
- package/packages/core/src/cli/connect.ts +3 -3
- package/packages/core/src/cli/dispatch-frontend.ts +1 -0
- 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-repository-feed.ts +107 -26
- package/packages/core/src/cli/dispatch.ts +135 -27
- package/packages/core/src/cli/dispatcher-liveness.ts +6 -1
- package/packages/core/src/cli/forge-clear.ts +30 -0
- package/packages/core/src/cli/help.ts +17 -0
- 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 +72 -0
- package/packages/core/src/cli/publication-settlement.ts +4 -1
- package/packages/core/src/cli/secrets.ts +173 -0
- package/packages/core/src/cli/store-opening.ts +11 -1
- package/packages/core/src/cli/upgrade-agent.ts +9 -1
- package/packages/core/src/cli/watch.ts +12 -14
- package/packages/core/src/config/accounts.ts +101 -0
- package/packages/core/src/config/live.ts +14 -1
- package/packages/core/src/config/roles.ts +54 -3
- package/packages/core/src/config/schema.ts +60 -0
- package/packages/core/src/events/catalog.ts +1 -0
- package/packages/core/src/events/compatibility.ts +2 -1
- package/packages/core/src/events/forge-degradation-policy.ts +11 -0
- package/packages/core/src/events/payloads.ts +13 -0
- package/packages/core/src/events/policy.ts +9 -0
- package/packages/core/src/events/repository.ts +75 -2
- package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +278 -0
- package/packages/core/src/kernel/change-presence.ts +74 -8
- package/packages/core/src/kernel/engine.ts +8 -0
- package/packages/core/src/kernel/generators/node-restack-log.ts +14 -0
- package/packages/core/src/kernel/node-polling.ts +40 -7
- package/packages/core/src/kernel/node-restack-demand.ts +74 -0
- package/packages/core/src/kernel/node-restack-state.ts +5 -1
- package/packages/core/src/kernel/node-restack-transaction.ts +33 -1
- package/packages/core/src/kernel/read-facts.ts +6 -0
- package/packages/core/src/kernel/reducer-registry.ts +3 -0
- package/packages/core/src/kernel/reducer.ts +4 -1
- package/packages/core/src/kernel/ticket-preparation.ts +66 -0
- package/packages/core/src/operator/annotations.ts +6 -0
- package/packages/core/src/operator/control.ts +20 -0
- package/packages/core/src/operator/index.ts +4 -0
- package/packages/core/src/operator/protocol.ts +6 -0
- package/packages/core/src/operator/registry.ts +42 -1
- package/packages/core/src/operator/tickets.ts +25 -4
- package/packages/core/src/plugins/manifest.ts +2 -1
- package/packages/core/src/ports/runner/account-env.ts +139 -0
- package/packages/core/src/ports/runner/chatgpt-provider.ts +85 -0
- package/packages/core/src/ports/runner/claude.ts +70 -42
- 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 +6 -1
- 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/provider-capabilities.ts +1 -0
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +125 -35
- package/packages/core/src/processes/build-runner.ts +32 -1
- package/packages/core/src/processes/dispatcher.ts +493 -121
- package/packages/core/src/processes/execution-no-progress.ts +1 -0
- 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/harvest.ts +11 -16
- package/packages/core/src/processes/repo-view.ts +3 -0
- package/packages/core/src/project-secrets.ts +105 -0
- package/packages/core/src/secret-name.ts +41 -0
- package/packages/core/src/store/account-redaction.ts +156 -0
- package/packages/core/src/store/artifact-meta.ts +7 -0
- package/packages/core/src/store/contract.ts +60 -1
- package/packages/core/src/store/memory.ts +4 -0
- package/packages/core/src/store/reducer-value-shapes.json +7389 -6647
- package/packages/core/src/store/reducer-value-validation.ts +106 -6
- package/packages/core/src/store/remote/client.ts +85 -50
- package/packages/core/src/store/remote/compatibility.ts +57 -0
- package/packages/core/src/store/remote/index.ts +6 -0
- package/packages/core/src/store/remote/protocol.ts +8 -1
- package/packages/core/src/store/remote/released-reducer-values/1.2.0.json +8129 -0
- package/packages/core/src/store/remote/version.ts +4 -0
- package/packages/core/src/store/repo-state-events.ts +4 -2
- package/packages/core/src/store/snapshots.ts +16 -3
- package/packages/core/src/store/types.ts +2 -0
- package/packages/core/src/testing/dispatch-read-budget.ts +266 -5
- package/packages/core/src/testing/index.ts +2 -1
- package/packages/core/src/testing/reduced-read-budget.ts +560 -5
- package/skills/guide/SKILL.md +164 -37
- package/skills/guide/references/accounts.md +130 -0
- package/skills/guide/references/integration-tree.md +16 -5
- package/skills/guide/references/merge-policy.md +1 -3
- package/skills/guide/references/operator-api.md +144 -53
- package/skills/guide/references/plugin-authoring.md +27 -1
- package/skills/guide/references/remote-store-protocol.md +259 -91
- package/skills/guide/references/remote-ticket-protocol.md +29 -49
- package/skills/guide/references/restacking.md +45 -0
- package/skills/guide/references/setup.md +34 -129
- package/skills/guide/references/spec-standard.md +4 -2
- package/skills/guide/references/ticket-dependencies.md +8 -0
- package/skills/harvest/SKILL.md +3 -1
- package/skills/harvest-review/SKILL.md +3 -1
- package/skills/operate/SKILL.md +26 -11
- package/skills/reconcile/SKILL.md +6 -0
- package/skills/spec/SKILL.md +1 -1
- package/skills/tickets/SKILL.md +10 -5
- package/templates/hosted-service/package.json +2 -2
- package/docs/hosted-dispatcher-cutover-validation.md +0 -178
- package/docs/hosted-dispatcher.md +0 -278
- package/docs/hosted-service.md +0 -304
- package/docs/mcp.md +0 -150
|
@@ -36,6 +36,7 @@ import { systemClock, type BuildStore, type Clock } from '../store/types'
|
|
|
36
36
|
import { TicketOperationError } from '../ports/tickets/operations'
|
|
37
37
|
import { OPERATOR_NOTES_REPO_KIND } from '../store/retention'
|
|
38
38
|
import {
|
|
39
|
+
clearForgeDegradation,
|
|
39
40
|
OperatorControlError,
|
|
40
41
|
setRepositorySetting,
|
|
41
42
|
toggleHarvestGate,
|
|
@@ -311,6 +312,18 @@ const ticketsListInput = z.strictObject({
|
|
|
311
312
|
.array(z.string().min(1))
|
|
312
313
|
.optional()
|
|
313
314
|
.describe('Narrow the list to tickets carrying every named label.'),
|
|
315
|
+
limit: z
|
|
316
|
+
.number()
|
|
317
|
+
.int()
|
|
318
|
+
.min(0)
|
|
319
|
+
.max(200)
|
|
320
|
+
.optional()
|
|
321
|
+
.describe('Return at most this many tickets; `total` still counts every match.'),
|
|
322
|
+
offset: z.number().int().min(0).optional().describe('Skip this many tickets first.'),
|
|
323
|
+
order: z
|
|
324
|
+
.enum(['oldest', 'newest'])
|
|
325
|
+
.optional()
|
|
326
|
+
.describe('Oldest first (the source order, the default) or newest first.'),
|
|
314
327
|
})
|
|
315
328
|
|
|
316
329
|
const ticketsCreateInput = ticketCreateRequestSchema.extend({ repo: repoField })
|
|
@@ -666,6 +679,25 @@ export const TOOLS: readonly ToolEntry[] = [
|
|
|
666
679
|
})
|
|
667
680
|
},
|
|
668
681
|
),
|
|
682
|
+
defineTool(
|
|
683
|
+
'repository.forge_clear',
|
|
684
|
+
'Explicitly clear erroneous forge cooldown state with an audited reason; does not prove provider recovery.',
|
|
685
|
+
z.strictObject({
|
|
686
|
+
repo: z.string().min(1).describe('Exact repository identity.'),
|
|
687
|
+
forge: z.string().trim().min(1).describe('Forge adapter name.'),
|
|
688
|
+
reason: z.string().trim().min(1).describe('Reason for the operator override.'),
|
|
689
|
+
}),
|
|
690
|
+
'{event}: the durable human-attributed recovery fact.',
|
|
691
|
+
async (raw, ctx) => {
|
|
692
|
+
const input = raw as { repo: string; forge: string; reason: string }
|
|
693
|
+
return clearForgeDegradation({
|
|
694
|
+
...input,
|
|
695
|
+
store: ctx.store,
|
|
696
|
+
user: attributed(ctx),
|
|
697
|
+
...(ctx.via !== undefined ? { via: ctx.via } : {}),
|
|
698
|
+
})
|
|
699
|
+
},
|
|
700
|
+
),
|
|
669
701
|
defineTool(
|
|
670
702
|
'repository.status',
|
|
671
703
|
'Report one repository’s durable dispatcher settings and journal state.',
|
|
@@ -783,7 +815,7 @@ export const TOOLS: readonly ToolEntry[] = [
|
|
|
783
815
|
'tickets.list',
|
|
784
816
|
'List the repository’s ticket grooming queue from the configured ticket source.',
|
|
785
817
|
ticketsListInput,
|
|
786
|
-
'{states, tickets, diagnostics, criteria, triageState, readyState}: the queue listing, the effective lifecycle names,
|
|
818
|
+
'{states, tickets, diagnostics, criteria, triageState, readyState, doneState, total}: the queue listing (one page when `limit` is set), the effective lifecycle names, the criteria actually applied, and the match count before paging.',
|
|
787
819
|
async (raw, ctx) => {
|
|
788
820
|
const input = raw as z.infer<typeof ticketsListInput>
|
|
789
821
|
return listOperatorTickets({
|
|
@@ -792,6 +824,15 @@ export const TOOLS: readonly ToolEntry[] = [
|
|
|
792
824
|
backend: ticketsOf(ctx),
|
|
793
825
|
...(input.state !== undefined ? { state: input.state } : {}),
|
|
794
826
|
...(input.labels !== undefined ? { labels: input.labels } : {}),
|
|
827
|
+
...(input.limit !== undefined
|
|
828
|
+
? {
|
|
829
|
+
page: {
|
|
830
|
+
limit: input.limit,
|
|
831
|
+
...(input.offset !== undefined ? { offset: input.offset } : {}),
|
|
832
|
+
...(input.order !== undefined ? { order: input.order } : {}),
|
|
833
|
+
},
|
|
834
|
+
}
|
|
835
|
+
: {}),
|
|
795
836
|
})
|
|
796
837
|
},
|
|
797
838
|
),
|
|
@@ -17,7 +17,14 @@ import {
|
|
|
17
17
|
requireTicket,
|
|
18
18
|
updateTicket,
|
|
19
19
|
} from '../ports/tickets/operations'
|
|
20
|
-
import type {
|
|
20
|
+
import type {
|
|
21
|
+
DependencyState,
|
|
22
|
+
Ticket,
|
|
23
|
+
TicketSource,
|
|
24
|
+
TicketUpdate,
|
|
25
|
+
TicketPage,
|
|
26
|
+
TicketPageListing,
|
|
27
|
+
} from '../ports/types'
|
|
21
28
|
import type { BuildStore } from '../store/types'
|
|
22
29
|
import { ticketLifecycle } from '../ports/tickets/lifecycle'
|
|
23
30
|
import { effectiveConfig } from './query'
|
|
@@ -47,8 +54,14 @@ export interface OperatorTicketQueue {
|
|
|
47
54
|
/** Effective repository lifecycle names; clients must not invent provider states. */
|
|
48
55
|
triageState: string
|
|
49
56
|
readyState: string
|
|
57
|
+
doneState: string
|
|
58
|
+
/** Tickets matching the criteria before `page` sliced them. */
|
|
59
|
+
total: number
|
|
50
60
|
}
|
|
51
61
|
|
|
62
|
+
/** A window over a listing. `newest` reverses the source's oldest-first order. */
|
|
63
|
+
export type OperatorTicketPage = TicketPage
|
|
64
|
+
|
|
52
65
|
export interface OperatorTicketBuild {
|
|
53
66
|
slug: string
|
|
54
67
|
status: string
|
|
@@ -91,9 +104,11 @@ export async function listOperatorTickets(opts: {
|
|
|
91
104
|
backend: OperatorTicketBackend
|
|
92
105
|
state?: string
|
|
93
106
|
labels?: string[]
|
|
107
|
+
page?: OperatorTicketPage
|
|
94
108
|
}): Promise<OperatorTicketQueue> {
|
|
95
109
|
const { context, source } = await openOperatorTickets(opts)
|
|
96
|
-
const
|
|
110
|
+
const lifecycle = ticketLifecycle(context.tickets)
|
|
111
|
+
const triageState = lifecycle.triage
|
|
97
112
|
const readyState = context.tickets.readyState
|
|
98
113
|
// The operator surface is a grooming queue. Unlike the CLI's ticket list,
|
|
99
114
|
// labels narrow the default state instead of replacing its lifecycle gate.
|
|
@@ -101,17 +116,23 @@ export async function listOperatorTickets(opts: {
|
|
|
101
116
|
state: opts.state ?? triageState,
|
|
102
117
|
...(opts.labels !== undefined ? { labels: opts.labels } : {}),
|
|
103
118
|
}
|
|
119
|
+
const nativePage = opts.page !== undefined && source.listPage !== undefined
|
|
104
120
|
const [listing, states] = await Promise.all([
|
|
105
|
-
source.listReady(criteria),
|
|
121
|
+
nativePage ? source.listPage!(criteria, opts.page!) : source.listReady(criteria),
|
|
106
122
|
opts.backend.statesFor(context, source),
|
|
107
123
|
])
|
|
124
|
+
const ordered =
|
|
125
|
+
!nativePage && opts.page?.order === 'newest' ? [...listing.tickets].reverse() : listing.tickets
|
|
126
|
+
const offset = opts.page?.offset ?? 0
|
|
108
127
|
return {
|
|
109
128
|
states,
|
|
110
|
-
tickets:
|
|
129
|
+
tickets: opts.page && !nativePage ? ordered.slice(offset, offset + opts.page.limit) : ordered,
|
|
111
130
|
diagnostics: listing.diagnostics,
|
|
112
131
|
criteria,
|
|
113
132
|
triageState,
|
|
114
133
|
readyState,
|
|
134
|
+
doneState: lifecycle.done,
|
|
135
|
+
total: nativePage ? (listing as TicketPageListing).total : listing.tickets.length,
|
|
115
136
|
}
|
|
116
137
|
}
|
|
117
138
|
|
|
@@ -15,7 +15,7 @@ import type {
|
|
|
15
15
|
} from '../ports/workspace/provider-capabilities'
|
|
16
16
|
|
|
17
17
|
/** Version of the in-process plugin contract exposed by `@defrex/autobuild/plugin-sdk`. */
|
|
18
|
-
export const PLUGIN_API_VERSION = '1.
|
|
18
|
+
export const PLUGIN_API_VERSION = '1.9.0' as const
|
|
19
19
|
|
|
20
20
|
/** Context supplied when a registered adapter or contract fixture is selected. */
|
|
21
21
|
export interface PluginFactoryContext<Config = Record<string, unknown>> {
|
|
@@ -59,6 +59,7 @@ export interface WorkspaceProviderPluginFactoryContext<Config = Record<string, u
|
|
|
59
59
|
/** Host-derived effective runtime routes (the referenced runtimes and their
|
|
60
60
|
* selection provenance). Absent at call sites that reference no runtimes
|
|
61
61
|
* (for example the MCP server), which never construct a workspace. */
|
|
62
|
+
accounts?: () => import('../config/accounts').Accounts
|
|
62
63
|
runtimeReferences?: RuntimeReferencesSource
|
|
63
64
|
/** Checkout-less seam: the repository's HTTPS origin. When absent the
|
|
64
65
|
* provider falls back to host `git` from `repoRoot` — the same default the
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { mkdtemp, writeFile, rm } from 'node:fs/promises'
|
|
2
|
+
import { tmpdir } from 'node:os'
|
|
3
|
+
import { join, resolve } from 'node:path'
|
|
4
|
+
import {
|
|
5
|
+
accountRemedy,
|
|
6
|
+
accountPlaceholder,
|
|
7
|
+
kimiEndpoint,
|
|
8
|
+
type SelectedAccount,
|
|
9
|
+
} from '../../config/accounts'
|
|
10
|
+
|
|
11
|
+
const ROUTING_KEYS = [
|
|
12
|
+
'ANTHROPIC_CUSTOM_HEADERS',
|
|
13
|
+
'CLAUDE_CODE_OAUTH_TOKEN',
|
|
14
|
+
'ANTHROPIC_API_KEY',
|
|
15
|
+
'ANTHROPIC_AUTH_TOKEN',
|
|
16
|
+
'ANTHROPIC_DEFAULT_FABLE_MODEL',
|
|
17
|
+
'ANTHROPIC_SMALL_FAST_MODEL',
|
|
18
|
+
'ANTHROPIC_BASE_URL',
|
|
19
|
+
'ANTHROPIC_MODEL',
|
|
20
|
+
'ANTHROPIC_DEFAULT_HAIKU_MODEL',
|
|
21
|
+
'ANTHROPIC_DEFAULT_SONNET_MODEL',
|
|
22
|
+
'ANTHROPIC_DEFAULT_OPUS_MODEL',
|
|
23
|
+
'CLAUDE_CODE_SUBAGENT_MODEL',
|
|
24
|
+
'CLAUDE_CODE_USE_BEDROCK',
|
|
25
|
+
'CLAUDE_CODE_USE_VERTEX',
|
|
26
|
+
'CLAUDE_CODE_USE_FOUNDRY',
|
|
27
|
+
] as const
|
|
28
|
+
|
|
29
|
+
/** Existing environment-owned launch metadata also covers a runner that
|
|
30
|
+
* started before its first account was declared by a live configuration reload. */
|
|
31
|
+
function usesPlaceholders(env: Readonly<Record<string, string>>): boolean {
|
|
32
|
+
if (env.AB_ACCOUNT_PLACEHOLDERS === '1') return true
|
|
33
|
+
return [env.AB_BUILD_RUNNER_OPTIONS, env.AB_HARVEST_RUNNER_OPTIONS].some((raw) => {
|
|
34
|
+
if (!raw) return false
|
|
35
|
+
try {
|
|
36
|
+
const launch = JSON.parse(raw) as { supervision?: { kind?: unknown } } | null
|
|
37
|
+
return launch?.supervision?.kind === 'environment'
|
|
38
|
+
} catch {
|
|
39
|
+
return false
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Apply after ambient merging. Inputs and process.env remain untouched. */
|
|
45
|
+
export function accountEnvironment(
|
|
46
|
+
ambient: Readonly<Record<string, string>>,
|
|
47
|
+
account?: SelectedAccount,
|
|
48
|
+
model?: string,
|
|
49
|
+
): Record<string, string> {
|
|
50
|
+
if (account === undefined) return { ...ambient }
|
|
51
|
+
const env = { ...ambient }
|
|
52
|
+
const remote = usesPlaceholders(env)
|
|
53
|
+
const originalCredential = env[account.descriptor.credentialEnv]
|
|
54
|
+
const credential = remote ? accountPlaceholder(account.name) : originalCredential
|
|
55
|
+
if (remote && originalCredential)
|
|
56
|
+
for (const [key, value] of Object.entries(env))
|
|
57
|
+
if (value.includes(originalCredential)) delete env[key]
|
|
58
|
+
for (const key of ROUTING_KEYS) delete env[key]
|
|
59
|
+
if (remote && !ROUTING_KEYS.some((key) => key === account.descriptor.credentialEnv))
|
|
60
|
+
env[account.descriptor.credentialEnv] = credential!
|
|
61
|
+
if (account.descriptor.kind === 'kimi-code') {
|
|
62
|
+
if (!credential) throw new Error(`Missing credential: ${accountRemedy(account)}`)
|
|
63
|
+
const selectedModel = model ?? 'kimi-for-coding'
|
|
64
|
+
Object.assign(env, {
|
|
65
|
+
ANTHROPIC_API_KEY: credential,
|
|
66
|
+
ANTHROPIC_BASE_URL: kimiEndpoint(account.descriptor.region).baseUrl,
|
|
67
|
+
ANTHROPIC_MODEL: selectedModel,
|
|
68
|
+
ANTHROPIC_DEFAULT_FABLE_MODEL: selectedModel,
|
|
69
|
+
ANTHROPIC_DEFAULT_HAIKU_MODEL: selectedModel,
|
|
70
|
+
ANTHROPIC_DEFAULT_SONNET_MODEL: selectedModel,
|
|
71
|
+
ANTHROPIC_DEFAULT_OPUS_MODEL: selectedModel,
|
|
72
|
+
CLAUDE_CODE_SUBAGENT_MODEL: selectedModel,
|
|
73
|
+
})
|
|
74
|
+
} else if (credential) env.CLAUDE_CODE_OAUTH_TOKEN = credential
|
|
75
|
+
else if (env.AB_WORKSPACE_PROVIDER === 'vercel-sandbox')
|
|
76
|
+
throw new Error(`Missing credential: ${accountRemedy(account)}`)
|
|
77
|
+
return env
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** CLI settings outrank user/project settings.env. Empty competing values also
|
|
81
|
+
* override saved values, without editing the user's files. */
|
|
82
|
+
export async function withAccountSettings<T>(
|
|
83
|
+
env: Record<string, string>,
|
|
84
|
+
account: SelectedAccount | undefined,
|
|
85
|
+
invoke: (args: string[]) => Promise<T>,
|
|
86
|
+
files: {
|
|
87
|
+
mkdtemp: (prefix: string) => Promise<string>
|
|
88
|
+
writeFile: typeof writeFile
|
|
89
|
+
rm: typeof rm
|
|
90
|
+
} = { mkdtemp, writeFile, rm },
|
|
91
|
+
): Promise<T> {
|
|
92
|
+
if (account === undefined) return await invoke([])
|
|
93
|
+
let directory: string | undefined
|
|
94
|
+
try {
|
|
95
|
+
// mkdtemp creates the directory with mode 0700 from the outset.
|
|
96
|
+
directory = await files.mkdtemp(join(resolve(tmpdir()), 'autobuild-account-'))
|
|
97
|
+
const path = join(directory, 'settings.json')
|
|
98
|
+
await files.writeFile(
|
|
99
|
+
path,
|
|
100
|
+
JSON.stringify({
|
|
101
|
+
apiKeyHelper: '',
|
|
102
|
+
env: Object.fromEntries(ROUTING_KEYS.map((key) => [key, env[key] ?? ''])),
|
|
103
|
+
}),
|
|
104
|
+
{ flag: 'wx', mode: 0o600 },
|
|
105
|
+
)
|
|
106
|
+
} catch {
|
|
107
|
+
if (directory !== undefined) {
|
|
108
|
+
try {
|
|
109
|
+
await files.rm(directory, { recursive: true, force: true })
|
|
110
|
+
} catch {
|
|
111
|
+
throw new Error('Account settings setup and cleanup failed')
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
throw new Error('Account settings setup failed')
|
|
115
|
+
}
|
|
116
|
+
let outcome: { value: T } | { error: unknown }
|
|
117
|
+
let cleanupFailed = false
|
|
118
|
+
try {
|
|
119
|
+
outcome = { value: await invoke(['--settings', join(directory, 'settings.json')]) }
|
|
120
|
+
} catch (error) {
|
|
121
|
+
outcome = { error }
|
|
122
|
+
} finally {
|
|
123
|
+
try {
|
|
124
|
+
await files.rm(directory, { recursive: true, force: true })
|
|
125
|
+
} catch {
|
|
126
|
+
cleanupFailed = true
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (cleanupFailed) {
|
|
130
|
+
throw new Error(
|
|
131
|
+
'error' in outcome
|
|
132
|
+
? 'Account invocation failed and settings cleanup failed'
|
|
133
|
+
: 'Account settings cleanup failed',
|
|
134
|
+
'error' in outcome ? { cause: outcome.error } : undefined,
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
if ('error' in outcome) throw outcome.error
|
|
138
|
+
return outcome.value
|
|
139
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { sensitiveAccountValues } from '../../accounts/sensitive'
|
|
2
|
+
import { LocalCredentials } from '../../accounts/local-credentials'
|
|
3
|
+
import { localInference } from '../../accounts/local-inference'
|
|
4
|
+
import { resolveMainRepo } from '../../cli/main-checkout'
|
|
5
|
+
import { spawnExec } from '../workspace/git-worktree'
|
|
6
|
+
import { mkdirSync, lstatSync } from 'node:fs'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
import type { SelectedAccount } from '../../config/accounts'
|
|
9
|
+
import { accountPlaceholder } from '../../config/accounts'
|
|
10
|
+
|
|
11
|
+
/** A selected account owns provider routing even when callers supply config flags. */
|
|
12
|
+
export async function chatgptProvider(
|
|
13
|
+
account: SelectedAccount | undefined,
|
|
14
|
+
env: Record<string, string>,
|
|
15
|
+
cwd: string,
|
|
16
|
+
): Promise<{ env: Record<string, string>; args: string[]; close: () => Promise<void> }> {
|
|
17
|
+
if (account?.descriptor.kind !== 'chatgpt-subscription')
|
|
18
|
+
return { env, args: [], close: async () => {} }
|
|
19
|
+
const originalCredential = env[account.descriptor.credentialEnv]
|
|
20
|
+
if (originalCredential && originalCredential !== accountPlaceholder(account.name))
|
|
21
|
+
sensitiveAccountValues.add(originalCredential)
|
|
22
|
+
const selected = { ...env }
|
|
23
|
+
for (const key of [
|
|
24
|
+
'OPENAI_API_KEY',
|
|
25
|
+
'OPENAI_BASE_URL',
|
|
26
|
+
'OPENAI_ORG_ID',
|
|
27
|
+
'OPENAI_ORGANIZATION',
|
|
28
|
+
'OPENAI_PROJECT_ID',
|
|
29
|
+
'CODEX_API_KEY',
|
|
30
|
+
account.descriptor.credentialEnv,
|
|
31
|
+
])
|
|
32
|
+
delete selected[key]
|
|
33
|
+
const scratch = join(cwd, '.ab')
|
|
34
|
+
mkdirSync(scratch, { recursive: true, mode: 0o700 })
|
|
35
|
+
const scratchInfo = lstatSync(scratch)
|
|
36
|
+
if (!scratchInfo.isDirectory() || scratchInfo.isSymbolicLink())
|
|
37
|
+
throw new Error('Unsafe isolated Codex state directory')
|
|
38
|
+
const home = join(scratch, 'codex-chatgpt')
|
|
39
|
+
mkdirSync(home, { recursive: true, mode: 0o700 })
|
|
40
|
+
const homeInfo = lstatSync(home)
|
|
41
|
+
if (
|
|
42
|
+
!homeInfo.isDirectory() ||
|
|
43
|
+
homeInfo.isSymbolicLink() ||
|
|
44
|
+
(homeInfo.mode & 0o777) !== 0o700 ||
|
|
45
|
+
homeInfo.uid !== process.getuid?.()
|
|
46
|
+
)
|
|
47
|
+
throw new Error('Unsafe isolated Codex state directory')
|
|
48
|
+
const remote = env.AB_ACCOUNT_PLACEHOLDERS === '1'
|
|
49
|
+
const transport = remote
|
|
50
|
+
? undefined
|
|
51
|
+
: localInference(
|
|
52
|
+
new LocalCredentials(join(await resolveMainRepo(cwd, spawnExec), '.autobuild')),
|
|
53
|
+
account.name,
|
|
54
|
+
)
|
|
55
|
+
const base = remote ? 'https://api.openai.com/v1' : transport!.baseUrl
|
|
56
|
+
const credential = remote ? accountPlaceholder(account.name) : transport!.capability
|
|
57
|
+
if (!base || !credential || (!remote && !/^http:\/\/127\.0\.0\.1:\d+\/v1$/.test(base)))
|
|
58
|
+
throw new Error(
|
|
59
|
+
`ChatGPT account ${JSON.stringify(account.name)} requires its dispatcher inference transport; sign in again`,
|
|
60
|
+
)
|
|
61
|
+
for (const [key, value] of Object.entries(selected))
|
|
62
|
+
if ([...sensitiveAccountValues].some((secret) => value.includes(secret))) delete selected[key]
|
|
63
|
+
selected.CODEX_HOME = home
|
|
64
|
+
selected.AUTOBUILD_CHATGPT_BEARER = credential
|
|
65
|
+
const provider = 'model_providers.autobuild_chatgpt.'
|
|
66
|
+
const settings: Record<string, string | boolean> = {
|
|
67
|
+
model_provider: 'autobuild_chatgpt',
|
|
68
|
+
[`${provider}name`]: 'ChatGPT plan',
|
|
69
|
+
[`${provider}base_url`]: base,
|
|
70
|
+
[`${provider}env_key`]: 'AUTOBUILD_CHATGPT_BEARER',
|
|
71
|
+
[`${provider}wire_api`]: 'responses',
|
|
72
|
+
[`${provider}requires_openai_auth`]: false,
|
|
73
|
+
[`${provider}supports_websockets`]: false,
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
env: selected,
|
|
77
|
+
close: async () => {
|
|
78
|
+
await transport?.close()
|
|
79
|
+
},
|
|
80
|
+
args: [
|
|
81
|
+
'--ignore-user-config',
|
|
82
|
+
...Object.entries(settings).flatMap(([k, v]) => ['-c', `${k}=${JSON.stringify(v)}`]),
|
|
83
|
+
],
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { accountRemedy } from '../../config/accounts'
|
|
2
|
+
import { accountEnvironment, withAccountSettings } from './account-env'
|
|
1
3
|
import { hostname } from 'node:os'
|
|
2
4
|
import {
|
|
3
5
|
guardNativeHandle,
|
|
@@ -228,19 +230,23 @@ export class ClaudeAgentRunner implements AgentRunner, OneShotCompletion {
|
|
|
228
230
|
* resumable session persistence. With no built-in or MCP tools, the single
|
|
229
231
|
* print-mode prompt cannot enter a tool-result loop. */
|
|
230
232
|
async complete(input: OneShotCompletionInput): Promise<OneShotCompletionResult> {
|
|
233
|
+
const env = accountEnvironment(sessionEnv(input.env), input.account, input.model)
|
|
231
234
|
const args = this.baseArgs()
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
235
|
+
return await withAccountSettings(env, input.account, async (settingsArgs) => {
|
|
236
|
+
args.push(...settingsArgs)
|
|
237
|
+
args.push('--tools', '', '--disallowedTools', 'mcp__*', '--no-session-persistence')
|
|
238
|
+
if (input.model !== undefined) args.push(CLAUDE_MODEL_ARG, input.model)
|
|
239
|
+
args.push(...(input.args ?? []), CLAUDE_PROMPT_BOUNDARY, input.prompt)
|
|
240
|
+
|
|
241
|
+
const turn = await this.runPrompt({
|
|
242
|
+
args,
|
|
243
|
+
cwd: input.cwd,
|
|
244
|
+
env,
|
|
245
|
+
...(input.signal !== undefined ? { signal: input.signal } : {}),
|
|
246
|
+
})
|
|
247
|
+
if (turn.failure !== undefined) throw new Error(turn.failure.message)
|
|
248
|
+
return { text: turn.text }
|
|
241
249
|
})
|
|
242
|
-
if (turn.failure !== undefined) throw new Error(turn.failure.message)
|
|
243
|
-
return { text: turn.text }
|
|
244
250
|
}
|
|
245
251
|
|
|
246
252
|
async start(
|
|
@@ -382,6 +388,24 @@ export class ClaudeAgentRunner implements AgentRunner, OneShotCompletion {
|
|
|
382
388
|
}
|
|
383
389
|
|
|
384
390
|
private async runTurn(
|
|
391
|
+
prompt: string,
|
|
392
|
+
opts: AgentStartOpts,
|
|
393
|
+
session: { sessionId: string } | { resume: string },
|
|
394
|
+
signal = opts.signal,
|
|
395
|
+
): Promise<ClaudeTurn> {
|
|
396
|
+
let turn: ClaudeTurn
|
|
397
|
+
try {
|
|
398
|
+
turn = await this.runAuthenticatedTurn(prompt, opts, session, signal)
|
|
399
|
+
} catch (error) {
|
|
400
|
+
if (!opts.account) throw error
|
|
401
|
+
turn = launchFailure(error)
|
|
402
|
+
}
|
|
403
|
+
if (opts.account && turn.failure?.cause === 'credentials')
|
|
404
|
+
turn.failure.message += `; ${accountRemedy(opts.account)}`
|
|
405
|
+
return turn
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private async runAuthenticatedTurn(
|
|
385
409
|
prompt: string,
|
|
386
410
|
opts: AgentStartOpts,
|
|
387
411
|
session: { sessionId: string } | { resume: string },
|
|
@@ -418,37 +442,41 @@ export class ClaudeAgentRunner implements AgentRunner, OneShotCompletion {
|
|
|
418
442
|
if (opts.stream !== undefined) args.push(CLAUDE_INCLUDE_PARTIAL_ARG)
|
|
419
443
|
args.push(...(opts.args ?? []), CLAUDE_PROMPT_BOUNDARY, prompt)
|
|
420
444
|
|
|
421
|
-
const
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
if (
|
|
431
|
-
return this.
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
445
|
+
const env = accountEnvironment(sessionEnv(opts.env), opts.account, opts.model)
|
|
446
|
+
return await withAccountSettings(env, opts.account, async (settingsArgs) => {
|
|
447
|
+
args.unshift(...settingsArgs)
|
|
448
|
+
const invocation: ClaudeCliInvocation = {
|
|
449
|
+
args,
|
|
450
|
+
cwd: opts.workspacePath,
|
|
451
|
+
env,
|
|
452
|
+
...(signal !== undefined ? { signal } : {}),
|
|
453
|
+
}
|
|
454
|
+
if (opts.stream === undefined && opts.onNativeSession === undefined)
|
|
455
|
+
return this.runPrompt(invocation)
|
|
456
|
+
if (opts.stream === undefined) {
|
|
457
|
+
if (this.runCliStream !== undefined)
|
|
458
|
+
return this.runStreamingPrompt(invocation, undefined, observe)
|
|
459
|
+
const turn = await this.runPrompt(invocation)
|
|
460
|
+
for (const event of turn.events) await observeEvent(event)
|
|
461
|
+
return turn
|
|
462
|
+
}
|
|
436
463
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
464
|
+
// Streaming turn: translate while the CLI runs. Without an injected
|
|
465
|
+
// streaming boundary (offline fakes), the buffered path still translates
|
|
466
|
+
// after completion — degraded latency, same content.
|
|
467
|
+
const emitter = opts.stream
|
|
468
|
+
emitter.append([promptPart(prompt), startPart(crypto.randomUUID())])
|
|
469
|
+
let turn: ClaudeTurn
|
|
470
|
+
if (this.runCliStream !== undefined) {
|
|
471
|
+
turn = await this.runStreamingPrompt(invocation, emitter, observe)
|
|
472
|
+
} else {
|
|
473
|
+
turn = await this.runPrompt(invocation)
|
|
474
|
+
for (const event of turn.events) await observeEvent(event)
|
|
475
|
+
translateBufferedClaudeTurn(turn, (parts) => emitter?.append(parts))
|
|
476
|
+
}
|
|
477
|
+
this.emitTurnEnd(turn, emitter, signal)
|
|
478
|
+
return turn
|
|
479
|
+
})
|
|
452
480
|
}
|
|
453
481
|
|
|
454
482
|
/** Completed-turn, failed-turn, and cancelled-turn closing parts. */
|
|
@@ -535,7 +563,7 @@ export class ClaudeAgentRunner implements AgentRunner, OneShotCompletion {
|
|
|
535
563
|
} catch (error) {
|
|
536
564
|
if (error instanceof NativeObservationError) {
|
|
537
565
|
controller.abort(error)
|
|
538
|
-
|
|
566
|
+
await handle.result.catch(() => {})
|
|
539
567
|
throw error
|
|
540
568
|
}
|
|
541
569
|
// The result promise below still carries the CLI's exit and stderr.
|