@h-rig/guard-plugin 0.0.6-alpha.157 → 0.0.6-alpha.158

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.
@@ -0,0 +1,9 @@
1
+ export type AgentBinaryBuildOptions = {
2
+ entrypoint: string;
3
+ outputPath: string;
4
+ cwd: string;
5
+ define?: Record<string, string>;
6
+ env?: Record<string, string | undefined>;
7
+ };
8
+ export declare function materializeSelfExecRole(outputPath: string, define?: Record<string, string>): void;
9
+ export declare function buildAgentBinary(entrypoint: string, outputPath: string, cwd: string, defines?: Record<string, string>): Promise<void>;
@@ -0,0 +1,88 @@
1
+ // @bun
2
+ // packages/guard-plugin/src/agent-binary-build.ts
3
+ import { chmodSync, copyFileSync, linkSync, mkdirSync, rmSync, writeFileSync } from "fs";
4
+ import { basename, dirname, resolve } from "path";
5
+ import { runtimeProvisioningEnv } from "@rig/core/runtime-provisioning-env";
6
+
7
+ // packages/guard-plugin/src/embedded-native-assets.ts
8
+ var embeddedNatives = null;
9
+
10
+ // packages/guard-plugin/src/agent-binary-build.ts
11
+ function materializeSelfExecRole(outputPath, define) {
12
+ const selfPath = process.execPath;
13
+ mkdirSync(dirname(outputPath), { recursive: true });
14
+ rmSync(outputPath, { force: true });
15
+ try {
16
+ linkSync(selfPath, outputPath);
17
+ } catch {
18
+ copyFileSync(selfPath, outputPath);
19
+ }
20
+ chmodSync(outputPath, 493);
21
+ const role = basename(outputPath).replace(/\.exe$/i, "");
22
+ writeFileSync(`${outputPath}.rig-runconfig.json`, `${JSON.stringify({ role, define: define ?? {} }, null, 2)}
23
+ `, { mode: 384 });
24
+ }
25
+ function hasEmbeddedNatives() {
26
+ return embeddedNatives !== null;
27
+ }
28
+ async function buildAgentBinary(entrypoint, outputPath, cwd, defines) {
29
+ if (hasEmbeddedNatives()) {
30
+ materializeSelfExecRole(outputPath, defines);
31
+ return;
32
+ }
33
+ await buildRuntimeBinary({
34
+ entrypoint,
35
+ outputPath,
36
+ cwd,
37
+ ...defines ? { define: defines } : {},
38
+ env: runtimeProvisioningEnv()
39
+ });
40
+ }
41
+ async function buildRuntimeBinary(options) {
42
+ mkdirSync(dirname(options.outputPath), { recursive: true });
43
+ await withTemporaryEnv({
44
+ ...options.env,
45
+ ...options.define ? { RIG_BUILD_CONFIG_JSON: JSON.stringify(options.define) } : {}
46
+ }, async () => {
47
+ const result = await Bun.build({
48
+ entrypoints: [resolve(options.cwd, options.entrypoint)],
49
+ compile: { outfile: options.outputPath },
50
+ target: "bun",
51
+ format: "esm",
52
+ minify: true,
53
+ bytecode: true,
54
+ ...options.define ? { define: { __RIG_BUILD_CONFIG__: JSON.stringify(options.define) } } : {}
55
+ });
56
+ if (!result.success) {
57
+ throw new Error(result.logs.map((log) => log.message).join(`
58
+ `) || `Failed to compile ${options.entrypoint}`);
59
+ }
60
+ });
61
+ chmodSync(options.outputPath, 493);
62
+ }
63
+ async function withTemporaryEnv(env, callback) {
64
+ const previous = {};
65
+ for (const [key, value] of Object.entries(env)) {
66
+ previous[key] = process.env[key];
67
+ if (value === undefined) {
68
+ delete process.env[key];
69
+ } else {
70
+ process.env[key] = value;
71
+ }
72
+ }
73
+ try {
74
+ return await callback();
75
+ } finally {
76
+ for (const [key, value] of Object.entries(previous)) {
77
+ if (value === undefined) {
78
+ delete process.env[key];
79
+ } else {
80
+ process.env[key] = value;
81
+ }
82
+ }
83
+ }
84
+ }
85
+ export {
86
+ materializeSelfExecRole,
87
+ buildAgentBinary
88
+ };
@@ -0,0 +1,24 @@
1
+ import { type CommandExecutionResult, type OutputMode, type PolicyMode } from "@rig/contracts";
2
+ import type { RunnerContext } from "@rig/core/runtime-runner-context";
3
+ import { buildAgentBinary } from "./agent-binary-build";
4
+ export type { RunnerContext } from "@rig/core/runtime-runner-context";
5
+ export { loadPolicy } from "./guard";
6
+ export declare function withProjectRoot(projectRoot: string): void;
7
+ export declare function formatCommand(parts: string[]): string;
8
+ type AgentShellBuild = typeof buildAgentBinary;
9
+ export type AgentShellMaterializationOptions = {
10
+ readonly env?: Record<string, string | undefined>;
11
+ readonly cwd?: string;
12
+ readonly moduleDir?: string;
13
+ readonly build?: AgentShellBuild;
14
+ };
15
+ export declare function resolveAgentMaterializationSourceRoot(projectRoot: string, options?: AgentShellMaterializationOptions): string;
16
+ export declare function ensureAgentShellBinary(projectRoot: string, options?: AgentShellMaterializationOptions): Promise<string>;
17
+ export declare function initializeRuntime(options: {
18
+ projectRoot: string;
19
+ dryRun: boolean;
20
+ outputMode: OutputMode;
21
+ runId?: string;
22
+ policyMode?: PolicyMode;
23
+ }): Promise<RunnerContext>;
24
+ export declare function runCommand(context: RunnerContext, parts: string[]): Promise<CommandExecutionResult>;