@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,432 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Repository agent context: the dispatcher's durable record of the base
|
|
3
|
+
* branch's installed skills (`.agents/skills`, `.claude/skills`) and agent
|
|
4
|
+
* instructions (`AGENTS.md` / `CLAUDE.md`, root and nested, plus the files
|
|
5
|
+
* they import by `@path`), taken at one base commit.
|
|
6
|
+
*
|
|
7
|
+
* Three repository artifact families carry it, all retention-managed
|
|
8
|
+
* (`store/retention.ts`) and all read only at their latest revision:
|
|
9
|
+
*
|
|
10
|
+
* - `agent-context` — the record: gzip-compressed canonical JSON. A new
|
|
11
|
+
* revision is deposited only when its content digest changes.
|
|
12
|
+
* - `agent-context-head` — a ~1 KiB pointer naming the last base head the
|
|
13
|
+
* capture handled, the input fingerprint, and any standing diagnostic.
|
|
14
|
+
* - `agent-context-cache.<i>` — continuation slots holding blob bytes an
|
|
15
|
+
* interrupted capture already fetched, so a fresh process resumes instead
|
|
16
|
+
* of refetching. Valid only when their `captureId` matches the pointer's.
|
|
17
|
+
*
|
|
18
|
+
* Reading the record (`readAgentContext`) is one latest-revision artifact
|
|
19
|
+
* read, whatever the number of stored revisions.
|
|
20
|
+
*/
|
|
21
|
+
import { createHash } from 'node:crypto'
|
|
22
|
+
import { gunzipSync, gzipSync } from 'node:zlib'
|
|
23
|
+
import { z } from 'zod'
|
|
24
|
+
import {
|
|
25
|
+
AGENT_CONTEXT_ARTIFACT,
|
|
26
|
+
AGENT_CONTEXT_HEAD_ARTIFACT,
|
|
27
|
+
agentContextCacheKind,
|
|
28
|
+
} from './retention'
|
|
29
|
+
import type { BuildStore, RepositoryArtifactMeta } from './types'
|
|
30
|
+
|
|
31
|
+
export { AGENT_CONTEXT_ARTIFACT, AGENT_CONTEXT_HEAD_ARTIFACT, agentContextCacheKind }
|
|
32
|
+
|
|
33
|
+
const MIB = 1024 * 1024
|
|
34
|
+
|
|
35
|
+
/** Encoded bound: gzip-encoded record bytes, below the hosted artifact ceiling. */
|
|
36
|
+
export const AGENT_CONTEXT_MAX_BYTES = 2.25 * MIB
|
|
37
|
+
/** Raw bound: the record's uncompressed JSON bytes. It is both the
|
|
38
|
+
* producer's limit and every reader's decode ceiling, so a record that was
|
|
39
|
+
* published is always readable. Captured blob bytes never exceed it either
|
|
40
|
+
* (each is stored at least as long as it is), which bounds the continuation. */
|
|
41
|
+
export const AGENT_CONTEXT_MAX_RAW_BYTES = 32 * MIB
|
|
42
|
+
/** Continuation packing size: serialized part bytes per slot (a storage
|
|
43
|
+
* packing size, never a limit on the record). */
|
|
44
|
+
export const AGENT_CONTEXT_CACHE_SLOT_PAYLOAD = 1.75 * MIB
|
|
45
|
+
/** Decoded-size ceiling for any record a reader accepts: the raw bound. */
|
|
46
|
+
export const AGENT_CONTEXT_MAX_DECODED_BYTES = AGENT_CONTEXT_MAX_RAW_BYTES
|
|
47
|
+
/** Per-part envelope cost counted against a slot's payload (oid, offset,
|
|
48
|
+
* JSON punctuation), so a slot of many tiny parts still stays in bounds. */
|
|
49
|
+
export const AGENT_CONTEXT_CACHE_PART_OVERHEAD = 128
|
|
50
|
+
/** Parts the slot budget leaves envelope room for, beyond the base64 itself. */
|
|
51
|
+
const AGENT_CONTEXT_CACHE_PART_HEADROOM = 65_536
|
|
52
|
+
/** Enough slots for the raw input bound's base64 plus the envelopes of
|
|
53
|
+
* 65,536 parts. A capture beyond that (tens of thousands of captured files)
|
|
54
|
+
* drops the excess, which a later invocation fetches again. */
|
|
55
|
+
export const AGENT_CONTEXT_CACHE_SLOTS = Math.ceil(
|
|
56
|
+
((AGENT_CONTEXT_MAX_RAW_BYTES * 4) / 3 +
|
|
57
|
+
AGENT_CONTEXT_CACHE_PART_HEADROOM * AGENT_CONTEXT_CACHE_PART_OVERHEAD) /
|
|
58
|
+
AGENT_CONTEXT_CACHE_SLOT_PAYLOAD,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
const content = {
|
|
62
|
+
oid: z.string().min(1),
|
|
63
|
+
bytes: z.number().int().nonnegative(),
|
|
64
|
+
text: z.string().optional(),
|
|
65
|
+
base64: z.string().optional(),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const skillFileSchema = z.strictObject({ path: z.string().min(1), ...content })
|
|
69
|
+
const skillSchema = z.strictObject({
|
|
70
|
+
name: z.string().min(1),
|
|
71
|
+
paths: z.array(z.string().min(1)).min(1),
|
|
72
|
+
files: z.array(skillFileSchema),
|
|
73
|
+
})
|
|
74
|
+
const instructionSchema = z.strictObject({
|
|
75
|
+
paths: z.array(z.string().min(1)).min(1),
|
|
76
|
+
directory: z.string(),
|
|
77
|
+
...content,
|
|
78
|
+
imports: z.array(z.string()),
|
|
79
|
+
})
|
|
80
|
+
const importSchema = z.strictObject({
|
|
81
|
+
path: z.string().min(1),
|
|
82
|
+
importedBy: z.array(z.string()).min(1),
|
|
83
|
+
...content,
|
|
84
|
+
imports: z.array(z.string()),
|
|
85
|
+
})
|
|
86
|
+
export const AGENT_CONTEXT_OMISSION_REASONS = [
|
|
87
|
+
'outside-repository',
|
|
88
|
+
'dangling',
|
|
89
|
+
'submodule',
|
|
90
|
+
'link-depth',
|
|
91
|
+
] as const
|
|
92
|
+
export const agentContextRecordSchema = z.strictObject({
|
|
93
|
+
version: z.literal(1),
|
|
94
|
+
commit: z.string().min(1),
|
|
95
|
+
baseBranch: z.string().min(1),
|
|
96
|
+
skills: z.array(skillSchema),
|
|
97
|
+
instructions: z.array(instructionSchema),
|
|
98
|
+
imports: z.array(importSchema),
|
|
99
|
+
links: z.array(z.strictObject({ path: z.string(), target: z.string() })),
|
|
100
|
+
omitted: z.array(
|
|
101
|
+
z.strictObject({ path: z.string(), reason: z.enum(AGENT_CONTEXT_OMISSION_REASONS) }),
|
|
102
|
+
),
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
export type AgentContextRecord = z.infer<typeof agentContextRecordSchema>
|
|
106
|
+
export type AgentContextSkill = AgentContextRecord['skills'][number]
|
|
107
|
+
export type AgentContextInstruction = AgentContextRecord['instructions'][number]
|
|
108
|
+
export type AgentContextImport = AgentContextRecord['imports'][number]
|
|
109
|
+
export type AgentContextOmission = AgentContextRecord['omitted'][number]
|
|
110
|
+
export interface AgentContextContent {
|
|
111
|
+
oid: string
|
|
112
|
+
bytes: number
|
|
113
|
+
text?: string
|
|
114
|
+
base64?: string
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const contributorSchema = z.strictObject({ path: z.string(), bytes: z.number() })
|
|
118
|
+
export const agentContextDiagnosticSchema = z.discriminatedUnion('kind', [
|
|
119
|
+
z.strictObject({
|
|
120
|
+
kind: z.literal('oversize'),
|
|
121
|
+
commit: z.string(),
|
|
122
|
+
bound: z.number(),
|
|
123
|
+
bytes: z.number(),
|
|
124
|
+
largest: z.array(contributorSchema),
|
|
125
|
+
}),
|
|
126
|
+
z.strictObject({
|
|
127
|
+
kind: z.literal('raw-oversize'),
|
|
128
|
+
commit: z.string(),
|
|
129
|
+
rawBound: z.number(),
|
|
130
|
+
rawBytes: z.number(),
|
|
131
|
+
largest: z.array(contributorSchema),
|
|
132
|
+
}),
|
|
133
|
+
z.strictObject({ kind: z.literal('tree-unlistable'), commit: z.string(), directory: z.string() }),
|
|
134
|
+
z.strictObject({
|
|
135
|
+
kind: z.literal('capture-failed'),
|
|
136
|
+
commit: z.string().optional(),
|
|
137
|
+
message: z.string(),
|
|
138
|
+
}),
|
|
139
|
+
z.strictObject({ kind: z.literal('unavailable'), message: z.string() }),
|
|
140
|
+
])
|
|
141
|
+
export type AgentContextDiagnostic = z.infer<typeof agentContextDiagnosticSchema>
|
|
142
|
+
|
|
143
|
+
export const agentContextPointerSchema = z.strictObject({
|
|
144
|
+
version: z.literal(1),
|
|
145
|
+
branch: z.string().min(1),
|
|
146
|
+
/** The last base head fully handled; null before the first completes. */
|
|
147
|
+
observed: z.string().nullable(),
|
|
148
|
+
/** Input fingerprint of `observed`; null before the first completes. */
|
|
149
|
+
key: z.string().nullable(),
|
|
150
|
+
/** Paths outside the fingerprint's fixed set that the last assembly probed. */
|
|
151
|
+
dependencies: z.array(z.string()),
|
|
152
|
+
revision: z.number().int().nonnegative().optional(),
|
|
153
|
+
digest: z.string().optional(),
|
|
154
|
+
diagnostic: agentContextDiagnosticSchema.optional(),
|
|
155
|
+
pending: z
|
|
156
|
+
.strictObject({
|
|
157
|
+
head: z.string(),
|
|
158
|
+
captureId: z.string(),
|
|
159
|
+
slots: z.number().int().positive(),
|
|
160
|
+
})
|
|
161
|
+
.optional(),
|
|
162
|
+
})
|
|
163
|
+
export type AgentContextPointer = z.infer<typeof agentContextPointerSchema>
|
|
164
|
+
|
|
165
|
+
const cachePartSchema = z.strictObject({
|
|
166
|
+
oid: z.string().min(1),
|
|
167
|
+
offset: z.number().int().nonnegative(),
|
|
168
|
+
base64: z.string(),
|
|
169
|
+
})
|
|
170
|
+
export const agentContextCacheSlotSchema = z.strictObject({
|
|
171
|
+
version: z.literal(1),
|
|
172
|
+
captureId: z.string().min(1),
|
|
173
|
+
slot: z.number().int().nonnegative(),
|
|
174
|
+
parts: z.array(cachePartSchema),
|
|
175
|
+
})
|
|
176
|
+
export type AgentContextCachePart = z.infer<typeof cachePartSchema>
|
|
177
|
+
export type AgentContextCacheSlot = z.infer<typeof agentContextCacheSlotSchema>
|
|
178
|
+
|
|
179
|
+
const utf8 = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
|
|
180
|
+
const encoder = new TextEncoder()
|
|
181
|
+
|
|
182
|
+
/** `text` when the bytes are exact UTF-8 (BOM kept) and JSON escaping does
|
|
183
|
+
* not make them longer than base64 would; otherwise `base64`. Content then
|
|
184
|
+
* never takes more than 4/3 of its bytes in the record (control-heavy or
|
|
185
|
+
* NUL-filled text would otherwise escape to six bytes per byte). */
|
|
186
|
+
export function contentFields(bytes: Uint8Array): { text: string } | { base64: string } {
|
|
187
|
+
const base64 = Buffer.from(bytes).toString('base64')
|
|
188
|
+
try {
|
|
189
|
+
const text = utf8.decode(bytes)
|
|
190
|
+
if (
|
|
191
|
+
Buffer.from(encoder.encode(text)).equals(Buffer.from(bytes)) &&
|
|
192
|
+
Buffer.byteLength(JSON.stringify(text)) <= base64.length + 2
|
|
193
|
+
)
|
|
194
|
+
return { text }
|
|
195
|
+
} catch {
|
|
196
|
+
// Not UTF-8: fall through.
|
|
197
|
+
}
|
|
198
|
+
return { base64 }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The exact bytes a captured entry holds. */
|
|
202
|
+
export function contentBytes(entry: AgentContextContent): Uint8Array {
|
|
203
|
+
if (entry.text !== undefined) return encoder.encode(entry.text)
|
|
204
|
+
return new Uint8Array(Buffer.from(entry.base64 ?? '', 'base64'))
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Captured blob bytes: every skill file, instruction file and import. */
|
|
208
|
+
export function agentContextRawBytes(record: AgentContextRecord): number {
|
|
209
|
+
let total = 0
|
|
210
|
+
for (const skill of record.skills) for (const file of skill.files) total += file.bytes
|
|
211
|
+
for (const entry of record.instructions) total += entry.bytes
|
|
212
|
+
for (const entry of record.imports) total += entry.bytes
|
|
213
|
+
return total
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function sha256(text: string): string {
|
|
217
|
+
return createHash('sha256').update(text).digest('hex')
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Content identity: the canonical JSON without `commit`, so two commits
|
|
221
|
+
* whose captured content is identical share one digest. */
|
|
222
|
+
export function agentContextDigest(record: AgentContextRecord): string {
|
|
223
|
+
const { commit: _commit, ...rest } = record
|
|
224
|
+
return sha256(JSON.stringify(rest))
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export interface EncodedAgentContext {
|
|
228
|
+
content: Uint8Array
|
|
229
|
+
digest: string
|
|
230
|
+
/** Captured blob bytes. */
|
|
231
|
+
rawBytes: number
|
|
232
|
+
/** Uncompressed JSON bytes: what a reader decodes, held to the raw bound. */
|
|
233
|
+
jsonBytes: number
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function encodeAgentContext(record: AgentContextRecord): EncodedAgentContext {
|
|
237
|
+
const json = JSON.stringify(record)
|
|
238
|
+
return {
|
|
239
|
+
content: new Uint8Array(gzipSync(json)),
|
|
240
|
+
digest: agentContextDigest(record),
|
|
241
|
+
rawBytes: agentContextRawBytes(record),
|
|
242
|
+
jsonBytes: Buffer.byteLength(json),
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function gunzipBounded(bytes: Uint8Array, limit: number, what: string): string {
|
|
247
|
+
let raw: Buffer
|
|
248
|
+
try {
|
|
249
|
+
raw = gunzipSync(bytes, { maxOutputLength: limit })
|
|
250
|
+
} catch (error) {
|
|
251
|
+
if (error instanceof RangeError || (error as { code?: string }).code === 'ERR_BUFFER_TOO_LARGE')
|
|
252
|
+
throw new Error(`${what} decodes beyond ${limit} bytes`)
|
|
253
|
+
throw error
|
|
254
|
+
}
|
|
255
|
+
return raw.toString('utf8')
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Decode a stored record. Refuses one that inflates past
|
|
259
|
+
* `AGENT_CONTEXT_MAX_DECODED_BYTES`, so no reader can be made to allocate
|
|
260
|
+
* without bound by a hostile record. */
|
|
261
|
+
export function decodeAgentContext(
|
|
262
|
+
bytes: Uint8Array,
|
|
263
|
+
limit = AGENT_CONTEXT_MAX_DECODED_BYTES,
|
|
264
|
+
): AgentContextRecord {
|
|
265
|
+
return agentContextRecordSchema.parse(
|
|
266
|
+
JSON.parse(gunzipBounded(bytes, limit, 'agent-context record')),
|
|
267
|
+
)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function encodeAgentContextCache(slot: AgentContextCacheSlot): Uint8Array {
|
|
271
|
+
return new Uint8Array(gzipSync(JSON.stringify(slot)))
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function decodeAgentContextCache(bytes: Uint8Array): AgentContextCacheSlot {
|
|
275
|
+
return agentContextCacheSlotSchema.parse(
|
|
276
|
+
JSON.parse(gunzipBounded(bytes, 2 * AGENT_CONTEXT_CACHE_SLOT_PAYLOAD + MIB, 'cache slot')),
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function encodeAgentContextPointer(pointer: AgentContextPointer): string {
|
|
281
|
+
return JSON.stringify(agentContextPointerSchema.parse(pointer))
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function decodeAgentContextPointer(bytes: Uint8Array): AgentContextPointer {
|
|
285
|
+
return agentContextPointerSchema.parse(JSON.parse(new TextDecoder().decode(bytes)))
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** The latest agent-context record: one latest-revision artifact read, never
|
|
289
|
+
* a listing, so its cost is the same at 1 and at 1,000 stored revisions. */
|
|
290
|
+
export async function readAgentContext(
|
|
291
|
+
store: Pick<BuildStore, 'getRepoArtifact'>,
|
|
292
|
+
repo: string,
|
|
293
|
+
): Promise<{ record: AgentContextRecord; meta: RepositoryArtifactMeta } | null> {
|
|
294
|
+
const artifact = await store.getRepoArtifact(repo, AGENT_CONTEXT_ARTIFACT)
|
|
295
|
+
if (!artifact) return null
|
|
296
|
+
return { record: decodeAgentContext(artifact.content), meta: artifact.meta }
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export async function readAgentContextPointer(
|
|
300
|
+
store: Pick<BuildStore, 'getRepoArtifact'>,
|
|
301
|
+
repo: string,
|
|
302
|
+
): Promise<AgentContextPointer | null> {
|
|
303
|
+
const artifact = await store.getRepoArtifact(repo, AGENT_CONTEXT_HEAD_ARTIFACT)
|
|
304
|
+
return artifact ? decodeAgentContextPointer(artifact.content) : null
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function partCost(part: AgentContextCachePart): number {
|
|
308
|
+
return part.base64.length + AGENT_CONTEXT_CACHE_PART_OVERHEAD
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function slotCost(slot: AgentContextCacheSlot): number {
|
|
312
|
+
let total = 0
|
|
313
|
+
for (const part of slot.parts) total += partCost(part)
|
|
314
|
+
return total
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Pack blob bytes into continuation slots. `previous` are the live slots of
|
|
319
|
+
* the same capture, in slot order: every full one is kept untouched, the
|
|
320
|
+
* last one is rewritten only when it has room, and new slots are appended
|
|
321
|
+
* after it. A blob larger than the space left is split by `offset` across
|
|
322
|
+
* slots (at multiples of 3 bytes, so each part's base64 is exact). Slot count
|
|
323
|
+
* therefore follows the live bytes, never the number of invocations.
|
|
324
|
+
* `rewritten` lists the slot indexes the caller must write.
|
|
325
|
+
*/
|
|
326
|
+
export function packContinuation(
|
|
327
|
+
captureId: string,
|
|
328
|
+
previous: readonly AgentContextCacheSlot[],
|
|
329
|
+
blobs: readonly { oid: string; bytes: Uint8Array }[],
|
|
330
|
+
payload = AGENT_CONTEXT_CACHE_SLOT_PAYLOAD,
|
|
331
|
+
): { slots: AgentContextCacheSlot[]; rewritten: number[] } {
|
|
332
|
+
const minimum = AGENT_CONTEXT_CACHE_PART_OVERHEAD + 4
|
|
333
|
+
if (payload < minimum) throw new Error(`slot payload must be at least ${minimum}`)
|
|
334
|
+
const slots = previous.map((slot) => ({ ...slot, parts: [...slot.parts] }))
|
|
335
|
+
const rewritten = new Set<number>()
|
|
336
|
+
let current = slots.at(-1)
|
|
337
|
+
let used = current ? slotCost(current) : 0
|
|
338
|
+
const open = (): AgentContextCacheSlot => {
|
|
339
|
+
const slot: AgentContextCacheSlot = { version: 1, captureId, slot: slots.length, parts: [] }
|
|
340
|
+
slots.push(slot)
|
|
341
|
+
used = 0
|
|
342
|
+
return slot
|
|
343
|
+
}
|
|
344
|
+
for (const blob of blobs) {
|
|
345
|
+
let offset = 0
|
|
346
|
+
do {
|
|
347
|
+
if (current === undefined || payload - used < minimum) current = open()
|
|
348
|
+
const room = payload - used - AGENT_CONTEXT_CACHE_PART_OVERHEAD
|
|
349
|
+
const take = Math.min(blob.bytes.byteLength - offset, Math.floor(room / 4) * 3)
|
|
350
|
+
const part = {
|
|
351
|
+
oid: blob.oid,
|
|
352
|
+
offset,
|
|
353
|
+
base64: Buffer.from(blob.bytes.subarray(offset, offset + take)).toString('base64'),
|
|
354
|
+
}
|
|
355
|
+
current.parts.push(part)
|
|
356
|
+
used += partCost(part)
|
|
357
|
+
rewritten.add(current.slot)
|
|
358
|
+
offset += take
|
|
359
|
+
} while (offset < blob.bytes.byteLength)
|
|
360
|
+
}
|
|
361
|
+
return { slots, rewritten: [...rewritten].sort((a, b) => a - b) }
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Reassemble the blobs whose parts in `slots` cover them exactly, verified
|
|
365
|
+
* by `verify` (the git blob oid check). Partial or corrupt blobs are dropped. */
|
|
366
|
+
export function unpackContinuation(
|
|
367
|
+
slots: readonly AgentContextCacheSlot[],
|
|
368
|
+
verify: (oid: string, bytes: Uint8Array) => boolean,
|
|
369
|
+
): Map<string, Uint8Array> {
|
|
370
|
+
const parts = new Map<string, AgentContextCachePart[]>()
|
|
371
|
+
for (const slot of slots)
|
|
372
|
+
for (const part of slot.parts) {
|
|
373
|
+
const list = parts.get(part.oid) ?? []
|
|
374
|
+
list.push(part)
|
|
375
|
+
parts.set(part.oid, list)
|
|
376
|
+
}
|
|
377
|
+
const blobs = new Map<string, Uint8Array>()
|
|
378
|
+
for (const [oid, list] of parts) {
|
|
379
|
+
list.sort((a, b) => a.offset - b.offset)
|
|
380
|
+
const chunks: Buffer[] = []
|
|
381
|
+
let offset = 0
|
|
382
|
+
let contiguous = true
|
|
383
|
+
for (const part of list) {
|
|
384
|
+
if (part.offset !== offset) {
|
|
385
|
+
contiguous = false
|
|
386
|
+
break
|
|
387
|
+
}
|
|
388
|
+
const chunk = Buffer.from(part.base64, 'base64')
|
|
389
|
+
chunks.push(chunk)
|
|
390
|
+
offset += chunk.byteLength
|
|
391
|
+
}
|
|
392
|
+
if (!contiguous) continue
|
|
393
|
+
const bytes = new Uint8Array(Buffer.concat(chunks))
|
|
394
|
+
if (verify(oid, bytes)) blobs.set(oid, bytes)
|
|
395
|
+
}
|
|
396
|
+
return blobs
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function kib(bytes: number): string {
|
|
400
|
+
return `${(bytes / 1024).toFixed(1)} KiB`
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function largestList(largest: readonly { path: string; bytes: number }[]): string {
|
|
404
|
+
return largest.map((entry) => `${entry.path} (${kib(entry.bytes)})`).join(', ')
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export function formatAgentContextDiagnostic(diagnostic: AgentContextDiagnostic): string {
|
|
408
|
+
const at = (commit?: string) => (commit ? ` at ${commit.slice(0, 12)}` : '')
|
|
409
|
+
switch (diagnostic.kind) {
|
|
410
|
+
case 'oversize':
|
|
411
|
+
return (
|
|
412
|
+
`agent context${at(diagnostic.commit)} encodes to ${kib(diagnostic.bytes)}, over the ` +
|
|
413
|
+
`${kib(diagnostic.bound)} record bound; keeping the last good revision. Largest: ` +
|
|
414
|
+
largestList(diagnostic.largest)
|
|
415
|
+
)
|
|
416
|
+
case 'raw-oversize':
|
|
417
|
+
return (
|
|
418
|
+
`agent context${at(diagnostic.commit)} captures ${kib(diagnostic.rawBytes)} of files, over ` +
|
|
419
|
+
`the ${kib(diagnostic.rawBound)} raw input bound; keeping the last good revision. Largest: ` +
|
|
420
|
+
largestList(diagnostic.largest)
|
|
421
|
+
)
|
|
422
|
+
case 'tree-unlistable':
|
|
423
|
+
return (
|
|
424
|
+
`agent context${at(diagnostic.commit)} not captured: the forge cannot list directory ` +
|
|
425
|
+
`${diagnostic.directory === '' ? '/' : diagnostic.directory} completely; keeping the last good revision`
|
|
426
|
+
)
|
|
427
|
+
case 'capture-failed':
|
|
428
|
+
return `agent context capture${at(diagnostic.commit)} failed: ${diagnostic.message}`
|
|
429
|
+
case 'unavailable':
|
|
430
|
+
return `agent context capture unavailable: ${diagnostic.message}`
|
|
431
|
+
}
|
|
432
|
+
}
|