@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,856 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/runtime/src/control-plane/state-sync/read.ts
|
|
3
|
+
import { existsSync as existsSync6, readFileSync as readFileSync2 } from "fs";
|
|
4
|
+
import { resolve as resolve6 } from "path";
|
|
5
|
+
|
|
6
|
+
// packages/runtime/src/control-plane/native/git-native.ts
|
|
7
|
+
import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "fs";
|
|
8
|
+
import { tmpdir } from "os";
|
|
9
|
+
import { dirname, isAbsolute, resolve } from "path";
|
|
10
|
+
import { createHash } from "crypto";
|
|
11
|
+
var sharedGitNativeOutputDir = resolve(tmpdir(), "rig-native");
|
|
12
|
+
var sharedGitNativeOutputPath = resolve(sharedGitNativeOutputDir, `rig-git-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
|
|
13
|
+
var trackerCommandUsageProbe = "usage: rig-git fetch-ref <repo-path> <remote> <branch>";
|
|
14
|
+
function temporaryGitBinaryOutputPath(outputPath) {
|
|
15
|
+
const suffix = process.platform === "win32" ? ".exe" : "";
|
|
16
|
+
return resolve(dirname(outputPath), `.rig-git-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}${suffix}`);
|
|
17
|
+
}
|
|
18
|
+
function publishGitBinary(tempOutputPath, outputPath) {
|
|
19
|
+
try {
|
|
20
|
+
renameSync(tempOutputPath, outputPath);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
if (process.platform === "win32" && existsSync(outputPath)) {
|
|
23
|
+
rmSync(outputPath, { force: true });
|
|
24
|
+
renameSync(tempOutputPath, outputPath);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function runtimeRigGitFileName() {
|
|
31
|
+
return `rig-git${process.platform === "win32" ? ".exe" : ""}`;
|
|
32
|
+
}
|
|
33
|
+
function rigGitSourceCandidates() {
|
|
34
|
+
const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
|
|
35
|
+
const cwd = process.cwd()?.trim() || "";
|
|
36
|
+
const projectRoot = process.env.PROJECT_RIG_ROOT?.trim() || "";
|
|
37
|
+
const hostProjectRoot = process.env.RIG_HOST_PROJECT_ROOT?.trim() || "";
|
|
38
|
+
const moduleRelativeSource = resolve(import.meta.dir, "../../../native/rig-git.zig");
|
|
39
|
+
return [...new Set([
|
|
40
|
+
process.env.RIG_NATIVE_GIT_SOURCE?.trim() || "",
|
|
41
|
+
moduleRelativeSource,
|
|
42
|
+
projectRoot ? resolve(projectRoot, "packages/runtime/native/rig-git.zig") : "",
|
|
43
|
+
hostProjectRoot ? resolve(hostProjectRoot, "packages/runtime/native/rig-git.zig") : "",
|
|
44
|
+
cwd ? resolve(cwd, "packages/runtime/native/rig-git.zig") : "",
|
|
45
|
+
execDir ? resolve(execDir, "..", "..", "packages/runtime/native/rig-git.zig") : "",
|
|
46
|
+
execDir ? resolve(execDir, "..", "native", "rig-git.zig") : ""
|
|
47
|
+
].filter(Boolean))];
|
|
48
|
+
}
|
|
49
|
+
function nativePackageBinaryCandidates(fromDir, fileName) {
|
|
50
|
+
const candidates = [];
|
|
51
|
+
let cursor = resolve(fromDir);
|
|
52
|
+
for (let index = 0;index < 8; index += 1) {
|
|
53
|
+
candidates.push(resolve(cursor, "native", `${process.platform}-${process.arch}`, fileName), resolve(cursor, "native", `${process.platform}-${process.arch}`, "bin", fileName), resolve(cursor, "native", fileName), resolve(cursor, "native", "bin", fileName));
|
|
54
|
+
const parent = dirname(cursor);
|
|
55
|
+
if (parent === cursor)
|
|
56
|
+
break;
|
|
57
|
+
cursor = parent;
|
|
58
|
+
}
|
|
59
|
+
return candidates;
|
|
60
|
+
}
|
|
61
|
+
function rigGitBinaryCandidates() {
|
|
62
|
+
const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
|
|
63
|
+
const fileName = runtimeRigGitFileName();
|
|
64
|
+
const explicit = process.env.RIG_NATIVE_GIT_BIN?.trim() || "";
|
|
65
|
+
return [...new Set([
|
|
66
|
+
explicit,
|
|
67
|
+
...nativePackageBinaryCandidates(import.meta.dir, fileName),
|
|
68
|
+
execDir ? resolve(execDir, fileName) : "",
|
|
69
|
+
execDir ? resolve(execDir, "..", fileName) : "",
|
|
70
|
+
execDir ? resolve(execDir, "..", "bin", fileName) : "",
|
|
71
|
+
sharedGitNativeOutputPath
|
|
72
|
+
].filter(Boolean))];
|
|
73
|
+
}
|
|
74
|
+
function resolveGitSourcePath() {
|
|
75
|
+
for (const candidate of rigGitSourceCandidates()) {
|
|
76
|
+
if (candidate && existsSync(candidate)) {
|
|
77
|
+
return candidate;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
function resolveGitBinaryPath() {
|
|
83
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
for (const candidate of rigGitBinaryCandidates()) {
|
|
87
|
+
if (candidate && existsSync(candidate)) {
|
|
88
|
+
return candidate;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
function preferredGitBinaryOutputPath() {
|
|
94
|
+
const explicit = process.env.RIG_NATIVE_GIT_BIN?.trim() || "";
|
|
95
|
+
return explicit || sharedGitNativeOutputPath;
|
|
96
|
+
}
|
|
97
|
+
function binarySupportsTrackerCommandsSync(binaryPath) {
|
|
98
|
+
try {
|
|
99
|
+
const probe = Bun.spawnSync([binaryPath, "fetch-ref", "."], {
|
|
100
|
+
stdout: "pipe",
|
|
101
|
+
stderr: "pipe"
|
|
102
|
+
});
|
|
103
|
+
const stdout = probe.stdout.toString().trim();
|
|
104
|
+
const stderr = probe.stderr.toString().trim();
|
|
105
|
+
if (stdout.includes('"error":"unknown command"')) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
return probe.exitCode === 2 && stderr.includes(trackerCommandUsageProbe);
|
|
109
|
+
} catch {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function nativeBuildManifestPath(outputPath) {
|
|
114
|
+
return `${outputPath}.build-manifest.json`;
|
|
115
|
+
}
|
|
116
|
+
function hasMatchingNativeBuildManifestSync(manifestPath, buildKey) {
|
|
117
|
+
if (!existsSync(manifestPath)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
122
|
+
return manifest.version === 1 && manifest.buildKey === buildKey;
|
|
123
|
+
} catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function sha256FileSync(path) {
|
|
128
|
+
return createHash("sha256").update(readFileSync(path)).digest("hex");
|
|
129
|
+
}
|
|
130
|
+
function ensureRigGitBinaryPathSync(outputPath = preferredGitBinaryOutputPath()) {
|
|
131
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
132
|
+
throw new Error("Zig native git is disabled via RIG_DISABLE_ZIG_NATIVE=1");
|
|
133
|
+
}
|
|
134
|
+
const sourcePath = resolveGitSourcePath();
|
|
135
|
+
if (!sourcePath) {
|
|
136
|
+
const binaryPath = resolveGitBinaryPath();
|
|
137
|
+
if (binaryPath) {
|
|
138
|
+
return binaryPath;
|
|
139
|
+
}
|
|
140
|
+
throw new Error("rig-git.zig source file not found.");
|
|
141
|
+
}
|
|
142
|
+
const zigBinary = Bun.which("zig");
|
|
143
|
+
if (!zigBinary) {
|
|
144
|
+
throw new Error("zig is required to build native Rig git tools.");
|
|
145
|
+
}
|
|
146
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
147
|
+
const sourceDigest = sha256FileSync(sourcePath);
|
|
148
|
+
const buildKey = JSON.stringify({
|
|
149
|
+
version: 1,
|
|
150
|
+
zigBinary,
|
|
151
|
+
platform: process.platform,
|
|
152
|
+
arch: process.arch,
|
|
153
|
+
sourcePath,
|
|
154
|
+
sourceDigest
|
|
155
|
+
});
|
|
156
|
+
const manifestPath = nativeBuildManifestPath(outputPath);
|
|
157
|
+
const needsBuild = !existsSync(outputPath) || !hasMatchingNativeBuildManifestSync(manifestPath, buildKey) || !binarySupportsTrackerCommandsSync(outputPath);
|
|
158
|
+
if (!needsBuild) {
|
|
159
|
+
chmodSync(outputPath, 493);
|
|
160
|
+
return outputPath;
|
|
161
|
+
}
|
|
162
|
+
const tempOutputPath = temporaryGitBinaryOutputPath(outputPath);
|
|
163
|
+
const build = Bun.spawnSync([
|
|
164
|
+
zigBinary,
|
|
165
|
+
"build-exe",
|
|
166
|
+
sourcePath,
|
|
167
|
+
"-O",
|
|
168
|
+
"ReleaseFast",
|
|
169
|
+
`-femit-bin=${tempOutputPath}`
|
|
170
|
+
], {
|
|
171
|
+
cwd: dirname(sourcePath),
|
|
172
|
+
stdout: "pipe",
|
|
173
|
+
stderr: "pipe"
|
|
174
|
+
});
|
|
175
|
+
if (build.exitCode !== 0 || !existsSync(tempOutputPath)) {
|
|
176
|
+
const stderr = build.stderr.toString().trim();
|
|
177
|
+
const stdout = build.stdout.toString().trim();
|
|
178
|
+
const details = [stderr, stdout].filter(Boolean).join(`
|
|
179
|
+
`);
|
|
180
|
+
throw new Error(`Failed to build native Rig git tools: ${details || `zig exited with code ${build.exitCode}`}`);
|
|
181
|
+
}
|
|
182
|
+
chmodSync(tempOutputPath, 493);
|
|
183
|
+
if (existsSync(outputPath) && hasMatchingNativeBuildManifestSync(manifestPath, buildKey)) {
|
|
184
|
+
rmSync(tempOutputPath, { force: true });
|
|
185
|
+
chmodSync(outputPath, 493);
|
|
186
|
+
return outputPath;
|
|
187
|
+
}
|
|
188
|
+
publishGitBinary(tempOutputPath, outputPath);
|
|
189
|
+
if (!binarySupportsTrackerCommandsSync(outputPath)) {
|
|
190
|
+
rmSync(outputPath, { force: true });
|
|
191
|
+
throw new Error("Failed to build native Rig git tools: tracker command probe failed");
|
|
192
|
+
}
|
|
193
|
+
writeFileSync(manifestPath, `${JSON.stringify({ version: 1, buildKey }, null, 2)}
|
|
194
|
+
`, "utf8");
|
|
195
|
+
return outputPath;
|
|
196
|
+
}
|
|
197
|
+
function runGitNative(command, args) {
|
|
198
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
199
|
+
return { ok: false, error: "rig-git native disabled" };
|
|
200
|
+
}
|
|
201
|
+
const trackerCommand = command === "fetch-ref" || command === "read-blob-at-ref" || command === "write-tree-commit" || command === "push-ref-with-lease";
|
|
202
|
+
let binaryPath = null;
|
|
203
|
+
if (trackerCommand) {
|
|
204
|
+
try {
|
|
205
|
+
binaryPath = ensureRigGitBinaryPathSync(preferredGitBinaryOutputPath());
|
|
206
|
+
} catch (error) {
|
|
207
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
208
|
+
if (message.includes("rig-git.zig source file not found")) {
|
|
209
|
+
return { ok: false, error: "rig-git binary not found" };
|
|
210
|
+
}
|
|
211
|
+
return { ok: false, error: message };
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
const explicitBinaryPath = process.env.RIG_NATIVE_GIT_BIN?.trim() || "";
|
|
215
|
+
binaryPath = explicitBinaryPath && existsSync(explicitBinaryPath) ? explicitBinaryPath : !explicitBinaryPath ? resolveGitBinaryPath() : null;
|
|
216
|
+
if (!binaryPath) {
|
|
217
|
+
try {
|
|
218
|
+
binaryPath = ensureRigGitBinaryPathSync(preferredGitBinaryOutputPath());
|
|
219
|
+
} catch (error) {
|
|
220
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
221
|
+
if (message.includes("rig-git.zig source file not found")) {
|
|
222
|
+
return { ok: false, error: "rig-git binary not found" };
|
|
223
|
+
}
|
|
224
|
+
return { ok: false, error: message };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
const proc = Bun.spawnSync([binaryPath, command, ...args], {
|
|
230
|
+
stdout: "pipe",
|
|
231
|
+
stderr: "pipe",
|
|
232
|
+
env: process.env
|
|
233
|
+
});
|
|
234
|
+
if (proc.exitCode !== 0) {
|
|
235
|
+
const stdoutText = proc.stdout.toString().trim();
|
|
236
|
+
if (stdoutText) {
|
|
237
|
+
try {
|
|
238
|
+
const parsed = JSON.parse(stdoutText);
|
|
239
|
+
if (!parsed.ok) {
|
|
240
|
+
return parsed;
|
|
241
|
+
}
|
|
242
|
+
} catch {}
|
|
243
|
+
}
|
|
244
|
+
const errText = proc.stderr.toString().trim() || `exit code ${proc.exitCode}`;
|
|
245
|
+
return { ok: false, error: errText };
|
|
246
|
+
}
|
|
247
|
+
const output = proc.stdout.toString().trim();
|
|
248
|
+
return JSON.parse(output);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
return { ok: false, error: String(err) };
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function requireGitNative(command, args) {
|
|
254
|
+
const result = runGitNative(command, args);
|
|
255
|
+
if (!result.ok) {
|
|
256
|
+
throw new Error(`rig-git ${command} failed: ${result.error}`);
|
|
257
|
+
}
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
function requireGitNativeString(command, args) {
|
|
261
|
+
const result = requireGitNative(command, args);
|
|
262
|
+
if ("value" in result && typeof result.value === "string") {
|
|
263
|
+
return result.value;
|
|
264
|
+
}
|
|
265
|
+
throw new Error(`rig-git ${command} returned an unexpected result payload`);
|
|
266
|
+
}
|
|
267
|
+
function nativeFetchRef(repoPath, remote, branch) {
|
|
268
|
+
return requireGitNativeString("fetch-ref", [repoPath, remote, branch]);
|
|
269
|
+
}
|
|
270
|
+
function nativeReadBlobAtRef(repoPath, ref, path) {
|
|
271
|
+
const requestDir = resolve(sharedGitNativeOutputDir, "reads", `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`);
|
|
272
|
+
mkdirSync(requestDir, { recursive: true });
|
|
273
|
+
const outputPath = resolve(requestDir, "blob.txt");
|
|
274
|
+
try {
|
|
275
|
+
requireGitNative("read-blob-at-ref", [repoPath, ref, path, outputPath]);
|
|
276
|
+
return readFileSync(outputPath, "utf8");
|
|
277
|
+
} finally {
|
|
278
|
+
rmSync(requestDir, { recursive: true, force: true });
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// packages/runtime/src/layout.ts
|
|
283
|
+
import { existsSync as existsSync2 } from "fs";
|
|
284
|
+
import { basename, dirname as dirname2, resolve as resolve2 } from "path";
|
|
285
|
+
function resolveMonorepoRoot(projectRoot) {
|
|
286
|
+
const normalizedProjectRoot = resolve2(projectRoot);
|
|
287
|
+
const explicit = process.env.MONOREPO_ROOT?.trim();
|
|
288
|
+
if (explicit) {
|
|
289
|
+
const explicitRoot = resolve2(explicit);
|
|
290
|
+
const explicitParent = dirname2(explicitRoot);
|
|
291
|
+
if (basename(explicitParent) === ".worktrees") {
|
|
292
|
+
const owner = dirname2(explicitParent);
|
|
293
|
+
const ownerHasGit = existsSync2(resolve2(owner, ".git"));
|
|
294
|
+
const ownerHasTaskConfig = existsSync2(resolve2(owner, ".rig", "task-config.json"));
|
|
295
|
+
const ownerHasRigConfig = existsSync2(resolve2(owner, "rig.config.ts"));
|
|
296
|
+
if (ownerHasGit && (ownerHasTaskConfig || ownerHasRigConfig)) {
|
|
297
|
+
return owner;
|
|
298
|
+
}
|
|
299
|
+
throw new Error(`MONOREPO_ROOT points to worktree ${explicitRoot}, but the owner checkout is incomplete at ${owner}.`);
|
|
300
|
+
}
|
|
301
|
+
if (!existsSync2(resolve2(explicitRoot, ".git"))) {
|
|
302
|
+
throw new Error(`MONOREPO_ROOT points to ${explicitRoot}, but no git checkout was found there.`);
|
|
303
|
+
}
|
|
304
|
+
const hasTaskConfig = existsSync2(resolve2(explicitRoot, ".rig", "task-config.json"));
|
|
305
|
+
const hasRigConfig = existsSync2(resolve2(explicitRoot, "rig.config.ts"));
|
|
306
|
+
if (!hasTaskConfig && !hasRigConfig) {
|
|
307
|
+
throw new Error(`MONOREPO_ROOT points to ${explicitRoot}, but neither .rig/task-config.json nor rig.config.ts exists there.`);
|
|
308
|
+
}
|
|
309
|
+
return explicitRoot;
|
|
310
|
+
}
|
|
311
|
+
const projectParent = dirname2(normalizedProjectRoot);
|
|
312
|
+
if (basename(projectParent) === ".worktrees") {
|
|
313
|
+
const worktreeOwner = dirname2(projectParent);
|
|
314
|
+
const ownerHasGit = existsSync2(resolve2(worktreeOwner, ".git"));
|
|
315
|
+
const ownerHasTaskConfig = existsSync2(resolve2(worktreeOwner, ".rig", "task-config.json"));
|
|
316
|
+
const ownerHasRigConfig = existsSync2(resolve2(worktreeOwner, "rig.config.ts"));
|
|
317
|
+
if (ownerHasGit && (ownerHasTaskConfig || ownerHasRigConfig)) {
|
|
318
|
+
return worktreeOwner;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return normalizedProjectRoot;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// packages/runtime/src/control-plane/native/runtime-native.ts
|
|
325
|
+
import { dlopen, ptr, suffix, toBuffer } from "bun:ffi";
|
|
326
|
+
import { copyFileSync as copyFileSync2, existsSync as existsSync3, mkdirSync as mkdirSync2, renameSync as renameSync2, rmSync as rmSync2, statSync } from "fs";
|
|
327
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
328
|
+
import { dirname as dirname3, resolve as resolve3 } from "path";
|
|
329
|
+
var sharedNativeRuntimeOutputDir = resolve3(tmpdir2(), "rig-native");
|
|
330
|
+
var sharedNativeRuntimeOutputPath = resolve3(sharedNativeRuntimeOutputDir, `runtime-native-${process.platform}-${process.arch}.${suffix}`);
|
|
331
|
+
var colocatedNativeRuntimeFileName = `runtime-native.${suffix}`;
|
|
332
|
+
var nativeRuntimeLibrary = await loadNativeRuntimeLibrary();
|
|
333
|
+
async function ensureNativeRuntimeLibraryPath(outputPath = sharedNativeRuntimeOutputPath, options = {}) {
|
|
334
|
+
if (await buildNativeRuntimeLibrary(outputPath, options)) {
|
|
335
|
+
return outputPath;
|
|
336
|
+
}
|
|
337
|
+
return !options.force && existsSync3(outputPath) ? outputPath : null;
|
|
338
|
+
}
|
|
339
|
+
async function loadNativeRuntimeLibrary() {
|
|
340
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
for (const candidate of nativeRuntimeLibraryCandidates()) {
|
|
344
|
+
if (!candidate || !existsSync3(candidate)) {
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
const loaded = tryDlopenNativeRuntimeLibrary(candidate);
|
|
348
|
+
if (loaded) {
|
|
349
|
+
return loaded;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const builtLibraryPath = await ensureNativeRuntimeLibraryPath(sharedNativeRuntimeOutputPath, { force: true });
|
|
353
|
+
if (!builtLibraryPath) {
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
return tryDlopenNativeRuntimeLibrary(builtLibraryPath);
|
|
357
|
+
}
|
|
358
|
+
function nativePackageLibraryCandidates(fromDir, names) {
|
|
359
|
+
const candidates = [];
|
|
360
|
+
let cursor = resolve3(fromDir);
|
|
361
|
+
for (let index = 0;index < 8; index += 1) {
|
|
362
|
+
for (const name of names) {
|
|
363
|
+
candidates.push(resolve3(cursor, "native", `${process.platform}-${process.arch}`, name), resolve3(cursor, "native", `${process.platform}-${process.arch}`, "lib", name), resolve3(cursor, "native", name), resolve3(cursor, "native", "lib", name));
|
|
364
|
+
}
|
|
365
|
+
const parent = dirname3(cursor);
|
|
366
|
+
if (parent === cursor)
|
|
367
|
+
break;
|
|
368
|
+
cursor = parent;
|
|
369
|
+
}
|
|
370
|
+
return candidates;
|
|
371
|
+
}
|
|
372
|
+
function nativeRuntimeLibraryCandidates() {
|
|
373
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_LIB?.trim() || "";
|
|
374
|
+
const execDir = process.execPath?.trim() ? dirname3(process.execPath.trim()) : "";
|
|
375
|
+
const platformSpecific = `runtime-native-${process.platform}-${process.arch}.${suffix}`;
|
|
376
|
+
return [...new Set([
|
|
377
|
+
explicit,
|
|
378
|
+
...nativePackageLibraryCandidates(import.meta.dir, [colocatedNativeRuntimeFileName, platformSpecific]),
|
|
379
|
+
execDir ? resolve3(execDir, colocatedNativeRuntimeFileName) : "",
|
|
380
|
+
execDir ? resolve3(execDir, platformSpecific) : "",
|
|
381
|
+
execDir ? resolve3(execDir, "..", colocatedNativeRuntimeFileName) : "",
|
|
382
|
+
execDir ? resolve3(execDir, "..", platformSpecific) : "",
|
|
383
|
+
execDir ? resolve3(execDir, "lib", colocatedNativeRuntimeFileName) : "",
|
|
384
|
+
execDir ? resolve3(execDir, "..", "lib", colocatedNativeRuntimeFileName) : "",
|
|
385
|
+
sharedNativeRuntimeOutputPath
|
|
386
|
+
].filter(Boolean))];
|
|
387
|
+
}
|
|
388
|
+
function resolveNativeRuntimeSourcePath() {
|
|
389
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_SOURCE?.trim();
|
|
390
|
+
if (explicit && existsSync3(explicit)) {
|
|
391
|
+
return explicit;
|
|
392
|
+
}
|
|
393
|
+
const bundled = resolve3(import.meta.dir, "../../../native/snapshot.zig");
|
|
394
|
+
return existsSync3(bundled) ? bundled : null;
|
|
395
|
+
}
|
|
396
|
+
async function buildNativeRuntimeLibrary(outputPath, options = {}) {
|
|
397
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
const zigBinary = Bun.which("zig");
|
|
401
|
+
const sourcePath = resolveNativeRuntimeSourcePath();
|
|
402
|
+
if (!zigBinary || !sourcePath) {
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
const tempOutputPath = `${outputPath}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
406
|
+
try {
|
|
407
|
+
mkdirSync2(dirname3(outputPath), { recursive: true });
|
|
408
|
+
const needsBuild = options.force === true || !existsSync3(outputPath) || statSync(sourcePath).mtimeMs > statSync(outputPath).mtimeMs;
|
|
409
|
+
if (!needsBuild) {
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
const build = Bun.spawn([
|
|
413
|
+
zigBinary,
|
|
414
|
+
"build-lib",
|
|
415
|
+
sourcePath,
|
|
416
|
+
"-dynamic",
|
|
417
|
+
"-O",
|
|
418
|
+
"ReleaseFast",
|
|
419
|
+
`-femit-bin=${tempOutputPath}`
|
|
420
|
+
], {
|
|
421
|
+
cwd: import.meta.dir,
|
|
422
|
+
stdout: "pipe",
|
|
423
|
+
stderr: "pipe"
|
|
424
|
+
});
|
|
425
|
+
const exitCode = await build.exited;
|
|
426
|
+
if (exitCode !== 0 || !existsSync3(tempOutputPath)) {
|
|
427
|
+
rmSync2(tempOutputPath, { force: true });
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
renameSync2(tempOutputPath, outputPath);
|
|
431
|
+
return true;
|
|
432
|
+
} catch {
|
|
433
|
+
rmSync2(tempOutputPath, { force: true });
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function tryDlopenNativeRuntimeLibrary(outputPath) {
|
|
438
|
+
try {
|
|
439
|
+
return dlopen(outputPath, {
|
|
440
|
+
rig_scope_match: {
|
|
441
|
+
args: ["ptr", "ptr"],
|
|
442
|
+
returns: "u8"
|
|
443
|
+
},
|
|
444
|
+
snapshot_capture: {
|
|
445
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
446
|
+
returns: "ptr"
|
|
447
|
+
},
|
|
448
|
+
snapshot_delta: {
|
|
449
|
+
args: ["ptr", "ptr"],
|
|
450
|
+
returns: "ptr"
|
|
451
|
+
},
|
|
452
|
+
snapshot_store_delta: {
|
|
453
|
+
args: ["ptr", "ptr", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
454
|
+
returns: "ptr"
|
|
455
|
+
},
|
|
456
|
+
snapshot_inspect_delta: {
|
|
457
|
+
args: ["ptr", "u64"],
|
|
458
|
+
returns: "ptr"
|
|
459
|
+
},
|
|
460
|
+
snapshot_apply_delta: {
|
|
461
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
462
|
+
returns: "ptr"
|
|
463
|
+
},
|
|
464
|
+
snapshot_release: {
|
|
465
|
+
args: ["ptr"],
|
|
466
|
+
returns: "void"
|
|
467
|
+
},
|
|
468
|
+
runtime_hash_file: {
|
|
469
|
+
args: ["ptr", "u64"],
|
|
470
|
+
returns: "ptr"
|
|
471
|
+
},
|
|
472
|
+
runtime_hash_tree: {
|
|
473
|
+
args: ["ptr", "u64"],
|
|
474
|
+
returns: "ptr"
|
|
475
|
+
},
|
|
476
|
+
runtime_prepare_paths: {
|
|
477
|
+
args: ["ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
478
|
+
returns: "ptr"
|
|
479
|
+
},
|
|
480
|
+
runtime_link_dependency_layer: {
|
|
481
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
482
|
+
returns: "ptr"
|
|
483
|
+
},
|
|
484
|
+
runtime_scan_worktrees: {
|
|
485
|
+
args: ["ptr", "u64"],
|
|
486
|
+
returns: "ptr"
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
} catch {
|
|
490
|
+
return null;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// packages/runtime/src/control-plane/native/utils.ts
|
|
495
|
+
function resolveMonorepoRoot2(projectRoot) {
|
|
496
|
+
return resolveMonorepoRoot(projectRoot);
|
|
497
|
+
}
|
|
498
|
+
var scopeRegexCache = new Map;
|
|
499
|
+
|
|
500
|
+
// packages/runtime/src/control-plane/runtime/context.ts
|
|
501
|
+
var RUNTIME_CONTEXT_ENV = "RIG_RUNTIME_CONTEXT_FILE";
|
|
502
|
+
|
|
503
|
+
// packages/runtime/src/control-plane/state-sync/repo.ts
|
|
504
|
+
import { existsSync as existsSync5 } from "fs";
|
|
505
|
+
import { resolve as resolve5 } from "path";
|
|
506
|
+
|
|
507
|
+
// packages/runtime/src/control-plane/repos/layout.ts
|
|
508
|
+
import { existsSync as existsSync4 } from "fs";
|
|
509
|
+
import { basename as basename2, dirname as dirname4, join, resolve as resolve4 } from "path";
|
|
510
|
+
|
|
511
|
+
// packages/runtime/src/control-plane/repos/registry.ts
|
|
512
|
+
var MANAGED_REPOS = new Map;
|
|
513
|
+
function getManagedRepoEntry(repoId) {
|
|
514
|
+
const entry = MANAGED_REPOS.get(repoId);
|
|
515
|
+
if (!entry) {
|
|
516
|
+
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.`);
|
|
517
|
+
}
|
|
518
|
+
return entry;
|
|
519
|
+
}
|
|
520
|
+
function listManagedRepoEntries() {
|
|
521
|
+
return Array.from(MANAGED_REPOS.values());
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// packages/runtime/src/control-plane/repos/layout.ts
|
|
525
|
+
function resolveRepoStateDir(projectRoot) {
|
|
526
|
+
const normalizedProjectRoot = resolve4(projectRoot);
|
|
527
|
+
const projectParent = dirname4(normalizedProjectRoot);
|
|
528
|
+
if (basename2(projectParent) === ".worktrees") {
|
|
529
|
+
const ownerRoot = dirname4(projectParent);
|
|
530
|
+
const ownerHasRepoMarkers = existsSync4(resolve4(ownerRoot, ".git")) || existsSync4(resolve4(ownerRoot, ".rig", "state"));
|
|
531
|
+
if (ownerHasRepoMarkers) {
|
|
532
|
+
return resolve4(ownerRoot, ".rig", "state");
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return resolve4(projectRoot, ".rig", "state");
|
|
536
|
+
}
|
|
537
|
+
function resolveManagedRepoLayout(projectRoot, repoId) {
|
|
538
|
+
const normalizedProjectRoot = resolve4(projectRoot);
|
|
539
|
+
const entry = getManagedRepoEntry(repoId);
|
|
540
|
+
const stateDir = resolveRepoStateDir(normalizedProjectRoot);
|
|
541
|
+
const metadataRelativePath = join("repos", entry.id);
|
|
542
|
+
const metadataRoot = resolve4(stateDir, metadataRelativePath);
|
|
543
|
+
const runtimeWorkspace = process.env.RIG_TASK_WORKSPACE?.trim();
|
|
544
|
+
const runsInsideTaskWorktree = runtimeWorkspace && resolve4(runtimeWorkspace) === normalizedProjectRoot || basename2(dirname4(normalizedProjectRoot)) === ".worktrees";
|
|
545
|
+
const isPrimaryManagedRepo = listManagedRepoEntries()[0]?.id === repoId;
|
|
546
|
+
const checkoutRoot = isPrimaryManagedRepo && runsInsideTaskWorktree ? resolveMonorepoRoot(normalizedProjectRoot) : entry.checkoutEnvVar && process.env[entry.checkoutEnvVar]?.trim() ? resolve4(process.env[entry.checkoutEnvVar].trim()) : resolve4(normalizedProjectRoot, entry.alias);
|
|
547
|
+
return {
|
|
548
|
+
projectRoot: normalizedProjectRoot,
|
|
549
|
+
repoId: entry.id,
|
|
550
|
+
alias: entry.alias,
|
|
551
|
+
defaultBranch: entry.defaultBranch,
|
|
552
|
+
remoteUrl: entry.remoteEnvVar && process.env[entry.remoteEnvVar]?.trim() ? process.env[entry.remoteEnvVar].trim() : entry.defaultRemoteUrl,
|
|
553
|
+
checkoutRoot,
|
|
554
|
+
worktreesRoot: resolve4(checkoutRoot, ".worktrees"),
|
|
555
|
+
stateDir,
|
|
556
|
+
metadataRoot,
|
|
557
|
+
metadataRelativePath,
|
|
558
|
+
mirrorRoot: resolve4(metadataRoot, "mirror.git"),
|
|
559
|
+
mirrorStatePath: resolve4(metadataRoot, "mirror-state.json"),
|
|
560
|
+
mirrorStateRelativePath: join(metadataRelativePath, "mirror-state.json")
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
function resolveMonorepoRepoLayout(projectRoot) {
|
|
564
|
+
const entries = listManagedRepoEntries();
|
|
565
|
+
if (entries.length === 0) {
|
|
566
|
+
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.");
|
|
567
|
+
}
|
|
568
|
+
const primary = entries[0];
|
|
569
|
+
return resolveManagedRepoLayout(projectRoot, primary.id);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// packages/runtime/src/control-plane/state-sync/repo.ts
|
|
573
|
+
function resolveTrackerRepoPath(projectRoot) {
|
|
574
|
+
const monorepoRoot = resolveMonorepoRoot2(projectRoot);
|
|
575
|
+
try {
|
|
576
|
+
const layout = resolveMonorepoRepoLayout(projectRoot);
|
|
577
|
+
if (existsSync5(resolve5(layout.mirrorRoot, "HEAD"))) {
|
|
578
|
+
return layout.mirrorRoot;
|
|
579
|
+
}
|
|
580
|
+
} catch {}
|
|
581
|
+
return monorepoRoot;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// packages/runtime/src/control-plane/state-sync/types.ts
|
|
585
|
+
var SUPPORTED_TASK_STATE_SCHEMA_VERSION = 1;
|
|
586
|
+
var CANONICAL_TASK_LIFECYCLE_STATUSES = new Set([
|
|
587
|
+
"draft",
|
|
588
|
+
"open",
|
|
589
|
+
"ready",
|
|
590
|
+
"queued",
|
|
591
|
+
"in_progress",
|
|
592
|
+
"under_review",
|
|
593
|
+
"blocked",
|
|
594
|
+
"completed",
|
|
595
|
+
"cancelled"
|
|
596
|
+
]);
|
|
597
|
+
function normalizeTaskLifecycleStatus(status) {
|
|
598
|
+
switch (status) {
|
|
599
|
+
case "draft":
|
|
600
|
+
case "open":
|
|
601
|
+
case "ready":
|
|
602
|
+
case "queued":
|
|
603
|
+
case "in_progress":
|
|
604
|
+
case "under_review":
|
|
605
|
+
case "blocked":
|
|
606
|
+
case "completed":
|
|
607
|
+
case "cancelled":
|
|
608
|
+
return status;
|
|
609
|
+
case "closed":
|
|
610
|
+
return "completed";
|
|
611
|
+
case "running":
|
|
612
|
+
return "in_progress";
|
|
613
|
+
case "failed":
|
|
614
|
+
return "ready";
|
|
615
|
+
default:
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
function normalizeTaskStateMetadataStatus(status) {
|
|
620
|
+
if (CANONICAL_TASK_LIFECYCLE_STATUSES.has(status)) {
|
|
621
|
+
return status;
|
|
622
|
+
}
|
|
623
|
+
switch (status) {
|
|
624
|
+
case "closed":
|
|
625
|
+
return "completed";
|
|
626
|
+
case "running":
|
|
627
|
+
return "in_progress";
|
|
628
|
+
case "failed":
|
|
629
|
+
return "ready";
|
|
630
|
+
default:
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
function canonicalizeTaskStateMetadata(raw) {
|
|
635
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
636
|
+
return null;
|
|
637
|
+
}
|
|
638
|
+
const metadata = raw;
|
|
639
|
+
const claimId = typeof metadata.claimId === "string" && metadata.claimId.length > 0 ? metadata.claimId : undefined;
|
|
640
|
+
const status = normalizeTaskStateMetadataStatus(metadata.status);
|
|
641
|
+
if (!status) {
|
|
642
|
+
return null;
|
|
643
|
+
}
|
|
644
|
+
return {
|
|
645
|
+
...claimId ? { claimId } : {},
|
|
646
|
+
status,
|
|
647
|
+
...typeof metadata.ownerId === "string" && metadata.ownerId.length > 0 ? { ownerId: metadata.ownerId } : {},
|
|
648
|
+
...typeof metadata.claimedAt === "string" && metadata.claimedAt.length > 0 ? { claimedAt: metadata.claimedAt } : {},
|
|
649
|
+
...typeof metadata.lastEvidenceAt === "string" && metadata.lastEvidenceAt.length > 0 ? { lastEvidenceAt: metadata.lastEvidenceAt } : {},
|
|
650
|
+
...typeof metadata.runId === "string" && metadata.runId.length > 0 ? { runId: metadata.runId } : {},
|
|
651
|
+
...typeof metadata.branchName === "string" && metadata.branchName.length > 0 ? { branchName: metadata.branchName } : {},
|
|
652
|
+
...typeof metadata.prNumber === "number" ? { prNumber: metadata.prNumber } : {},
|
|
653
|
+
...typeof metadata.prUrl === "string" && metadata.prUrl.length > 0 ? { prUrl: metadata.prUrl } : {},
|
|
654
|
+
...typeof metadata.reviewState === "string" && metadata.reviewState.length > 0 ? { reviewState: metadata.reviewState } : {},
|
|
655
|
+
...typeof metadata.blockerReason === "string" && metadata.blockerReason.length > 0 ? { blockerReason: metadata.blockerReason } : {},
|
|
656
|
+
...typeof metadata.sourceCommit === "string" && metadata.sourceCommit.length > 0 ? { sourceCommit: metadata.sourceCommit } : {}
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
function readTaskStateMetadataEnvelope(raw) {
|
|
660
|
+
if (!raw || typeof raw !== "object") {
|
|
661
|
+
return { schemaVersion: SUPPORTED_TASK_STATE_SCHEMA_VERSION, supported: true, baseTrackerCommit: null, tasks: {} };
|
|
662
|
+
}
|
|
663
|
+
const envelope = raw;
|
|
664
|
+
const schemaVersion = typeof envelope.schemaVersion === "number" ? envelope.schemaVersion : SUPPORTED_TASK_STATE_SCHEMA_VERSION;
|
|
665
|
+
if (schemaVersion !== SUPPORTED_TASK_STATE_SCHEMA_VERSION) {
|
|
666
|
+
return { schemaVersion, supported: false, baseTrackerCommit: null, tasks: {} };
|
|
667
|
+
}
|
|
668
|
+
const rawTasks = envelope.tasks && typeof envelope.tasks === "object" && !Array.isArray(envelope.tasks) ? envelope.tasks : {};
|
|
669
|
+
const tasks = Object.fromEntries(Object.entries(rawTasks).map(([taskId, metadata]) => [taskId, canonicalizeTaskStateMetadata(metadata)]).filter((entry) => entry[1] != null));
|
|
670
|
+
return {
|
|
671
|
+
schemaVersion,
|
|
672
|
+
supported: true,
|
|
673
|
+
baseTrackerCommit: typeof envelope.baseTrackerCommit === "string" && envelope.baseTrackerCommit.length > 0 ? envelope.baseTrackerCommit : null,
|
|
674
|
+
tasks
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// packages/runtime/src/control-plane/state-sync/read.ts
|
|
679
|
+
var DEFAULT_READ_DEPS = {
|
|
680
|
+
fetchRef: nativeFetchRef,
|
|
681
|
+
readBlobAtRef: nativeReadBlobAtRef,
|
|
682
|
+
exists: existsSync6,
|
|
683
|
+
readFile: (path) => readFileSync2(path, "utf8")
|
|
684
|
+
};
|
|
685
|
+
function parseIssueStatus(rawStatus) {
|
|
686
|
+
const normalized = normalizeTaskLifecycleStatus(rawStatus);
|
|
687
|
+
return normalized ?? "unknown";
|
|
688
|
+
}
|
|
689
|
+
function parseIssueDependencies(raw) {
|
|
690
|
+
if (!Array.isArray(raw)) {
|
|
691
|
+
return [];
|
|
692
|
+
}
|
|
693
|
+
return raw.filter((entry) => !!entry && typeof entry === "object" && !Array.isArray(entry)).map((entry) => ({
|
|
694
|
+
issueId: typeof entry.issue_id === "string" && entry.issue_id.trim() ? entry.issue_id.trim() : null,
|
|
695
|
+
dependsOnId: typeof entry.depends_on_id === "string" && entry.depends_on_id.trim() ? entry.depends_on_id.trim() : null,
|
|
696
|
+
id: typeof entry.id === "string" && entry.id.trim() ? entry.id.trim() : null,
|
|
697
|
+
type: typeof entry.type === "string" && entry.type.trim() ? entry.type.trim() : null
|
|
698
|
+
}));
|
|
699
|
+
}
|
|
700
|
+
function dependencyTargetId(dependency) {
|
|
701
|
+
for (const candidate of [dependency.dependsOnId, dependency.id ?? null, dependency.issueId]) {
|
|
702
|
+
if (typeof candidate === "string" && candidate.trim()) {
|
|
703
|
+
return candidate.trim();
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return null;
|
|
707
|
+
}
|
|
708
|
+
function parseIssuesJsonl(raw) {
|
|
709
|
+
const issues = [];
|
|
710
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
711
|
+
const trimmed = line.trim();
|
|
712
|
+
if (!trimmed) {
|
|
713
|
+
continue;
|
|
714
|
+
}
|
|
715
|
+
try {
|
|
716
|
+
const record = JSON.parse(trimmed);
|
|
717
|
+
const id = typeof record.id === "string" && record.id.trim() ? record.id.trim() : "";
|
|
718
|
+
if (!id) {
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
const rawStatus = typeof record.status === "string" && record.status.trim() ? record.status.trim() : null;
|
|
722
|
+
issues.push({
|
|
723
|
+
id,
|
|
724
|
+
title: typeof record.title === "string" && record.title.trim() ? record.title.trim() : null,
|
|
725
|
+
description: typeof record.description === "string" && record.description.trim() ? record.description.trim() : null,
|
|
726
|
+
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,
|
|
727
|
+
issueType: typeof record.issue_type === "string" && record.issue_type.trim() ? record.issue_type.trim() : null,
|
|
728
|
+
status: parseIssueStatus(rawStatus),
|
|
729
|
+
rawStatus,
|
|
730
|
+
priority: typeof record.priority === "number" ? record.priority : null,
|
|
731
|
+
dependencies: parseIssueDependencies(record.dependencies)
|
|
732
|
+
});
|
|
733
|
+
} catch {}
|
|
734
|
+
}
|
|
735
|
+
return issues;
|
|
736
|
+
}
|
|
737
|
+
function parseTaskStateEnvelope(raw) {
|
|
738
|
+
if (!raw || !raw.trim()) {
|
|
739
|
+
return readTaskStateMetadataEnvelope(null);
|
|
740
|
+
}
|
|
741
|
+
try {
|
|
742
|
+
return readTaskStateMetadataEnvelope(JSON.parse(raw));
|
|
743
|
+
} catch {
|
|
744
|
+
return readTaskStateMetadataEnvelope(null);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
function readRemoteBlobAtRef(deps, repoPath, ref, path, options) {
|
|
748
|
+
try {
|
|
749
|
+
return deps.readBlobAtRef(repoPath, ref, path);
|
|
750
|
+
} catch (error) {
|
|
751
|
+
if (options.required) {
|
|
752
|
+
throw error;
|
|
753
|
+
}
|
|
754
|
+
return null;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
function shouldPreferLocalTrackerState(options) {
|
|
758
|
+
if (!options.allowLocalFallback) {
|
|
759
|
+
return false;
|
|
760
|
+
}
|
|
761
|
+
const runtimeWorkspace = process.env.RIG_TASK_WORKSPACE?.trim();
|
|
762
|
+
if (!runtimeWorkspace) {
|
|
763
|
+
return false;
|
|
764
|
+
}
|
|
765
|
+
if (process.env.RIG_TASK_RUNTIME_ID?.trim()) {
|
|
766
|
+
return true;
|
|
767
|
+
}
|
|
768
|
+
const runtimeContextPath = process.env[RUNTIME_CONTEXT_ENV]?.trim();
|
|
769
|
+
if (runtimeContextPath) {
|
|
770
|
+
return true;
|
|
771
|
+
}
|
|
772
|
+
return existsSync6(resolve6(runtimeWorkspace, ".rig", "runtime-context.json"));
|
|
773
|
+
}
|
|
774
|
+
function readLocalTrackerState(projectRoot, deps) {
|
|
775
|
+
const monorepoRoot = resolveMonorepoRoot2(projectRoot);
|
|
776
|
+
const issuesPath = resolve6(monorepoRoot, ".beads", "issues.jsonl");
|
|
777
|
+
const taskStatePath = resolve6(monorepoRoot, ".beads", "task-state.json");
|
|
778
|
+
return projectSyncedTrackerSnapshot({
|
|
779
|
+
source: "local",
|
|
780
|
+
issuesBaseOid: null,
|
|
781
|
+
issuesText: deps.exists(issuesPath) ? deps.readFile(issuesPath) : "",
|
|
782
|
+
taskStateBaseOid: null,
|
|
783
|
+
taskStateText: deps.exists(taskStatePath) ? deps.readFile(taskStatePath) : null
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
function projectSyncedTrackerSnapshot(input) {
|
|
787
|
+
if (input.source === "remote" && input.issuesBaseOid && input.taskStateBaseOid && input.issuesBaseOid !== input.taskStateBaseOid) {
|
|
788
|
+
throw new Error("Remote tracker files must be read from the same fetched base.");
|
|
789
|
+
}
|
|
790
|
+
return {
|
|
791
|
+
source: input.source,
|
|
792
|
+
baseOid: input.issuesBaseOid ?? input.taskStateBaseOid ?? null,
|
|
793
|
+
issues: parseIssuesJsonl(input.issuesText),
|
|
794
|
+
taskState: parseTaskStateEnvelope(input.taskStateText)
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
function readSyncedTrackerState(projectRoot, deps = {}, options = {}) {
|
|
798
|
+
const readDeps = { ...DEFAULT_READ_DEPS, ...deps };
|
|
799
|
+
const trackerRepoPath = resolveTrackerRepoPath(projectRoot);
|
|
800
|
+
if (shouldPreferLocalTrackerState(options)) {
|
|
801
|
+
return readLocalTrackerState(projectRoot, readDeps);
|
|
802
|
+
}
|
|
803
|
+
try {
|
|
804
|
+
const baseOid = readDeps.fetchRef(trackerRepoPath, "origin", "main");
|
|
805
|
+
return projectSyncedTrackerSnapshot({
|
|
806
|
+
source: "remote",
|
|
807
|
+
issuesBaseOid: baseOid,
|
|
808
|
+
issuesText: readRemoteBlobAtRef(readDeps, trackerRepoPath, baseOid, ".beads/issues.jsonl", {
|
|
809
|
+
required: true
|
|
810
|
+
}) ?? "",
|
|
811
|
+
taskStateBaseOid: baseOid,
|
|
812
|
+
taskStateText: readRemoteBlobAtRef(readDeps, trackerRepoPath, baseOid, ".beads/task-state.json", {
|
|
813
|
+
required: false
|
|
814
|
+
})
|
|
815
|
+
});
|
|
816
|
+
} catch (error) {
|
|
817
|
+
if (!options.allowLocalFallback) {
|
|
818
|
+
throw error;
|
|
819
|
+
}
|
|
820
|
+
return readLocalTrackerState(projectRoot, readDeps);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
function listReadyTaskIdsFromTracker(snapshot) {
|
|
824
|
+
const tasks = snapshot.issues.filter((issue) => issue.issueType === "task");
|
|
825
|
+
const taskById = new Map(tasks.map((issue) => [issue.id, issue]));
|
|
826
|
+
const readyTaskIds = [];
|
|
827
|
+
for (const issue of tasks) {
|
|
828
|
+
if (issue.status === "ready") {
|
|
829
|
+
readyTaskIds.push(issue.id);
|
|
830
|
+
continue;
|
|
831
|
+
}
|
|
832
|
+
if (issue.status !== "open") {
|
|
833
|
+
continue;
|
|
834
|
+
}
|
|
835
|
+
const hasOpenBlocker = issue.dependencies.some((dependency) => {
|
|
836
|
+
if (dependency.type === "parent-child") {
|
|
837
|
+
return false;
|
|
838
|
+
}
|
|
839
|
+
const targetTaskId = dependencyTargetId(dependency);
|
|
840
|
+
if (!targetTaskId) {
|
|
841
|
+
return false;
|
|
842
|
+
}
|
|
843
|
+
const dependencyStatus = taskById.get(targetTaskId)?.status ?? "unknown";
|
|
844
|
+
return dependencyStatus !== "completed" && dependencyStatus !== "cancelled";
|
|
845
|
+
});
|
|
846
|
+
if (!hasOpenBlocker) {
|
|
847
|
+
readyTaskIds.push(issue.id);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
return readyTaskIds;
|
|
851
|
+
}
|
|
852
|
+
export {
|
|
853
|
+
readSyncedTrackerState,
|
|
854
|
+
projectSyncedTrackerSnapshot,
|
|
855
|
+
listReadyTaskIdsFromTracker
|
|
856
|
+
};
|