@rarog/server 1.1.1 → 1.1.2-beta.2
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/node_modules/@rarog/shared/package.json +1 -1
- package/node_modules/@rarog/shared/src/decode.js +46 -1
- package/node_modules/@rarog/shared/src/notify-copy.js +2 -0
- package/node_modules/@rarog/shared/src/protocol/background.js +7 -0
- package/node_modules/@rarog/shared/src/protocol/context.js +57 -0
- package/node_modules/@rarog/shared/src/protocol/coordination.js +42 -0
- package/node_modules/@rarog/shared/src/protocol/dialog-live.js +117 -0
- package/node_modules/@rarog/shared/src/protocol/files.js +1 -1
- package/node_modules/@rarog/shared/src/protocol/inbox.js +6 -0
- package/node_modules/@rarog/shared/src/protocol/presence.js +47 -0
- package/node_modules/@rarog/shared/src/protocol.js +14 -146
- package/node_modules/@rarog/shared/src/rest.js +16 -1
- package/node_modules/@rarog/shared/src/sentences/dialogs.js +10 -0
- package/node_modules/@rarog/shared/src/sentences/push.js +9 -0
- package/node_modules/@rarog/shared/src/sentences/rest.js +9 -0
- package/node_modules/@rarog/shared/src/sentences/restart.js +1 -0
- package/node_modules/@rarog/shared/src/sentences/session.js +24 -0
- package/package.json +2 -2
- package/plugin/rarog/.claude-plugin/plugin.json +4 -0
- package/plugin/rarog/bin/rarog-coordinator +7 -0
- package/plugin/rarog/hooks/hooks.json +18 -0
- package/plugin/rarog/hooks/recipient.mjs +95 -0
- package/plugin/rarog/lib/coordinator.mjs +1340 -0
- package/plugin/rarog/lib/threads.mjs +350 -0
- package/plugin/rarog/skills/coordinator/SKILL.md +111 -0
- package/plugin/rarog/skills/coordinator/check-and-merge.md +46 -0
- package/plugin/rarog/skills/coordinator/close-thread.md +15 -0
- package/plugin/rarog/skills/coordinator/role.md +61 -0
- package/plugin/rarog/skills/coordinator/send-back.md +39 -0
- package/plugin/rarog/skills/coordinator/start-thread.md +43 -0
- package/server/src/bootstrap/coordination.js +49 -0
- package/server/src/bootstrap/coordinator-start.js +27 -0
- package/server/src/bootstrap/inbox.js +4 -0
- package/server/src/bootstrap/pr-status.js +15 -7
- package/server/src/bootstrap/push.js +59 -5
- package/server/src/cli/doctor-pr-status.js +10 -6
- package/server/src/companion/catalog.js +1 -1
- package/server/src/companion/index.js +2 -4
- package/server/src/config/flags.js +4 -0
- package/server/src/config-env.js +15 -0
- package/server/src/config.js +17 -3
- package/server/src/coordination/merge-events.js +86 -0
- package/server/src/coordination/merge-prompt.js +244 -0
- package/server/src/coordination/registry.js +289 -0
- package/server/src/coordination/state.js +94 -0
- package/server/src/coordination/summary.js +422 -0
- package/server/src/coordination/thread-file.js +245 -0
- package/server/src/coordination/thread-git.js +110 -0
- package/server/src/coordination/watch.js +237 -0
- package/server/src/demo/content/coordinator.js +89 -0
- package/server/src/demo/content/en.js +57 -1
- package/server/src/demo/content/pl.js +57 -1
- package/server/src/demo/control.js +4 -2
- package/server/src/demo/inbox.js +12 -0
- package/server/src/demo/mock-herdr.js +5 -4
- package/server/src/demo/review-routes.js +2 -2
- package/server/src/demo/scenarios.js +31 -17
- package/server/src/demo/seeds.js +82 -40
- package/server/src/drivers/herdr/live-dialog.js +412 -0
- package/server/src/drivers/herdr/live-step.js +261 -0
- package/server/src/drivers/herdr/pause-gate.js +60 -0
- package/server/src/drivers/herdr/pull-request-poll.js +5 -5
- package/server/src/drivers/herdr/queue-take-back.js +4 -0
- package/server/src/drivers/herdr/returned-prompt.js +4 -1
- package/server/src/drivers/herdr/screen-fallback.js +50 -0
- package/server/src/drivers/herdr/start-dialog-gate.js +136 -0
- package/server/src/drivers/herdr-driver.js +159 -77
- package/server/src/files/refusals.js +9 -2
- package/server/src/files/session-gate.js +5 -4
- package/server/src/git-branch-diff.js +194 -0
- package/server/src/herdr/argv.js +34 -3
- package/server/src/herdr/audit.js +32 -1
- package/server/src/herdr/background/cli-shapes.js +35 -15
- package/server/src/herdr/background/index.js +7 -7
- package/server/src/herdr/background/merge.js +17 -5
- package/server/src/herdr/background/notification.js +0 -4
- package/server/src/herdr/background/pull-request-files.js +53 -161
- package/server/src/herdr/background/pull-requests.js +54 -632
- package/server/src/herdr/background/queued-prompt.js +46 -0
- package/server/src/herdr/background/work.js +96 -0
- package/server/src/herdr/client.js +22 -46
- package/server/src/herdr/commands/catalog.js +11 -7
- package/server/src/herdr/commands/discover.js +33 -1
- package/server/src/herdr/commands/index.js +3 -2
- package/server/src/herdr/dialogs/copy.js +7 -0
- package/server/src/herdr/dialogs/index.js +13 -1
- package/server/src/herdr/dialogs/layer1/hook-list/hook-list.js +4 -56
- package/server/src/herdr/dialogs/live/model.js +220 -0
- package/server/src/herdr/dialogs/live/policy.js +108 -0
- package/server/src/herdr/dialogs/live/view.js +261 -0
- package/server/src/herdr/dialogs/screen/form.js +6 -4
- package/server/src/herdr/dialogs/screen/option-run.js +25 -3
- package/server/src/herdr/dialogs/screen/parse.js +3 -3
- package/server/src/herdr/dialogs/screen/signatures.js +78 -0
- package/server/src/herdr/dialogs/screen/start-dialog.js +72 -0
- package/server/src/herdr/dialogs/screen/trust-arrow.js +46 -9
- package/server/src/herdr/engines.js +96 -0
- package/server/src/herdr/history-steps.js +9 -4
- package/server/src/herdr/identity.js +20 -5
- package/server/src/herdr/jsonl-follow.js +26 -3
- package/server/src/herdr/label-code.js +104 -0
- package/server/src/herdr/label-migration.js +142 -0
- package/server/src/herdr/launcher.js +30 -5
- package/server/src/herdr/plugin-copy.js +281 -0
- package/server/src/herdr/provenance.js +80 -40
- package/server/src/herdr/session-index.js +19 -0
- package/server/src/herdr/start-dialog-wait.js +96 -0
- package/server/src/herdr/supervisor.js +49 -23
- package/server/src/herdr/types.js +1 -0
- package/server/src/herdr/workspace-resolver.js +5 -4
- package/server/src/inbox/card.js +31 -8
- package/server/src/inbox/heads.js +1 -0
- package/server/src/inbox/inbox.js +21 -4
- package/server/src/inbox/removals.js +10 -1
- package/server/src/index.js +45 -8
- package/server/src/notify/apns-payload.js +5 -3
- package/server/src/notify/content.js +64 -0
- package/server/src/notify/index.js +11 -3
- package/server/src/notify/live-activity-state.js +19 -6
- package/server/src/notify/live-activity.js +5 -2
- package/server/src/notify/policy.js +53 -13
- package/server/src/notify/ready-to-merge.js +148 -0
- package/server/src/notify/stalled-wait.js +213 -0
- package/server/src/pr-status/facts.js +87 -0
- package/server/src/pr-status/gh.js +76 -9
- package/server/src/pr-status/index.js +1 -10
- package/server/src/pr-status/source.js +20 -6
- package/server/src/pr-status/tracker.js +32 -9
- package/server/src/project-root.js +233 -0
- package/server/src/routes/sessions.js +68 -2
- package/server/src/sessions/branch-diff.js +57 -0
- package/server/src/sessions/cli-registry.js +17 -3
- package/server/src/sessions/companion-scope.js +2 -1
- package/server/src/sessions/consent-cards.js +49 -2
- package/server/src/sessions/conversation-lists.js +11 -2
- package/server/src/sessions/coordinator-start.js +172 -0
- package/server/src/sessions/demo-modules.js +1 -0
- package/server/src/sessions/demo-seeds.js +100 -0
- package/server/src/sessions/driver-events.js +68 -14
- package/server/src/sessions/herdr-lifecycle.js +11 -4
- package/server/src/sessions/interrupt-windows.js +28 -4
- package/server/src/sessions/launch-gate.js +36 -11
- package/server/src/sessions/live-dialog.js +69 -0
- package/server/src/sessions/manager.js +138 -287
- package/server/src/sessions/owner-turn.js +36 -0
- package/server/src/sessions/pr-status-link.js +64 -16
- package/server/src/sessions/seen-marks.js +31 -12
- package/server/src/sessions/session-dialog.js +82 -0
- package/server/src/sessions/session-git.js +1 -0
- package/server/src/sessions/session-scope.js +6 -5
- package/server/src/sessions/session-take-back.js +5 -0
- package/server/src/sessions/session.js +82 -10
- package/server/src/sessions/state-machine.js +13 -0
- package/server/src/sessions/state-sentences.js +87 -30
- package/server/src/sessions/test-session.js +50 -0
- package/server/src/sessions/waiting-line.js +49 -0
- package/server/src/term/herdr-backend.js +15 -12
- package/server/src/term/lifecycle.js +4 -6
- package/server/src/transcript-import.js +20 -2
- package/server/src/version.js +4 -0
- /package/server/src/{companion/asks-owner.js → asks-owner.js} +0 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provenance suffix of a Rarog pane label: `rt:<code>` for a terminal, `rc:<code><code>` for a
|
|
3
|
+
* Claude pane, hex values written in Crockford base32 (lowercase, most significant bit first, the
|
|
4
|
+
* last character padded with zero bits). Pure functions over strings, the writers and the closed
|
|
5
|
+
* patterns side by side. What the values mean (terminal id, project key, tab label trace) lives in
|
|
6
|
+
* `herdr/provenance.ts`, which reads and writes every pane label through this module.
|
|
7
|
+
*/
|
|
8
|
+
/** Crockford base32 in lowercase: digits and letters without `i`, `l`, `o`, `u`. */
|
|
9
|
+
const ALPHABET = '0123456789abcdefghjkmnpqrstvwxyz';
|
|
10
|
+
const CODE_CHAR = '[0-9a-hjkmnp-tv-z]';
|
|
11
|
+
const HEX_RE = /^[0-9a-f]+$/;
|
|
12
|
+
/** Hex digits of a terminal id and of a tab label trace. */
|
|
13
|
+
const TERM_ID_HEX = 16;
|
|
14
|
+
const TRACE_HEX = 8;
|
|
15
|
+
/** Characters of a project key code: the first 35 bits of the 64-bit key. */
|
|
16
|
+
export const PROJECT_KEY_CODE_LENGTH = 7;
|
|
17
|
+
const KEY_CODE_RE = new RegExp(`^${CODE_CHAR}{${String(PROJECT_KEY_CODE_LENGTH)}}$`);
|
|
18
|
+
/** Between the description and the suffix. */
|
|
19
|
+
const SEPARATOR = ' · ';
|
|
20
|
+
/** Characters of a code for `hexLength` hex digits: whole 5-bit characters. */
|
|
21
|
+
function codeLength(hexLength) {
|
|
22
|
+
return Math.ceil((hexLength * 4) / 5);
|
|
23
|
+
}
|
|
24
|
+
/** Lowercase hex → code; the last character is padded with zero bits. */
|
|
25
|
+
export function encodeHex(hex) {
|
|
26
|
+
if (!HEX_RE.test(hex))
|
|
27
|
+
throw new Error('Label code input is not lowercase hex.');
|
|
28
|
+
const length = codeLength(hex.length);
|
|
29
|
+
let value = BigInt(`0x${hex}`) << BigInt(length * 5 - hex.length * 4);
|
|
30
|
+
const chars = [];
|
|
31
|
+
for (let i = 0; i < length; i += 1) {
|
|
32
|
+
chars.unshift(ALPHABET.charAt(Number(value & 31n)));
|
|
33
|
+
value >>= 5n;
|
|
34
|
+
}
|
|
35
|
+
return chars.join('');
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Code → `hexLength` lowercase hex digits, or `null` for a code of another length, a character
|
|
39
|
+
* outside the alphabet or padding bits other than zero (so a value has exactly one code).
|
|
40
|
+
*/
|
|
41
|
+
export function decodeCode(code, hexLength) {
|
|
42
|
+
const length = codeLength(hexLength);
|
|
43
|
+
if (code.length !== length)
|
|
44
|
+
return null;
|
|
45
|
+
let value = 0n;
|
|
46
|
+
for (const char of code) {
|
|
47
|
+
const digit = ALPHABET.indexOf(char);
|
|
48
|
+
if (digit < 0)
|
|
49
|
+
return null;
|
|
50
|
+
value = (value << 5n) | BigInt(digit);
|
|
51
|
+
}
|
|
52
|
+
const padding = BigInt(length * 5 - hexLength * 4);
|
|
53
|
+
if ((value & ((1n << padding) - 1n)) !== 0n)
|
|
54
|
+
return null;
|
|
55
|
+
return (value >> padding).toString(16).padStart(hexLength, '0');
|
|
56
|
+
}
|
|
57
|
+
/** The code of a 16-hex project key as a Claude label carries it. */
|
|
58
|
+
export function keyCodeOf(key) {
|
|
59
|
+
return encodeHex(key).slice(0, PROJECT_KEY_CODE_LENGTH);
|
|
60
|
+
}
|
|
61
|
+
/** `rt:<13 characters>` — the whole terminal id, lossless. */
|
|
62
|
+
export function termLabelSuffix(id) {
|
|
63
|
+
if (id.length !== TERM_ID_HEX)
|
|
64
|
+
throw new Error('Terminal id is not 16 hex digits.');
|
|
65
|
+
return `rt:${encodeHex(id)}`;
|
|
66
|
+
}
|
|
67
|
+
/** `rc:<7 characters of the key><7 characters of the trace>` — the trace is lossless. */
|
|
68
|
+
export function claudeLabelSuffix(keyCode, trace) {
|
|
69
|
+
if (!KEY_CODE_RE.test(keyCode))
|
|
70
|
+
throw new Error('Project key code is not 7 label code characters.');
|
|
71
|
+
if (trace.length !== TRACE_HEX)
|
|
72
|
+
throw new Error('Tab label trace is not 8 hex digits.');
|
|
73
|
+
return `rc:${keyCode}${encodeHex(trace)}`;
|
|
74
|
+
}
|
|
75
|
+
/** A captured description is valid when absent (bare format) or not blank. */
|
|
76
|
+
export function descriptionOk(description) {
|
|
77
|
+
return description === undefined || /\S/u.test(description);
|
|
78
|
+
}
|
|
79
|
+
/** `<description> · <suffix>`, or the bare suffix when the description is empty. */
|
|
80
|
+
export function joinLabel(description, suffix) {
|
|
81
|
+
return description === '' ? suffix : `${description}${SEPARATOR}${suffix}`;
|
|
82
|
+
}
|
|
83
|
+
/*
|
|
84
|
+
* Closed patterns anchored at the end, with an optional leading description. The description is
|
|
85
|
+
* captured and checked in code (at least one non-whitespace character), so a separator alone
|
|
86
|
+
* stays foreign; one greedy group keeps the match linear on long labels.
|
|
87
|
+
*/
|
|
88
|
+
const TERM_LABEL_RE = new RegExp(`^(?:(.+)${SEPARATOR})?rt:(${CODE_CHAR}{${String(codeLength(TERM_ID_HEX))}})$`);
|
|
89
|
+
const CLAUDE_LABEL_RE = new RegExp(`^(?:(.+)${SEPARATOR})?rc:(${CODE_CHAR}{${String(PROJECT_KEY_CODE_LENGTH)}})`
|
|
90
|
+
+ `(${CODE_CHAR}{${String(codeLength(TRACE_HEX))}})$`);
|
|
91
|
+
/** A pane label in the current format → its suffix, or `null` for anything outside the patterns. */
|
|
92
|
+
export function readLabelSuffix(label) {
|
|
93
|
+
const term = TERM_LABEL_RE.exec(label);
|
|
94
|
+
if (term?.[2] !== undefined) {
|
|
95
|
+
const id = descriptionOk(term[1]) ? decodeCode(term[2], TERM_ID_HEX) : null;
|
|
96
|
+
return id === null ? null : { kind: 'term', id };
|
|
97
|
+
}
|
|
98
|
+
const claude = CLAUDE_LABEL_RE.exec(label);
|
|
99
|
+
if (claude?.[2] !== undefined && claude[3] !== undefined) {
|
|
100
|
+
const trace = descriptionOk(claude[1]) ? decodeCode(claude[3], TRACE_HEX) : null;
|
|
101
|
+
return trace === null ? null : { kind: 'claude', keyCode: claude[2], trace };
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pane labels in the old provenance format, the only code that knows it:
|
|
3
|
+
* `<description> · rarog-term <16 hex terminal id>` and
|
|
4
|
+
* `<description> · rarog-claude <16 hex project key> <8 hex tab label trace>`, the description optional.
|
|
5
|
+
*
|
|
6
|
+
* Two parts. {@link legacyToCurrent} is a pure normalizer: `parsePaneLabel` reads every label
|
|
7
|
+
* through it, so a pane with an old label is Rarog's for every reader. {@link createLabelMigration}
|
|
8
|
+
* is the pass that rewrites old labels in herdr to the current format, called by the supervisor
|
|
9
|
+
* whenever herdr answers. It writes nothing but `pane.rename` on a pane whose old label a fresh read
|
|
10
|
+
* has just confirmed, so it never meets another writer on the same label: every other writer
|
|
11
|
+
* confirms a label in the current format or no label at all.
|
|
12
|
+
*/
|
|
13
|
+
import { HerdrUnavailableError } from './socket.js';
|
|
14
|
+
import { HerdrReadOnlyError } from './types.js';
|
|
15
|
+
import { claudeLabelSuffix, descriptionOk, joinLabel, keyCodeOf, termLabelSuffix, } from './label-code.js';
|
|
16
|
+
const LEGACY_TERM_RE = /^(?:(.+) · )?rarog-term ([0-9a-f]{16})$/;
|
|
17
|
+
const LEGACY_CLAUDE_RE = /^(?:(.+) · )?rarog-claude ([0-9a-f]{16}) ([0-9a-f]{8})$/;
|
|
18
|
+
/**
|
|
19
|
+
* An old-format label → the same provenance in the current format, the description verbatim; any
|
|
20
|
+
* other label comes back unchanged. The terminal id and the trace stay whole, the project key
|
|
21
|
+
* becomes its code (the current format carries no more of it).
|
|
22
|
+
*/
|
|
23
|
+
export function legacyToCurrent(label) {
|
|
24
|
+
const term = LEGACY_TERM_RE.exec(label);
|
|
25
|
+
if (term?.[2] !== undefined && descriptionOk(term[1])) {
|
|
26
|
+
return joinLabel(term[1] ?? '', termLabelSuffix(term[2]));
|
|
27
|
+
}
|
|
28
|
+
const claude = LEGACY_CLAUDE_RE.exec(label);
|
|
29
|
+
if (claude?.[2] !== undefined && claude[3] !== undefined && descriptionOk(claude[1])) {
|
|
30
|
+
return joinLabel(claude[1] ?? '', claudeLabelSuffix(keyCodeOf(claude[2]), claude[3]));
|
|
31
|
+
}
|
|
32
|
+
return label;
|
|
33
|
+
}
|
|
34
|
+
/** The label is in the old format: the migration pass is the one to rewrite it. */
|
|
35
|
+
export function isLegacyPaneLabel(label) {
|
|
36
|
+
return label !== null && legacyToCurrent(label) !== label;
|
|
37
|
+
}
|
|
38
|
+
const messageOf = (err) => (err instanceof Error ? err.message : String(err));
|
|
39
|
+
export function createLabelMigration(config) {
|
|
40
|
+
const { client, writable, log } = config;
|
|
41
|
+
let due = true;
|
|
42
|
+
let disposed = false;
|
|
43
|
+
let inFlight = null;
|
|
44
|
+
/** `pane|label` that herdr refused to rename (pane gone, a label the socket rejects): not retried. */
|
|
45
|
+
const refused = new Set();
|
|
46
|
+
const logged = new Set();
|
|
47
|
+
const logOnce = (tag, line) => {
|
|
48
|
+
if (logged.has(tag))
|
|
49
|
+
return;
|
|
50
|
+
logged.add(tag);
|
|
51
|
+
log(line);
|
|
52
|
+
};
|
|
53
|
+
const pass = async () => {
|
|
54
|
+
if (!writable()) {
|
|
55
|
+
logOnce('read-only', 'herdr: pane label migration on hold — write channel closed (RAROG_HERDR_SEND=0)');
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
due = false;
|
|
59
|
+
let panes;
|
|
60
|
+
try {
|
|
61
|
+
panes = await client.listPanes();
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
due = true;
|
|
65
|
+
log(`herdr: pane label migration could not read the panes (${messageOf(err)})`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const old = panes.filter((pane) => isLegacyPaneLabel(pane.label)
|
|
69
|
+
&& !refused.has(`${pane.paneId}|${pane.label ?? ''}`));
|
|
70
|
+
if (old.length === 0)
|
|
71
|
+
return;
|
|
72
|
+
let rewritten = 0;
|
|
73
|
+
for (const pane of old) {
|
|
74
|
+
if (disposed)
|
|
75
|
+
return;
|
|
76
|
+
if (!writable()) {
|
|
77
|
+
due = true;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
const label = pane.label ?? '';
|
|
81
|
+
let current;
|
|
82
|
+
try {
|
|
83
|
+
current = (await client.listPanes()).find((candidate) => candidate.paneId === pane.paneId)?.label;
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
due = true;
|
|
87
|
+
log(`herdr: pane ${pane.paneId} not confirmed before its label rewrite (${messageOf(err)})`);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
if (disposed)
|
|
91
|
+
return;
|
|
92
|
+
// Addresses are positional and a human may rename a pane: only the label read above is rewritten.
|
|
93
|
+
if (current !== label) {
|
|
94
|
+
// A pane gone from its address may live on under another one: the next pass reads the whole list again.
|
|
95
|
+
if (current === undefined || isLegacyPaneLabel(current))
|
|
96
|
+
due = true;
|
|
97
|
+
log(`herdr: pane ${pane.paneId} changed before its label rewrite — skipping`);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
await client.renamePane(pane.paneId, legacyToCurrent(label));
|
|
102
|
+
rewritten += 1;
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
if (err instanceof HerdrReadOnlyError || err instanceof HerdrUnavailableError) {
|
|
106
|
+
// The write gate, or a timeout that does not say whether the rename landed: the next pass reads again.
|
|
107
|
+
due = true;
|
|
108
|
+
log(`herdr: label rewrite of pane ${pane.paneId} — not done or result unknown (${messageOf(err)})`);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
refused.add(`${pane.paneId}|${label}`);
|
|
112
|
+
log(`herdr: label rewrite of pane ${pane.paneId} failed (${messageOf(err)})`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
log(`herdr: pane label migration — ${String(rewritten)} of ${String(old.length)} old-format pane labels rewritten`);
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
run() {
|
|
119
|
+
if (inFlight !== null)
|
|
120
|
+
return inFlight;
|
|
121
|
+
if (disposed || !due)
|
|
122
|
+
return Promise.resolve();
|
|
123
|
+
const current = pass()
|
|
124
|
+
.catch((err) => {
|
|
125
|
+
due = true;
|
|
126
|
+
log(`herdr: pane label migration threw an error — ${messageOf(err)}`);
|
|
127
|
+
})
|
|
128
|
+
.finally(() => {
|
|
129
|
+
if (inFlight === current)
|
|
130
|
+
inFlight = null;
|
|
131
|
+
});
|
|
132
|
+
inFlight = current;
|
|
133
|
+
return current;
|
|
134
|
+
},
|
|
135
|
+
requestPass() {
|
|
136
|
+
due = true;
|
|
137
|
+
},
|
|
138
|
+
dispose() {
|
|
139
|
+
disposed = true;
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
* 6. `agent.start`, etykieta pochodzenia panelu (`pane.rename`) i sygnał „etykieta gotowa"
|
|
21
21
|
* dla nadzorcy (`paneLabelled` — od niego rusza synchronizacja etykiety taba z kartą), tytuł
|
|
22
22
|
* (`pane.report_metadata`), poll `agent.get` — w TLE, rozstrzygają `ready`.
|
|
23
|
+
* A start dialog herdr reads as ready ends the poll as `blocked` (`start-dialog-wait.ts`).
|
|
23
24
|
*
|
|
24
25
|
* Launcher nie emituje żadnych zdarzeń cyklu życia i nie zna managera.
|
|
25
26
|
*/
|
|
@@ -29,8 +30,9 @@ import { foreign, say } from '@rarog/shared/sentences';
|
|
|
29
30
|
import { DEFAULT_CLAUDE_ENV_CLEAR, HERDR_LAUNCH_POLL_MS, HERDR_LAUNCH_READY_TIMEOUT_MS, HERDR_LAUNCH_SHELL_WAIT_MS, PANE_SHELL_ENV, } from '../config.js';
|
|
30
31
|
import { assertAllowedArgs, buildClaudeArgs } from './argv.js';
|
|
31
32
|
import { decideProjectWorkspace, sharedProjectPlacement } from './project-workspace.js';
|
|
32
|
-
import {
|
|
33
|
+
import { claudePaneLabelFrom, newClaudeProvenance, newRarogAgentName, realProjectPath, sanitizePaneTitle, } from './provenance.js';
|
|
33
34
|
import { snippet } from './dialogs/screen/snippet.js';
|
|
35
|
+
import { createStartDialogWatch, startDialogSample, watchStartDialogSession } from './start-dialog-wait.js';
|
|
34
36
|
// `HerdrUntrustedCwdError` NIE jest tu importowany: brak zaufania katalogu jest
|
|
35
37
|
// ostrzeżeniem (`trustPrompt`), a nie odmową startu. Klasa zostaje w `types.ts` razem
|
|
36
38
|
// z tłumaczeniem na kod `herdr-untrusted` w routerze — patrz jej docstring.
|
|
@@ -305,6 +307,7 @@ export function createHerdrLauncher(deps) {
|
|
|
305
307
|
*/
|
|
306
308
|
const waitReady = async (paneId, sessionId, start) => {
|
|
307
309
|
const deadline = now() + readyTimeoutMs;
|
|
310
|
+
const startDialogs = createStartDialogWatch();
|
|
308
311
|
/** Rozjazd `--session-id` znaczy, że ścieżka jsonl byłaby zgadywaniem — to porażka startu. */
|
|
309
312
|
const assertSameSession = async (probe) => {
|
|
310
313
|
if (probe.agentSessionId === sessionId)
|
|
@@ -374,7 +377,20 @@ export function createHerdrLauncher(deps) {
|
|
|
374
377
|
// `agent_session` przy dialogu bywa jeszcze puste — sprawdzamy tylko ROZJAZD.
|
|
375
378
|
if (probe.agentSessionId !== '')
|
|
376
379
|
await assertSameSession(probe);
|
|
377
|
-
|
|
380
|
+
// Without the session id herdr may never push the answer (trust, then a start dialog herdr reads as ready).
|
|
381
|
+
const pending = probe.agentSessionId === '' ? { sessionPending: true } : {};
|
|
382
|
+
return { status: 'blocked', interactiveReady: false, ...pending };
|
|
383
|
+
}
|
|
384
|
+
// A start dialog herdr reads as ready: the same `blocked` as above, so the card and the waiting start prompt
|
|
385
|
+
// take the trust dialog's path. A failed read proves nothing and leaves the poll as it was.
|
|
386
|
+
if (deps.readScreen !== undefined && startDialogSample(probe)) {
|
|
387
|
+
const screen = await deps.readScreen(paneId).catch(() => null);
|
|
388
|
+
const startDialog = startDialogs.observe(screen, now());
|
|
389
|
+
if (startDialog !== null) {
|
|
390
|
+
log(`herdr launcher: pane ${paneId} stands on a start dialog herdr reads as ready `
|
|
391
|
+
+ `(${startDialog.id ?? 'unknown'}).`);
|
|
392
|
+
return { status: 'blocked', interactiveReady: false, startDialog, sessionPending: true };
|
|
393
|
+
}
|
|
378
394
|
}
|
|
379
395
|
if (now() >= deadline) {
|
|
380
396
|
// Panel ZOSTAJE (patrz `finish`), więc karta musi mówić, że jest na czym pracować —
|
|
@@ -514,10 +530,10 @@ export function createHerdrLauncher(deps) {
|
|
|
514
530
|
/*
|
|
515
531
|
* Krok 6: pochodzenie i tytuł panelu — oba BEST EFFORT, żaden nie przerywa startu.
|
|
516
532
|
*
|
|
517
|
-
* The pane label leads with the tab label set above (`<title> ·
|
|
533
|
+
* The pane label leads with the tab label set above (`<title> · rc:…`), so
|
|
518
534
|
* herdr's Go-to window names the pane like its tab.
|
|
519
535
|
*
|
|
520
|
-
* Etykieta PANELU `
|
|
536
|
+
* Etykieta PANELU `rc:<kod klucza projektu><kod śladu etykiety taba>` jest jedyną rzeczą
|
|
521
537
|
* Raroga, którą herdr trzyma w snapshotcie (przeżywa jego restart) — dowód „ten panel
|
|
522
538
|
* założył Rarog dla tego projektu". Idzie PO `agent.start`, na panelu, który launcher
|
|
523
539
|
* sam przed chwilą stworzył; herdr zostawia ją przy wykryciu agenta, przy jego wyjściu
|
|
@@ -525,7 +541,8 @@ export function createHerdrLauncher(deps) {
|
|
|
525
541
|
* nazwa agenta `rarog-<6 hex>` i klucz zarezerwowany w nadzorcy (`registerLaunch`).
|
|
526
542
|
*/
|
|
527
543
|
try {
|
|
528
|
-
|
|
544
|
+
const label = claudePaneLabelFrom(newClaudeProvenance(realCwd, title), title, 'keep');
|
|
545
|
+
await client.renamePane(paneId, label);
|
|
529
546
|
}
|
|
530
547
|
catch (err) {
|
|
531
548
|
log(`herdr launcher: failed to set the provenance label on pane ${paneId} `
|
|
@@ -544,10 +561,18 @@ export function createHerdrLauncher(deps) {
|
|
|
544
561
|
// Kroki 7–8. Timeout NIE sprząta: panel może stać na dialogu i za chwilę ruszyć,
|
|
545
562
|
// a zabicie go byłoby gorsze niż wisząca karta.
|
|
546
563
|
const settled = await waitReady(paneId, sessionId, startKind);
|
|
564
|
+
const { sessionAppeared, launchHeld } = deps;
|
|
565
|
+
if (settled.sessionPending === true && sessionAppeared !== undefined) {
|
|
566
|
+
void watchStartDialogSession({
|
|
567
|
+
key, paneId, probe: probeAgent, held: (k) => launchHeld?.(k) ?? true,
|
|
568
|
+
appeared: sessionAppeared, sleep, now, log,
|
|
569
|
+
});
|
|
570
|
+
}
|
|
547
571
|
return {
|
|
548
572
|
key, paneId, tabId, agentSessionId: sessionId, createdWorkspace, argv,
|
|
549
573
|
status: settled.status,
|
|
550
574
|
interactiveReady: settled.interactiveReady,
|
|
575
|
+
...(settled.startDialog === undefined ? {} : { startDialog: settled.startDialog }),
|
|
551
576
|
};
|
|
552
577
|
};
|
|
553
578
|
// Kroki 6–8 dopiero po oddaniu uchwytu (patrz `defer`): manager ma zdążyć odpowiedzieć
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copy of the coordinator plugin in the data directory, one per package version:
|
|
3
|
+
* `<dataDir>/plugins/coordinator/<version>/`, the directory a coordinator gets in `--plugin-dir` (`argv.ts`).
|
|
4
|
+
*
|
|
5
|
+
* The package carries the plugin as `plugin/rarog/` next to its `package.json`; an update of the package replaces
|
|
6
|
+
* those files, so a live coordinator never runs from the package itself. {@link ensurePluginCopy} reads the plugin and
|
|
7
|
+
* the version from the package on disk at that moment, so the copy and its directory name come from the same package.
|
|
8
|
+
* A copy whose content hash (relative path, execute bit and bytes of every file) differs from the package is written
|
|
9
|
+
* again; copies of other versions go once no live session uses them ({@link prunePluginCopies}).
|
|
10
|
+
*
|
|
11
|
+
* The module does not decide who is a coordinator, does not start anything and does not know live sessions: the
|
|
12
|
+
* caller passes the `--plugin-dir` values of live sessions, or `null` when it does not know them.
|
|
13
|
+
*/
|
|
14
|
+
import { randomBytes, createHash } from 'node:crypto';
|
|
15
|
+
import { chmodSync, lstatSync, mkdirSync, readdirSync, renameSync, rmSync, writeFileSync, } from 'node:fs';
|
|
16
|
+
import path from 'node:path';
|
|
17
|
+
import { INSTALL_ROOT } from '../install/mode.js';
|
|
18
|
+
import { readRegularFileSync } from '../safe-read.js';
|
|
19
|
+
import { readPackageVersion } from '../version.js';
|
|
20
|
+
import { isPluginDirValue } from './argv.js';
|
|
21
|
+
/** The plugin directory under the install root (the package and the clone have the same layout). */
|
|
22
|
+
export const PLUGIN_SOURCE_SUBDIR = ['plugin', 'rarog'];
|
|
23
|
+
/** The copies' directory under the data directory. */
|
|
24
|
+
export const PLUGIN_COPY_SUBDIR = ['plugins', 'coordinator'];
|
|
25
|
+
/** Mode of every directory of the copy: like the data directory, nobody else writes code that runs from it. */
|
|
26
|
+
export const PLUGIN_COPY_DIR_MODE = 0o700;
|
|
27
|
+
/** Ceilings of the plugin tree: the plugin is a dozen small files, so more means a wrong directory. */
|
|
28
|
+
export const PLUGIN_COPY_MAX_FILES = 64;
|
|
29
|
+
export const PLUGIN_COPY_MAX_BYTES = 2 * 1024 * 1024;
|
|
30
|
+
export const PLUGIN_COPY_MAX_DEPTH = 8;
|
|
31
|
+
/** Work directory of a copy being written; a leftover one is removed by {@link prunePluginCopies}. */
|
|
32
|
+
const TMP_PREFIX = '.tmp-';
|
|
33
|
+
/** A replaced copy on its way out. */
|
|
34
|
+
const OLD_PREFIX = '.old-';
|
|
35
|
+
/** A version directory name: the `version` alphabet of `package.json` (`version.ts`), never `.` or `..`. */
|
|
36
|
+
const VERSION_NAME = /^[0-9A-Za-z.+-]{1,64}$/;
|
|
37
|
+
const isVersionName = (name) => VERSION_NAME.test(name) && name !== '.' && name !== '..';
|
|
38
|
+
function codeOf(failure) {
|
|
39
|
+
const code = failure?.code;
|
|
40
|
+
return typeof code === 'string' ? code : failure instanceof Error ? failure.message : String(failure);
|
|
41
|
+
}
|
|
42
|
+
/** Hash of the tree: for every file its path, execute bit, length and bytes, in path order. */
|
|
43
|
+
function treeHash(files) {
|
|
44
|
+
const hash = createHash('sha256');
|
|
45
|
+
for (const file of files) {
|
|
46
|
+
hash.update(`${file.rel}\0${file.exec ? 'x' : '-'}\0${String(file.bytes.length)}\0`).update(file.bytes);
|
|
47
|
+
}
|
|
48
|
+
return hash.digest('hex');
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Every regular file under `root`, with its bytes. A symlink, FIFO, device or socket anywhere in the tree, a root
|
|
52
|
+
* that is not a real directory, or a tree over the ceilings gives a reason instead: the copy runs outside the
|
|
53
|
+
* sandbox, so it carries exactly what the package has, and a FIFO must not hang the server (`safe-read.ts`).
|
|
54
|
+
*/
|
|
55
|
+
export function readPluginTree(root) {
|
|
56
|
+
const files = [];
|
|
57
|
+
let bytes = 0;
|
|
58
|
+
const walk = (rel, depth) => {
|
|
59
|
+
const dir = path.join(root, rel);
|
|
60
|
+
let names;
|
|
61
|
+
try {
|
|
62
|
+
names = readdirSync(dir).sort();
|
|
63
|
+
}
|
|
64
|
+
catch (failure) {
|
|
65
|
+
return `${dir}: ${codeOf(failure)}`;
|
|
66
|
+
}
|
|
67
|
+
for (const name of names) {
|
|
68
|
+
const childRel = rel === '' ? name : `${rel}/${name}`;
|
|
69
|
+
const child = path.join(root, childRel);
|
|
70
|
+
let info;
|
|
71
|
+
try {
|
|
72
|
+
info = lstatSync(child);
|
|
73
|
+
}
|
|
74
|
+
catch (failure) {
|
|
75
|
+
return `${child}: ${codeOf(failure)}`;
|
|
76
|
+
}
|
|
77
|
+
if (info.isDirectory()) {
|
|
78
|
+
if (depth >= PLUGIN_COPY_MAX_DEPTH)
|
|
79
|
+
return `${child}: deeper than ${String(PLUGIN_COPY_MAX_DEPTH)} levels`;
|
|
80
|
+
const issue = walk(childRel, depth + 1);
|
|
81
|
+
if (issue !== null)
|
|
82
|
+
return issue;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (!info.isFile())
|
|
86
|
+
return `${child}: not a regular file or directory`;
|
|
87
|
+
if (files.length >= PLUGIN_COPY_MAX_FILES)
|
|
88
|
+
return `${root}: more than ${String(PLUGIN_COPY_MAX_FILES)} files`;
|
|
89
|
+
let read;
|
|
90
|
+
try {
|
|
91
|
+
read = readRegularFileSync(child, { maxBytes: PLUGIN_COPY_MAX_BYTES - bytes, followSymlink: false });
|
|
92
|
+
}
|
|
93
|
+
catch (failure) {
|
|
94
|
+
return `${child}: ${codeOf(failure)}`;
|
|
95
|
+
}
|
|
96
|
+
if (read === null)
|
|
97
|
+
return `${child}: not a regular file`;
|
|
98
|
+
if (read.truncated)
|
|
99
|
+
return `${root}: more than ${String(PLUGIN_COPY_MAX_BYTES)} bytes`;
|
|
100
|
+
bytes += read.bytes.length;
|
|
101
|
+
files.push({ rel: childRel, bytes: read.bytes, exec: (info.mode & 0o100) !== 0 });
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
};
|
|
105
|
+
let info;
|
|
106
|
+
try {
|
|
107
|
+
info = lstatSync(root);
|
|
108
|
+
}
|
|
109
|
+
catch (failure) {
|
|
110
|
+
return { ok: false, reason: `${root}: ${codeOf(failure)}` };
|
|
111
|
+
}
|
|
112
|
+
if (!info.isDirectory())
|
|
113
|
+
return { ok: false, reason: `${root}: not a directory (a symlink is not accepted)` };
|
|
114
|
+
const issue = walk('', 1);
|
|
115
|
+
if (issue !== null)
|
|
116
|
+
return { ok: false, reason: issue };
|
|
117
|
+
files.sort((a, b) => (a.rel < b.rel ? -1 : a.rel > b.rel ? 1 : 0));
|
|
118
|
+
return { ok: true, files, hash: treeHash(files) };
|
|
119
|
+
}
|
|
120
|
+
/** `<dataDir>/plugins/coordinator`. */
|
|
121
|
+
export function pluginCopyRoot(dataDir) {
|
|
122
|
+
return path.join(dataDir, ...PLUGIN_COPY_SUBDIR);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Why a directory on the way to the copies cannot hold them, or `null`: it must be a real directory (not a symlink)
|
|
126
|
+
* of this user that nobody else can write.
|
|
127
|
+
*/
|
|
128
|
+
function dirIssue(dir) {
|
|
129
|
+
let info;
|
|
130
|
+
try {
|
|
131
|
+
info = lstatSync(dir);
|
|
132
|
+
}
|
|
133
|
+
catch (failure) {
|
|
134
|
+
return `${dir}: ${codeOf(failure)}`;
|
|
135
|
+
}
|
|
136
|
+
if (!info.isDirectory())
|
|
137
|
+
return `${dir}: expected a directory (symlinks and other types are rejected)`;
|
|
138
|
+
const uid = process.getuid?.();
|
|
139
|
+
if (uid !== undefined && info.uid !== uid)
|
|
140
|
+
return `${dir}: owned by uid ${String(info.uid)}, not by this process`;
|
|
141
|
+
if ((info.mode & 0o022) !== 0)
|
|
142
|
+
return `${dir}: writable by group or others (mode ${(info.mode & 0o777).toString(8)})`;
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
/** `dir` created with {@link PLUGIN_COPY_DIR_MODE} when missing, then checked by {@link dirIssue}. */
|
|
146
|
+
function ensureDir(dir) {
|
|
147
|
+
try {
|
|
148
|
+
mkdirSync(dir, { mode: PLUGIN_COPY_DIR_MODE });
|
|
149
|
+
}
|
|
150
|
+
catch (failure) {
|
|
151
|
+
if (codeOf(failure) !== 'EEXIST')
|
|
152
|
+
return `${dir}: ${codeOf(failure)}`;
|
|
153
|
+
}
|
|
154
|
+
return dirIssue(dir);
|
|
155
|
+
}
|
|
156
|
+
/** Writes the tree into the new directory `dir`: directories 0700, files read-only, `bin/` entries executable. */
|
|
157
|
+
function writeTree(dir, files) {
|
|
158
|
+
for (const file of files) {
|
|
159
|
+
const target = path.join(dir, ...file.rel.split('/'));
|
|
160
|
+
mkdirSync(path.dirname(target), { recursive: true, mode: PLUGIN_COPY_DIR_MODE });
|
|
161
|
+
writeFileSync(target, file.bytes, { flag: 'wx', mode: file.exec ? 0o500 : 0o400 });
|
|
162
|
+
// The mode of `writeFileSync` passes through the umask; the execute bit must not depend on it.
|
|
163
|
+
chmodSync(target, file.exec ? 0o500 : 0o400);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The copy for the package on disk, ready for `--plugin-dir`: an existing copy with the package's hash is used as it
|
|
168
|
+
* is; a missing one, or one with another hash, is written to a work directory next to it, checked against the hash and
|
|
169
|
+
* renamed into place. A copy that differs is replaced even when a live session runs from it: it no longer is what the
|
|
170
|
+
* package ships. Then other versions not in `inUse` are removed. Synchronous, so two starts in one process never
|
|
171
|
+
* interleave. Never throws.
|
|
172
|
+
*/
|
|
173
|
+
export function ensurePluginCopy(opts) {
|
|
174
|
+
const installRoot = opts.installRoot ?? INSTALL_ROOT;
|
|
175
|
+
let version;
|
|
176
|
+
try {
|
|
177
|
+
version = readPackageVersion(path.join(installRoot, 'package.json'));
|
|
178
|
+
}
|
|
179
|
+
catch (failure) {
|
|
180
|
+
return { ok: false, code: 'source', reason: `package version: ${codeOf(failure)}` };
|
|
181
|
+
}
|
|
182
|
+
if (!isVersionName(version)) {
|
|
183
|
+
return { ok: false, code: 'source', reason: `package version ${JSON.stringify(version)} cannot name a directory` };
|
|
184
|
+
}
|
|
185
|
+
const root = pluginCopyRoot(opts.dataDir);
|
|
186
|
+
const dir = path.join(root, version);
|
|
187
|
+
if (!isPluginDirValue(dir)) {
|
|
188
|
+
return { ok: false, code: 'path', reason: `${dir}: the path fails the --plugin-dir rule (isPluginDirValue)` };
|
|
189
|
+
}
|
|
190
|
+
const source = readPluginTree(path.join(installRoot, ...PLUGIN_SOURCE_SUBDIR));
|
|
191
|
+
if (!source.ok)
|
|
192
|
+
return { ok: false, code: 'source', reason: source.reason };
|
|
193
|
+
const issue = ensureDir(path.dirname(root)) ?? ensureDir(root);
|
|
194
|
+
if (issue !== null)
|
|
195
|
+
return { ok: false, code: 'data-dir', reason: issue };
|
|
196
|
+
let present = true;
|
|
197
|
+
try {
|
|
198
|
+
lstatSync(dir);
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
present = false;
|
|
202
|
+
}
|
|
203
|
+
if (present) {
|
|
204
|
+
const copy = readPluginTree(dir);
|
|
205
|
+
if (copy.ok && copy.hash === source.hash) {
|
|
206
|
+
prunePluginCopies(opts.dataDir, { current: version, inUse: opts.inUse });
|
|
207
|
+
return { ok: true, dir, version, written: false };
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const work = path.join(root, `${TMP_PREFIX}${randomBytes(6).toString('hex')}`);
|
|
211
|
+
let old = null;
|
|
212
|
+
try {
|
|
213
|
+
mkdirSync(work, { mode: PLUGIN_COPY_DIR_MODE });
|
|
214
|
+
writeTree(work, source.files);
|
|
215
|
+
const written = readPluginTree(work);
|
|
216
|
+
if (!written.ok || written.hash !== source.hash) {
|
|
217
|
+
return { ok: false, code: 'write', reason: `${work}: the written copy differs from the package` };
|
|
218
|
+
}
|
|
219
|
+
if (present) {
|
|
220
|
+
old = path.join(root, `${OLD_PREFIX}${randomBytes(6).toString('hex')}`);
|
|
221
|
+
renameSync(dir, old);
|
|
222
|
+
}
|
|
223
|
+
renameSync(work, dir);
|
|
224
|
+
}
|
|
225
|
+
catch (failure) {
|
|
226
|
+
// The old copy goes back when the new one did not take its place.
|
|
227
|
+
if (old !== null) {
|
|
228
|
+
try {
|
|
229
|
+
renameSync(old, dir);
|
|
230
|
+
old = null;
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
// Left as `.old-*`: the next prune removes it, the next start writes the copy again.
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return { ok: false, code: 'write', reason: `${dir}: ${codeOf(failure)}` };
|
|
237
|
+
}
|
|
238
|
+
finally {
|
|
239
|
+
rmSync(work, { recursive: true, force: true });
|
|
240
|
+
if (old !== null)
|
|
241
|
+
rmSync(old, { recursive: true, force: true });
|
|
242
|
+
}
|
|
243
|
+
prunePluginCopies(opts.dataDir, { current: version, inUse: opts.inUse });
|
|
244
|
+
return { ok: true, dir, version, written: true };
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Removes copies no live session uses and leftovers of an interrupted copy; returns the removed paths. Only direct
|
|
248
|
+
* children of the copies' directory that are real directories: a version-shaped name other than `current` and not in
|
|
249
|
+
* `inUse`, or a `.tmp-*` and `.old-*` leftover. Anything else (a file, a symlink, another name) stays. Never throws.
|
|
250
|
+
*/
|
|
251
|
+
export function prunePluginCopies(dataDir, opts) {
|
|
252
|
+
const root = pluginCopyRoot(dataDir);
|
|
253
|
+
if (dirIssue(root) !== null)
|
|
254
|
+
return [];
|
|
255
|
+
let names;
|
|
256
|
+
try {
|
|
257
|
+
names = readdirSync(root).sort();
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
return [];
|
|
261
|
+
}
|
|
262
|
+
const removed = [];
|
|
263
|
+
for (const name of names) {
|
|
264
|
+
const dir = path.join(root, name);
|
|
265
|
+
const leftover = name.startsWith(TMP_PREFIX) || name.startsWith(OLD_PREFIX);
|
|
266
|
+
if (!leftover) {
|
|
267
|
+
if (opts.inUse === null || !isVersionName(name) || name === opts.current || opts.inUse.has(dir))
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
if (!lstatSync(dir).isDirectory())
|
|
272
|
+
continue;
|
|
273
|
+
rmSync(dir, { recursive: true, force: true });
|
|
274
|
+
removed.push(dir);
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
// A copy that cannot be removed now stays for the next prune.
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return removed;
|
|
281
|
+
}
|