@williacj/rigger 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +370 -2
- package/package.json +56 -4
- package/scripts/doc-reference-check.mjs +59 -0
- package/scripts/gate-check.mjs +249 -0
- package/scripts/lib/pack-result.mjs +9 -0
- package/scripts/record-commit.mjs +67 -0
- package/src/cli/doctor.mjs +1021 -0
- package/src/cli/init.mjs +384 -0
- package/src/cli/once.mjs +489 -0
- package/src/cli/pause.mjs +56 -0
- package/src/cli/plan.mjs +106 -0
- package/src/cli/recording.mjs +52 -0
- package/src/cli/report.mjs +242 -0
- package/src/cli/resume.mjs +96 -0
- package/src/cli/rigger.mjs +12 -0
- package/src/cli/run.mjs +10 -0
- package/src/cli/setup-board.mjs +108 -0
- package/src/cli/verbs.mjs +82 -0
- package/src/cli/verdict.mjs +180 -0
- package/src/config/validate.mjs +960 -0
- package/src/execution/groups.mjs +185 -0
- package/src/execution/kept-output.mjs +21 -0
- package/src/execution/role.mjs +171 -0
- package/src/execution/run.mjs +490 -0
- package/src/execution/step.mjs +26 -0
- package/src/execution/workspace.mjs +866 -0
- package/src/observation/push.mjs +103 -0
- package/src/observation/report.mjs +415 -0
- package/src/observation/sink.mjs +271 -0
- package/src/package-commit.json +1 -0
- package/src/scheduling/admission.mjs +390 -0
- package/src/scheduling/clock.mjs +98 -0
- package/src/scheduling/lane.mjs +160 -0
- package/src/scheduling/loop.mjs +1738 -0
- package/src/scheduling/pull-order.mjs +46 -0
- package/src/substrate/data-ref.mjs +147 -0
- package/src/substrate/forge/assets.mjs +5 -0
- package/src/substrate/forge/graphql.mjs +258 -0
- package/src/substrate/forge/item-write.mjs +74 -0
- package/src/substrate/forge/read.mjs +998 -0
- package/src/substrate/forge/repository-write.mjs +152 -0
- package/src/substrate/forge/runners.mjs +510 -0
- package/src/substrate/forge/schema-write.mjs +70 -0
- package/src/substrate/forge/write-made.mjs +40 -0
- package/src/substrate/git-environment.mjs +54 -0
- package/src/substrate/host.mjs +23 -0
- package/src/substrate/launchd.mjs +23 -0
- package/src/substrate/process-table.sh +48 -0
- package/src/substrate/process.mjs +2474 -0
- package/src/substrate/providers/adapters.mjs +8 -0
- package/src/substrate/providers/claude.mjs +427 -0
- package/src/substrate/providers/codex.mjs +349 -0
- package/src/substrate/standard-error.mjs +134 -0
- package/src/substrate/worktrees.mjs +517 -0
- package/src/workflow/clock-mark.mjs +40 -0
- package/src/workflow/escalation.mjs +110 -0
- package/src/workflow/facts.mjs +92 -0
- package/src/workflow/first-above.mjs +17 -0
- package/src/workflow/form-check.mjs +88 -0
- package/src/workflow/gate.mjs +62 -0
- package/src/workflow/judges.mjs +695 -0
- package/src/workflow/marker.mjs +265 -0
- package/src/workflow/next-action.mjs +456 -0
- package/src/workflow/status-issue.mjs +268 -0
- package/src/workflow/transitions.mjs +588 -0
- package/templates/claude/agents/architect.md +116 -0
- package/templates/claude/agents/engineer.md +57 -0
- package/templates/claude/agents/pm.md +114 -0
- package/templates/claude/agents/reviewer.md +37 -0
- package/templates/claude/agents/spike-engineer.md +80 -0
- package/templates/claude/hooks/refuse-reserved-git-commands.mjs +909 -0
- package/templates/claude/settings.json +50 -0
- package/templates/claude/skills/acceptance/SKILL.md +275 -0
- package/templates/claude/skills/agent-style/SKILL.md +78 -0
- package/templates/claude/skills/code-review/SKILL.md +242 -0
- package/templates/claude/skills/proposal/SKILL.md +175 -0
- package/templates/claude/skills/spec-style/SKILL.md +112 -0
- package/templates/claude/skills/tdd/SKILL.md +230 -0
- package/templates/codex/config.toml +19 -0
- package/templates/doc-reference-check.md +25 -0
- package/templates/githooks/pre-push +24 -0
- package/templates/github/workflows/rigger-gate.yml +209 -0
- package/templates/launchd/com.williacj.rigger.plist +31 -0
- package/templates/rigger.config.mjs +78 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// ABOUTME: L1's record of the process group each running dispatch holds, one file in the state
|
|
2
|
+
// directory, including signs a settled dispatch leaves for the next start to resolve.
|
|
3
|
+
|
|
4
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { atExit } from '../substrate/process.mjs';
|
|
6
|
+
import { writeWhole } from '../substrate/standard-error.mjs';
|
|
7
|
+
import { processSigns } from '../substrate/process.mjs';
|
|
8
|
+
import { isAbsolute, join } from 'node:path';
|
|
9
|
+
|
|
10
|
+
const RECORD = 'groups.json';
|
|
11
|
+
const written = new Map();
|
|
12
|
+
const failedWrites = new Map();
|
|
13
|
+
|
|
14
|
+
/** The record's file, inside the state directory the caller names. */
|
|
15
|
+
export const recordPath = (directory) => join(directory, RECORD);
|
|
16
|
+
|
|
17
|
+
/** The file a write of the record in `directory` fills before it renames it over the record. */
|
|
18
|
+
export const partialPath = (directory) => `${recordPath(directory)}.partial`;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Every entry the record in `directory` holds, or none where there is no record. A record whose
|
|
22
|
+
* content is not a list of entries fails whole, naming its file, so nothing acts on part of it.
|
|
23
|
+
*/
|
|
24
|
+
export const readGroups = (directory) => readEntries(recordPath(directory));
|
|
25
|
+
|
|
26
|
+
/** Reads an unfinished write without removing or replacing it. */
|
|
27
|
+
export const readPartial = (directory) => readEntries(partialPath(directory));
|
|
28
|
+
|
|
29
|
+
/** Validates a whole record before any entry reaches a caller. */
|
|
30
|
+
function readEntries(path) {
|
|
31
|
+
let text;
|
|
32
|
+
try {
|
|
33
|
+
text = readFileSync(path, 'utf8');
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (error.code === 'ENOENT') return [];
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
let entries;
|
|
39
|
+
try {
|
|
40
|
+
entries = JSON.parse(text);
|
|
41
|
+
} catch (cause) {
|
|
42
|
+
throw new Error(`L1's record of process groups ${path} cannot be read as entries: ${cause.message}`, { cause });
|
|
43
|
+
}
|
|
44
|
+
const unreadable = Array.isArray(entries) ? entries.find((entry) => !isEntry(entry)) : entries;
|
|
45
|
+
if (!Array.isArray(entries) || unreadable !== undefined) {
|
|
46
|
+
throw new Error(`L1's record of process groups ${path} cannot be read as entries: it holds ${JSON.stringify(unreadable)}`);
|
|
47
|
+
}
|
|
48
|
+
return entries;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether `entry` names a group a dispatch can hold, the start of its leader, the dispatch's
|
|
53
|
+
* directory by an absolute path where it has one, with its device and inode as decimal strings
|
|
54
|
+
* where it has them, its scratch directory as `scratch`, an absolute `path` with its device and
|
|
55
|
+
* inode likewise, where it has one, its tracked processes as `tracked` where it has any, and a
|
|
56
|
+
* dispatch the record can hold. No group
|
|
57
|
+
* Rigger creates has an id of 1 or less, and L0 signals a group by its id negated, so 1, 0 or a
|
|
58
|
+
* negative id would reach launchd, the caller's own group, or every process.
|
|
59
|
+
*/
|
|
60
|
+
const isEntry = (entry) => typeof entry === 'object' && entry !== null
|
|
61
|
+
&& Number.isSafeInteger(entry.group) && entry.group > 1
|
|
62
|
+
&& Number.isSafeInteger(entry.started)
|
|
63
|
+
&& (entry.workspace === undefined || (typeof entry.workspace === 'string' && isAbsolute(entry.workspace)))
|
|
64
|
+
&& [entry.device, entry.inode].every(isDecimal)
|
|
65
|
+
&& (entry.scratch === undefined || (typeof entry.scratch === 'object' && entry.scratch !== null && typeof entry.scratch.path === 'string' && isAbsolute(entry.scratch.path) && [entry.scratch.device, entry.scratch.inode].every(isDecimal)))
|
|
66
|
+
&& (entry.tracked === undefined || (Array.isArray(entry.tracked) && entry.tracked.every(isTracked)))
|
|
67
|
+
&& (entry.settled === undefined || typeof entry.settled === 'boolean')
|
|
68
|
+
&& (entry.unended === undefined || (Array.isArray(entry.unended) && entry.unended.every(isUnended)))
|
|
69
|
+
&& (entry.unread === undefined || (Array.isArray(entry.unread) && entry.unread.every(isUnread)))
|
|
70
|
+
&& holdsDispatch(entry.dispatch, entry.card);
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Whether `each` names a tracked process by a pid L0 may signal and its start time. A pid of 1 or
|
|
74
|
+
* less would reach launchd, the caller's own group, or every process, as a group's id would.
|
|
75
|
+
*/
|
|
76
|
+
const isTracked = (each) => typeof each === 'object' && each !== null && Number.isSafeInteger(each.pid) && each.pid > 1 && Number.isSafeInteger(each.started);
|
|
77
|
+
|
|
78
|
+
/** Whether `field` is absent or a decimal string, as a device or an inode is recorded. */
|
|
79
|
+
const isDecimal = (field) => field === undefined || (typeof field === 'string' && /^\d+$/.test(field));
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Whether the record can hold dispatch `id` of `card`: an id is a string that is not empty, and a
|
|
83
|
+
* card is an issue number, where there is one. L1 asks before it starts a dispatch, because an
|
|
84
|
+
* entry the record would refuse on its next read would refuse every later dispatch and start.
|
|
85
|
+
*/
|
|
86
|
+
export const holdsDispatch = (id, card) => typeof id === 'string' && id !== ''
|
|
87
|
+
&& (card === undefined || (Number.isSafeInteger(card) && card > 0));
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Replaces the record in `directory` with `entries`.
|
|
91
|
+
*
|
|
92
|
+
* The whole record is written beside it and renamed over it, so a reader finds it as it was
|
|
93
|
+
* before the write or as it is after, never part of one: a rename within one directory replaces
|
|
94
|
+
* the name in one step (POSIX `rename`). A writer stopped before the rename leaves the partial
|
|
95
|
+
* file beside the record. A start reads it before reaping and replaces it with its kept entries. Nothing
|
|
96
|
+
* is flushed to the disk, as in L5's stream: what this buys is surviving the engine's death, not
|
|
97
|
+
* the machine's.
|
|
98
|
+
*/
|
|
99
|
+
export function writeGroups(directory, entries, options = {}) {
|
|
100
|
+
try {
|
|
101
|
+
mkdirSync(directory, { recursive: true });
|
|
102
|
+
const partial = partialPath(directory);
|
|
103
|
+
const text = JSON.stringify(entries);
|
|
104
|
+
writeFileSync(partial, text);
|
|
105
|
+
renameSync(partial, recordPath(directory));
|
|
106
|
+
written.set(directory, JSON.parse(text));
|
|
107
|
+
} catch (cause) {
|
|
108
|
+
const before = new Set((written.get(directory) ?? []).filter(isEntry).flatMap(processesOf).map(keyOf));
|
|
109
|
+
const failures = failedWrites.get(directory) ?? [];
|
|
110
|
+
for (const entry of entries.filter(isEntry)) {
|
|
111
|
+
const processes = processesOf(entry).filter((each) => !before.has(keyOf(each)));
|
|
112
|
+
if (processes.length > 0) failures.push({ entry, processes, reason: cause.message, options });
|
|
113
|
+
}
|
|
114
|
+
if (failures.length > 0) failedWrites.set(directory, failures);
|
|
115
|
+
throw cause;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The identities a record names, including its group leader. */
|
|
120
|
+
const processesOf = (entry) => [{ pid: entry.group, started: entry.started }, ...(entry.tracked ?? []), ...(entry.unended ?? [])];
|
|
121
|
+
const keyOf = ({ pid, started }) => `${pid}:${started}`;
|
|
122
|
+
|
|
123
|
+
/** A failed write remains a sign until L0's successful whole-table read shows its omitted processes gone. */
|
|
124
|
+
export function failedWriteSigns(directory, options = {}) {
|
|
125
|
+
const failures = failedWrites.get(directory) ?? [];
|
|
126
|
+
for (const failure of failures) failure.processes = processSigns(failure.processes, { ...failure.options, ...options }).unended;
|
|
127
|
+
const standing = failures.filter(({ processes }) => processes.length > 0);
|
|
128
|
+
if (standing.length === 0) failedWrites.delete(directory);
|
|
129
|
+
else failedWrites.set(directory, standing);
|
|
130
|
+
return standing.map(({ reason }) => ({ kind: 'write', path: recordPath(directory), reason }));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Adds `entry` to the record in `directory`. */
|
|
134
|
+
export const addGroup = (directory, entry) => writeGroups(directory, [...readGroups(directory), entry]);
|
|
135
|
+
|
|
136
|
+
/** Names the tracked processes `added` in the entry for `group` in the record in `directory`, after those it names. */
|
|
137
|
+
export const addTracked = (directory, group, added, options) => writeGroups(directory, readGroups(directory).map((entry) => (entry.group === group ? { ...entry, tracked: [...(entry.tracked ?? []), ...added] } : entry)), options);
|
|
138
|
+
|
|
139
|
+
/** Signs held by entries, in L3's reporting vocabulary. */
|
|
140
|
+
export const entrySigns = (entries) => entries.flatMap((entry) => [
|
|
141
|
+
...(entry.unended ?? []).map(({ pid, name, cmd }) => ({ kind: 'unended', pid, name, cmd })),
|
|
142
|
+
...(entry.unread ?? []).map(({ kind, reason, ...identity }) => ({ kind: 'read', reason, ...(kind === 'dispatch' ? { dispatch: entry.dispatch } : kind === 'group' ? { group: identity.group } : { directory: identity.directory }) })),
|
|
143
|
+
]);
|
|
144
|
+
|
|
145
|
+
/** The refusal whose command a sign kept from starting. */
|
|
146
|
+
export const SIGN_STOPPED = 'SIGN_STOPPED';
|
|
147
|
+
|
|
148
|
+
/** A refusal distinct from an environment failure; L3 counts no attempt for it. */
|
|
149
|
+
export const signFailure = (signs) => Object.assign(new Error(`signs of an earlier dispatch's process: ${JSON.stringify(signs)}`), { code: SIGN_STOPPED, signs });
|
|
150
|
+
|
|
151
|
+
/** Keeps a dispatch's signs after its containment settles. */
|
|
152
|
+
export function settleGroup(directory, group, signs = {}) {
|
|
153
|
+
const entries = readGroups(directory).flatMap((entry) => {
|
|
154
|
+
if (entry.group !== group) return [entry];
|
|
155
|
+
const unended = [...(entry.unended ?? []), ...(signs.unended ?? [])];
|
|
156
|
+
const unread = [...(entry.unread ?? []), ...(signs.unread ?? [])];
|
|
157
|
+
return unended.length + unread.length === 0 ? [] : [{ ...entry, settled: true, ...(unended.length > 0 ? { unended } : {}), ...(unread.length > 0 ? { unread } : {}) }];
|
|
158
|
+
});
|
|
159
|
+
writeGroups(directory, entries);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The normal exit makes omitted processes durable, without marking budget-limited cleanup settled. */
|
|
163
|
+
atExit(() => {
|
|
164
|
+
for (const directory of failedWrites.keys()) {
|
|
165
|
+
const signs = failedWriteSigns(directory);
|
|
166
|
+
if (signs.length === 0) continue;
|
|
167
|
+
try {
|
|
168
|
+
const entries = readGroups(directory);
|
|
169
|
+
for (const { entry, processes } of failedWrites.get(directory)) {
|
|
170
|
+
let kept = entries.find(({ group }) => group === entry.group);
|
|
171
|
+
if (kept === undefined) { kept = { ...entry }; entries.push(kept); }
|
|
172
|
+
kept.unended = [...new Map([...(kept.unended ?? []), ...processes].map((each) => [keyOf(each), each])).values()];
|
|
173
|
+
}
|
|
174
|
+
writeGroups(directory, entries);
|
|
175
|
+
} catch (cause) { signs.push({ kind: 'write', path: recordPath(directory), reason: cause.message }); }
|
|
176
|
+
writeWhole(`${signFailure(signs).message}\n`);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
/** Sign identities may omit a start time only where no read gave one. */
|
|
181
|
+
const isUnended = (each) => each !== null && typeof each === 'object' && Number.isSafeInteger(each.pid) && each.pid > 1
|
|
182
|
+
&& (each.started === undefined || Number.isSafeInteger(each.started)) && [each.name, each.cmd].every((field) => field === undefined || typeof field === 'string');
|
|
183
|
+
const isUnread = (each) => each !== null && typeof each === 'object' && typeof each.reason === 'string'
|
|
184
|
+
&& (each.kind === 'dispatch' || (each.kind === 'group' && Number.isSafeInteger(each.group) && each.group > 1)
|
|
185
|
+
|| (each.kind === 'directory' && typeof each.directory === 'string' && isAbsolute(each.directory)));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// ABOUTME: L1 keeps each role dispatch's output bytes locally, separately from the event stream.
|
|
2
|
+
|
|
3
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
/** Keeps both streams exactly as L1 returned them, under the dispatch's id. */
|
|
7
|
+
export function keepOutput({ directory, id, card, sink, result }) {
|
|
8
|
+
for (const stream of ['stdout', 'stderr']) {
|
|
9
|
+
const path = join(directory, 'output', id, stream);
|
|
10
|
+
try {
|
|
11
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
12
|
+
writeFileSync(path, result[stream], { mode: 0o600 });
|
|
13
|
+
} catch (cause) {
|
|
14
|
+
try {
|
|
15
|
+
sink.emitter({ layer: 'L1', card, dispatch: id }).emit('output.failed', { path, reason: cause.message });
|
|
16
|
+
} catch {
|
|
17
|
+
// An unavailable sink cannot record this failure either; keeping never changes the dispatch's outcome.
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// ABOUTME: L1 asks a role's provider adapter for its CLI invocation and session folder, then
|
|
2
|
+
// hands the command and folder to L1's dispatching function.
|
|
3
|
+
|
|
4
|
+
import { lstatSync, realpathSync } from 'node:fs';
|
|
5
|
+
import { dirname, isAbsolute, join, resolve, sep } from 'node:path';
|
|
6
|
+
|
|
7
|
+
import { within } from './run.mjs';
|
|
8
|
+
import { SCRATCH_REFUSED, WORKSPACE_NOT_MADE, makeScratch } from './workspace.mjs';
|
|
9
|
+
import { gitEnvironment } from '../substrate/git-environment.mjs';
|
|
10
|
+
import { EVENT_REFUSED, NOT_STARTED } from '../substrate/process.mjs';
|
|
11
|
+
import { startFailure } from '../substrate/process.mjs';
|
|
12
|
+
import { ADAPTERS } from '../substrate/providers/adapters.mjs';
|
|
13
|
+
import { workspaces } from '../substrate/worktrees.mjs';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* What L1's `dispatch` is handed to run `answer`, L2's role answer (the architect's ruling 1, Q1
|
|
17
|
+
* and Q4, on #467): the command, arguments and standard input the provider adapter answers, `cwd`
|
|
18
|
+
* as the agent CLI's working directory, `directory` as the dispatch's directory, which `dispatch`
|
|
19
|
+
* takes as its `workspace`, the environment, and the answer's `timeout`. It also answers the
|
|
20
|
+
* answer's `facts` and the `digest` of its evidence L2 handed beside it, which `dispatch.start`
|
|
21
|
+
* records: L1 records the digest it is handed and computes none (the architect's r2 ruling 6 on
|
|
22
|
+
* #642). The caller adds
|
|
23
|
+
* the dispatch's id, card, state directory and sink, and runs it once this has answered (ruling 5).
|
|
24
|
+
*
|
|
25
|
+
* L3 hands the directories beside L2's answer, which names none (ruling 3, P8): for a judge, `cwd`
|
|
26
|
+
* is `main` and `directory` the judge's directory, and they differ. `reach` goes to the adapter
|
|
27
|
+
* unchanged. The adapter is the module `adapters`, L0's map unless a test gives one, holds under
|
|
28
|
+
* the answer's `provider`. It is handed the agent file joined under `cwd` as an absolute path, the
|
|
29
|
+
* answer's `tier`, the prompt as `instruction` then `evidence`, and an L0 emitter under dispatch
|
|
30
|
+
* `id` and `card`, through `sink`, for any process it runs (ruling 5).
|
|
31
|
+
* Only a dispatch L3 marks `maker` supplies a lazy `gitDirectories` reader through L0's workspace
|
|
32
|
+
* adapter. An adapter that does not need git metadata never calls it.
|
|
33
|
+
*
|
|
34
|
+
* The environment is built in four steps (ruling 9): `env`, as L3 hands it; less every variable
|
|
35
|
+
* that redirects git (`gitEnvironment`); less the variables the adapter's `unset` names; and then
|
|
36
|
+
* the adapter's `env`, set over what is left. The evidence itself reaches no event.
|
|
37
|
+
* The adapter also answers `folder`, or none, from the role's working directory and this
|
|
38
|
+
* environment. L1 hands it to `dispatch` beside the role's scratch directory.
|
|
39
|
+
*
|
|
40
|
+
* `scratch` is the card's scratch base, and `repository` the repository L1 made from, each of which
|
|
41
|
+
* L3 hands unread from L1's make (the architect's rulings 19 and 21 on #467). Once the provider is
|
|
42
|
+
* found, and before the adapter is asked for the invocation, L1 makes the role's scratch
|
|
43
|
+
* directory, `<scratch>/<role>`, fresh (`makeScratch`), hands the adapter its absolute path as
|
|
44
|
+
* `scratch`, and answers it as `scratch` for `dispatch`, whose census sweeps it and whose record
|
|
45
|
+
* names it (ruling 20).
|
|
46
|
+
*
|
|
47
|
+
* A dispatch that cannot start rejects with `NOT_STARTED`, before `dispatch` runs, so L1 records no
|
|
48
|
+
* `dispatch.start` for it: a provider the map does not hold; a `scratch` or `repository` that is
|
|
49
|
+
* no absolute path; an unanswered scratch check or make; an `invocation` that rejects; or a
|
|
50
|
+
* variable the adapter may not set (`unsettable`). Each names its reason. An answered scratch
|
|
51
|
+
* refusal rejects with `SCRATCH_REFUSED`, for a maker or a judge. A scratch directory not made, because the file system or git gave
|
|
52
|
+
* no answer, none in time, or an error, rejects with `NOT_STARTED` naming that directory as
|
|
53
|
+
* `missing`, which no other dispatch that never started names, so L2 can withhold a judge for it
|
|
54
|
+
* ("Failure model", step 4). A sink that refused one of L1's scratch events rejects as the
|
|
55
|
+
* refused event it is (`EVENT_REFUSED`).
|
|
56
|
+
*/
|
|
57
|
+
export async function roleDispatch({ answer, cwd, directory, scratch, repository, reach, maker = false, env, sink, id, card, adapters = ADAPTERS }) {
|
|
58
|
+
const { provider } = answer;
|
|
59
|
+
if (!Object.hasOwn(adapters, provider)) throw unstarted(`L0 holds no provider adapter named ${JSON.stringify(provider)}, which role ${answer.role} names`);
|
|
60
|
+
if (typeof scratch !== 'string' || !isAbsolute(scratch)) throw startFailure(unstarted(`L3 handed it the scratch base ${JSON.stringify(scratch)} as scratch, which is no absolute path`), 'dispatch-location');
|
|
61
|
+
if (typeof repository !== 'string' || !isAbsolute(repository)) throw startFailure(unstarted(`L3 handed it ${JSON.stringify(repository)} as repository, which is no absolute path`), 'dispatch-location');
|
|
62
|
+
let made;
|
|
63
|
+
try {
|
|
64
|
+
made = await makeScratch({ base: scratch, role: answer.role, card, dispatch: id, repository, sink });
|
|
65
|
+
} catch (cause) {
|
|
66
|
+
if (cause.code === EVENT_REFUSED) throw cause;
|
|
67
|
+
if (cause.code === SCRATCH_REFUSED) throw cause;
|
|
68
|
+
if (cause.code === WORKSPACE_NOT_MADE) throw Object.assign(unstarted(cause.message, cause), { missing: cause.path });
|
|
69
|
+
throw unstarted(cause.message, cause);
|
|
70
|
+
}
|
|
71
|
+
const environment = gitEnvironment(env);
|
|
72
|
+
let invoked;
|
|
73
|
+
try {
|
|
74
|
+
if (typeof answer.agent !== 'string') throw startFailure(new TypeError('the configured agent file is no string'), 'agent-name-invalid');
|
|
75
|
+
invoked = await adapters[provider].invocation({
|
|
76
|
+
agent: resolve(join(cwd, answer.agent)),
|
|
77
|
+
tier: answer.tier,
|
|
78
|
+
prompt: `${answer.instruction}${answer.evidence}`,
|
|
79
|
+
directory: cwd,
|
|
80
|
+
card,
|
|
81
|
+
scratch: made,
|
|
82
|
+
reach,
|
|
83
|
+
...(maker ? { gitDirectories: () => workspaces({ repository, emitter: sink.emitter({ layer: 'L0', card, dispatch: id }) }).gitDirectories(cwd) } : {}),
|
|
84
|
+
environment,
|
|
85
|
+
emitter: sink.emitter({ layer: 'L0', card, dispatch: id }),
|
|
86
|
+
});
|
|
87
|
+
} catch (cause) {
|
|
88
|
+
const failure = unstarted(`the ${provider} adapter answered no invocation for role ${answer.role}: ${cause.message}`, cause);
|
|
89
|
+
if (cause.startCause !== undefined) {
|
|
90
|
+
const { command, file, ...detail } = cause.startCause;
|
|
91
|
+
failure.startCause = { ...detail, ...(command === undefined ? {} : { provider }), ...(file === undefined ? {} : { file: answer.agent }) };
|
|
92
|
+
}
|
|
93
|
+
if (cause.code === EVENT_REFUSED) failure.startCause = { kind: 'event-unrecorded' };
|
|
94
|
+
throw failure;
|
|
95
|
+
}
|
|
96
|
+
const { command, args, input, unset, env: set, folder } = invoked;
|
|
97
|
+
for (const [key, value] of Object.entries(set)) {
|
|
98
|
+
const why = unsettable(key, value, unset, [directory, made]);
|
|
99
|
+
if (why !== undefined) {
|
|
100
|
+
const { reason, ...startCause } = why;
|
|
101
|
+
throw Object.assign(unstarted(`the ${provider} adapter sets ${key}, which ${reason}`), { startCause });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const name of unset) delete environment[name];
|
|
105
|
+
Object.assign(environment, set);
|
|
106
|
+
return { provider, command, args, input, cwd, workspace: directory, scratch: made, env: environment, folder, timeout: answer.timeout, facts: answer.facts, digest: answer.digest };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The failure of a role's dispatch that never started, which L3 hands L2 as the environment's
|
|
111
|
+
* failure (the architect's ruling 5 on #467). L1 recorded no `dispatch.start` for it.
|
|
112
|
+
*/
|
|
113
|
+
const unstarted = (reason, cause) => Object.assign(new Error(`the role's dispatch did not start: ${reason}`, { cause }), { code: NOT_STARTED });
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Why an adapter may not set `key` to `value` for a dispatch whose directories are `directories`,
|
|
117
|
+
* its own and its scratch directory, or nothing where it may (the architect's ruling 9 on #467, as
|
|
118
|
+
* ruling 18 widens its limit 3). A key L1 removes would undo the removal:
|
|
119
|
+
* one `gitEnvironment` removes, for #151, or one the adapter's own `unset` names, for the
|
|
120
|
+
* measurement behind it. A value must be an absolute path whose real path is one of `directories`
|
|
121
|
+
* or lies under one, compared as `escapes` in L1's dispatching function compares a step's working
|
|
122
|
+
* directory, so whatever the agent writes there stays within the census's reach. A path
|
|
123
|
+
* that does not exist yet resolves through its nearest existing parent (`realOnDisk`). A path that
|
|
124
|
+
* cannot be resolved is refused. The reason never holds the value, which an adapter that set a
|
|
125
|
+
* credential by mistake would carry to whoever records the failure.
|
|
126
|
+
*/
|
|
127
|
+
function unsettable(key, value, unset, directories) {
|
|
128
|
+
const disallowed = (reason) => ({ reason, kind: 'adapter-variable', variable: key });
|
|
129
|
+
if (Object.keys(gitEnvironment({ [key]: '' })).length === 0) return disallowed('is a variable that redirects git, which L1 removes');
|
|
130
|
+
if (unset.includes(key)) return disallowed('is a variable the same adapter says its CLI must not inherit');
|
|
131
|
+
if (typeof value !== 'string' || !isAbsolute(value)) return disallowed('is set to no absolute path');
|
|
132
|
+
let real;
|
|
133
|
+
let roots;
|
|
134
|
+
try {
|
|
135
|
+
real = realOnDisk(value);
|
|
136
|
+
roots = directories.map((directory) => realpathSync.native(directory));
|
|
137
|
+
} catch (cause) {
|
|
138
|
+
return { reason: `is set to a path that cannot be resolved (${cause.code ?? 'unreadable'})`, kind: 'directory-unusable' };
|
|
139
|
+
}
|
|
140
|
+
return roots.some((root) => within(real, root)) ? undefined : disallowed(`is set to a path whose real path lies outside both the dispatch's directory ${roots[0]} and its scratch directory ${roots[1]}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The real path of the absolute path `path`, which need not exist, read one segment at a time
|
|
145
|
+
* from the root as the file system reads it. A segment that exists is resolved, a symbolic link
|
|
146
|
+
* included, before the next is read; one that does not is taken as written, and so is everything
|
|
147
|
+
* under it. `..` goes up from the real path reached so far, never from the path as written, so
|
|
148
|
+
* `missing/../link` resolves `link`, and `link/..` is the parent of where `link` points. Anything
|
|
149
|
+
* but an absent segment, such as a link that cannot be resolved, throws, so it is refused rather
|
|
150
|
+
* than taken for absent.
|
|
151
|
+
*/
|
|
152
|
+
function realOnDisk(path) {
|
|
153
|
+
let real = sep;
|
|
154
|
+
for (const segment of path.split(sep)) {
|
|
155
|
+
if (segment === '' || segment === '.') continue;
|
|
156
|
+
if (segment === '..') {
|
|
157
|
+
real = dirname(real);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const next = join(real, segment);
|
|
161
|
+
try {
|
|
162
|
+
lstatSync(next);
|
|
163
|
+
} catch (failure) {
|
|
164
|
+
if (failure.code !== 'ENOENT') throw failure;
|
|
165
|
+
real = next;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
real = realpathSync.native(next);
|
|
169
|
+
}
|
|
170
|
+
return real;
|
|
171
|
+
}
|