@h-rig/runtime 0.0.6-alpha.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 +27 -0
- package/dist/bin/rig-agent-dispatch.js +9615 -0
- package/dist/bin/rig-agent.js +9512 -0
- package/dist/bin/rig-browser-tool.js +269 -0
- package/dist/src/agent-mode.js +48 -0
- package/dist/src/baked-secrets.js +121 -0
- package/dist/src/binary-build-worker.js +312 -0
- package/dist/src/binary-run.js +540 -0
- package/dist/src/boundaries.js +1 -0
- package/dist/src/build-time-config.js +25 -0
- package/dist/src/control-plane/agent-roles.js +27 -0
- package/dist/src/control-plane/agent-wrapper.js +9621 -0
- package/dist/src/control-plane/authority-files.js +582 -0
- package/dist/src/control-plane/browser-contract.js +135 -0
- package/dist/src/control-plane/controlled-bash.js +1111 -0
- package/dist/src/control-plane/errors.js +13 -0
- package/dist/src/control-plane/harness-main.js +10828 -0
- package/dist/src/control-plane/hook-materializer.js +75 -0
- package/dist/src/control-plane/hooks/audit-trail.js +353 -0
- package/dist/src/control-plane/hooks/completion-verification.js +7552 -0
- package/dist/src/control-plane/hooks/import-guard.js +890 -0
- package/dist/src/control-plane/hooks/inject-context.js +4189 -0
- package/dist/src/control-plane/hooks/post-edit-lint.js +43 -0
- package/dist/src/control-plane/hooks/safety-guard.js +910 -0
- package/dist/src/control-plane/hooks/scope-guard.js +907 -0
- package/dist/src/control-plane/hooks/shared.js +44 -0
- package/dist/src/control-plane/hooks/submodule-branch.js +7797 -0
- package/dist/src/control-plane/hooks/task-runtime-start.js +7799 -0
- package/dist/src/control-plane/hooks/test-integrity-guard.js +891 -0
- package/dist/src/control-plane/materialize-task-config.js +453 -0
- package/dist/src/control-plane/memory-sync/cli.js +2019 -0
- package/dist/src/control-plane/memory-sync/db.js +753 -0
- package/dist/src/control-plane/memory-sync/embed.js +281 -0
- package/dist/src/control-plane/memory-sync/index.js +2049 -0
- package/dist/src/control-plane/memory-sync/query.js +294 -0
- package/dist/src/control-plane/memory-sync/read.js +784 -0
- package/dist/src/control-plane/memory-sync/types.js +6 -0
- package/dist/src/control-plane/memory-sync/write.js +1547 -0
- package/dist/src/control-plane/native/git-native.js +490 -0
- package/dist/src/control-plane/native/git-ops.js +2860 -0
- package/dist/src/control-plane/native/harness-cli.js +9721 -0
- package/dist/src/control-plane/native/pr-automation.js +373 -0
- package/dist/src/control-plane/native/profile-ops.js +481 -0
- package/dist/src/control-plane/native/repo-ops.js +2342 -0
- package/dist/src/control-plane/native/root-resolver.js +66 -0
- package/dist/src/control-plane/native/run-ops.js +3281 -0
- package/dist/src/control-plane/native/runtime-native-sidecar.js +299 -0
- package/dist/src/control-plane/native/runtime-native.js +392 -0
- package/dist/src/control-plane/native/scope-rules.js +17 -0
- package/dist/src/control-plane/native/task-ops.js +6320 -0
- package/dist/src/control-plane/native/task-state.js +1512 -0
- package/dist/src/control-plane/native/utils.js +535 -0
- package/dist/src/control-plane/native/validator-binaries.js +889 -0
- package/dist/src/control-plane/native/validator.js +2197 -0
- package/dist/src/control-plane/native/verifier.js +3249 -0
- package/dist/src/control-plane/native/workspace-ops.js +1635 -0
- package/dist/src/control-plane/plugin-host-context.js +334 -0
- package/dist/src/control-plane/project-main-pre-run-sync.js +630 -0
- package/dist/src/control-plane/provider/claude-stream-records.js +158 -0
- package/dist/src/control-plane/provider/codex-app-server.js +885 -0
- package/dist/src/control-plane/provider/codex-exec-records.js +203 -0
- package/dist/src/control-plane/provider/rig-task-run-skill.js +39 -0
- package/dist/src/control-plane/provider/runtime-instructions.js +96 -0
- package/dist/src/control-plane/remote.js +854 -0
- package/dist/src/control-plane/repos/index.js +473 -0
- package/dist/src/control-plane/repos/layout.js +124 -0
- package/dist/src/control-plane/repos/mirror/bootstrap.js +268 -0
- package/dist/src/control-plane/repos/mirror/refresh.js +398 -0
- package/dist/src/control-plane/repos/mirror/state.js +167 -0
- package/dist/src/control-plane/repos/registry.js +77 -0
- package/dist/src/control-plane/repos/types.js +1 -0
- package/dist/src/control-plane/runtime/agent-mode.js +48 -0
- package/dist/src/control-plane/runtime/baked-secrets.js +120 -0
- package/dist/src/control-plane/runtime/claude-tool-router-binary.js +343 -0
- package/dist/src/control-plane/runtime/claude-tool-router.js +520 -0
- package/dist/src/control-plane/runtime/context.js +216 -0
- package/dist/src/control-plane/runtime/events.js +218 -0
- package/dist/src/control-plane/runtime/guard-types.js +6 -0
- package/dist/src/control-plane/runtime/guard.js +880 -0
- package/dist/src/control-plane/runtime/image/fingerprint-sidecar.js +1194 -0
- package/dist/src/control-plane/runtime/image/index.js +2255 -0
- package/dist/src/control-plane/runtime/image-fingerprint-sidecar.js +1191 -0
- package/dist/src/control-plane/runtime/image.js +2255 -0
- package/dist/src/control-plane/runtime/index.js +8511 -0
- package/dist/src/control-plane/runtime/isolation/discovery.js +599 -0
- package/dist/src/control-plane/runtime/isolation/home.js +1217 -0
- package/dist/src/control-plane/runtime/isolation/index.js +8193 -0
- package/dist/src/control-plane/runtime/isolation/runner.js +2651 -0
- package/dist/src/control-plane/runtime/isolation/shared.js +501 -0
- package/dist/src/control-plane/runtime/isolation/toolchain.js +1892 -0
- package/dist/src/control-plane/runtime/isolation/types.js +1 -0
- package/dist/src/control-plane/runtime/isolation/worktree.js +509 -0
- package/dist/src/control-plane/runtime/isolation.js +8193 -0
- package/dist/src/control-plane/runtime/overlay.js +67 -0
- package/dist/src/control-plane/runtime/plugin-mode.js +41 -0
- package/dist/src/control-plane/runtime/plugins.js +1131 -0
- package/dist/src/control-plane/runtime/provisioning-env.js +220 -0
- package/dist/src/control-plane/runtime/queue.js +8358 -0
- package/dist/src/control-plane/runtime/rig-shell.js +205 -0
- package/dist/src/control-plane/runtime/rig-tools.js +182 -0
- package/dist/src/control-plane/runtime/runner-context.js +1 -0
- package/dist/src/control-plane/runtime/runtime-paths.js +184 -0
- package/dist/src/control-plane/runtime/sandbox/backend-bwrap.js +311 -0
- package/dist/src/control-plane/runtime/sandbox/backend-none.js +21 -0
- package/dist/src/control-plane/runtime/sandbox/backend-seatbelt.js +268 -0
- package/dist/src/control-plane/runtime/sandbox/backend.js +1718 -0
- package/dist/src/control-plane/runtime/sandbox/orchestrator.js +1745 -0
- package/dist/src/control-plane/runtime/sandbox/utils.js +137 -0
- package/dist/src/control-plane/runtime/sandbox-backend-bwrap.js +311 -0
- package/dist/src/control-plane/runtime/sandbox-backend-none.js +21 -0
- package/dist/src/control-plane/runtime/sandbox-backend-seatbelt.js +268 -0
- package/dist/src/control-plane/runtime/sandbox-backend.js +1718 -0
- package/dist/src/control-plane/runtime/sandbox-orchestrator.js +1745 -0
- package/dist/src/control-plane/runtime/sandbox-utils.js +137 -0
- package/dist/src/control-plane/runtime/snapshot/index.js +454 -0
- package/dist/src/control-plane/runtime/snapshot/sidecar.js +502 -0
- package/dist/src/control-plane/runtime/snapshot/task-run.js +1578 -0
- package/dist/src/control-plane/runtime/snapshot-sidecar.js +498 -0
- package/dist/src/control-plane/runtime/snapshot.js +454 -0
- package/dist/src/control-plane/runtime/task-run-snapshot.js +1578 -0
- package/dist/src/control-plane/runtime/tool-gateway.js +422 -0
- package/dist/src/control-plane/runtime/tooling/browser-tools.js +32 -0
- package/dist/src/control-plane/runtime/tooling/claude-router-binary.js +343 -0
- package/dist/src/control-plane/runtime/tooling/claude-router.js +524 -0
- package/dist/src/control-plane/runtime/tooling/file-tools.js +182 -0
- package/dist/src/control-plane/runtime/tooling/gateway.js +422 -0
- package/dist/src/control-plane/runtime/tooling/index.js +1290 -0
- package/dist/src/control-plane/runtime/tooling/shell.js +205 -0
- package/dist/src/control-plane/runtime/types.js +1 -0
- package/dist/src/control-plane/setup-version.js +14 -0
- package/dist/src/control-plane/state-sync/index.js +1509 -0
- package/dist/src/control-plane/state-sync/read.js +856 -0
- package/dist/src/control-plane/state-sync/reconcile.js +260 -0
- package/dist/src/control-plane/state-sync/repo.js +302 -0
- package/dist/src/control-plane/state-sync/types.js +111 -0
- package/dist/src/control-plane/state-sync/write.js +1469 -0
- package/dist/src/control-plane/task-fields.js +38 -0
- package/dist/src/control-plane/task-source-bootstrap.js +46 -0
- package/dist/src/control-plane/task-source.js +30 -0
- package/dist/src/control-plane/tasks/legacy-task-config-source.js +130 -0
- package/dist/src/control-plane/tasks/plugin-task-source.js +103 -0
- package/dist/src/control-plane/tasks/source-aware-task-config-source.js +611 -0
- package/dist/src/control-plane/tasks/source-lifecycle.js +1093 -0
- package/dist/src/control-plane/tasks/task-record-reader.js +9 -0
- package/dist/src/control-plane/validators/boundary/public-apis.js +107 -0
- package/dist/src/control-plane/validators/integration/_shared.js +51 -0
- package/dist/src/control-plane/validators/integration/adm-audit-http.js +85 -0
- package/dist/src/control-plane/validators/integration/adm-auth-http.js +78 -0
- package/dist/src/control-plane/validators/integration/adm-issuer-http.js +80 -0
- package/dist/src/control-plane/validators/integration/adm-migration.js +78 -0
- package/dist/src/control-plane/validators/integration/adm-scaffold.js +78 -0
- package/dist/src/control-plane/validators/runtime-registration.js +64 -0
- package/dist/src/control-plane/validators/shared.js +683 -0
- package/dist/src/events.js +218 -0
- package/dist/src/execution.js +35 -0
- package/dist/src/index.js +1633 -0
- package/dist/src/layout.js +145 -0
- package/dist/src/local-server.js +202 -0
- package/dist/src/plugins.js +329 -0
- package/dist/src/remote-http.js +83 -0
- package/dist/src/runtime-context.js +216 -0
- package/dist/src/types.js +1 -0
- package/native/darwin-arm64/bin/rig-git +0 -0
- package/native/darwin-arm64/bin/rig-shell +0 -0
- package/native/darwin-arm64/bin/rig-tools +0 -0
- package/native/darwin-arm64/lib/runtime-native-darwin-arm64.dylib +0 -0
- package/native/darwin-arm64/lib/runtime-native.dylib +0 -0
- package/native/darwin-arm64/manifest.json +1 -0
- package/native/linux-x64/bin/rig-git +0 -0
- package/native/linux-x64/bin/rig-shell +0 -0
- package/native/linux-x64/bin/rig-tools +0 -0
- package/native/linux-x64/lib/runtime-native-linux-x64.so +0 -0
- package/native/linux-x64/lib/runtime-native.so +0 -0
- package/native/linux-x64/manifest.json +1 -0
- package/package.json +74 -0
- package/skills/rig-task-run.md +71 -0
|
@@ -0,0 +1,1512 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/runtime/src/control-plane/native/task-state.ts
|
|
3
|
+
import { existsSync as existsSync9, readFileSync as readFileSync5, readdirSync, statSync as statSync2, writeFileSync as writeFileSync3 } from "fs";
|
|
4
|
+
import { basename as basename3, resolve as resolve9 } from "path";
|
|
5
|
+
|
|
6
|
+
// packages/runtime/src/control-plane/runtime/context.ts
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
8
|
+
import { dirname, resolve } from "path";
|
|
9
|
+
var RUNTIME_CONTEXT_ENV = "RIG_RUNTIME_CONTEXT_FILE";
|
|
10
|
+
var runtimeContextStringFields = [
|
|
11
|
+
"runtimeId",
|
|
12
|
+
"taskId",
|
|
13
|
+
"role",
|
|
14
|
+
"workspaceDir",
|
|
15
|
+
"stateDir",
|
|
16
|
+
"logsDir",
|
|
17
|
+
"sessionDir",
|
|
18
|
+
"sessionFile",
|
|
19
|
+
"policyFile",
|
|
20
|
+
"binDir",
|
|
21
|
+
"createdAt"
|
|
22
|
+
];
|
|
23
|
+
var runtimeContextArrayFields = ["scopes", "validation"];
|
|
24
|
+
var runtimeContextOptionalStringFields = [
|
|
25
|
+
"artifactRoot",
|
|
26
|
+
"hostProjectRoot",
|
|
27
|
+
"monorepoMainRoot",
|
|
28
|
+
"monorepoBaseRef",
|
|
29
|
+
"monorepoBaseCommit"
|
|
30
|
+
];
|
|
31
|
+
function loadRuntimeContext(path) {
|
|
32
|
+
const absPath = resolve(path);
|
|
33
|
+
if (!existsSync(absPath)) {
|
|
34
|
+
throw new Error(`RuntimeTaskContext file not found: ${absPath}`);
|
|
35
|
+
}
|
|
36
|
+
let raw;
|
|
37
|
+
try {
|
|
38
|
+
raw = JSON.parse(readFileSync(absPath, "utf8"));
|
|
39
|
+
} catch (err) {
|
|
40
|
+
throw new Error(`Failed to parse RuntimeTaskContext at ${absPath}: ${String(err)}`);
|
|
41
|
+
}
|
|
42
|
+
if (typeof raw !== "object" || raw === null) {
|
|
43
|
+
throw new Error(`RuntimeTaskContext at ${absPath} is not an object`);
|
|
44
|
+
}
|
|
45
|
+
const obj = raw;
|
|
46
|
+
for (const field of runtimeContextStringFields) {
|
|
47
|
+
if (typeof obj[field] !== "string" || obj[field].length === 0) {
|
|
48
|
+
throw new Error(`RuntimeTaskContext field "${field}" must be a non-empty string (at ${absPath})`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const field of runtimeContextArrayFields) {
|
|
52
|
+
if (!Array.isArray(obj[field])) {
|
|
53
|
+
throw new Error(`RuntimeTaskContext field "${field}" must be an array (at ${absPath})`);
|
|
54
|
+
}
|
|
55
|
+
if (!obj[field].every((entry) => typeof entry === "string")) {
|
|
56
|
+
throw new Error(`RuntimeTaskContext field "${field}" must be a string[] (at ${absPath})`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
for (const field of runtimeContextOptionalStringFields) {
|
|
60
|
+
if (field in obj && obj[field] !== undefined && typeof obj[field] !== "string") {
|
|
61
|
+
throw new Error(`RuntimeTaskContext field "${field}" must be a string when present (at ${absPath})`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (obj.browser !== undefined) {
|
|
65
|
+
if (typeof obj.browser !== "object" || obj.browser === null || Array.isArray(obj.browser)) {
|
|
66
|
+
throw new Error(`RuntimeTaskContext field "browser" must be an object when present (at ${absPath})`);
|
|
67
|
+
}
|
|
68
|
+
const browser = obj.browser;
|
|
69
|
+
for (const field of [
|
|
70
|
+
"preset",
|
|
71
|
+
"mode",
|
|
72
|
+
"stateDir",
|
|
73
|
+
"defaultProfile",
|
|
74
|
+
"effectiveProfile",
|
|
75
|
+
"defaultAttachUrl",
|
|
76
|
+
"effectiveAttachUrl",
|
|
77
|
+
"launchHelper",
|
|
78
|
+
"checkHelper",
|
|
79
|
+
"attachInfoHelper",
|
|
80
|
+
"e2eHelper",
|
|
81
|
+
"resetProfileHelper"
|
|
82
|
+
]) {
|
|
83
|
+
if (typeof browser[field] !== "string" || browser[field].length === 0) {
|
|
84
|
+
throw new Error(`RuntimeTaskContext field "browser.${field}" must be a non-empty string (at ${absPath})`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
for (const field of ["devCommand", "launchCommand", "checkCommand", "e2eCommand"]) {
|
|
88
|
+
if (browser[field] !== undefined && typeof browser[field] !== "string") {
|
|
89
|
+
throw new Error(`RuntimeTaskContext field "browser.${field}" must be a string when present (at ${absPath})`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (typeof browser.required !== "boolean") {
|
|
93
|
+
throw new Error(`RuntimeTaskContext field "browser.required" must be a boolean (at ${absPath})`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (obj.memory !== undefined) {
|
|
97
|
+
if (typeof obj.memory !== "object" || obj.memory === null || Array.isArray(obj.memory)) {
|
|
98
|
+
throw new Error(`RuntimeTaskContext field "memory" must be an object when present (at ${absPath})`);
|
|
99
|
+
}
|
|
100
|
+
const memory = obj.memory;
|
|
101
|
+
for (const field of ["canonicalPath", "canonicalRef", "canonicalBaseOid", "hydratedPath"]) {
|
|
102
|
+
if (typeof memory[field] !== "string" || memory[field].length === 0) {
|
|
103
|
+
throw new Error(`RuntimeTaskContext field "memory.${field}" must be a non-empty string (at ${absPath})`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (typeof memory.createdFresh !== "boolean") {
|
|
107
|
+
throw new Error(`RuntimeTaskContext field "memory.createdFresh" must be a boolean (at ${absPath})`);
|
|
108
|
+
}
|
|
109
|
+
if (typeof memory.retrieval !== "object" || memory.retrieval === null || Array.isArray(memory.retrieval)) {
|
|
110
|
+
throw new Error(`RuntimeTaskContext field "memory.retrieval" must be an object (at ${absPath})`);
|
|
111
|
+
}
|
|
112
|
+
const retrieval = memory.retrieval;
|
|
113
|
+
for (const field of ["topK", "lexicalWeight", "vectorWeight", "recencyWeight", "confidenceWeight"]) {
|
|
114
|
+
if (typeof retrieval[field] !== "number" || Number.isNaN(retrieval[field])) {
|
|
115
|
+
throw new Error(`RuntimeTaskContext field "memory.retrieval.${field}" must be a number (at ${absPath})`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (obj.initialDirtyFiles !== undefined) {
|
|
120
|
+
if (typeof obj.initialDirtyFiles !== "object" || obj.initialDirtyFiles === null || Array.isArray(obj.initialDirtyFiles)) {
|
|
121
|
+
throw new Error(`RuntimeTaskContext field "initialDirtyFiles" must be an object when present (at ${absPath})`);
|
|
122
|
+
}
|
|
123
|
+
const dirtyFiles = obj.initialDirtyFiles;
|
|
124
|
+
for (const key of ["project", "monorepo"]) {
|
|
125
|
+
if (dirtyFiles[key] === undefined) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (!Array.isArray(dirtyFiles[key]) || !dirtyFiles[key].every((entry) => typeof entry === "string")) {
|
|
129
|
+
throw new Error(`RuntimeTaskContext field "initialDirtyFiles.${key}" must be a string[] when present (at ${absPath})`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (obj.initialHeadCommits !== undefined) {
|
|
134
|
+
if (typeof obj.initialHeadCommits !== "object" || obj.initialHeadCommits === null || Array.isArray(obj.initialHeadCommits)) {
|
|
135
|
+
throw new Error(`RuntimeTaskContext field "initialHeadCommits" must be an object when present (at ${absPath})`);
|
|
136
|
+
}
|
|
137
|
+
const headCommits = obj.initialHeadCommits;
|
|
138
|
+
for (const key of ["project", "monorepo"]) {
|
|
139
|
+
if (headCommits[key] === undefined) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (typeof headCommits[key] !== "string") {
|
|
143
|
+
throw new Error(`RuntimeTaskContext field "initialHeadCommits.${key}" must be a string when present (at ${absPath})`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return obj;
|
|
148
|
+
}
|
|
149
|
+
function loadRuntimeContextFromEnv(env = process.env) {
|
|
150
|
+
const contextFile = env[RUNTIME_CONTEXT_ENV];
|
|
151
|
+
if (contextFile) {
|
|
152
|
+
return loadRuntimeContext(contextFile);
|
|
153
|
+
}
|
|
154
|
+
const inferred = findRuntimeContextFile(process.cwd());
|
|
155
|
+
if (!inferred) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
return loadRuntimeContext(inferred);
|
|
159
|
+
}
|
|
160
|
+
function findRuntimeContextFile(startPath) {
|
|
161
|
+
let current = resolve(startPath);
|
|
162
|
+
while (true) {
|
|
163
|
+
const candidate = resolve(current, "runtime-context.json");
|
|
164
|
+
if (existsSync(candidate) && isAgentRuntimeContextPath(candidate)) {
|
|
165
|
+
return candidate;
|
|
166
|
+
}
|
|
167
|
+
const parent = dirname(current);
|
|
168
|
+
if (parent === current) {
|
|
169
|
+
return "";
|
|
170
|
+
}
|
|
171
|
+
current = parent;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function isAgentRuntimeContextPath(path) {
|
|
175
|
+
const normalized = path.replace(/\\/g, "/");
|
|
176
|
+
return /\/\.rig\/runtime-context\.json$/.test(normalized);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// packages/runtime/src/control-plane/state-sync/types.ts
|
|
180
|
+
var SUPPORTED_TASK_STATE_SCHEMA_VERSION = 1;
|
|
181
|
+
var CANONICAL_TASK_LIFECYCLE_STATUSES = new Set([
|
|
182
|
+
"draft",
|
|
183
|
+
"open",
|
|
184
|
+
"ready",
|
|
185
|
+
"queued",
|
|
186
|
+
"in_progress",
|
|
187
|
+
"under_review",
|
|
188
|
+
"blocked",
|
|
189
|
+
"completed",
|
|
190
|
+
"cancelled"
|
|
191
|
+
]);
|
|
192
|
+
function normalizeTaskLifecycleStatus(status) {
|
|
193
|
+
switch (status) {
|
|
194
|
+
case "draft":
|
|
195
|
+
case "open":
|
|
196
|
+
case "ready":
|
|
197
|
+
case "queued":
|
|
198
|
+
case "in_progress":
|
|
199
|
+
case "under_review":
|
|
200
|
+
case "blocked":
|
|
201
|
+
case "completed":
|
|
202
|
+
case "cancelled":
|
|
203
|
+
return status;
|
|
204
|
+
case "closed":
|
|
205
|
+
return "completed";
|
|
206
|
+
case "running":
|
|
207
|
+
return "in_progress";
|
|
208
|
+
case "failed":
|
|
209
|
+
return "ready";
|
|
210
|
+
default:
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function normalizeTaskStateMetadataStatus(status) {
|
|
215
|
+
if (CANONICAL_TASK_LIFECYCLE_STATUSES.has(status)) {
|
|
216
|
+
return status;
|
|
217
|
+
}
|
|
218
|
+
switch (status) {
|
|
219
|
+
case "closed":
|
|
220
|
+
return "completed";
|
|
221
|
+
case "running":
|
|
222
|
+
return "in_progress";
|
|
223
|
+
case "failed":
|
|
224
|
+
return "ready";
|
|
225
|
+
default:
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function canonicalizeTaskStateMetadata(raw) {
|
|
230
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
const metadata = raw;
|
|
234
|
+
const claimId = typeof metadata.claimId === "string" && metadata.claimId.length > 0 ? metadata.claimId : undefined;
|
|
235
|
+
const status = normalizeTaskStateMetadataStatus(metadata.status);
|
|
236
|
+
if (!status) {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
...claimId ? { claimId } : {},
|
|
241
|
+
status,
|
|
242
|
+
...typeof metadata.ownerId === "string" && metadata.ownerId.length > 0 ? { ownerId: metadata.ownerId } : {},
|
|
243
|
+
...typeof metadata.claimedAt === "string" && metadata.claimedAt.length > 0 ? { claimedAt: metadata.claimedAt } : {},
|
|
244
|
+
...typeof metadata.lastEvidenceAt === "string" && metadata.lastEvidenceAt.length > 0 ? { lastEvidenceAt: metadata.lastEvidenceAt } : {},
|
|
245
|
+
...typeof metadata.runId === "string" && metadata.runId.length > 0 ? { runId: metadata.runId } : {},
|
|
246
|
+
...typeof metadata.branchName === "string" && metadata.branchName.length > 0 ? { branchName: metadata.branchName } : {},
|
|
247
|
+
...typeof metadata.prNumber === "number" ? { prNumber: metadata.prNumber } : {},
|
|
248
|
+
...typeof metadata.prUrl === "string" && metadata.prUrl.length > 0 ? { prUrl: metadata.prUrl } : {},
|
|
249
|
+
...typeof metadata.reviewState === "string" && metadata.reviewState.length > 0 ? { reviewState: metadata.reviewState } : {},
|
|
250
|
+
...typeof metadata.blockerReason === "string" && metadata.blockerReason.length > 0 ? { blockerReason: metadata.blockerReason } : {},
|
|
251
|
+
...typeof metadata.sourceCommit === "string" && metadata.sourceCommit.length > 0 ? { sourceCommit: metadata.sourceCommit } : {}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function discardMismatchedTaskStateMetadata(input) {
|
|
255
|
+
input.taskId;
|
|
256
|
+
const canonicalMetadata = canonicalizeTaskStateMetadata(input.metadata);
|
|
257
|
+
if (!canonicalMetadata || !input.lifecycleStatus) {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
const metadataStatus = canonicalMetadata.status ?? null;
|
|
261
|
+
if (metadataStatus && metadataStatus !== input.lifecycleStatus) {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
return canonicalMetadata;
|
|
265
|
+
}
|
|
266
|
+
function readTaskStateMetadataEnvelope(raw) {
|
|
267
|
+
if (!raw || typeof raw !== "object") {
|
|
268
|
+
return { schemaVersion: SUPPORTED_TASK_STATE_SCHEMA_VERSION, supported: true, baseTrackerCommit: null, tasks: {} };
|
|
269
|
+
}
|
|
270
|
+
const envelope = raw;
|
|
271
|
+
const schemaVersion = typeof envelope.schemaVersion === "number" ? envelope.schemaVersion : SUPPORTED_TASK_STATE_SCHEMA_VERSION;
|
|
272
|
+
if (schemaVersion !== SUPPORTED_TASK_STATE_SCHEMA_VERSION) {
|
|
273
|
+
return { schemaVersion, supported: false, baseTrackerCommit: null, tasks: {} };
|
|
274
|
+
}
|
|
275
|
+
const rawTasks = envelope.tasks && typeof envelope.tasks === "object" && !Array.isArray(envelope.tasks) ? envelope.tasks : {};
|
|
276
|
+
const tasks = Object.fromEntries(Object.entries(rawTasks).map(([taskId, metadata]) => [taskId, canonicalizeTaskStateMetadata(metadata)]).filter((entry) => entry[1] != null));
|
|
277
|
+
return {
|
|
278
|
+
schemaVersion,
|
|
279
|
+
supported: true,
|
|
280
|
+
baseTrackerCommit: typeof envelope.baseTrackerCommit === "string" && envelope.baseTrackerCommit.length > 0 ? envelope.baseTrackerCommit : null,
|
|
281
|
+
tasks
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
// packages/runtime/src/control-plane/state-sync/read.ts
|
|
285
|
+
import { existsSync as existsSync8, readFileSync as readFileSync4 } from "fs";
|
|
286
|
+
import { resolve as resolve8 } from "path";
|
|
287
|
+
|
|
288
|
+
// packages/runtime/src/control-plane/native/git-native.ts
|
|
289
|
+
import { chmodSync, copyFileSync, existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, renameSync, rmSync, writeFileSync as writeFileSync2 } from "fs";
|
|
290
|
+
import { tmpdir } from "os";
|
|
291
|
+
import { dirname as dirname2, isAbsolute, resolve as resolve2 } from "path";
|
|
292
|
+
import { createHash } from "crypto";
|
|
293
|
+
var sharedGitNativeOutputDir = resolve2(tmpdir(), "rig-native");
|
|
294
|
+
var sharedGitNativeOutputPath = resolve2(sharedGitNativeOutputDir, `rig-git-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
|
|
295
|
+
var trackerCommandUsageProbe = "usage: rig-git fetch-ref <repo-path> <remote> <branch>";
|
|
296
|
+
function temporaryGitBinaryOutputPath(outputPath) {
|
|
297
|
+
const suffix = process.platform === "win32" ? ".exe" : "";
|
|
298
|
+
return resolve2(dirname2(outputPath), `.rig-git-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}${suffix}`);
|
|
299
|
+
}
|
|
300
|
+
function publishGitBinary(tempOutputPath, outputPath) {
|
|
301
|
+
try {
|
|
302
|
+
renameSync(tempOutputPath, outputPath);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
if (process.platform === "win32" && existsSync2(outputPath)) {
|
|
305
|
+
rmSync(outputPath, { force: true });
|
|
306
|
+
renameSync(tempOutputPath, outputPath);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
function runtimeRigGitFileName() {
|
|
313
|
+
return `rig-git${process.platform === "win32" ? ".exe" : ""}`;
|
|
314
|
+
}
|
|
315
|
+
function rigGitSourceCandidates() {
|
|
316
|
+
const execDir = process.execPath?.trim() ? dirname2(process.execPath.trim()) : "";
|
|
317
|
+
const cwd = process.cwd()?.trim() || "";
|
|
318
|
+
const projectRoot = process.env.PROJECT_RIG_ROOT?.trim() || "";
|
|
319
|
+
const hostProjectRoot = process.env.RIG_HOST_PROJECT_ROOT?.trim() || "";
|
|
320
|
+
const moduleRelativeSource = resolve2(import.meta.dir, "../../../native/rig-git.zig");
|
|
321
|
+
return [...new Set([
|
|
322
|
+
process.env.RIG_NATIVE_GIT_SOURCE?.trim() || "",
|
|
323
|
+
moduleRelativeSource,
|
|
324
|
+
projectRoot ? resolve2(projectRoot, "packages/runtime/native/rig-git.zig") : "",
|
|
325
|
+
hostProjectRoot ? resolve2(hostProjectRoot, "packages/runtime/native/rig-git.zig") : "",
|
|
326
|
+
cwd ? resolve2(cwd, "packages/runtime/native/rig-git.zig") : "",
|
|
327
|
+
execDir ? resolve2(execDir, "..", "..", "packages/runtime/native/rig-git.zig") : "",
|
|
328
|
+
execDir ? resolve2(execDir, "..", "native", "rig-git.zig") : ""
|
|
329
|
+
].filter(Boolean))];
|
|
330
|
+
}
|
|
331
|
+
function nativePackageBinaryCandidates(fromDir, fileName) {
|
|
332
|
+
const candidates = [];
|
|
333
|
+
let cursor = resolve2(fromDir);
|
|
334
|
+
for (let index = 0;index < 8; index += 1) {
|
|
335
|
+
candidates.push(resolve2(cursor, "native", `${process.platform}-${process.arch}`, fileName), resolve2(cursor, "native", `${process.platform}-${process.arch}`, "bin", fileName), resolve2(cursor, "native", fileName), resolve2(cursor, "native", "bin", fileName));
|
|
336
|
+
const parent = dirname2(cursor);
|
|
337
|
+
if (parent === cursor)
|
|
338
|
+
break;
|
|
339
|
+
cursor = parent;
|
|
340
|
+
}
|
|
341
|
+
return candidates;
|
|
342
|
+
}
|
|
343
|
+
function rigGitBinaryCandidates() {
|
|
344
|
+
const execDir = process.execPath?.trim() ? dirname2(process.execPath.trim()) : "";
|
|
345
|
+
const fileName = runtimeRigGitFileName();
|
|
346
|
+
const explicit = process.env.RIG_NATIVE_GIT_BIN?.trim() || "";
|
|
347
|
+
return [...new Set([
|
|
348
|
+
explicit,
|
|
349
|
+
...nativePackageBinaryCandidates(import.meta.dir, fileName),
|
|
350
|
+
execDir ? resolve2(execDir, fileName) : "",
|
|
351
|
+
execDir ? resolve2(execDir, "..", fileName) : "",
|
|
352
|
+
execDir ? resolve2(execDir, "..", "bin", fileName) : "",
|
|
353
|
+
sharedGitNativeOutputPath
|
|
354
|
+
].filter(Boolean))];
|
|
355
|
+
}
|
|
356
|
+
function resolveGitSourcePath() {
|
|
357
|
+
for (const candidate of rigGitSourceCandidates()) {
|
|
358
|
+
if (candidate && existsSync2(candidate)) {
|
|
359
|
+
return candidate;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
function resolveGitBinaryPath() {
|
|
365
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
for (const candidate of rigGitBinaryCandidates()) {
|
|
369
|
+
if (candidate && existsSync2(candidate)) {
|
|
370
|
+
return candidate;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
function preferredGitBinaryOutputPath() {
|
|
376
|
+
const explicit = process.env.RIG_NATIVE_GIT_BIN?.trim() || "";
|
|
377
|
+
return explicit || sharedGitNativeOutputPath;
|
|
378
|
+
}
|
|
379
|
+
function binarySupportsTrackerCommandsSync(binaryPath) {
|
|
380
|
+
try {
|
|
381
|
+
const probe = Bun.spawnSync([binaryPath, "fetch-ref", "."], {
|
|
382
|
+
stdout: "pipe",
|
|
383
|
+
stderr: "pipe"
|
|
384
|
+
});
|
|
385
|
+
const stdout = probe.stdout.toString().trim();
|
|
386
|
+
const stderr = probe.stderr.toString().trim();
|
|
387
|
+
if (stdout.includes('"error":"unknown command"')) {
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
return probe.exitCode === 2 && stderr.includes(trackerCommandUsageProbe);
|
|
391
|
+
} catch {
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function nativeBuildManifestPath(outputPath) {
|
|
396
|
+
return `${outputPath}.build-manifest.json`;
|
|
397
|
+
}
|
|
398
|
+
function hasMatchingNativeBuildManifestSync(manifestPath, buildKey) {
|
|
399
|
+
if (!existsSync2(manifestPath)) {
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
try {
|
|
403
|
+
const manifest = JSON.parse(readFileSync2(manifestPath, "utf8"));
|
|
404
|
+
return manifest.version === 1 && manifest.buildKey === buildKey;
|
|
405
|
+
} catch {
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function sha256FileSync(path) {
|
|
410
|
+
return createHash("sha256").update(readFileSync2(path)).digest("hex");
|
|
411
|
+
}
|
|
412
|
+
function ensureRigGitBinaryPathSync(outputPath = preferredGitBinaryOutputPath()) {
|
|
413
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
414
|
+
throw new Error("Zig native git is disabled via RIG_DISABLE_ZIG_NATIVE=1");
|
|
415
|
+
}
|
|
416
|
+
const sourcePath = resolveGitSourcePath();
|
|
417
|
+
if (!sourcePath) {
|
|
418
|
+
const binaryPath = resolveGitBinaryPath();
|
|
419
|
+
if (binaryPath) {
|
|
420
|
+
return binaryPath;
|
|
421
|
+
}
|
|
422
|
+
throw new Error("rig-git.zig source file not found.");
|
|
423
|
+
}
|
|
424
|
+
const zigBinary = Bun.which("zig");
|
|
425
|
+
if (!zigBinary) {
|
|
426
|
+
throw new Error("zig is required to build native Rig git tools.");
|
|
427
|
+
}
|
|
428
|
+
mkdirSync2(dirname2(outputPath), { recursive: true });
|
|
429
|
+
const sourceDigest = sha256FileSync(sourcePath);
|
|
430
|
+
const buildKey = JSON.stringify({
|
|
431
|
+
version: 1,
|
|
432
|
+
zigBinary,
|
|
433
|
+
platform: process.platform,
|
|
434
|
+
arch: process.arch,
|
|
435
|
+
sourcePath,
|
|
436
|
+
sourceDigest
|
|
437
|
+
});
|
|
438
|
+
const manifestPath = nativeBuildManifestPath(outputPath);
|
|
439
|
+
const needsBuild = !existsSync2(outputPath) || !hasMatchingNativeBuildManifestSync(manifestPath, buildKey) || !binarySupportsTrackerCommandsSync(outputPath);
|
|
440
|
+
if (!needsBuild) {
|
|
441
|
+
chmodSync(outputPath, 493);
|
|
442
|
+
return outputPath;
|
|
443
|
+
}
|
|
444
|
+
const tempOutputPath = temporaryGitBinaryOutputPath(outputPath);
|
|
445
|
+
const build = Bun.spawnSync([
|
|
446
|
+
zigBinary,
|
|
447
|
+
"build-exe",
|
|
448
|
+
sourcePath,
|
|
449
|
+
"-O",
|
|
450
|
+
"ReleaseFast",
|
|
451
|
+
`-femit-bin=${tempOutputPath}`
|
|
452
|
+
], {
|
|
453
|
+
cwd: dirname2(sourcePath),
|
|
454
|
+
stdout: "pipe",
|
|
455
|
+
stderr: "pipe"
|
|
456
|
+
});
|
|
457
|
+
if (build.exitCode !== 0 || !existsSync2(tempOutputPath)) {
|
|
458
|
+
const stderr = build.stderr.toString().trim();
|
|
459
|
+
const stdout = build.stdout.toString().trim();
|
|
460
|
+
const details = [stderr, stdout].filter(Boolean).join(`
|
|
461
|
+
`);
|
|
462
|
+
throw new Error(`Failed to build native Rig git tools: ${details || `zig exited with code ${build.exitCode}`}`);
|
|
463
|
+
}
|
|
464
|
+
chmodSync(tempOutputPath, 493);
|
|
465
|
+
if (existsSync2(outputPath) && hasMatchingNativeBuildManifestSync(manifestPath, buildKey)) {
|
|
466
|
+
rmSync(tempOutputPath, { force: true });
|
|
467
|
+
chmodSync(outputPath, 493);
|
|
468
|
+
return outputPath;
|
|
469
|
+
}
|
|
470
|
+
publishGitBinary(tempOutputPath, outputPath);
|
|
471
|
+
if (!binarySupportsTrackerCommandsSync(outputPath)) {
|
|
472
|
+
rmSync(outputPath, { force: true });
|
|
473
|
+
throw new Error("Failed to build native Rig git tools: tracker command probe failed");
|
|
474
|
+
}
|
|
475
|
+
writeFileSync2(manifestPath, `${JSON.stringify({ version: 1, buildKey }, null, 2)}
|
|
476
|
+
`, "utf8");
|
|
477
|
+
return outputPath;
|
|
478
|
+
}
|
|
479
|
+
function runGitNative(command, args) {
|
|
480
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
481
|
+
return { ok: false, error: "rig-git native disabled" };
|
|
482
|
+
}
|
|
483
|
+
const trackerCommand = command === "fetch-ref" || command === "read-blob-at-ref" || command === "write-tree-commit" || command === "push-ref-with-lease";
|
|
484
|
+
let binaryPath = null;
|
|
485
|
+
if (trackerCommand) {
|
|
486
|
+
try {
|
|
487
|
+
binaryPath = ensureRigGitBinaryPathSync(preferredGitBinaryOutputPath());
|
|
488
|
+
} catch (error) {
|
|
489
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
490
|
+
if (message.includes("rig-git.zig source file not found")) {
|
|
491
|
+
return { ok: false, error: "rig-git binary not found" };
|
|
492
|
+
}
|
|
493
|
+
return { ok: false, error: message };
|
|
494
|
+
}
|
|
495
|
+
} else {
|
|
496
|
+
const explicitBinaryPath = process.env.RIG_NATIVE_GIT_BIN?.trim() || "";
|
|
497
|
+
binaryPath = explicitBinaryPath && existsSync2(explicitBinaryPath) ? explicitBinaryPath : !explicitBinaryPath ? resolveGitBinaryPath() : null;
|
|
498
|
+
if (!binaryPath) {
|
|
499
|
+
try {
|
|
500
|
+
binaryPath = ensureRigGitBinaryPathSync(preferredGitBinaryOutputPath());
|
|
501
|
+
} catch (error) {
|
|
502
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
503
|
+
if (message.includes("rig-git.zig source file not found")) {
|
|
504
|
+
return { ok: false, error: "rig-git binary not found" };
|
|
505
|
+
}
|
|
506
|
+
return { ok: false, error: message };
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
try {
|
|
511
|
+
const proc = Bun.spawnSync([binaryPath, command, ...args], {
|
|
512
|
+
stdout: "pipe",
|
|
513
|
+
stderr: "pipe",
|
|
514
|
+
env: process.env
|
|
515
|
+
});
|
|
516
|
+
if (proc.exitCode !== 0) {
|
|
517
|
+
const stdoutText = proc.stdout.toString().trim();
|
|
518
|
+
if (stdoutText) {
|
|
519
|
+
try {
|
|
520
|
+
const parsed = JSON.parse(stdoutText);
|
|
521
|
+
if (!parsed.ok) {
|
|
522
|
+
return parsed;
|
|
523
|
+
}
|
|
524
|
+
} catch {}
|
|
525
|
+
}
|
|
526
|
+
const errText = proc.stderr.toString().trim() || `exit code ${proc.exitCode}`;
|
|
527
|
+
return { ok: false, error: errText };
|
|
528
|
+
}
|
|
529
|
+
const output = proc.stdout.toString().trim();
|
|
530
|
+
return JSON.parse(output);
|
|
531
|
+
} catch (err) {
|
|
532
|
+
return { ok: false, error: String(err) };
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
function requireGitNative(command, args) {
|
|
536
|
+
const result = runGitNative(command, args);
|
|
537
|
+
if (!result.ok) {
|
|
538
|
+
throw new Error(`rig-git ${command} failed: ${result.error}`);
|
|
539
|
+
}
|
|
540
|
+
return result;
|
|
541
|
+
}
|
|
542
|
+
function requireGitNativeString(command, args) {
|
|
543
|
+
const result = requireGitNative(command, args);
|
|
544
|
+
if ("value" in result && typeof result.value === "string") {
|
|
545
|
+
return result.value;
|
|
546
|
+
}
|
|
547
|
+
throw new Error(`rig-git ${command} returned an unexpected result payload`);
|
|
548
|
+
}
|
|
549
|
+
function nativeFetchRef(repoPath, remote, branch) {
|
|
550
|
+
return requireGitNativeString("fetch-ref", [repoPath, remote, branch]);
|
|
551
|
+
}
|
|
552
|
+
function nativeReadBlobAtRef(repoPath, ref, path) {
|
|
553
|
+
const requestDir = resolve2(sharedGitNativeOutputDir, "reads", `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`);
|
|
554
|
+
mkdirSync2(requestDir, { recursive: true });
|
|
555
|
+
const outputPath = resolve2(requestDir, "blob.txt");
|
|
556
|
+
try {
|
|
557
|
+
requireGitNative("read-blob-at-ref", [repoPath, ref, path, outputPath]);
|
|
558
|
+
return readFileSync2(outputPath, "utf8");
|
|
559
|
+
} finally {
|
|
560
|
+
rmSync(requestDir, { recursive: true, force: true });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// packages/runtime/src/control-plane/native/utils.ts
|
|
565
|
+
import { existsSync as existsSync5, readFileSync as readFileSync3 } from "fs";
|
|
566
|
+
import { resolve as resolve5 } from "path";
|
|
567
|
+
|
|
568
|
+
// packages/runtime/src/layout.ts
|
|
569
|
+
import { existsSync as existsSync3 } from "fs";
|
|
570
|
+
import { basename, dirname as dirname3, resolve as resolve3 } from "path";
|
|
571
|
+
var RIG_DEFINITION_DIRNAME = "rig";
|
|
572
|
+
var RIG_ARTIFACTS_DIRNAME = "artifacts";
|
|
573
|
+
function resolveMonorepoRoot(projectRoot) {
|
|
574
|
+
const normalizedProjectRoot = resolve3(projectRoot);
|
|
575
|
+
const explicit = process.env.MONOREPO_ROOT?.trim();
|
|
576
|
+
if (explicit) {
|
|
577
|
+
const explicitRoot = resolve3(explicit);
|
|
578
|
+
const explicitParent = dirname3(explicitRoot);
|
|
579
|
+
if (basename(explicitParent) === ".worktrees") {
|
|
580
|
+
const owner = dirname3(explicitParent);
|
|
581
|
+
const ownerHasGit = existsSync3(resolve3(owner, ".git"));
|
|
582
|
+
const ownerHasTaskConfig = existsSync3(resolve3(owner, ".rig", "task-config.json"));
|
|
583
|
+
const ownerHasRigConfig = existsSync3(resolve3(owner, "rig.config.ts"));
|
|
584
|
+
if (ownerHasGit && (ownerHasTaskConfig || ownerHasRigConfig)) {
|
|
585
|
+
return owner;
|
|
586
|
+
}
|
|
587
|
+
throw new Error(`MONOREPO_ROOT points to worktree ${explicitRoot}, but the owner checkout is incomplete at ${owner}.`);
|
|
588
|
+
}
|
|
589
|
+
if (!existsSync3(resolve3(explicitRoot, ".git"))) {
|
|
590
|
+
throw new Error(`MONOREPO_ROOT points to ${explicitRoot}, but no git checkout was found there.`);
|
|
591
|
+
}
|
|
592
|
+
const hasTaskConfig = existsSync3(resolve3(explicitRoot, ".rig", "task-config.json"));
|
|
593
|
+
const hasRigConfig = existsSync3(resolve3(explicitRoot, "rig.config.ts"));
|
|
594
|
+
if (!hasTaskConfig && !hasRigConfig) {
|
|
595
|
+
throw new Error(`MONOREPO_ROOT points to ${explicitRoot}, but neither .rig/task-config.json nor rig.config.ts exists there.`);
|
|
596
|
+
}
|
|
597
|
+
return explicitRoot;
|
|
598
|
+
}
|
|
599
|
+
const projectParent = dirname3(normalizedProjectRoot);
|
|
600
|
+
if (basename(projectParent) === ".worktrees") {
|
|
601
|
+
const worktreeOwner = dirname3(projectParent);
|
|
602
|
+
const ownerHasGit = existsSync3(resolve3(worktreeOwner, ".git"));
|
|
603
|
+
const ownerHasTaskConfig = existsSync3(resolve3(worktreeOwner, ".rig", "task-config.json"));
|
|
604
|
+
const ownerHasRigConfig = existsSync3(resolve3(worktreeOwner, "rig.config.ts"));
|
|
605
|
+
if (ownerHasGit && (ownerHasTaskConfig || ownerHasRigConfig)) {
|
|
606
|
+
return worktreeOwner;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
return normalizedProjectRoot;
|
|
610
|
+
}
|
|
611
|
+
function resolveRuntimeWorkspaceLayout(workspaceDir) {
|
|
612
|
+
const root = resolve3(workspaceDir);
|
|
613
|
+
const rigRoot = resolve3(root, ".rig");
|
|
614
|
+
const logsDir = resolve3(rigRoot, "logs");
|
|
615
|
+
const stateDir = resolve3(rigRoot, "state");
|
|
616
|
+
const runtimeDir = resolve3(rigRoot, "runtime");
|
|
617
|
+
const binDir = resolve3(rigRoot, "bin");
|
|
618
|
+
return {
|
|
619
|
+
workspaceDir: root,
|
|
620
|
+
rigRoot,
|
|
621
|
+
stateDir,
|
|
622
|
+
logsDir,
|
|
623
|
+
artifactsRoot: resolve3(root, RIG_ARTIFACTS_DIRNAME),
|
|
624
|
+
runtimeDir,
|
|
625
|
+
homeDir: resolve3(rigRoot, "home"),
|
|
626
|
+
tmpDir: resolve3(rigRoot, "tmp"),
|
|
627
|
+
cacheDir: resolve3(rigRoot, "cache"),
|
|
628
|
+
sessionDir: resolve3(rigRoot, "session"),
|
|
629
|
+
binDir,
|
|
630
|
+
distDir: resolve3(rigRoot, "dist"),
|
|
631
|
+
pluginBinDir: resolve3(binDir, "plugins"),
|
|
632
|
+
contextPath: resolve3(rigRoot, "runtime-context.json"),
|
|
633
|
+
controlPlaneEventsFile: resolve3(logsDir, "control-plane.events.jsonl")
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
function resolveActiveRuntimeWorkspaceRoot(monorepoRoot) {
|
|
637
|
+
const explicit = process.env.RIG_TASK_WORKSPACE?.trim();
|
|
638
|
+
if (!explicit) {
|
|
639
|
+
throw new Error("No active runtime workspace. Set RIG_TASK_WORKSPACE or provision a task runtime first.");
|
|
640
|
+
}
|
|
641
|
+
return resolve3(explicit);
|
|
642
|
+
}
|
|
643
|
+
function resolveRigLayout(projectRoot) {
|
|
644
|
+
const monorepoRoot = resolveMonorepoRoot(projectRoot);
|
|
645
|
+
const definitionRoot = resolve3(projectRoot, RIG_DEFINITION_DIRNAME);
|
|
646
|
+
const runtimeWorkspaceRoot = resolveActiveRuntimeWorkspaceRoot(monorepoRoot);
|
|
647
|
+
const runtimeLayout = resolveRuntimeWorkspaceLayout(runtimeWorkspaceRoot);
|
|
648
|
+
const policyDir = resolve3(definitionRoot, "policy");
|
|
649
|
+
return {
|
|
650
|
+
projectRoot,
|
|
651
|
+
monorepoRoot,
|
|
652
|
+
definitionRoot,
|
|
653
|
+
runtimeWorkspaceRoot,
|
|
654
|
+
stateRoot: runtimeLayout.rigRoot,
|
|
655
|
+
artifactsRoot: runtimeLayout.artifactsRoot,
|
|
656
|
+
configPath: resolve3(definitionRoot, "config.sh"),
|
|
657
|
+
taskConfigPath: resolve3(runtimeWorkspaceRoot, ".rig", "task-config.json"),
|
|
658
|
+
policyDir,
|
|
659
|
+
policyFile: resolve3(policyDir, "policy.json"),
|
|
660
|
+
pluginsDir: resolve3(definitionRoot, "plugins"),
|
|
661
|
+
hooksDir: resolve3(definitionRoot, "hooks"),
|
|
662
|
+
toolsDir: resolve3(definitionRoot, "tools"),
|
|
663
|
+
templatesDir: resolve3(definitionRoot, "templates"),
|
|
664
|
+
validationDir: resolve3(definitionRoot, "validation"),
|
|
665
|
+
stateDir: runtimeLayout.stateDir,
|
|
666
|
+
logsDir: runtimeLayout.logsDir,
|
|
667
|
+
notificationsDir: resolve3(definitionRoot, "notifications"),
|
|
668
|
+
runtimeDir: runtimeLayout.runtimeDir,
|
|
669
|
+
distDir: runtimeLayout.distDir,
|
|
670
|
+
binDir: runtimeLayout.binDir,
|
|
671
|
+
pluginBinDir: runtimeLayout.pluginBinDir,
|
|
672
|
+
keybindingsPath: resolve3(definitionRoot, "keybindings.json"),
|
|
673
|
+
controlPlaneEventsFile: runtimeLayout.controlPlaneEventsFile
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// packages/runtime/src/control-plane/native/runtime-native.ts
|
|
678
|
+
import { dlopen, ptr, suffix, toBuffer } from "bun:ffi";
|
|
679
|
+
import { copyFileSync as copyFileSync2, existsSync as existsSync4, mkdirSync as mkdirSync3, renameSync as renameSync2, rmSync as rmSync2, statSync } from "fs";
|
|
680
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
681
|
+
import { dirname as dirname4, resolve as resolve4 } from "path";
|
|
682
|
+
var sharedNativeRuntimeOutputDir = resolve4(tmpdir2(), "rig-native");
|
|
683
|
+
var sharedNativeRuntimeOutputPath = resolve4(sharedNativeRuntimeOutputDir, `runtime-native-${process.platform}-${process.arch}.${suffix}`);
|
|
684
|
+
var colocatedNativeRuntimeFileName = `runtime-native.${suffix}`;
|
|
685
|
+
var nativeRuntimeLibrary = await loadNativeRuntimeLibrary();
|
|
686
|
+
async function ensureNativeRuntimeLibraryPath(outputPath = sharedNativeRuntimeOutputPath, options = {}) {
|
|
687
|
+
if (await buildNativeRuntimeLibrary(outputPath, options)) {
|
|
688
|
+
return outputPath;
|
|
689
|
+
}
|
|
690
|
+
return !options.force && existsSync4(outputPath) ? outputPath : null;
|
|
691
|
+
}
|
|
692
|
+
async function loadNativeRuntimeLibrary() {
|
|
693
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
694
|
+
return null;
|
|
695
|
+
}
|
|
696
|
+
for (const candidate of nativeRuntimeLibraryCandidates()) {
|
|
697
|
+
if (!candidate || !existsSync4(candidate)) {
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
const loaded = tryDlopenNativeRuntimeLibrary(candidate);
|
|
701
|
+
if (loaded) {
|
|
702
|
+
return loaded;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
const builtLibraryPath = await ensureNativeRuntimeLibraryPath(sharedNativeRuntimeOutputPath, { force: true });
|
|
706
|
+
if (!builtLibraryPath) {
|
|
707
|
+
return null;
|
|
708
|
+
}
|
|
709
|
+
return tryDlopenNativeRuntimeLibrary(builtLibraryPath);
|
|
710
|
+
}
|
|
711
|
+
function nativePackageLibraryCandidates(fromDir, names) {
|
|
712
|
+
const candidates = [];
|
|
713
|
+
let cursor = resolve4(fromDir);
|
|
714
|
+
for (let index = 0;index < 8; index += 1) {
|
|
715
|
+
for (const name of names) {
|
|
716
|
+
candidates.push(resolve4(cursor, "native", `${process.platform}-${process.arch}`, name), resolve4(cursor, "native", `${process.platform}-${process.arch}`, "lib", name), resolve4(cursor, "native", name), resolve4(cursor, "native", "lib", name));
|
|
717
|
+
}
|
|
718
|
+
const parent = dirname4(cursor);
|
|
719
|
+
if (parent === cursor)
|
|
720
|
+
break;
|
|
721
|
+
cursor = parent;
|
|
722
|
+
}
|
|
723
|
+
return candidates;
|
|
724
|
+
}
|
|
725
|
+
function nativeRuntimeLibraryCandidates() {
|
|
726
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_LIB?.trim() || "";
|
|
727
|
+
const execDir = process.execPath?.trim() ? dirname4(process.execPath.trim()) : "";
|
|
728
|
+
const platformSpecific = `runtime-native-${process.platform}-${process.arch}.${suffix}`;
|
|
729
|
+
return [...new Set([
|
|
730
|
+
explicit,
|
|
731
|
+
...nativePackageLibraryCandidates(import.meta.dir, [colocatedNativeRuntimeFileName, platformSpecific]),
|
|
732
|
+
execDir ? resolve4(execDir, colocatedNativeRuntimeFileName) : "",
|
|
733
|
+
execDir ? resolve4(execDir, platformSpecific) : "",
|
|
734
|
+
execDir ? resolve4(execDir, "..", colocatedNativeRuntimeFileName) : "",
|
|
735
|
+
execDir ? resolve4(execDir, "..", platformSpecific) : "",
|
|
736
|
+
execDir ? resolve4(execDir, "lib", colocatedNativeRuntimeFileName) : "",
|
|
737
|
+
execDir ? resolve4(execDir, "..", "lib", colocatedNativeRuntimeFileName) : "",
|
|
738
|
+
sharedNativeRuntimeOutputPath
|
|
739
|
+
].filter(Boolean))];
|
|
740
|
+
}
|
|
741
|
+
function resolveNativeRuntimeSourcePath() {
|
|
742
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_SOURCE?.trim();
|
|
743
|
+
if (explicit && existsSync4(explicit)) {
|
|
744
|
+
return explicit;
|
|
745
|
+
}
|
|
746
|
+
const bundled = resolve4(import.meta.dir, "../../../native/snapshot.zig");
|
|
747
|
+
return existsSync4(bundled) ? bundled : null;
|
|
748
|
+
}
|
|
749
|
+
async function buildNativeRuntimeLibrary(outputPath, options = {}) {
|
|
750
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
751
|
+
return false;
|
|
752
|
+
}
|
|
753
|
+
const zigBinary = Bun.which("zig");
|
|
754
|
+
const sourcePath = resolveNativeRuntimeSourcePath();
|
|
755
|
+
if (!zigBinary || !sourcePath) {
|
|
756
|
+
return false;
|
|
757
|
+
}
|
|
758
|
+
const tempOutputPath = `${outputPath}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
759
|
+
try {
|
|
760
|
+
mkdirSync3(dirname4(outputPath), { recursive: true });
|
|
761
|
+
const needsBuild = options.force === true || !existsSync4(outputPath) || statSync(sourcePath).mtimeMs > statSync(outputPath).mtimeMs;
|
|
762
|
+
if (!needsBuild) {
|
|
763
|
+
return true;
|
|
764
|
+
}
|
|
765
|
+
const build = Bun.spawn([
|
|
766
|
+
zigBinary,
|
|
767
|
+
"build-lib",
|
|
768
|
+
sourcePath,
|
|
769
|
+
"-dynamic",
|
|
770
|
+
"-O",
|
|
771
|
+
"ReleaseFast",
|
|
772
|
+
`-femit-bin=${tempOutputPath}`
|
|
773
|
+
], {
|
|
774
|
+
cwd: import.meta.dir,
|
|
775
|
+
stdout: "pipe",
|
|
776
|
+
stderr: "pipe"
|
|
777
|
+
});
|
|
778
|
+
const exitCode = await build.exited;
|
|
779
|
+
if (exitCode !== 0 || !existsSync4(tempOutputPath)) {
|
|
780
|
+
rmSync2(tempOutputPath, { force: true });
|
|
781
|
+
return false;
|
|
782
|
+
}
|
|
783
|
+
renameSync2(tempOutputPath, outputPath);
|
|
784
|
+
return true;
|
|
785
|
+
} catch {
|
|
786
|
+
rmSync2(tempOutputPath, { force: true });
|
|
787
|
+
return false;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
function tryDlopenNativeRuntimeLibrary(outputPath) {
|
|
791
|
+
try {
|
|
792
|
+
return dlopen(outputPath, {
|
|
793
|
+
rig_scope_match: {
|
|
794
|
+
args: ["ptr", "ptr"],
|
|
795
|
+
returns: "u8"
|
|
796
|
+
},
|
|
797
|
+
snapshot_capture: {
|
|
798
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
799
|
+
returns: "ptr"
|
|
800
|
+
},
|
|
801
|
+
snapshot_delta: {
|
|
802
|
+
args: ["ptr", "ptr"],
|
|
803
|
+
returns: "ptr"
|
|
804
|
+
},
|
|
805
|
+
snapshot_store_delta: {
|
|
806
|
+
args: ["ptr", "ptr", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
807
|
+
returns: "ptr"
|
|
808
|
+
},
|
|
809
|
+
snapshot_inspect_delta: {
|
|
810
|
+
args: ["ptr", "u64"],
|
|
811
|
+
returns: "ptr"
|
|
812
|
+
},
|
|
813
|
+
snapshot_apply_delta: {
|
|
814
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
815
|
+
returns: "ptr"
|
|
816
|
+
},
|
|
817
|
+
snapshot_release: {
|
|
818
|
+
args: ["ptr"],
|
|
819
|
+
returns: "void"
|
|
820
|
+
},
|
|
821
|
+
runtime_hash_file: {
|
|
822
|
+
args: ["ptr", "u64"],
|
|
823
|
+
returns: "ptr"
|
|
824
|
+
},
|
|
825
|
+
runtime_hash_tree: {
|
|
826
|
+
args: ["ptr", "u64"],
|
|
827
|
+
returns: "ptr"
|
|
828
|
+
},
|
|
829
|
+
runtime_prepare_paths: {
|
|
830
|
+
args: ["ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
831
|
+
returns: "ptr"
|
|
832
|
+
},
|
|
833
|
+
runtime_link_dependency_layer: {
|
|
834
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
835
|
+
returns: "ptr"
|
|
836
|
+
},
|
|
837
|
+
runtime_scan_worktrees: {
|
|
838
|
+
args: ["ptr", "u64"],
|
|
839
|
+
returns: "ptr"
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
} catch {
|
|
843
|
+
return null;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
// packages/runtime/src/control-plane/native/utils.ts
|
|
848
|
+
function resolveMonorepoRoot2(projectRoot) {
|
|
849
|
+
return resolveMonorepoRoot(projectRoot);
|
|
850
|
+
}
|
|
851
|
+
var scopeRegexCache = new Map;
|
|
852
|
+
function readJsonFile(path, fallback) {
|
|
853
|
+
if (!existsSync5(path)) {
|
|
854
|
+
return fallback;
|
|
855
|
+
}
|
|
856
|
+
try {
|
|
857
|
+
return JSON.parse(readFileSync3(path, "utf-8"));
|
|
858
|
+
} catch {
|
|
859
|
+
return fallback;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
function resolveHarnessPaths(projectRoot) {
|
|
863
|
+
const hasRuntimeWorkspace = Boolean(process.env.RIG_TASK_WORKSPACE?.trim());
|
|
864
|
+
const monorepoRoot = resolveMonorepoRoot2(projectRoot);
|
|
865
|
+
const harnessRoot = resolve5(projectRoot, "rig");
|
|
866
|
+
const stateRoot = resolve5(projectRoot, ".rig");
|
|
867
|
+
const layout = hasRuntimeWorkspace ? resolveRigLayout(projectRoot) : null;
|
|
868
|
+
const stateDir = layout?.stateDir ?? resolve5(stateRoot, "state");
|
|
869
|
+
const logsDir = layout?.logsDir ?? resolve5(stateRoot, "logs");
|
|
870
|
+
const artifactsDir = layout?.artifactsRoot ?? resolve5(monorepoRoot, "artifacts");
|
|
871
|
+
const taskConfigPath = layout?.taskConfigPath ?? resolve5(monorepoRoot, ".rig", "task-config.json");
|
|
872
|
+
const binDir = layout?.binDir ?? resolve5(stateRoot, "bin");
|
|
873
|
+
return {
|
|
874
|
+
harnessRoot,
|
|
875
|
+
stateDir: process.env.RIG_STATE_DIR || stateDir,
|
|
876
|
+
artifactsDir,
|
|
877
|
+
logsDir: process.env.RIG_LOGS_DIR || logsDir,
|
|
878
|
+
binDir,
|
|
879
|
+
hooksDir: resolve5(harnessRoot, "hooks"),
|
|
880
|
+
validationDir: resolve5(harnessRoot, "validation"),
|
|
881
|
+
taskConfigPath,
|
|
882
|
+
sessionPath: process.env.RIG_SESSION_FILE || resolve5(stateRoot, "session", "session.json"),
|
|
883
|
+
monorepoRoot,
|
|
884
|
+
tsApiTestsDir: process.env.TS_API_TESTS_DIR || resolve5(monorepoRoot, "TSAPITests"),
|
|
885
|
+
taskRepoCommitsPath: resolve5(stateDir, "task-repo-commits.json"),
|
|
886
|
+
baseRepoPinsPath: resolve5(stateDir, "base-repo-pins.json"),
|
|
887
|
+
failedApproachesPath: resolve5(stateDir, "failed_approaches.md"),
|
|
888
|
+
agentProfilePath: resolve5(stateDir, "agent-profile.json"),
|
|
889
|
+
reviewProfilePath: resolve5(stateDir, "review-profile.json")
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
// packages/runtime/src/control-plane/state-sync/repo.ts
|
|
894
|
+
import { existsSync as existsSync7 } from "fs";
|
|
895
|
+
import { resolve as resolve7 } from "path";
|
|
896
|
+
|
|
897
|
+
// packages/runtime/src/control-plane/repos/layout.ts
|
|
898
|
+
import { existsSync as existsSync6 } from "fs";
|
|
899
|
+
import { basename as basename2, dirname as dirname5, join, resolve as resolve6 } from "path";
|
|
900
|
+
|
|
901
|
+
// packages/runtime/src/control-plane/repos/registry.ts
|
|
902
|
+
var MANAGED_REPOS = new Map;
|
|
903
|
+
function getManagedRepoEntry(repoId) {
|
|
904
|
+
const entry = MANAGED_REPOS.get(repoId);
|
|
905
|
+
if (!entry) {
|
|
906
|
+
throw new Error(`managed repo not registered: ${repoId}. Plugins contribute repos via RigPlugin.contributes.repoSources; ` + `make sure a plugin declares this id and the plugin host has been initialized.`);
|
|
907
|
+
}
|
|
908
|
+
return entry;
|
|
909
|
+
}
|
|
910
|
+
function listManagedRepoEntries() {
|
|
911
|
+
return Array.from(MANAGED_REPOS.values());
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
// packages/runtime/src/control-plane/repos/layout.ts
|
|
915
|
+
function resolveRepoStateDir(projectRoot) {
|
|
916
|
+
const normalizedProjectRoot = resolve6(projectRoot);
|
|
917
|
+
const projectParent = dirname5(normalizedProjectRoot);
|
|
918
|
+
if (basename2(projectParent) === ".worktrees") {
|
|
919
|
+
const ownerRoot = dirname5(projectParent);
|
|
920
|
+
const ownerHasRepoMarkers = existsSync6(resolve6(ownerRoot, ".git")) || existsSync6(resolve6(ownerRoot, ".rig", "state"));
|
|
921
|
+
if (ownerHasRepoMarkers) {
|
|
922
|
+
return resolve6(ownerRoot, ".rig", "state");
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
return resolve6(projectRoot, ".rig", "state");
|
|
926
|
+
}
|
|
927
|
+
function resolveManagedRepoLayout(projectRoot, repoId) {
|
|
928
|
+
const normalizedProjectRoot = resolve6(projectRoot);
|
|
929
|
+
const entry = getManagedRepoEntry(repoId);
|
|
930
|
+
const stateDir = resolveRepoStateDir(normalizedProjectRoot);
|
|
931
|
+
const metadataRelativePath = join("repos", entry.id);
|
|
932
|
+
const metadataRoot = resolve6(stateDir, metadataRelativePath);
|
|
933
|
+
const runtimeWorkspace = process.env.RIG_TASK_WORKSPACE?.trim();
|
|
934
|
+
const runsInsideTaskWorktree = runtimeWorkspace && resolve6(runtimeWorkspace) === normalizedProjectRoot || basename2(dirname5(normalizedProjectRoot)) === ".worktrees";
|
|
935
|
+
const isPrimaryManagedRepo = listManagedRepoEntries()[0]?.id === repoId;
|
|
936
|
+
const checkoutRoot = isPrimaryManagedRepo && runsInsideTaskWorktree ? resolveMonorepoRoot(normalizedProjectRoot) : entry.checkoutEnvVar && process.env[entry.checkoutEnvVar]?.trim() ? resolve6(process.env[entry.checkoutEnvVar].trim()) : resolve6(normalizedProjectRoot, entry.alias);
|
|
937
|
+
return {
|
|
938
|
+
projectRoot: normalizedProjectRoot,
|
|
939
|
+
repoId: entry.id,
|
|
940
|
+
alias: entry.alias,
|
|
941
|
+
defaultBranch: entry.defaultBranch,
|
|
942
|
+
remoteUrl: entry.remoteEnvVar && process.env[entry.remoteEnvVar]?.trim() ? process.env[entry.remoteEnvVar].trim() : entry.defaultRemoteUrl,
|
|
943
|
+
checkoutRoot,
|
|
944
|
+
worktreesRoot: resolve6(checkoutRoot, ".worktrees"),
|
|
945
|
+
stateDir,
|
|
946
|
+
metadataRoot,
|
|
947
|
+
metadataRelativePath,
|
|
948
|
+
mirrorRoot: resolve6(metadataRoot, "mirror.git"),
|
|
949
|
+
mirrorStatePath: resolve6(metadataRoot, "mirror-state.json"),
|
|
950
|
+
mirrorStateRelativePath: join(metadataRelativePath, "mirror-state.json")
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
function resolveMonorepoRepoLayout(projectRoot) {
|
|
954
|
+
const entries = listManagedRepoEntries();
|
|
955
|
+
if (entries.length === 0) {
|
|
956
|
+
throw new Error("resolveMonorepoRepoLayout: no managed repos registered. Either contribute one via " + "RigPlugin.contributes.repoSources (with defaultBranch set), or avoid calling this " + "function for projects where the project root IS the monorepo.");
|
|
957
|
+
}
|
|
958
|
+
const primary = entries[0];
|
|
959
|
+
return resolveManagedRepoLayout(projectRoot, primary.id);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// packages/runtime/src/control-plane/state-sync/repo.ts
|
|
963
|
+
function resolveTrackerRepoPath(projectRoot) {
|
|
964
|
+
const monorepoRoot = resolveMonorepoRoot2(projectRoot);
|
|
965
|
+
try {
|
|
966
|
+
const layout = resolveMonorepoRepoLayout(projectRoot);
|
|
967
|
+
if (existsSync7(resolve7(layout.mirrorRoot, "HEAD"))) {
|
|
968
|
+
return layout.mirrorRoot;
|
|
969
|
+
}
|
|
970
|
+
} catch {}
|
|
971
|
+
return monorepoRoot;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
// packages/runtime/src/control-plane/state-sync/read.ts
|
|
975
|
+
var DEFAULT_READ_DEPS = {
|
|
976
|
+
fetchRef: nativeFetchRef,
|
|
977
|
+
readBlobAtRef: nativeReadBlobAtRef,
|
|
978
|
+
exists: existsSync8,
|
|
979
|
+
readFile: (path) => readFileSync4(path, "utf8")
|
|
980
|
+
};
|
|
981
|
+
function parseIssueStatus(rawStatus) {
|
|
982
|
+
const normalized = normalizeTaskLifecycleStatus(rawStatus);
|
|
983
|
+
return normalized ?? "unknown";
|
|
984
|
+
}
|
|
985
|
+
function parseIssueDependencies(raw) {
|
|
986
|
+
if (!Array.isArray(raw)) {
|
|
987
|
+
return [];
|
|
988
|
+
}
|
|
989
|
+
return raw.filter((entry) => !!entry && typeof entry === "object" && !Array.isArray(entry)).map((entry) => ({
|
|
990
|
+
issueId: typeof entry.issue_id === "string" && entry.issue_id.trim() ? entry.issue_id.trim() : null,
|
|
991
|
+
dependsOnId: typeof entry.depends_on_id === "string" && entry.depends_on_id.trim() ? entry.depends_on_id.trim() : null,
|
|
992
|
+
id: typeof entry.id === "string" && entry.id.trim() ? entry.id.trim() : null,
|
|
993
|
+
type: typeof entry.type === "string" && entry.type.trim() ? entry.type.trim() : null
|
|
994
|
+
}));
|
|
995
|
+
}
|
|
996
|
+
function parseIssuesJsonl(raw) {
|
|
997
|
+
const issues = [];
|
|
998
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
999
|
+
const trimmed = line.trim();
|
|
1000
|
+
if (!trimmed) {
|
|
1001
|
+
continue;
|
|
1002
|
+
}
|
|
1003
|
+
try {
|
|
1004
|
+
const record = JSON.parse(trimmed);
|
|
1005
|
+
const id = typeof record.id === "string" && record.id.trim() ? record.id.trim() : "";
|
|
1006
|
+
if (!id) {
|
|
1007
|
+
continue;
|
|
1008
|
+
}
|
|
1009
|
+
const rawStatus = typeof record.status === "string" && record.status.trim() ? record.status.trim() : null;
|
|
1010
|
+
issues.push({
|
|
1011
|
+
id,
|
|
1012
|
+
title: typeof record.title === "string" && record.title.trim() ? record.title.trim() : null,
|
|
1013
|
+
description: typeof record.description === "string" && record.description.trim() ? record.description.trim() : null,
|
|
1014
|
+
acceptanceCriteria: typeof record.acceptance_criteria === "string" && record.acceptance_criteria.trim() ? record.acceptance_criteria.trim() : typeof record.acceptanceCriteria === "string" && record.acceptanceCriteria.trim() ? record.acceptanceCriteria.trim() : null,
|
|
1015
|
+
issueType: typeof record.issue_type === "string" && record.issue_type.trim() ? record.issue_type.trim() : null,
|
|
1016
|
+
status: parseIssueStatus(rawStatus),
|
|
1017
|
+
rawStatus,
|
|
1018
|
+
priority: typeof record.priority === "number" ? record.priority : null,
|
|
1019
|
+
dependencies: parseIssueDependencies(record.dependencies)
|
|
1020
|
+
});
|
|
1021
|
+
} catch {}
|
|
1022
|
+
}
|
|
1023
|
+
return issues;
|
|
1024
|
+
}
|
|
1025
|
+
function parseTaskStateEnvelope(raw) {
|
|
1026
|
+
if (!raw || !raw.trim()) {
|
|
1027
|
+
return readTaskStateMetadataEnvelope(null);
|
|
1028
|
+
}
|
|
1029
|
+
try {
|
|
1030
|
+
return readTaskStateMetadataEnvelope(JSON.parse(raw));
|
|
1031
|
+
} catch {
|
|
1032
|
+
return readTaskStateMetadataEnvelope(null);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
function readRemoteBlobAtRef(deps, repoPath, ref, path, options) {
|
|
1036
|
+
try {
|
|
1037
|
+
return deps.readBlobAtRef(repoPath, ref, path);
|
|
1038
|
+
} catch (error) {
|
|
1039
|
+
if (options.required) {
|
|
1040
|
+
throw error;
|
|
1041
|
+
}
|
|
1042
|
+
return null;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
function shouldPreferLocalTrackerState(options) {
|
|
1046
|
+
if (!options.allowLocalFallback) {
|
|
1047
|
+
return false;
|
|
1048
|
+
}
|
|
1049
|
+
const runtimeWorkspace = process.env.RIG_TASK_WORKSPACE?.trim();
|
|
1050
|
+
if (!runtimeWorkspace) {
|
|
1051
|
+
return false;
|
|
1052
|
+
}
|
|
1053
|
+
if (process.env.RIG_TASK_RUNTIME_ID?.trim()) {
|
|
1054
|
+
return true;
|
|
1055
|
+
}
|
|
1056
|
+
const runtimeContextPath = process.env[RUNTIME_CONTEXT_ENV]?.trim();
|
|
1057
|
+
if (runtimeContextPath) {
|
|
1058
|
+
return true;
|
|
1059
|
+
}
|
|
1060
|
+
return existsSync8(resolve8(runtimeWorkspace, ".rig", "runtime-context.json"));
|
|
1061
|
+
}
|
|
1062
|
+
function readLocalTrackerState(projectRoot, deps) {
|
|
1063
|
+
const monorepoRoot = resolveMonorepoRoot2(projectRoot);
|
|
1064
|
+
const issuesPath = resolve8(monorepoRoot, ".beads", "issues.jsonl");
|
|
1065
|
+
const taskStatePath = resolve8(monorepoRoot, ".beads", "task-state.json");
|
|
1066
|
+
return projectSyncedTrackerSnapshot({
|
|
1067
|
+
source: "local",
|
|
1068
|
+
issuesBaseOid: null,
|
|
1069
|
+
issuesText: deps.exists(issuesPath) ? deps.readFile(issuesPath) : "",
|
|
1070
|
+
taskStateBaseOid: null,
|
|
1071
|
+
taskStateText: deps.exists(taskStatePath) ? deps.readFile(taskStatePath) : null
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
function projectSyncedTrackerSnapshot(input) {
|
|
1075
|
+
if (input.source === "remote" && input.issuesBaseOid && input.taskStateBaseOid && input.issuesBaseOid !== input.taskStateBaseOid) {
|
|
1076
|
+
throw new Error("Remote tracker files must be read from the same fetched base.");
|
|
1077
|
+
}
|
|
1078
|
+
return {
|
|
1079
|
+
source: input.source,
|
|
1080
|
+
baseOid: input.issuesBaseOid ?? input.taskStateBaseOid ?? null,
|
|
1081
|
+
issues: parseIssuesJsonl(input.issuesText),
|
|
1082
|
+
taskState: parseTaskStateEnvelope(input.taskStateText)
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
1085
|
+
function readSyncedTrackerState(projectRoot, deps = {}, options = {}) {
|
|
1086
|
+
const readDeps = { ...DEFAULT_READ_DEPS, ...deps };
|
|
1087
|
+
const trackerRepoPath = resolveTrackerRepoPath(projectRoot);
|
|
1088
|
+
if (shouldPreferLocalTrackerState(options)) {
|
|
1089
|
+
return readLocalTrackerState(projectRoot, readDeps);
|
|
1090
|
+
}
|
|
1091
|
+
try {
|
|
1092
|
+
const baseOid = readDeps.fetchRef(trackerRepoPath, "origin", "main");
|
|
1093
|
+
return projectSyncedTrackerSnapshot({
|
|
1094
|
+
source: "remote",
|
|
1095
|
+
issuesBaseOid: baseOid,
|
|
1096
|
+
issuesText: readRemoteBlobAtRef(readDeps, trackerRepoPath, baseOid, ".beads/issues.jsonl", {
|
|
1097
|
+
required: true
|
|
1098
|
+
}) ?? "",
|
|
1099
|
+
taskStateBaseOid: baseOid,
|
|
1100
|
+
taskStateText: readRemoteBlobAtRef(readDeps, trackerRepoPath, baseOid, ".beads/task-state.json", {
|
|
1101
|
+
required: false
|
|
1102
|
+
})
|
|
1103
|
+
});
|
|
1104
|
+
} catch (error) {
|
|
1105
|
+
if (!options.allowLocalFallback) {
|
|
1106
|
+
throw error;
|
|
1107
|
+
}
|
|
1108
|
+
return readLocalTrackerState(projectRoot, readDeps);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
// packages/runtime/src/control-plane/state-sync/reconcile.ts
|
|
1112
|
+
var STALE_CLAIM_MS = 24 * 60 * 60 * 1000;
|
|
1113
|
+
// packages/runtime/src/control-plane/native/task-state.ts
|
|
1114
|
+
function readTaskConfig(projectRoot) {
|
|
1115
|
+
const raw = readJsonFile(resolveTaskConfigPath(projectRoot), {});
|
|
1116
|
+
return stripTaskConfigMetadata(raw);
|
|
1117
|
+
}
|
|
1118
|
+
function readSourceTaskConfig(projectRoot) {
|
|
1119
|
+
const raw = readAndSyncSourceTaskConfig(projectRoot);
|
|
1120
|
+
return stripTaskConfigMetadata(raw);
|
|
1121
|
+
}
|
|
1122
|
+
function readValidationDescriptions(projectRoot) {
|
|
1123
|
+
const raw = readJsonFile(resolveTaskConfigPath(projectRoot), {});
|
|
1124
|
+
return readValidationDescriptionMap(raw);
|
|
1125
|
+
}
|
|
1126
|
+
function readSourceValidationDescriptions(projectRoot) {
|
|
1127
|
+
const rootRaw = readJsonFile(resolve9(projectRoot, "rig", "task-config.json"), {});
|
|
1128
|
+
const sourcePath = findSourceTaskConfigPath(projectRoot);
|
|
1129
|
+
const sourceRaw = sourcePath ? readJsonFile(sourcePath, {}) : {};
|
|
1130
|
+
const rootDescriptions = readValidationDescriptionMap(rootRaw);
|
|
1131
|
+
const sourceDescriptions = readValidationDescriptionMap(sourceRaw);
|
|
1132
|
+
return {
|
|
1133
|
+
...rootDescriptions,
|
|
1134
|
+
...sourceDescriptions
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
function currentTaskId(projectRoot) {
|
|
1138
|
+
const fromEnv = (process.env.RIG_TASK_ID || "").trim();
|
|
1139
|
+
if (fromEnv) {
|
|
1140
|
+
return fromEnv;
|
|
1141
|
+
}
|
|
1142
|
+
const runtimeId = (process.env.RIG_TASK_RUNTIME_ID || "").trim();
|
|
1143
|
+
if (runtimeId.startsWith("task-") && runtimeId.length > "task-".length) {
|
|
1144
|
+
return runtimeId.slice("task-".length);
|
|
1145
|
+
}
|
|
1146
|
+
const workspace = (process.env.RIG_TASK_WORKSPACE || "").trim();
|
|
1147
|
+
const inferredFromWorkspace = inferTaskIdFromRuntimePath(workspace);
|
|
1148
|
+
if (inferredFromWorkspace) {
|
|
1149
|
+
return inferredFromWorkspace;
|
|
1150
|
+
}
|
|
1151
|
+
const inferredFromCwd = inferTaskIdFromRuntimePath(process.cwd());
|
|
1152
|
+
if (inferredFromCwd) {
|
|
1153
|
+
return inferredFromCwd;
|
|
1154
|
+
}
|
|
1155
|
+
try {
|
|
1156
|
+
const paths = resolveHarnessPaths(projectRoot);
|
|
1157
|
+
const session = readJsonFile(paths.sessionPath, {});
|
|
1158
|
+
return session.activeTaskIds?.[0] || "";
|
|
1159
|
+
} catch {
|
|
1160
|
+
return "";
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
function readSourceTaskStateMetadata(projectRoot, taskId, lifecycleStatus, snapshot) {
|
|
1164
|
+
const syncedSnapshot = snapshot ?? readSyncedTrackerState(projectRoot);
|
|
1165
|
+
const syncedIssue = syncedSnapshot.issues.find((issue) => issue.id === taskId) ?? null;
|
|
1166
|
+
const syncedLifecycleStatus = syncedIssue && syncedIssue.status !== "unknown" ? syncedIssue.status : readLocalSourceTaskLifecycleStatus(projectRoot, taskId);
|
|
1167
|
+
const metadata = syncedSnapshot.taskState.tasks[taskId] ?? readLocalSourceTaskStateEnvelope(projectRoot).tasks[taskId] ?? null;
|
|
1168
|
+
return discardMismatchedTaskStateMetadata({
|
|
1169
|
+
taskId,
|
|
1170
|
+
lifecycleStatus: syncedLifecycleStatus ?? normalizeTaskLifecycleStatus(lifecycleStatus),
|
|
1171
|
+
metadata
|
|
1172
|
+
});
|
|
1173
|
+
}
|
|
1174
|
+
function readValidationDescriptionMap(raw) {
|
|
1175
|
+
return {
|
|
1176
|
+
...coerceValidationDescriptions(raw.validation_descriptions),
|
|
1177
|
+
...coerceValidationDescriptions(readValidationDescriptionsFromMeta(raw._meta))
|
|
1178
|
+
};
|
|
1179
|
+
}
|
|
1180
|
+
function stripTaskConfigMetadata(raw) {
|
|
1181
|
+
const { validation_descriptions: _legacyDescriptions, _meta, ...tasks } = raw;
|
|
1182
|
+
return tasks;
|
|
1183
|
+
}
|
|
1184
|
+
function coerceValidationDescriptions(candidate) {
|
|
1185
|
+
if (!candidate || typeof candidate !== "object" || Array.isArray(candidate)) {
|
|
1186
|
+
return {};
|
|
1187
|
+
}
|
|
1188
|
+
const descriptions = {};
|
|
1189
|
+
for (const [key, value] of Object.entries(candidate)) {
|
|
1190
|
+
if (typeof value === "string" && value.length > 0) {
|
|
1191
|
+
descriptions[key] = value;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
return descriptions;
|
|
1195
|
+
}
|
|
1196
|
+
function readValidationDescriptionsFromMeta(meta) {
|
|
1197
|
+
if (!meta || typeof meta !== "object" || Array.isArray(meta)) {
|
|
1198
|
+
return;
|
|
1199
|
+
}
|
|
1200
|
+
return meta.validation_descriptions;
|
|
1201
|
+
}
|
|
1202
|
+
function readLocalSourceTaskStateEnvelope(projectRoot) {
|
|
1203
|
+
const taskStatePath = resolve9(resolveMonorepoRoot2(projectRoot), ".beads", "task-state.json");
|
|
1204
|
+
return readTaskStateMetadataEnvelope(readJsonFile(taskStatePath, {}));
|
|
1205
|
+
}
|
|
1206
|
+
function readLocalSourceTaskLifecycleStatus(projectRoot, taskId) {
|
|
1207
|
+
const issuesPath = resolve9(resolveMonorepoRoot2(projectRoot), ".beads", "issues.jsonl");
|
|
1208
|
+
if (!existsSync9(issuesPath)) {
|
|
1209
|
+
return null;
|
|
1210
|
+
}
|
|
1211
|
+
for (const line of readFileSync5(issuesPath, "utf8").split(/\r?\n/)) {
|
|
1212
|
+
const trimmed = line.trim();
|
|
1213
|
+
if (!trimmed) {
|
|
1214
|
+
continue;
|
|
1215
|
+
}
|
|
1216
|
+
try {
|
|
1217
|
+
const parsed = JSON.parse(trimmed);
|
|
1218
|
+
if (parsed.id === taskId && parsed.issue_type === "task") {
|
|
1219
|
+
return normalizeTaskLifecycleStatus(parsed.status);
|
|
1220
|
+
}
|
|
1221
|
+
} catch {}
|
|
1222
|
+
}
|
|
1223
|
+
return null;
|
|
1224
|
+
}
|
|
1225
|
+
function inferTaskIdFromRuntimePath(path) {
|
|
1226
|
+
if (!path) {
|
|
1227
|
+
return "";
|
|
1228
|
+
}
|
|
1229
|
+
const match = path.match(/\/\.rig\/runtime\/agents\/task-([^/]+)\/worktree(?:\/|$)/) || path.match(/\/\.worktrees\/([^/]+)(?:\/|$)/);
|
|
1230
|
+
const candidate = match?.[1] || "";
|
|
1231
|
+
return candidate.startsWith("bd-") ? candidate : "";
|
|
1232
|
+
}
|
|
1233
|
+
function lookupTask(projectRoot, input) {
|
|
1234
|
+
if (!input) {
|
|
1235
|
+
return "";
|
|
1236
|
+
}
|
|
1237
|
+
if (!input.startsWith("bd-")) {
|
|
1238
|
+
return input;
|
|
1239
|
+
}
|
|
1240
|
+
try {
|
|
1241
|
+
const taskConfig2 = readTaskConfig(projectRoot);
|
|
1242
|
+
if (taskConfig2[input]) {
|
|
1243
|
+
return input;
|
|
1244
|
+
}
|
|
1245
|
+
} catch {}
|
|
1246
|
+
const taskConfig = readSourceTaskConfig(projectRoot);
|
|
1247
|
+
return taskConfig[input] ? input : "";
|
|
1248
|
+
}
|
|
1249
|
+
function ensureArtifactAlias(projectRoot, id) {}
|
|
1250
|
+
function artifactDirForId(projectRoot, id) {
|
|
1251
|
+
const workspaceDir = process.env.RIG_TASK_WORKSPACE?.trim();
|
|
1252
|
+
if (workspaceDir) {
|
|
1253
|
+
const worktreeArtifacts = resolve9(workspaceDir, "artifacts", id);
|
|
1254
|
+
if (existsSync9(worktreeArtifacts) || existsSync9(resolve9(workspaceDir, "artifacts"))) {
|
|
1255
|
+
return worktreeArtifacts;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
try {
|
|
1259
|
+
const paths = resolveHarnessPaths(projectRoot);
|
|
1260
|
+
return resolve9(paths.artifactsDir, id);
|
|
1261
|
+
} catch {
|
|
1262
|
+
return resolve9(resolveMonorepoRoot2(projectRoot), "artifacts", id);
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
function resolveTaskConfigPath(projectRoot) {
|
|
1266
|
+
const paths = resolveHarnessPaths(projectRoot);
|
|
1267
|
+
if (existsSync9(paths.taskConfigPath)) {
|
|
1268
|
+
return paths.taskConfigPath;
|
|
1269
|
+
}
|
|
1270
|
+
for (const candidate of sourceTaskConfigCandidates(projectRoot)) {
|
|
1271
|
+
if (existsSync9(candidate)) {
|
|
1272
|
+
return candidate;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
throw new Error(`Task config missing at ${paths.taskConfigPath}.`);
|
|
1276
|
+
}
|
|
1277
|
+
function findSourceTaskConfigPath(projectRoot) {
|
|
1278
|
+
for (const candidate of sourceTaskConfigCandidates(projectRoot)) {
|
|
1279
|
+
if (existsSync9(candidate)) {
|
|
1280
|
+
return candidate;
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
return null;
|
|
1284
|
+
}
|
|
1285
|
+
var FILE_TASK_PATTERN = /\.(task\.)?json$/;
|
|
1286
|
+
function readAndSyncSourceTaskConfig(projectRoot) {
|
|
1287
|
+
const sourcePath = findSourceTaskConfigPath(projectRoot);
|
|
1288
|
+
const raw = sourcePath ? readJsonFile(sourcePath, {}) : readConfiguredFileTaskConfig(projectRoot);
|
|
1289
|
+
const synced = synchronizeTaskConfigWithTracker(projectRoot, raw);
|
|
1290
|
+
if (sourcePath && synced.updated) {
|
|
1291
|
+
try {
|
|
1292
|
+
writeFileSync3(sourcePath, `${JSON.stringify(synced.config, null, 2)}
|
|
1293
|
+
`, "utf-8");
|
|
1294
|
+
} catch {}
|
|
1295
|
+
}
|
|
1296
|
+
return synced.config;
|
|
1297
|
+
}
|
|
1298
|
+
function synchronizeSourceTaskConfig(projectRoot) {
|
|
1299
|
+
return readAndSyncSourceTaskConfig(projectRoot);
|
|
1300
|
+
}
|
|
1301
|
+
function synchronizeTaskConfigWithTracker(projectRoot, rawConfig) {
|
|
1302
|
+
const issues = readSourceIssueRecords(projectRoot);
|
|
1303
|
+
if (issues.length === 0) {
|
|
1304
|
+
return { config: rawConfig, updated: false };
|
|
1305
|
+
}
|
|
1306
|
+
const taskConfig = stripTaskConfigMetadata(rawConfig);
|
|
1307
|
+
const mergedConfig = { ...taskConfig };
|
|
1308
|
+
const validationDescriptions = coerceValidationDescriptions(rawConfig.validation_descriptions);
|
|
1309
|
+
const metaValidationDescriptions = coerceValidationDescriptions(readValidationDescriptionsFromMeta(rawConfig._meta));
|
|
1310
|
+
let updated = false;
|
|
1311
|
+
for (const issue of issues) {
|
|
1312
|
+
if (issue.issueType !== "task") {
|
|
1313
|
+
continue;
|
|
1314
|
+
}
|
|
1315
|
+
if (mergedConfig[issue.id] && !shouldRefreshAutoSyncedTaskConfigEntry(mergedConfig[issue.id])) {
|
|
1316
|
+
continue;
|
|
1317
|
+
}
|
|
1318
|
+
mergedConfig[issue.id] = buildAutoSyncedTaskConfigEntry(issue);
|
|
1319
|
+
updated = true;
|
|
1320
|
+
}
|
|
1321
|
+
return {
|
|
1322
|
+
config: {
|
|
1323
|
+
...mergedConfig,
|
|
1324
|
+
...Object.keys(validationDescriptions).length > 0 ? { validation_descriptions: validationDescriptions } : {},
|
|
1325
|
+
...Object.keys(metaValidationDescriptions).length > 0 ? { _meta: { validation_descriptions: metaValidationDescriptions } } : {}
|
|
1326
|
+
},
|
|
1327
|
+
updated
|
|
1328
|
+
};
|
|
1329
|
+
}
|
|
1330
|
+
function shouldRefreshAutoSyncedTaskConfigEntry(entry) {
|
|
1331
|
+
if (!entry || typeof entry !== "object") {
|
|
1332
|
+
return false;
|
|
1333
|
+
}
|
|
1334
|
+
const candidate = entry;
|
|
1335
|
+
if (!candidate.auto_synced) {
|
|
1336
|
+
return false;
|
|
1337
|
+
}
|
|
1338
|
+
if (!Array.isArray(candidate.scope) || candidate.scope.length === 0) {
|
|
1339
|
+
return true;
|
|
1340
|
+
}
|
|
1341
|
+
if (candidate.scope.some((glob) => typeof glob !== "string" || glob.trim().length === 0)) {
|
|
1342
|
+
return true;
|
|
1343
|
+
}
|
|
1344
|
+
return !candidate.role;
|
|
1345
|
+
}
|
|
1346
|
+
function readSourceIssueRecords(projectRoot) {
|
|
1347
|
+
const issuesPath = resolve9(resolveMonorepoRoot2(projectRoot), ".beads", "issues.jsonl");
|
|
1348
|
+
if (!existsSync9(issuesPath)) {
|
|
1349
|
+
return [];
|
|
1350
|
+
}
|
|
1351
|
+
const records = [];
|
|
1352
|
+
for (const line of readFileSync5(issuesPath, "utf-8").split(/\r?\n/)) {
|
|
1353
|
+
const trimmed = line.trim();
|
|
1354
|
+
if (!trimmed) {
|
|
1355
|
+
continue;
|
|
1356
|
+
}
|
|
1357
|
+
try {
|
|
1358
|
+
const parsed = JSON.parse(trimmed);
|
|
1359
|
+
const id = typeof parsed.id === "string" ? parsed.id.trim() : "";
|
|
1360
|
+
if (!id) {
|
|
1361
|
+
continue;
|
|
1362
|
+
}
|
|
1363
|
+
records.push({
|
|
1364
|
+
id,
|
|
1365
|
+
title: typeof parsed.title === "string" ? parsed.title.trim() : "",
|
|
1366
|
+
issueType: typeof parsed.issue_type === "string" ? parsed.issue_type.trim() : null,
|
|
1367
|
+
labels: Array.isArray(parsed.labels) ? parsed.labels.filter((label) => typeof label === "string") : []
|
|
1368
|
+
});
|
|
1369
|
+
} catch {}
|
|
1370
|
+
}
|
|
1371
|
+
return records;
|
|
1372
|
+
}
|
|
1373
|
+
function buildAutoSyncedTaskConfigEntry(issue) {
|
|
1374
|
+
return {
|
|
1375
|
+
auto_synced: true,
|
|
1376
|
+
role: inferAutoSyncedTaskRole(issue),
|
|
1377
|
+
scope: inferAutoSyncedTaskScope(issue),
|
|
1378
|
+
validation: []
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
function inferAutoSyncedTaskRole(issue) {
|
|
1382
|
+
for (const label of issue.labels) {
|
|
1383
|
+
if (label === "role:architect")
|
|
1384
|
+
return "architect";
|
|
1385
|
+
if (label === "role:extractor")
|
|
1386
|
+
return "extractor";
|
|
1387
|
+
if (label === "role:mechanic")
|
|
1388
|
+
return "mechanic";
|
|
1389
|
+
if (label === "role:verifier")
|
|
1390
|
+
return "verifier";
|
|
1391
|
+
}
|
|
1392
|
+
if (/\bDESIGN\b/i.test(issue.title)) {
|
|
1393
|
+
return "architect";
|
|
1394
|
+
}
|
|
1395
|
+
if (/\bInitialize\b/i.test(issue.title)) {
|
|
1396
|
+
return "mechanic";
|
|
1397
|
+
}
|
|
1398
|
+
return "extractor";
|
|
1399
|
+
}
|
|
1400
|
+
function inferAutoSyncedTaskScope(issue) {
|
|
1401
|
+
return [`artifacts/${issue.id}/**`];
|
|
1402
|
+
}
|
|
1403
|
+
function readConfiguredFileTaskConfig(projectRoot) {
|
|
1404
|
+
const sourcePath = readConfiguredFilesTaskSourcePath(projectRoot);
|
|
1405
|
+
if (!sourcePath) {
|
|
1406
|
+
return {};
|
|
1407
|
+
}
|
|
1408
|
+
const directory = resolve9(projectRoot, sourcePath);
|
|
1409
|
+
if (!existsSync9(directory)) {
|
|
1410
|
+
return {};
|
|
1411
|
+
}
|
|
1412
|
+
const config = {};
|
|
1413
|
+
for (const name of readdirSync(directory)) {
|
|
1414
|
+
if (!FILE_TASK_PATTERN.test(name))
|
|
1415
|
+
continue;
|
|
1416
|
+
const file = resolve9(directory, name);
|
|
1417
|
+
try {
|
|
1418
|
+
if (!statSync2(file).isFile())
|
|
1419
|
+
continue;
|
|
1420
|
+
const raw = JSON.parse(readFileSync5(file, "utf8"));
|
|
1421
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
1422
|
+
continue;
|
|
1423
|
+
const record = raw;
|
|
1424
|
+
const inferredId = basename3(name).replace(FILE_TASK_PATTERN, "");
|
|
1425
|
+
const id = typeof record.id === "string" && record.id.trim().length > 0 ? record.id.trim() : inferredId;
|
|
1426
|
+
config[id] = fileTaskToConfigEntry(record, { kind: "files", path: sourcePath });
|
|
1427
|
+
} catch {}
|
|
1428
|
+
}
|
|
1429
|
+
return config;
|
|
1430
|
+
}
|
|
1431
|
+
function fileTaskToConfigEntry(task, source) {
|
|
1432
|
+
const labels = Array.isArray(task.labels) ? task.labels.filter((label) => typeof label === "string") : [];
|
|
1433
|
+
const scope = firstStringList(task.scope, labels.filter((label) => label.startsWith("scope:")).map((label) => label.slice("scope:".length)));
|
|
1434
|
+
const validation = firstStringList(task.validation, task.validators, labels.filter((label) => label.startsWith("validator:")).map((label) => label.slice("validator:".length)));
|
|
1435
|
+
const roleLabel = labels.find((label) => label.startsWith("role:"));
|
|
1436
|
+
const role = typeof task.role === "string" && task.role.trim().length > 0 ? task.role.trim() : roleLabel ? roleLabel.slice("role:".length) : undefined;
|
|
1437
|
+
return {
|
|
1438
|
+
auto_synced: true,
|
|
1439
|
+
...typeof task.title === "string" ? { title: task.title } : {},
|
|
1440
|
+
...typeof task.status === "string" ? { status: task.status } : {},
|
|
1441
|
+
...typeof task.description === "string" ? { description: task.description } : {},
|
|
1442
|
+
...typeof task.acceptance_criteria === "string" ? { acceptance_criteria: task.acceptance_criteria } : typeof task.acceptanceCriteria === "string" ? { acceptance_criteria: task.acceptanceCriteria } : {},
|
|
1443
|
+
...role ? { role } : {},
|
|
1444
|
+
...scope.length > 0 ? { scope } : {},
|
|
1445
|
+
...validation.length > 0 ? { validation } : {},
|
|
1446
|
+
_rig: { taskSource: source }
|
|
1447
|
+
};
|
|
1448
|
+
}
|
|
1449
|
+
function firstStringList(...candidates) {
|
|
1450
|
+
for (const candidate of candidates) {
|
|
1451
|
+
if (!Array.isArray(candidate)) {
|
|
1452
|
+
continue;
|
|
1453
|
+
}
|
|
1454
|
+
const list = candidate.filter((entry) => typeof entry === "string" && entry.trim().length > 0);
|
|
1455
|
+
if (list.length > 0) {
|
|
1456
|
+
return list;
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
return [];
|
|
1460
|
+
}
|
|
1461
|
+
function readConfiguredFilesTaskSourcePath(projectRoot) {
|
|
1462
|
+
const jsonPath = resolve9(projectRoot, "rig.config.json");
|
|
1463
|
+
if (existsSync9(jsonPath)) {
|
|
1464
|
+
try {
|
|
1465
|
+
const parsed = JSON.parse(readFileSync5(jsonPath, "utf8"));
|
|
1466
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
1467
|
+
const taskSource = parsed.taskSource;
|
|
1468
|
+
if (taskSource && typeof taskSource === "object" && !Array.isArray(taskSource)) {
|
|
1469
|
+
const record = taskSource;
|
|
1470
|
+
return record.kind === "files" && typeof record.path === "string" ? record.path : null;
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
} catch {
|
|
1474
|
+
return null;
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
const tsPath = resolve9(projectRoot, "rig.config.ts");
|
|
1478
|
+
if (!existsSync9(tsPath)) {
|
|
1479
|
+
return null;
|
|
1480
|
+
}
|
|
1481
|
+
try {
|
|
1482
|
+
const source = readFileSync5(tsPath, "utf8");
|
|
1483
|
+
const taskSourceBlock = source.match(/taskSource\s*:\s*\{[\s\S]*?\}/m)?.[0] ?? "";
|
|
1484
|
+
const kind = taskSourceBlock.match(/kind\s*:\s*["']([^"']+)["']/)?.[1];
|
|
1485
|
+
if (kind !== "files") {
|
|
1486
|
+
return null;
|
|
1487
|
+
}
|
|
1488
|
+
return taskSourceBlock.match(/path\s*:\s*["']([^"']+)["']/)?.[1] ?? null;
|
|
1489
|
+
} catch {
|
|
1490
|
+
return null;
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
function sourceTaskConfigCandidates(projectRoot) {
|
|
1494
|
+
const runtimeContext = loadRuntimeContextFromEnv();
|
|
1495
|
+
return [
|
|
1496
|
+
runtimeContext?.monorepoMainRoot ? resolve9(runtimeContext.monorepoMainRoot, ".rig", "task-config.json") : "",
|
|
1497
|
+
process.env.MONOREPO_MAIN_ROOT?.trim() ? resolve9(process.env.MONOREPO_MAIN_ROOT.trim(), ".rig", "task-config.json") : "",
|
|
1498
|
+
resolve9(resolveMonorepoRoot2(projectRoot), ".rig", "task-config.json")
|
|
1499
|
+
].filter(Boolean);
|
|
1500
|
+
}
|
|
1501
|
+
export {
|
|
1502
|
+
synchronizeSourceTaskConfig,
|
|
1503
|
+
readValidationDescriptions,
|
|
1504
|
+
readTaskConfig,
|
|
1505
|
+
readSourceValidationDescriptions,
|
|
1506
|
+
readSourceTaskStateMetadata,
|
|
1507
|
+
readSourceTaskConfig,
|
|
1508
|
+
lookupTask,
|
|
1509
|
+
ensureArtifactAlias,
|
|
1510
|
+
currentTaskId,
|
|
1511
|
+
artifactDirForId
|
|
1512
|
+
};
|