@phren/cli 0.3.52 → 0.3.54
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/dist/bridge/account-add.d.ts +7 -0
- package/dist/bridge/account-add.js +39 -0
- package/dist/bridge/claude-accounts.js +1 -1
- package/dist/bridge/client.js +1 -1
- package/dist/bridge/command.js +2 -0
- package/dist/bridge/computers.js +3 -2
- package/dist/bridge/conductor-role.d.ts +162 -2
- package/dist/bridge/conductor-role.js +56 -23
- package/dist/bridge/conductor-succession.d.ts +100 -0
- package/dist/bridge/conductor-succession.js +649 -0
- package/dist/bridge/continuation-launch.d.ts +54 -0
- package/dist/bridge/continuation-launch.js +106 -0
- package/dist/bridge/dispatch-command.js +11 -4
- package/dist/bridge/dispatch-hosts.d.ts +3 -0
- package/dist/bridge/dispatch-hosts.js +6 -1
- package/dist/bridge/dispatch-returns.d.ts +21 -3
- package/dist/bridge/dispatch-returns.js +169 -39
- package/dist/bridge/dispatch.d.ts +11 -0
- package/dist/bridge/dispatch.js +70 -5
- package/dist/bridge/hand-off-queue.d.ts +3 -0
- package/dist/bridge/hand-off-queue.js +8 -0
- package/dist/bridge/hand-off.d.ts +13 -0
- package/dist/bridge/hand-off.js +20 -1
- package/dist/bridge/install.js +0 -6
- package/dist/bridge/launch-brief.d.ts +2 -0
- package/dist/bridge/launch-brief.js +19 -8
- package/dist/bridge/move-computers.d.ts +47 -0
- package/dist/bridge/move-computers.js +118 -0
- package/dist/bridge/peer-handoff.d.ts +5 -0
- package/dist/bridge/peer-handoff.js +52 -0
- package/dist/bridge/request-origin.d.ts +6 -0
- package/dist/bridge/request-origin.js +20 -0
- package/dist/bridge/scoped-gateway.d.ts +2 -1
- package/dist/bridge/scoped-gateway.js +16 -3
- package/dist/bridge/server-launch.d.ts +2 -0
- package/dist/bridge/server-launch.js +63 -2
- package/dist/bridge/server-routes.d.ts +6 -0
- package/dist/bridge/server-routes.js +228 -15
- package/dist/bridge/server.js +39 -12
- package/dist/bridge/session-move.d.ts +28 -1
- package/dist/bridge/session-move.js +269 -26
- package/dist/bridge/terminal-herdr.js +14 -0
- package/dist/bridge/terminal-tmux.js +1 -0
- package/dist/bridge/terminal.d.ts +2 -0
- package/dist/bridge/terminal.js +6 -0
- package/dist/bridge/worker-relocation.d.ts +5 -0
- package/dist/bridge/worker-relocation.js +33 -0
- package/dist/bridge-hook.mjs +16386 -14258
- package/dist/modules/registry.js +8 -8
- package/dist/tools/dispatch.js +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,649 @@
|
|
|
1
|
+
import { redactPeerPayload } from "./peer-handoff.js";
|
|
2
|
+
import { deliveryIdSchema } from "./prompt-once.js";
|
|
3
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { conductorPane, readRoleState, resetRoleState, successionSchema, updateConductorAuthority } from "./conductor-role.js";
|
|
8
|
+
import { hookPeers, peerRequest } from "./peers.js";
|
|
9
|
+
import { isLocalComputer, peerFingerprint } from "./dispatch-hosts.js";
|
|
10
|
+
import { atomicInPrivateDir, BridgeError, object } from "./protocol.js";
|
|
11
|
+
const actions = z.enum(["dispatch", "hand_off", "returns", "approve", "grants-list", "grants-add", "grants-remove"]);
|
|
12
|
+
const callSchema = z.object({ id: z.string().uuid(), generation: z.string().uuid(), operationId: z.string().uuid() }).strict();
|
|
13
|
+
const computerIdentitySchema = z.object({ id: z.string().uuid(), fingerprint: z.string().regex(/^[a-f0-9]{64}$/).optional() }).strict();
|
|
14
|
+
const operationSchema = z.object({ action: actions, data: z.record(z.string(), z.unknown()), computerIdentity: computerIdentitySchema.optional() }).strict();
|
|
15
|
+
const samePane = (left, right) => object(left).server === right.server && object(left).pane === right.pane;
|
|
16
|
+
const sameTarget = (left, right) => !!left && !!right && Object.keys(left).every(key => left[key] === right[key]);
|
|
17
|
+
const busy = new Map();
|
|
18
|
+
const transitions = new Map();
|
|
19
|
+
const readOperation = (action, data) => action === "returns" || action === "grants-list" || action === "hand_off" && data.status === true;
|
|
20
|
+
const peerData = redactPeerPayload;
|
|
21
|
+
/** Check the original Hook's actual receipts over its own pinned directory,
|
|
22
|
+
* before any role reservation, hand-off or successor launch. */
|
|
23
|
+
export async function preflightConductorWorkers(from, computerId, peers = hookPeers, request = peerRequest) {
|
|
24
|
+
const { dispatchStatus } = await import("./dispatch.js");
|
|
25
|
+
const directory = await peers().catch(() => []);
|
|
26
|
+
const receipts = (await dispatchStatus()).filter(receipt => receipt.origin?.server === from.server && receipt.origin.pane === from.pane && !receipt.closedAt && (receipt.approval || !receipt.returned?.read || ["working", "blocked", "stalled"].includes(String(receipt.worker?.state))));
|
|
27
|
+
await Promise.all([...new Set(receipts.map(receipt => receipt.computer))].map(async (name) => {
|
|
28
|
+
const group = receipts.filter(receipt => receipt.computer === name);
|
|
29
|
+
const peer = directory.find(peer => peer.name === name);
|
|
30
|
+
if (group.every(receipt => receipt.computerId === computerId || !peer && !receipt.computerId && !receipt.moves?.some(move => move.to.computer) && isLocalComputer(name)))
|
|
31
|
+
return;
|
|
32
|
+
if (!peer)
|
|
33
|
+
throw new BridgeError(409, "A worker computer is not enrolled; the conductor move was refused before changes.");
|
|
34
|
+
let health;
|
|
35
|
+
try {
|
|
36
|
+
health = await request(peer, "/v1/health", undefined, 5_000);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
throw new BridgeError(409, "A worker computer is unreachable; the conductor move was refused before changes.");
|
|
40
|
+
}
|
|
41
|
+
for (const receipt of group) {
|
|
42
|
+
const moved = receipt.moves?.slice().reverse().find(move => move.to.computer);
|
|
43
|
+
const expected = moved?.computerId ?? receipt.computerId;
|
|
44
|
+
if (expected && object(health.computer).id !== expected || moved?.computerFingerprint && peerFingerprint(peer) !== moved.computerFingerprint)
|
|
45
|
+
throw new BridgeError(409, "A worker computer's identity changed; the conductor move was refused before changes.");
|
|
46
|
+
}
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
/** A owns the generation and every receipt/approval/grant. B holds only a
|
|
50
|
+
* delegated pane and operation outbox. No login or bearer credential crosses
|
|
51
|
+
* Hooks. Peer writes are authenticated by fetching their intent back over
|
|
52
|
+
* our own enrolled, pinned SSH account, rather than trusting a claimed alias. */
|
|
53
|
+
export class ConductorSuccessionService {
|
|
54
|
+
options;
|
|
55
|
+
peers;
|
|
56
|
+
request;
|
|
57
|
+
reservedCallers = new Set();
|
|
58
|
+
operations = new Map();
|
|
59
|
+
constructor(options) {
|
|
60
|
+
this.options = options;
|
|
61
|
+
this.peers = options.peers ?? (() => hookPeers(options.root));
|
|
62
|
+
this.request = options.request ?? peerRequest;
|
|
63
|
+
}
|
|
64
|
+
/** Synchronous admission also pauses conductor calls during preflight. */
|
|
65
|
+
reserveCaller(target) {
|
|
66
|
+
const key = `${target.server}\0${target.pane}`;
|
|
67
|
+
if (this.reservedCallers.has(key))
|
|
68
|
+
throw new BridgeError(409, "This session is already being moved.");
|
|
69
|
+
this.reservedCallers.add(key);
|
|
70
|
+
return () => { this.reservedCallers.delete(key); };
|
|
71
|
+
}
|
|
72
|
+
assertCallerAvailable(origin) {
|
|
73
|
+
const value = object(origin);
|
|
74
|
+
if (this.reservedCallers.has(`${value.server}\0${value.pane}`))
|
|
75
|
+
throw new BridgeError(409, "The conductor is being moved; new conductor operations are paused.");
|
|
76
|
+
}
|
|
77
|
+
file(id, suffix = "state") {
|
|
78
|
+
return path.join(this.options.root, "conductor-moves", z.string().uuid().parse(id), `${suffix}.json`);
|
|
79
|
+
}
|
|
80
|
+
async save(state) { await atomicInPrivateDir(this.file(state.id), JSON.stringify(state)); }
|
|
81
|
+
async status(id) {
|
|
82
|
+
z.string().uuid().parse(id);
|
|
83
|
+
const current = (await readRoleState(this.options.root))?.succession;
|
|
84
|
+
let state;
|
|
85
|
+
if (current?.id === id)
|
|
86
|
+
state = current;
|
|
87
|
+
else {
|
|
88
|
+
try {
|
|
89
|
+
state = successionSchema.parse(JSON.parse(await readFile(this.file(id), "utf8")));
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
throw new BridgeError(404, "No conductor move with that id is recorded on this Hook.");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Resume data is private to A: it includes unsanitized source labels and
|
|
96
|
+
// paths and must never be copied to B by a status/preparation probe.
|
|
97
|
+
const { resume: _resume, ...publicState } = state;
|
|
98
|
+
return publicState;
|
|
99
|
+
}
|
|
100
|
+
/** Identity is supplied only by the scoped SSH gateway, never the body. */
|
|
101
|
+
async authorize(id, caller, preparingOrigin) {
|
|
102
|
+
if (!caller)
|
|
103
|
+
throw new BridgeError(403, "This route requires an authenticated enrolled computer key. Re-enroll legacy peer keys.");
|
|
104
|
+
let state;
|
|
105
|
+
try {
|
|
106
|
+
state = await this.status(id);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (!(error instanceof BridgeError && error.status === 404 && preparingOrigin))
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
if (state) {
|
|
113
|
+
if (caller.name !== undefined && caller.name !== state.connection.name || caller.id !== undefined && caller.id !== state.connection.id || caller.fingerprint !== state.connection.fingerprint)
|
|
114
|
+
throw new BridgeError(403, "This peer is not the move's recorded counterpart.");
|
|
115
|
+
await this.connection(state);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
if (caller.id !== undefined && caller.id !== preparingOrigin)
|
|
119
|
+
throw new BridgeError(403, "This peer is not the preparing origin.");
|
|
120
|
+
const peers = (await this.peers()).filter(peer => peerFingerprint(peer) === caller.fingerprint && (!caller.name || peer.name === caller.name));
|
|
121
|
+
if (peers.length !== 1 || object((await this.request(peers[0], "/v1/health", undefined, 5_000)).computer).id !== preparingOrigin)
|
|
122
|
+
throw new BridgeError(403, "The preparing peer is not currently verified.");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async workerIdentities(from) {
|
|
126
|
+
const { dispatchStatus } = await import("./dispatch.js");
|
|
127
|
+
const directory = await this.peers();
|
|
128
|
+
const receipts = (await dispatchStatus()).filter(receipt => samePane(receipt.origin, from) && !receipt.closedAt);
|
|
129
|
+
const identities = new Map();
|
|
130
|
+
for (const receipt of receipts) {
|
|
131
|
+
const peer = directory.find(peer => peer.name === receipt.computer);
|
|
132
|
+
const moved = receipt.moves?.slice().reverse().find(move => move.to.computer);
|
|
133
|
+
if (receipt.computerId === this.options.computer.id || !peer && !moved && isLocalComputer(receipt.computer, [this.options.computer.name])) {
|
|
134
|
+
identities.set(this.options.computer.id, { id: this.options.computer.id });
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (!peer)
|
|
138
|
+
throw new BridgeError(409, "A worker computer is not enrolled in the original Hook's directory.");
|
|
139
|
+
const health = await this.request(peer, "/v1/health", undefined, 5_000);
|
|
140
|
+
const id = z.string().uuid().parse(object(health.computer).id), fingerprint = peerFingerprint(peer);
|
|
141
|
+
if ((moved?.computerId ?? receipt.computerId) && (moved?.computerId ?? receipt.computerId) !== id || moved?.computerFingerprint && moved.computerFingerprint !== fingerprint)
|
|
142
|
+
throw new BridgeError(409, "A worker computer's identity changed.");
|
|
143
|
+
identities.set(`${id}:${fingerprint}`, { id, fingerprint });
|
|
144
|
+
}
|
|
145
|
+
return [...identities.values()];
|
|
146
|
+
}
|
|
147
|
+
async connection(state) {
|
|
148
|
+
const peer = (await this.peers()).find(candidate => candidate.name === state.connection.name && candidate.server === state.connection.server && peerFingerprint(candidate) === state.connection.fingerprint);
|
|
149
|
+
if (!peer)
|
|
150
|
+
throw new BridgeError(409, "The conductor's enrolled connection was removed or changed.");
|
|
151
|
+
const health = await this.request(peer, "/v1/health", undefined, 5_000).catch(() => undefined);
|
|
152
|
+
if (!health || object(health.computer).id !== state.connection.id)
|
|
153
|
+
throw new BridgeError(409, "The original conductor Hook is unreachable or its identity changed. Conductor operations are unavailable.");
|
|
154
|
+
return peer;
|
|
155
|
+
}
|
|
156
|
+
async change(id, run) {
|
|
157
|
+
const saved = await updateConductorAuthority(this.options.root, async (role) => {
|
|
158
|
+
if (role.succession?.id !== id)
|
|
159
|
+
throw new BridgeError(409, "The conductor ownership generation changed.");
|
|
160
|
+
return { ...role, succession: run(role.succession) };
|
|
161
|
+
});
|
|
162
|
+
await this.save(saved.succession);
|
|
163
|
+
return saved.succession;
|
|
164
|
+
}
|
|
165
|
+
async begin(id, expected, from, destination, computerId, resume) {
|
|
166
|
+
if (busy.get(this.options.root))
|
|
167
|
+
throw new BridgeError(409, "The conductor has an operation in flight; try the move again after it completes.");
|
|
168
|
+
await this.options.checkWorkers(from);
|
|
169
|
+
if (!destination.fingerprint)
|
|
170
|
+
throw new BridgeError(409, "The destination has no pinned identity.");
|
|
171
|
+
const health = await destination.request("/v1/health", undefined, 5_000);
|
|
172
|
+
if (object(health.computer).id !== computerId || object(health.capabilities).moveConductorToComputer !== true)
|
|
173
|
+
throw new BridgeError(409, `Update ${destination.name}'s Hook to move the conductor`);
|
|
174
|
+
const current = (await readRoleState(this.options.root))?.succession;
|
|
175
|
+
if (current?.id === id) {
|
|
176
|
+
if (current.phase === "aborted")
|
|
177
|
+
throw new BridgeError(409, "This conductor move was aborted; use a new move id.");
|
|
178
|
+
await this.ensurePrepared(id);
|
|
179
|
+
return current;
|
|
180
|
+
}
|
|
181
|
+
const workers = await this.workerIdentities(from);
|
|
182
|
+
const transaction = { workers, id, generation: randomUUID(), side: "origin", phase: "preparing", originId: this.options.computer.id, destinationId: computerId,
|
|
183
|
+
previous: expected, from, ...(resume ? { resume } : {}), connection: { name: destination.name, server: destination.server, fingerprint: destination.fingerprint, id: computerId } };
|
|
184
|
+
await updateConductorAuthority(this.options.root, async (role) => {
|
|
185
|
+
if (busy.get(this.options.root))
|
|
186
|
+
throw new BridgeError(409, "The conductor has an operation in flight; try the move again after it completes.");
|
|
187
|
+
if (role.succession && role.succession.phase !== "aborted")
|
|
188
|
+
throw new BridgeError(409, "A conductor move or delegation is already active.");
|
|
189
|
+
if (JSON.stringify(role.conductor) !== JSON.stringify(expected))
|
|
190
|
+
throw new BridgeError(409, "The conductor changed during the move; nothing was transferred.");
|
|
191
|
+
return { ...role, succession: transaction };
|
|
192
|
+
});
|
|
193
|
+
await this.save(transaction);
|
|
194
|
+
try {
|
|
195
|
+
const peer = await this.connection(transaction);
|
|
196
|
+
await this.request(peer, "/v1/conductor/move/prepare", { id, originId: transaction.originId, generation: transaction.generation, workers: transaction.workers ?? [] }, 10_000);
|
|
197
|
+
return transaction;
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
await this.abort(id);
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async ensurePrepared(id) {
|
|
205
|
+
const state = await this.status(id);
|
|
206
|
+
if (state.side !== "origin" || state.phase === "aborted")
|
|
207
|
+
throw new BridgeError(409, "The conductor move is no longer preparing.");
|
|
208
|
+
const peer = await this.connection(state);
|
|
209
|
+
await this.request(peer, "/v1/conductor/move/prepare", { id, originId: state.originId, generation: state.generation, workers: state.workers ?? [] }, 10_000);
|
|
210
|
+
}
|
|
211
|
+
async prepare(input) {
|
|
212
|
+
const data = z.object({ id: z.string().uuid(), originId: z.string().uuid(), generation: z.string().uuid(), workers: z.array(computerIdentitySchema).default([]) }).strict().parse(input);
|
|
213
|
+
const existing = (await readRoleState(this.options.root))?.succession;
|
|
214
|
+
if (existing?.id === data.id && existing.generation === data.generation)
|
|
215
|
+
return { ok: true, phase: existing.phase };
|
|
216
|
+
const candidates = await Promise.all((await this.peers()).map(async (peer) => {
|
|
217
|
+
try {
|
|
218
|
+
const health = await this.request(peer, "/v1/health", undefined, 5_000);
|
|
219
|
+
if (object(health.computer).id !== data.originId)
|
|
220
|
+
return undefined;
|
|
221
|
+
const state = successionSchema.parse(await this.request(peer, `/v1/conductor/move/status?id=${data.id}`, undefined, 5_000));
|
|
222
|
+
if (state.side !== "origin" || state.phase === "aborted" || state.generation !== data.generation || state.destinationId !== this.options.computer.id)
|
|
223
|
+
return undefined;
|
|
224
|
+
return { peer, state };
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
}));
|
|
230
|
+
const verified = candidates.filter(value => value !== undefined);
|
|
231
|
+
if (verified.length !== 1)
|
|
232
|
+
throw new BridgeError(403, "No unique pinned origin Hook authorized this conductor prepare.");
|
|
233
|
+
const { peer, state } = verified[0];
|
|
234
|
+
if (JSON.stringify(data.workers) !== JSON.stringify(state.workers ?? []))
|
|
235
|
+
throw new BridgeError(403, "The worker preflight does not match the original Hook intent.");
|
|
236
|
+
for (const worker of data.workers)
|
|
237
|
+
await this.resolveComputer(worker);
|
|
238
|
+
const successor = { ...state, side: "successor", phase: "prepared", connection: { name: peer.name, server: peer.server, fingerprint: peerFingerprint(peer), id: data.originId } };
|
|
239
|
+
// Adopt a legacy named role through the same resolver as the overview,
|
|
240
|
+
// including prefixed names and mux agent-name mappings.
|
|
241
|
+
const snapshot = await this.options.snapshot(state.connection.server);
|
|
242
|
+
await conductorPane(state.connection.server, snapshot, this.options.root);
|
|
243
|
+
await updateConductorAuthority(this.options.root, async (role) => {
|
|
244
|
+
if (role.conductor || role.succession && role.succession.phase !== "aborted")
|
|
245
|
+
throw new BridgeError(409, "This Hook already has a conductor or prepared successor.");
|
|
246
|
+
return { ...role, conductor: null, succession: successor };
|
|
247
|
+
});
|
|
248
|
+
await this.save(successor);
|
|
249
|
+
return { ok: true, phase: "prepared" };
|
|
250
|
+
}
|
|
251
|
+
/** The prepared successor pulls the canonical launch over its own pin.
|
|
252
|
+
* An unrelated enrolled peer cannot pre-empt a known move id with a brief. */
|
|
253
|
+
async saveLaunch(id, input) {
|
|
254
|
+
const state = await this.status(id);
|
|
255
|
+
if (state.side !== "origin" || state.phase === "aborted")
|
|
256
|
+
throw new BridgeError(409, "This conductor move is not preparing a launch.");
|
|
257
|
+
await atomicInPrivateDir(this.file(id, "launch"), JSON.stringify(input));
|
|
258
|
+
}
|
|
259
|
+
async launchIntent(id) {
|
|
260
|
+
const state = await this.status(id);
|
|
261
|
+
if (state.side !== "origin" || state.phase === "aborted")
|
|
262
|
+
throw new BridgeError(409, "This conductor launch is no longer authorized.");
|
|
263
|
+
return JSON.parse(await readFile(this.file(id, "launch"), "utf8"));
|
|
264
|
+
}
|
|
265
|
+
async verifiedLaunch(input) {
|
|
266
|
+
const state = (await readRoleState(this.options.root))?.succession;
|
|
267
|
+
if (!state || state.side !== "successor" || state.id !== object(input.brief).id)
|
|
268
|
+
return input;
|
|
269
|
+
if (state.phase === "aborted")
|
|
270
|
+
throw new BridgeError(409, "This prepared conductor move was aborted.");
|
|
271
|
+
const peer = await this.connection(state);
|
|
272
|
+
const authority = successionSchema.parse(await this.request(peer, `/v1/conductor/move/status?id=${state.id}`, undefined, 5_000));
|
|
273
|
+
if (authority.phase === "aborted" || authority.generation !== state.generation)
|
|
274
|
+
throw new BridgeError(403, "The conductor launch generation is not authorized.");
|
|
275
|
+
const canonical = await this.request(peer, `/v1/conductor/move/launch?id=${state.id}`, undefined, 5_000);
|
|
276
|
+
if (JSON.stringify(canonical) !== JSON.stringify(input))
|
|
277
|
+
throw new BridgeError(403, "This is not the origin Hook's prepared conductor launch.");
|
|
278
|
+
return canonical;
|
|
279
|
+
}
|
|
280
|
+
async prepared(id, target) {
|
|
281
|
+
await this.change(id, state => {
|
|
282
|
+
if (state.side !== "origin" || state.phase === "aborted")
|
|
283
|
+
throw new BridgeError(409, "This conductor move is no longer preparing.");
|
|
284
|
+
if (state.target && !sameTarget(state.target, target))
|
|
285
|
+
throw new BridgeError(409, "This move already prepared a different successor.");
|
|
286
|
+
return { ...state, target, phase: state.phase === "committed" ? "committed" : "prepared" };
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
async commit(id) {
|
|
290
|
+
const saved = await updateConductorAuthority(this.options.root, async (role) => {
|
|
291
|
+
const state = role.succession;
|
|
292
|
+
if (!state || state.id !== id || state.side !== "origin" || !state.target)
|
|
293
|
+
throw new BridgeError(409, "The successor was not prepared and confirmed.");
|
|
294
|
+
if (state.phase === "committed")
|
|
295
|
+
return role;
|
|
296
|
+
if (state.phase !== "prepared" || JSON.stringify(role.conductor) !== JSON.stringify(state.previous))
|
|
297
|
+
throw new BridgeError(409, "The conductor changed during the move; nothing was transferred.");
|
|
298
|
+
return { ...role, conductor: null, succession: { ...state, phase: "committed" } };
|
|
299
|
+
});
|
|
300
|
+
// The ownership write is the commit point. A failed notification or lost
|
|
301
|
+
// acknowledgement never restores the previous generation.
|
|
302
|
+
const state = saved.succession;
|
|
303
|
+
await this.save(state);
|
|
304
|
+
return state;
|
|
305
|
+
}
|
|
306
|
+
async transition(id, run) {
|
|
307
|
+
const key = this.file(id, "transition");
|
|
308
|
+
const work = (transitions.get(key) ?? Promise.resolve()).catch(() => { }).then(run);
|
|
309
|
+
transitions.set(key, work);
|
|
310
|
+
try {
|
|
311
|
+
return await work;
|
|
312
|
+
}
|
|
313
|
+
finally {
|
|
314
|
+
if (transitions.get(key) === work)
|
|
315
|
+
transitions.delete(key);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
async activate(input) {
|
|
319
|
+
const id = z.object({ id: z.string().uuid() }).parse(input).id;
|
|
320
|
+
return this.transition(id, () => this.activateOnce(input));
|
|
321
|
+
}
|
|
322
|
+
async activateOnce(input) {
|
|
323
|
+
const data = z.object({ id: z.string().uuid(), generation: z.string().uuid() }).strict().parse(input);
|
|
324
|
+
const successor = await this.status(data.id);
|
|
325
|
+
if (successor.side !== "successor" || successor.generation !== data.generation)
|
|
326
|
+
throw new BridgeError(403, "This is not the prepared successor generation.");
|
|
327
|
+
const peer = await this.connection(successor);
|
|
328
|
+
const authority = successionSchema.parse(await this.request(peer, `/v1/conductor/move/status?id=${data.id}`, undefined, 5_000));
|
|
329
|
+
if (authority.phase !== "committed" || authority.generation !== successor.generation || authority.destinationId !== this.options.computer.id || !authority.target)
|
|
330
|
+
throw new BridgeError(409, "The original Hook has not committed this successor. You are not the conductor.");
|
|
331
|
+
const target = authority.target;
|
|
332
|
+
const pane = await this.options.validate(target);
|
|
333
|
+
if (!pane || !["idle", "working"].includes(String(pane.agent_status)))
|
|
334
|
+
throw new BridgeError(409, "The prepared successor is not confirmed running.");
|
|
335
|
+
await updateConductorAuthority(this.options.root, async (role) => {
|
|
336
|
+
if (role.succession?.id !== data.id || role.succession.generation !== data.generation || role.succession.phase === "aborted")
|
|
337
|
+
throw new BridgeError(409, "The successor generation changed.");
|
|
338
|
+
return { ...role, conductor: { server: target.server, pane: target.pane, workspace: target.workspace, tab: target.tab, source: target.source, session: target.session,
|
|
339
|
+
...(typeof pane.terminal_id === "string" ? { terminal: pane.terminal_id } : {}), since: successor.previous.since, by: "owner" }, succession: { ...successor, phase: "committed", target } };
|
|
340
|
+
});
|
|
341
|
+
await this.options.deliver(target, "The original Hook committed the hand-over. You are now the conductor. Its receipts, approvals and grants stay there; your conductor operations are forwarded there and fail closed if it is unreachable.", `move-${data.id}-conductor-confirmed`);
|
|
342
|
+
return { ok: true, phase: "committed", target };
|
|
343
|
+
}
|
|
344
|
+
async finish(id) { return this.transition(id, () => this.finishOnce(id)); }
|
|
345
|
+
async finishOnce(id) {
|
|
346
|
+
const state = await this.status(id);
|
|
347
|
+
if (state.side !== "origin" || state.phase !== "committed")
|
|
348
|
+
throw new BridgeError(409, "The origin has not committed this hand-over.");
|
|
349
|
+
if (state.activated)
|
|
350
|
+
return;
|
|
351
|
+
const peer = await this.connection(state);
|
|
352
|
+
await this.request(peer, "/v1/conductor/move/commit", { id, generation: state.generation }, 10_000);
|
|
353
|
+
await this.options.deliver(state.from, "The conductor role moved to another computer. You are no longer the conductor; conductor operations from this chat are fenced. Keep this chat open as its hand-off record.", `move-${id}-conductor-fenced`);
|
|
354
|
+
await this.change(id, current => ({ ...current, activated: true }));
|
|
355
|
+
}
|
|
356
|
+
/** Explicit owner revocation on A ends delegation without restoring the
|
|
357
|
+
* old chat. This is distinct from rolling back an uncertain commit. */
|
|
358
|
+
async stopOrigin(pane) {
|
|
359
|
+
const current = (await readRoleState(this.options.root))?.succession;
|
|
360
|
+
if (!current || current.phase === "aborted")
|
|
361
|
+
return undefined;
|
|
362
|
+
if (current.side !== "origin")
|
|
363
|
+
throw new BridgeError(409, "Stop this delegated conductor from its original Hook.");
|
|
364
|
+
if (current.phase !== "committed")
|
|
365
|
+
throw new BridgeError(409, "The conductor is being moved; reconcile the move before stopping it.");
|
|
366
|
+
if (pane !== undefined)
|
|
367
|
+
throw new BridgeError(409, "The conductor is on another computer. Stop its delegation on the original Hook without a pane id.");
|
|
368
|
+
const state = await updateConductorAuthority(this.options.root, async (role) => {
|
|
369
|
+
if (busy.get(this.options.root))
|
|
370
|
+
throw new BridgeError(409, "The conductor has an operation in flight; try stopping it again after it completes.");
|
|
371
|
+
if (role.succession?.id !== current.id || role.succession.generation !== current.generation || role.succession.phase !== "committed")
|
|
372
|
+
throw new BridgeError(409, "The conductor generation changed.");
|
|
373
|
+
return { ...role, conductor: null, succession: { ...role.succession, phase: "aborted", revoked: true } };
|
|
374
|
+
});
|
|
375
|
+
await this.save(state.succession);
|
|
376
|
+
return { ok: true, stopped: true };
|
|
377
|
+
}
|
|
378
|
+
async abort(id) {
|
|
379
|
+
// Re-read disk after any uncertain write. A committed generation is never
|
|
380
|
+
// rolled back, even if the acknowledgement or following status write failed.
|
|
381
|
+
resetRoleState();
|
|
382
|
+
const state = await this.status(id);
|
|
383
|
+
if (state.phase === "committed")
|
|
384
|
+
return;
|
|
385
|
+
const aborted = await updateConductorAuthority(this.options.root, async (role) => {
|
|
386
|
+
if (role.succession?.id !== id || role.succession.phase === "committed")
|
|
387
|
+
return role;
|
|
388
|
+
return { ...role, ...(role.succession.side === "successor" ? { conductor: null } : {}), succession: { ...role.succession, phase: "aborted" } };
|
|
389
|
+
});
|
|
390
|
+
if (aborted.succession)
|
|
391
|
+
await this.save(aborted.succession);
|
|
392
|
+
}
|
|
393
|
+
async call(action, data, origin, execute) {
|
|
394
|
+
this.assertCallerAvailable(origin);
|
|
395
|
+
const role = await readRoleState(this.options.root), state = role?.succession;
|
|
396
|
+
if (state?.revoked && !samePane(origin, role?.conductor ?? { server: "", pane: "" }) && samePane(origin, state.side === "origin" ? state.from : state.target ?? state.from))
|
|
397
|
+
throw new BridgeError(409, "This conductor ownership generation is fenced.");
|
|
398
|
+
if (!state || state.phase === "aborted") {
|
|
399
|
+
let held = false;
|
|
400
|
+
await updateConductorAuthority(this.options.root, async (current) => {
|
|
401
|
+
this.assertCallerAvailable(origin);
|
|
402
|
+
if (current.succession && current.succession.phase !== "aborted")
|
|
403
|
+
throw new BridgeError(409, "The conductor is being moved; new conductor operations are paused.");
|
|
404
|
+
if (current.conductor && samePane(origin, current.conductor)) {
|
|
405
|
+
busy.set(this.options.root, (busy.get(this.options.root) ?? 0) + 1);
|
|
406
|
+
held = true;
|
|
407
|
+
}
|
|
408
|
+
return current;
|
|
409
|
+
});
|
|
410
|
+
try {
|
|
411
|
+
return await execute();
|
|
412
|
+
}
|
|
413
|
+
finally {
|
|
414
|
+
if (held)
|
|
415
|
+
busy.set(this.options.root, (busy.get(this.options.root) ?? 1) - 1);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const local = state.side === "origin" ? state.from : state.target;
|
|
419
|
+
// Prepared successor operations with a live origin are refused until the
|
|
420
|
+
// exact conversation is bound and A's generation commit has been proved.
|
|
421
|
+
if (state.side === "successor" && state.phase !== "committed" && (samePane(origin, state.target ?? { server: "", pane: "" }) || typeof object(origin).server === "string" && typeof object(origin).pane === "string" && await this.options.isSuccessor?.(origin, state.id)))
|
|
422
|
+
throw new BridgeError(409, "This chat is a prepared successor, not the conductor yet.");
|
|
423
|
+
if (!local || !samePane(origin, local))
|
|
424
|
+
return execute();
|
|
425
|
+
if (state.side === "origin")
|
|
426
|
+
throw new BridgeError(409, state.phase === "committed" ? "This chat is no longer the conductor. Its ownership generation is fenced." : "The conductor is being moved; new conductor operations are paused.");
|
|
427
|
+
await this.options.validate(state.target);
|
|
428
|
+
const peer = await this.connection(state);
|
|
429
|
+
const authority = successionSchema.parse(await this.request(peer, `/v1/conductor/move/status?id=${state.id}`, undefined, 5_000));
|
|
430
|
+
if (authority.phase !== "committed" || authority.generation !== state.generation || !sameTarget(authority.target, state.target))
|
|
431
|
+
throw new BridgeError(409, "The original Hook no longer authorizes this conductor generation.");
|
|
432
|
+
const operation = { action, data: peerData(data), ...(["dispatch", "hand_off"].includes(action) && data.computer !== "anywhere" ? { computerIdentity: await this.identifyComputer(data.computer) } : {}) };
|
|
433
|
+
const hash = createHash("sha256").update(JSON.stringify(operation)).digest("hex");
|
|
434
|
+
const key = this.file(state.id, `call-${hash}`);
|
|
435
|
+
const pending = this.operations.get(key);
|
|
436
|
+
if (pending)
|
|
437
|
+
return pending;
|
|
438
|
+
const work = (async () => {
|
|
439
|
+
let saved;
|
|
440
|
+
try {
|
|
441
|
+
saved = JSON.parse(await readFile(key, "utf8"));
|
|
442
|
+
}
|
|
443
|
+
catch (error) {
|
|
444
|
+
if (error.code !== "ENOENT")
|
|
445
|
+
throw error;
|
|
446
|
+
}
|
|
447
|
+
// Only an unacknowledged intent is replayed. A successful call starts
|
|
448
|
+
// a fresh intent next time, especially for draining newly arrived returns.
|
|
449
|
+
const operationId = !readOperation(action, data) && saved && !saved.result && !saved.failed ? saved.operationId : randomUUID();
|
|
450
|
+
await atomicInPrivateDir(this.file(state.id, `out-${operationId}`), JSON.stringify(operation));
|
|
451
|
+
await atomicInPrivateDir(key, JSON.stringify({ operationId, at: Date.now() }));
|
|
452
|
+
try {
|
|
453
|
+
const result = await this.request(peer, "/v1/conductor/move/operate", { id: state.id, generation: state.generation, operationId }, 65_000);
|
|
454
|
+
await atomicInPrivateDir(key, JSON.stringify({ operationId, at: Date.now(), result }));
|
|
455
|
+
if (action === "returns")
|
|
456
|
+
await this.request(peer, "/v1/conductor/move/ack", { id: state.id, generation: state.generation, operationId }, 10_000).catch(() => undefined);
|
|
457
|
+
return result;
|
|
458
|
+
}
|
|
459
|
+
catch (error) {
|
|
460
|
+
if (error instanceof BridgeError && error.status >= 400 && error.status < 500)
|
|
461
|
+
await atomicInPrivateDir(key, JSON.stringify({ operationId, at: Date.now(), failed: true }));
|
|
462
|
+
throw error;
|
|
463
|
+
}
|
|
464
|
+
})();
|
|
465
|
+
this.operations.set(key, work);
|
|
466
|
+
try {
|
|
467
|
+
return await work;
|
|
468
|
+
}
|
|
469
|
+
finally {
|
|
470
|
+
this.operations.delete(key);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
async identifyComputer(name) {
|
|
474
|
+
if (name === undefined || typeof name === "string" && isLocalComputer(name, [this.options.computer.name]))
|
|
475
|
+
return { id: this.options.computer.id };
|
|
476
|
+
const peer = (await this.peers()).find(peer => peer.name === name);
|
|
477
|
+
if (!peer)
|
|
478
|
+
throw new BridgeError(409, "The selected computer is not in the successor Hook's verified directory.");
|
|
479
|
+
const health = await this.request(peer, "/v1/health", undefined, 5_000);
|
|
480
|
+
return { id: z.string().uuid().parse(object(health.computer).id), fingerprint: peerFingerprint(peer) };
|
|
481
|
+
}
|
|
482
|
+
async resolveComputer(identity) {
|
|
483
|
+
if (identity.id === this.options.computer.id)
|
|
484
|
+
return this.options.computer.name;
|
|
485
|
+
const matches = (await Promise.all((await this.peers()).filter(peer => !identity.fingerprint || peerFingerprint(peer) === identity.fingerprint).map(async (peer) => {
|
|
486
|
+
const health = await this.request(peer, "/v1/health", undefined, 5_000).catch(() => undefined);
|
|
487
|
+
return object(health?.computer).id === identity.id ? peer.name : undefined;
|
|
488
|
+
}))).filter((name) => !!name);
|
|
489
|
+
if (matches.length !== 1)
|
|
490
|
+
throw new BridgeError(409, "The selected computer identity is not uniquely enrolled on the original conductor Hook.");
|
|
491
|
+
return matches[0];
|
|
492
|
+
}
|
|
493
|
+
/** Automatic failover has no authority from the fenced old pane. Its
|
|
494
|
+
* generation is checked against the live successor before executing at A. */
|
|
495
|
+
async automatic(action, data, origin, execute) {
|
|
496
|
+
this.assertCallerAvailable(origin);
|
|
497
|
+
const state = (await readRoleState(this.options.root))?.succession;
|
|
498
|
+
if (state?.side !== "origin" || state.phase !== "committed" || !samePane(origin, state.from))
|
|
499
|
+
return this.call(action, data, origin, execute);
|
|
500
|
+
const peer = await this.connection(state);
|
|
501
|
+
const successor = await this.request(peer, `/v1/conductor/move/status?id=${state.id}`, undefined, 5_000);
|
|
502
|
+
if (successor.phase !== "committed" || successor.generation !== state.generation || !sameTarget(object(successor.target), state.target))
|
|
503
|
+
throw new BridgeError(409, "The delegated conductor is unavailable; automatic dispatch is paused.");
|
|
504
|
+
await updateConductorAuthority(this.options.root, async (role) => {
|
|
505
|
+
if (role.succession?.generation !== state.generation || role.succession.phase !== "committed")
|
|
506
|
+
throw new BridgeError(409, "The conductor generation changed.");
|
|
507
|
+
busy.set(this.options.root, (busy.get(this.options.root) ?? 0) + 1);
|
|
508
|
+
return role;
|
|
509
|
+
});
|
|
510
|
+
try {
|
|
511
|
+
return await execute();
|
|
512
|
+
}
|
|
513
|
+
finally {
|
|
514
|
+
busy.set(this.options.root, (busy.get(this.options.root) ?? 1) - 1);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
async outgoing(id, operationId) {
|
|
518
|
+
const state = await this.status(id);
|
|
519
|
+
if (state.side !== "successor" || state.phase !== "committed")
|
|
520
|
+
throw new BridgeError(409, "No active successor outbox.");
|
|
521
|
+
return operationSchema.parse(JSON.parse(await readFile(this.file(id, `out-${z.string().uuid().parse(operationId)}`), "utf8")));
|
|
522
|
+
}
|
|
523
|
+
async operate(input, execute) {
|
|
524
|
+
const data = callSchema.parse(input), state = await this.status(data.id);
|
|
525
|
+
if (state.side !== "origin" || state.phase !== "committed" || state.generation !== data.generation)
|
|
526
|
+
throw new BridgeError(403, "The conductor generation is not authorized.");
|
|
527
|
+
const peer = await this.connection(state);
|
|
528
|
+
const operation = operationSchema.parse(await this.request(peer, `/v1/conductor/move/operation?id=${data.id}&operationId=${data.operationId}`, undefined, 5_000));
|
|
529
|
+
const key = this.file(data.id, `result-${data.operationId}`);
|
|
530
|
+
const pending = this.operations.get(key);
|
|
531
|
+
if (pending)
|
|
532
|
+
return pending;
|
|
533
|
+
const work = (async () => {
|
|
534
|
+
let saved;
|
|
535
|
+
try {
|
|
536
|
+
saved = JSON.parse(await readFile(key, "utf8"));
|
|
537
|
+
}
|
|
538
|
+
catch (error) {
|
|
539
|
+
if (error.code !== "ENOENT")
|
|
540
|
+
throw error;
|
|
541
|
+
}
|
|
542
|
+
if (saved?.result)
|
|
543
|
+
return peerData(object(saved.result));
|
|
544
|
+
if (saved?.state === "failed")
|
|
545
|
+
throw new BridgeError(Number(saved.status), String(saved.error));
|
|
546
|
+
if (saved && !readOperation(operation.action, operation.data))
|
|
547
|
+
throw new BridgeError(503, "This conductor operation has an uncertain outcome. Inspect its existing receipt; it will not be repeated.");
|
|
548
|
+
await updateConductorAuthority(this.options.root, async (role) => {
|
|
549
|
+
if (role.succession?.id !== data.id || role.succession.phase !== "committed" || role.succession.generation !== data.generation)
|
|
550
|
+
throw new BridgeError(403, "The conductor generation was fenced.");
|
|
551
|
+
busy.set(this.options.root, (busy.get(this.options.root) ?? 0) + 1);
|
|
552
|
+
return role;
|
|
553
|
+
});
|
|
554
|
+
try {
|
|
555
|
+
await atomicInPrivateDir(key, JSON.stringify({ state: "uncertain", operation }));
|
|
556
|
+
const { server, workspace, tab, pane } = state.from;
|
|
557
|
+
const payload = { ...operation.data };
|
|
558
|
+
if (operation.computerIdentity)
|
|
559
|
+
payload.computer = await this.resolveComputer(operation.computerIdentity);
|
|
560
|
+
const result = peerData(await execute(operation.action, payload, { server, workspace, tab, pane }, true));
|
|
561
|
+
await atomicInPrivateDir(key, JSON.stringify({ state: "done", result, operation }));
|
|
562
|
+
return result;
|
|
563
|
+
}
|
|
564
|
+
catch (error) {
|
|
565
|
+
if (error instanceof BridgeError && error.status >= 400 && error.status < 500)
|
|
566
|
+
await atomicInPrivateDir(key, JSON.stringify({ state: "failed", status: error.status, error: error.message }));
|
|
567
|
+
throw error;
|
|
568
|
+
}
|
|
569
|
+
finally {
|
|
570
|
+
busy.set(this.options.root, (busy.get(this.options.root) ?? 1) - 1);
|
|
571
|
+
}
|
|
572
|
+
})();
|
|
573
|
+
this.operations.set(key, work);
|
|
574
|
+
try {
|
|
575
|
+
return await work;
|
|
576
|
+
}
|
|
577
|
+
finally {
|
|
578
|
+
this.operations.delete(key);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
async acknowledge(input, acknowledge) {
|
|
582
|
+
const data = callSchema.parse(input), state = await this.status(data.id);
|
|
583
|
+
if (state.side !== "origin" || state.phase !== "committed" || state.generation !== data.generation)
|
|
584
|
+
throw new BridgeError(403, "The conductor generation is not authorized.");
|
|
585
|
+
const saved = object(JSON.parse(await readFile(this.file(data.id, `result-${data.operationId}`), "utf8")));
|
|
586
|
+
if (object(saved.operation).action === "returns" && saved.state === "done")
|
|
587
|
+
await acknowledge((object(saved.result).returns ?? []));
|
|
588
|
+
return { ok: true };
|
|
589
|
+
}
|
|
590
|
+
async forward(origin, text, deliveryId) {
|
|
591
|
+
const state = (await readRoleState(this.options.root))?.succession;
|
|
592
|
+
if (state?.side !== "origin" || !samePane(origin, state.from))
|
|
593
|
+
return undefined;
|
|
594
|
+
if (state.phase === "preparing" || state.phase === "prepared")
|
|
595
|
+
return { delivered: false };
|
|
596
|
+
if (state.phase !== "committed")
|
|
597
|
+
return undefined;
|
|
598
|
+
const peer = await this.connection(state);
|
|
599
|
+
await atomicInPrivateDir(this.file(state.id, `notice-${deliveryId}`), JSON.stringify(peerData({ text, deliveryId })));
|
|
600
|
+
const sent = await this.request(peer, "/v1/conductor/move/notice", { id: state.id, generation: state.generation, deliveryId }, 10_000);
|
|
601
|
+
const current = (await readRoleState(this.options.root))?.succession;
|
|
602
|
+
if (current?.id !== state.id || current.generation !== state.generation || current.phase !== "committed")
|
|
603
|
+
return { delivered: false };
|
|
604
|
+
return { delivered: sent.delivered === true || sent.queued === true };
|
|
605
|
+
}
|
|
606
|
+
async notice(input) {
|
|
607
|
+
const data = z.object({ id: z.string().uuid(), generation: z.string().uuid(), deliveryId: deliveryIdSchema.unwrap() }).strict().parse(input);
|
|
608
|
+
const state = await this.status(data.id);
|
|
609
|
+
if (state.side !== "successor" || state.phase !== "committed" || state.generation !== data.generation || !state.target)
|
|
610
|
+
throw new BridgeError(403, "This conductor generation is not active.");
|
|
611
|
+
const peer = await this.connection(state);
|
|
612
|
+
const authority = successionSchema.parse(await this.request(peer, `/v1/conductor/move/status?id=${state.id}`, undefined, 5_000));
|
|
613
|
+
if (authority.phase !== "committed" || authority.generation !== state.generation)
|
|
614
|
+
throw new BridgeError(403, "The original Hook fenced this conductor generation.");
|
|
615
|
+
await this.options.validate(state.target);
|
|
616
|
+
const notice = await this.request(peer, `/v1/conductor/move/notification?id=${state.id}&deliveryId=${data.deliveryId}`, undefined, 5_000);
|
|
617
|
+
return this.options.deliver(state.target, z.string().max(32768).parse(notice.text), data.deliveryId);
|
|
618
|
+
}
|
|
619
|
+
async notification(id, deliveryId) {
|
|
620
|
+
deliveryIdSchema.unwrap().parse(deliveryId);
|
|
621
|
+
await this.status(id);
|
|
622
|
+
return JSON.parse(await readFile(this.file(id, `notice-${deliveryId}`), "utf8"));
|
|
623
|
+
}
|
|
624
|
+
async reconcile() {
|
|
625
|
+
const state = (await readRoleState(this.options.root))?.succession;
|
|
626
|
+
if (!state || state.phase === "aborted")
|
|
627
|
+
return;
|
|
628
|
+
if (state.side === "origin" && state.phase === "committed")
|
|
629
|
+
await this.finish(state.id);
|
|
630
|
+
if (state.side === "successor") {
|
|
631
|
+
const peer = await this.connection(state);
|
|
632
|
+
const authority = successionSchema.parse(await this.request(peer, `/v1/conductor/move/status?id=${state.id}`, undefined, 5_000));
|
|
633
|
+
if (authority.phase === "committed" && state.phase !== "committed")
|
|
634
|
+
await this.activate({ id: state.id, generation: state.generation });
|
|
635
|
+
if (authority.target && !state.target)
|
|
636
|
+
await this.change(state.id, current => ({ ...current, target: authority.target }));
|
|
637
|
+
if (authority.phase === "aborted") {
|
|
638
|
+
if (state.phase === "committed") {
|
|
639
|
+
await updateConductorAuthority(this.options.root, async (role) => role.succession?.id !== state.id ? role : { ...role, conductor: null, succession: { ...role.succession, phase: "aborted", revoked: true } });
|
|
640
|
+
}
|
|
641
|
+
else
|
|
642
|
+
await this.abort(state.id);
|
|
643
|
+
const target = authority.target ?? state.target;
|
|
644
|
+
if (target)
|
|
645
|
+
await this.options.deliver(target, authority.revoked ? "The owner stopped the delegated conductor on the original Hook. You are no longer the conductor." : "The hand-over failed. You are not the conductor; the previous conductor keeps the role.", `move-${state.id}-conductor-failed`);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|