@h-rig/core 0.0.6-alpha.17 → 0.0.6-alpha.170

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.
Files changed (107) hide show
  1. package/dist/src/agent-role-registry.d.ts +4 -0
  2. package/dist/src/agent-role-registry.js +27 -0
  3. package/dist/src/authority-paths.d.ts +15 -0
  4. package/dist/src/authority-paths.js +80 -0
  5. package/dist/src/baked-secrets.d.ts +6 -0
  6. package/dist/src/baked-secrets.js +121 -0
  7. package/dist/src/build-time-config.d.ts +12 -0
  8. package/dist/src/build-time-config.js +25 -0
  9. package/dist/src/build-time-config.macro.d.ts +1 -0
  10. package/dist/src/capability-loaders.d.ts +51 -0
  11. package/dist/src/capability-loaders.js +758 -0
  12. package/dist/src/capability.d.ts +79 -0
  13. package/dist/src/capability.js +63 -0
  14. package/dist/src/checkout-root.d.ts +1 -0
  15. package/dist/src/checkout-root.js +30 -0
  16. package/dist/src/config-env.d.ts +4 -0
  17. package/dist/src/config-env.js +23 -0
  18. package/dist/src/config.d.ts +3 -0
  19. package/dist/src/config.js +44 -0
  20. package/dist/src/declarative-config.d.ts +14 -0
  21. package/dist/src/declarative-config.js +82 -0
  22. package/dist/src/default-kernel.d.ts +1 -0
  23. package/dist/src/default-kernel.js +12 -0
  24. package/dist/src/define-config.d.ts +20 -0
  25. package/dist/src/define-config.js +28 -15
  26. package/dist/src/define-plugin.d.ts +13 -0
  27. package/dist/src/define-plugin.js +4 -43
  28. package/dist/src/embedded-plugins.d.ts +59 -0
  29. package/dist/src/embedded-plugins.js +22 -0
  30. package/dist/src/exec.d.ts +13 -0
  31. package/dist/src/exec.js +101 -0
  32. package/dist/src/harness-paths.d.ts +18 -0
  33. package/dist/src/harness-paths.js +141 -0
  34. package/dist/src/hook-materializer.d.ts +72 -0
  35. package/dist/src/hook-materializer.js +281 -0
  36. package/dist/src/hook-protocol.d.ts +2 -0
  37. package/dist/src/hook-protocol.js +462 -0
  38. package/dist/src/hook-runner.d.ts +48 -0
  39. package/dist/src/hook-runner.js +756 -0
  40. package/dist/src/hook-runtime.d.ts +52 -0
  41. package/dist/src/hook-runtime.js +462 -0
  42. package/dist/src/index.d.ts +8 -0
  43. package/dist/src/index.js +210 -2499
  44. package/dist/src/json-files.d.ts +9 -0
  45. package/dist/src/json-files.js +125 -0
  46. package/dist/src/kernel-boot.d.ts +2 -0
  47. package/dist/src/kernel-boot.js +10 -0
  48. package/dist/src/kernel-entrypoint.d.ts +22 -0
  49. package/dist/src/kernel-entrypoint.js +548 -0
  50. package/dist/src/kernel-plugin-abi.d.ts +1 -0
  51. package/dist/src/kernel-plugin-abi.js +1 -0
  52. package/dist/src/kernel-resolver.d.ts +2 -0
  53. package/dist/src/kernel-resolver.js +6 -0
  54. package/dist/src/layout.d.ts +10 -0
  55. package/dist/src/layout.js +144 -0
  56. package/dist/src/load-config.d.ts +2 -0
  57. package/dist/src/load-config.js +423 -30
  58. package/dist/src/placement.d.ts +50 -0
  59. package/dist/src/placement.js +996 -0
  60. package/dist/src/plugin-host-context.d.ts +66 -0
  61. package/dist/src/plugin-host-context.js +1292 -0
  62. package/dist/src/plugin-host-registries.d.ts +31 -0
  63. package/dist/src/plugin-host-registries.js +79 -0
  64. package/dist/src/plugin-host.d.ts +77 -0
  65. package/dist/src/plugin-host.js +127 -63
  66. package/dist/src/plugin-runtime.d.ts +173 -0
  67. package/dist/src/profile-ops.d.ts +9 -0
  68. package/dist/src/profile-ops.js +252 -0
  69. package/dist/src/project-plugins.d.ts +63 -0
  70. package/dist/src/project-plugins.js +793 -0
  71. package/dist/src/remote-config.d.ts +183 -0
  72. package/dist/src/remote-config.js +574 -0
  73. package/dist/src/root-resolver.d.ts +5 -0
  74. package/dist/src/root-resolver.js +69 -0
  75. package/dist/src/run-provisioning.d.ts +58 -0
  76. package/dist/src/run-provisioning.js +128 -0
  77. package/dist/src/runtime-context.d.ts +20 -0
  78. package/dist/src/runtime-context.js +257 -0
  79. package/dist/src/runtime-events.d.ts +44 -0
  80. package/dist/src/runtime-events.js +212 -0
  81. package/dist/src/runtime-overlay.d.ts +11 -0
  82. package/dist/src/runtime-overlay.js +71 -0
  83. package/dist/src/runtime-paths.d.ts +21 -0
  84. package/dist/src/runtime-paths.js +181 -0
  85. package/dist/src/runtime-provisioning-env.d.ts +5 -0
  86. package/dist/src/runtime-provisioning-env.js +217 -0
  87. package/dist/src/runtime-runner-context.d.ts +12 -0
  88. package/dist/src/runtime-runner-context.js +1 -0
  89. package/dist/src/safe-identifiers.d.ts +44 -0
  90. package/dist/src/safe-identifiers.js +96 -0
  91. package/dist/src/scope-rules.d.ts +4 -0
  92. package/dist/src/scope-rules.js +21 -0
  93. package/dist/src/server-paths.d.ts +26 -0
  94. package/dist/src/server-paths.js +308 -0
  95. package/dist/src/setup-version.d.ts +3 -0
  96. package/dist/src/setup-version.js +14 -0
  97. package/dist/src/task-record-reader.d.ts +3 -0
  98. package/dist/src/task-record-reader.js +9 -0
  99. package/dist/src/validator-registry.d.ts +27 -0
  100. package/dist/src/validator-registry.js +64 -0
  101. package/package.json +166 -10
  102. package/dist/src/engineReadModelReducer.js +0 -1780
  103. package/dist/src/rig-init-builder.js +0 -57
  104. package/dist/src/rigSelectors.js +0 -293
  105. package/dist/src/taskGraph.js +0 -64
  106. package/dist/src/taskGraphCodes.js +0 -26
  107. package/dist/src/taskGraphLayout.js +0 -374
@@ -0,0 +1,58 @@
1
+ export declare const RUN_WORKSPACE_ISOLATION_MODE: "worktree";
2
+ export declare const RUN_WORKSPACE_PROVIDER: "pi";
3
+ export declare const REMOTE_RIG_RUN_COMMAND: "rig-run";
4
+ export declare const REMOTE_RESOLVED_RIG_RUN_COMMAND: "\"$__RIG_RUN_BIN\"";
5
+ export type RunWorkspaceProvisioningOptions = {
6
+ readonly projectRoot: string;
7
+ readonly id: string;
8
+ readonly taskId: string;
9
+ readonly mode: typeof RUN_WORKSPACE_ISOLATION_MODE;
10
+ readonly provider: typeof RUN_WORKSPACE_PROVIDER;
11
+ };
12
+ export type RigRunLaunchOptions = {
13
+ readonly taskId: string;
14
+ readonly title?: string | null | undefined;
15
+ readonly model?: string | null | undefined;
16
+ readonly prompt?: string | null | undefined;
17
+ readonly forceSteerOnce?: boolean | undefined;
18
+ };
19
+ export type RemoteCheckoutPlan = {
20
+ readonly mode: "configured" | "managed";
21
+ readonly repoName: string;
22
+ readonly checkoutPath: string;
23
+ readonly checkoutPathKind: "literal" | "home-relative";
24
+ readonly originUrl: string | null;
25
+ };
26
+ export type RunGithubAuthMaterialization = {
27
+ readonly operatorAuthPath: string;
28
+ readonly operatorStateDir: string;
29
+ readonly remoteAuthPath: string;
30
+ readonly remoteStateDir: string;
31
+ };
32
+ export declare function buildRunWorkspaceProvisioningOptions(input: {
33
+ readonly projectRoot: string;
34
+ readonly runId: string;
35
+ readonly taskId: string;
36
+ }): RunWorkspaceProvisioningOptions;
37
+ export declare function buildRigRunLaunchArgs(input: RigRunLaunchOptions, runId: string, projectRoot: string): string[];
38
+ export declare function resolveRemoteCheckoutPlan(input: {
39
+ readonly selectedCheckout?: string | null;
40
+ readonly projectSlug?: string | null;
41
+ }): RemoteCheckoutPlan;
42
+ export declare function resolveRunGithubAuthMaterialization(projectRoot: string, checkoutExpression?: string): RunGithubAuthMaterialization;
43
+ export declare function readGithubAuthStateBase64(operatorAuthPath: string): string;
44
+ export declare function buildRemoteRigRunResolutionShell(installUrl: string, command?: "rig-run"): string[];
45
+ export declare function isCompiledBunBinary(importMetaUrl: string): boolean;
46
+ export declare function resolveCompiledRigRunSiblingPath(execPath: string, platform?: NodeJS.Platform): string;
47
+ export declare function ensureRigRunSiblingCurrent(rigBin: string, rigRun: string): void;
48
+ export declare function resolveLocalDispatchRigRunBin(input: {
49
+ readonly importMetaUrl: string;
50
+ readonly sourceRigRunPath: string;
51
+ readonly execPath?: string;
52
+ readonly platform?: NodeJS.Platform;
53
+ }): string;
54
+ export declare function resolveInProcessRigRunBin(input: {
55
+ readonly importMetaUrl: string;
56
+ readonly sourceRigRunPath: string;
57
+ readonly execPath?: string;
58
+ }): string;
@@ -0,0 +1,128 @@
1
+ // @bun
2
+ // packages/core/src/run-provisioning.ts
3
+ import { chmodSync, copyFileSync, existsSync, linkSync, readFileSync, renameSync, rmSync, statSync } from "fs";
4
+ import { dirname, resolve } from "path";
5
+ var RUN_WORKSPACE_ISOLATION_MODE = "worktree";
6
+ var RUN_WORKSPACE_PROVIDER = "pi";
7
+ var REMOTE_RIG_RUN_COMMAND = "rig-run";
8
+ var REMOTE_RESOLVED_RIG_RUN_COMMAND = '"$__RIG_RUN_BIN"';
9
+ function buildRunWorkspaceProvisioningOptions(input) {
10
+ return {
11
+ projectRoot: input.projectRoot,
12
+ id: input.runId,
13
+ taskId: input.taskId,
14
+ mode: RUN_WORKSPACE_ISOLATION_MODE,
15
+ provider: RUN_WORKSPACE_PROVIDER
16
+ };
17
+ }
18
+ function buildRigRunLaunchArgs(input, runId, projectRoot) {
19
+ const args = ["--task", input.taskId, "--run-id", runId, "--project", projectRoot];
20
+ const title = optionalString(input.title);
21
+ const model = optionalString(input.model);
22
+ const prompt = optionalString(input.prompt);
23
+ if (title)
24
+ args.push("--title", title);
25
+ if (model)
26
+ args.push("--model", model);
27
+ if (input.forceSteerOnce)
28
+ args.push("--force-steer-once");
29
+ if (prompt)
30
+ args.push("--prompt", prompt);
31
+ return args;
32
+ }
33
+ function resolveRemoteCheckoutPlan(input) {
34
+ const repoName = sanitizeRepoName(input.projectSlug?.split("/").pop() ?? "project");
35
+ const selectedCheckout = optionalString(input.selectedCheckout);
36
+ return {
37
+ mode: selectedCheckout ? "configured" : "managed",
38
+ repoName,
39
+ checkoutPath: selectedCheckout ?? `.rig/checkouts/${repoName}`,
40
+ checkoutPathKind: selectedCheckout ? "literal" : "home-relative",
41
+ originUrl: resolveProjectOriginUrl(input.projectSlug)
42
+ };
43
+ }
44
+ function resolveRunGithubAuthMaterialization(projectRoot, checkoutExpression = "$CHECKOUT") {
45
+ const operatorStateDir = resolve(projectRoot, ".rig", "state");
46
+ return {
47
+ operatorAuthPath: resolve(operatorStateDir, "github-auth.json"),
48
+ operatorStateDir,
49
+ remoteAuthPath: `${checkoutExpression}/.rig/state/github-auth.json`,
50
+ remoteStateDir: `${checkoutExpression}/.rig/state`
51
+ };
52
+ }
53
+ function readGithubAuthStateBase64(operatorAuthPath) {
54
+ return existsSync(operatorAuthPath) ? readFileSync(operatorAuthPath).toString("base64") : "";
55
+ }
56
+ function buildRemoteRigRunResolutionShell(installUrl, command = REMOTE_RIG_RUN_COMMAND) {
57
+ return [
58
+ `export PATH="$HOME/.local/bin:$HOME/.bun/bin:$PATH"`,
59
+ `__RIG_RUN_BIN="$(command -v ${command} || true)"`,
60
+ `if [ -z "$__RIG_RUN_BIN" ]; then for __RIG_RUN_CANDIDATE in "$HOME"/.bun/install/global/node_modules/@h-rig/cli-*/rig-run; do if [ -x "$__RIG_RUN_CANDIDATE" ]; then __RIG_RUN_BIN="$__RIG_RUN_CANDIDATE"; break; fi; done; fi`,
61
+ `[ -x "$__RIG_RUN_BIN" ] || { echo "rig: remote rig-run not found; install @h-rig/cli on the remote host (curl -fsSL ${installUrl} | bash) and ensure rig-run is installed" >&2; exit 1; }`
62
+ ];
63
+ }
64
+ function isCompiledBunBinary(importMetaUrl) {
65
+ return importMetaUrl.includes("$bunfs");
66
+ }
67
+ function resolveCompiledRigRunSiblingPath(execPath, platform = process.platform) {
68
+ const suffix = platform === "win32" || execPath.toLowerCase().endsWith(".exe") ? ".exe" : "";
69
+ return resolve(dirname(execPath), `${REMOTE_RIG_RUN_COMMAND}${suffix}`);
70
+ }
71
+ function ensureRigRunSiblingCurrent(rigBin, rigRun) {
72
+ const tempRigRun = `${rigRun}.tmp-${process.pid}`;
73
+ try {
74
+ if (existsSync(rigRun) && statSync(rigRun).mtimeMs >= statSync(rigBin).mtimeMs)
75
+ return;
76
+ rmSync(tempRigRun, { force: true });
77
+ try {
78
+ linkSync(rigBin, tempRigRun);
79
+ } catch {
80
+ copyFileSync(rigBin, tempRigRun);
81
+ }
82
+ chmodSync(tempRigRun, 493);
83
+ renameSync(tempRigRun, rigRun);
84
+ } catch {
85
+ try {
86
+ rmSync(tempRigRun, { force: true });
87
+ } catch {}
88
+ }
89
+ }
90
+ function resolveLocalDispatchRigRunBin(input) {
91
+ if (!isCompiledBunBinary(input.importMetaUrl))
92
+ return input.sourceRigRunPath;
93
+ const rigBin = input.execPath ?? process.execPath;
94
+ const rigRun = resolveCompiledRigRunSiblingPath(rigBin, input.platform);
95
+ ensureRigRunSiblingCurrent(rigBin, rigRun);
96
+ return rigRun;
97
+ }
98
+ function resolveInProcessRigRunBin(input) {
99
+ return isCompiledBunBinary(input.importMetaUrl) ? input.execPath ?? process.execPath : input.sourceRigRunPath;
100
+ }
101
+ function optionalString(value) {
102
+ const trimmed = value?.trim();
103
+ return trimmed ? trimmed : undefined;
104
+ }
105
+ function sanitizeRepoName(value) {
106
+ return (optionalString(value) ?? "project").replace(/[^A-Za-z0-9._-]/g, "-");
107
+ }
108
+ function resolveProjectOriginUrl(projectSlug) {
109
+ const slug = optionalString(projectSlug);
110
+ return slug && /^[^/]+\/[^/]+$/.test(slug) ? `https://github.com/${slug}.git` : null;
111
+ }
112
+ export {
113
+ resolveRunGithubAuthMaterialization,
114
+ resolveRemoteCheckoutPlan,
115
+ resolveLocalDispatchRigRunBin,
116
+ resolveInProcessRigRunBin,
117
+ resolveCompiledRigRunSiblingPath,
118
+ readGithubAuthStateBase64,
119
+ isCompiledBunBinary,
120
+ ensureRigRunSiblingCurrent,
121
+ buildRunWorkspaceProvisioningOptions,
122
+ buildRigRunLaunchArgs,
123
+ buildRemoteRigRunResolutionShell,
124
+ RUN_WORKSPACE_PROVIDER,
125
+ RUN_WORKSPACE_ISOLATION_MODE,
126
+ REMOTE_RIG_RUN_COMMAND,
127
+ REMOTE_RESOLVED_RIG_RUN_COMMAND
128
+ };
@@ -0,0 +1,20 @@
1
+ import type { RuntimeBrowserContext, RuntimeMemoryRetrievalConfig, RuntimeTaskContext } from "@rig/contracts";
2
+ export type { RuntimeBrowserContext, RuntimeMemoryRetrievalConfig, RuntimeMemoryContext, RuntimeSourceTaskContract, RuntimeTaskContext, } from "@rig/contracts";
3
+ export declare const RUNTIME_CONTEXT_ENV = "RIG_RUNTIME_CONTEXT_FILE";
4
+ export declare const DEFAULT_RUNTIME_MEMORY_RETRIEVAL: RuntimeMemoryRetrievalConfig;
5
+ export declare const runtimeContextStringFields: readonly (keyof RuntimeTaskContext)[];
6
+ export declare const runtimeContextArrayFields: readonly (keyof RuntimeTaskContext)[];
7
+ export declare const runtimeContextOptionalStringFields: readonly (keyof RuntimeTaskContext)[];
8
+ export declare function writeRuntimeContext(path: string, ctx: RuntimeTaskContext): void;
9
+ export declare function loadRuntimeContext(path: string): RuntimeTaskContext;
10
+ export declare function runtimeMemoryEnvFromContext(ctx: RuntimeTaskContext | null | undefined): Record<string, string>;
11
+ /**
12
+ * Serialize an already-resolved browser context into the RIG_BROWSER_* env wire
13
+ * contract read by rig-agent and rig-browser-tool. This is dumb substrate
14
+ * plumbing (a flat projection of the resolved context's own fields) — the
15
+ * browser DOMAIN resolution that produced this context lives in
16
+ * @rig/browser-plugin, resolved via browser-contract-port.ts. Mirrors
17
+ * runtimeMemoryEnvFromContext above.
18
+ */
19
+ export declare function browserEnvFromContext(browser: RuntimeBrowserContext | undefined): Record<string, string>;
20
+ export declare function loadRuntimeContextFromEnv(env?: NodeJS.ProcessEnv): RuntimeTaskContext | null;
@@ -0,0 +1,257 @@
1
+ // @bun
2
+ // packages/core/src/runtime-context.ts
3
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
4
+ import { dirname, resolve } from "path";
5
+ var RUNTIME_CONTEXT_ENV = "RIG_RUNTIME_CONTEXT_FILE";
6
+ var DEFAULT_RUNTIME_MEMORY_RETRIEVAL = {
7
+ topK: 5,
8
+ lexicalWeight: 0.35,
9
+ vectorWeight: 0.45,
10
+ recencyWeight: 0.1,
11
+ confidenceWeight: 0.1
12
+ };
13
+ var runtimeContextStringFields = [
14
+ "runtimeId",
15
+ "taskId",
16
+ "role",
17
+ "workspaceDir",
18
+ "stateDir",
19
+ "logsDir",
20
+ "sessionDir",
21
+ "sessionFile",
22
+ "policyFile",
23
+ "binDir",
24
+ "createdAt"
25
+ ];
26
+ var runtimeContextArrayFields = ["scopes", "validation"];
27
+ var runtimeContextOptionalStringFields = [
28
+ "artifactRoot",
29
+ "hostProjectRoot",
30
+ "monorepoMainRoot",
31
+ "monorepoBaseRef",
32
+ "monorepoBaseCommit"
33
+ ];
34
+ function writeRuntimeContext(path, ctx) {
35
+ const absPath = resolve(path);
36
+ const dir = dirname(absPath);
37
+ if (!existsSync(dir)) {
38
+ mkdirSync(dir, { recursive: true });
39
+ }
40
+ writeFileSync(absPath, JSON.stringify(ctx, null, 2), "utf8");
41
+ }
42
+ function loadRuntimeContext(path) {
43
+ const absPath = resolve(path);
44
+ if (!existsSync(absPath)) {
45
+ throw new Error(`RuntimeTaskContext file not found: ${absPath}`);
46
+ }
47
+ let raw;
48
+ try {
49
+ raw = JSON.parse(readFileSync(absPath, "utf8"));
50
+ } catch (err) {
51
+ throw new Error(`Failed to parse RuntimeTaskContext at ${absPath}: ${String(err)}`);
52
+ }
53
+ if (typeof raw !== "object" || raw === null) {
54
+ throw new Error(`RuntimeTaskContext at ${absPath} is not an object`);
55
+ }
56
+ const obj = raw;
57
+ for (const field of runtimeContextStringFields) {
58
+ if (typeof obj[field] !== "string" || obj[field].length === 0) {
59
+ throw new Error(`RuntimeTaskContext field "${field}" must be a non-empty string (at ${absPath})`);
60
+ }
61
+ }
62
+ for (const field of runtimeContextArrayFields) {
63
+ if (!Array.isArray(obj[field])) {
64
+ throw new Error(`RuntimeTaskContext field "${field}" must be an array (at ${absPath})`);
65
+ }
66
+ if (!obj[field].every((entry) => typeof entry === "string")) {
67
+ throw new Error(`RuntimeTaskContext field "${field}" must be a string[] (at ${absPath})`);
68
+ }
69
+ }
70
+ for (const field of runtimeContextOptionalStringFields) {
71
+ if (field in obj && obj[field] !== undefined && typeof obj[field] !== "string") {
72
+ throw new Error(`RuntimeTaskContext field "${field}" must be a string when present (at ${absPath})`);
73
+ }
74
+ }
75
+ if (obj.browser !== undefined) {
76
+ if (typeof obj.browser !== "object" || obj.browser === null || Array.isArray(obj.browser)) {
77
+ throw new Error(`RuntimeTaskContext field "browser" must be an object when present (at ${absPath})`);
78
+ }
79
+ const browser = obj.browser;
80
+ for (const field of [
81
+ "preset",
82
+ "mode",
83
+ "stateDir",
84
+ "defaultProfile",
85
+ "effectiveProfile",
86
+ "defaultAttachUrl",
87
+ "effectiveAttachUrl",
88
+ "launchHelper",
89
+ "checkHelper",
90
+ "attachInfoHelper",
91
+ "e2eHelper",
92
+ "resetProfileHelper"
93
+ ]) {
94
+ if (typeof browser[field] !== "string" || browser[field].length === 0) {
95
+ throw new Error(`RuntimeTaskContext field "browser.${field}" must be a non-empty string (at ${absPath})`);
96
+ }
97
+ }
98
+ for (const field of ["devCommand", "launchCommand", "checkCommand", "e2eCommand"]) {
99
+ if (browser[field] !== undefined && typeof browser[field] !== "string") {
100
+ throw new Error(`RuntimeTaskContext field "browser.${field}" must be a string when present (at ${absPath})`);
101
+ }
102
+ }
103
+ if (typeof browser.required !== "boolean") {
104
+ throw new Error(`RuntimeTaskContext field "browser.required" must be a boolean (at ${absPath})`);
105
+ }
106
+ }
107
+ if (obj.memory !== undefined) {
108
+ if (typeof obj.memory !== "object" || obj.memory === null || Array.isArray(obj.memory)) {
109
+ throw new Error(`RuntimeTaskContext field "memory" must be an object when present (at ${absPath})`);
110
+ }
111
+ const memory = obj.memory;
112
+ for (const field of ["canonicalPath", "canonicalRef", "hydratedPath"]) {
113
+ if (typeof memory[field] !== "string" || memory[field].length === 0) {
114
+ throw new Error(`RuntimeTaskContext field "memory.${field}" must be a non-empty string (at ${absPath})`);
115
+ }
116
+ }
117
+ if (typeof memory.createdFresh !== "boolean") {
118
+ throw new Error(`RuntimeTaskContext field "memory.createdFresh" must be a boolean (at ${absPath})`);
119
+ }
120
+ const hasCanonicalBaseOid = typeof memory.canonicalBaseOid === "string" && memory.canonicalBaseOid.length > 0;
121
+ if (memory.canonicalBaseOid !== undefined && !hasCanonicalBaseOid) {
122
+ throw new Error(`RuntimeTaskContext field "memory.canonicalBaseOid" must be a non-empty string when present (at ${absPath})`);
123
+ }
124
+ if (!memory.createdFresh && !hasCanonicalBaseOid) {
125
+ throw new Error(`RuntimeTaskContext field "memory.canonicalBaseOid" must be a non-empty string when memory.createdFresh is false (at ${absPath})`);
126
+ }
127
+ if (typeof memory.retrieval !== "object" || memory.retrieval === null || Array.isArray(memory.retrieval)) {
128
+ throw new Error(`RuntimeTaskContext field "memory.retrieval" must be an object (at ${absPath})`);
129
+ }
130
+ const retrieval = memory.retrieval;
131
+ for (const field of ["topK", "lexicalWeight", "vectorWeight", "recencyWeight", "confidenceWeight"]) {
132
+ if (typeof retrieval[field] !== "number" || Number.isNaN(retrieval[field])) {
133
+ throw new Error(`RuntimeTaskContext field "memory.retrieval.${field}" must be a number (at ${absPath})`);
134
+ }
135
+ }
136
+ }
137
+ if (obj.initialDirtyFiles !== undefined) {
138
+ if (typeof obj.initialDirtyFiles !== "object" || obj.initialDirtyFiles === null || Array.isArray(obj.initialDirtyFiles)) {
139
+ throw new Error(`RuntimeTaskContext field "initialDirtyFiles" must be an object when present (at ${absPath})`);
140
+ }
141
+ const dirtyFiles = obj.initialDirtyFiles;
142
+ for (const key of ["project", "monorepo"]) {
143
+ if (dirtyFiles[key] === undefined) {
144
+ continue;
145
+ }
146
+ if (!Array.isArray(dirtyFiles[key]) || !dirtyFiles[key].every((entry) => typeof entry === "string")) {
147
+ throw new Error(`RuntimeTaskContext field "initialDirtyFiles.${key}" must be a string[] when present (at ${absPath})`);
148
+ }
149
+ }
150
+ }
151
+ if (obj.initialHeadCommits !== undefined) {
152
+ if (typeof obj.initialHeadCommits !== "object" || obj.initialHeadCommits === null || Array.isArray(obj.initialHeadCommits)) {
153
+ throw new Error(`RuntimeTaskContext field "initialHeadCommits" must be an object when present (at ${absPath})`);
154
+ }
155
+ const headCommits = obj.initialHeadCommits;
156
+ for (const key of ["project", "monorepo"]) {
157
+ if (headCommits[key] === undefined) {
158
+ continue;
159
+ }
160
+ if (typeof headCommits[key] !== "string") {
161
+ throw new Error(`RuntimeTaskContext field "initialHeadCommits.${key}" must be a string when present (at ${absPath})`);
162
+ }
163
+ }
164
+ }
165
+ return obj;
166
+ }
167
+ function runtimeMemoryEnvFromContext(ctx) {
168
+ if (!ctx?.memory) {
169
+ return {};
170
+ }
171
+ return {
172
+ RIG_MEMORY_DB_PATH: ctx.memory.hydratedPath,
173
+ RIG_MEMORY_CANONICAL_PATH: ctx.memory.canonicalPath,
174
+ RIG_MEMORY_CANONICAL_REF: ctx.memory.canonicalRef,
175
+ ...ctx.memory.canonicalBaseOid ? { RIG_MEMORY_CANONICAL_BASE_OID: ctx.memory.canonicalBaseOid } : {},
176
+ RIG_MEMORY_CREATED_FRESH: ctx.memory.createdFresh ? "1" : "0",
177
+ RIG_MEMORY_RETRIEVAL_TOP_K: String(ctx.memory.retrieval.topK),
178
+ RIG_MEMORY_RETRIEVAL_LEXICAL_WEIGHT: String(ctx.memory.retrieval.lexicalWeight),
179
+ RIG_MEMORY_RETRIEVAL_VECTOR_WEIGHT: String(ctx.memory.retrieval.vectorWeight),
180
+ RIG_MEMORY_RETRIEVAL_RECENCY_WEIGHT: String(ctx.memory.retrieval.recencyWeight),
181
+ RIG_MEMORY_RETRIEVAL_CONFIDENCE_WEIGHT: String(ctx.memory.retrieval.confidenceWeight)
182
+ };
183
+ }
184
+ function browserEnvFromContext(browser) {
185
+ if (!browser?.required) {
186
+ return {};
187
+ }
188
+ const env = {
189
+ RIG_BROWSER_REQUIRED: "1",
190
+ RIG_BROWSER_PRESET: browser.preset,
191
+ RIG_BROWSER_MODE: browser.mode,
192
+ RIG_BROWSER_STATE_DIR: browser.stateDir,
193
+ RIG_BROWSER_PROFILE: browser.effectiveProfile,
194
+ RIG_BROWSER_BASE_PROFILE: browser.defaultProfile,
195
+ RIG_BROWSER_ATTACH_URL: browser.effectiveAttachUrl,
196
+ RIG_BROWSER_DEFAULT_ATTACH_URL: browser.defaultAttachUrl,
197
+ RIG_BROWSER_LAUNCH_HELPER: browser.launchHelper,
198
+ RIG_BROWSER_CHECK_HELPER: browser.checkHelper,
199
+ RIG_BROWSER_ATTACH_INFO_HELPER: browser.attachInfoHelper,
200
+ RIG_BROWSER_E2E_HELPER: browser.e2eHelper,
201
+ RIG_BROWSER_RESET_HELPER: browser.resetProfileHelper
202
+ };
203
+ if (browser.devCommand) {
204
+ env.RIG_BROWSER_DEV_COMMAND = browser.devCommand;
205
+ }
206
+ if (browser.launchCommand) {
207
+ env.RIG_BROWSER_LAUNCH_COMMAND = browser.launchCommand;
208
+ }
209
+ if (browser.checkCommand) {
210
+ env.RIG_BROWSER_CHECK_COMMAND = browser.checkCommand;
211
+ }
212
+ if (browser.e2eCommand) {
213
+ env.RIG_BROWSER_E2E_COMMAND = browser.e2eCommand;
214
+ }
215
+ return env;
216
+ }
217
+ function loadRuntimeContextFromEnv(env = process.env) {
218
+ const contextFile = env[RUNTIME_CONTEXT_ENV];
219
+ if (contextFile) {
220
+ return loadRuntimeContext(contextFile);
221
+ }
222
+ const inferred = findRuntimeContextFile(process.cwd());
223
+ if (!inferred) {
224
+ return null;
225
+ }
226
+ return loadRuntimeContext(inferred);
227
+ }
228
+ function findRuntimeContextFile(startPath) {
229
+ let current = resolve(startPath);
230
+ while (true) {
231
+ const candidate = resolve(current, "runtime-context.json");
232
+ if (existsSync(candidate) && isAgentRuntimeContextPath(candidate)) {
233
+ return candidate;
234
+ }
235
+ const parent = dirname(current);
236
+ if (parent === current) {
237
+ return "";
238
+ }
239
+ current = parent;
240
+ }
241
+ }
242
+ function isAgentRuntimeContextPath(path) {
243
+ const normalized = path.replace(/\\/g, "/");
244
+ return /\/\.rig\/runtime-context\.json$/.test(normalized);
245
+ }
246
+ export {
247
+ writeRuntimeContext,
248
+ runtimeMemoryEnvFromContext,
249
+ runtimeContextStringFields,
250
+ runtimeContextOptionalStringFields,
251
+ runtimeContextArrayFields,
252
+ loadRuntimeContextFromEnv,
253
+ loadRuntimeContext,
254
+ browserEnvFromContext,
255
+ RUNTIME_CONTEXT_ENV,
256
+ DEFAULT_RUNTIME_MEMORY_RETRIEVAL
257
+ };
@@ -0,0 +1,44 @@
1
+ import type { HarnessEvent, HarnessEventType } from "@rig/contracts";
2
+ export type EventBusLike = {
3
+ getRunId(): string;
4
+ getEventsFile(): string;
5
+ getMemoryEvents(): HarnessEvent[];
6
+ emit<TPayload extends Record<string, unknown>>(type: HarnessEventType, payload: TPayload): Promise<HarnessEvent<TPayload>>;
7
+ attachRuntimeBus(runtimeBus: RuntimeEventBus): Promise<void>;
8
+ attachRuntimeWorkspace(projectRoot: string): Promise<RuntimeEventBus>;
9
+ };
10
+ export declare class RuntimeEventBus implements EventBusLike {
11
+ private readonly runId;
12
+ private readonly eventsFile;
13
+ private readonly memoryEvents;
14
+ constructor(options: {
15
+ projectRoot: string;
16
+ runId?: string;
17
+ eventsFile?: string;
18
+ });
19
+ getRunId(): string;
20
+ getEventsFile(): string;
21
+ getMemoryEvents(): HarnessEvent[];
22
+ attachRuntimeBus(runtimeBus: RuntimeEventBus): Promise<void>;
23
+ attachRuntimeWorkspace(projectRoot: string): Promise<RuntimeEventBus>;
24
+ ingest(event: HarnessEvent): Promise<void>;
25
+ emit<TPayload extends Record<string, unknown>>(type: HarnessEventType, payload: TPayload): Promise<HarnessEvent<TPayload>>;
26
+ }
27
+ export declare class GeneralCliEventBus implements EventBusLike {
28
+ private readonly runId;
29
+ private readonly eventsFile;
30
+ private readonly memoryEvents;
31
+ private readonly runtimeBuses;
32
+ constructor(options: {
33
+ projectRoot: string;
34
+ runId?: string;
35
+ eventsFile?: string;
36
+ });
37
+ getRunId(): string;
38
+ getEventsFile(): string;
39
+ getMemoryEvents(): HarnessEvent[];
40
+ attachRuntimeBus(runtimeBus: RuntimeEventBus): Promise<void>;
41
+ attachRuntimeWorkspace(projectRoot: string): Promise<RuntimeEventBus>;
42
+ emit<TPayload extends Record<string, unknown>>(type: HarnessEventType, payload: TPayload): Promise<HarnessEvent<TPayload>>;
43
+ }
44
+ export { GeneralCliEventBus as EventBus };