@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,119 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, truncateSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { agentRoleSchema } from '../../domain/agent-role.js';
|
|
6
|
+
import { MetricsLogCorrupt } from '../../domain/errors.js';
|
|
7
|
+
import { stepLocationSchema } from '../../domain/events.js';
|
|
8
|
+
import { outcomeTypeSchema } from '../../domain/outcome.js';
|
|
9
|
+
import { runIdSchema } from '../../domain/run-id.js';
|
|
10
|
+
import { summarizeMetrics, } from '../../observers/metrics.js';
|
|
11
|
+
import { stateDirectory } from './run-paths.js';
|
|
12
|
+
/**
|
|
13
|
+
* Appends metric records to `.jig/<runId>/state/metrics.jsonl`, one JSON object per line,
|
|
14
|
+
* across every invocation of the run. Appends are synchronous so records keep event order.
|
|
15
|
+
* A record counts once its line is complete: a process killed mid-append can only leave the
|
|
16
|
+
* last line without its newline, so that tail is ignored on read and cut off before the next
|
|
17
|
+
* append. A complete line that is not a record cannot come from a crash, so it is reported as `MetricsLogCorrupt`.
|
|
18
|
+
*/
|
|
19
|
+
export class MetricsLog {
|
|
20
|
+
file;
|
|
21
|
+
tornTailRemoved = false;
|
|
22
|
+
constructor(repoRoot, runId) {
|
|
23
|
+
this.file = path.join(stateDirectory(repoRoot, runId), 'metrics.jsonl');
|
|
24
|
+
}
|
|
25
|
+
append(record) {
|
|
26
|
+
mkdirSync(path.dirname(this.file), { recursive: true });
|
|
27
|
+
if (!this.tornTailRemoved) {
|
|
28
|
+
this.removeTornTail();
|
|
29
|
+
this.tornTailRemoved = true;
|
|
30
|
+
}
|
|
31
|
+
appendFileSync(this.file, `${JSON.stringify(record)}\n`);
|
|
32
|
+
}
|
|
33
|
+
removeTornTail() {
|
|
34
|
+
if (!existsSync(this.file))
|
|
35
|
+
return;
|
|
36
|
+
const contents = readFileSync(this.file);
|
|
37
|
+
const completeLength = contents.lastIndexOf('\n') + 1;
|
|
38
|
+
if (completeLength < contents.length)
|
|
39
|
+
truncateSync(this.file, completeLength);
|
|
40
|
+
}
|
|
41
|
+
/** The run's totals so far, across every invocation this machine ran. */
|
|
42
|
+
summary() {
|
|
43
|
+
return this.readAll().map(summarizeMetrics);
|
|
44
|
+
}
|
|
45
|
+
readAll() {
|
|
46
|
+
if (!existsSync(this.file))
|
|
47
|
+
return Result.ok([]);
|
|
48
|
+
const records = [];
|
|
49
|
+
for (const [index, line] of completeLines(readFileSync(this.file, 'utf-8')).entries()) {
|
|
50
|
+
const record = this.parseLine(line, index + 1);
|
|
51
|
+
if (record.isErr())
|
|
52
|
+
return Result.err(record.error);
|
|
53
|
+
records.push(record.value);
|
|
54
|
+
}
|
|
55
|
+
return Result.ok(records);
|
|
56
|
+
}
|
|
57
|
+
parseLine(line, lineNumber) {
|
|
58
|
+
const parsed = metricRecordSchema.safeParse(parseJson(line));
|
|
59
|
+
if (parsed.success)
|
|
60
|
+
return Result.ok(parsed.data);
|
|
61
|
+
return Result.err(new MetricsLogCorrupt({
|
|
62
|
+
message: `Line ${lineNumber} of ${this.file} is not a metric record: ${parsed.error.message}`,
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function completeLines(contents) {
|
|
67
|
+
return contents
|
|
68
|
+
.slice(0, contents.lastIndexOf('\n') + 1)
|
|
69
|
+
.split('\n')
|
|
70
|
+
.filter(Boolean);
|
|
71
|
+
}
|
|
72
|
+
const metricRecordSchema = z.discriminatedUnion('kind', [
|
|
73
|
+
z.object({
|
|
74
|
+
kind: z.literal('step'),
|
|
75
|
+
at: z.string(),
|
|
76
|
+
runId: runIdSchema,
|
|
77
|
+
location: stepLocationSchema,
|
|
78
|
+
outcome: outcomeTypeSchema,
|
|
79
|
+
durationMs: z.number(),
|
|
80
|
+
commit: z
|
|
81
|
+
.object({
|
|
82
|
+
sha: z.string(),
|
|
83
|
+
filesChanged: z.number(),
|
|
84
|
+
insertions: z.number(),
|
|
85
|
+
deletions: z.number(),
|
|
86
|
+
})
|
|
87
|
+
.optional(),
|
|
88
|
+
}),
|
|
89
|
+
z.object({
|
|
90
|
+
kind: z.literal('agentCall'),
|
|
91
|
+
at: z.string(),
|
|
92
|
+
runId: runIdSchema,
|
|
93
|
+
location: stepLocationSchema.optional(),
|
|
94
|
+
role: agentRoleSchema,
|
|
95
|
+
costUsd: z.number().optional(),
|
|
96
|
+
tokens: z.object({
|
|
97
|
+
input: z.number(),
|
|
98
|
+
output: z.number(),
|
|
99
|
+
cacheRead: z.number(),
|
|
100
|
+
cacheWrite: z.number(),
|
|
101
|
+
reasoning: z.number(),
|
|
102
|
+
}),
|
|
103
|
+
}),
|
|
104
|
+
z.object({
|
|
105
|
+
kind: z.literal('retry'),
|
|
106
|
+
at: z.string(),
|
|
107
|
+
runId: runIdSchema,
|
|
108
|
+
location: stepLocationSchema,
|
|
109
|
+
reason: z.string(),
|
|
110
|
+
}),
|
|
111
|
+
]);
|
|
112
|
+
function parseJson(line) {
|
|
113
|
+
try {
|
|
114
|
+
return JSON.parse(line);
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { mkdir, rm } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import writeFileAtomic from 'write-file-atomic';
|
|
5
|
+
import { designReviewReport } from '../../domain/design-review-report.js';
|
|
6
|
+
import { runStopReason } from '../../domain/run-state.js';
|
|
7
|
+
import { formatSummary } from '../../observers/metrics.js';
|
|
8
|
+
import { onLocalState } from './local-state.js';
|
|
9
|
+
import { makeRunDirectory, stateDirectory } from './run-paths.js';
|
|
10
|
+
/**
|
|
11
|
+
* This machine's record of how an invocation of a run ended, written once at its end and only
|
|
12
|
+
* here: `result.json` for CI, and the design, plan, design review and pull request description
|
|
13
|
+
* as markdown for a person. Each file is replaced atomically, so a reader never sees half of one.
|
|
14
|
+
*/
|
|
15
|
+
export class RunArtefacts {
|
|
16
|
+
repoRoot;
|
|
17
|
+
constructor(repoRoot) {
|
|
18
|
+
this.repoRoot = repoRoot;
|
|
19
|
+
}
|
|
20
|
+
async write(state, status, metrics) {
|
|
21
|
+
const directory = stateDirectory(this.repoRoot, state.runId);
|
|
22
|
+
return onLocalState(directory, async () => {
|
|
23
|
+
await this.makeStateDirectory(state.runId);
|
|
24
|
+
const write = (name, contents) => writeOrRemove(path.join(directory, name), contents);
|
|
25
|
+
await write('result.json', `${JSON.stringify(resultFor(state, status, metrics), null, 2)}\n`);
|
|
26
|
+
const contents = documentContents(state, metrics);
|
|
27
|
+
for (const name of DOCUMENTS)
|
|
28
|
+
await write(name, contents[name]);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async makeStateDirectory(runId) {
|
|
32
|
+
await makeRunDirectory(this.repoRoot, runId);
|
|
33
|
+
await mkdir(stateDirectory(this.repoRoot, runId), { recursive: true });
|
|
34
|
+
}
|
|
35
|
+
/** The documents this machine holds for the run; a run that has not produced one has no file. */
|
|
36
|
+
documents(runId) {
|
|
37
|
+
const directory = stateDirectory(this.repoRoot, runId);
|
|
38
|
+
return DOCUMENTS.map((name) => ({ name, path: path.join(directory, name) })).filter((document) => existsSync(document.path));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async function writeOrRemove(file, contents) {
|
|
42
|
+
if (contents === undefined)
|
|
43
|
+
await rm(file, { force: true });
|
|
44
|
+
else
|
|
45
|
+
await writeFileAtomic(file, contents);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A small, stable summary for CI to read instead of parsing output that also carries whatever
|
|
49
|
+
* an agent printed. `complete` and `blockedReason` keep the shape earlier workflows read.
|
|
50
|
+
*/
|
|
51
|
+
function resultFor(state, status, metrics) {
|
|
52
|
+
return {
|
|
53
|
+
runId: state.runId,
|
|
54
|
+
status,
|
|
55
|
+
phase: state.phase,
|
|
56
|
+
complete: status === 'done',
|
|
57
|
+
blockedReason: state.blockedReason ?? runStopReason(state),
|
|
58
|
+
metrics,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function documentContents(state, metrics) {
|
|
62
|
+
return {
|
|
63
|
+
'design.md': state.docs?.design,
|
|
64
|
+
'plan.md': state.docs?.plan,
|
|
65
|
+
'design-review.md': designReviewReport(state),
|
|
66
|
+
'PR_DESCRIPTION.md': pullRequestDescription(state, metrics),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function pullRequestDescription(state, metrics) {
|
|
70
|
+
if (state.prDescription === undefined)
|
|
71
|
+
return undefined;
|
|
72
|
+
const footer = `<sub>jig run ${state.runId}: ${formatSummary(metrics)}</sub>`;
|
|
73
|
+
return `${state.prDescription}\n\n---\n${footer}\n`;
|
|
74
|
+
}
|
|
75
|
+
/** The markdown a run leaves for a person to read, in the order `jig status` lists them. */
|
|
76
|
+
const DOCUMENTS = ['design.md', 'plan.md', 'design-review.md', 'PR_DESCRIPTION.md'];
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import lockfile from 'proper-lockfile';
|
|
4
|
+
import { LocalStateUnwritable, messageOf, RunInProgress } from '../../domain/errors.js';
|
|
5
|
+
import { onLocalState } from './local-state.js';
|
|
6
|
+
import { findLinkInRunDirectory, makeRunDirectory, runDirectory } from './run-paths.js';
|
|
7
|
+
/**
|
|
8
|
+
* Makes this process the run's only writer until the returned release is called. A second
|
|
9
|
+
* process fails at once with `RunInProgress` rather than waiting for the first to finish. Every
|
|
10
|
+
* write of a run comes after this lock, so a run directory holding a link is refused here.
|
|
11
|
+
*/
|
|
12
|
+
export async function lockRun(repoRoot, runId) {
|
|
13
|
+
const directory = runDirectory(repoRoot, runId);
|
|
14
|
+
const lockfilePath = path.join(directory, 'run.lock');
|
|
15
|
+
const link = await onLocalState(directory, async () => findLinkInRunDirectory(repoRoot, runId));
|
|
16
|
+
if (link.isErr())
|
|
17
|
+
return Result.err(link.error);
|
|
18
|
+
if (link.value) {
|
|
19
|
+
return Result.err(new LocalStateUnwritable({
|
|
20
|
+
message: `Refusing to use run "${runId}": ${link.value} is a symbolic link.`,
|
|
21
|
+
path: link.value,
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
return Result.tryPromise({
|
|
25
|
+
try: async () => {
|
|
26
|
+
await makeRunDirectory(repoRoot, runId);
|
|
27
|
+
return lockfile.lock(directory, {
|
|
28
|
+
lockfilePath,
|
|
29
|
+
realpath: false,
|
|
30
|
+
stale: STALE_LOCK_MS,
|
|
31
|
+
retries: 0,
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
catch: (cause) => isHeldElsewhere(cause)
|
|
35
|
+
? new RunInProgress({
|
|
36
|
+
message: `Run "${runId}" is already running in another jig process.`,
|
|
37
|
+
})
|
|
38
|
+
: new LocalStateUnwritable({
|
|
39
|
+
message: `Cannot lock run "${runId}" at ${lockfilePath}: ${messageOf(cause)}`,
|
|
40
|
+
path: lockfilePath,
|
|
41
|
+
}),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A live holder refreshes its lock every half of this; a lock left this long without a refresh
|
|
46
|
+
* belongs to a process that died, and the next run takes it over.
|
|
47
|
+
*/
|
|
48
|
+
const STALE_LOCK_MS = 30_000;
|
|
49
|
+
function isHeldElsewhere(error) {
|
|
50
|
+
return error instanceof Error && 'code' in error && error.code === 'ELOCKED';
|
|
51
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { findSymlink } from '../find-symlink.js';
|
|
5
|
+
/** This machine's log of a run: what jig did, one line per event. */
|
|
6
|
+
export function runLogFile(repoRoot, runId) {
|
|
7
|
+
return path.join(stateDirectory(repoRoot, runId), 'run.log');
|
|
8
|
+
}
|
|
9
|
+
export function stateDirectory(repoRoot, runId) {
|
|
10
|
+
return path.join(runDirectory(repoRoot, runId), 'state');
|
|
11
|
+
}
|
|
12
|
+
export function runDirectory(repoRoot, runId) {
|
|
13
|
+
return path.join(repoRoot, JIG_DIR, runId);
|
|
14
|
+
}
|
|
15
|
+
/** jig's scratch space inside the operator's repository; it ignores itself. */
|
|
16
|
+
export const JIG_DIR = '.jig';
|
|
17
|
+
/** What the agents printed during a run, kept apart from jig's own log. */
|
|
18
|
+
export function agentLogFile(repoRoot, runId) {
|
|
19
|
+
return path.join(stateDirectory(repoRoot, runId), 'agents.log');
|
|
20
|
+
}
|
|
21
|
+
/** Creates the run's directory under `.jig/`, which ignores itself so it never dirties the target repo. */
|
|
22
|
+
export async function makeRunDirectory(repoRoot, runId) {
|
|
23
|
+
const directory = runDirectory(repoRoot, runId);
|
|
24
|
+
await mkdir(directory, { recursive: true });
|
|
25
|
+
await ignoreJigDirectory(repoRoot);
|
|
26
|
+
return directory;
|
|
27
|
+
}
|
|
28
|
+
export async function ignoreJigDirectory(repoRoot) {
|
|
29
|
+
await mkdir(path.join(repoRoot, JIG_DIR), { recursive: true });
|
|
30
|
+
const gitignore = path.join(repoRoot, JIG_DIR, '.gitignore');
|
|
31
|
+
if (!existsSync(gitignore))
|
|
32
|
+
await writeFile(gitignore, '*\n');
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The first link among the paths a run reads and writes. Each level is checked before it is
|
|
36
|
+
* listed, so the listing never follows a link. Worktrees below these levels may hold links freely.
|
|
37
|
+
*/
|
|
38
|
+
export function findLinkInRunDirectory(repoRoot, runId) {
|
|
39
|
+
const jigDirectory = path.join(repoRoot, JIG_DIR);
|
|
40
|
+
const directory = runDirectory(repoRoot, runId);
|
|
41
|
+
return (findSymlink([jigDirectory, path.join(jigDirectory, '.gitignore'), directory]) ??
|
|
42
|
+
findSymlink(entriesOf(directory)) ??
|
|
43
|
+
findSymlink(entriesOf(stateDirectory(repoRoot, runId))));
|
|
44
|
+
}
|
|
45
|
+
function entriesOf(directory) {
|
|
46
|
+
if (!existsSync(directory))
|
|
47
|
+
return [];
|
|
48
|
+
return readdirSync(directory).map((entry) => path.join(directory, entry));
|
|
49
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { mkdirSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { pino } from 'pino';
|
|
4
|
+
import pretty from 'pino-pretty';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
export const logFormatSchema = z.enum(['pretty', 'json']);
|
|
7
|
+
/** Pretty for a person at a terminal, JSON lines everywhere else (CI, pipes, files). */
|
|
8
|
+
export function defaultLogFormat(stream = process.stdout) {
|
|
9
|
+
return stream.isTTY ? 'pretty' : 'json';
|
|
10
|
+
}
|
|
11
|
+
export function createConsoleLogger(format, level = 'info') {
|
|
12
|
+
return pino({ level }, consoleStream(format));
|
|
13
|
+
}
|
|
14
|
+
function consoleStream(format) {
|
|
15
|
+
if (format === 'json')
|
|
16
|
+
return pino.destination({ dest: 1, sync: true });
|
|
17
|
+
return pretty({ sync: true, ignore: 'pid,hostname,event,runId,at', translateTime: 'HH:MM:ss' });
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The console logger plus a JSON log file for the run, so a run can be inspected afterwards
|
|
21
|
+
* whatever the console showed.
|
|
22
|
+
*/
|
|
23
|
+
export function createRunLogger(format, logFile, level = 'info') {
|
|
24
|
+
mkdirSync(path.dirname(logFile), { recursive: true });
|
|
25
|
+
return pino({ level }, pino.multistream([
|
|
26
|
+
{ level, stream: consoleStream(format) },
|
|
27
|
+
{ level, stream: pino.destination({ dest: logFile, sync: true }) },
|
|
28
|
+
]));
|
|
29
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
/**
|
|
3
|
+
* Keeps a copy of each run in every store. A load or a peek asks every store and returns the
|
|
4
|
+
* copy with the highest revision; a tie goes to the store listed first. A load also lets a
|
|
5
|
+
* remote store learn what it must not overwrite. A save stamps the next revision past any this store has seen, so one
|
|
6
|
+
* process never writes two different states under the same revision.
|
|
7
|
+
*/
|
|
8
|
+
export class CompositeRunStore {
|
|
9
|
+
stores;
|
|
10
|
+
latestRevisions = new Map();
|
|
11
|
+
constructor(stores) {
|
|
12
|
+
this.stores = stores;
|
|
13
|
+
}
|
|
14
|
+
async load(runId) {
|
|
15
|
+
const copies = await this.readEach((store) => store.load(runId));
|
|
16
|
+
if (copies.isErr())
|
|
17
|
+
return Result.err(copies.error);
|
|
18
|
+
const freshest = freshestOf(copies.value);
|
|
19
|
+
if (freshest)
|
|
20
|
+
this.latestRevisions.set(runId, freshest.revision);
|
|
21
|
+
return Result.ok(freshest);
|
|
22
|
+
}
|
|
23
|
+
async readEach(read) {
|
|
24
|
+
const copies = [];
|
|
25
|
+
for (const store of this.stores) {
|
|
26
|
+
const copy = await read(store);
|
|
27
|
+
if (copy.isErr())
|
|
28
|
+
return Result.err(copy.error);
|
|
29
|
+
copies.push(copy.value);
|
|
30
|
+
}
|
|
31
|
+
return Result.ok(copies);
|
|
32
|
+
}
|
|
33
|
+
async peek(runId) {
|
|
34
|
+
const copies = await this.readEach((store) => store.peek(runId));
|
|
35
|
+
return copies.map(freshestOf);
|
|
36
|
+
}
|
|
37
|
+
async save(state) {
|
|
38
|
+
const revision = Math.max(state.revision, this.latestRevisions.get(state.runId) ?? 0) + 1;
|
|
39
|
+
this.latestRevisions.set(state.runId, revision);
|
|
40
|
+
const stamped = { ...state, revision };
|
|
41
|
+
for (const store of this.stores) {
|
|
42
|
+
const saved = await store.save(stamped);
|
|
43
|
+
if (saved.isErr())
|
|
44
|
+
return saved;
|
|
45
|
+
}
|
|
46
|
+
return Result.ok();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The copy with the highest revision; on a tie, the one listed first. */
|
|
50
|
+
function freshestOf(copies) {
|
|
51
|
+
let freshest;
|
|
52
|
+
for (const copy of copies) {
|
|
53
|
+
if (copy && (!freshest || copy.revision > freshest.revision))
|
|
54
|
+
freshest = copy;
|
|
55
|
+
}
|
|
56
|
+
return freshest;
|
|
57
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { execa } from 'execa';
|
|
3
|
+
import { ConfigInvalid, GitCommandFailed, messageOf, RemoteUnavailable, RunStateConflict, } from '../../domain/errors.js';
|
|
4
|
+
import { retryResult } from '../../domain/retry.js';
|
|
5
|
+
import { runBranchFor, runStateSchema } from '../../domain/run-state.js';
|
|
6
|
+
/**
|
|
7
|
+
* Keeps each run's state as a commit on a hidden `refs/jig/<runId>` ref and pushes it, with the
|
|
8
|
+
* run branch, to a remote — so another machine, such as a retried CI job, can resume the run.
|
|
9
|
+
* The hidden ref keeps state commits out of the repository's branch list. A save only replaces
|
|
10
|
+
* what this store last saw on the remote, so a concurrent writer is refused rather than overwritten.
|
|
11
|
+
*/
|
|
12
|
+
export class GitRemoteRunStore {
|
|
13
|
+
repoRoot;
|
|
14
|
+
remote;
|
|
15
|
+
seenOnRemote = new Map();
|
|
16
|
+
constructor(repoRoot, remote = 'origin') {
|
|
17
|
+
this.repoRoot = repoRoot;
|
|
18
|
+
this.remote = remote;
|
|
19
|
+
}
|
|
20
|
+
load(runId) {
|
|
21
|
+
return Result.gen(async function* () {
|
|
22
|
+
const remoteRun = yield* Result.await(this.fetchRun(runId));
|
|
23
|
+
this.seenOnRemote.set(runId, remoteRun);
|
|
24
|
+
if (!remoteRun.state)
|
|
25
|
+
return Result.ok(undefined);
|
|
26
|
+
if (remoteRun.branch)
|
|
27
|
+
yield* Result.await(this.catchUpRunBranch(runId, remoteRun.branch));
|
|
28
|
+
const shown = yield* Result.await(this.required(['show', `${stateRef(runId)}:${STATE_FILE}`]));
|
|
29
|
+
return parseState(shown, this.remote);
|
|
30
|
+
}, this);
|
|
31
|
+
}
|
|
32
|
+
/** Brings the run's refs over from the remote; the run branch lands on a remote-tracking ref. */
|
|
33
|
+
fetchRun(runId) {
|
|
34
|
+
return Result.gen(async function* () {
|
|
35
|
+
const refsOnRemote = yield* Result.await(this.listOnRemote(runId));
|
|
36
|
+
if (!refsOnRemote.has(stateRef(runId)))
|
|
37
|
+
return Result.ok({});
|
|
38
|
+
const hasBranch = refsOnRemote.has(branchRef(runId));
|
|
39
|
+
yield* Result.await(this.remoteCall([
|
|
40
|
+
'fetch',
|
|
41
|
+
'--quiet',
|
|
42
|
+
this.remote,
|
|
43
|
+
`+${stateRef(runId)}:${stateRef(runId)}`,
|
|
44
|
+
...(hasBranch ? [`+${branchRef(runId)}:${this.trackingRef(runId)}`] : []),
|
|
45
|
+
]));
|
|
46
|
+
return Result.ok({
|
|
47
|
+
state: await this.revision(stateRef(runId)),
|
|
48
|
+
branch: hasBranch ? await this.revision(this.trackingRef(runId)) : undefined,
|
|
49
|
+
});
|
|
50
|
+
}, this);
|
|
51
|
+
}
|
|
52
|
+
/** The run's refs on the remote, each with the commit it points at. */
|
|
53
|
+
async listOnRemote(runId) {
|
|
54
|
+
const listed = await this.remoteCall(['ls-remote', '--exit-code', this.remote, stateRef(runId), branchRef(runId)], NO_MATCHING_REFS);
|
|
55
|
+
return listed.map((output) => {
|
|
56
|
+
const entries = output.stdout
|
|
57
|
+
.split('\n')
|
|
58
|
+
.filter(Boolean)
|
|
59
|
+
.map((line) => {
|
|
60
|
+
const [commit = '', ref = ''] = line.split('\t');
|
|
61
|
+
return [ref, commit];
|
|
62
|
+
});
|
|
63
|
+
return new Map(entries);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/** A remote operation, retried while the failure looks like the network rather than a conflict. */
|
|
67
|
+
remoteCall(args, alsoAcceptedExitCode) {
|
|
68
|
+
return retryResult(async () => {
|
|
69
|
+
const result = await this.git(args);
|
|
70
|
+
if (result.exitCode === 0 || result.exitCode === alsoAcceptedExitCode) {
|
|
71
|
+
return Result.ok(result);
|
|
72
|
+
}
|
|
73
|
+
return Result.err(isLeaseRejection(result.stderr)
|
|
74
|
+
? new RunStateConflict({
|
|
75
|
+
message: `The run's state on ${this.remote} moved since it was loaded: ${result.stderr}`,
|
|
76
|
+
})
|
|
77
|
+
: new RemoteUnavailable({
|
|
78
|
+
message: `git ${args[0]} to ${this.remote} failed: ${result.stderr}`,
|
|
79
|
+
}));
|
|
80
|
+
}, {
|
|
81
|
+
retries: REMOTE_RETRIES,
|
|
82
|
+
delayMs: REMOTE_RETRY_DELAY_MS,
|
|
83
|
+
shouldRetry: (error) => error._tag === 'RemoteUnavailable',
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
git(args, input) {
|
|
87
|
+
return execa('git', args, {
|
|
88
|
+
cwd: this.repoRoot,
|
|
89
|
+
input,
|
|
90
|
+
reject: false,
|
|
91
|
+
env: COMMITTER,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
trackingRef(runId) {
|
|
95
|
+
return `refs/remotes/${this.remote}/${runBranchFor(runId)}`;
|
|
96
|
+
}
|
|
97
|
+
async revision(ref) {
|
|
98
|
+
const parsed = await this.git(['rev-parse', '--verify', '--quiet', ref]);
|
|
99
|
+
return parsed.exitCode === 0 ? parsed.stdout.trim() : undefined;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Moves the local run branch to the remote's when it is behind, keeps it when it is ahead with
|
|
103
|
+
* commits not pushed yet, and refuses when the two have diverged.
|
|
104
|
+
*/
|
|
105
|
+
async catchUpRunBranch(runId, remoteHead) {
|
|
106
|
+
const local = await this.revision(branchRef(runId));
|
|
107
|
+
if (!local) {
|
|
108
|
+
return (await this.required(['update-ref', branchRef(runId), remoteHead, ''])).map(() => undefined);
|
|
109
|
+
}
|
|
110
|
+
if (await this.isAncestor(remoteHead, local))
|
|
111
|
+
return Result.ok();
|
|
112
|
+
if (!(await this.isAncestor(local, remoteHead))) {
|
|
113
|
+
return Result.err(new RunStateConflict({
|
|
114
|
+
message: `The run branch "${runBranchFor(runId)}" here and on ${this.remote} have diverged.`,
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
return this.fastForward(runId, local, remoteHead);
|
|
118
|
+
}
|
|
119
|
+
async required(args, input) {
|
|
120
|
+
const result = await this.git(args, input);
|
|
121
|
+
if (result.exitCode === 0)
|
|
122
|
+
return Result.ok(result.stdout.trim());
|
|
123
|
+
return Result.err(new GitCommandFailed({
|
|
124
|
+
message: `git ${args[0]} failed on the run's state: ${result.stderr}`,
|
|
125
|
+
command: `git ${args.join(' ')}`,
|
|
126
|
+
output: result.stderr,
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
async isAncestor(ancestor, descendant) {
|
|
130
|
+
const checked = await this.git(['merge-base', '--is-ancestor', ancestor, descendant]);
|
|
131
|
+
return checked.exitCode === 0;
|
|
132
|
+
}
|
|
133
|
+
/** Git refuses to move a branch a worktree has checked out, so that worktree is moved instead. */
|
|
134
|
+
async fastForward(runId, from, to) {
|
|
135
|
+
const worktree = await this.worktreeWithCheckedOut(branchRef(runId));
|
|
136
|
+
if (worktree.isErr())
|
|
137
|
+
return Result.err(worktree.error);
|
|
138
|
+
const moved = worktree.value
|
|
139
|
+
? await this.required(['-C', worktree.value, 'merge', '--ff-only', '--quiet', to])
|
|
140
|
+
: await this.required(['update-ref', branchRef(runId), to, from]);
|
|
141
|
+
return moved.map(() => undefined);
|
|
142
|
+
}
|
|
143
|
+
async worktreeWithCheckedOut(ref) {
|
|
144
|
+
const listed = await this.required(['worktree', 'list', '--porcelain']);
|
|
145
|
+
return listed.map((output) => {
|
|
146
|
+
const entry = output
|
|
147
|
+
.split('\n\n')
|
|
148
|
+
.map((block) => block.split('\n'))
|
|
149
|
+
.find((lines) => lines.includes(`branch ${ref}`));
|
|
150
|
+
return entry?.[0]?.replace(/^worktree /, '');
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/** Reads the remote's copy without creating or moving a ref here, not even FETCH_HEAD. */
|
|
154
|
+
peek(runId) {
|
|
155
|
+
return Result.gen(async function* () {
|
|
156
|
+
const listed = yield* Result.await(this.listOnRemote(runId));
|
|
157
|
+
const stateCommit = listed.get(stateRef(runId));
|
|
158
|
+
if (!stateCommit)
|
|
159
|
+
return Result.ok(undefined);
|
|
160
|
+
yield* Result.await(this.remoteCall([
|
|
161
|
+
'fetch',
|
|
162
|
+
'--quiet',
|
|
163
|
+
'--no-write-fetch-head',
|
|
164
|
+
this.remote,
|
|
165
|
+
stateRef(runId),
|
|
166
|
+
]));
|
|
167
|
+
const shown = yield* Result.await(this.required(['show', `${stateCommit}:${STATE_FILE}`]));
|
|
168
|
+
return parseState(shown, this.remote);
|
|
169
|
+
}, this);
|
|
170
|
+
}
|
|
171
|
+
save(state) {
|
|
172
|
+
return Result.gen(async function* () {
|
|
173
|
+
const commit = yield* Result.await(this.commitState(state));
|
|
174
|
+
const branch = await this.revision(branchRef(state.runId));
|
|
175
|
+
const lease = this.seenOnRemote.get(state.runId) ?? {};
|
|
176
|
+
yield* Result.await(this.remoteCall([
|
|
177
|
+
'push',
|
|
178
|
+
'--quiet',
|
|
179
|
+
'--atomic',
|
|
180
|
+
`--force-with-lease=${stateRef(state.runId)}:${lease.state ?? ''}`,
|
|
181
|
+
...(branch ? [`--force-with-lease=${branchRef(state.runId)}:${lease.branch ?? ''}`] : []),
|
|
182
|
+
this.remote,
|
|
183
|
+
`${stateRef(state.runId)}:${stateRef(state.runId)}`,
|
|
184
|
+
...(branch ? [`${branchRef(state.runId)}:${branchRef(state.runId)}`] : []),
|
|
185
|
+
]));
|
|
186
|
+
this.seenOnRemote.set(state.runId, { state: commit, branch: branch ?? lease.branch });
|
|
187
|
+
return Result.ok();
|
|
188
|
+
}, this);
|
|
189
|
+
}
|
|
190
|
+
commitState(state) {
|
|
191
|
+
return Result.gen(async function* () {
|
|
192
|
+
const ref = stateRef(state.runId);
|
|
193
|
+
const blob = yield* Result.await(this.required(['hash-object', '-w', '--stdin'], `${JSON.stringify(state, null, 2)}\n`));
|
|
194
|
+
const tree = yield* Result.await(this.required(['mktree'], `100644 blob ${blob}\t${STATE_FILE}\n`));
|
|
195
|
+
const parent = await this.revision(ref);
|
|
196
|
+
const commit = yield* Result.await(this.required([
|
|
197
|
+
'commit-tree',
|
|
198
|
+
tree,
|
|
199
|
+
...(parent ? ['-p', parent] : []),
|
|
200
|
+
'-m',
|
|
201
|
+
`jig run ${state.runId}: ${state.phase}`,
|
|
202
|
+
]));
|
|
203
|
+
yield* Result.await(this.required(['update-ref', ref, commit]));
|
|
204
|
+
return Result.ok(commit);
|
|
205
|
+
}, this);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function stateRef(runId) {
|
|
209
|
+
return `refs/jig/${runId}`;
|
|
210
|
+
}
|
|
211
|
+
const STATE_FILE = 'run.json';
|
|
212
|
+
function parseState(shown, remote) {
|
|
213
|
+
return Result.try({
|
|
214
|
+
try: () => runStateSchema.parse(JSON.parse(shown)),
|
|
215
|
+
catch: (cause) => new ConfigInvalid({
|
|
216
|
+
message: `The run's state on ${remote} is not valid: ${messageOf(cause)}`,
|
|
217
|
+
}),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
function branchRef(runId) {
|
|
221
|
+
return `refs/heads/${runBranchFor(runId)}`;
|
|
222
|
+
}
|
|
223
|
+
/** `git ls-remote --exit-code` exits with this when the remote has none of the refs asked for. */
|
|
224
|
+
const NO_MATCHING_REFS = 2;
|
|
225
|
+
/** Git reports a push whose lease no longer matches, or that is not a fast-forward, as rejected. */
|
|
226
|
+
function isLeaseRejection(stderr) {
|
|
227
|
+
return stderr.includes('[rejected]') || stderr.includes('stale info');
|
|
228
|
+
}
|
|
229
|
+
/** How many more times a fetch or push that failed on the network is tried. */
|
|
230
|
+
const REMOTE_RETRIES = 3;
|
|
231
|
+
/** The wait before the first retry; each one after it waits twice as long. */
|
|
232
|
+
const REMOTE_RETRY_DELAY_MS = 1000;
|
|
233
|
+
/** Identity for the state commits; they are jig's bookkeeping, not anyone's work. */
|
|
234
|
+
const COMMITTER = {
|
|
235
|
+
GIT_AUTHOR_NAME: 'jig',
|
|
236
|
+
GIT_AUTHOR_EMAIL: 'jig@localhost',
|
|
237
|
+
GIT_COMMITTER_NAME: 'jig',
|
|
238
|
+
GIT_COMMITTER_EMAIL: 'jig@localhost',
|
|
239
|
+
};
|