@ygrishajev/jig 0.0.0-stage → 1.0.1
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/LICENSE +21 -0
- package/README.md +135 -2
- package/dist/adapters/find-symlink.js +23 -0
- package/dist/adapters/git/git-workspace.js +297 -0
- package/dist/adapters/local-store/credentials-file.js +51 -0
- package/dist/adapters/local-store/local-run-store.js +45 -0
- package/dist/adapters/local-store/local-state.js +12 -0
- package/dist/adapters/local-store/metrics-log.js +119 -0
- package/dist/adapters/local-store/run-artefacts.js +76 -0
- package/dist/adapters/local-store/run-lock.js +51 -0
- package/dist/adapters/local-store/run-paths.js +49 -0
- package/dist/adapters/logging/create-logger.js +29 -0
- package/dist/adapters/run-store/composite-run-store.js +57 -0
- package/dist/adapters/run-store/git-remote-run-store.js +239 -0
- package/dist/adapters/sandbox-image/docker-images.js +93 -0
- package/dist/adapters/sandbox-image/image-recipe.js +96 -0
- package/dist/adapters/sandbox-image/latest-agent-versions.js +9 -0
- package/dist/adapters/sandcastle/agents.js +100 -0
- package/dist/adapters/sandcastle/codex-auth.js +191 -0
- package/dist/adapters/sandcastle/git-hooks.js +20 -0
- package/dist/adapters/sandcastle/open-session.js +67 -0
- package/dist/adapters/sandcastle/private-clone-docker.js +232 -0
- package/dist/adapters/sandcastle/sandcastle-env.js +17 -0
- package/dist/adapters/sandcastle/sandcastle-session.js +338 -0
- package/dist/adapters/sandcastle/structured-output.js +35 -0
- package/dist/adapters/sandcastle/usage.js +65 -0
- package/dist/adapters/telemetry/open-telemetry.js +29 -0
- package/dist/adapters/terminal/ask-for-agents.js +61 -0
- package/dist/adapters/terminal/ask-for-credential.js +32 -0
- package/dist/adapters/terminal/ask-for-preset.js +15 -0
- package/dist/adapters/terminal/standard-input.js +7 -0
- package/dist/adapters/terminal/terminal.js +45 -0
- package/dist/app/container.js +131 -0
- package/dist/app/init-sandbox.js +185 -0
- package/dist/app/observed-events.js +10 -0
- package/dist/app/report-cleanup.js +10 -0
- package/dist/app/run-jig.js +101 -0
- package/dist/app/run-status.js +20 -0
- package/dist/app/run-stores.js +107 -0
- package/dist/app/sandbox-image.js +151 -0
- package/dist/app/throwaway-sandbox.js +49 -0
- package/dist/cli/app-action.js +18 -0
- package/dist/cli/commands/presets.js +32 -0
- package/dist/cli/commands/run.js +82 -0
- package/dist/cli/commands/sandbox.js +54 -0
- package/dist/cli/commands/status.js +41 -0
- package/dist/cli/config-option.js +18 -0
- package/dist/cli/exit-when-done.js +11 -0
- package/dist/cli/log-format-option.js +14 -0
- package/dist/cli/main.js +33 -0
- package/dist/cli/read-request.js +36 -0
- package/dist/cli/with-app.js +73 -0
- package/dist/cli/with-error-handling.js +31 -0
- package/dist/cli/with-input.js +27 -0
- package/dist/config/describe-issues.js +6 -0
- package/dist/config/environment.js +53 -0
- package/dist/config/load-config.js +122 -0
- package/dist/config/presets.js +150 -0
- package/dist/config/write-config.js +39 -0
- package/dist/domain/agent-role.js +12 -0
- package/dist/domain/apply-outcome.js +132 -0
- package/dist/domain/completion-reuse.js +176 -0
- package/dist/domain/design-docs.js +3 -0
- package/dist/domain/design-review-report.js +86 -0
- package/dist/domain/design-review.js +193 -0
- package/dist/domain/errors.js +139 -0
- package/dist/domain/events.js +8 -0
- package/dist/domain/final-check.js +133 -0
- package/dist/domain/outcome.js +26 -0
- package/dist/domain/plan-coverage.js +129 -0
- package/dist/domain/protected-paths.js +35 -0
- package/dist/domain/retry.js +21 -0
- package/dist/domain/run-id.js +42 -0
- package/dist/domain/run-state.js +147 -0
- package/dist/domain/settings.js +49 -0
- package/dist/domain/slice.js +63 -0
- package/dist/domain/usage.js +1 -0
- package/dist/domain/work-facts.js +22 -0
- package/dist/flow/define-flow.js +11 -0
- package/dist/flow/engine.js +255 -0
- package/dist/flow/run-flow.js +129 -0
- package/dist/git/branch.js +26 -0
- package/dist/git/merge.js +92 -0
- package/dist/git/repository.js +48 -0
- package/dist/git/throwaway-branch.js +64 -0
- package/dist/observers/logging-observer.js +51 -0
- package/dist/observers/metrics.js +109 -0
- package/dist/observers/run-events.js +25 -0
- package/dist/observers/tracing-observer.js +86 -0
- package/dist/ports/agent-session.js +1 -0
- package/dist/ports/observability.js +1 -0
- package/dist/ports/run-store.js +1 -0
- package/dist/ports/sandbox-git.js +1 -0
- package/dist/ports/sandbox-images.js +1 -0
- package/dist/ports/workspace.js +1 -0
- package/dist/prompts/design-review.js +172 -0
- package/dist/prompts/final-check.js +58 -0
- package/dist/prompts/planning.js +111 -0
- package/dist/prompts/pull-request.js +78 -0
- package/dist/prompts/slice.js +254 -0
- package/dist/prompts/toolchain.js +37 -0
- package/dist/steps/completion-reuse.js +32 -0
- package/dist/steps/design-review.js +38 -0
- package/dist/steps/final-check.js +39 -0
- package/dist/steps/finishing.js +72 -0
- package/dist/steps/planning.js +77 -0
- package/dist/steps/pull-request-description.js +142 -0
- package/dist/steps/slice.js +149 -0
- package/package.json +95 -4
- package/presets/cheap-volume.json +7 -0
- package/presets/claude-only.json +7 -0
- package/presets/codex-only.json +7 -0
- package/presets/codex-reviewed-opencode.json +7 -0
- package/presets/full-power-codex.json +7 -0
- package/presets/full-power.json +7 -0
- package/presets/long-claude.json +7 -0
- package/presets/open-weight.json +7 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { execa } from 'execa';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { ImageBuildFailed, messageOf } from '../../domain/errors.js';
|
|
5
|
+
export class DockerImages {
|
|
6
|
+
options;
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
}
|
|
10
|
+
async labels(image) {
|
|
11
|
+
const inspected = await runDocker(execa('docker', ['image', 'inspect', image, '--format', '{{json .Config.Labels}}'], {
|
|
12
|
+
reject: false,
|
|
13
|
+
}), `Could not ask docker about ${image}`);
|
|
14
|
+
if (inspected.isErr())
|
|
15
|
+
return Result.err(inspected.error);
|
|
16
|
+
return classifyInspect(image, inspected.value);
|
|
17
|
+
}
|
|
18
|
+
async build(spec) {
|
|
19
|
+
const build = execa('docker', dockerBuildArguments(spec, this.options.cache), {
|
|
20
|
+
input: spec.dockerfile,
|
|
21
|
+
all: true,
|
|
22
|
+
reject: false,
|
|
23
|
+
});
|
|
24
|
+
build.all?.on('data', (chunk) => this.options.onOutput?.(chunk.toString()));
|
|
25
|
+
const built = await runDocker(build, `Building ${spec.tag} failed`);
|
|
26
|
+
if (built.isErr())
|
|
27
|
+
return Result.err(built.error);
|
|
28
|
+
if (built.value.exitCode === 0)
|
|
29
|
+
return Result.ok();
|
|
30
|
+
const output = didNotStart(built.value)
|
|
31
|
+
? dockerFailure(built.value)
|
|
32
|
+
: (built.value.all ?? '').split('\n').slice(-FAILED_BUILD_OUTPUT_LINES).join('\n');
|
|
33
|
+
return Result.err(new ImageBuildFailed({ message: `Building ${spec.tag} failed:\n${output}`, output }));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Awaiting the process should not reject with `reject: false`; if it does, the cause still reaches the operator. */
|
|
37
|
+
function runDocker(process, message) {
|
|
38
|
+
return Result.tryPromise({
|
|
39
|
+
try: async () => process,
|
|
40
|
+
catch: (cause) => new ImageBuildFailed({ message: `${message}: ${messageOf(cause)}`, output: '' }),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** An image's labels, `undefined` when docker says the image is not there, or why docker could not say. */
|
|
44
|
+
export function classifyInspect(image, outcome) {
|
|
45
|
+
if (outcome.exitCode === 0) {
|
|
46
|
+
return Result.ok(imageLabelsSchema.parse(JSON.parse(outcome.stdout ?? '')) ?? {});
|
|
47
|
+
}
|
|
48
|
+
if (!didNotStart(outcome) && NO_SUCH_IMAGE.test(outcome.stderr ?? ''))
|
|
49
|
+
return Result.ok(undefined);
|
|
50
|
+
const output = dockerFailure(outcome);
|
|
51
|
+
return Result.err(new ImageBuildFailed({ message: `Could not ask docker about ${image}: ${output}`, output }));
|
|
52
|
+
}
|
|
53
|
+
/** `docker image inspect` prints `null` for an image built without labels. */
|
|
54
|
+
const imageLabelsSchema = z.record(z.string()).nullable();
|
|
55
|
+
function didNotStart(outcome) {
|
|
56
|
+
return outcome.exitCode === undefined;
|
|
57
|
+
}
|
|
58
|
+
/** `docker image inspect` of an image that is not there; any other failure is docker's. */
|
|
59
|
+
const NO_SUCH_IMAGE = /no such (image|object)/i;
|
|
60
|
+
/** Docker's own words, or execa's account of why docker did not run at all. */
|
|
61
|
+
function dockerFailure(outcome) {
|
|
62
|
+
return (didNotStart(outcome) ? outcome.shortMessage : outcome.stderr)?.trim() ?? '';
|
|
63
|
+
}
|
|
64
|
+
export function dockerBuildArguments(spec, cache) {
|
|
65
|
+
return [
|
|
66
|
+
...builderArguments(spec.tag, cache),
|
|
67
|
+
'-t',
|
|
68
|
+
spec.tag,
|
|
69
|
+
...flags('--build-arg', spec.buildArgs),
|
|
70
|
+
...flags('--label', spec.labels ?? {}),
|
|
71
|
+
spec.dockerfile === undefined ? (spec.contextDirectory ?? '.') : '-',
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
/** `gha` shares layers between CI jobs through GitHub Actions' cache, which needs buildx. */
|
|
75
|
+
function builderArguments(tag, cache) {
|
|
76
|
+
if (cache !== 'gha')
|
|
77
|
+
return ['build'];
|
|
78
|
+
const scope = tag.replace(/[^a-zA-Z0-9-]/g, '-');
|
|
79
|
+
return [
|
|
80
|
+
'buildx',
|
|
81
|
+
'build',
|
|
82
|
+
'--load',
|
|
83
|
+
'--cache-from',
|
|
84
|
+
`type=gha,scope=${scope}`,
|
|
85
|
+
'--cache-to',
|
|
86
|
+
`type=gha,mode=max,scope=${scope}`,
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
function flags(flag, values) {
|
|
90
|
+
return Object.entries(values).flatMap(([name, value]) => [flag, `${name}=${value}`]);
|
|
91
|
+
}
|
|
92
|
+
/** How many lines of a failed build's output the error carries. */
|
|
93
|
+
const FAILED_BUILD_OUTPUT_LINES = 30;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { ConfigInvalid } from '../../domain/errors.js';
|
|
4
|
+
export const SANDBOX_DIRECTORY = '.sandcastle';
|
|
5
|
+
export const DOCKERFILE = 'Dockerfile';
|
|
6
|
+
export const TOOLCHAIN_START = '# --- project toolchain (yours: what setupCommand and verifyCommand need) ---';
|
|
7
|
+
export const TOOLCHAIN_END = '# --- end project toolchain ---';
|
|
8
|
+
/**
|
|
9
|
+
* The repository's sandbox image without agents: base tools, then the project's toolchain, then
|
|
10
|
+
* the `agent` user. The toolchain comes before the UID so its layers are cached across machines.
|
|
11
|
+
*/
|
|
12
|
+
export const BASE_DOCKERFILE = [
|
|
13
|
+
'FROM node:22-bookworm',
|
|
14
|
+
'',
|
|
15
|
+
'RUN apt-get update \\',
|
|
16
|
+
' && apt-get install -y --no-install-recommends git curl jq ripgrep ca-certificates procps \\',
|
|
17
|
+
' && rm -rf /var/lib/apt/lists/*',
|
|
18
|
+
'',
|
|
19
|
+
TOOLCHAIN_START,
|
|
20
|
+
TOOLCHAIN_END,
|
|
21
|
+
'',
|
|
22
|
+
'ARG AGENT_UID=1000',
|
|
23
|
+
'ARG AGENT_GID=1000',
|
|
24
|
+
'RUN groupmod -o -g $AGENT_GID node && usermod -o -u $AGENT_UID -g $AGENT_GID -d /home/agent -m -l agent node',
|
|
25
|
+
'USER ${AGENT_UID}:${AGENT_GID}',
|
|
26
|
+
'WORKDIR /home/agent',
|
|
27
|
+
'',
|
|
28
|
+
].join('\n');
|
|
29
|
+
/** The layer jig owns: the agent CLIs the config uses, at exact versions, on top of the base image. */
|
|
30
|
+
export function agentLayerDockerfile(baseImage, agents) {
|
|
31
|
+
const packages = agents.map((agent) => `${agent.npmPackage}@${agent.version}`).join(' ');
|
|
32
|
+
return [
|
|
33
|
+
`FROM ${baseImage}`,
|
|
34
|
+
'USER root',
|
|
35
|
+
`RUN npm install -g ${packages} && rm -rf /tmp/opencode`,
|
|
36
|
+
'ARG AGENT_UID=1000',
|
|
37
|
+
'ARG AGENT_GID=1000',
|
|
38
|
+
'USER ${AGENT_UID}:${AGENT_GID}',
|
|
39
|
+
'ENTRYPOINT ["sleep", "infinity"]',
|
|
40
|
+
'',
|
|
41
|
+
].join('\n');
|
|
42
|
+
}
|
|
43
|
+
/** Replaces whatever sits between the toolchain markers, leaving the rest of the Dockerfile alone. */
|
|
44
|
+
export function withToolchain(dockerfile, toolchain) {
|
|
45
|
+
const start = dockerfile.indexOf(TOOLCHAIN_START);
|
|
46
|
+
const end = dockerfile.indexOf(TOOLCHAIN_END);
|
|
47
|
+
if (start === -1 || end === -1 || end < start) {
|
|
48
|
+
return Result.err(new ConfigInvalid({
|
|
49
|
+
message: `The Dockerfile has no "${TOOLCHAIN_START}" section to fill in.`,
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
const body = toolchain.trim();
|
|
53
|
+
return Result.ok([
|
|
54
|
+
dockerfile.slice(0, start + TOOLCHAIN_START.length),
|
|
55
|
+
...(body ? [body] : []),
|
|
56
|
+
dockerfile.slice(end),
|
|
57
|
+
].join('\n'));
|
|
58
|
+
}
|
|
59
|
+
/** Changes whenever the Dockerfile or the user it is built for changes. */
|
|
60
|
+
export function recipeHash(dockerfile, uid, gid) {
|
|
61
|
+
return createHash('sha256').update(`${uid}:${gid}\n${dockerfile}`).digest('hex').slice(0, 16);
|
|
62
|
+
}
|
|
63
|
+
/** What the sandbox image is labelled with, and what a later run compares against to decide what to rebuild. */
|
|
64
|
+
export function imageLabels(dockerfileHash, agents) {
|
|
65
|
+
return {
|
|
66
|
+
[LABELS.dockerfileHash]: dockerfileHash,
|
|
67
|
+
[LABELS.agents]: agents
|
|
68
|
+
.map((agent) => agent.name)
|
|
69
|
+
.sort()
|
|
70
|
+
.join(','),
|
|
71
|
+
[LABELS.agentVersions]: formatAgentVersions(agents),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export const LABELS = {
|
|
75
|
+
dockerfileHash: 'jig.dockerfile-hash',
|
|
76
|
+
agents: 'jig.agents',
|
|
77
|
+
agentVersions: 'jig.agent-versions',
|
|
78
|
+
};
|
|
79
|
+
export function formatAgentVersions(agents) {
|
|
80
|
+
return [...agents]
|
|
81
|
+
.sort((a, b) => a.name.localeCompare(b.name))
|
|
82
|
+
.map((agent) => `${agent.name}=${agent.version}`)
|
|
83
|
+
.join(',');
|
|
84
|
+
}
|
|
85
|
+
/** Reads the label `formatAgentVersions` wrote; an entry without a version counts as not installed. */
|
|
86
|
+
export function parseAgentVersions(label) {
|
|
87
|
+
if (!label)
|
|
88
|
+
return {};
|
|
89
|
+
return Object.fromEntries(label.split(',').flatMap(agentVersionEntry));
|
|
90
|
+
}
|
|
91
|
+
function agentVersionEntry(entry) {
|
|
92
|
+
const separator = entry.indexOf('=');
|
|
93
|
+
if (separator <= 0 || separator === entry.length - 1)
|
|
94
|
+
return [];
|
|
95
|
+
return [[entry.slice(0, separator), entry.slice(separator + 1)]];
|
|
96
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import latestVersion from 'latest-version';
|
|
2
|
+
import pTimeout from 'p-timeout';
|
|
3
|
+
/** The latest published version of each package, or undefined for any the registry did not answer for. */
|
|
4
|
+
export async function latestAgentVersions(npmPackages, lookup = latestVersion, timeoutMs = LOOKUP_TIMEOUT_MS) {
|
|
5
|
+
const versions = await Promise.all(npmPackages.map((npmPackage) => pTimeout(lookup(npmPackage), { milliseconds: timeoutMs }).catch(() => undefined)));
|
|
6
|
+
return Object.fromEntries(npmPackages.map((npmPackage, index) => [npmPackage, versions[index]]));
|
|
7
|
+
}
|
|
8
|
+
/** A slow or unreachable registry must never hold up a run. */
|
|
9
|
+
const LOOKUP_TIMEOUT_MS = 3000;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { claudeCode, codex, opencode } from '@ai-hero/sandcastle';
|
|
2
|
+
import { Result } from 'better-result';
|
|
3
|
+
import { ConfigInvalid } from '../../domain/errors.js';
|
|
4
|
+
import { CREDENTIALS_FILE } from '../local-store/credentials-file.js';
|
|
5
|
+
import { codexLoginProblem } from './codex-auth.js';
|
|
6
|
+
export function isAgentName(name) {
|
|
7
|
+
return Object.hasOwn(AGENTS, name);
|
|
8
|
+
}
|
|
9
|
+
/** Used when a step names claude without a model; the CLI accepts the alias. */
|
|
10
|
+
export const DEFAULT_CLAUDE_MODEL = 'opus';
|
|
11
|
+
/** Every agent jig can run; adding one here is all the config and the sandbox image need. */
|
|
12
|
+
export const AGENTS = {
|
|
13
|
+
claude: {
|
|
14
|
+
npmPackage: '@anthropic-ai/claude-code',
|
|
15
|
+
modelOptions: ['fable', 'opus', 'sonnet', 'haiku'],
|
|
16
|
+
credentialKeys: () => Result.ok(['CLAUDE_CODE_OAUTH_TOKEN', 'ANTHROPIC_API_KEY']),
|
|
17
|
+
credentialHelp: '`claude setup-token` prints an OAuth token',
|
|
18
|
+
credentialIsSecret: true,
|
|
19
|
+
credentialProblem: () => undefined,
|
|
20
|
+
create: (model) => Result.ok(claudeCode(model ?? DEFAULT_CLAUDE_MODEL, { captureSessions: false })),
|
|
21
|
+
},
|
|
22
|
+
opencode: {
|
|
23
|
+
npmPackage: 'opencode-ai',
|
|
24
|
+
modelOptions: [],
|
|
25
|
+
credentialKeys: (model) => requireModel(model).map((required) => [`${modelProvider(required).toUpperCase()}_API_KEY`]),
|
|
26
|
+
credentialHelp: "the API key of the model's provider",
|
|
27
|
+
credentialIsSecret: true,
|
|
28
|
+
credentialProblem: () => undefined,
|
|
29
|
+
create: (model) => requireModel(model).map((required) => opencode(required, { captureSessions: false })),
|
|
30
|
+
},
|
|
31
|
+
codex: {
|
|
32
|
+
npmPackage: '@openai/codex',
|
|
33
|
+
modelOptions: ['gpt-6.1-sol', 'gpt-6-astra', 'gpt-6.1-terra', 'gpt-6-luna'],
|
|
34
|
+
credentialKeys: () => Result.ok(['JIG_CODEX_AUTH_DIR']),
|
|
35
|
+
credentialHelp: 'the folder holding your ChatGPT login, usually ~/.codex',
|
|
36
|
+
credentialIsSecret: false,
|
|
37
|
+
credentialProblem: codexLoginProblem,
|
|
38
|
+
create: (model) => model
|
|
39
|
+
? Result.ok(codex(model, { captureSessions: false }))
|
|
40
|
+
: Result.err(new ConfigInvalid({
|
|
41
|
+
message: 'codex needs an explicit model — set "model" on the step.',
|
|
42
|
+
})),
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
function requireModel(model) {
|
|
46
|
+
return model
|
|
47
|
+
? Result.ok(model)
|
|
48
|
+
: Result.err(new ConfigInvalid({
|
|
49
|
+
message: 'opencode needs a model as "provider/model" (e.g. "openrouter/<model>") — set "model" on the step.',
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
function modelProvider(model) {
|
|
53
|
+
return model.split('/')[0] ?? model;
|
|
54
|
+
}
|
|
55
|
+
/** What is wrong with running `provider` on `model`, if anything. */
|
|
56
|
+
export function modelProblem(provider, model) {
|
|
57
|
+
const created = AGENTS[provider].create(model);
|
|
58
|
+
return created.isErr() ? created.error.message : undefined;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The credentials every configured step needs, looked up by name. Fails before any container
|
|
62
|
+
* starts, naming what to set, when a step's agent has no credential at all or one that cannot work.
|
|
63
|
+
*/
|
|
64
|
+
export function collectCredentials(steps, credential) {
|
|
65
|
+
return missingCredentials(steps, credential).andThen((missing) => {
|
|
66
|
+
if (missing.length > 0) {
|
|
67
|
+
const wanted = missing
|
|
68
|
+
.map(({ provider, keys }) => `${provider}: ${keys.join(' or ')}`)
|
|
69
|
+
.join('; ');
|
|
70
|
+
return Result.err(new ConfigInvalid({
|
|
71
|
+
message: `Agent credentials are missing — set ${wanted} in the environment or in ${CREDENTIALS_FILE}.`,
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
const collected = new Map(steps.flatMap((step) => credentialKeysOf(step).flatMap((key) => {
|
|
75
|
+
const value = credential(key);
|
|
76
|
+
return value ? [[key, { value, provider: step.provider }]] : [];
|
|
77
|
+
})));
|
|
78
|
+
for (const [key, { value, provider }] of collected) {
|
|
79
|
+
const problem = AGENTS[provider].credentialProblem(value);
|
|
80
|
+
if (problem)
|
|
81
|
+
return Result.err(new ConfigInvalid({ message: `${key}: ${problem}` }));
|
|
82
|
+
}
|
|
83
|
+
return Result.ok(Object.fromEntries([...collected].map(([key, { value }]) => [key, value])));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
export function missingCredentials(steps, credential) {
|
|
87
|
+
return Result.gen(function* () {
|
|
88
|
+
const missing = new Map();
|
|
89
|
+
for (const { provider, model } of steps) {
|
|
90
|
+
const keys = yield* AGENTS[provider].credentialKeys(model);
|
|
91
|
+
if (!keys.some((key) => credential(key)))
|
|
92
|
+
missing.set(keys.join(), { provider, keys });
|
|
93
|
+
}
|
|
94
|
+
return Result.ok([...missing.values()]);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function credentialKeysOf({ provider, model }) {
|
|
98
|
+
const keys = AGENTS[provider].credentialKeys(model);
|
|
99
|
+
return keys.isOk() ? keys.value : [];
|
|
100
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { constants, readFileSync } from 'node:fs';
|
|
3
|
+
import { mkdtemp, open, readFile, realpath, rm } from 'node:fs/promises';
|
|
4
|
+
import { homedir, tmpdir } from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import lockfile from 'proper-lockfile';
|
|
7
|
+
import writeFileAtomic from 'write-file-atomic';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { CodexLoginNotSaved, ConfigInvalid, messageOf } from '../../domain/errors.js';
|
|
10
|
+
/** What stops `location` from serving as a Codex login, if anything. */
|
|
11
|
+
export function codexLoginProblem(location) {
|
|
12
|
+
const file = codexLoginFile(location);
|
|
13
|
+
const text = Result.try(() => readFileSync(file, 'utf8'));
|
|
14
|
+
if (text.isErr())
|
|
15
|
+
return `No Codex login at ${file}: expected the auth.json of \`codex login\`.`;
|
|
16
|
+
const valid = validateSubscription(text.value);
|
|
17
|
+
return valid.isErr() ? valid.error.message : undefined;
|
|
18
|
+
}
|
|
19
|
+
/** An operator's path to a login: `~` is the home directory, and the file itself works too. */
|
|
20
|
+
function codexLoginFile(location) {
|
|
21
|
+
const expanded = location.replace(/^~(?=$|\/)/, homedir());
|
|
22
|
+
return path.basename(expanded) === LOGIN_FILE ? expanded : path.join(expanded, LOGIN_FILE);
|
|
23
|
+
}
|
|
24
|
+
const LOGIN_FILE = 'auth.json';
|
|
25
|
+
function validateSubscription(text) {
|
|
26
|
+
const value = Result.try(() => JSON.parse(text));
|
|
27
|
+
if (value.isErr()) {
|
|
28
|
+
return Result.err(new ConfigInvalid({ message: 'Invalid Codex subscription login JSON.' }));
|
|
29
|
+
}
|
|
30
|
+
return subscriptionSchema.safeParse(value.value).success
|
|
31
|
+
? Result.ok()
|
|
32
|
+
: Result.err(new ConfigInvalid({
|
|
33
|
+
message: 'Invalid Codex subscription login: expected the full ChatGPT token bundle.',
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
const subscriptionSchema = z.object({
|
|
37
|
+
auth_mode: z.literal('chatgpt').optional(),
|
|
38
|
+
tokens: z.object({
|
|
39
|
+
access_token: z.string().min(1),
|
|
40
|
+
id_token: z.string().min(1),
|
|
41
|
+
refresh_token: z.string().min(1),
|
|
42
|
+
account_id: z.string().min(1),
|
|
43
|
+
}),
|
|
44
|
+
});
|
|
45
|
+
/** Mounts only a private login copy; Codex can atomically refresh it without exposing host config. */
|
|
46
|
+
export async function openCodexAuthCache(location) {
|
|
47
|
+
const source = await Result.tryPromise({
|
|
48
|
+
try: () => realpath(codexLoginFile(location)),
|
|
49
|
+
catch: (cause) => new ConfigInvalid({
|
|
50
|
+
message: `Could not find the Codex login in ${location}: ${messageOf(cause)}`,
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
if (source.isErr())
|
|
54
|
+
return source;
|
|
55
|
+
const release = await Result.tryPromise({
|
|
56
|
+
try: () => lockfile.lock(source.value, { retries: 0, lockfilePath: `${source.value}${LOCK_SUFFIX}` }),
|
|
57
|
+
catch: (cause) => new ConfigInvalid({
|
|
58
|
+
message: isHeldElsewhere(cause)
|
|
59
|
+
? 'The Codex login is in use by another jig run. Serialize runs using this login.'
|
|
60
|
+
: `Could not lock the Codex login: ${messageOf(cause)}`,
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
if (release.isErr())
|
|
64
|
+
return release;
|
|
65
|
+
const cache = await copyLogin(source.value, release.value);
|
|
66
|
+
if (cache.isErr())
|
|
67
|
+
await releaseAfterFailedCopy(release.value);
|
|
68
|
+
return cache;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Codex keeps its own `auth.json.lock` file beside the login, so proper-lockfile's default lock
|
|
72
|
+
* directory at that path would collide with it and the login would never lock.
|
|
73
|
+
*/
|
|
74
|
+
const LOCK_SUFFIX = '.jig-lock';
|
|
75
|
+
function isHeldElsewhere(cause) {
|
|
76
|
+
return cause?.code === 'ELOCKED';
|
|
77
|
+
}
|
|
78
|
+
async function copyLogin(source, release) {
|
|
79
|
+
const original = await Result.tryPromise({
|
|
80
|
+
try: () => readFile(source, 'utf8'),
|
|
81
|
+
catch: (cause) => new ConfigInvalid({ message: `Could not read the Codex login: ${messageOf(cause)}` }),
|
|
82
|
+
});
|
|
83
|
+
if (original.isErr())
|
|
84
|
+
return original;
|
|
85
|
+
const valid = validateSubscription(original.value);
|
|
86
|
+
if (valid.isErr())
|
|
87
|
+
return valid;
|
|
88
|
+
const directory = await Result.tryPromise({
|
|
89
|
+
try: async () => {
|
|
90
|
+
const created = await mkdtemp(path.join(tmpdir(), 'jig-codex-auth-'));
|
|
91
|
+
let copied = false;
|
|
92
|
+
try {
|
|
93
|
+
await writeFileAtomic(path.join(created, LOGIN_FILE), original.value, { mode: 0o600 });
|
|
94
|
+
copied = true;
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
if (!copied)
|
|
98
|
+
await rm(created, { recursive: true, force: true });
|
|
99
|
+
}
|
|
100
|
+
return created;
|
|
101
|
+
},
|
|
102
|
+
catch: (cause) => new ConfigInvalid({ message: `Could not copy the Codex login: ${messageOf(cause)}` }),
|
|
103
|
+
});
|
|
104
|
+
return directory.map((copy) => ({
|
|
105
|
+
directory: copy,
|
|
106
|
+
close: () => persistAndRelease(source, original.value, copy, release),
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
async function persistAndRelease(source, original, directory, release) {
|
|
110
|
+
let persisted;
|
|
111
|
+
let released;
|
|
112
|
+
try {
|
|
113
|
+
persisted = await persistOrDiscard(source, original, directory);
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
released = await Result.tryPromise({
|
|
117
|
+
try: () => release(),
|
|
118
|
+
catch: (cause) => new CodexLoginNotSaved({
|
|
119
|
+
message: `Could not unlock the Codex login: ${messageOf(cause)}`,
|
|
120
|
+
}),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return persisted.isErr() ? persisted : released;
|
|
124
|
+
}
|
|
125
|
+
/** Hands a valid refresh back to the operator's login; the copy is kept only when that fails. */
|
|
126
|
+
async function persistOrDiscard(source, original, directory) {
|
|
127
|
+
const refreshed = await Result.tryPromise({
|
|
128
|
+
try: () => readRefreshedLogin(path.join(directory, 'auth.json')),
|
|
129
|
+
catch: (cause) => new CodexLoginNotSaved({
|
|
130
|
+
message: `Could not read the refreshed Codex login: ${messageOf(cause)}`,
|
|
131
|
+
}),
|
|
132
|
+
});
|
|
133
|
+
if (refreshed.isErr())
|
|
134
|
+
return keepFirstFailure(refreshed, await removeCopy(directory));
|
|
135
|
+
const valid = validateSubscription(refreshed.value).mapError((error) => new CodexLoginNotSaved({ message: error.message }));
|
|
136
|
+
const persisted = valid.isOk() && refreshed.value !== original
|
|
137
|
+
? await persistRefresh(source, original, refreshed.value, directory)
|
|
138
|
+
: valid;
|
|
139
|
+
const retained = persisted.isErr() && valid.isOk();
|
|
140
|
+
return retained ? persisted : keepFirstFailure(persisted, await removeCopy(directory));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The sandbox can write this directory, so the entry may be a link to any file on this machine.
|
|
144
|
+
* Refusing the link while opening leaves no gap between checking the file and reading it.
|
|
145
|
+
*/
|
|
146
|
+
async function readRefreshedLogin(file) {
|
|
147
|
+
const handle = await open(file, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
148
|
+
try {
|
|
149
|
+
return await handle.readFile('utf8');
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
await handle.close();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function keepFirstFailure(first, then) {
|
|
156
|
+
if (first.isErr())
|
|
157
|
+
return Result.err(first.error);
|
|
158
|
+
return then;
|
|
159
|
+
}
|
|
160
|
+
/** The copy holds credentials, so one that cannot be removed is reported, not left behind quietly. */
|
|
161
|
+
function removeCopy(directory) {
|
|
162
|
+
return Result.tryPromise({
|
|
163
|
+
try: () => rm(directory, { recursive: true, force: true }),
|
|
164
|
+
catch: (cause) => new CodexLoginNotSaved({
|
|
165
|
+
message: `Could not remove the copy of the Codex login at ${directory}: ${messageOf(cause)}`,
|
|
166
|
+
}),
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
async function persistRefresh(source, original, refreshed, directory) {
|
|
170
|
+
const current = await Result.tryPromise({
|
|
171
|
+
try: () => readFile(source, 'utf8'),
|
|
172
|
+
catch: (cause) => new CodexLoginNotSaved({ message: `Could not read the Codex login: ${messageOf(cause)}` }),
|
|
173
|
+
});
|
|
174
|
+
if (current.isErr())
|
|
175
|
+
return current.map(() => undefined);
|
|
176
|
+
if (current.value !== original) {
|
|
177
|
+
return Result.err(new CodexLoginNotSaved({
|
|
178
|
+
message: `The Codex login changed during the run. Refreshed credentials are retained at ${directory}.`,
|
|
179
|
+
}));
|
|
180
|
+
}
|
|
181
|
+
return Result.tryPromise({
|
|
182
|
+
try: () => writeFileAtomic(source, refreshed, { mode: 0o600 }),
|
|
183
|
+
catch: (cause) => new CodexLoginNotSaved({
|
|
184
|
+
message: `Could not save the refreshed Codex login: ${messageOf(cause)}`,
|
|
185
|
+
}),
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/** The copy already failed and is what the caller needs; proper-lockfile drops the lock at exit anyway. */
|
|
189
|
+
async function releaseAfterFailedCopy(release) {
|
|
190
|
+
await release().catch(() => undefined);
|
|
191
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* The manager whose hooks the sandbox's clone needs. A clone has none: they are installed by an
|
|
5
|
+
* npm script or by hand, and a repository may disable install scripts.
|
|
6
|
+
*/
|
|
7
|
+
export function hookManagerOf(repoRoot) {
|
|
8
|
+
return HOOK_MANAGERS.find((manager) => manager.isUsedIn(repoRoot));
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The hook managers jig installs in the sandbox, tried in order. Each `npx --no` uses the copy
|
|
12
|
+
* the setup command installed and never fetches one from the registry.
|
|
13
|
+
*/
|
|
14
|
+
const HOOK_MANAGERS = [
|
|
15
|
+
{
|
|
16
|
+
name: 'husky',
|
|
17
|
+
installCommand: 'npx --no -- husky',
|
|
18
|
+
isUsedIn: (repoRoot) => existsSync(path.join(repoRoot, '.husky')),
|
|
19
|
+
},
|
|
20
|
+
];
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { configuredSteps } from '../../config/load-config.js';
|
|
3
|
+
import { agentLogFile } from '../local-store/run-paths.js';
|
|
4
|
+
import { sandboxTelemetryEnvironment } from '../telemetry/open-telemetry.js';
|
|
5
|
+
import { AGENTS, collectCredentials } from './agents.js';
|
|
6
|
+
import { sandcastleEnvKeys } from './sandcastle-env.js';
|
|
7
|
+
import { SandcastleSession, } from './sandcastle-session.js';
|
|
8
|
+
/** Starts a sandbox for `input.run`, warning first about variables `.sandcastle/.env` adds to it. */
|
|
9
|
+
export async function openSandcastleSession(context, input) {
|
|
10
|
+
await warnWhenSandcastleEnvAddsVariables(context.repoRoot, context.logger);
|
|
11
|
+
const options = sandcastleSessionOptions(context, input);
|
|
12
|
+
if (options.isErr())
|
|
13
|
+
return Result.err(options.error);
|
|
14
|
+
return SandcastleSession.open(options.value);
|
|
15
|
+
}
|
|
16
|
+
/** jig passes credentials from the environment; a variable from `.sandcastle/.env` is usually a mistake. */
|
|
17
|
+
export async function warnWhenSandcastleEnvAddsVariables(repoRoot, logger) {
|
|
18
|
+
const injected = await sandcastleEnvKeys(repoRoot);
|
|
19
|
+
if (injected.length === 0)
|
|
20
|
+
return;
|
|
21
|
+
logger.warn(`.sandcastle/.env also puts ${injected.join(', ')} into the sandbox — jig passes credentials from the environment; remove the file unless the agents need these`);
|
|
22
|
+
}
|
|
23
|
+
/** What the run's sandbox is started with: the configured agents, their credentials and limits. */
|
|
24
|
+
export function sandcastleSessionOptions({ repoRoot, config, environment, imageName, logger }, { run, events, setupCommand }) {
|
|
25
|
+
const configured = configuredSteps(config);
|
|
26
|
+
if (configured.isErr())
|
|
27
|
+
return Result.err(configured.error);
|
|
28
|
+
const steps = configured.value;
|
|
29
|
+
const credentials = stepCredentials(config, environment);
|
|
30
|
+
if (credentials.isErr())
|
|
31
|
+
return Result.err(credentials.error);
|
|
32
|
+
const agents = agentsFor(steps);
|
|
33
|
+
if (agents.isErr())
|
|
34
|
+
return Result.err(agents.error);
|
|
35
|
+
return Result.ok({
|
|
36
|
+
repoRoot,
|
|
37
|
+
...run,
|
|
38
|
+
imageName,
|
|
39
|
+
codexAuthDirectory: Object.values(steps).some((step) => step.provider === 'codex')
|
|
40
|
+
? environment.codexAuthDirectory
|
|
41
|
+
: undefined,
|
|
42
|
+
credentials: {
|
|
43
|
+
...credentials.value,
|
|
44
|
+
...sandboxTelemetryEnvironment(environment.telemetry),
|
|
45
|
+
},
|
|
46
|
+
agents: agents.value,
|
|
47
|
+
setupCommand: setupCommand === null ? undefined : (setupCommand ?? config.setupCommand),
|
|
48
|
+
setupTimeoutSeconds: config.setupTimeoutSeconds,
|
|
49
|
+
idleTimeoutSeconds: config.agentIdleTimeoutSeconds,
|
|
50
|
+
agentLogFile: agentLogFile(repoRoot, run.runId),
|
|
51
|
+
events,
|
|
52
|
+
logger,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/** The credentials of every step's agent; fails naming what to set when one has none. */
|
|
56
|
+
export function stepCredentials(config, environment) {
|
|
57
|
+
return configuredSteps(config).andThen((steps) => collectCredentials(Object.values(steps), environment.credential));
|
|
58
|
+
}
|
|
59
|
+
function agentsFor(steps) {
|
|
60
|
+
return Result.gen(function* () {
|
|
61
|
+
const planner = yield* AGENTS[steps.planner.provider].create(steps.planner.model);
|
|
62
|
+
const implementer = yield* AGENTS[steps.implementer.provider].create(steps.implementer.model);
|
|
63
|
+
const reviewer = yield* AGENTS[steps.reviewer.provider].create(steps.reviewer.model);
|
|
64
|
+
const describer = yield* AGENTS[steps.describer.provider].create(steps.describer.model);
|
|
65
|
+
return Result.ok({ planner, implementer, reviewer, describer });
|
|
66
|
+
});
|
|
67
|
+
}
|