@defrex/autobuild 1.10.0 → 1.11.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 +1 -1
- package/SPEC.md +738 -68
- package/bin/ab-dispatch-generation.ts +3 -1
- package/docs/accounts.md +2 -1
- package/docs/answering-blocked-builds.md +20 -0
- package/docs/architecture.md +155 -25
- package/docs/build-store.md +10 -7
- package/docs/cli.md +33 -1
- package/docs/configuration-overview.md +4 -1
- package/docs/configuration.md +381 -29
- package/docs/dispatcher.md +63 -4
- package/docs/forges.md +7 -0
- package/docs/integration-tree.md +2 -2
- package/docs/operating.md +4 -2
- package/docs/operator-api.md +134 -6
- package/docs/operator-surfaces.md +85 -0
- package/docs/optimization-nodes.md +69 -11
- package/docs/plugin-authoring.md +37 -1
- package/docs/remote-store-protocol.md +94 -13
- package/docs/remote-ticket-protocol.md +40 -0
- package/docs/restacking.md +20 -4
- package/docs/setup.md +14 -2
- package/docs/ticket-dependencies.md +10 -3
- package/docs/ticket-sources.md +24 -2
- package/docs/tickets.md +7 -1
- package/docs/troubleshooting.md +8 -0
- package/docs/verification.md +4 -3
- package/docs/workspaces.md +3 -0
- package/package.json +9 -5
- package/packages/core/src/cli/accounts.ts +7 -2
- package/packages/core/src/cli/attention.ts +3 -1
- package/packages/core/src/cli/binary-direct.ts +2 -0
- package/packages/core/src/cli/binary-lifecycle.ts +2 -1
- package/packages/core/src/cli/build-control.ts +79 -1
- package/packages/core/src/cli/context.ts +4 -0
- package/packages/core/src/cli/dashboard/facts.ts +49 -53
- package/packages/core/src/cli/dashboard/header.ts +101 -0
- package/packages/core/src/cli/dashboard/model.ts +31 -39
- package/packages/core/src/cli/dashboard/objective-row.ts +46 -14
- package/packages/core/src/cli/dashboard/poll.ts +178 -76
- package/packages/core/src/cli/dashboard/render.ts +129 -230
- package/packages/core/src/cli/dashboard/row-text.ts +538 -0
- package/packages/core/src/cli/dashboard/timeline.ts +288 -0
- package/packages/core/src/cli/dashboard/vocabulary.ts +188 -0
- package/packages/core/src/cli/dispatch-args.ts +44 -1
- package/packages/core/src/cli/dispatch-frontend.ts +29 -38
- package/packages/core/src/cli/dispatch-launch-stop.ts +18 -0
- package/packages/core/src/cli/dispatch-launcher.ts +20 -7
- package/packages/core/src/cli/dispatch-web.ts +155 -0
- package/packages/core/src/cli/dispatch.ts +200 -34
- package/packages/core/src/cli/help.ts +87 -3
- package/packages/core/src/cli/init-validation.ts +3 -3
- package/packages/core/src/cli/main.ts +70 -6
- package/packages/core/src/cli/mcp.ts +35 -3
- package/packages/core/src/cli/objective.ts +1 -1
- package/packages/core/src/cli/operator-preference.ts +57 -0
- package/packages/core/src/cli/repo-lease.ts +2 -0
- package/packages/core/src/cli/repo-state.ts +56 -6
- package/packages/core/src/cli/repository-status.ts +97 -43
- package/packages/core/src/cli/status.ts +82 -1
- package/packages/core/src/cli/terminal-restore.ts +32 -7
- package/packages/core/src/cli/ticket.ts +98 -36
- package/packages/core/src/cli/wait.ts +14 -6
- package/packages/core/src/cli/watch.ts +20 -4
- package/packages/core/src/cli/web.ts +139 -0
- package/packages/core/src/config/live.ts +99 -14
- package/packages/core/src/config/load.ts +61 -20
- package/packages/core/src/config/local.ts +212 -0
- package/packages/core/src/config/pipeline-source.ts +2 -0
- package/packages/core/src/config/reference.ts +18 -0
- package/packages/core/src/config/schema.ts +45 -10
- package/packages/core/src/events/catalog.ts +4 -0
- package/packages/core/src/events/payloads.ts +97 -0
- package/packages/core/src/events/policy.ts +10 -0
- package/packages/core/src/events/repository.ts +46 -3
- package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +316 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/017d8dd5bb6dd80b.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/178e12f1759b7552.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/4932c70a2af97faf.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/536085773daf6b58.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/6bd6275061aaffa5.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/89625654b02e9ac1.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/a98af134be2da8a9.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/d5d5ccca80a893d8.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/f8155387eb5f375c.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/3c27f918b194e3b3.json +465 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/79f0e0f4ccd43ba2.json +455 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/859c09edac738823.json +1086 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a283c1f3462b1cc1.json +81 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a6e570fb126991ff.json +36 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a8acf22c968b6e3d.json +81 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/ba7b85895bf6698e.json +1153 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/fc66408cde0b6b04.json +637 -0
- package/packages/core/src/kernel/build-roster.ts +230 -0
- package/packages/core/src/kernel/dispatch-capacity.ts +4 -2
- package/packages/core/src/kernel/engine.ts +43 -5
- package/packages/core/src/kernel/execution-intervals.ts +61 -0
- package/packages/core/src/kernel/generators/optimization-log.ts +101 -0
- package/packages/core/src/kernel/generators/repository-journals.ts +13 -0
- package/packages/core/src/kernel/leaf-restack-base.ts +33 -7
- package/packages/core/src/kernel/leaf-restack-state.ts +82 -3
- package/packages/core/src/kernel/log-index.ts +19 -0
- package/packages/core/src/kernel/optimization-admission.ts +244 -3
- package/packages/core/src/kernel/optimization.ts +183 -4
- package/packages/core/src/kernel/phase-failures.ts +121 -0
- package/packages/core/src/kernel/published-branch-head.ts +60 -0
- package/packages/core/src/kernel/reducer-registry.ts +27 -8
- package/packages/core/src/kernel/reducer-revisions.ts +25 -0
- package/packages/core/src/kernel/reducer.ts +63 -4
- package/packages/core/src/kernel/rollup.ts +61 -0
- package/packages/core/src/kernel/setup-streak.ts +38 -0
- package/packages/core/src/kernel/step-durations.ts +257 -0
- package/packages/core/src/kernel/target-observation.ts +6 -1
- package/packages/core/src/kernel/target-rewrite-recovery.ts +116 -0
- package/packages/core/src/kernel/verify-diff-base.ts +5 -0
- package/packages/core/src/kernel/verify-stages.ts +13 -3
- package/packages/core/src/ontology.ts +5 -0
- package/packages/core/src/operator/agent-context-serve.ts +631 -0
- package/packages/core/src/operator/annotations.ts +75 -5
- package/packages/core/src/operator/attention.ts +470 -0
- package/packages/core/src/operator/canonical-skills.ts +35 -0
- package/packages/core/src/operator/cli-equivalents.ts +270 -0
- package/packages/core/src/operator/context-prompts.ts +61 -0
- package/packages/core/src/operator/index.ts +49 -1
- package/packages/core/src/operator/landings.ts +28 -19
- package/packages/core/src/operator/optimization.ts +7 -0
- package/packages/core/src/operator/protocol.ts +3 -0
- package/packages/core/src/operator/query.ts +122 -72
- package/packages/core/src/operator/read-view.ts +5 -2
- package/packages/core/src/operator/registry.ts +406 -46
- package/packages/core/src/operator/requests.ts +20 -18
- package/packages/core/src/operator/sandbox-host.ts +107 -11
- package/packages/core/src/operator/sandbox-resolver.ts +253 -0
- package/packages/core/src/operator/sandbox.ts +778 -82
- package/packages/core/src/operator/ticket-asset-list.ts +77 -0
- package/packages/core/src/operator/ticket-assets.ts +167 -0
- package/packages/core/src/operator/tickets.ts +33 -2
- package/packages/core/src/plugin-sdk/index.ts +22 -0
- package/packages/core/src/plugins/manifest.ts +1 -1
- package/packages/core/src/ports/forge/fake.ts +92 -1
- package/packages/core/src/ports/forge/git-blob.ts +15 -0
- package/packages/core/src/ports/forge/github.ts +82 -0
- package/packages/core/src/ports/forge/local-git.ts +61 -1
- package/packages/core/src/ports/forge-failure.ts +24 -0
- package/packages/core/src/ports/runner/antigravity-test.fixture.ts +149 -0
- package/packages/core/src/ports/runner/claude-test.fixture.ts +71 -0
- package/packages/core/src/ports/runner/codex-test.fixture.ts +63 -0
- package/packages/core/src/ports/tickets/contract.ts +71 -0
- package/packages/core/src/ports/tickets/fake.ts +11 -0
- package/packages/core/src/ports/tickets/file.ts +18 -0
- package/packages/core/src/ports/tickets/github-fake.ts +136 -4
- package/packages/core/src/ports/tickets/github.ts +387 -30
- package/packages/core/src/ports/tickets/hosted-index.ts +10 -0
- package/packages/core/src/ports/tickets/linear.ts +130 -0
- package/packages/core/src/ports/tickets/priority.ts +9 -0
- package/packages/core/src/ports/tickets/remote-protocol.ts +34 -0
- package/packages/core/src/ports/tickets/remote.ts +70 -6
- package/packages/core/src/ports/tickets/search.ts +139 -0
- package/packages/core/src/ports/types.ts +104 -2
- package/packages/core/src/ports/workspace/builtin-capabilities.ts +9 -0
- package/packages/core/src/ports/workspace/create.ts +15 -1
- package/packages/core/src/ports/workspace/fake.ts +340 -12
- package/packages/core/src/ports/workspace/git-worktree.ts +833 -42
- package/packages/core/src/ports/workspace/operator-sandbox.ts +129 -12
- package/packages/core/src/ports/workspace/process-group.ts +48 -0
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +1260 -123
- package/packages/core/src/processes/agent-context-assemble.ts +576 -0
- package/packages/core/src/processes/agent-context.ts +469 -0
- package/packages/core/src/processes/build-runner.ts +387 -236
- package/packages/core/src/processes/dispatcher.ts +211 -55
- package/packages/core/src/processes/execution-no-progress.ts +4 -0
- package/packages/core/src/processes/optimization-activity.ts +120 -0
- package/packages/core/src/processes/phase-failure-budget.ts +1 -0
- package/packages/core/src/processes/repo-view.ts +78 -2
- package/packages/core/src/processes/sandbox-state.ts +53 -6
- package/packages/core/src/processes/tick-publication.ts +2 -0
- package/packages/core/src/processes/workspace-preparation.ts +76 -0
- package/packages/core/src/scenarios/harness.ts +0 -5
- package/packages/core/src/store/account-redaction.ts +1 -0
- package/packages/core/src/store/adapter.ts +52 -1
- package/packages/core/src/store/agent-context.ts +432 -0
- package/packages/core/src/store/build-feed.ts +291 -0
- package/packages/core/src/store/build-scope.ts +19 -1
- package/packages/core/src/store/contract.ts +911 -14
- package/packages/core/src/store/digest.ts +19 -6
- package/packages/core/src/store/histogram.ts +113 -0
- package/packages/core/src/store/local/schema.ts +62 -0
- package/packages/core/src/store/local/store.ts +628 -22
- package/packages/core/src/store/local/ticket-asset-heads.ts +23 -0
- package/packages/core/src/store/memory.ts +461 -69
- package/packages/core/src/store/open-builds.ts +19 -0
- package/packages/core/src/store/phase-session.ts +32 -4
- package/packages/core/src/store/reduced-reads.ts +14 -4
- package/packages/core/src/store/reducer-value-shapes.json +6987 -5749
- package/packages/core/src/store/reducer-values.ts +6 -1
- package/packages/core/src/store/remote/client.ts +208 -2
- package/packages/core/src/store/remote/compatibility.ts +1 -1
- package/packages/core/src/store/remote/index.ts +33 -0
- package/packages/core/src/store/remote/protocol.ts +69 -0
- package/packages/core/src/store/remote/released-clients.json +11 -0
- package/packages/core/src/store/remote/released-reducer-values/1.11.0.json +35680 -0
- package/packages/core/src/store/remote/released-reducer-values/index.ts +18 -16
- package/packages/core/src/store/retention.ts +23 -1
- package/packages/core/src/store/rollups.ts +337 -0
- package/packages/core/src/store/session-handle.ts +20 -2
- package/packages/core/src/store/snapshots.ts +10 -0
- package/packages/core/src/store/types.ts +108 -9
- package/packages/core/src/testing/dispatch-read-budget.ts +1092 -2
- package/packages/core/src/testing/git-config-isolation.ts +7 -5
- package/packages/core/src/testing/index.ts +27 -0
- package/packages/core/src/testing/memory-operator-sandbox.ts +346 -0
- package/packages/core/src/testing/reduced-read-budget.ts +1873 -98
- package/packages/core/src/web/analytics-source.ts +303 -0
- package/packages/core/src/web/assets/Atkinson-OFL.txt +93 -0
- package/packages/core/src/web/assets/AtkinsonHyperlegibleNext.latin.woff2 +0 -0
- package/packages/core/src/web/assets/JetBrains-OFL.txt +93 -0
- package/packages/core/src/web/assets/JetBrainsMono.latin.woff2 +0 -0
- package/packages/core/src/web/assets/app.css +1 -0
- package/packages/core/src/web/assets/app.js +9 -0
- package/packages/core/src/web/assets/manifest.json +31 -0
- package/packages/core/src/web/assets/theme.js +1 -0
- package/packages/core/src/web/bounded.ts +31 -0
- package/packages/core/src/web/http.ts +315 -0
- package/packages/core/src/web/hub.ts +160 -0
- package/packages/core/src/web/options.ts +38 -0
- package/packages/core/src/web/overview-source.ts +322 -0
- package/packages/core/src/web/page.ts +56 -0
- package/packages/core/src/web/protocol.ts +151 -0
- package/packages/core/src/web/read-only.ts +23 -0
- package/packages/core/src/web/repository-source.ts +35 -0
- package/packages/core/src/web/server.ts +97 -0
- package/skills/code-review/SKILL.md +10 -2
- package/skills/guide/SKILL.md +366 -52
- package/skills/guide/references/accounts.md +2 -1
- package/skills/guide/references/integration-tree.md +2 -2
- package/skills/guide/references/operator-api.md +134 -6
- package/skills/guide/references/optimization-nodes.md +69 -11
- package/skills/guide/references/plugin-authoring.md +37 -1
- package/skills/guide/references/remote-store-protocol.md +94 -13
- package/skills/guide/references/remote-ticket-protocol.md +40 -0
- package/skills/guide/references/restacking.md +20 -4
- package/skills/guide/references/setup.md +14 -2
- package/skills/guide/references/ticket-dependencies.md +10 -3
- package/skills/implement/SKILL.md +23 -4
- package/skills/operate/SKILL.md +134 -32
- package/skills/plan/SKILL.md +10 -4
- package/skills/plan-review/SKILL.md +9 -2
- package/skills/spec/SKILL.md +34 -8
- package/skills/tickets/SKILL.md +51 -5
- package/templates/hosted-service/package.json +2 -2
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dispatcher's agent-context stage: keep one repository-level record of
|
|
3
|
+
* the base branch's installed skills and agent instructions in the store
|
|
4
|
+
* (`store/agent-context.ts`), read through the forge in both checkout and
|
|
5
|
+
* origin mode so both produce the same record for the same commit.
|
|
6
|
+
*
|
|
7
|
+
* Recurring cost: one base-head read per minute per process (and on its
|
|
8
|
+
* first tick), and one ~1 KiB pointer read per process. While the head is unchanged nothing else is read. A head
|
|
9
|
+
* change reads the tree; a change to the context fingerprint additionally
|
|
10
|
+
* reads the latest record (to reuse its blobs) and fetches only new blobs.
|
|
11
|
+
* No repository events are read or written.
|
|
12
|
+
*/
|
|
13
|
+
import { randomUUID } from 'node:crypto'
|
|
14
|
+
import { rethrowAuthorityLapse } from '../cli/repo-lease'
|
|
15
|
+
import { isForgeTreeUnlistable } from '../ports/forge-failure'
|
|
16
|
+
import { ForgeRequestDeferred } from '../ports/forge-request-context'
|
|
17
|
+
import { gitBlobOid, matchesGitBlobOid } from '../ports/forge/git-blob'
|
|
18
|
+
import type { Forge, ForgeTreeEntry } from '../ports/types'
|
|
19
|
+
import {
|
|
20
|
+
AGENT_CONTEXT_ARTIFACT,
|
|
21
|
+
AGENT_CONTEXT_CACHE_SLOT_PAYLOAD,
|
|
22
|
+
AGENT_CONTEXT_CACHE_SLOTS,
|
|
23
|
+
AGENT_CONTEXT_HEAD_ARTIFACT,
|
|
24
|
+
AGENT_CONTEXT_MAX_BYTES,
|
|
25
|
+
AGENT_CONTEXT_MAX_RAW_BYTES,
|
|
26
|
+
agentContextCacheKind,
|
|
27
|
+
contentBytes,
|
|
28
|
+
decodeAgentContext,
|
|
29
|
+
decodeAgentContextCache,
|
|
30
|
+
decodeAgentContextPointer,
|
|
31
|
+
encodeAgentContext,
|
|
32
|
+
encodeAgentContextCache,
|
|
33
|
+
encodeAgentContextPointer,
|
|
34
|
+
packContinuation,
|
|
35
|
+
unpackContinuation,
|
|
36
|
+
type AgentContextCacheSlot,
|
|
37
|
+
type AgentContextDiagnostic,
|
|
38
|
+
type AgentContextPointer,
|
|
39
|
+
type AgentContextRecord,
|
|
40
|
+
} from '../store/agent-context'
|
|
41
|
+
import type { BuildStore } from '../store/types'
|
|
42
|
+
import {
|
|
43
|
+
assembleAgentContext,
|
|
44
|
+
contextKey,
|
|
45
|
+
type AgentContextContributor,
|
|
46
|
+
} from './agent-context-assemble'
|
|
47
|
+
|
|
48
|
+
/** Blob reads in flight at once. */
|
|
49
|
+
const FETCH_CONCURRENCY = 8
|
|
50
|
+
/** Time kept back from the invocation deadline for the continuation write. */
|
|
51
|
+
const DEADLINE_RESERVE_MS = 2_000
|
|
52
|
+
const LARGEST = 5
|
|
53
|
+
/** A failed capture is retried at the same head after this long, and at
|
|
54
|
+
* once when the head moves, so a persistent failure costs no per-tick reads. */
|
|
55
|
+
export const AGENT_CONTEXT_RETRY_MS = 5 * 60_000
|
|
56
|
+
/** A process reads the base head at most this often (always on its first
|
|
57
|
+
* run), so idle ticks make no forge request, and no guarded-forge health
|
|
58
|
+
* read, for the context. */
|
|
59
|
+
export const AGENT_CONTEXT_HEAD_POLL_MS = 60_000
|
|
60
|
+
|
|
61
|
+
export interface AgentContextBounds {
|
|
62
|
+
maxBytes: number
|
|
63
|
+
maxRawBytes: number
|
|
64
|
+
slotPayload: number
|
|
65
|
+
slots: number
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface AgentContextRunOpts {
|
|
69
|
+
baseBranch: string
|
|
70
|
+
/** Invocation bound in epoch ms against `now`. */
|
|
71
|
+
deadlineAt?: number
|
|
72
|
+
/** Repository authority; when false the stage reads and writes nothing. */
|
|
73
|
+
holdsAuthority?: () => boolean
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type Store = Pick<BuildStore, 'getRepoArtifact' | 'putRepoArtifact'>
|
|
77
|
+
|
|
78
|
+
interface Continuation {
|
|
79
|
+
captureId: string
|
|
80
|
+
slots: AgentContextCacheSlot[]
|
|
81
|
+
/** Every pointer-named slot was present and carried this capture's id. */
|
|
82
|
+
intact: boolean
|
|
83
|
+
blobs: Map<string, Uint8Array>
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function seedFromRecord(cache: Map<string, Uint8Array>, record: AgentContextRecord): void {
|
|
87
|
+
for (const skill of record.skills)
|
|
88
|
+
for (const file of skill.files) cache.set(file.oid, contentBytes(file))
|
|
89
|
+
for (const entry of record.instructions) cache.set(entry.oid, contentBytes(entry))
|
|
90
|
+
for (const entry of record.imports) cache.set(entry.oid, contentBytes(entry))
|
|
91
|
+
for (const link of record.links) {
|
|
92
|
+
const bytes = new TextEncoder().encode(link.target)
|
|
93
|
+
cache.set(gitBlobOid(bytes), bytes)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function message(error: unknown): string {
|
|
98
|
+
return error instanceof Error ? error.message : String(error)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class AgentContextCapture {
|
|
102
|
+
private readonly store: Store
|
|
103
|
+
private readonly repo: string
|
|
104
|
+
private readonly forge: Forge
|
|
105
|
+
/** The dispatch clock, read only to honor `deadlineAt`. */
|
|
106
|
+
private readonly now: () => number
|
|
107
|
+
/** Monotonic process time pacing head polls and failure retries. */
|
|
108
|
+
private readonly elapsed: () => number
|
|
109
|
+
private readonly bounds: AgentContextBounds
|
|
110
|
+
/** Last pointer this process read or wrote; `null` when none exists. This
|
|
111
|
+
* dispatcher is the pointer's only writer while it holds the repository. */
|
|
112
|
+
private pointer: AgentContextPointer | null | undefined
|
|
113
|
+
private unavailableReported = false
|
|
114
|
+
private lastFailure: { message: string; head?: string; at: number } | undefined
|
|
115
|
+
private headReadAt: number | undefined
|
|
116
|
+
|
|
117
|
+
constructor(opts: {
|
|
118
|
+
store: Store
|
|
119
|
+
repo: string
|
|
120
|
+
forge: Forge
|
|
121
|
+
now?: () => number
|
|
122
|
+
/** Pacing clock in ms; defaults to monotonic process time. */
|
|
123
|
+
elapsed?: () => number
|
|
124
|
+
bounds?: Partial<AgentContextBounds>
|
|
125
|
+
}) {
|
|
126
|
+
this.store = opts.store
|
|
127
|
+
this.repo = opts.repo
|
|
128
|
+
this.forge = opts.forge
|
|
129
|
+
this.now = opts.now ?? (() => Date.now())
|
|
130
|
+
this.elapsed = opts.elapsed ?? (() => performance.now())
|
|
131
|
+
this.bounds = {
|
|
132
|
+
maxBytes: opts.bounds?.maxBytes ?? AGENT_CONTEXT_MAX_BYTES,
|
|
133
|
+
maxRawBytes: opts.bounds?.maxRawBytes ?? AGENT_CONTEXT_MAX_RAW_BYTES,
|
|
134
|
+
slotPayload: opts.bounds?.slotPayload ?? AGENT_CONTEXT_CACHE_SLOT_PAYLOAD,
|
|
135
|
+
slots: opts.bounds?.slots ?? AGENT_CONTEXT_CACHE_SLOTS,
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async run(opts: AgentContextRunOpts): Promise<AgentContextDiagnostic[]> {
|
|
140
|
+
if (opts.holdsAuthority?.() === false) return []
|
|
141
|
+
const forge = this.forge
|
|
142
|
+
if (!forge.remoteBranchSha || !forge.readTree || !forge.readBlob) {
|
|
143
|
+
if (this.unavailableReported) return []
|
|
144
|
+
this.unavailableReported = true
|
|
145
|
+
return [
|
|
146
|
+
{
|
|
147
|
+
kind: 'unavailable',
|
|
148
|
+
message: `the ${forge.name} forge cannot read repository trees and blobs`,
|
|
149
|
+
},
|
|
150
|
+
]
|
|
151
|
+
}
|
|
152
|
+
if (
|
|
153
|
+
this.headReadAt !== undefined &&
|
|
154
|
+
this.elapsed() - this.headReadAt < AGENT_CONTEXT_HEAD_POLL_MS
|
|
155
|
+
)
|
|
156
|
+
return []
|
|
157
|
+
this.headReadAt = this.elapsed()
|
|
158
|
+
let head: string | undefined
|
|
159
|
+
try {
|
|
160
|
+
try {
|
|
161
|
+
head = await forge.remoteBranchSha(opts.baseBranch)
|
|
162
|
+
} catch (error) {
|
|
163
|
+
// A repository whose base branch does not exist yet has no context
|
|
164
|
+
// to capture. Checked only on the failure path, so it costs nothing
|
|
165
|
+
// while the branch resolves.
|
|
166
|
+
if (forge.remoteBranchExists && !(await forge.remoteBranchExists(opts.baseBranch)))
|
|
167
|
+
return []
|
|
168
|
+
throw error
|
|
169
|
+
}
|
|
170
|
+
const failed = this.lastFailure
|
|
171
|
+
if (failed?.head === head && this.elapsed() - failed.at < AGENT_CONTEXT_RETRY_MS) return []
|
|
172
|
+
const diagnostics = await this.capture(head, opts)
|
|
173
|
+
this.lastFailure = undefined
|
|
174
|
+
return diagnostics
|
|
175
|
+
} catch (error) {
|
|
176
|
+
rethrowAuthorityLapse(error)
|
|
177
|
+
if (error instanceof ForgeRequestDeferred) return []
|
|
178
|
+
const text = message(error)
|
|
179
|
+
const repeated = this.lastFailure?.message === text
|
|
180
|
+
this.lastFailure = {
|
|
181
|
+
message: text,
|
|
182
|
+
...(head !== undefined ? { head } : {}),
|
|
183
|
+
at: this.elapsed(),
|
|
184
|
+
}
|
|
185
|
+
// Reported once per distinct failure.
|
|
186
|
+
if (repeated) return []
|
|
187
|
+
return [
|
|
188
|
+
{ kind: 'capture-failed', ...(head !== undefined ? { commit: head } : {}), message: text },
|
|
189
|
+
]
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private async loadPointer(): Promise<AgentContextPointer | null> {
|
|
194
|
+
if (this.pointer !== undefined) return this.pointer
|
|
195
|
+
const artifact = await this.store.getRepoArtifact(this.repo, AGENT_CONTEXT_HEAD_ARTIFACT)
|
|
196
|
+
let pointer: AgentContextPointer | null = null
|
|
197
|
+
if (artifact) {
|
|
198
|
+
try {
|
|
199
|
+
pointer = decodeAgentContextPointer(artifact.content)
|
|
200
|
+
} catch {
|
|
201
|
+
// An unreadable pointer only costs a recapture.
|
|
202
|
+
pointer = null
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
this.pointer = pointer
|
|
206
|
+
return pointer
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private async writePointer(pointer: AgentContextPointer): Promise<void> {
|
|
210
|
+
this.pointer = undefined
|
|
211
|
+
await this.store.putRepoArtifact(this.repo, {
|
|
212
|
+
kind: AGENT_CONTEXT_HEAD_ARTIFACT,
|
|
213
|
+
content: encodeAgentContextPointer(pointer),
|
|
214
|
+
})
|
|
215
|
+
this.pointer = pointer
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private async capture(
|
|
219
|
+
head: string,
|
|
220
|
+
opts: AgentContextRunOpts,
|
|
221
|
+
): Promise<AgentContextDiagnostic[]> {
|
|
222
|
+
const forge = this.forge as Required<Pick<Forge, 'readTree' | 'readBlob'>> & Forge
|
|
223
|
+
const lost = () => opts.holdsAuthority?.() === false
|
|
224
|
+
const branch = opts.baseBranch
|
|
225
|
+
const loaded = await this.loadPointer()
|
|
226
|
+
const pointer = loaded !== null && loaded.branch === branch ? loaded : null
|
|
227
|
+
if (pointer?.observed === head && pointer.pending === undefined) return []
|
|
228
|
+
const settled: AgentContextPointer = {
|
|
229
|
+
version: 1,
|
|
230
|
+
branch,
|
|
231
|
+
observed: pointer?.observed ?? null,
|
|
232
|
+
key: pointer?.key ?? null,
|
|
233
|
+
dependencies: pointer?.dependencies ?? [],
|
|
234
|
+
...(pointer?.revision !== undefined ? { revision: pointer.revision } : {}),
|
|
235
|
+
...(pointer?.digest !== undefined ? { digest: pointer.digest } : {}),
|
|
236
|
+
...(pointer?.diagnostic !== undefined ? { diagnostic: pointer.diagnostic } : {}),
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
let entries: ForgeTreeEntry[]
|
|
240
|
+
try {
|
|
241
|
+
entries = await forge.readTree(head)
|
|
242
|
+
} catch (error) {
|
|
243
|
+
if (!isForgeTreeUnlistable(error)) throw error
|
|
244
|
+
const diagnostic: AgentContextDiagnostic = {
|
|
245
|
+
kind: 'tree-unlistable',
|
|
246
|
+
commit: head,
|
|
247
|
+
directory: error.directory,
|
|
248
|
+
}
|
|
249
|
+
if (!lost()) await this.writePointer({ ...settled, observed: head, diagnostic })
|
|
250
|
+
return [diagnostic]
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// An unchanged fingerprint means an unchanged record: no blob or record reads.
|
|
254
|
+
if (pointer?.key != null && contextKey(entries, pointer.dependencies) === pointer.key) {
|
|
255
|
+
if (!lost()) await this.writePointer({ ...settled, observed: head })
|
|
256
|
+
return []
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const cache = new Map<string, Uint8Array>()
|
|
260
|
+
const latest = await this.store.getRepoArtifact(this.repo, AGENT_CONTEXT_ARTIFACT)
|
|
261
|
+
const latestDigest =
|
|
262
|
+
typeof latest?.meta.metadata.digest === 'string' ? latest.meta.metadata.digest : undefined
|
|
263
|
+
const latestRevision = latest?.meta.revision
|
|
264
|
+
if (latest) {
|
|
265
|
+
try {
|
|
266
|
+
seedFromRecord(cache, decodeAgentContext(latest.content))
|
|
267
|
+
} catch {
|
|
268
|
+
// An unreadable record only costs refetching its blobs.
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const continuation = await this.readContinuation(loaded?.pending)
|
|
272
|
+
for (const [oid, bytes] of continuation?.blobs ?? []) cache.set(oid, bytes)
|
|
273
|
+
|
|
274
|
+
const kept = {
|
|
275
|
+
...(latestRevision !== undefined ? { revision: latestRevision } : {}),
|
|
276
|
+
...(latestDigest !== undefined ? { digest: latestDigest } : {}),
|
|
277
|
+
}
|
|
278
|
+
const rawOversize = async (
|
|
279
|
+
rawBytes: number,
|
|
280
|
+
contributors: AgentContextContributor[],
|
|
281
|
+
dependencies: string[],
|
|
282
|
+
): Promise<AgentContextDiagnostic[]> => {
|
|
283
|
+
const diagnostic: AgentContextDiagnostic = {
|
|
284
|
+
kind: 'raw-oversize',
|
|
285
|
+
commit: head,
|
|
286
|
+
rawBound: this.bounds.maxRawBytes,
|
|
287
|
+
rawBytes,
|
|
288
|
+
largest: contributors.slice(0, LARGEST),
|
|
289
|
+
}
|
|
290
|
+
if (!lost())
|
|
291
|
+
await this.writePointer({
|
|
292
|
+
version: 1,
|
|
293
|
+
branch,
|
|
294
|
+
observed: head,
|
|
295
|
+
key: contextKey(entries, dependencies),
|
|
296
|
+
dependencies,
|
|
297
|
+
...kept,
|
|
298
|
+
diagnostic,
|
|
299
|
+
})
|
|
300
|
+
return [diagnostic]
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const fetched = new Map<string, Uint8Array>()
|
|
304
|
+
for (;;) {
|
|
305
|
+
const pass = assembleAgentContext({
|
|
306
|
+
commit: head,
|
|
307
|
+
baseBranch: branch,
|
|
308
|
+
entries,
|
|
309
|
+
blob: (oid) => cache.get(oid),
|
|
310
|
+
})
|
|
311
|
+
if (pass.rawBytes > this.bounds.maxRawBytes)
|
|
312
|
+
return rawOversize(pass.rawBytes, pass.contributors, pass.dependencies)
|
|
313
|
+
const record = pass.record
|
|
314
|
+
if (record !== undefined) {
|
|
315
|
+
const encoded = encodeAgentContext(record)
|
|
316
|
+
// The reader's decode ceiling is this same bound: never publish a
|
|
317
|
+
// record it would refuse.
|
|
318
|
+
if (encoded.jsonBytes > this.bounds.maxRawBytes)
|
|
319
|
+
return rawOversize(encoded.jsonBytes, pass.contributors, pass.dependencies)
|
|
320
|
+
const key = contextKey(entries, pass.dependencies)
|
|
321
|
+
const bytes = encoded.content.byteLength
|
|
322
|
+
if (bytes > this.bounds.maxBytes) {
|
|
323
|
+
const diagnostic: AgentContextDiagnostic = {
|
|
324
|
+
kind: 'oversize',
|
|
325
|
+
commit: head,
|
|
326
|
+
bound: this.bounds.maxBytes,
|
|
327
|
+
bytes,
|
|
328
|
+
largest: pass.contributors.slice(0, LARGEST),
|
|
329
|
+
}
|
|
330
|
+
if (!lost())
|
|
331
|
+
await this.writePointer({
|
|
332
|
+
version: 1,
|
|
333
|
+
branch,
|
|
334
|
+
observed: head,
|
|
335
|
+
key,
|
|
336
|
+
dependencies: pass.dependencies,
|
|
337
|
+
...kept,
|
|
338
|
+
diagnostic,
|
|
339
|
+
})
|
|
340
|
+
return [diagnostic]
|
|
341
|
+
}
|
|
342
|
+
if (lost()) return []
|
|
343
|
+
let revision = latestRevision
|
|
344
|
+
if (encoded.digest !== latestDigest) {
|
|
345
|
+
const meta = await this.store.putRepoArtifact(this.repo, {
|
|
346
|
+
kind: AGENT_CONTEXT_ARTIFACT,
|
|
347
|
+
content: encoded.content,
|
|
348
|
+
metadata: {
|
|
349
|
+
schema: 1,
|
|
350
|
+
commit: head,
|
|
351
|
+
digest: encoded.digest,
|
|
352
|
+
bytes,
|
|
353
|
+
rawBytes: encoded.rawBytes,
|
|
354
|
+
},
|
|
355
|
+
})
|
|
356
|
+
revision = meta.revision
|
|
357
|
+
}
|
|
358
|
+
// Written after the record: a crash between the two re-runs assembly,
|
|
359
|
+
// and the digest comparison above keeps that from duplicating it.
|
|
360
|
+
await this.writePointer({
|
|
361
|
+
version: 1,
|
|
362
|
+
branch,
|
|
363
|
+
observed: head,
|
|
364
|
+
key,
|
|
365
|
+
dependencies: pass.dependencies,
|
|
366
|
+
...(revision !== undefined ? { revision } : {}),
|
|
367
|
+
digest: encoded.digest,
|
|
368
|
+
})
|
|
369
|
+
return []
|
|
370
|
+
}
|
|
371
|
+
for (let i = 0; i < pass.missing.length; i += FETCH_CONCURRENCY) {
|
|
372
|
+
if (opts.deadlineAt !== undefined && opts.deadlineAt - this.now() < DEADLINE_RESERVE_MS) {
|
|
373
|
+
if (!lost())
|
|
374
|
+
await this.persistContinuation({
|
|
375
|
+
head,
|
|
376
|
+
settled,
|
|
377
|
+
continuation,
|
|
378
|
+
wanted: pass.wanted,
|
|
379
|
+
fetched,
|
|
380
|
+
})
|
|
381
|
+
return []
|
|
382
|
+
}
|
|
383
|
+
const batch = pass.missing.slice(i, i + FETCH_CONCURRENCY)
|
|
384
|
+
const blobs = await Promise.all(batch.map((oid) => forge.readBlob(oid)))
|
|
385
|
+
batch.forEach((oid, index) => {
|
|
386
|
+
const bytes = blobs[index]!
|
|
387
|
+
cache.set(oid, bytes)
|
|
388
|
+
fetched.set(oid, bytes)
|
|
389
|
+
})
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** The live continuation the pointer names. A missing or foreign slot is
|
|
395
|
+
* never a failure: only its bytes are lost, and they are fetched again. */
|
|
396
|
+
private async readContinuation(
|
|
397
|
+
pending: AgentContextPointer['pending'],
|
|
398
|
+
): Promise<Continuation | undefined> {
|
|
399
|
+
if (pending === undefined) return undefined
|
|
400
|
+
const slots: AgentContextCacheSlot[] = []
|
|
401
|
+
let intact = true
|
|
402
|
+
for (let index = 0; index < pending.slots; index++) {
|
|
403
|
+
const artifact = await this.store.getRepoArtifact(this.repo, agentContextCacheKind(index))
|
|
404
|
+
let slot: AgentContextCacheSlot | undefined
|
|
405
|
+
try {
|
|
406
|
+
slot = artifact ? decodeAgentContextCache(artifact.content) : undefined
|
|
407
|
+
} catch {
|
|
408
|
+
slot = undefined
|
|
409
|
+
}
|
|
410
|
+
if (slot === undefined || slot.captureId !== pending.captureId || slot.slot !== index) {
|
|
411
|
+
intact = false
|
|
412
|
+
continue
|
|
413
|
+
}
|
|
414
|
+
slots.push(slot)
|
|
415
|
+
}
|
|
416
|
+
return {
|
|
417
|
+
captureId: pending.captureId,
|
|
418
|
+
slots,
|
|
419
|
+
intact,
|
|
420
|
+
blobs: unpackContinuation(slots, matchesGitBlobOid),
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Persist what this invocation fetched, plus the earlier continuation
|
|
425
|
+
* still needed, then point at it. An unchanged selection keeps every full
|
|
426
|
+
* slot and rewrites only the last partial one; otherwise a fresh capture
|
|
427
|
+
* repacks densely from slot 0. */
|
|
428
|
+
private async persistContinuation(input: {
|
|
429
|
+
head: string
|
|
430
|
+
settled: AgentContextPointer
|
|
431
|
+
continuation: Continuation | undefined
|
|
432
|
+
wanted: ReadonlySet<string>
|
|
433
|
+
fetched: ReadonlyMap<string, Uint8Array>
|
|
434
|
+
}): Promise<void> {
|
|
435
|
+
const { continuation, wanted, fetched } = input
|
|
436
|
+
const reuse =
|
|
437
|
+
continuation?.intact === true &&
|
|
438
|
+
continuation.slots.every((slot) => slot.parts.every((part) => wanted.has(part.oid)))
|
|
439
|
+
let captureId: string
|
|
440
|
+
let base: AgentContextCacheSlot[]
|
|
441
|
+
const blobs: { oid: string; bytes: Uint8Array }[] = []
|
|
442
|
+
if (reuse) {
|
|
443
|
+
captureId = continuation.captureId
|
|
444
|
+
base = continuation.slots
|
|
445
|
+
} else {
|
|
446
|
+
captureId = randomUUID()
|
|
447
|
+
base = []
|
|
448
|
+
for (const [oid, bytes] of continuation?.blobs ?? [])
|
|
449
|
+
if (wanted.has(oid) && !fetched.has(oid)) blobs.push({ oid, bytes })
|
|
450
|
+
}
|
|
451
|
+
for (const [oid, bytes] of fetched) blobs.push({ oid, bytes })
|
|
452
|
+
if (blobs.length === 0) return
|
|
453
|
+
const packed = packContinuation(captureId, base, blobs, this.bounds.slotPayload)
|
|
454
|
+
// Past the slot budget, the excess is simply fetched again later.
|
|
455
|
+
const slots = packed.slots.slice(0, this.bounds.slots)
|
|
456
|
+
for (const index of packed.rewritten) {
|
|
457
|
+
const slot = slots[index]
|
|
458
|
+
if (slot === undefined) continue
|
|
459
|
+
await this.store.putRepoArtifact(this.repo, {
|
|
460
|
+
kind: agentContextCacheKind(index),
|
|
461
|
+
content: encodeAgentContextCache(slot),
|
|
462
|
+
})
|
|
463
|
+
}
|
|
464
|
+
await this.writePointer({
|
|
465
|
+
...input.settled,
|
|
466
|
+
pending: { head: input.head, captureId, slots: slots.length },
|
|
467
|
+
})
|
|
468
|
+
}
|
|
469
|
+
}
|