@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,866 @@
|
|
|
1
|
+
// ABOUTME: L1 makes and removes card directories, reads a maker's line tip, and lists and sweeps
|
|
2
|
+
// the provider's session folder after a dispatch settles.
|
|
3
|
+
|
|
4
|
+
import { chmodSync, lstatSync, mkdirSync, readdirSync, realpathSync, rmSync, rmdirSync, statSync, unlinkSync } from 'node:fs';
|
|
5
|
+
import { basename, dirname, isAbsolute, join, relative, sep } from 'node:path';
|
|
6
|
+
|
|
7
|
+
import { within as underOrAt } from './run.mjs';
|
|
8
|
+
import { EVENT_REFUSED, NOT_STARTED } from '../substrate/process.mjs';
|
|
9
|
+
import { ADDING, FETCH_FAILED as L0_FETCH_FAILED, LINE_DELETED as L0_LINE_DELETED, LINE_MOVED as L0_LINE_MOVED, workspaces } from '../substrate/worktrees.mjs';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The `code` of the failure L1 rejects with when it could not make a card's workspace, so its
|
|
13
|
+
* caller tells it from a command that ran, or never started, without reading the message.
|
|
14
|
+
*/
|
|
15
|
+
export const WORKSPACE_NOT_MADE = 'WORKSPACE_NOT_MADE';
|
|
16
|
+
export const WORKSPACE_REFUSED = 'WORKSPACE_REFUSED';
|
|
17
|
+
export const BRANCH_ELSEWHERE = 'BRANCH_ELSEWHERE';
|
|
18
|
+
export const SCRATCH_REFUSED = 'SCRATCH_REFUSED';
|
|
19
|
+
export const LINE_MOVED = 'LINE_MOVED';
|
|
20
|
+
export const LINE_DELETED = 'LINE_DELETED';
|
|
21
|
+
export const FETCH_FAILED = 'FETCH_FAILED';
|
|
22
|
+
export const BRANCH_REFUSED = 'BRANCH_REFUSED';
|
|
23
|
+
export const ROOT_NOT_ABSOLUTE = 'ROOT_NOT_ABSOLUTE';
|
|
24
|
+
|
|
25
|
+
/** A completed replace-rule check that refuses the path, distinct from a check that failed. */
|
|
26
|
+
const replacementRefused = (message, condition) => Object.assign(new Error(message), { code: 'REPLACEMENT_REFUSED', condition });
|
|
27
|
+
|
|
28
|
+
/** L1's report for a cause it met while making a card's workspace. */
|
|
29
|
+
function workspaceReport(cause) {
|
|
30
|
+
if (cause.code === 'REPLACEMENT_REFUSED') return { code: WORKSPACE_REFUSED, requirement: 'R-WORK-13', condition: cause.condition };
|
|
31
|
+
if ([BRANCH_ELSEWHERE, BRANCH_REFUSED, ROOT_NOT_ABSOLUTE].includes(cause.code)) return { code: cause.code };
|
|
32
|
+
if (cause.code === L0_LINE_MOVED) return { code: LINE_MOVED };
|
|
33
|
+
if (cause.code === L0_LINE_DELETED) return { code: LINE_DELETED };
|
|
34
|
+
if (cause.code === L0_FETCH_FAILED) return { code: FETCH_FAILED };
|
|
35
|
+
return { code: WORKSPACE_NOT_MADE };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The name `topic` derives for `card`: the topic with each `{number}` replaced by the card's issue
|
|
40
|
+
* number. It names both the card's workspace, directly under the root, and its branch.
|
|
41
|
+
*/
|
|
42
|
+
export const topicFor = (topic, card) => topic.replaceAll('{number}', String(card));
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The card whose name `topic` derives as `branch`, as `topicFor` derives it, or null where the
|
|
46
|
+
* topic derives no card's name as that branch. An issue number has no leading zero, and every
|
|
47
|
+
* `{number}` in the topic stands for the same one.
|
|
48
|
+
*/
|
|
49
|
+
export function cardOfBranch(topic, branch) {
|
|
50
|
+
// The first `{number}` captures the card and each later one must repeat it. The repeat is a named
|
|
51
|
+
// backreference, so a literal digit after it is never read as part of the reference's number.
|
|
52
|
+
const [first, ...rest] = topic.split('{number}').map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
|
53
|
+
const found = new RegExp(`^${first}(?<card>[1-9][0-9]*)${rest.join('\\k<card>')}$`).exec(branch);
|
|
54
|
+
return found === null ? null : Number(found.groups.card);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The scratch base of card `card`, `<root>/scratch/<topic>`, under which `roleDispatch` makes each
|
|
59
|
+
* role's scratch directory (the architect's ruling 19 on #467). Both makes answer it beside their
|
|
60
|
+
* own paths, and neither creates it. No topic derives `scratch`, since every topic holds a digit.
|
|
61
|
+
*/
|
|
62
|
+
const scratchBase = (root, topic, card) => join(root, 'scratch', topicFor(topic, card));
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Makes card `card`'s workspace fresh from main when handed the card alone, or from its forge line
|
|
66
|
+
* when handed `head` as L2 read it (`R-WORK-10`, `R-WORK-25`), and settles on its `path` and
|
|
67
|
+
* `branch` (`R-WORK-3`), the card's scratch base as
|
|
68
|
+
* `scratch` (`scratchBase`), and `repository` as it was handed (the architect's ruling 21 on #467).
|
|
69
|
+
*
|
|
70
|
+
* `root` is the absolute root the verb resolved (the architect's ruling 3, P3, on #423), `topic`
|
|
71
|
+
* the rule naming the workspace and its branch, `repository` the working tree of the repository
|
|
72
|
+
* Rigger works, which L0's adapter runs git in, and `sink` L5's, through which L1 records under
|
|
73
|
+
* the card a workspace made, one removed, and one that could not be made (ruling 1, A8).
|
|
74
|
+
*
|
|
75
|
+
* Whatever is at the workspace's path, asked of what is there now through any symbolic link, is
|
|
76
|
+
* replaced only where L0 finds it a linked worktree of the repository, not its main working tree,
|
|
77
|
+
* with exactly the card's branch checked out, not the worktree whose top level git reports for
|
|
78
|
+
* `repository`, the worktree L1 was handed, and holding inside it, by real path, no other worktree
|
|
79
|
+
* git lists for the repository, the handed one included. Anything else there fails the attempt
|
|
80
|
+
* naming the path, before any git call that could write, so neither it nor the branch is changed
|
|
81
|
+
* (`R-WORK-13` to `R-WORK-17`; the architect's ruling 9 on #423). Before any of that, L1 asks L0 whether git
|
|
82
|
+
* accepts the card's own derived name as a literal branch name. A workspace that passes is made
|
|
83
|
+
* removable, as `writable` says, and removed; where nothing is at the path, a stale registration
|
|
84
|
+
* there locked by `git worktree add` is unlocked, as `unlockedIfAdding` says. For a continuing
|
|
85
|
+
* attempt, L1 checks that the branch is checked out nowhere else and asks L0 to verify and fetch
|
|
86
|
+
* the forge head before removal. L0 then makes the workspace on the branch at main or `head`,
|
|
87
|
+
* resetting the branch where it exists.
|
|
88
|
+
*
|
|
89
|
+
* An unanswered check or make rejects with `WORKSPACE_NOT_MADE`; an answered replace-rule refusal
|
|
90
|
+
* rejects with `WORKSPACE_REFUSED`. A branch checked out elsewhere rejects with
|
|
91
|
+
* `BRANCH_ELSEWHERE`; a moved or deleted forge line with `LINE_MOVED` or `LINE_DELETED`; a failed
|
|
92
|
+
* forge read with `FETCH_FAILED`; and a refused branch name or nonabsolute root with
|
|
93
|
+
* `BRANCH_REFUSED` or `ROOT_NOT_ABSOLUTE`. Each names its path and reason after L1 records the
|
|
94
|
+
* failure. Where the sink refused any event on the way, L0's, L1's record of the
|
|
95
|
+
* failure, or its record of a workspace made or removed, though the workspace was made or
|
|
96
|
+
* removed, it rejects with `EVENT_REFUSED` instead, naming each such event as unrecorded: a
|
|
97
|
+
* refused event is the halt, not a workspace L1 could not make, so L2 spends no attempt on it (the
|
|
98
|
+
* owner's O4 on #423).
|
|
99
|
+
*/
|
|
100
|
+
export async function makeWorkspace({ root, topic, card, head, repository, sink, timeout }) {
|
|
101
|
+
const events = sink.emitter({ layer: 'L1', card });
|
|
102
|
+
const branch = topicFor(topic, card);
|
|
103
|
+
const path = join(root, branch);
|
|
104
|
+
const adapter = workspaces({ repository, emitter: sink.emitter({ layer: 'L0', card }), timeout, rejectFailedChecks: true });
|
|
105
|
+
/** Runs `work`, rejecting as a workspace L1 could not make where it rejects. */
|
|
106
|
+
const step = async (work) => {
|
|
107
|
+
try {
|
|
108
|
+
return await work();
|
|
109
|
+
} catch (cause) {
|
|
110
|
+
throw notMade(events, `card #${card}'s workspace`, { path }, cause, workspaceReport(cause));
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
const removed = await step(async () => {
|
|
114
|
+
if (!isAbsolute(root)) throw Object.assign(new Error(`the root ${root} is not an absolute path, and L1 is handed the root the verb resolved`), { code: ROOT_NOT_ABSOLUTE });
|
|
115
|
+
const { accepted, why } = await adapter.acceptsBranch(branch);
|
|
116
|
+
if (!accepted) throw Object.assign(new Error(`card #${card}'s topic \`${topic}\` derives ${branch}, which git refuses as a branch name: ${why}`), { code: BRANCH_REFUSED });
|
|
117
|
+
if (head !== undefined) {
|
|
118
|
+
const elsewhere = (await adapter.listing()).find((each) => each.branch === branch && nearestReal(each.at) !== nearestReal(path));
|
|
119
|
+
if (elsewhere !== undefined) throw Object.assign(new Error(`${branch} is checked out at ${elsewhere.at}, not ${path}; R-WORK-25 refuses another worktree`), { code: BRANCH_ELSEWHERE });
|
|
120
|
+
}
|
|
121
|
+
return cleared({ adapter, card, branch, path, repository }, head === undefined ? undefined : () => adapter.fetchLineAt(branch, head));
|
|
122
|
+
});
|
|
123
|
+
if (removed !== undefined) recorded(events, card, 'workspace.removed', { path: removed });
|
|
124
|
+
await step(() => head === undefined ? adapter.make(path, branch) : adapter.makeAt(path, branch, head));
|
|
125
|
+
recorded(events, card, 'workspace.made', { path, branch });
|
|
126
|
+
return { path, branch, scratch: scratchBase(root, topic, card), repository };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Reads the line tip the maker left in its linked workspace, through L0's queued git adapter. */
|
|
130
|
+
async function readWorkspaceTip({ card, dispatch, path, branch, repository, sink }) {
|
|
131
|
+
try {
|
|
132
|
+
return await workspaces({ repository, emitter: sink.emitter({ layer: 'L0', card, dispatch }) }).tip(path, branch);
|
|
133
|
+
} catch (cause) {
|
|
134
|
+
if (cause.code === EVENT_REFUSED) throw cause;
|
|
135
|
+
throw new Error(`card #${card}'s line of work has no readable tip in workspace ${path}: ${cause.message}`, { cause });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Makes the directory judge `role` of card `card` runs in, `<root>/judges/<topic>/<role>`, and
|
|
141
|
+
* settles on its `path`, the card's scratch base as `scratch` (`scratchBase`), `repository` as it
|
|
142
|
+
* was handed (ruling 21), and the paths of the two worktrees it holds, each at a detached commit:
|
|
143
|
+
* `main` at the commit the main line held on `origin` as L1 made it, and `head` at `head`, the
|
|
144
|
+
* pull request's head commit L1 was handed, fetched from `origin` (the owner's O3; ruling 1 Q3).
|
|
145
|
+
*
|
|
146
|
+
* L1 records under the card a `workspace.made` carrying `role`, the path and both commits, and a
|
|
147
|
+
* `workspace.failed` carrying `role`, the path and why (ruling 2 P5). An answered replace-rule
|
|
148
|
+
* refusal rejects with `WORKSPACE_REFUSED`; an unanswered check or failed make rejects with
|
|
149
|
+
* `WORKSPACE_NOT_MADE`, naming the path and why. A refused event rejects with `EVENT_REFUSED`,
|
|
150
|
+
* as `makeWorkspace` does.
|
|
151
|
+
*/
|
|
152
|
+
export const makeJudgeDirectory = (options) => makeDetachedDirectory({ ...options, family: 'judges' });
|
|
153
|
+
|
|
154
|
+
/** Makes the escalation role's detached directory under escalations, using the same replace rule. */
|
|
155
|
+
export const makeEscalationDirectory = (options) => makeDetachedDirectory({ ...options, family: 'escalations' });
|
|
156
|
+
|
|
157
|
+
/** Shared detached-worktree creation and replacement for judges and escalation roles. */
|
|
158
|
+
async function makeDetachedDirectory({ root, topic, card, role, head, repository, sink, family }) {
|
|
159
|
+
const events = sink.emitter({ layer: 'L1', card });
|
|
160
|
+
const path = join(root, family, topicFor(topic, card), role);
|
|
161
|
+
const adapter = workspaces({ repository, emitter: sink.emitter({ layer: 'L0', card }), rejectFailedChecks: family === 'escalations' });
|
|
162
|
+
const trees = { main: join(path, 'main'), ...(head === undefined && family === 'escalations' ? {} : { head: join(path, 'head') }) };
|
|
163
|
+
/** Reports a refused replace rule separately from an unanswered check or make. */
|
|
164
|
+
const step = async (work) => {
|
|
165
|
+
try {
|
|
166
|
+
return await work();
|
|
167
|
+
} catch (cause) {
|
|
168
|
+
const refused = cause.code === 'REPLACEMENT_REFUSED';
|
|
169
|
+
const code = refused ? WORKSPACE_REFUSED : family === 'escalations' ? NOT_STARTED : WORKSPACE_NOT_MADE;
|
|
170
|
+
const what = family === 'escalations' ? 'escalation role' : 'judge';
|
|
171
|
+
const failure = notMade(events, `${what} ${role}'s directory for card #${card}`, { role, path }, cause,
|
|
172
|
+
{ code, ...(refused && family === 'escalations' ? { requirement: 'R-WORK-32', condition: 'replace rule' } : {}) });
|
|
173
|
+
if (failure.code === NOT_STARTED) failure.missing = path;
|
|
174
|
+
throw failure;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
const found = await step(() => judgeChecked({ adapter, path, repository }));
|
|
178
|
+
const main = await step(async () => {
|
|
179
|
+
const { commit } = await adapter.fetchMainLine();
|
|
180
|
+
if (trees.head !== undefined) {
|
|
181
|
+
const { held, why } = await adapter.holdsAfterFetch(head);
|
|
182
|
+
if (!held) throw new Error(`the repository does not hold the head commit ${head} after fetching it: ${why}`);
|
|
183
|
+
}
|
|
184
|
+
return commit;
|
|
185
|
+
});
|
|
186
|
+
await step(() => judgeCleared(adapter, found));
|
|
187
|
+
if (found.present) recorded(events, card, 'workspace.removed', { role, path });
|
|
188
|
+
await step(async () => {
|
|
189
|
+
await adapter.makeDetached(trees.main, main);
|
|
190
|
+
if (trees.head !== undefined) await adapter.makeDetached(trees.head, head);
|
|
191
|
+
});
|
|
192
|
+
recorded(events, card, 'workspace.made', { role, path, main, head });
|
|
193
|
+
return { path, ...trees, scratch: scratchBase(root, topic, card), repository };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Makes role `role`'s scratch directory of card `card`, `<base>/<role>`, fresh, and settles on its
|
|
198
|
+
* path (the architect's ruling 19 on #467). `base` is the card's scratch base, which L1's makes
|
|
199
|
+
* answer (`scratchBase`), and which this makes where it does not exist yet.
|
|
200
|
+
*
|
|
201
|
+
* A directory already there is removed first, without following any symbolic link inside it, once
|
|
202
|
+
* `scratchReplaceable` has passed what is there. L1
|
|
203
|
+
* records under the card and optional dispatch a `workspace.removed` and a `workspace.made`, each carrying `role` and
|
|
204
|
+
* the path, and a `workspace.failed` carrying them and why. An answered replace-rule refusal
|
|
205
|
+
* rejects with `SCRATCH_REFUSED`; an unanswered check or make rejects with `WORKSPACE_NOT_MADE`.
|
|
206
|
+
* Each names the path and why. A refused sink event rejects with `EVENT_REFUSED`, as in
|
|
207
|
+
* `makeWorkspace`.
|
|
208
|
+
*/
|
|
209
|
+
export async function makeScratch({ base, role, card, dispatch, repository, sink }) {
|
|
210
|
+
const events = sink.emitter({ layer: 'L1', card, dispatch });
|
|
211
|
+
const path = join(base, role);
|
|
212
|
+
const step = async (work) => {
|
|
213
|
+
try {
|
|
214
|
+
return await work();
|
|
215
|
+
} catch (cause) {
|
|
216
|
+
const report = cause.code === 'REPLACEMENT_REFUSED'
|
|
217
|
+
? { code: SCRATCH_REFUSED, requirement: 'R-WORK-26', condition: cause.condition }
|
|
218
|
+
: { code: WORKSPACE_NOT_MADE };
|
|
219
|
+
throw notMade(events, `role ${role}'s scratch directory for card #${card}`, { role, path }, cause, report);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
const there = await step(() => scratchReplaceable({ base, path, repository, adapter: workspaces({ repository, emitter: sink.emitter({ layer: 'L0', card, dispatch }) }) }));
|
|
223
|
+
if (there) {
|
|
224
|
+
await step(async () => {
|
|
225
|
+
writable(path);
|
|
226
|
+
rmSync(path, { recursive: true });
|
|
227
|
+
});
|
|
228
|
+
recorded(events, card, 'workspace.removed', { role, path });
|
|
229
|
+
}
|
|
230
|
+
await step(() => mkdirSync(path, { recursive: true }));
|
|
231
|
+
recorded(events, card, 'workspace.made', { role, path });
|
|
232
|
+
return path;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** The complete scratch replace rule, shared by making a role directory and removing a merged card's. */
|
|
236
|
+
async function scratchReplaceable({ base, path, repository, adapter }) {
|
|
237
|
+
noCheckoutAbove(base, path);
|
|
238
|
+
const there = scratchChecked(base, path);
|
|
239
|
+
await unregistered({ path, there, repository, adapter });
|
|
240
|
+
if (there) {
|
|
241
|
+
const checkout = checkoutIn(path);
|
|
242
|
+
if (checkout !== undefined) throw replacementRefused(`${path} holds ${checkout}, a git checkout, which may be a worktree of another repository, so L1 leaves it as it is`, 'R-WORK-28');
|
|
243
|
+
}
|
|
244
|
+
return there;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Fails naming `path` and a `.git` where one is, of any kind and unfollowed, at any ancestor of
|
|
249
|
+
* `path` from its parent, the scratch base `base`, up to the file system's root, as written and as
|
|
250
|
+
* the real path of the nearest existing one reads: then `path` lies in a checkout, of this
|
|
251
|
+
* repository or another, and L1 makes and removes nothing there, since the scratch directory lies
|
|
252
|
+
* outside every worktree. The real path's ancestors are walked too, so a root reached through a
|
|
253
|
+
* symbolic link into a checkout is found. An ancestor where L1 cannot read whether a `.git` is
|
|
254
|
+
* there fails too, as does a real path L1 cannot resolve for any reason but absence, since L1
|
|
255
|
+
* cannot tell. So an operator whose home directory is a checkout is refused, naming that `.git`.
|
|
256
|
+
*/
|
|
257
|
+
function noCheckoutAbove(base, path) {
|
|
258
|
+
noCheckoutFrom(base, path);
|
|
259
|
+
let real;
|
|
260
|
+
try {
|
|
261
|
+
real = nearestReal(base);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
throw new Error(`${path} lies under ${base}, whose real path L1 cannot resolve, so it cannot tell ${path} lies in no checkout: ${error.message}`, { cause: error });
|
|
264
|
+
}
|
|
265
|
+
if (real !== base) noCheckoutFrom(real, path);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** `noCheckoutAbove`'s walk from `start` up to the file system's root, for `path`. */
|
|
269
|
+
function noCheckoutFrom(start, path) {
|
|
270
|
+
for (let ancestor = start; ; ancestor = dirname(ancestor)) {
|
|
271
|
+
const git = join(ancestor, '.git');
|
|
272
|
+
let found = true;
|
|
273
|
+
try {
|
|
274
|
+
lstatSync(git);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if (error.code !== 'ENOENT') throw new Error(`${path} lies under ${ancestor}, where L1 cannot read whether ${git} is there, so it cannot tell ${path} lies in no checkout: ${error.message}`, { cause: error });
|
|
277
|
+
found = false;
|
|
278
|
+
}
|
|
279
|
+
if (found) throw replacementRefused(`${path} lies under ${ancestor}${realNamed(ancestor)}, which holds ${git}, so it lies in a git checkout, and L1 makes and removes nothing there`, 'R-WORK-28');
|
|
280
|
+
if (dirname(ancestor) === ancestor) return;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** `path`'s real path as a failure adds it, where it differs from `path` and can be read. */
|
|
285
|
+
function realNamed(path) {
|
|
286
|
+
try {
|
|
287
|
+
const real = realpathSync.native(path);
|
|
288
|
+
return real === path ? '' : ` (${real})`;
|
|
289
|
+
} catch {
|
|
290
|
+
return '';
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Checks what is at a role's scratch directory, `path`, under the scratch base `base`, changing
|
|
296
|
+
* nothing, and hands back whether a directory is there to replace. The worktree root's `scratch`
|
|
297
|
+
* and the base must each be a directory, not a symbolic link, where they exist, so nothing L1
|
|
298
|
+
* removes or makes lies elsewhere. At `path` must be nothing, or a directory, not a symbolic link,
|
|
299
|
+
* whose real path resolves. Anything else fails naming the path, as does a path L1 cannot read,
|
|
300
|
+
* since L1 cannot tell what is there. `scratchReplaceable` then asks git (`unregistered`) and walks the
|
|
301
|
+
* directory (`checkoutIn`) before it removes anything.
|
|
302
|
+
*/
|
|
303
|
+
function scratchChecked(base, path) {
|
|
304
|
+
for (const each of [dirname(base), base]) {
|
|
305
|
+
const there = lstatOrAbsent(each);
|
|
306
|
+
if (there === undefined) return false;
|
|
307
|
+
if (!there.isDirectory()) throw notDirectory(each, there);
|
|
308
|
+
}
|
|
309
|
+
const there = lstatOrAbsent(path);
|
|
310
|
+
if (there === undefined) return false;
|
|
311
|
+
if (!there.isDirectory()) throw notDirectory(path, there);
|
|
312
|
+
try {
|
|
313
|
+
realpathSync.native(path);
|
|
314
|
+
} catch (error) {
|
|
315
|
+
throw new Error(`${path} cannot be resolved: ${error.message}`, { cause: error });
|
|
316
|
+
}
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Fails naming `path` and the worktree where git, asked through L0's workspace adapter `adapter`
|
|
322
|
+
* over `repository`, lists a worktree, the main working tree included, that `path` is or lies in,
|
|
323
|
+
* by the real path of `path` or, where it is not there yet, of its nearest existing parent; or,
|
|
324
|
+
* where a directory is `there`, a worktree that lies in it (the architect's ruling 21 on #467). So
|
|
325
|
+
* L1 makes and removes nothing in a worktree, and removes none. Registration is git's fact (`D16`
|
|
326
|
+
* rule 1), so a worktree whose `.git` was moved aside is still found. A registration whose
|
|
327
|
+
* directory is gone lies nowhere; one L1 cannot resolve for any other reason fails, since L1 cannot
|
|
328
|
+
* tell where it lies.
|
|
329
|
+
*
|
|
330
|
+
* Where it can be wrong (`D16` rule 3): this git lists only this repository's worktrees, and
|
|
331
|
+
* `checkoutIn`, the second check, finds another repository's only by its `.git`. So a worktree of
|
|
332
|
+
* another repository whose `.git` was moved aside is found by neither, and is removed.
|
|
333
|
+
*/
|
|
334
|
+
async function unregistered({ path, there, repository, adapter }) {
|
|
335
|
+
const real = nearestReal(path);
|
|
336
|
+
for (const listed of await adapter.registered()) {
|
|
337
|
+
let tree;
|
|
338
|
+
try {
|
|
339
|
+
tree = realpathSync.native(listed);
|
|
340
|
+
} catch (error) {
|
|
341
|
+
if (error.code === 'ENOENT') continue;
|
|
342
|
+
throw new Error(`${path} may lie in or hold the worktree git lists at ${listed}, which L1 could not resolve, so L1 leaves it as it is: ${error.message}`, { cause: error });
|
|
343
|
+
}
|
|
344
|
+
if (underOrAt(real, tree)) throw replacementRefused(`${path} lies in ${listed}, a worktree git registers for the repository at ${repository}, so L1 makes and removes nothing there`, 'R-WORK-29');
|
|
345
|
+
if (there && underOrAt(tree, real)) throw replacementRefused(`${path} holds ${listed}, a worktree git registers for the repository at ${repository}, so L1 leaves it as it is`, 'R-WORK-29');
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The real path of `path`, or, where nothing is there, its nearest existing parent's joined to
|
|
351
|
+
* the rest of it. Any failure but absence throws, so L1 cannot take an unreadable path for absent.
|
|
352
|
+
*/
|
|
353
|
+
function nearestReal(path) {
|
|
354
|
+
try {
|
|
355
|
+
return realpathSync.native(path);
|
|
356
|
+
} catch (error) {
|
|
357
|
+
if (error.code !== 'ENOENT' || dirname(path) === path) throw error;
|
|
358
|
+
return join(nearestReal(dirname(path)), basename(path));
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** The failure for `path`, which `lstat` read as `there`, no directory: a symbolic link, which is never followed, or anything else. */
|
|
363
|
+
const notDirectory = (path, there) => replacementRefused(`${path} is ${there.isSymbolicLink() ? 'a symbolic link' : 'not a directory'}, so L1 leaves it as it is`, 'R-WORK-27');
|
|
364
|
+
|
|
365
|
+
/** What `lstat` reads at `path`, or nothing where nothing is there; anything else unreadable throws naming it. */
|
|
366
|
+
function lstatOrAbsent(path) {
|
|
367
|
+
try {
|
|
368
|
+
return lstatSync(path);
|
|
369
|
+
} catch (error) {
|
|
370
|
+
if (error.code === 'ENOENT') return undefined;
|
|
371
|
+
throw new Error(`${path} cannot be read: ${error.message}`, { cause: error });
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** The directory identity used to refuse a sweep if its path now names another directory. */
|
|
376
|
+
function folderIdentity(path) {
|
|
377
|
+
const real = realpathSync.native(path);
|
|
378
|
+
const held = statSync(real, { bigint: true });
|
|
379
|
+
if (!held.isDirectory()) throw new Error(`${path} is not a directory`);
|
|
380
|
+
return { real, device: String(held.dev), inode: String(held.ino) };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** Paths already under a folder, read without following any link. */
|
|
384
|
+
function pathsInFolder(path, found = new Set(), prefix = '') {
|
|
385
|
+
for (const name of readdirSync(path).sort()) {
|
|
386
|
+
const relativePath = prefix === '' ? name : join(prefix, name);
|
|
387
|
+
const at = join(path, name);
|
|
388
|
+
const held = lstatOrAbsent(at);
|
|
389
|
+
if (held === undefined) throw new Error(`${at} disappeared while its folder was listed`);
|
|
390
|
+
found.add(relativePath);
|
|
391
|
+
if (held.isDirectory()) pathsInFolder(at, found, relativePath);
|
|
392
|
+
}
|
|
393
|
+
return found;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** Lists a CLI folder before its command starts, or names why no later removal is safe. */
|
|
397
|
+
export function listCliFolder({ home, name }) {
|
|
398
|
+
if (typeof home !== 'string' || !isAbsolute(home)) return { reason: 'the session has no absolute configuration home' };
|
|
399
|
+
if (typeof name !== 'string' || name === '' || name === '.' || name === '..' || name.includes(sep)) {
|
|
400
|
+
return { reason: `the adapter named no single folder segment: ${JSON.stringify(name)}` };
|
|
401
|
+
}
|
|
402
|
+
const path = join(home, 'projects', name);
|
|
403
|
+
try {
|
|
404
|
+
const held = lstatOrAbsent(path);
|
|
405
|
+
if (held !== undefined) {
|
|
406
|
+
if (!held.isDirectory()) return { path, reason: 'the folder is not a directory, so no listing followed it' };
|
|
407
|
+
const root = folderIdentity(path);
|
|
408
|
+
return { path, root, before: pathsInFolder(path) };
|
|
409
|
+
}
|
|
410
|
+
return { path, parent: folderIdentity(dirname(path)), before: new Set() };
|
|
411
|
+
} catch (error) {
|
|
412
|
+
return { path, reason: `the folder or its parent could not be read before the session: ${error.message}` };
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** Removes entries absent from the listing, children first, without following any link. */
|
|
417
|
+
function removeAdded(path, relativePath, before, removeRoot, left) {
|
|
418
|
+
let held;
|
|
419
|
+
try {
|
|
420
|
+
held = lstatOrAbsent(path);
|
|
421
|
+
if (held === undefined) return;
|
|
422
|
+
if (held.isDirectory()) {
|
|
423
|
+
for (const name of readdirSync(path).sort()) {
|
|
424
|
+
removeAdded(join(path, name), relativePath === '' ? name : join(relativePath, name), before, removeRoot, left);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (relativePath === '' ? removeRoot : !before.has(relativePath)) {
|
|
428
|
+
if (held.isDirectory()) rmdirSync(path);
|
|
429
|
+
else unlinkSync(path);
|
|
430
|
+
}
|
|
431
|
+
} catch (error) {
|
|
432
|
+
left.push({ path, reason: `the added path could not be removed: ${error.message}` });
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** Removes only new entries while the listed directory identity still holds. */
|
|
437
|
+
export function sweepCliFolder(listing) {
|
|
438
|
+
if (listing.reason !== undefined) return [{ path: listing.path, reason: listing.reason }];
|
|
439
|
+
const { path, parent, root, before } = listing;
|
|
440
|
+
try {
|
|
441
|
+
if (root !== undefined && lstatOrAbsent(path) === undefined) return [];
|
|
442
|
+
const anchor = root === undefined ? dirname(path) : path;
|
|
443
|
+
if (root === undefined && lstatOrAbsent(anchor) === undefined) return [];
|
|
444
|
+
const expected = root ?? parent;
|
|
445
|
+
const now = folderIdentity(anchor);
|
|
446
|
+
if (now.real !== expected.real || now.device !== expected.device || now.inode !== expected.inode) {
|
|
447
|
+
return [{ path, reason: 'the folder anchor is no longer the directory listed before the session' }];
|
|
448
|
+
}
|
|
449
|
+
const held = lstatOrAbsent(path);
|
|
450
|
+
if (held === undefined) return [];
|
|
451
|
+
if (!held.isDirectory()) return [{ path, reason: 'the new folder is not a directory, so no removal followed it' }];
|
|
452
|
+
const left = [];
|
|
453
|
+
removeAdded(path, '', before, root === undefined, left);
|
|
454
|
+
return left;
|
|
455
|
+
} catch (error) {
|
|
456
|
+
return [{ path, reason: `the folder could not be removed: ${error.message}` }];
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* The first `.git` under the directory `path`, at any depth, or nothing where it holds none, read
|
|
462
|
+
* in name order. No symbolic link is followed. A directory that cannot be read throws naming it,
|
|
463
|
+
* since L1 cannot tell it holds no checkout, and nothing is changed to read it.
|
|
464
|
+
*/
|
|
465
|
+
function checkoutIn(path) {
|
|
466
|
+
let names;
|
|
467
|
+
try {
|
|
468
|
+
names = readdirSync(path, { withFileTypes: true }).sort((one, other) => (one.name < other.name ? -1 : 1));
|
|
469
|
+
} catch (error) {
|
|
470
|
+
throw new Error(`${path} cannot be read, so L1 cannot tell it holds no git checkout, and leaves it as it is: ${error.message}`, { cause: error });
|
|
471
|
+
}
|
|
472
|
+
for (const entry of names) {
|
|
473
|
+
const at = join(path, entry.name);
|
|
474
|
+
if (entry.name === '.git') return at;
|
|
475
|
+
if (entry.isDirectory()) {
|
|
476
|
+
const found = checkoutIn(at);
|
|
477
|
+
if (found !== undefined) return found;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
return undefined;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Checks what is at a judge's directory, `path`, against `R-WORK-20` to `R-WORK-23`, making no git
|
|
485
|
+
* call that writes, so a refusal changes nothing: no ref, no object, no file. It runs before any
|
|
486
|
+
* fetch for that reason. It hands back the directory's real path, whether anything is there, the
|
|
487
|
+
* worktrees in it, and the registrations L1 must unlock first.
|
|
488
|
+
*
|
|
489
|
+
* Nothing there passes. Otherwise the path must be a directory, not a symbolic link, and everything
|
|
490
|
+
* in it a directory L0 finds a linked worktree of the repository, which git lists at a detached
|
|
491
|
+
* commit, unlocked or locked with `ADDING`, the reason Rigger's own `git worktree add` writes while
|
|
492
|
+
* it makes one (#423's ruling 9); neither the path nor anything inside it, by real path, may be the
|
|
493
|
+
* worktree git reports for `repository`, the worktree L1 was handed; and git may list no other
|
|
494
|
+
* worktree of the repository inside it. A registration inside it whose directory is gone passes
|
|
495
|
+
* where it is unlocked, which the make's prune forgets, or detached and locked with `ADDING`, which
|
|
496
|
+
* L1 unlocks; any other lock is a person's, and fails. Anything else fails naming the path, as does
|
|
497
|
+
* a path whose real path or contents L1 cannot read, since L1 cannot tell what is there.
|
|
498
|
+
*
|
|
499
|
+
* Every path is compared by the file system's own real path, `realpath(3)`, on both sides, since
|
|
500
|
+
* git keeps the spelling a worktree was added through and a volume that folds case answers to
|
|
501
|
+
* either.
|
|
502
|
+
*/
|
|
503
|
+
async function judgeChecked({ adapter, path, repository }) {
|
|
504
|
+
const leaves = (why, blocksDone = false) => Object.assign(replacementRefused(`${path} ${why}, so L1 leaves it as it is`), { blocksDone });
|
|
505
|
+
/** `there`'s real path, or, where nothing is there, its parent's joined to its name. */
|
|
506
|
+
const real = (there) => {
|
|
507
|
+
try {
|
|
508
|
+
return realpathSync.native(there);
|
|
509
|
+
} catch (error) {
|
|
510
|
+
if (error.code !== 'ENOENT' || dirname(there) === there) throw new Error(`${path} cannot be read at ${there}: ${error.message}`, { cause: error });
|
|
511
|
+
return join(real(dirname(there)), basename(there));
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
let there;
|
|
515
|
+
try {
|
|
516
|
+
there = lstatSync(path);
|
|
517
|
+
} catch (error) {
|
|
518
|
+
if (error.code !== 'ENOENT') throw new Error(`${path} cannot be read: ${error.message}`, { cause: error });
|
|
519
|
+
}
|
|
520
|
+
if (there !== undefined && !there.isDirectory()) throw leaves('is not a directory');
|
|
521
|
+
const own = real(path);
|
|
522
|
+
let names = [];
|
|
523
|
+
if (there !== undefined) {
|
|
524
|
+
try {
|
|
525
|
+
names = readdirSync(own);
|
|
526
|
+
} catch (error) {
|
|
527
|
+
throw new Error(`${path} cannot be read: ${error.message}`, { cause: error });
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
const inside = (at) => {
|
|
531
|
+
const from = relative(own, at);
|
|
532
|
+
return from !== '' && !from.startsWith(`..${sep}`) && from !== '..' && !isAbsolute(from);
|
|
533
|
+
};
|
|
534
|
+
const handed = realpathSync.native(await adapter.topLevel());
|
|
535
|
+
if (handed === own || inside(handed)) throw leaves(`holds the worktree L1 was handed as the repository, at ${repository}`);
|
|
536
|
+
const listed = (await adapter.listing()).map((record) => ({ ...record, real: real(record.at) }));
|
|
537
|
+
const trees = [];
|
|
538
|
+
const unlocking = [];
|
|
539
|
+
/** Adds `record`'s worktree to those L1 unlocks where `ADDING` locks it, failing on any other lock. */
|
|
540
|
+
const lockOf = (record) => {
|
|
541
|
+
if (record.locked === undefined) return;
|
|
542
|
+
if (!record.detached || record.locked !== ADDING) throw leaves(`holds ${record.at}, a worktree of the repository locked with the reason ${JSON.stringify(record.locked)}`, true);
|
|
543
|
+
unlocking.push(record.real);
|
|
544
|
+
};
|
|
545
|
+
for (const name of names) {
|
|
546
|
+
const tree = join(own, name);
|
|
547
|
+
if (!lstatSync(tree).isDirectory() || !(await adapter.isWorktree(tree))) throw leaves(`holds ${tree}, which is not a worktree of the repository`);
|
|
548
|
+
const record = listed.find((each) => each.real === tree);
|
|
549
|
+
if (record === undefined) throw leaves(`holds ${tree}, a worktree where git lists no worktree of the repository`);
|
|
550
|
+
if (!record.detached) throw leaves(`holds ${tree}, a worktree of the repository on the branch ${record.branch}, not at a detached commit`);
|
|
551
|
+
lockOf(record);
|
|
552
|
+
trees.push(tree);
|
|
553
|
+
}
|
|
554
|
+
for (const record of listed.filter((each) => inside(each.real) && !trees.includes(each.real))) {
|
|
555
|
+
if (present(record.real)) throw leaves(`holds another worktree of the repository, at ${record.at}`);
|
|
556
|
+
lockOf(record);
|
|
557
|
+
}
|
|
558
|
+
return { real: own, present: there !== undefined, trees, unlocking };
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Clears the judge's directory `judgeChecked` passed: unlocks each registration it named, then
|
|
563
|
+
* makes the directory removable, as `writable` says, and removes each worktree in it and the
|
|
564
|
+
* directory itself.
|
|
565
|
+
*/
|
|
566
|
+
async function judgeCleared(adapter, { real, present: there, trees, unlocking }) {
|
|
567
|
+
for (const tree of unlocking) await adapter.unlock(tree);
|
|
568
|
+
if (!there) return;
|
|
569
|
+
writable(real);
|
|
570
|
+
for (const tree of trees) await adapter.remove(tree);
|
|
571
|
+
rmdirSync(real);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Clears card `card`'s workspace path, `path`, for a workspace on `branch`, under the replace rule
|
|
576
|
+
* above, and hands back the real path of the workspace it removed, or nothing where none was there.
|
|
577
|
+
*
|
|
578
|
+
* Every path is compared by the file system's own real path, `realpath(3)`, as L0's checks compare
|
|
579
|
+
* them, since the root reaches L1 in whatever case its caller spelled it and git prints paths in
|
|
580
|
+
* the case the disk holds.
|
|
581
|
+
*/
|
|
582
|
+
async function cleared({ adapter, card, branch, path, repository }, beforeRemove = async () => {}) {
|
|
583
|
+
if (!present(path)) {
|
|
584
|
+
await beforeRemove();
|
|
585
|
+
await unlockedIfAdding(adapter, path, branch);
|
|
586
|
+
return undefined;
|
|
587
|
+
}
|
|
588
|
+
if (!(await adapter.isWorktree(path))) {
|
|
589
|
+
throw replacementRefused(`${path} holds something that is not a workspace of the repository at ${repository}, so L1 leaves it as it is`, 'R-WORK-14');
|
|
590
|
+
}
|
|
591
|
+
const real = realpathSync.native(path);
|
|
592
|
+
if (real === (await adapter.topLevel())) {
|
|
593
|
+
throw replacementRefused(`${path} is the worktree L1 was handed as the repository, at ${repository}, so L1 leaves it as it is`, 'R-WORK-16');
|
|
594
|
+
}
|
|
595
|
+
const held = await adapter.registration(real);
|
|
596
|
+
if (held?.branch !== branch) {
|
|
597
|
+
const holding = held?.detached ? 'a detached HEAD' : `the branch ${held?.branch}`;
|
|
598
|
+
throw replacementRefused(`${path} is a worktree of the repository holding ${holding}, not card #${card}'s branch ${branch}, so L1 leaves it as it is`, 'R-WORK-15');
|
|
599
|
+
}
|
|
600
|
+
const nested = (await adapter.registered()).find((listed) => within(path, real, listed));
|
|
601
|
+
if (nested !== undefined) {
|
|
602
|
+
throw replacementRefused(`${path} holds another worktree of the repository, at ${nested}, so L1 leaves it as it is`, 'R-WORK-17');
|
|
603
|
+
}
|
|
604
|
+
await beforeRemove();
|
|
605
|
+
writable(real);
|
|
606
|
+
await adapter.remove(real);
|
|
607
|
+
// A workspace named through a symbolic link leaves the link behind, pointing at nothing.
|
|
608
|
+
if (present(path)) unlinkSync(path);
|
|
609
|
+
return real;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Records L1's `event` of card `card`, with `fields`, through `events`, or, where the sink refuses
|
|
614
|
+
* it, rejects with an `EVENT_REFUSED` failure naming the event as unrecorded, carrying it in
|
|
615
|
+
* `unrecorded` as L0's and L1's other refusals do.
|
|
616
|
+
*/
|
|
617
|
+
function recorded(events, card, event, fields) {
|
|
618
|
+
try {
|
|
619
|
+
events.emit(event, fields);
|
|
620
|
+
} catch (cause) {
|
|
621
|
+
const failure = new Error(`the sink refused L1's ${event} of card #${card} ${JSON.stringify(fields)}, so it went unrecorded: ${cause.message}`, { cause });
|
|
622
|
+
throw Object.assign(failure, { code: EVENT_REFUSED, unrecorded: [{ event, ...fields, cause }] });
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Builds L1's workspace handle over `root` and `topic` for the repository at `repository`: the
|
|
628
|
+
* function L3 is handed. It makes from main with the card alone, or from the card's forge line
|
|
629
|
+
* when also handed the head L2 read, as `makeWorkspace` does (the architect's ruling 5, P4, on #423).
|
|
630
|
+
* Its `tip` method reads the line's checked-out commit after a continuing maker exits.
|
|
631
|
+
*
|
|
632
|
+
* Building it asks L0 whether git accepts the name the topic derives for card 1 as a literal
|
|
633
|
+
* branch name, and rejects naming the topic and why where it does not, before any directory is
|
|
634
|
+
* made. That is a fast refusal of a topic bad for every card, not the guard: `@{-1}` and `@{-3}`
|
|
635
|
+
* name different previous checkouts, so card 1's answer does not stand for card 3's, and every
|
|
636
|
+
* attempt asks again about its own name before any git call that changes a ref or a directory
|
|
637
|
+
* (ruling 6, Q-B, as the architect's ruling 9 on #423 corrects it).
|
|
638
|
+
*/
|
|
639
|
+
export async function workspaceHandle({ root, topic, repository, sink }) {
|
|
640
|
+
const name = topicFor(topic, 1);
|
|
641
|
+
const { accepted, why } = await workspaces({ repository, emitter: sink.emitter({ layer: 'L0' }) }).acceptsBranch(name);
|
|
642
|
+
if (!accepted) throw new Error(`the worktree topic \`${topic}\` derives ${name} for card #1, which git refuses as a branch name: ${why}`);
|
|
643
|
+
const handle = (card, head) => makeWorkspace({ root, topic, card, head, repository, sink });
|
|
644
|
+
handle.tip = ({ card, dispatch, path, branch }) => readWorkspaceTip({ card, dispatch, path, branch, repository, sink });
|
|
645
|
+
return handle;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* Builds L1's make of a judge's directory over `root` and `topic` for the repository at
|
|
650
|
+
* `repository`: the function L3 is handed as `judgeDirectory`, which makes judge `role`'s directory
|
|
651
|
+
* of card `card` at the head commit `head`, as `makeJudgeDirectory` does (the architect's ruling 4's
|
|
652
|
+
* addendum on #467).
|
|
653
|
+
*/
|
|
654
|
+
export const judgeDirectoryHandle = ({ root, topic, repository, sink }) => (card, role, head) => makeJudgeDirectory({ root, topic, card, role, head, repository, sink });
|
|
655
|
+
|
|
656
|
+
/** The escalation-directory make L3 receives from the verb. */
|
|
657
|
+
export const escalationDirectoryHandle = ({ root, topic, repository, sink }) => (card, role, head) => makeEscalationDirectory({ root, topic, card, role, head, repository, sink });
|
|
658
|
+
|
|
659
|
+
/** Removes a card's directories under their replace rules. */
|
|
660
|
+
export async function removeCardDirectories({ root, topic, card, repository, sink, timeout }) {
|
|
661
|
+
if (!isAbsolute(root)) throw new Error(`the root ${root} is not an absolute path`);
|
|
662
|
+
const name = topicFor(topic, card);
|
|
663
|
+
const events = sink.emitter({ layer: 'L1', card });
|
|
664
|
+
const adapter = workspaces({ repository, emitter: sink.emitter({ layer: 'L0', card }), timeout, rejectFailedChecks: true });
|
|
665
|
+
const failures = [];
|
|
666
|
+
const left = (path, reason, role) => recorded(events, card, 'workspace.left', { path, reason, ...(role === undefined ? {} : { role }) });
|
|
667
|
+
const removed = (path, role) => recorded(events, card, 'workspace.removed', { path, ...(role === undefined ? {} : { role }) });
|
|
668
|
+
/** Only a completed rule's refusal leaves Done unblocked; an unread check or failed removal blocks it. */
|
|
669
|
+
const attempt = async (path, role, work) => {
|
|
670
|
+
let begun = false;
|
|
671
|
+
try {
|
|
672
|
+
const gone = await work(() => { begun = true; });
|
|
673
|
+
if (gone) removed(typeof gone === 'string' ? gone : path, role);
|
|
674
|
+
} catch (failure) {
|
|
675
|
+
if (failure.code === EVENT_REFUSED) throw failure;
|
|
676
|
+
left(path, failure.message, role);
|
|
677
|
+
if (begun || failure.blocksDone || failure.code !== 'REPLACEMENT_REFUSED') failures.push(failure);
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
const workspace = join(root, name);
|
|
681
|
+
await attempt(workspace, undefined, async (begin) => {
|
|
682
|
+
const held = await adapter.registration(workspace);
|
|
683
|
+
if (held?.locked !== undefined && held.locked !== ADDING) throw Object.assign(new Error(`${workspace} is locked with the reason ${JSON.stringify(held.locked)}`), { blocksDone: true });
|
|
684
|
+
const gone = await cleared({ adapter, card, branch: name, path: workspace, repository }, async () => {
|
|
685
|
+
begin();
|
|
686
|
+
if (held?.locked === ADDING) await adapter.unlock(workspace);
|
|
687
|
+
});
|
|
688
|
+
return gone;
|
|
689
|
+
});
|
|
690
|
+
/** Removes each disk entry under one topic, then the topic directory once safely empty. */
|
|
691
|
+
const children = async (kind, remove) => {
|
|
692
|
+
const parent = join(root, kind);
|
|
693
|
+
const base = join(parent, name);
|
|
694
|
+
let names;
|
|
695
|
+
try {
|
|
696
|
+
const above = lstatOrAbsent(parent);
|
|
697
|
+
if (above === undefined) return;
|
|
698
|
+
if (!above.isDirectory()) throw notDirectory(parent, above);
|
|
699
|
+
const there = lstatOrAbsent(base);
|
|
700
|
+
if (there === undefined) return;
|
|
701
|
+
if (!there.isDirectory()) throw notDirectory(base, there);
|
|
702
|
+
names = readdirSync(base).sort();
|
|
703
|
+
} catch (failure) {
|
|
704
|
+
left(base, failure.message);
|
|
705
|
+
if (failure.code !== 'REPLACEMENT_REFUSED') failures.push(failure);
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
for (const role of names) {
|
|
709
|
+
const path = join(base, role);
|
|
710
|
+
await attempt(path, role, (begin) => remove({ path, base, role, begin }));
|
|
711
|
+
}
|
|
712
|
+
try {
|
|
713
|
+
if (readdirSync(base).length > 0) {
|
|
714
|
+
left(base, `${base} is not empty`);
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
rmdirSync(base);
|
|
718
|
+
removed(base);
|
|
719
|
+
} catch (failure) {
|
|
720
|
+
if (failure.code === EVENT_REFUSED) throw failure;
|
|
721
|
+
left(base, failure.message);
|
|
722
|
+
failures.push(failure);
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
const removeDetached = async ({ path, begin }) => {
|
|
726
|
+
const found = await judgeChecked({ adapter, path, repository });
|
|
727
|
+
if (!found.present) return false;
|
|
728
|
+
begin();
|
|
729
|
+
await judgeCleared(adapter, found);
|
|
730
|
+
return true;
|
|
731
|
+
};
|
|
732
|
+
await children('judges', removeDetached);
|
|
733
|
+
await children('escalations', removeDetached);
|
|
734
|
+
await children('scratch', async ({ path, base, begin }) => {
|
|
735
|
+
const there = await scratchReplaceable({ base, path, repository, adapter });
|
|
736
|
+
if (!there) return false;
|
|
737
|
+
begin();
|
|
738
|
+
writable(path);
|
|
739
|
+
rmSync(path, { recursive: true });
|
|
740
|
+
return true;
|
|
741
|
+
});
|
|
742
|
+
for (const kind of ['judges', 'escalations']) {
|
|
743
|
+
const parentPath = join(root, kind);
|
|
744
|
+
const topicPath = join(parentPath, name);
|
|
745
|
+
try {
|
|
746
|
+
const parent = lstatOrAbsent(parentPath);
|
|
747
|
+
if (parent !== undefined && !parent.isDirectory()) continue;
|
|
748
|
+
const topicDirectory = lstatOrAbsent(topicPath);
|
|
749
|
+
if ((parent === undefined || parent.isDirectory()) && (topicDirectory === undefined || topicDirectory.isDirectory())) {
|
|
750
|
+
const base = nearestReal(topicPath);
|
|
751
|
+
for (const record of await adapter.listing()) {
|
|
752
|
+
if (record.locked === undefined || lstatOrAbsent(record.at) !== undefined) continue;
|
|
753
|
+
const at = nearestReal(record.at);
|
|
754
|
+
if (!underOrAt(at, base) || at === base) continue;
|
|
755
|
+
const role = relative(base, at).split(sep)[0];
|
|
756
|
+
if (record.detached && record.locked === ADDING) {
|
|
757
|
+
await adapter.unlock(record.at);
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
const failure = new Error(`${record.at} is registered with a person's lock, ${JSON.stringify(record.locked)}`);
|
|
761
|
+
left(record.at, failure.message, role);
|
|
762
|
+
failures.push(failure);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
} catch (failure) {
|
|
766
|
+
if (failure.code === EVENT_REFUSED) throw failure;
|
|
767
|
+
left(topicPath, `git could not inspect gone ${kind} worktrees: ${failure.message}`);
|
|
768
|
+
failures.push(failure);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
try {
|
|
772
|
+
await adapter.prune();
|
|
773
|
+
} catch (failure) {
|
|
774
|
+
if (failure.code === EVENT_REFUSED) throw failure;
|
|
775
|
+
left(workspace, `git could not prune gone worktree registrations: ${failure.message}`);
|
|
776
|
+
failures.push(failure);
|
|
777
|
+
}
|
|
778
|
+
if (failures.length > 0) throw new AggregateError(failures, `L1 could not remove all directories of card #${card}`);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* Takes away the lock on a registration at `path`, whose directory is gone, where git lists it on
|
|
783
|
+
* exactly `branch` and locked with `ADDING`, the reason `git worktree add` writes while it makes a
|
|
784
|
+
* worktree: what an engine killed during that command leaves, which fails every later attempt
|
|
785
|
+
* until unlocked (the architect's ruling 9 on #423, observation 6). L0's make then prunes it.
|
|
786
|
+
* Locked on another branch, or with another reason, a person's lock, it fails the attempt naming
|
|
787
|
+
* the path and the reason, and changes nothing.
|
|
788
|
+
*/
|
|
789
|
+
async function unlockedIfAdding(adapter, path, branch) {
|
|
790
|
+
const held = await adapter.registration(path);
|
|
791
|
+
if (held?.locked === undefined) return;
|
|
792
|
+
if (held.branch !== branch || held.locked !== ADDING) {
|
|
793
|
+
const on = held.detached ? 'a detached HEAD' : `the branch ${held.branch}`;
|
|
794
|
+
throw new Error(`${path} is registered as a worktree whose directory is gone, on ${on}, locked with the reason ${JSON.stringify(held.locked)}, so L1 leaves its lock in place`);
|
|
795
|
+
}
|
|
796
|
+
await adapter.unlock(path);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
/**
|
|
800
|
+
* Makes `path` and every directory under it owner-readable, -writable and -searchable, `u+rwx`,
|
|
801
|
+
* each before descending into it, so that `git worktree remove --force` can delete what an attempt
|
|
802
|
+
* left. Removing an entry needs write and search permission on its parent directory, not on the
|
|
803
|
+
* entry, so no file's mode is changed, and a hard link to a file outside the workspace keeps its
|
|
804
|
+
* mode. A symbolic link is not followed. Git otherwise drops the registration and then fails to
|
|
805
|
+
* delete the directory, exiting 255 with `failed to delete '<path>': Permission denied` (measured
|
|
806
|
+
* with git 2.54.0, j448-10), and every later attempt refuses the plain directory left behind.
|
|
807
|
+
* Called only once the card's own workspace has passed the replace rule.
|
|
808
|
+
*/
|
|
809
|
+
function writable(path) {
|
|
810
|
+
const held = lstatSync(path);
|
|
811
|
+
if (!held.isDirectory()) return;
|
|
812
|
+
chmodSync(path, held.mode | 0o700);
|
|
813
|
+
for (const name of readdirSync(path)) writable(join(path, name));
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* Whether the worktree git lists at `listed` lies inside the card's workspace at `path`, whose real
|
|
818
|
+
* path is `real`, compared by the file system's own real path, `realpath(3)`, whatever spelling git
|
|
819
|
+
* lists it under. On a volume that folds case, Node's JavaScript `realpathSync` keeps the case it
|
|
820
|
+
* was handed, so two spellings of one directory would compare as two.
|
|
821
|
+
*
|
|
822
|
+
* A worktree reached only through a symbolic link inside the workspace lies where the link points,
|
|
823
|
+
* and a registration whose directory is gone, `ENOENT`, has no real path and lies nowhere. Any
|
|
824
|
+
* other failure to resolve it, such as a directory on the way that cannot be searched, fails the
|
|
825
|
+
* attempt naming the path, since L1 cannot tell that it lies outside.
|
|
826
|
+
*/
|
|
827
|
+
function within(path, real, listed) {
|
|
828
|
+
let there;
|
|
829
|
+
try {
|
|
830
|
+
there = realpathSync.native(listed);
|
|
831
|
+
} catch (error) {
|
|
832
|
+
if (error.code === 'ENOENT') return false;
|
|
833
|
+
throw new Error(`${path} may hold the worktree git lists at ${listed}, which L1 could not resolve, so L1 leaves it as it is: ${error.message}`, { cause: error });
|
|
834
|
+
}
|
|
835
|
+
const from = relative(real, there);
|
|
836
|
+
return from !== '' && !from.startsWith(`..${sep}`) && from !== '..' && !isAbsolute(from);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
/** Whether anything is at `path`, a symbolic link to nothing included. */
|
|
840
|
+
function present(path) {
|
|
841
|
+
return lstatOrAbsent(path) !== undefined;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* The failure for `what`, at `fields.path`, which `cause` kept L1 from making, once L1 has recorded
|
|
846
|
+
* it through `events` as a `workspace.failed` carrying `fields` and why, or has added to it that the
|
|
847
|
+
* sink refused the record.
|
|
848
|
+
*
|
|
849
|
+
* Where the sink refused an event, L0's that `cause` carries or L1's `workspace.failed`, the
|
|
850
|
+
* failure has the code `EVENT_REFUSED` and carries each such event in `unrecorded`: a refused
|
|
851
|
+
* event is the halt, not a workspace L1 could not make (the owner's O4 on #423).
|
|
852
|
+
*/
|
|
853
|
+
function notMade(events, what, fields, cause, report = { code: WORKSPACE_NOT_MADE }) {
|
|
854
|
+
const { path } = fields;
|
|
855
|
+
const requirement = report.requirement === undefined ? '' : `${report.requirement} ${report.condition}: `;
|
|
856
|
+
const failure = Object.assign(new Error(`L1 could not make ${what} at ${path}: ${requirement}${cause.message}`, { cause }), { code: report.code, path });
|
|
857
|
+
const unrecorded = cause.code === EVENT_REFUSED ? [...cause.unrecorded] : [];
|
|
858
|
+
try {
|
|
859
|
+
events.emit('workspace.failed', { ...fields, reason: cause.message });
|
|
860
|
+
} catch (refusal) {
|
|
861
|
+
failure.message += `\nand the sink refused L1's workspace.failed, so it went unrecorded: ${refusal.message}`;
|
|
862
|
+
unrecorded.push({ event: 'workspace.failed', ...fields, reason: cause.message, cause: refusal });
|
|
863
|
+
}
|
|
864
|
+
if (unrecorded.length > 0) Object.assign(failure, { code: EVENT_REFUSED, unrecorded });
|
|
865
|
+
return failure;
|
|
866
|
+
}
|