@withgauge/cli 0.8.0 → 0.9.1-internal.20261019.00b83ea

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,250 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // ../packages/local/dist/scripts.js
8
+ var SESSION_DIR = "/gauge/session";
9
+ var WORKSPACE_DIR = `${SESSION_DIR}/workspace`;
10
+ var BASE_COMMIT_MARKER = "GAUGE_BASE_COMMIT";
11
+ var PI_AGENT_DIR = "/tmp/pi-agent";
12
+ function shellQuote(value) {
13
+ return `'${value.replace(/'/g, `'\\''`)}'`;
14
+ }
15
+ function splitModel(model) {
16
+ const slash = model.indexOf("/");
17
+ if (slash < 1 || slash === model.length - 1)
18
+ throw new Error(`model must be provider/model, got ${model}`);
19
+ return { provider: model.slice(0, slash), model: model.slice(slash + 1) };
20
+ }
21
+ function agentEnv(agent) {
22
+ switch (agent) {
23
+ case "claude-code":
24
+ return { CLAUDE_CONFIG_DIR: `${SESSION_DIR}/claude` };
25
+ case "codex":
26
+ return { CODEX_HOME: `${SESSION_DIR}/codex` };
27
+ case "pi":
28
+ return { PI_CODING_AGENT_DIR: PI_AGENT_DIR };
29
+ case "opencode":
30
+ return {
31
+ XDG_DATA_HOME: `${SESSION_DIR}/opencode/data`,
32
+ XDG_STATE_HOME: `${SESSION_DIR}/opencode/state`,
33
+ OPENCODE_CONFIG_CONTENT: JSON.stringify({ autoupdate: false, permission: { "*": "allow", question: "ask" } })
34
+ };
35
+ }
36
+ }
37
+ function prepareScript(spec) {
38
+ const lines = ["set -eu"];
39
+ for (const resource of spec.resources) {
40
+ const src = `/gauge/inputs/${resource.name}`;
41
+ const dest = `${SESSION_DIR}/resources/${resource.name}`;
42
+ const hex = resource.digest.replace(/^sha256:/, "");
43
+ lines.push(`echo ${shellQuote(`${hex} ${src}`)} | sha256sum -c --quiet -`, `mkdir -p ${shellQuote(dest)}`, `cp ${shellQuote(src)} ${shellQuote(`${dest}/${resource.filename}`)}`);
44
+ }
45
+ lines.push(`mkdir -p ${SESSION_DIR}/home ${SESSION_DIR}/claude ${SESSION_DIR}/codex ${SESSION_DIR}/opencode ${SESSION_DIR}/pi`);
46
+ if (spec.workspace)
47
+ lines.push(`git clone --quiet -- ${shellQuote(spec.workspace.repo)} ${WORKSPACE_DIR}`, `cd ${WORKSPACE_DIR}`, `ref=${shellQuote(spec.workspace.ref)}`, `git rev-parse --verify --quiet "$ref^{commit}" >/dev/null || ref="origin/$ref"`, `git checkout --quiet --detach "$ref"`);
48
+ else
49
+ lines.push(`git init --quiet ${WORKSPACE_DIR}`, `cd ${WORKSPACE_DIR}`, "git -c user.name=gauge -c user.email=gauge@localhost commit --quiet --allow-empty -m base");
50
+ lines.push("gauge_base_commit=$(git rev-parse HEAD)");
51
+ const skillRoot = {
52
+ "claude-code": `${SESSION_DIR}/claude/skills`,
53
+ codex: `${SESSION_DIR}/codex/skills`,
54
+ pi: `${PI_AGENT_DIR}/skills`,
55
+ opencode: `${SESSION_DIR}/home/.config/opencode/skills`
56
+ };
57
+ for (const resource of spec.resources) {
58
+ if (resource.kind !== "archive")
59
+ continue;
60
+ const root = `${SESSION_DIR}/resources/${resource.name}`;
61
+ const contents = `${root}/content`;
62
+ lines.push(`mkdir -p ${shellQuote(contents)}`, `tar --extract --gzip --file ${shellQuote(`${root}/${resource.filename}`)} --directory ${shellQuote(contents)} --no-same-owner --no-same-permissions`);
63
+ if (resource.use?.type === "skill") {
64
+ const target = `${skillRoot[spec.agent.kind]}/${resource.name}`;
65
+ lines.push(`test -f ${shellQuote(`${contents}/SKILL.md`)}`, `mkdir -p ${shellQuote(target)}`, `cp -R ${shellQuote(`${contents}/.`)} ${shellQuote(target)}`);
66
+ } else if (resource.use?.type === "npm-package") {
67
+ const args = resource.use.install === "environment" ? `--global --prefix ${SESSION_DIR}/npm` : "";
68
+ lines.push(`npm install --no-audit --no-fund ${args} ${shellQuote(`${contents}/${resource.use.package}`)} >&2`);
69
+ if (resource.use.install === "environment")
70
+ lines.push(`export PATH=${SESSION_DIR}/npm/bin:$PATH`);
71
+ }
72
+ }
73
+ lines.push(`echo "${BASE_COMMIT_MARKER} $gauge_base_commit"`);
74
+ return lines.join("\n");
75
+ }
76
+ function captureScript(baseCommit) {
77
+ return [
78
+ "set -eu",
79
+ `cd ${WORKSPACE_DIR}`,
80
+ "git add --all --intent-to-add .",
81
+ `git diff --binary ${shellQuote(baseCommit)}`
82
+ ].join("\n");
83
+ }
84
+ function harnessCommand(spec) {
85
+ switch (spec.agent.kind) {
86
+ case "claude-code":
87
+ return ["claude", "-p", "--input-format", "stream-json", "--output-format", "stream-json", "--verbose", "--model", spec.agent.model, "--dangerously-skip-permissions", "--permission-prompt-tool", "stdio"];
88
+ case "codex":
89
+ return ["codex", "app-server", "--listen", "stdio://"];
90
+ case "pi": {
91
+ const m = splitModel(spec.agent.model);
92
+ return ["pi", "--mode", "rpc", "--no-approve", "--session-dir", "/gauge/session/pi", "--provider", m.provider, "--model", m.model];
93
+ }
94
+ case "opencode":
95
+ return ["opencode", "serve", "--hostname", "127.0.0.1", "--port", "4096"];
96
+ }
97
+ }
98
+
99
+ // ../packages/local/dist/docker.js
100
+ import { execFile } from "child_process";
101
+ import { promisify } from "util";
102
+ var exec = promisify(execFile);
103
+ var DockerError = class extends Error {
104
+ args;
105
+ stderr;
106
+ constructor(args, stderr) {
107
+ super(`docker ${args[0]} failed: ${stderr.trim() || "unknown error"}`);
108
+ this.args = args;
109
+ this.stderr = stderr;
110
+ }
111
+ };
112
+ function mountArg(mount) {
113
+ for (const path of [mount.source, mount.target])
114
+ if (/[,"\n]/.test(path))
115
+ throw new Error(`unsupported character in mount path: ${path}`);
116
+ return `type=bind,source=${mount.source},target=${mount.target}${mount.readonly ? ",readonly" : ""}`;
117
+ }
118
+ var isMissing = (stderr) => /No such (container|object|volume)/i.test(stderr);
119
+ var DockerCli = class {
120
+ binary;
121
+ constructor(binary = "docker") {
122
+ this.binary = binary;
123
+ }
124
+ async run(args) {
125
+ try {
126
+ const { stdout } = await exec(this.binary, args, {
127
+ maxBuffer: 16 * 1024 * 1024
128
+ });
129
+ return stdout;
130
+ } catch (error) {
131
+ const stderr = String(error.stderr ?? error);
132
+ throw new DockerError(args, stderr);
133
+ }
134
+ }
135
+ async create(spec) {
136
+ const args = [
137
+ "create",
138
+ "--name",
139
+ spec.name,
140
+ "--restart",
141
+ "no",
142
+ "--log-driver",
143
+ "json-file",
144
+ "--log-opt",
145
+ "max-size=1g"
146
+ ];
147
+ if (spec.interactive)
148
+ args.push("--interactive");
149
+ if (spec.user)
150
+ args.push("--user", spec.user);
151
+ for (const [key, value] of Object.entries(spec.labels))
152
+ args.push("--label", `${key}=${value}`);
153
+ for (const [key, value] of Object.entries(spec.env))
154
+ args.push("--env", `${key}=${value}`);
155
+ for (const mount of spec.mounts)
156
+ args.push("--mount", mountArg(mount));
157
+ args.push(spec.image, ...spec.command);
158
+ try {
159
+ await this.run(args);
160
+ return "created";
161
+ } catch (error) {
162
+ if (error instanceof DockerError && /Conflict|already in use/i.test(error.stderr))
163
+ return "exists";
164
+ throw error;
165
+ }
166
+ }
167
+ /** Recovery must never remove an unrelated container named in a stale record. */
168
+ async assertSessionOwner(name, credentialDir) {
169
+ let inspection;
170
+ try {
171
+ inspection = await this.run(["inspect", name]);
172
+ } catch (error) {
173
+ if (error instanceof DockerError && isMissing(error.stderr))
174
+ return;
175
+ throw error;
176
+ }
177
+ const container = JSON.parse(inspection)[0];
178
+ if (container?.Config?.Labels?.["sh.gauge.session"] !== name || !container?.Mounts?.some((mount) => mount.Source === `${credentialDir}/workspace` && mount.Destination === "/gauge/session"))
179
+ throw new Error("Refusing cleanup: container ownership does not match the Gauge session");
180
+ }
181
+ async stop(name, graceS) {
182
+ try {
183
+ await this.run(["stop", "--time", String(graceS), name]);
184
+ } catch (error) {
185
+ if (!(error instanceof DockerError && isMissing(error.stderr)))
186
+ throw error;
187
+ }
188
+ }
189
+ async remove(name) {
190
+ try {
191
+ await this.run(["rm", name]);
192
+ } catch (error) {
193
+ if (!(error instanceof DockerError && isMissing(error.stderr)))
194
+ throw error;
195
+ }
196
+ }
197
+ };
198
+
199
+ // ../packages/local/dist/workspace.js
200
+ import { cp, lstat, mkdtemp, rename, rm } from "fs/promises";
201
+ import { join } from "path";
202
+ async function directoryExists(path) {
203
+ try {
204
+ if (!(await lstat(path)).isDirectory())
205
+ throw new Error(`Expected a directory: ${path}`);
206
+ return true;
207
+ } catch (error) {
208
+ if (error.code === "ENOENT")
209
+ return false;
210
+ throw error;
211
+ }
212
+ }
213
+ async function preserveWorkspace(directory, runtime) {
214
+ const artifact = "recovered-workspace";
215
+ const destination = join(directory, artifact);
216
+ if (await directoryExists(destination))
217
+ return artifact;
218
+ const source = join(runtime, "workspace", "workspace");
219
+ if (!await directoryExists(source))
220
+ return void 0;
221
+ try {
222
+ await rename(source, destination);
223
+ } catch (error) {
224
+ if (error.code !== "EXDEV")
225
+ throw error;
226
+ const staging = await mkdtemp(join(directory, ".workspace-"));
227
+ try {
228
+ await cp(source, join(staging, "repo"), { recursive: true, dereference: false, verbatimSymlinks: true });
229
+ await rename(join(staging, "repo"), destination);
230
+ } finally {
231
+ await rm(staging, { recursive: true, force: true });
232
+ }
233
+ }
234
+ return artifact;
235
+ }
236
+
237
+ export {
238
+ __export,
239
+ WORKSPACE_DIR,
240
+ BASE_COMMIT_MARKER,
241
+ PI_AGENT_DIR,
242
+ shellQuote,
243
+ splitModel,
244
+ agentEnv,
245
+ prepareScript,
246
+ captureScript,
247
+ harnessCommand,
248
+ DockerCli,
249
+ preserveWorkspace
250
+ };