@ygrishajev/jig 1.1.0 → 1.2.0
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/README.md +16 -0
- package/dist/adapters/git/remote-ref.js +125 -0
- package/dist/adapters/git/run-code.js +66 -0
- package/dist/adapters/git/run-history.js +79 -0
- package/dist/adapters/run-store/composite-run-store.js +68 -14
- package/dist/adapters/run-store/git-remote-run-store.js +34 -129
- package/dist/app/run-jig.js +28 -7
- package/dist/app/run-stores.js +4 -3
- package/dist/cli/main.js +3 -15
- package/dist/domain/apply-outcome.js +30 -13
- package/dist/domain/diagnostics.js +104 -0
- package/dist/domain/errors.js +8 -0
- package/dist/domain/final-check.js +21 -1
- package/dist/domain/run-id.js +4 -1
- package/dist/domain/run-state.js +43 -1
- package/dist/domain/slice.js +8 -2
- package/dist/integrations/github/cli.js +20 -0
- package/dist/integrations/github/command.js +68 -0
- package/dist/integrations/github/errors.js +19 -0
- package/dist/integrations/github/findings-comment.js +53 -0
- package/dist/integrations/github/github-port.js +1 -0
- package/dist/integrations/github/implement-issue.js +281 -0
- package/dist/integrations/github/octokit-github.js +101 -0
- package/dist/integrations/github/orchestration-state.js +132 -0
- package/dist/integrations/github/orchestration-store.js +109 -0
- package/dist/integrations/github/repository-identity.js +27 -0
- package/dist/integrations/github/request-snapshot.js +3 -0
- package/dist/integrations/github/run-branch-publication.js +102 -0
- package/dist/ports/run-code.js +1 -0
- package/dist/prompts/design-review.js +2 -0
- package/dist/prompts/planning.js +5 -1
- package/dist/prompts/pull-request.js +4 -1
- package/dist/prompts/slice.js +11 -0
- package/dist/steps/pull-request-description.js +1 -1
- package/dist/steps/slice.js +1 -0
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -132,6 +132,22 @@ lands agent code without a human checkpoint. Agent commits are unsigned by defau
|
|
|
132
132
|
`"commitSigning": "repo"` re-signs the run branch during an automatic merge and requires
|
|
133
133
|
a signing setup that works without prompts. See [sandbox boundaries](docs/sandbox.md#merging-and-signing) for details.
|
|
134
134
|
|
|
135
|
+
### Implement a GitHub issue
|
|
136
|
+
|
|
137
|
+
From your repository's default branch, with `GITHUB_TOKEN` (or `GH_TOKEN`) and Git
|
|
138
|
+
push access configured:
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
jig github implement --issue 42
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
jig snapshots the request, implements it in the sandbox and opens a pull request.
|
|
145
|
+
Repeating the command resumes the same run and reuses its PR. Completed work opens
|
|
146
|
+
a ready PR; a stuck run with commits opens a draft with one PR-level comment of its
|
|
147
|
+
findings. Workflow changes are refused rather than pushed, and other unfinished runs
|
|
148
|
+
comment on the issue. You review and merge it.
|
|
149
|
+
See [setup and retry behavior](docs/commands.md#implement-a-github-issue).
|
|
150
|
+
|
|
135
151
|
## License
|
|
136
152
|
|
|
137
153
|
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { execa } from 'execa';
|
|
3
|
+
import { GitCommandFailed, RemoteUnavailable, RunStateConflict } from '../../domain/errors.js';
|
|
4
|
+
import { retryResult } from '../../domain/retry.js';
|
|
5
|
+
export class RemoteRef {
|
|
6
|
+
repoRoot;
|
|
7
|
+
remote;
|
|
8
|
+
constructor(repoRoot, remote = 'origin') {
|
|
9
|
+
this.repoRoot = repoRoot;
|
|
10
|
+
this.remote = remote;
|
|
11
|
+
}
|
|
12
|
+
async list(refs) {
|
|
13
|
+
const listed = await this.remoteCall(['ls-remote', '--exit-code', this.remote, ...refs], NO_MATCHING_REFS);
|
|
14
|
+
return listed.map((output) => {
|
|
15
|
+
const entries = output.stdout
|
|
16
|
+
.split('\n')
|
|
17
|
+
.filter(Boolean)
|
|
18
|
+
.map((line) => {
|
|
19
|
+
const [commit = '', ref = ''] = line.split('\t');
|
|
20
|
+
return [ref, commit];
|
|
21
|
+
});
|
|
22
|
+
return new Map(entries);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/** A remote operation, retried while the failure looks like the network rather than a conflict. */
|
|
26
|
+
remoteCall(args, alsoAcceptedExitCode) {
|
|
27
|
+
return retryResult(async () => {
|
|
28
|
+
const result = await this.git(args);
|
|
29
|
+
if (result.exitCode === 0 || result.exitCode === alsoAcceptedExitCode) {
|
|
30
|
+
return Result.ok(result);
|
|
31
|
+
}
|
|
32
|
+
return Result.err(isLeaseRejection(result.stderr)
|
|
33
|
+
? new RunStateConflict({
|
|
34
|
+
message: `The run's state on ${this.remote} moved since it was loaded: ${result.stderr}`,
|
|
35
|
+
})
|
|
36
|
+
: new RemoteUnavailable({
|
|
37
|
+
message: `git ${args[0]} to ${this.remote} failed: ${result.stderr}`,
|
|
38
|
+
}));
|
|
39
|
+
}, {
|
|
40
|
+
retries: REMOTE_RETRIES,
|
|
41
|
+
delayMs: REMOTE_RETRY_DELAY_MS,
|
|
42
|
+
shouldRetry: (error) => error._tag === 'RemoteUnavailable',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
git(args, input) {
|
|
46
|
+
return execa('git', args, {
|
|
47
|
+
cwd: this.repoRoot,
|
|
48
|
+
input,
|
|
49
|
+
reject: false,
|
|
50
|
+
stripFinalNewline: false,
|
|
51
|
+
env: COMMITTER,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async revision(ref) {
|
|
55
|
+
const parsed = await this.git(['rev-parse', '--verify', '--quiet', ref]);
|
|
56
|
+
return parsed.exitCode === 0 ? parsed.stdout.trim() : undefined;
|
|
57
|
+
}
|
|
58
|
+
async required(args, input, preserveWhitespace = false) {
|
|
59
|
+
const result = await this.git(args, input);
|
|
60
|
+
if (result.exitCode === 0)
|
|
61
|
+
return Result.ok(preserveWhitespace ? result.stdout : result.stdout.trim());
|
|
62
|
+
return Result.err(new GitCommandFailed({
|
|
63
|
+
message: `git ${args[0]} failed on the run's state: ${result.stderr}`,
|
|
64
|
+
command: `git ${args.join(' ')}`,
|
|
65
|
+
output: result.stderr,
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
fetch(refspecs) {
|
|
69
|
+
return this.remoteCall(['fetch', '--quiet', this.remote, ...refspecs]).then((result) => result.map(() => undefined));
|
|
70
|
+
}
|
|
71
|
+
/** Fetches objects without moving local refs, including FETCH_HEAD. */
|
|
72
|
+
peek(ref) {
|
|
73
|
+
return Result.gen(async function* () {
|
|
74
|
+
const listed = yield* Result.await(this.list([ref]));
|
|
75
|
+
const commit = listed.get(ref);
|
|
76
|
+
if (!commit)
|
|
77
|
+
return Result.ok(undefined);
|
|
78
|
+
yield* Result.await(this.remoteCall(['fetch', '--quiet', '--no-write-fetch-head', this.remote, ref]));
|
|
79
|
+
const files = yield* Result.await(this.required(['ls-tree', '-r', '--name-only', commit]));
|
|
80
|
+
const contents = new Map();
|
|
81
|
+
for (const file of files.split('\n').filter(Boolean)) {
|
|
82
|
+
const shown = yield* Result.await(this.required(['show', `${commit}:${file}`], undefined, true));
|
|
83
|
+
contents.set(file, shown);
|
|
84
|
+
}
|
|
85
|
+
return Result.ok(contents);
|
|
86
|
+
}, this);
|
|
87
|
+
}
|
|
88
|
+
pushLeased(updates) {
|
|
89
|
+
return this.remoteCall([
|
|
90
|
+
'push',
|
|
91
|
+
'--quiet',
|
|
92
|
+
'--atomic',
|
|
93
|
+
...updates.map(({ ref, expected }) => `--force-with-lease=${ref}:${expected ?? ''}`),
|
|
94
|
+
this.remote,
|
|
95
|
+
...updates.map(({ ref, source }) => `${source}:${ref}`),
|
|
96
|
+
]).then((result) => result.map(() => undefined));
|
|
97
|
+
}
|
|
98
|
+
commitFile(ref, file, contents, message) {
|
|
99
|
+
return Result.gen(async function* () {
|
|
100
|
+
const blob = yield* Result.await(this.required(['hash-object', '-w', '--stdin'], contents));
|
|
101
|
+
const tree = yield* Result.await(this.required(['mktree'], `100644 blob ${blob}\t${file}\n`));
|
|
102
|
+
const parent = await this.revision(ref);
|
|
103
|
+
const commit = yield* Result.await(this.required(['commit-tree', tree, ...(parent ? ['-p', parent] : []), '-m', message]));
|
|
104
|
+
yield* Result.await(this.required(['update-ref', ref, commit]));
|
|
105
|
+
return Result.ok(commit);
|
|
106
|
+
}, this);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/** `git ls-remote --exit-code` exits with this when the remote has none of the refs asked for. */
|
|
110
|
+
const NO_MATCHING_REFS = 2;
|
|
111
|
+
/** Git reports a push whose lease no longer matches, or that is not a fast-forward, as rejected. */
|
|
112
|
+
function isLeaseRejection(stderr) {
|
|
113
|
+
return stderr.includes('[rejected]') || stderr.includes('stale info');
|
|
114
|
+
}
|
|
115
|
+
/** How many more times a fetch or push that failed on the network is tried. */
|
|
116
|
+
const REMOTE_RETRIES = 3;
|
|
117
|
+
/** The wait before the first retry; each one after it waits twice as long. */
|
|
118
|
+
const REMOTE_RETRY_DELAY_MS = 1000;
|
|
119
|
+
/** Identity for the state commits; they are jig's bookkeeping, not anyone's work. */
|
|
120
|
+
const COMMITTER = {
|
|
121
|
+
GIT_AUTHOR_NAME: 'jig',
|
|
122
|
+
GIT_AUTHOR_EMAIL: 'jig@localhost',
|
|
123
|
+
GIT_COMMITTER_NAME: 'jig',
|
|
124
|
+
GIT_COMMITTER_EMAIL: 'jig@localhost',
|
|
125
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { RunCodeUnavailable, RunStateConflict, } from '../../domain/errors.js';
|
|
3
|
+
import { runBranchFor } from '../../domain/run-state.js';
|
|
4
|
+
import { RemoteRef } from './remote-ref.js';
|
|
5
|
+
import { isAncestor } from './repository.js';
|
|
6
|
+
import { exactHead, fastForwardWorktree, hasCommit, worktreeHolding } from './run-history.js';
|
|
7
|
+
/** Restores a deferred run's code checkpoint from this clone, or from the branch already published. */
|
|
8
|
+
export class GitRunCode {
|
|
9
|
+
repoRoot;
|
|
10
|
+
remote;
|
|
11
|
+
refs;
|
|
12
|
+
constructor(repoRoot, remote = 'origin') {
|
|
13
|
+
this.repoRoot = repoRoot;
|
|
14
|
+
this.remote = remote;
|
|
15
|
+
this.refs = new RemoteRef(repoRoot, remote);
|
|
16
|
+
}
|
|
17
|
+
checkpoint(runId) {
|
|
18
|
+
return exactHead(this.repoRoot, branchRef(runId));
|
|
19
|
+
}
|
|
20
|
+
restore(runId, revision) {
|
|
21
|
+
return Result.gen(async function* () {
|
|
22
|
+
const local = yield* Result.await(exactHead(this.repoRoot, branchRef(runId)));
|
|
23
|
+
if (local === revision)
|
|
24
|
+
return Result.ok();
|
|
25
|
+
yield* Result.await(this.ensureAvailable(runId, revision));
|
|
26
|
+
if (!local)
|
|
27
|
+
return this.moveBranch(runId, revision, '');
|
|
28
|
+
if (yield* Result.await(isAncestor(this.repoRoot, revision, local)))
|
|
29
|
+
return Result.ok();
|
|
30
|
+
if (yield* Result.await(isAncestor(this.repoRoot, local, revision)))
|
|
31
|
+
return this.moveBranch(runId, revision, local);
|
|
32
|
+
return Result.err(new RunStateConflict({
|
|
33
|
+
message: `The run branch "${runBranchFor(runId)}" here has diverged from the saved checkpoint ${revision}.`,
|
|
34
|
+
}));
|
|
35
|
+
}, this);
|
|
36
|
+
}
|
|
37
|
+
ensureAvailable(runId, revision) {
|
|
38
|
+
return Result.gen(async function* () {
|
|
39
|
+
if (yield* Result.await(hasCommit(this.repoRoot, revision)))
|
|
40
|
+
return Result.ok();
|
|
41
|
+
const listed = yield* Result.await(this.refs.list([branchRef(runId)]));
|
|
42
|
+
if (listed.has(branchRef(runId)))
|
|
43
|
+
yield* Result.await(this.refs.fetch([
|
|
44
|
+
`+${branchRef(runId)}:refs/remotes/${this.remote}/${runBranchFor(runId)}`,
|
|
45
|
+
]));
|
|
46
|
+
if (yield* Result.await(hasCommit(this.repoRoot, revision)))
|
|
47
|
+
return Result.ok();
|
|
48
|
+
return Result.err(new RunCodeUnavailable({
|
|
49
|
+
message: `Run ${runId} saved its code at ${revision}, which is neither in this clone nor published on ${this.remote}. Resume it from the clone that ran it, or start a new run.`,
|
|
50
|
+
}));
|
|
51
|
+
}, this);
|
|
52
|
+
}
|
|
53
|
+
/** A branch checked out somewhere moves with its worktree, so its index and files follow it. */
|
|
54
|
+
moveBranch(runId, revision, expected) {
|
|
55
|
+
return Result.gen(async function* () {
|
|
56
|
+
const worktree = yield* Result.await(worktreeHolding(this.repoRoot, branchRef(runId)));
|
|
57
|
+
if (worktree)
|
|
58
|
+
return fastForwardWorktree(worktree, revision);
|
|
59
|
+
const moved = await this.refs.required(['update-ref', branchRef(runId), revision, expected]);
|
|
60
|
+
return moved.map(() => undefined);
|
|
61
|
+
}, this);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function branchRef(runId) {
|
|
65
|
+
return `refs/heads/${runBranchFor(runId)}`;
|
|
66
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Result } from 'better-result';
|
|
2
|
+
import { execa } from 'execa';
|
|
3
|
+
import { GitCommandFailed, messageOf } from '../../domain/errors.js';
|
|
4
|
+
/** The commit `ref` names exactly, or `undefined` when there is no such ref or commit here. */
|
|
5
|
+
export function exactHead(repoRoot, ref) {
|
|
6
|
+
return gitOutput(repoRoot, ['rev-parse', '--verify', '--quiet', '--end-of-options', `${ref}^{commit}`], [1]).then((result) => result.map(({ stdout, exitCode }) => (exitCode === 0 ? stdout.trim() : undefined)));
|
|
7
|
+
}
|
|
8
|
+
/** Runs git, accepting exit 0 and `alsoAccepted`; any other exit is the command's failure. */
|
|
9
|
+
async function gitOutput(repoRoot, args, alsoAccepted = []) {
|
|
10
|
+
const command = `git ${args.join(' ')}`;
|
|
11
|
+
const failure = (output) => new GitCommandFailed({ message: `${command} failed: ${output}`, command, output });
|
|
12
|
+
const ran = await Result.tryPromise({
|
|
13
|
+
try: () => execa('git', args, { cwd: repoRoot, reject: false, stripFinalNewline: false }),
|
|
14
|
+
catch: (cause) => failure(messageOf(cause)),
|
|
15
|
+
});
|
|
16
|
+
if (ran.isErr())
|
|
17
|
+
return Result.err(ran.error);
|
|
18
|
+
const { stdout, stderr, exitCode = -1 } = ran.value;
|
|
19
|
+
if (exitCode === 0 || alsoAccepted.includes(exitCode))
|
|
20
|
+
return Result.ok({ stdout, exitCode });
|
|
21
|
+
return Result.err(failure(stderr));
|
|
22
|
+
}
|
|
23
|
+
/** Whether this repository holds `commit` as an object, whether or not a ref reaches it. */
|
|
24
|
+
export function hasCommit(repoRoot, commit) {
|
|
25
|
+
return gitOutput(repoRoot, ['cat-file', '-e', '--end-of-options', commit], [1]).then((result) => result.map(({ exitCode }) => exitCode === 0));
|
|
26
|
+
}
|
|
27
|
+
/** The commits `head` has that `start` does not, newest first. */
|
|
28
|
+
export function commitsBeyond(repoRoot, start, head) {
|
|
29
|
+
return gitOutput(repoRoot, ['rev-list', '--end-of-options', `${start}..${head}`]).then((result) => result.map(({ stdout }) => stdout.split('\n').filter(Boolean)));
|
|
30
|
+
}
|
|
31
|
+
/** Where `head` forked from `defaultHead`, or `undefined` when they share no history. */
|
|
32
|
+
export function mergeBaseWith(repoRoot, defaultHead, head) {
|
|
33
|
+
return gitOutput(repoRoot, ['merge-base', '--end-of-options', defaultHead, head], [1]).then((result) => result.map(({ stdout, exitCode }) => (exitCode === 0 ? stdout.trim() : undefined)));
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Every path whose content or type differs between `from` and `to`. Rename detection is off, so a
|
|
37
|
+
* rename names both its old and its new path, and NUL-delimited output keeps unusual names intact.
|
|
38
|
+
*/
|
|
39
|
+
export function changedPaths(repoRoot, from, to) {
|
|
40
|
+
return gitOutput(repoRoot, [
|
|
41
|
+
'diff',
|
|
42
|
+
'--no-renames',
|
|
43
|
+
'--no-ext-diff',
|
|
44
|
+
'--no-textconv',
|
|
45
|
+
'--name-only',
|
|
46
|
+
'-z',
|
|
47
|
+
'--end-of-options',
|
|
48
|
+
from,
|
|
49
|
+
to,
|
|
50
|
+
'--',
|
|
51
|
+
]).then((result) => result.map(({ stdout }) => stdout.split('\0').filter(Boolean)));
|
|
52
|
+
}
|
|
53
|
+
/** The worktree that has `branchRef` checked out, or `undefined` when none does. */
|
|
54
|
+
export function worktreeHolding(repoRoot, branchRef) {
|
|
55
|
+
return gitOutput(repoRoot, ['worktree', 'list', '--porcelain', '-z']).then((result) => result.map(({ stdout }) => {
|
|
56
|
+
const holding = stdout
|
|
57
|
+
.split('\0\0')
|
|
58
|
+
.map((record) => record.split('\0'))
|
|
59
|
+
.find((fields) => fields.includes(`branch ${branchRef}`));
|
|
60
|
+
return holding?.find((field) => field.startsWith('worktree '))?.slice('worktree '.length);
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Fast-forwards the branch checked out in `worktree` to `revision`, moving its index and files with
|
|
65
|
+
* it. Uncommitted work git can carry along stays; work the move would overwrite makes git refuse.
|
|
66
|
+
* Hooks are off: the commits may hold code nobody on this host has read yet.
|
|
67
|
+
*/
|
|
68
|
+
export function fastForwardWorktree(worktree, revision) {
|
|
69
|
+
return gitOutput(worktree, [
|
|
70
|
+
'-c',
|
|
71
|
+
'core.hooksPath=/dev/null',
|
|
72
|
+
'merge',
|
|
73
|
+
'--ff-only',
|
|
74
|
+
'--no-stat',
|
|
75
|
+
'--quiet',
|
|
76
|
+
'--end-of-options',
|
|
77
|
+
revision,
|
|
78
|
+
]).then((result) => result.map(() => undefined));
|
|
79
|
+
}
|
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
import { Result } from 'better-result';
|
|
2
|
+
import { RunCodeUnavailable, RunStateConflict, } from '../../domain/errors.js';
|
|
3
|
+
import { canRecoverInitialCheckpoint, defersCodePublication, } from '../../domain/run-state.js';
|
|
2
4
|
/**
|
|
3
5
|
* 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;
|
|
5
|
-
*
|
|
6
|
-
*
|
|
6
|
+
* copy with the highest revision; copies tied at that revision must agree on how and where the
|
|
7
|
+
* run's code is published, so the store order never decides which code a run resumes with. A load
|
|
8
|
+
* also lets a remote store learn what it must not overwrite. A save stamps the next revision past any this store has seen, so one
|
|
9
|
+
* process never writes two different states under the same revision. A run that defers its code
|
|
10
|
+
* publication records the run branch's commit at each save, and a load restores that checkpoint
|
|
11
|
+
* before handing the run back, so the state never describes code this clone does not hold.
|
|
7
12
|
*/
|
|
8
13
|
export class CompositeRunStore {
|
|
9
14
|
stores;
|
|
15
|
+
code;
|
|
10
16
|
latestRevisions = new Map();
|
|
11
|
-
constructor(stores) {
|
|
17
|
+
constructor(stores, code) {
|
|
12
18
|
this.stores = stores;
|
|
19
|
+
this.code = code;
|
|
13
20
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
load(runId) {
|
|
22
|
+
return Result.gen(async function* () {
|
|
23
|
+
const copies = yield* Result.await(this.readEach((store) => store.load(runId)));
|
|
24
|
+
const freshest = yield* consistentFreshestOf(copies);
|
|
25
|
+
if (!freshest)
|
|
26
|
+
return Result.ok(undefined);
|
|
20
27
|
this.latestRevisions.set(runId, freshest.revision);
|
|
21
|
-
|
|
28
|
+
return this.restoreCheckpoint(freshest);
|
|
29
|
+
}, this);
|
|
22
30
|
}
|
|
23
31
|
async readEach(read) {
|
|
24
32
|
const copies = [];
|
|
@@ -30,14 +38,26 @@ export class CompositeRunStore {
|
|
|
30
38
|
}
|
|
31
39
|
return Result.ok(copies);
|
|
32
40
|
}
|
|
41
|
+
restoreCheckpoint(state) {
|
|
42
|
+
return Result.gen(async function* () {
|
|
43
|
+
if (!defersCodePublication(state))
|
|
44
|
+
return Result.ok(state);
|
|
45
|
+
const checkpointed = yield* recordedCheckpoint(state);
|
|
46
|
+
yield* Result.await(this.code.restore(state.runId, checkpointed.codeRevision));
|
|
47
|
+
return Result.ok(checkpointed);
|
|
48
|
+
}, this);
|
|
49
|
+
}
|
|
33
50
|
async peek(runId) {
|
|
34
51
|
const copies = await this.readEach((store) => store.peek(runId));
|
|
35
|
-
return copies.
|
|
52
|
+
return copies.andThen(consistentFreshestOf);
|
|
36
53
|
}
|
|
37
54
|
async save(state) {
|
|
55
|
+
const checkpointed = await this.withCodeCheckpoint(state);
|
|
56
|
+
if (checkpointed.isErr())
|
|
57
|
+
return Result.err(checkpointed.error);
|
|
38
58
|
const revision = Math.max(state.revision, this.latestRevisions.get(state.runId) ?? 0) + 1;
|
|
39
59
|
this.latestRevisions.set(state.runId, revision);
|
|
40
|
-
const stamped = { ...
|
|
60
|
+
const stamped = { ...checkpointed.value, revision };
|
|
41
61
|
for (const store of this.stores) {
|
|
42
62
|
const saved = await store.save(stamped);
|
|
43
63
|
if (saved.isErr())
|
|
@@ -45,8 +65,26 @@ export class CompositeRunStore {
|
|
|
45
65
|
}
|
|
46
66
|
return Result.ok();
|
|
47
67
|
}
|
|
68
|
+
async withCodeCheckpoint(state) {
|
|
69
|
+
if (!defersCodePublication(state))
|
|
70
|
+
return Result.ok(state);
|
|
71
|
+
const checkpoint = await this.code.checkpoint(state.runId);
|
|
72
|
+
return checkpoint.andThen((codeRevision) => recordedCheckpoint(codeRevision ? { ...state, codeRevision } : state));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The copy with the highest revision, listed first on a tie, once every tied copy agrees on its code
|
|
77
|
+
* publication policy and checkpoint; disagreeing copies would let store order pick the run's code.
|
|
78
|
+
*/
|
|
79
|
+
function consistentFreshestOf(copies) {
|
|
80
|
+
const freshest = freshestOf(copies);
|
|
81
|
+
const disagreeing = copies.find((copy) => copy && freshest && copy.revision === freshest.revision && !sameCode(copy, freshest));
|
|
82
|
+
if (!freshest || !disagreeing)
|
|
83
|
+
return Result.ok(freshest);
|
|
84
|
+
return Result.err(new RunStateConflict({
|
|
85
|
+
message: `Run ${freshest.runId} has two copies at revision ${freshest.revision} that disagree on its code (${codeOf(freshest)}; ${codeOf(disagreeing)}).`,
|
|
86
|
+
}));
|
|
48
87
|
}
|
|
49
|
-
/** The copy with the highest revision; on a tie, the one listed first. */
|
|
50
88
|
function freshestOf(copies) {
|
|
51
89
|
let freshest;
|
|
52
90
|
for (const copy of copies) {
|
|
@@ -55,3 +93,19 @@ function freshestOf(copies) {
|
|
|
55
93
|
}
|
|
56
94
|
return freshest;
|
|
57
95
|
}
|
|
96
|
+
function sameCode(first, second) {
|
|
97
|
+
return (first.codePublication === second.codePublication &&
|
|
98
|
+
first.startingCommit === second.startingCommit &&
|
|
99
|
+
first.codeRevision === second.codeRevision);
|
|
100
|
+
}
|
|
101
|
+
function codeOf({ codePublication, codeRevision }) {
|
|
102
|
+
return `${codePublication ?? 'published with state'}, checkpoint ${codeRevision ?? 'none'}`;
|
|
103
|
+
}
|
|
104
|
+
function recordedCheckpoint(state) {
|
|
105
|
+
const codeRevision = state.codeRevision ?? (canRecoverInitialCheckpoint(state) ? state.startingCommit : undefined);
|
|
106
|
+
if (codeRevision)
|
|
107
|
+
return Result.ok({ ...state, codeRevision });
|
|
108
|
+
return Result.err(new RunCodeUnavailable({
|
|
109
|
+
message: `Run ${state.runId} has no trustworthy code checkpoint. Resume from a copy with a recorded codeRevision, or start a new run; the current default branch cannot recover this run's code.`,
|
|
110
|
+
}));
|
|
111
|
+
}
|