@sjawhar/pi-envoy 13.10.2 → 13.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/dist/dispatch.js +6 -2
- package/dist/envoy.js +601 -431
- package/dist/skills/envoy/references/roles.md +3 -2
- package/package.json +1 -1
package/dist/envoy.js
CHANGED
|
@@ -35534,6 +35534,9 @@ function mergeInterestSources(registryTopics, liveTopics) {
|
|
|
35534
35534
|
}
|
|
35535
35535
|
return interests;
|
|
35536
35536
|
}
|
|
35537
|
+
function newClientInstanceID() {
|
|
35538
|
+
return Array.from(crypto.getRandomValues(new Uint8Array(16)), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
35539
|
+
}
|
|
35537
35540
|
function expandSubscriptionTopics(topics) {
|
|
35538
35541
|
const expanded = new Set;
|
|
35539
35542
|
for (const topic of topics) {
|
|
@@ -35628,6 +35631,7 @@ function createEnvoyClient(config) {
|
|
|
35628
35631
|
...input.capabilities === undefined ? {} : { capabilities: input.capabilities },
|
|
35629
35632
|
driving: input.driving,
|
|
35630
35633
|
...input.selfSubscribed === undefined ? {} : { self_subscribed: input.selfSubscribed },
|
|
35634
|
+
...input.clientInstanceID === undefined ? {} : { client_instance_id: input.clientInstanceID },
|
|
35631
35635
|
host: machineID(),
|
|
35632
35636
|
plugin: input.plugin.name,
|
|
35633
35637
|
plugin_version: input.plugin.version
|
|
@@ -35671,8 +35675,11 @@ function createEnvoyClient(config) {
|
|
|
35671
35675
|
...response.holder === undefined ? {} : { holder: response.holder }
|
|
35672
35676
|
};
|
|
35673
35677
|
},
|
|
35674
|
-
unregisterSession: async (
|
|
35675
|
-
|
|
35678
|
+
unregisterSession: async (input) => {
|
|
35679
|
+
const query = input.clientInstanceID === undefined ? "" : `?client_instance_id=${encodeURIComponent(input.clientInstanceID)}`;
|
|
35680
|
+
await request(`/v1/sessions/${encodeURIComponent(input.sessionID)}${query}`, {
|
|
35681
|
+
method: "DELETE"
|
|
35682
|
+
});
|
|
35676
35683
|
},
|
|
35677
35684
|
setRole: async (input) => {
|
|
35678
35685
|
const soft = input.soft === true;
|
|
@@ -37269,7 +37276,7 @@ var import_nats2 = __toESM(require_mod4(), 1);
|
|
|
37269
37276
|
|
|
37270
37277
|
// package.json
|
|
37271
37278
|
var name = "@sjawhar/pi-envoy";
|
|
37272
|
-
var version2 = "13.
|
|
37279
|
+
var version2 = "13.11.1";
|
|
37273
37280
|
var package_default = {
|
|
37274
37281
|
name,
|
|
37275
37282
|
version: version2,
|
|
@@ -38248,6 +38255,448 @@ function opensAsk({
|
|
|
38248
38255
|
}
|
|
38249
38256
|
}
|
|
38250
38257
|
|
|
38258
|
+
// src/registration-state.ts
|
|
38259
|
+
class AbandonedTaskError extends Error {
|
|
38260
|
+
}
|
|
38261
|
+
function taskWasAbandoned(error) {
|
|
38262
|
+
return error instanceof AbandonedTaskError;
|
|
38263
|
+
}
|
|
38264
|
+
|
|
38265
|
+
class StepRefused extends Error {
|
|
38266
|
+
}
|
|
38267
|
+
function createRegistrationState(deps) {
|
|
38268
|
+
const clientInstanceID = newClientInstanceID();
|
|
38269
|
+
let claimedRoleTopic;
|
|
38270
|
+
let claimedRoleSessionID;
|
|
38271
|
+
let holdUnconfirmed = false;
|
|
38272
|
+
const pendingHandBacks = new Map;
|
|
38273
|
+
const abandonedHolds = new Map;
|
|
38274
|
+
const refusedRoles = new Map;
|
|
38275
|
+
let roleNoticesFor;
|
|
38276
|
+
const roleNoticeSubjects = new Set;
|
|
38277
|
+
let roleEnds = 0;
|
|
38278
|
+
let registeredFor = "";
|
|
38279
|
+
let registeredAt = -Infinity;
|
|
38280
|
+
let establishedSession = "";
|
|
38281
|
+
let roleSettledFor = "";
|
|
38282
|
+
const agentSessions = new Set;
|
|
38283
|
+
let inboundOpenFor = "";
|
|
38284
|
+
const heldRole = () => claimedRoleTopic?.slice(ROLE_TOPIC_PREFIX.length);
|
|
38285
|
+
const isRoleBound = (subject) => roleNoticeSubjects.has(subject);
|
|
38286
|
+
const endRole = () => {
|
|
38287
|
+
claimedRoleTopic = undefined;
|
|
38288
|
+
claimedRoleSessionID = undefined;
|
|
38289
|
+
roleEnds += 1;
|
|
38290
|
+
for (const subject of roleNoticeSubjects)
|
|
38291
|
+
deps.closeSubject(subject);
|
|
38292
|
+
roleNoticeSubjects.clear();
|
|
38293
|
+
};
|
|
38294
|
+
const handBackAt = async (topic, sessionID) => {
|
|
38295
|
+
await deps.client().unsubscribe({ sessionID, topics: [topic] });
|
|
38296
|
+
};
|
|
38297
|
+
const refreshRegistration = (input) => {
|
|
38298
|
+
for (const [key, hold] of abandonedHolds) {
|
|
38299
|
+
if (hold.id !== input.sessionID)
|
|
38300
|
+
continue;
|
|
38301
|
+
abandonedHolds.delete(key);
|
|
38302
|
+
pendingHandBacks.set(key, { topic: hold.topic, id: input.sessionID });
|
|
38303
|
+
}
|
|
38304
|
+
return deps.client().subscribe({ ...input, clientInstanceID }).then((result) => {
|
|
38305
|
+
registeredFor = input.sessionID;
|
|
38306
|
+
registeredAt = Date.now();
|
|
38307
|
+
return result;
|
|
38308
|
+
});
|
|
38309
|
+
};
|
|
38310
|
+
const recordClaimAfterDeadline = (topic, sessionID) => {
|
|
38311
|
+
pendingHandBacks.set(`${topic}@${sessionID}`, { topic, id: sessionID });
|
|
38312
|
+
};
|
|
38313
|
+
const moveHold = async (topic, sessionID, previousTopic, previousHoldID, recordClaim) => {
|
|
38314
|
+
const movedOff = previousTopic === topic ? undefined : previousTopic;
|
|
38315
|
+
if (movedOff !== undefined)
|
|
38316
|
+
endRole();
|
|
38317
|
+
claimedRoleTopic = topic;
|
|
38318
|
+
claimedRoleSessionID = sessionID;
|
|
38319
|
+
recordClaim?.();
|
|
38320
|
+
if (movedOff !== undefined && previousHoldID)
|
|
38321
|
+
await handBackAt(movedOff, previousHoldID);
|
|
38322
|
+
};
|
|
38323
|
+
const observeAbandonedHold = async (topic, role, holder, live) => {
|
|
38324
|
+
const key = `${topic}@${holder}`;
|
|
38325
|
+
const abandoned = abandonedHolds.get(key);
|
|
38326
|
+
if (abandoned === undefined)
|
|
38327
|
+
return false;
|
|
38328
|
+
const seen = await deps.client().getRole(role).then((info) => info.holder === holder ? info.last_seen : undefined, () => {
|
|
38329
|
+
return;
|
|
38330
|
+
});
|
|
38331
|
+
if (!live() || seen === undefined)
|
|
38332
|
+
return true;
|
|
38333
|
+
if (abandoned.lastSeen !== undefined && seen !== abandoned.lastSeen) {
|
|
38334
|
+
abandonedHolds.delete(key);
|
|
38335
|
+
return false;
|
|
38336
|
+
}
|
|
38337
|
+
abandonedHolds.set(key, { topic, id: abandoned.id, lastSeen: seen });
|
|
38338
|
+
return true;
|
|
38339
|
+
};
|
|
38340
|
+
const createHandle = (live) => {
|
|
38341
|
+
let bound;
|
|
38342
|
+
let refused = false;
|
|
38343
|
+
const refusal = { claimAdopted: false };
|
|
38344
|
+
const acting = () => {
|
|
38345
|
+
if (refused)
|
|
38346
|
+
return false;
|
|
38347
|
+
if (live() && (bound === undefined || deps.sessionOpen(bound)))
|
|
38348
|
+
return true;
|
|
38349
|
+
refused = true;
|
|
38350
|
+
return false;
|
|
38351
|
+
};
|
|
38352
|
+
const refuse = (message) => {
|
|
38353
|
+
refused = true;
|
|
38354
|
+
throw new StepRefused(message);
|
|
38355
|
+
};
|
|
38356
|
+
const guard = () => {
|
|
38357
|
+
if (acting())
|
|
38358
|
+
return;
|
|
38359
|
+
refuse(live() ? `the task acting for session ${bound} refused a step: that session is no longer the open one` : "the task refused a step: it was abandoned at its deadline");
|
|
38360
|
+
};
|
|
38361
|
+
const actingFor = () => {
|
|
38362
|
+
if (bound === undefined) {
|
|
38363
|
+
throw new Error("this queued task has not named the session it acts for (actFor)");
|
|
38364
|
+
}
|
|
38365
|
+
return bound;
|
|
38366
|
+
};
|
|
38367
|
+
const handBackHeldRole = async (tellUser) => {
|
|
38368
|
+
guard();
|
|
38369
|
+
const role = heldRole();
|
|
38370
|
+
const holdID = claimedRoleSessionID;
|
|
38371
|
+
const topic = claimedRoleTopic;
|
|
38372
|
+
if (role === undefined || topic === undefined)
|
|
38373
|
+
return;
|
|
38374
|
+
const pluginHeld = roleNoticeSubjects.size > 0;
|
|
38375
|
+
endRole();
|
|
38376
|
+
if (holdID !== undefined && holdID !== "") {
|
|
38377
|
+
await handBackAt(topic, holdID).catch((error) => deps.log.warn("envoy: a role this process gave up was not handed back", {
|
|
38378
|
+
role,
|
|
38379
|
+
sessionID: holdID,
|
|
38380
|
+
error: messageFor(error)
|
|
38381
|
+
}));
|
|
38382
|
+
}
|
|
38383
|
+
if (tellUser) {
|
|
38384
|
+
const host = bound ?? "";
|
|
38385
|
+
deps.log.warn("envoy: role ended after a move with no session event", {
|
|
38386
|
+
role,
|
|
38387
|
+
sessionID: host
|
|
38388
|
+
});
|
|
38389
|
+
deps.notify(pluginHeld ? `envoy: role ${role} ended: the host moved to session ${host} with no session event, and that session's records do not name it; the plugin that opened its notices claims it again on its next session switch or restart` : `envoy: role ${role} ended: the host moved to session ${host} with no session event, and that session's records do not name it; if this session should hold it, run envoy_role_set`, "warning");
|
|
38390
|
+
}
|
|
38391
|
+
return { role, pluginHeld };
|
|
38392
|
+
};
|
|
38393
|
+
const settleEndedHolds = async (wanted) => {
|
|
38394
|
+
guard();
|
|
38395
|
+
let restored = false;
|
|
38396
|
+
const failed = [];
|
|
38397
|
+
for (const [key, hold] of pendingHandBacks) {
|
|
38398
|
+
if (!acting())
|
|
38399
|
+
break;
|
|
38400
|
+
pendingHandBacks.delete(key);
|
|
38401
|
+
if (wanted !== undefined && hold.topic === ROLE_TOPIC_PREFIX + wanted && deps.sessionOpen(hold.id)) {
|
|
38402
|
+
claimedRoleTopic = hold.topic;
|
|
38403
|
+
claimedRoleSessionID = hold.id;
|
|
38404
|
+
holdUnconfirmed = true;
|
|
38405
|
+
restored = true;
|
|
38406
|
+
continue;
|
|
38407
|
+
}
|
|
38408
|
+
await handBackAt(hold.topic, hold.id).catch((error) => {
|
|
38409
|
+
failed.push({ key, ...hold });
|
|
38410
|
+
deps.log.warn("envoy: a role this process gave up was not handed back", {
|
|
38411
|
+
topic: hold.topic,
|
|
38412
|
+
sessionID: hold.id,
|
|
38413
|
+
error: messageFor(error)
|
|
38414
|
+
});
|
|
38415
|
+
});
|
|
38416
|
+
}
|
|
38417
|
+
for (const { key, topic, id } of failed)
|
|
38418
|
+
abandonedHolds.set(key, { topic, id });
|
|
38419
|
+
guard();
|
|
38420
|
+
return restored;
|
|
38421
|
+
};
|
|
38422
|
+
const claimRole = async (role, options = {}) => {
|
|
38423
|
+
const id = actingFor();
|
|
38424
|
+
guard();
|
|
38425
|
+
const topic = ROLE_TOPIC_PREFIX + role;
|
|
38426
|
+
const previousTopic = claimedRoleTopic;
|
|
38427
|
+
const previousHoldID = claimedRoleSessionID;
|
|
38428
|
+
const result = await deps.client().setRole({
|
|
38429
|
+
sessionID: id,
|
|
38430
|
+
role,
|
|
38431
|
+
soft: options.soft,
|
|
38432
|
+
previousSessionID: options.previousSessionID
|
|
38433
|
+
});
|
|
38434
|
+
if (!live()) {
|
|
38435
|
+
if (result.claimed)
|
|
38436
|
+
recordClaimAfterDeadline(topic, id);
|
|
38437
|
+
refuse(`the role move that claimed ${role} for session ${id} was abandoned at its deadline while the claim was in flight`);
|
|
38438
|
+
}
|
|
38439
|
+
if (result.claimed) {
|
|
38440
|
+
refusedRoles.delete(id);
|
|
38441
|
+
holdUnconfirmed = false;
|
|
38442
|
+
for (const [key, hold] of abandonedHolds) {
|
|
38443
|
+
if (hold.topic === topic)
|
|
38444
|
+
abandonedHolds.delete(key);
|
|
38445
|
+
}
|
|
38446
|
+
for (const [key, hold] of pendingHandBacks) {
|
|
38447
|
+
if (hold.topic === topic)
|
|
38448
|
+
pendingHandBacks.delete(key);
|
|
38449
|
+
}
|
|
38450
|
+
}
|
|
38451
|
+
if (!deps.sessionOpen(id)) {
|
|
38452
|
+
if (result.claimed) {
|
|
38453
|
+
await moveHold(topic, id, previousTopic, previousHoldID);
|
|
38454
|
+
refusal.claimAdopted = true;
|
|
38455
|
+
}
|
|
38456
|
+
refuse(`the session moved on while its claim of role ${role} was in flight`);
|
|
38457
|
+
}
|
|
38458
|
+
if (!result.claimed) {
|
|
38459
|
+
endRole();
|
|
38460
|
+
const waitingOnReap = await observeAbandonedHold(topic, role, result.holder, live);
|
|
38461
|
+
const notRecorded = !live() ? "the move was abandoned at its deadline before the refusal was recorded" : deps.sessionOpen(id) ? undefined : "the session moved on before the refusal was recorded";
|
|
38462
|
+
if (notRecorded === undefined && !waitingOnReap)
|
|
38463
|
+
refusedRoles.set(id, role);
|
|
38464
|
+
deps.log.warn(notRecorded !== undefined ? `envoy: role held by another live session; ${notRecorded}` : waitingOnReap ? "envoy: role still held by a session this process left; claiming again once the listener reaps it" : "envoy: role held by another live session; not reclaimed", { role, sessionID: id, holder: result.holder });
|
|
38465
|
+
return false;
|
|
38466
|
+
}
|
|
38467
|
+
await moveHold(topic, id, previousTopic, previousHoldID, () => deps.appendRoleClaimEntry(role));
|
|
38468
|
+
return true;
|
|
38469
|
+
};
|
|
38470
|
+
const reassertRole = async (role, afterOutage) => {
|
|
38471
|
+
const id = actingFor();
|
|
38472
|
+
guard();
|
|
38473
|
+
const topic = ROLE_TOPIC_PREFIX + role;
|
|
38474
|
+
const client = deps.client();
|
|
38475
|
+
const stillHolds = () => claimedRoleTopic === topic;
|
|
38476
|
+
const holder = await client.getRole(role).then((info) => info.holder, (error) => {
|
|
38477
|
+
if (error instanceof EnvoyApiError && error.details.status === 404)
|
|
38478
|
+
return;
|
|
38479
|
+
throw error;
|
|
38480
|
+
});
|
|
38481
|
+
guard();
|
|
38482
|
+
if (!stillHolds())
|
|
38483
|
+
return { decided: false };
|
|
38484
|
+
if (holder === id) {
|
|
38485
|
+
return afterOutage ? { decided: true, regain: "reregistered" } : { decided: true };
|
|
38486
|
+
}
|
|
38487
|
+
const result = await client.setRole({ sessionID: id, role, soft: true });
|
|
38488
|
+
guard();
|
|
38489
|
+
if (!stillHolds())
|
|
38490
|
+
return { decided: false };
|
|
38491
|
+
if (!result.claimed) {
|
|
38492
|
+
endRole();
|
|
38493
|
+
refusedRoles.set(id, role);
|
|
38494
|
+
deps.log.warn("envoy: role re-assertion refused; held by another live session", {
|
|
38495
|
+
role,
|
|
38496
|
+
sessionID: id,
|
|
38497
|
+
holder: result.holder
|
|
38498
|
+
});
|
|
38499
|
+
deps.notify(`envoy: role ${role} is now held by session ${result.holder}; this session no longer holds it`, "warning");
|
|
38500
|
+
return { decided: true };
|
|
38501
|
+
}
|
|
38502
|
+
deps.log.warn("envoy: role re-asserted after the listener lost the claim", {
|
|
38503
|
+
role,
|
|
38504
|
+
sessionID: id,
|
|
38505
|
+
previousHolder: holder ?? null
|
|
38506
|
+
});
|
|
38507
|
+
return { decided: true, regain: "reclaimed" };
|
|
38508
|
+
};
|
|
38509
|
+
const dial = async () => {
|
|
38510
|
+
guard();
|
|
38511
|
+
const dialled = await deps.dial();
|
|
38512
|
+
if (!acting()) {
|
|
38513
|
+
await dialled.close().catch(() => {});
|
|
38514
|
+
refuse(live() ? `the task acting for session ${bound} dialled NATS for a session that is no longer the open one; the connection it opened was closed` : "the task that dialled NATS was abandoned at its deadline while the dial was in flight; the connection it opened was closed");
|
|
38515
|
+
}
|
|
38516
|
+
return dialled;
|
|
38517
|
+
};
|
|
38518
|
+
const connect = async () => {
|
|
38519
|
+
guard();
|
|
38520
|
+
const connected = await deps.connect(dial);
|
|
38521
|
+
if (!acting()) {
|
|
38522
|
+
refuse(live() ? `the task acting for session ${bound} connected to NATS for a session that is no longer the open one` : "the task that connected to NATS was abandoned at its deadline while the connection was made");
|
|
38523
|
+
}
|
|
38524
|
+
return connected;
|
|
38525
|
+
};
|
|
38526
|
+
const handle = {
|
|
38527
|
+
actFor: (id) => {
|
|
38528
|
+
if (bound !== undefined) {
|
|
38529
|
+
throw new Error(`this queued task already acts for session ${bound} and cannot act for ${id}`);
|
|
38530
|
+
}
|
|
38531
|
+
bound = id;
|
|
38532
|
+
},
|
|
38533
|
+
check: guard,
|
|
38534
|
+
register: (input) => {
|
|
38535
|
+
guard();
|
|
38536
|
+
return refreshRegistration(input);
|
|
38537
|
+
},
|
|
38538
|
+
markEstablished: (id) => {
|
|
38539
|
+
guard();
|
|
38540
|
+
establishedSession = id;
|
|
38541
|
+
},
|
|
38542
|
+
forgetOtherAgentSessions: (host) => {
|
|
38543
|
+
guard();
|
|
38544
|
+
const closed = [];
|
|
38545
|
+
for (const other of agentSessions) {
|
|
38546
|
+
if (other === host)
|
|
38547
|
+
continue;
|
|
38548
|
+
closed.push(other);
|
|
38549
|
+
agentSessions.delete(other);
|
|
38550
|
+
if (inboundOpenFor === other)
|
|
38551
|
+
inboundOpenFor = "";
|
|
38552
|
+
}
|
|
38553
|
+
return closed;
|
|
38554
|
+
},
|
|
38555
|
+
markAgentSessionOpen: (id) => {
|
|
38556
|
+
guard();
|
|
38557
|
+
agentSessions.add(id);
|
|
38558
|
+
},
|
|
38559
|
+
markInboundOpen: (id) => {
|
|
38560
|
+
guard();
|
|
38561
|
+
inboundOpenFor = id;
|
|
38562
|
+
},
|
|
38563
|
+
markRoleSettled: (id) => {
|
|
38564
|
+
guard();
|
|
38565
|
+
roleSettledFor = id;
|
|
38566
|
+
},
|
|
38567
|
+
handBackHeldRole,
|
|
38568
|
+
settleEndedHolds,
|
|
38569
|
+
claimRole,
|
|
38570
|
+
reassertRole,
|
|
38571
|
+
noteNoticesClosed: (role) => {
|
|
38572
|
+
guard();
|
|
38573
|
+
if (roleNoticesFor !== role || roleNoticeSubjects.size > 0)
|
|
38574
|
+
return;
|
|
38575
|
+
roleNoticesFor = undefined;
|
|
38576
|
+
deps.notify(`envoy: role ${role} is held again, but the notices a plugin opened for it stay closed until that plugin claims it again on its next session switch or restart`, "warning");
|
|
38577
|
+
},
|
|
38578
|
+
readRoleHolder: async (role) => {
|
|
38579
|
+
guard();
|
|
38580
|
+
return deps.client().getRole(role).then((info) => info.holder, () => {
|
|
38581
|
+
return;
|
|
38582
|
+
});
|
|
38583
|
+
},
|
|
38584
|
+
readInterest: async (sessionID) => {
|
|
38585
|
+
guard();
|
|
38586
|
+
return deps.client().getInterest(sessionID).catch(() => {
|
|
38587
|
+
return;
|
|
38588
|
+
});
|
|
38589
|
+
},
|
|
38590
|
+
unregisterTopics: async (sessionID, topics, warning) => {
|
|
38591
|
+
guard();
|
|
38592
|
+
await deps.client().unsubscribe({ sessionID, topics: [...topics] }).catch((error) => deps.log.warn(warning, { sessionID, topics, error: messageFor(error) }));
|
|
38593
|
+
},
|
|
38594
|
+
connect,
|
|
38595
|
+
subscribe: async (topic) => {
|
|
38596
|
+
guard();
|
|
38597
|
+
const opened = await deps.subscribeTopic(topic, connect);
|
|
38598
|
+
guard();
|
|
38599
|
+
return opened;
|
|
38600
|
+
}
|
|
38601
|
+
};
|
|
38602
|
+
return { handle, refusal };
|
|
38603
|
+
};
|
|
38604
|
+
const createQueue = () => {
|
|
38605
|
+
let tail = Promise.resolve();
|
|
38606
|
+
return {
|
|
38607
|
+
run(task, onRefused) {
|
|
38608
|
+
const next = tail.then(() => new Promise((resolve, reject) => {
|
|
38609
|
+
let abandoned = false;
|
|
38610
|
+
const taskDeadlineMs = deps.deadlineMs();
|
|
38611
|
+
const timer = setTimeout(() => {
|
|
38612
|
+
abandoned = true;
|
|
38613
|
+
reject(new AbandonedTaskError(`a queued task did not settle within ${taskDeadlineMs}ms and was abandoned; the queue moved on`));
|
|
38614
|
+
}, taskDeadlineMs);
|
|
38615
|
+
const running = createHandle(() => !abandoned);
|
|
38616
|
+
(async () => task(running.handle))().then((value) => {
|
|
38617
|
+
clearTimeout(timer);
|
|
38618
|
+
if (!abandoned)
|
|
38619
|
+
resolve(value);
|
|
38620
|
+
}, (error) => {
|
|
38621
|
+
clearTimeout(timer);
|
|
38622
|
+
if (abandoned)
|
|
38623
|
+
return;
|
|
38624
|
+
if (error instanceof StepRefused) {
|
|
38625
|
+
onRefused?.(running.refusal);
|
|
38626
|
+
resolve(undefined);
|
|
38627
|
+
} else
|
|
38628
|
+
reject(error);
|
|
38629
|
+
});
|
|
38630
|
+
}));
|
|
38631
|
+
tail = next.catch(() => {
|
|
38632
|
+
return;
|
|
38633
|
+
});
|
|
38634
|
+
return next;
|
|
38635
|
+
},
|
|
38636
|
+
settled: () => tail
|
|
38637
|
+
};
|
|
38638
|
+
};
|
|
38639
|
+
return {
|
|
38640
|
+
clientInstanceID,
|
|
38641
|
+
passes: createQueue(),
|
|
38642
|
+
roleMoves: createQueue(),
|
|
38643
|
+
refreshRegistration,
|
|
38644
|
+
registrationFresherThan: (sessionID, ms) => registeredFor === sessionID && Date.now() - registeredAt < ms,
|
|
38645
|
+
deregister: async (sessionID) => {
|
|
38646
|
+
await deps.client().unregisterSession({ sessionID, clientInstanceID });
|
|
38647
|
+
},
|
|
38648
|
+
clearRefusal: (sessionID) => {
|
|
38649
|
+
refusedRoles.delete(sessionID);
|
|
38650
|
+
},
|
|
38651
|
+
endUnwantedRole: (host, recordedRole, lineage) => {
|
|
38652
|
+
const held = heldRole();
|
|
38653
|
+
const topic = claimedRoleTopic;
|
|
38654
|
+
const holdID = claimedRoleSessionID;
|
|
38655
|
+
if (held === undefined || topic === undefined || holdID === undefined)
|
|
38656
|
+
return;
|
|
38657
|
+
const carried = holdID === host || lineage.includes(holdID);
|
|
38658
|
+
const wanted = recordedRole === undefined ? carried : recordedRole === held;
|
|
38659
|
+
if (wanted)
|
|
38660
|
+
return;
|
|
38661
|
+
endRole();
|
|
38662
|
+
if (holdID !== "")
|
|
38663
|
+
pendingHandBacks.set(`${topic}@${holdID}`, { topic, id: holdID });
|
|
38664
|
+
},
|
|
38665
|
+
markRoleBoundSubjects: (subjects, role) => {
|
|
38666
|
+
const marked = [];
|
|
38667
|
+
for (const subject of subjects) {
|
|
38668
|
+
if (roleNoticeSubjects.has(subject))
|
|
38669
|
+
continue;
|
|
38670
|
+
roleNoticeSubjects.add(subject);
|
|
38671
|
+
marked.push(subject);
|
|
38672
|
+
}
|
|
38673
|
+
roleNoticesFor = role;
|
|
38674
|
+
return marked;
|
|
38675
|
+
},
|
|
38676
|
+
unmarkRoleBoundSubjects: (subjects) => {
|
|
38677
|
+
for (const subject of subjects)
|
|
38678
|
+
roleNoticeSubjects.delete(subject);
|
|
38679
|
+
},
|
|
38680
|
+
recordClaimAfterDeadline,
|
|
38681
|
+
adoptClaimAfterSessionMoved: moveHold,
|
|
38682
|
+
heldRole,
|
|
38683
|
+
holdsRoleTopic: (topic) => claimedRoleTopic === topic,
|
|
38684
|
+
holdSessionID: () => claimedRoleSessionID,
|
|
38685
|
+
isRoleBound,
|
|
38686
|
+
roleBoundSubjects: () => [...roleNoticeSubjects],
|
|
38687
|
+
roleSnapshot: (topic) => ({
|
|
38688
|
+
roleBound: expandSubscriptionTopics([topic]).some(isRoleBound),
|
|
38689
|
+
endsBefore: roleEnds
|
|
38690
|
+
}),
|
|
38691
|
+
refusalFor: (sessionID) => refusedRoles.get(sessionID),
|
|
38692
|
+
roleEndCount: () => roleEnds,
|
|
38693
|
+
holdIsUnconfirmed: () => holdUnconfirmed,
|
|
38694
|
+
establishedFor: () => establishedSession,
|
|
38695
|
+
inboundOpenSession: () => inboundOpenFor,
|
|
38696
|
+
roleSettledSession: () => roleSettledFor
|
|
38697
|
+
};
|
|
38698
|
+
}
|
|
38699
|
+
|
|
38251
38700
|
// src/session-answers-context.ts
|
|
38252
38701
|
var SIDE_TURN_NOTICE = "Ephemeral side-channel turn; reuses current conversation context.";
|
|
38253
38702
|
var HANDOFF_PROMPT = `<critical>
|
|
@@ -42163,39 +42612,6 @@ var NATS_RETRY_INTERVAL_MS = 15000;
|
|
|
42163
42612
|
var DELIVERY_PICTURES_TIMEOUT_MS = 20000;
|
|
42164
42613
|
var BTW_EVENTS_LIMIT = 1000;
|
|
42165
42614
|
var REPLAYED_BTW_NOTE = "This BTW was asked while this session was not taking messages. It is shown for your information only: it runs no side turn and needs no answer. Dispatch shows whether it was answered.";
|
|
42166
|
-
var UNFENCED = () => true;
|
|
42167
|
-
|
|
42168
|
-
class AbandonedTaskError extends Error {
|
|
42169
|
-
}
|
|
42170
|
-
function serialQueue(deadlineMs) {
|
|
42171
|
-
let tail = Promise.resolve();
|
|
42172
|
-
return {
|
|
42173
|
-
run(task) {
|
|
42174
|
-
const next = tail.then(() => new Promise((resolve, reject) => {
|
|
42175
|
-
let abandoned = false;
|
|
42176
|
-
const taskDeadlineMs = deadlineMs();
|
|
42177
|
-
const timer = setTimeout(() => {
|
|
42178
|
-
abandoned = true;
|
|
42179
|
-
reject(new AbandonedTaskError(`a queued task did not settle within ${taskDeadlineMs}ms and was abandoned; the queue moved on`));
|
|
42180
|
-
}, taskDeadlineMs);
|
|
42181
|
-
(async () => task(() => !abandoned))().then((value) => {
|
|
42182
|
-
clearTimeout(timer);
|
|
42183
|
-
if (!abandoned)
|
|
42184
|
-
resolve(value);
|
|
42185
|
-
}, (error) => {
|
|
42186
|
-
clearTimeout(timer);
|
|
42187
|
-
if (!abandoned)
|
|
42188
|
-
reject(error);
|
|
42189
|
-
});
|
|
42190
|
-
}));
|
|
42191
|
-
tail = next.catch(() => {
|
|
42192
|
-
return;
|
|
42193
|
-
});
|
|
42194
|
-
return next;
|
|
42195
|
-
},
|
|
42196
|
-
settled: () => tail
|
|
42197
|
-
};
|
|
42198
|
-
}
|
|
42199
42615
|
var CAPABILITIES_WITHOUT_BTW = DELIVERY_CAPABILITIES.filter((capability) => capability !== "btw");
|
|
42200
42616
|
var ClaimIdentitySchema = DedupeIdentitySchema.extend({
|
|
42201
42617
|
event_id: DedupeIdentitySchema.shape.event_id.catch(undefined),
|
|
@@ -42372,20 +42788,7 @@ function envoyExtension(pi) {
|
|
|
42372
42788
|
}
|
|
42373
42789
|
return lineage;
|
|
42374
42790
|
};
|
|
42375
|
-
let establishedSession = "";
|
|
42376
42791
|
const notifiedSessions = new Set;
|
|
42377
|
-
let claimedRoleTopic;
|
|
42378
|
-
let claimedRoleSessionID;
|
|
42379
|
-
let holdUnconfirmed = false;
|
|
42380
|
-
const pendingHandBacks = new Map;
|
|
42381
|
-
const abandonedHolds = new Map;
|
|
42382
|
-
const refusedRoles = new Map;
|
|
42383
|
-
let roleNoticesFor;
|
|
42384
|
-
const roleNoticeSubjects = new Set;
|
|
42385
|
-
let roleEnds = 0;
|
|
42386
|
-
const roleMoves = serialQueue(passDeadlineMs);
|
|
42387
|
-
const isRoleBound = (subject) => roleNoticeSubjects.has(subject);
|
|
42388
|
-
const heldRole = () => claimedRoleTopic?.slice(ROLE_TOPIC_PREFIX.length);
|
|
42389
42792
|
let activeSessionContext;
|
|
42390
42793
|
const inbox = [];
|
|
42391
42794
|
let askAwareness = {
|
|
@@ -42540,20 +42943,23 @@ function envoyExtension(pi) {
|
|
|
42540
42943
|
}
|
|
42541
42944
|
return inbound;
|
|
42542
42945
|
};
|
|
42543
|
-
const
|
|
42544
|
-
if (connection?.isClosed() === false)
|
|
42545
|
-
return connection;
|
|
42946
|
+
const dialNats = async () => {
|
|
42546
42947
|
const { natsUrls, natsSeed } = requireInbound();
|
|
42547
|
-
|
|
42948
|
+
return import_nats2.connect(natsConnectionOptions({
|
|
42548
42949
|
servers: natsUrls,
|
|
42549
42950
|
name: `omp-${sessionID || "unknown"}`,
|
|
42550
42951
|
seed: natsSeed
|
|
42551
42952
|
}));
|
|
42552
|
-
|
|
42953
|
+
};
|
|
42954
|
+
const ensureConnection = async (dial = dialNats) => {
|
|
42955
|
+
if (connection?.isClosed() === false)
|
|
42956
|
+
return connection;
|
|
42957
|
+
const dialled = await dial();
|
|
42958
|
+
if (shuttingDown || connection?.isClosed() === false) {
|
|
42553
42959
|
await dialled.close().catch(() => {});
|
|
42554
42960
|
if (!shuttingDown && connection?.isClosed() === false)
|
|
42555
42961
|
return connection;
|
|
42556
|
-
throw new
|
|
42962
|
+
throw new Error("the session shut down while its NATS dial was in flight; the connection it opened was closed");
|
|
42557
42963
|
}
|
|
42558
42964
|
connection = dialled;
|
|
42559
42965
|
watchReconnects(dialled);
|
|
@@ -42904,7 +43310,7 @@ function envoyExtension(pi) {
|
|
|
42904
43310
|
return;
|
|
42905
43311
|
if (intentionallyClosed.delete(topic))
|
|
42906
43312
|
return;
|
|
42907
|
-
const snapshot = roleSnapshot(topic);
|
|
43313
|
+
const snapshot = registrationState.roleSnapshot(topic);
|
|
42908
43314
|
const retry = (delayMs) => {
|
|
42909
43315
|
awaitingRetry.add(topic);
|
|
42910
43316
|
setTimeout(() => {
|
|
@@ -42921,13 +43327,11 @@ function envoyExtension(pi) {
|
|
|
42921
43327
|
};
|
|
42922
43328
|
retry(RESUBSCRIBE_DELAY_MS);
|
|
42923
43329
|
};
|
|
42924
|
-
const subscribe = async (topic,
|
|
43330
|
+
const subscribe = async (topic, connect = ensureConnection) => {
|
|
42925
43331
|
const subjects = expandSubscriptionTopics([topic]).filter((candidate) => !subscriptions.has(candidate));
|
|
42926
43332
|
if (subjects.length === 0)
|
|
42927
43333
|
return false;
|
|
42928
|
-
const activeConnection = await
|
|
42929
|
-
if (!live())
|
|
42930
|
-
return false;
|
|
43334
|
+
const activeConnection = await connect();
|
|
42931
43335
|
for (const subject of subjects) {
|
|
42932
43336
|
const subscription = activeConnection.subscribe(subject);
|
|
42933
43337
|
intentionallyClosed.delete(subject);
|
|
@@ -42950,7 +43354,7 @@ function envoyExtension(pi) {
|
|
|
42950
43354
|
if (sessionID === "")
|
|
42951
43355
|
return [];
|
|
42952
43356
|
const open = [...subscriptions.keys(), ...awaitingRetry].filter((subject) => !intentionallyClosed.has(subject));
|
|
42953
|
-
return replayFollowedSubjects(agentSubject(sessionID), open, isRoleBound);
|
|
43357
|
+
return replayFollowedSubjects(agentSubject(sessionID), open, registrationState.isRoleBound);
|
|
42954
43358
|
};
|
|
42955
43359
|
const showReplayed = async (frame) => {
|
|
42956
43360
|
const rendered = renderInbound(frame.data, sessionID, frame.subject);
|
|
@@ -43013,7 +43417,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43013
43417
|
transport: active.client,
|
|
43014
43418
|
dedupe: delivered
|
|
43015
43419
|
});
|
|
43016
|
-
const replayOpen = () => !shuttingDown && sessionID !== "" &&
|
|
43420
|
+
const replayOpen = () => !shuttingDown && sessionID !== "" && registrationState.inboundOpenSession() === sessionID;
|
|
43017
43421
|
const triggerReplay = () => {
|
|
43018
43422
|
if (replay !== undefined && replayOpen())
|
|
43019
43423
|
replay.sweep();
|
|
@@ -43128,7 +43532,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43128
43532
|
});
|
|
43129
43533
|
}, RESUBSCRIBE_DELAY_MS);
|
|
43130
43534
|
};
|
|
43131
|
-
const watchAgentStream = async (session,
|
|
43535
|
+
const watchAgentStream = async (session, connect = ensureConnection) => {
|
|
43132
43536
|
if (agentStreamControlSession === session && agentStreamControl !== undefined)
|
|
43133
43537
|
return;
|
|
43134
43538
|
agentStreamControl?.unsubscribe();
|
|
@@ -43139,9 +43543,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43139
43543
|
agentStreamCommandsRefreshedOnWatchAt = -Infinity;
|
|
43140
43544
|
}
|
|
43141
43545
|
agentStreamControlSession = session;
|
|
43142
|
-
const connected = await
|
|
43143
|
-
if (!live())
|
|
43144
|
-
return;
|
|
43546
|
+
const connected = await connect();
|
|
43145
43547
|
const subscription = connected.subscribe(agentStreamControlSubject(session));
|
|
43146
43548
|
agentStreamControl = subscription;
|
|
43147
43549
|
pumpAgentStreamControl(subscription, session);
|
|
@@ -43176,39 +43578,29 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43176
43578
|
pi.on("retry_fallback_applied", async () => {
|
|
43177
43579
|
refreshAgentStreamSettings(sessionID);
|
|
43178
43580
|
});
|
|
43179
|
-
const endRole = () => {
|
|
43180
|
-
claimedRoleTopic = undefined;
|
|
43181
|
-
claimedRoleSessionID = undefined;
|
|
43182
|
-
roleEnds += 1;
|
|
43183
|
-
for (const subject of roleNoticeSubjects)
|
|
43184
|
-
closeIntentionally(subject);
|
|
43185
|
-
roleNoticeSubjects.clear();
|
|
43186
|
-
};
|
|
43187
|
-
const handBackRole = async () => {
|
|
43188
|
-
const role = heldRole();
|
|
43189
|
-
const holdID = claimedRoleSessionID;
|
|
43190
|
-
const topic = claimedRoleTopic;
|
|
43191
|
-
if (role === undefined || topic === undefined)
|
|
43192
|
-
return;
|
|
43193
|
-
const pluginHeld = roleNoticeSubjects.size > 0;
|
|
43194
|
-
endRole();
|
|
43195
|
-
if (holdID !== undefined && holdID !== "") {
|
|
43196
|
-
await requireEnvoy().client.unsubscribe({ sessionID: holdID, topics: [topic] }).catch((error) => logger3.warn("envoy: a role this process gave up was not handed back", {
|
|
43197
|
-
role,
|
|
43198
|
-
sessionID: holdID,
|
|
43199
|
-
error: messageFor(error)
|
|
43200
|
-
}));
|
|
43201
|
-
}
|
|
43202
|
-
return { role, pluginHeld };
|
|
43203
|
-
};
|
|
43204
43581
|
const hostSessionID = () => activeSessionContext?.sessionManager.getSessionId() ?? sessionID;
|
|
43205
43582
|
const sessionOpen = (id) => sessionID === id && hostSessionID() === id;
|
|
43206
|
-
const
|
|
43207
|
-
|
|
43208
|
-
|
|
43583
|
+
const sessionLeftDuringClaim = (from, role, claimAdopted) => new SessionLeftError(claimAdopted ? `the session moved from ${from} to ${hostSessionID()} while its claim of role ${role} was in flight; the next pass keeps the hold for the session the host is on, or hands it back` : `the session moved from ${from} to ${hostSessionID()} while its claim of role ${role} ran; nothing was claimed`);
|
|
43584
|
+
const registrationState = createRegistrationState({
|
|
43585
|
+
client: () => requireEnvoy().client,
|
|
43586
|
+
sessionOpen,
|
|
43587
|
+
deadlineMs: passDeadlineMs,
|
|
43588
|
+
notify: (message, level) => activeSessionContext?.ui.notify(message, level),
|
|
43589
|
+
log: logger3,
|
|
43590
|
+
appendRoleClaimEntry: (role) => {
|
|
43591
|
+
pi.appendEntry(ROLE_CLAIM_ENTRY, { role });
|
|
43592
|
+
if (activeSessionContext !== undefined)
|
|
43593
|
+
ensureHeartbeat(activeSessionContext);
|
|
43594
|
+
},
|
|
43595
|
+
dial: dialNats,
|
|
43596
|
+
connect: ensureConnection,
|
|
43597
|
+
subscribeTopic: subscribe,
|
|
43598
|
+
closeSubject: (subject) => {
|
|
43599
|
+
closeIntentionally(subject);
|
|
43600
|
+
}
|
|
43209
43601
|
});
|
|
43210
43602
|
const subscribeUnlessRoleEnds = async (topic, snapshot) => {
|
|
43211
|
-
const roleEnded = () => snapshot.roleBound &&
|
|
43603
|
+
const roleEnded = () => snapshot.roleBound && registrationState.roleEndCount() !== snapshot.endsBefore;
|
|
43212
43604
|
if (roleEnded())
|
|
43213
43605
|
return false;
|
|
43214
43606
|
await subscribe(topic);
|
|
@@ -43218,24 +43610,15 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43218
43610
|
closeIntentionally(subject);
|
|
43219
43611
|
return false;
|
|
43220
43612
|
};
|
|
43221
|
-
|
|
43222
|
-
let registeredAt = -Infinity;
|
|
43223
|
-
const registerSession = () => {
|
|
43224
|
-
const registeringFor = sessionID;
|
|
43613
|
+
const registrationInput = () => {
|
|
43225
43614
|
const own = agentSubject(sessionID);
|
|
43226
|
-
|
|
43227
|
-
if (hold.id !== sessionID)
|
|
43228
|
-
continue;
|
|
43229
|
-
abandonedHolds.delete(key);
|
|
43230
|
-
pendingHandBacks.set(key, { topic: hold.topic, id: sessionID });
|
|
43231
|
-
}
|
|
43232
|
-
return requireEnvoy().client.subscribe({
|
|
43615
|
+
return {
|
|
43233
43616
|
sessionID,
|
|
43234
43617
|
directory: sessionDirectory2,
|
|
43235
43618
|
topics: [
|
|
43236
43619
|
...new Set([
|
|
43237
43620
|
own,
|
|
43238
|
-
...[...subscriptions.keys()].filter((subject) => !isRoleBound(subject) && !reachesPrivateStreamSubject(own, subject))
|
|
43621
|
+
...[...subscriptions.keys()].filter((subject) => !registrationState.isRoleBound(subject) && !reachesPrivateStreamSubject(own, subject))
|
|
43239
43622
|
])
|
|
43240
43623
|
],
|
|
43241
43624
|
port: 0,
|
|
@@ -43244,59 +43627,9 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43244
43627
|
driving: false,
|
|
43245
43628
|
selfSubscribed: true,
|
|
43246
43629
|
plugin: { name, version: version2 }
|
|
43247
|
-
}
|
|
43248
|
-
registeredFor = registeringFor;
|
|
43249
|
-
registeredAt = Date.now();
|
|
43250
|
-
return result;
|
|
43251
|
-
});
|
|
43252
|
-
};
|
|
43253
|
-
const reassertRoleNow = async (id, role, afterOutage, context, live) => {
|
|
43254
|
-
const topic = ROLE_TOPIC_PREFIX + role;
|
|
43255
|
-
const { client } = requireEnvoy();
|
|
43256
|
-
const owns = () => live() && sessionOpen(id);
|
|
43257
|
-
const stillHolds = () => claimedRoleTopic === topic;
|
|
43258
|
-
const holder = await client.getRole(role).then((info) => info.holder, (error) => {
|
|
43259
|
-
if (error instanceof EnvoyApiError && error.details.status === 404)
|
|
43260
|
-
return;
|
|
43261
|
-
throw error;
|
|
43262
|
-
});
|
|
43263
|
-
if (!owns() || !stillHolds())
|
|
43264
|
-
return false;
|
|
43265
|
-
let reason;
|
|
43266
|
-
if (holder === id) {
|
|
43267
|
-
if (!afterOutage)
|
|
43268
|
-
return true;
|
|
43269
|
-
reason = "reregistered";
|
|
43270
|
-
} else {
|
|
43271
|
-
const result = await client.setRole({ sessionID: id, role, soft: true });
|
|
43272
|
-
if (!owns() || !stillHolds())
|
|
43273
|
-
return false;
|
|
43274
|
-
if (!result.claimed) {
|
|
43275
|
-
endRole();
|
|
43276
|
-
refusedRoles.set(id, role);
|
|
43277
|
-
logger3.warn("envoy: role re-assertion refused; held by another live session", {
|
|
43278
|
-
role,
|
|
43279
|
-
sessionID: id,
|
|
43280
|
-
holder: result.holder
|
|
43281
|
-
});
|
|
43282
|
-
context.ui.notify(`envoy: role ${role} is now held by session ${result.holder}; this session no longer holds it`, "warning");
|
|
43283
|
-
return true;
|
|
43284
|
-
}
|
|
43285
|
-
logger3.warn("envoy: role re-asserted after the listener lost the claim", {
|
|
43286
|
-
role,
|
|
43287
|
-
sessionID: id,
|
|
43288
|
-
previousHolder: holder ?? null
|
|
43289
|
-
});
|
|
43290
|
-
reason = "reclaimed";
|
|
43291
|
-
}
|
|
43292
|
-
const regained = legionRoleClaimBridge().regained;
|
|
43293
|
-
if (regained === undefined)
|
|
43294
|
-
return true;
|
|
43295
|
-
Promise.resolve().then(() => regained(role, reason)).catch((error) => {
|
|
43296
|
-
logger3.warn("envoy: role regain hook failed", { role, reason, error: messageFor(error) });
|
|
43297
|
-
});
|
|
43298
|
-
return true;
|
|
43630
|
+
};
|
|
43299
43631
|
};
|
|
43632
|
+
const registerSession = () => registrationState.refreshRegistration(registrationInput());
|
|
43300
43633
|
const ensureHeartbeat = (context) => {
|
|
43301
43634
|
if (heartbeatRegistered)
|
|
43302
43635
|
return;
|
|
@@ -43321,7 +43654,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43321
43654
|
triggerReplay();
|
|
43322
43655
|
if (healing) {
|
|
43323
43656
|
const refreshMs = requireEnvoy().settings.heartbeatMs;
|
|
43324
|
-
if (
|
|
43657
|
+
if (registrationState.registrationFresherThan(sessionID, refreshMs))
|
|
43325
43658
|
return;
|
|
43326
43659
|
noteStall("a pass has not settled a whole refresh interval after the last accepted registration");
|
|
43327
43660
|
registerSession().catch(() => {
|
|
@@ -43340,7 +43673,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43340
43673
|
reconcile(context, { heartbeat: true, afterOutage }).then(settled, (error) => {
|
|
43341
43674
|
if (error instanceof InboundError)
|
|
43342
43675
|
return settled(error.decided);
|
|
43343
|
-
if (error
|
|
43676
|
+
if (taskWasAbandoned(error)) {
|
|
43344
43677
|
registrationFailed = true;
|
|
43345
43678
|
noteStall(messageFor(error));
|
|
43346
43679
|
return;
|
|
@@ -43359,118 +43692,22 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43359
43692
|
}, requireEnvoy().settings.heartbeatMs);
|
|
43360
43693
|
heartbeatRegistered = true;
|
|
43361
43694
|
};
|
|
43362
|
-
const recoverRegisteredInterests = async (
|
|
43363
|
-
const
|
|
43364
|
-
const registry = await client.getInterest(sessionID).catch(() => {
|
|
43365
|
-
return;
|
|
43366
|
-
});
|
|
43367
|
-
if (!live())
|
|
43368
|
-
return;
|
|
43695
|
+
const recoverRegisteredInterests = async (handle) => {
|
|
43696
|
+
const registry = await handle.readInterest(sessionID);
|
|
43369
43697
|
const slackRetired = publishedInterface?.roleClaim.slackRoleOwned === true;
|
|
43370
43698
|
const registered = registry?.topics ?? [];
|
|
43371
43699
|
const retired = slackRetired ? registered.filter((topic) => slackTopicChannel(topic) !== undefined) : [];
|
|
43372
|
-
if (retired.length > 0)
|
|
43373
|
-
await
|
|
43374
|
-
logger3.warn("envoy: could not unregister a resumed controller's retired Slack topics", {
|
|
43375
|
-
sessionID,
|
|
43376
|
-
topics: retired,
|
|
43377
|
-
error: messageFor(error)
|
|
43378
|
-
});
|
|
43379
|
-
});
|
|
43380
|
-
}
|
|
43381
|
-
if (!live())
|
|
43382
|
-
return;
|
|
43700
|
+
if (retired.length > 0)
|
|
43701
|
+
await handle.unregisterTopics(sessionID, retired, "envoy: could not unregister a resumed controller's retired Slack topics");
|
|
43383
43702
|
const remembered = rememberedSubjects(activeSessionContext?.sessionManager.getEntries() ?? []).filter((topic) => !(slackRetired && slackTopicChannel(topic) !== undefined));
|
|
43384
43703
|
for (const topic of new Set([...registered, ...remembered])) {
|
|
43385
43704
|
if (topic === agentSubject(sessionID) || topic.startsWith(ROLE_TOPIC_PREFIX))
|
|
43386
43705
|
continue;
|
|
43387
43706
|
if (retired.includes(topic))
|
|
43388
43707
|
continue;
|
|
43389
|
-
await subscribe(topic
|
|
43708
|
+
await handle.subscribe(topic);
|
|
43390
43709
|
}
|
|
43391
43710
|
};
|
|
43392
|
-
const observeAbandonedHold = async (topic, role, holder, live) => {
|
|
43393
|
-
const key = `${topic}@${holder}`;
|
|
43394
|
-
const abandoned = abandonedHolds.get(key);
|
|
43395
|
-
if (abandoned === undefined)
|
|
43396
|
-
return false;
|
|
43397
|
-
const seen = await requireEnvoy().client.getRole(role).then((info) => info.holder === holder ? info.last_seen : undefined, () => {
|
|
43398
|
-
return;
|
|
43399
|
-
});
|
|
43400
|
-
if (!live() || seen === undefined)
|
|
43401
|
-
return true;
|
|
43402
|
-
if (abandoned.lastSeen !== undefined && seen !== abandoned.lastSeen) {
|
|
43403
|
-
abandonedHolds.delete(key);
|
|
43404
|
-
return false;
|
|
43405
|
-
}
|
|
43406
|
-
abandonedHolds.set(key, { topic, id: abandoned.id, lastSeen: seen });
|
|
43407
|
-
return true;
|
|
43408
|
-
};
|
|
43409
|
-
const setEnvoyRole = async (role, forSession, live, options = {}) => {
|
|
43410
|
-
if (!sessionOpen(forSession)) {
|
|
43411
|
-
throw new SessionLeftError(`the session moved from ${forSession} to ${hostSessionID()} before its claim of role ${role} ran; nothing was claimed`);
|
|
43412
|
-
}
|
|
43413
|
-
const topic = ROLE_TOPIC_PREFIX + role;
|
|
43414
|
-
const previousTopic = claimedRoleTopic;
|
|
43415
|
-
const previousHoldID = claimedRoleSessionID;
|
|
43416
|
-
const id = forSession;
|
|
43417
|
-
const { client } = requireEnvoy();
|
|
43418
|
-
const result = await client.setRole({
|
|
43419
|
-
sessionID: id,
|
|
43420
|
-
role,
|
|
43421
|
-
soft: options.soft,
|
|
43422
|
-
previousSessionID: options.previousSessionID
|
|
43423
|
-
});
|
|
43424
|
-
if (!live()) {
|
|
43425
|
-
if (result.claimed)
|
|
43426
|
-
pendingHandBacks.set(`${topic}@${id}`, { topic, id });
|
|
43427
|
-
throw new AbandonedTaskError(`the role move that claimed ${role} for session ${id} was abandoned at its deadline while the claim was in flight; ${result.claimed ? "the hold the listener granted is left for the next pass to hand back" : "nothing was claimed"}`);
|
|
43428
|
-
}
|
|
43429
|
-
if (result.claimed) {
|
|
43430
|
-
refusedRoles.delete(id);
|
|
43431
|
-
holdUnconfirmed = false;
|
|
43432
|
-
for (const [key, hold] of abandonedHolds) {
|
|
43433
|
-
if (hold.topic === topic)
|
|
43434
|
-
abandonedHolds.delete(key);
|
|
43435
|
-
}
|
|
43436
|
-
for (const [key, hold] of pendingHandBacks) {
|
|
43437
|
-
if (hold.topic === topic)
|
|
43438
|
-
pendingHandBacks.delete(key);
|
|
43439
|
-
}
|
|
43440
|
-
}
|
|
43441
|
-
if (!sessionOpen(id)) {
|
|
43442
|
-
if (result.claimed) {
|
|
43443
|
-
if (previousTopic !== undefined && previousTopic !== topic)
|
|
43444
|
-
endRole();
|
|
43445
|
-
claimedRoleTopic = topic;
|
|
43446
|
-
claimedRoleSessionID = id;
|
|
43447
|
-
if (previousTopic !== undefined && previousTopic !== topic && previousHoldID) {
|
|
43448
|
-
await client.unsubscribe({ sessionID: previousHoldID, topics: [previousTopic] });
|
|
43449
|
-
}
|
|
43450
|
-
}
|
|
43451
|
-
throw new SessionLeftError(`the session moved from ${id} to ${hostSessionID()} while its claim of role ${role} was in flight; ${result.claimed ? "the next pass keeps the hold for the session the host is on, or hands it back" : "nothing was claimed"}`);
|
|
43452
|
-
}
|
|
43453
|
-
if (!result.claimed) {
|
|
43454
|
-
endRole();
|
|
43455
|
-
const waitingOnReap = await observeAbandonedHold(topic, role, result.holder, live);
|
|
43456
|
-
const notRecorded = !live() ? "the move was abandoned at its deadline before the refusal was recorded" : sessionOpen(id) ? undefined : `the session moved to ${hostSessionID()} before the refusal was recorded`;
|
|
43457
|
-
if (notRecorded === undefined && !waitingOnReap)
|
|
43458
|
-
refusedRoles.set(id, role);
|
|
43459
|
-
logger3.warn(notRecorded !== undefined ? `envoy: role held by another live session; ${notRecorded}` : waitingOnReap ? "envoy: role still held by a session this process left; claiming again once the listener reaps it" : "envoy: role held by another live session; not reclaimed", { role, sessionID: id, holder: result.holder });
|
|
43460
|
-
return false;
|
|
43461
|
-
}
|
|
43462
|
-
if (previousTopic !== undefined && previousTopic !== topic)
|
|
43463
|
-
endRole();
|
|
43464
|
-
claimedRoleTopic = topic;
|
|
43465
|
-
claimedRoleSessionID = id;
|
|
43466
|
-
pi.appendEntry(ROLE_CLAIM_ENTRY, { role });
|
|
43467
|
-
if (activeSessionContext !== undefined)
|
|
43468
|
-
ensureHeartbeat(activeSessionContext);
|
|
43469
|
-
if (previousTopic !== undefined && previousTopic !== topic && previousHoldID) {
|
|
43470
|
-
await client.unsubscribe({ sessionID: previousHoldID, topics: [previousTopic] });
|
|
43471
|
-
}
|
|
43472
|
-
return true;
|
|
43473
|
-
};
|
|
43474
43711
|
const transcriptClaimedRole = (branch) => {
|
|
43475
43712
|
let role;
|
|
43476
43713
|
for (const entry of branch) {
|
|
@@ -43480,46 +43717,28 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43480
43717
|
}
|
|
43481
43718
|
return role;
|
|
43482
43719
|
};
|
|
43483
|
-
const desiredRole = async (id, branch, readRows) => {
|
|
43484
|
-
const want = (role) =>
|
|
43720
|
+
const desiredRole = async (handle, id, branch, readRows) => {
|
|
43721
|
+
const want = (role) => registrationState.refusalFor(id) === role ? undefined : role;
|
|
43485
43722
|
const recorded = transcriptClaimedRole(branch);
|
|
43486
43723
|
if (recorded === null)
|
|
43487
43724
|
return;
|
|
43488
43725
|
if (recorded !== undefined)
|
|
43489
43726
|
return want(recorded);
|
|
43490
|
-
const held = heldRole();
|
|
43491
|
-
const holdID =
|
|
43727
|
+
const held = registrationState.heldRole();
|
|
43728
|
+
const holdID = registrationState.holdSessionID();
|
|
43492
43729
|
const lineage = continuedBy(id);
|
|
43493
43730
|
if (held !== undefined && holdID !== undefined && lineage.includes(holdID))
|
|
43494
43731
|
return want(held);
|
|
43495
43732
|
if (!readRows)
|
|
43496
43733
|
return;
|
|
43497
|
-
const { client } = requireEnvoy();
|
|
43498
43734
|
for (const source of lineage) {
|
|
43499
|
-
const registry = await
|
|
43500
|
-
return;
|
|
43501
|
-
});
|
|
43735
|
+
const registry = await handle.readInterest(source);
|
|
43502
43736
|
const topic = registry?.topics.find((candidate) => candidate.startsWith(ROLE_TOPIC_PREFIX));
|
|
43503
43737
|
if (topic !== undefined)
|
|
43504
43738
|
return want(topic.slice(ROLE_TOPIC_PREFIX.length));
|
|
43505
43739
|
}
|
|
43506
43740
|
return;
|
|
43507
43741
|
};
|
|
43508
|
-
const endUnwantedRoleNow = (context) => {
|
|
43509
|
-
const host = context.sessionManager.getSessionId();
|
|
43510
|
-
const held = heldRole();
|
|
43511
|
-
const topic = claimedRoleTopic;
|
|
43512
|
-
const holdID = claimedRoleSessionID;
|
|
43513
|
-
if (held === undefined || topic === undefined || holdID === undefined)
|
|
43514
|
-
return;
|
|
43515
|
-
const recorded = transcriptClaimedRole(context.sessionManager.getBranch?.() ?? []);
|
|
43516
|
-
const wanted = recorded === undefined ? continuedBy(host).includes(holdID) : recorded === held;
|
|
43517
|
-
if (wanted)
|
|
43518
|
-
return;
|
|
43519
|
-
endRole();
|
|
43520
|
-
if (holdID !== "")
|
|
43521
|
-
pendingHandBacks.set(`${topic}@${holdID}`, { topic, id: holdID });
|
|
43522
|
-
};
|
|
43523
43742
|
const tellIdChanged = (current) => {
|
|
43524
43743
|
const previous = continues.get(current);
|
|
43525
43744
|
if (previous === undefined || previous === current || notifiedSessions.has(current))
|
|
@@ -43565,17 +43784,14 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43565
43784
|
knownSessions.add(host);
|
|
43566
43785
|
};
|
|
43567
43786
|
const knownSessions = new Set;
|
|
43568
|
-
const
|
|
43569
|
-
const
|
|
43570
|
-
const agentSessions = new Set;
|
|
43571
|
-
let inboundOpenFor = "";
|
|
43572
|
-
const reconcileNow = async (context, cause, live) => {
|
|
43787
|
+
const reconcile = (context, cause = {}) => registrationState.passes.run((handle) => reconcileNow(context, cause, handle)).then((decided) => decided ?? false);
|
|
43788
|
+
const reconcileNow = async (context, cause, handle) => {
|
|
43573
43789
|
const host = context.sessionManager.getSessionId();
|
|
43574
|
-
|
|
43790
|
+
handle.actFor(host);
|
|
43575
43791
|
noteMove(host);
|
|
43576
43792
|
if (sessionID !== host)
|
|
43577
43793
|
restoreLocalSessionState(context);
|
|
43578
|
-
if (
|
|
43794
|
+
if (registrationState.establishedFor() !== host) {
|
|
43579
43795
|
if ((process.env.DISPATCH_SESSION_ID ?? "") !== host) {
|
|
43580
43796
|
bindDispatchSession(host, context.ui);
|
|
43581
43797
|
procmgr.refreshShellConfigCache();
|
|
@@ -43584,60 +43800,44 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43584
43800
|
if (host !== lastHeard)
|
|
43585
43801
|
lastHeard = "";
|
|
43586
43802
|
}
|
|
43587
|
-
for (const other of
|
|
43588
|
-
if (other === host)
|
|
43589
|
-
continue;
|
|
43803
|
+
for (const other of handle.forgetOtherAgentSessions(host)) {
|
|
43590
43804
|
closeIntentionally(agentSubject(other));
|
|
43591
|
-
agentSessions.delete(other);
|
|
43592
|
-
if (inboundOpenFor === other)
|
|
43593
|
-
inboundOpenFor = "";
|
|
43594
43805
|
}
|
|
43595
43806
|
if (host === "") {
|
|
43596
|
-
|
|
43807
|
+
handle.markEstablished(host);
|
|
43597
43808
|
ensureHeartbeat(context);
|
|
43598
43809
|
return true;
|
|
43599
43810
|
}
|
|
43600
43811
|
let inboundFailure;
|
|
43601
43812
|
let openedInbound = false;
|
|
43602
|
-
if (inbound &&
|
|
43813
|
+
if (inbound && registrationState.inboundOpenSession() !== host) {
|
|
43603
43814
|
try {
|
|
43604
|
-
|
|
43605
|
-
await
|
|
43606
|
-
|
|
43607
|
-
|
|
43608
|
-
await subscribe(agentSubject(host), live);
|
|
43609
|
-
if (!owns())
|
|
43610
|
-
return false;
|
|
43611
|
-
await watchAgentStream(host, live);
|
|
43612
|
-
if (!owns())
|
|
43613
|
-
return false;
|
|
43815
|
+
handle.markAgentSessionOpen(host);
|
|
43816
|
+
await handle.connect();
|
|
43817
|
+
await handle.subscribe(agentSubject(host));
|
|
43818
|
+
await watchAgentStream(host, handle.connect);
|
|
43614
43819
|
if ((context.sessionManager.getBranch?.() ?? []).length > 0) {
|
|
43615
|
-
await recoverRegisteredInterests(
|
|
43616
|
-
if (!owns())
|
|
43617
|
-
return false;
|
|
43820
|
+
await recoverRegisteredInterests(handle);
|
|
43618
43821
|
}
|
|
43619
|
-
|
|
43822
|
+
handle.markInboundOpen(host);
|
|
43620
43823
|
openedInbound = true;
|
|
43621
43824
|
} catch (error) {
|
|
43622
43825
|
inboundFailure = error;
|
|
43623
43826
|
}
|
|
43624
43827
|
}
|
|
43625
|
-
|
|
43626
|
-
|
|
43627
|
-
const registration = await registerSession().then(() => {
|
|
43828
|
+
handle.check();
|
|
43829
|
+
const registration = await handle.register(registrationInput()).then(() => {
|
|
43628
43830
|
return;
|
|
43629
43831
|
}, (error) => ({ error }));
|
|
43630
|
-
|
|
43631
|
-
return false;
|
|
43832
|
+
handle.check();
|
|
43632
43833
|
if (registration === undefined) {
|
|
43633
|
-
|
|
43834
|
+
handle.markEstablished(host);
|
|
43634
43835
|
ensureHeartbeat(context);
|
|
43635
43836
|
}
|
|
43636
43837
|
if (openedInbound)
|
|
43637
43838
|
triggerReplay();
|
|
43638
|
-
const decided = await roleMoves.run((
|
|
43639
|
-
|
|
43640
|
-
return false;
|
|
43839
|
+
const decided = await registrationState.roleMoves.run((move) => settleRole(context, host, cause, registration === undefined, move)) ?? false;
|
|
43840
|
+
handle.check();
|
|
43641
43841
|
if (registration !== undefined) {
|
|
43642
43842
|
throw new RegistrationError(messageFor(registration.error), { cause: registration.error });
|
|
43643
43843
|
}
|
|
@@ -43646,119 +43846,75 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43646
43846
|
}
|
|
43647
43847
|
return decided;
|
|
43648
43848
|
};
|
|
43649
|
-
let roleSettledFor = "";
|
|
43650
43849
|
let lastHeard = "";
|
|
43651
|
-
const
|
|
43652
|
-
|
|
43653
|
-
const failed = [];
|
|
43654
|
-
for (const [key, hold] of pendingHandBacks) {
|
|
43655
|
-
if (!live())
|
|
43656
|
-
break;
|
|
43657
|
-
pendingHandBacks.delete(key);
|
|
43658
|
-
if (wanted !== undefined && hold.topic === ROLE_TOPIC_PREFIX + wanted && sessionOpen(hold.id)) {
|
|
43659
|
-
claimedRoleTopic = hold.topic;
|
|
43660
|
-
claimedRoleSessionID = hold.id;
|
|
43661
|
-
holdUnconfirmed = true;
|
|
43662
|
-
restored = true;
|
|
43663
|
-
continue;
|
|
43664
|
-
}
|
|
43665
|
-
await requireEnvoy().client.unsubscribe({ sessionID: hold.id, topics: [hold.topic] }).catch((error) => {
|
|
43666
|
-
failed.push({ key, ...hold });
|
|
43667
|
-
logger3.warn("envoy: a role this process gave up was not handed back", {
|
|
43668
|
-
topic: hold.topic,
|
|
43669
|
-
sessionID: hold.id,
|
|
43670
|
-
error: messageFor(error)
|
|
43671
|
-
});
|
|
43672
|
-
});
|
|
43673
|
-
}
|
|
43674
|
-
for (const { key, topic, id } of failed)
|
|
43675
|
-
abandonedHolds.set(key, { topic, id });
|
|
43676
|
-
if (!live())
|
|
43677
|
-
return false;
|
|
43678
|
-
return restored;
|
|
43679
|
-
};
|
|
43680
|
-
const settleRole = async (context, host, cause, registered, live) => {
|
|
43681
|
-
const owns = () => live() && sessionOpen(host);
|
|
43682
|
-
if (!owns())
|
|
43683
|
-
return false;
|
|
43850
|
+
const settleRole = async (context, host, cause, registered, handle) => {
|
|
43851
|
+
handle.actFor(host);
|
|
43684
43852
|
const branch = context.sessionManager.getBranch?.() ?? [];
|
|
43685
|
-
const
|
|
43686
|
-
const
|
|
43687
|
-
|
|
43688
|
-
return false;
|
|
43853
|
+
const settled = registrationState.roleSettledSession();
|
|
43854
|
+
const readRows = settled !== host && (branch.length > 0 || settled !== "");
|
|
43855
|
+
const desired = await desiredRole(handle, host, branch, readRows);
|
|
43689
43856
|
const unheard = lastHeard !== host;
|
|
43690
|
-
const held = heldRole();
|
|
43857
|
+
const held = registrationState.heldRole();
|
|
43691
43858
|
if (held !== undefined && held !== desired) {
|
|
43692
|
-
|
|
43693
|
-
if (ended !== undefined && unheard)
|
|
43694
|
-
warnRoleEnded(context, host, ended);
|
|
43695
|
-
if (!owns())
|
|
43696
|
-
return false;
|
|
43859
|
+
await handle.handBackHeldRole(unheard);
|
|
43697
43860
|
}
|
|
43698
|
-
if (await settleEndedHolds(desired
|
|
43699
|
-
|
|
43861
|
+
if (await handle.settleEndedHolds(desired) && unheard && desired !== undefined) {
|
|
43862
|
+
handle.noteNoticesClosed(desired);
|
|
43700
43863
|
}
|
|
43701
|
-
if (!owns())
|
|
43702
|
-
return false;
|
|
43703
43864
|
if (!registered)
|
|
43704
43865
|
return false;
|
|
43705
43866
|
if (desired === undefined) {
|
|
43706
|
-
|
|
43867
|
+
handle.markRoleSettled(host);
|
|
43707
43868
|
return true;
|
|
43708
43869
|
}
|
|
43709
|
-
if (!
|
|
43710
|
-
|
|
43711
|
-
|
|
43870
|
+
if (!registrationState.holdIsUnconfirmed() && registrationState.heldRole() === desired && registrationState.holdSessionID() === host) {
|
|
43871
|
+
handle.markRoleSettled(host);
|
|
43872
|
+
if (cause.heartbeat !== true)
|
|
43873
|
+
return true;
|
|
43874
|
+
const checked = await handle.reassertRole(desired, cause.afterOutage === true);
|
|
43875
|
+
const { regain } = checked;
|
|
43876
|
+
const regained = regain === undefined ? undefined : legionRoleClaimBridge().regained;
|
|
43877
|
+
if (regain !== undefined && regained !== undefined) {
|
|
43878
|
+
Promise.resolve().then(() => regained(desired, regain)).catch((error) => {
|
|
43879
|
+
logger3.warn("envoy: role regain hook failed", {
|
|
43880
|
+
role: desired,
|
|
43881
|
+
reason: regain,
|
|
43882
|
+
error: messageFor(error)
|
|
43883
|
+
});
|
|
43884
|
+
});
|
|
43885
|
+
}
|
|
43886
|
+
return checked.decided;
|
|
43712
43887
|
}
|
|
43713
43888
|
if (!inbound) {
|
|
43714
|
-
|
|
43889
|
+
handle.markRoleSettled(host);
|
|
43715
43890
|
return true;
|
|
43716
43891
|
}
|
|
43717
|
-
|
|
43892
|
+
const holdID = registrationState.holdSessionID();
|
|
43893
|
+
let supersedes = registrationState.heldRole() === desired && holdID !== undefined && holdID !== "" ? holdID : undefined;
|
|
43718
43894
|
const lineage = continuedBy(host);
|
|
43719
43895
|
if (supersedes === undefined && lineage.length > 1) {
|
|
43720
|
-
const holder = await
|
|
43721
|
-
return;
|
|
43722
|
-
});
|
|
43896
|
+
const holder = await handle.readRoleHolder(desired);
|
|
43723
43897
|
if (holder !== undefined && holder !== host && lineage.includes(holder))
|
|
43724
43898
|
supersedes = holder;
|
|
43725
|
-
if (!owns())
|
|
43726
|
-
return false;
|
|
43727
43899
|
}
|
|
43728
43900
|
try {
|
|
43729
|
-
const claimed = await
|
|
43901
|
+
const claimed = await handle.claimRole(desired, {
|
|
43730
43902
|
soft: true,
|
|
43731
43903
|
previousSessionID: supersedes
|
|
43732
43904
|
});
|
|
43733
43905
|
if (claimed && unheard)
|
|
43734
|
-
|
|
43906
|
+
handle.noteNoticesClosed(desired);
|
|
43735
43907
|
} catch (error) {
|
|
43736
|
-
|
|
43737
|
-
return false;
|
|
43908
|
+
handle.check();
|
|
43738
43909
|
logger3.warn("envoy: role reclaim failed", {
|
|
43739
43910
|
role: desired,
|
|
43740
43911
|
sessionID: host,
|
|
43741
43912
|
error: messageFor(error)
|
|
43742
43913
|
});
|
|
43743
43914
|
}
|
|
43744
|
-
|
|
43745
|
-
return false;
|
|
43746
|
-
roleSettledFor = host;
|
|
43915
|
+
handle.markRoleSettled(host);
|
|
43747
43916
|
return true;
|
|
43748
43917
|
};
|
|
43749
|
-
const warnRoleEnded = (context, host, ended) => {
|
|
43750
|
-
logger3.warn("envoy: role ended after a move with no session event", {
|
|
43751
|
-
role: ended.role,
|
|
43752
|
-
sessionID: host
|
|
43753
|
-
});
|
|
43754
|
-
context.ui.notify(ended.pluginHeld ? `envoy: role ${ended.role} ended: the host moved to session ${host} with no session event, and that session's records do not name it; the plugin that opened its notices claims it again on its next session switch or restart` : `envoy: role ${ended.role} ended: the host moved to session ${host} with no session event, and that session's records do not name it; if this session should hold it, run envoy_role_set`, "warning");
|
|
43755
|
-
};
|
|
43756
|
-
const warnNoticesClosed = (context, role) => {
|
|
43757
|
-
if (roleNoticesFor !== role || roleNoticeSubjects.size > 0)
|
|
43758
|
-
return;
|
|
43759
|
-
roleNoticesFor = undefined;
|
|
43760
|
-
context.ui.notify(`envoy: role ${role} is held again, but the notices a plugin opened for it stay closed until that plugin claims it again on its next session switch or restart`, "warning");
|
|
43761
|
-
};
|
|
43762
43918
|
const establishForBridge = (context) => reconcile(context).then(() => {
|
|
43763
43919
|
return;
|
|
43764
43920
|
}, (error) => {
|
|
@@ -43772,40 +43928,44 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43772
43928
|
}
|
|
43773
43929
|
markLegionManagedSession(targetSessionID);
|
|
43774
43930
|
requireInbound();
|
|
43775
|
-
if (
|
|
43931
|
+
if (registrationState.establishedFor() !== targetSessionID) {
|
|
43776
43932
|
await establishForBridge(context);
|
|
43933
|
+
}
|
|
43777
43934
|
await registerSession();
|
|
43778
|
-
|
|
43935
|
+
let claimAdopted = false;
|
|
43936
|
+
const claimed = await registrationState.roleMoves.run(async (handle) => {
|
|
43937
|
+
handle.actFor(targetSessionID);
|
|
43938
|
+
await handle.claimRole(role);
|
|
43939
|
+
return true;
|
|
43940
|
+
}, (refusal) => {
|
|
43941
|
+
claimAdopted = refusal.claimAdopted;
|
|
43942
|
+
});
|
|
43943
|
+
if (claimed === undefined)
|
|
43944
|
+
throw sessionLeftDuringClaim(targetSessionID, role, claimAdopted);
|
|
43779
43945
|
};
|
|
43780
43946
|
const subscribeNotice = async (targetSessionID, topic, callerContext, whileHolding) => {
|
|
43781
43947
|
const context = callerContext ?? activeSessionContext;
|
|
43782
43948
|
if (context === undefined || context.sessionManager.getSessionId() !== targetSessionID) {
|
|
43783
43949
|
throw new Error(`Envoy has no active session for Legion notice subscription: ${targetSessionID}`);
|
|
43784
43950
|
}
|
|
43785
|
-
if (
|
|
43951
|
+
if (registrationState.establishedFor() !== targetSessionID) {
|
|
43786
43952
|
await establishForBridge(context);
|
|
43787
|
-
|
|
43953
|
+
}
|
|
43954
|
+
let marked = [];
|
|
43788
43955
|
if (whileHolding !== undefined) {
|
|
43789
|
-
await roleMoves.settled();
|
|
43790
|
-
if (
|
|
43956
|
+
await registrationState.roleMoves.settled();
|
|
43957
|
+
if (!registrationState.holdsRoleTopic(ROLE_TOPIC_PREFIX + whileHolding)) {
|
|
43791
43958
|
throw new Error(`Envoy session ${targetSessionID} does not hold role ${whileHolding} for a notice subscription`);
|
|
43792
43959
|
}
|
|
43793
|
-
|
|
43794
|
-
if (isRoleBound(subject))
|
|
43795
|
-
continue;
|
|
43796
|
-
roleNoticeSubjects.add(subject);
|
|
43797
|
-
marked.push(subject);
|
|
43798
|
-
}
|
|
43799
|
-
roleNoticesFor = whileHolding;
|
|
43960
|
+
marked = registrationState.markRoleBoundSubjects(expandSubscriptionTopics([topic]), whileHolding);
|
|
43800
43961
|
}
|
|
43801
|
-
const snapshot = roleSnapshot(topic);
|
|
43962
|
+
const snapshot = registrationState.roleSnapshot(topic);
|
|
43802
43963
|
try {
|
|
43803
43964
|
if (!await subscribeUnlessRoleEnds(topic, snapshot))
|
|
43804
43965
|
return;
|
|
43805
43966
|
} catch (error) {
|
|
43806
|
-
if (
|
|
43807
|
-
|
|
43808
|
-
roleNoticeSubjects.delete(subject);
|
|
43967
|
+
if (registrationState.roleEndCount() === snapshot.endsBefore) {
|
|
43968
|
+
registrationState.unmarkRoleBoundSubjects(marked);
|
|
43809
43969
|
}
|
|
43810
43970
|
throw error;
|
|
43811
43971
|
}
|
|
@@ -43869,10 +44029,10 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43869
44029
|
const host = context.sessionManager.getSessionId();
|
|
43870
44030
|
noteMove(host, { kind: reason });
|
|
43871
44031
|
lastHeard = host;
|
|
43872
|
-
|
|
44032
|
+
registrationState.clearRefusal(host);
|
|
43873
44033
|
if (sessionID !== host)
|
|
43874
44034
|
restoreLocalSessionState(context);
|
|
43875
|
-
|
|
44035
|
+
registrationState.endUnwantedRole(host, transcriptClaimedRole(context.sessionManager.getBranch?.() ?? []), continuedBy(host));
|
|
43876
44036
|
if (!inbound)
|
|
43877
44037
|
return;
|
|
43878
44038
|
try {
|
|
@@ -43928,7 +44088,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
|
|
|
43928
44088
|
const deadline = Promise.withResolvers();
|
|
43929
44089
|
const timer = setTimeout(deadline.resolve, 1000);
|
|
43930
44090
|
try {
|
|
43931
|
-
const deregistration = sessionID === "" || active === undefined ? Promise.resolve() :
|
|
44091
|
+
const deregistration = sessionID === "" || active === undefined ? Promise.resolve() : registrationState.deregister(sessionID).catch(() => {
|
|
43932
44092
|
return;
|
|
43933
44093
|
});
|
|
43934
44094
|
const draining = connection?.drain().catch(() => {
|
|
@@ -44278,8 +44438,8 @@ Warnings: ${warnings.join("; ")}`, {
|
|
|
44278
44438
|
case EnvoyToolOperation.unsubscribe: {
|
|
44279
44439
|
const inbox = agentSubject(sessionID);
|
|
44280
44440
|
const candidates = topicsFor(parameters, [...subscriptions.keys()]).filter((subject) => subject !== inbox);
|
|
44281
|
-
const kept = candidates.filter(isRoleBound);
|
|
44282
|
-
const removed = candidates.filter((subject) => !isRoleBound(subject)).filter(closeIntentionally);
|
|
44441
|
+
const kept = candidates.filter(registrationState.isRoleBound);
|
|
44442
|
+
const removed = candidates.filter((subject) => !registrationState.isRoleBound(subject)).filter(closeIntentionally);
|
|
44283
44443
|
if (removed.length > 0)
|
|
44284
44444
|
await replayFollowedChanged();
|
|
44285
44445
|
const registryRemoved = removed.filter((topic) => !topic.startsWith(ROLE_TOPIC_PREFIX));
|
|
@@ -44288,7 +44448,7 @@ Warnings: ${warnings.join("; ")}`, {
|
|
|
44288
44448
|
}, messageFor);
|
|
44289
44449
|
const unsubscribed = `Unsubscribed: ${removed.join(", ") || "(none)"}`;
|
|
44290
44450
|
return toolSuccess(kept.length === 0 ? unsubscribed : `${unsubscribed}
|
|
44291
|
-
Kept (they last while this session holds role ${heldRole()}): ${kept.join(", ")}`, {
|
|
44451
|
+
Kept (they last while this session holds role ${registrationState.heldRole()}): ${kept.join(", ")}`, {
|
|
44292
44452
|
removed,
|
|
44293
44453
|
...kept.length === 0 ? {} : { kept },
|
|
44294
44454
|
...registrationError === undefined ? {} : { registrationError }
|
|
@@ -44339,15 +44499,25 @@ Not delivered: that holder is the session this subagent sends as, and the listen
|
|
|
44339
44499
|
}
|
|
44340
44500
|
case EnvoyToolOperation.setRole: {
|
|
44341
44501
|
const role = stringFor(parameters, "role");
|
|
44342
|
-
|
|
44343
|
-
|
|
44502
|
+
let target = sessionID;
|
|
44503
|
+
let claimAdopted = false;
|
|
44504
|
+
const moved = await registrationState.roleMoves.run(async (handle) => {
|
|
44505
|
+
target = sessionID;
|
|
44506
|
+
handle.actFor(target);
|
|
44507
|
+
const held = registrationState.heldRole();
|
|
44344
44508
|
const moving = held !== undefined && held !== role;
|
|
44345
|
-
|
|
44346
|
-
|
|
44509
|
+
const bound = registrationState.roleBoundSubjects();
|
|
44510
|
+
if (moving && bound.length > 0) {
|
|
44511
|
+
throw new Error(`This session holds role ${held}, and ${bound.join(", ")} lasts only while it does; envoy_role_set does not move it to ${role}`);
|
|
44347
44512
|
}
|
|
44348
|
-
await
|
|
44349
|
-
return moving ? held : undefined;
|
|
44513
|
+
await handle.claimRole(role);
|
|
44514
|
+
return { released: moving ? held : undefined };
|
|
44515
|
+
}, (refusal) => {
|
|
44516
|
+
claimAdopted = refusal.claimAdopted;
|
|
44350
44517
|
});
|
|
44518
|
+
if (moved === undefined)
|
|
44519
|
+
throw sessionLeftDuringClaim(target, role, claimAdopted);
|
|
44520
|
+
const { released } = moved;
|
|
44351
44521
|
return toolSuccess(released === undefined ? `Now holding role: ${role}` : `Now holding role: ${role} (released role ${released})`, { role, ...released === undefined ? {} : { released } });
|
|
44352
44522
|
}
|
|
44353
44523
|
case EnvoyToolOperation.getRole: {
|