@adhdev/daemon-core 0.9.82-rc.190 → 0.9.82-rc.192
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/index.js +104 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +104 -60
- package/dist/index.mjs.map +1 -1
- package/dist/mesh/beads-db.d.ts +10 -0
- package/package.json +1 -1
- package/src/mesh/beads-db.ts +37 -0
- package/src/mesh/mesh-events.ts +79 -75
package/dist/index.js
CHANGED
|
@@ -3397,6 +3397,15 @@ var init_beads_db = __esm({
|
|
|
3397
3397
|
|
|
3398
3398
|
CREATE INDEX IF NOT EXISTS idx_direct_dispatches_mesh_session
|
|
3399
3399
|
ON mesh_direct_dispatches(mesh_id, session_id, status);
|
|
3400
|
+
|
|
3401
|
+
CREATE TABLE IF NOT EXISTS remote_idle_sessions (
|
|
3402
|
+
node_id TEXT NOT NULL,
|
|
3403
|
+
session_id TEXT NOT NULL,
|
|
3404
|
+
provider_type TEXT NOT NULL,
|
|
3405
|
+
expires_at INTEGER NOT NULL,
|
|
3406
|
+
metadata TEXT,
|
|
3407
|
+
PRIMARY KEY (node_id, session_id)
|
|
3408
|
+
);
|
|
3400
3409
|
`);
|
|
3401
3410
|
}
|
|
3402
3411
|
hasCompletionFingerprint(fingerprint) {
|
|
@@ -3698,6 +3707,29 @@ var init_beads_db = __esm({
|
|
|
3698
3707
|
WHERE mesh_id = ? AND status = 'dispatched' AND dispatched_at < ?
|
|
3699
3708
|
`).run(now, meshId, cutoff);
|
|
3700
3709
|
}
|
|
3710
|
+
// ── Remote Idle Sessions ─────────────────────────────────────────────────
|
|
3711
|
+
setRemoteIdleSession(nodeId, sessionId, providerType, expiresAt, metadata) {
|
|
3712
|
+
this.db.prepare(`
|
|
3713
|
+
INSERT OR REPLACE INTO remote_idle_sessions (node_id, session_id, provider_type, expires_at, metadata)
|
|
3714
|
+
VALUES (?, ?, ?, ?, ?)
|
|
3715
|
+
`).run(nodeId, sessionId, providerType, expiresAt, metadata ? JSON.stringify(metadata) : null);
|
|
3716
|
+
}
|
|
3717
|
+
getRemoteIdleSessions() {
|
|
3718
|
+
const rows = this.db.prepare("SELECT node_id, session_id, provider_type, expires_at, metadata FROM remote_idle_sessions").all();
|
|
3719
|
+
return rows.map((r) => ({
|
|
3720
|
+
nodeId: r.node_id,
|
|
3721
|
+
sessionId: r.session_id,
|
|
3722
|
+
providerType: r.provider_type,
|
|
3723
|
+
expiresAt: r.expires_at,
|
|
3724
|
+
metadata: r.metadata ? JSON.parse(r.metadata) : void 0
|
|
3725
|
+
}));
|
|
3726
|
+
}
|
|
3727
|
+
deleteRemoteIdleSession(nodeId, sessionId) {
|
|
3728
|
+
this.db.prepare("DELETE FROM remote_idle_sessions WHERE node_id = ? AND session_id = ?").run(nodeId, sessionId);
|
|
3729
|
+
}
|
|
3730
|
+
pruneExpiredRemoteIdleSessions() {
|
|
3731
|
+
this.db.prepare("DELETE FROM remote_idle_sessions WHERE expires_at <= ?").run(Date.now());
|
|
3732
|
+
}
|
|
3701
3733
|
};
|
|
3702
3734
|
}
|
|
3703
3735
|
});
|
|
@@ -4141,9 +4173,9 @@ function __resetIdleAutoFastForwardForTests() {
|
|
|
4141
4173
|
idleAutoFastForwardLastAttempt.clear();
|
|
4142
4174
|
}
|
|
4143
4175
|
function sweepExpiredRemoteIdleSessions() {
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4176
|
+
try {
|
|
4177
|
+
BeadsDB.getInstance().pruneExpiredRemoteIdleSessions();
|
|
4178
|
+
} catch {
|
|
4147
4179
|
}
|
|
4148
4180
|
}
|
|
4149
4181
|
function readRefineJobId(event) {
|
|
@@ -5043,13 +5075,21 @@ async function triggerMeshQueue(components, meshId) {
|
|
|
5043
5075
|
});
|
|
5044
5076
|
}
|
|
5045
5077
|
}
|
|
5046
|
-
|
|
5078
|
+
let remoteSessions = [];
|
|
5079
|
+
try {
|
|
5080
|
+
remoteSessions = BeadsDB.getInstance().getRemoteIdleSessions();
|
|
5081
|
+
} catch {
|
|
5082
|
+
}
|
|
5083
|
+
for (const idle of remoteSessions) {
|
|
5047
5084
|
const node = mesh.nodes.find((n) => n.id === idle.nodeId);
|
|
5048
5085
|
if (node) {
|
|
5049
5086
|
remoteIdleSessionsChecked += 1;
|
|
5050
5087
|
const assigned = tryAssignQueueTask(components, meshId, idle.nodeId, idle.sessionId, idle.providerType);
|
|
5051
5088
|
if (assigned) {
|
|
5052
|
-
|
|
5089
|
+
try {
|
|
5090
|
+
BeadsDB.getInstance().deleteRemoteIdleSession(idle.nodeId, idle.sessionId);
|
|
5091
|
+
} catch {
|
|
5092
|
+
}
|
|
5053
5093
|
}
|
|
5054
5094
|
}
|
|
5055
5095
|
}
|
|
@@ -5248,7 +5288,10 @@ function injectMeshSystemMessage(components, args) {
|
|
|
5248
5288
|
});
|
|
5249
5289
|
if (intentionalCleanupStop) {
|
|
5250
5290
|
if (eventSessionId && eventNodeId) {
|
|
5251
|
-
|
|
5291
|
+
try {
|
|
5292
|
+
BeadsDB.getInstance().deleteRemoteIdleSession(eventNodeId, eventSessionId);
|
|
5293
|
+
} catch {
|
|
5294
|
+
}
|
|
5252
5295
|
}
|
|
5253
5296
|
LOG.info("MeshEvents", `Suppressed ${args.event} for intentionally cleanup-stopped session ${eventSessionId || "(unknown session)"}`);
|
|
5254
5297
|
return { success: true, forwarded: 0, suppressed: true, intentionalCleanupStop: true };
|
|
@@ -5335,18 +5378,21 @@ function injectMeshSystemMessage(components, args) {
|
|
|
5335
5378
|
return { success: true, forwarded: 0, suppressed: true, duplicateCompletion: true };
|
|
5336
5379
|
}
|
|
5337
5380
|
}
|
|
5381
|
+
function markSessionTerminal(sessionId, outcome, occurredAtMs) {
|
|
5382
|
+
const task = updateSessionTaskStatus(args.meshId, sessionId, outcome, {
|
|
5383
|
+
occurredAt: occurredAtMs != null ? new Date(occurredAtMs).toISOString() : void 0
|
|
5384
|
+
});
|
|
5385
|
+
updateDirectDispatchStatus(args.meshId, sessionId, outcome);
|
|
5386
|
+
setImmediate(() => cleanupTerminalDirectDispatches());
|
|
5387
|
+
return task ? { id: task.id } : null;
|
|
5388
|
+
}
|
|
5338
5389
|
let completedTaskForLedger = null;
|
|
5339
5390
|
if (args.event === "agent:generating_completed") {
|
|
5340
5391
|
const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
|
|
5341
5392
|
const nodeId = readNonEmptyString2(args.nodeId) || readNonEmptyString2(args.metadataEvent.meshNodeId);
|
|
5342
5393
|
const providerType = readNonEmptyString2(args.metadataEvent.providerType);
|
|
5343
5394
|
if (sessionId) {
|
|
5344
|
-
|
|
5345
|
-
occurredAt: eventTimestamp !== null ? new Date(eventTimestamp).toISOString() : void 0
|
|
5346
|
-
});
|
|
5347
|
-
completedTaskForLedger = completedTask ? { id: completedTask.id } : null;
|
|
5348
|
-
updateDirectDispatchStatus(args.meshId, sessionId, "completed");
|
|
5349
|
-
setImmediate(() => cleanupTerminalDirectDispatches());
|
|
5395
|
+
completedTaskForLedger = markSessionTerminal(sessionId, "completed", eventTimestamp);
|
|
5350
5396
|
if (nodeId && providerType) {
|
|
5351
5397
|
runIdleMaintenanceThenAssignQueue(components, { meshId: args.meshId, nodeId, sessionId, providerType });
|
|
5352
5398
|
}
|
|
@@ -5359,53 +5405,50 @@ function injectMeshSystemMessage(components, args) {
|
|
|
5359
5405
|
const finalSummary = readNonEmptyString2(args.metadataEvent.finalSummary) || void 0;
|
|
5360
5406
|
const workerResult = readWorkerResultMetadata(args.metadataEvent);
|
|
5361
5407
|
const hasCompletionEvidence = !!finalSummary || !!workerResult;
|
|
5362
|
-
|
|
5363
|
-
|
|
5364
|
-
completedTaskForLedger
|
|
5365
|
-
|
|
5366
|
-
|
|
5367
|
-
|
|
5368
|
-
|
|
5369
|
-
|
|
5370
|
-
|
|
5371
|
-
|
|
5372
|
-
|
|
5373
|
-
|
|
5374
|
-
|
|
5375
|
-
|
|
5376
|
-
taskId: completedTask.id,
|
|
5377
|
-
completedViaReady: true,
|
|
5378
|
-
providerSessionId,
|
|
5379
|
-
finalSummary,
|
|
5380
|
-
workerResult,
|
|
5381
|
-
evidence: buildTaskCompletionEvidence({
|
|
5382
|
-
event: "agent:ready",
|
|
5383
|
-
nodeId,
|
|
5384
|
-
sessionId,
|
|
5385
|
-
providerType: providerType || void 0,
|
|
5408
|
+
if (sessionId && hasCompletionEvidence) {
|
|
5409
|
+
completedTaskForLedger = markSessionTerminal(sessionId, "completed");
|
|
5410
|
+
if (completedTaskForLedger) {
|
|
5411
|
+
try {
|
|
5412
|
+
appendLedgerEntry(args.meshId, {
|
|
5413
|
+
kind: "task_completed",
|
|
5414
|
+
nodeId: nodeId || void 0,
|
|
5415
|
+
sessionId,
|
|
5416
|
+
providerType: providerType || void 0,
|
|
5417
|
+
payload: {
|
|
5418
|
+
event: args.event,
|
|
5419
|
+
nodeLabel: args.nodeLabel,
|
|
5420
|
+
taskId: completedTaskForLedger.id,
|
|
5421
|
+
completedViaReady: true,
|
|
5386
5422
|
providerSessionId,
|
|
5387
5423
|
finalSummary,
|
|
5388
|
-
workerResult
|
|
5389
|
-
|
|
5390
|
-
|
|
5391
|
-
|
|
5392
|
-
|
|
5393
|
-
|
|
5424
|
+
workerResult,
|
|
5425
|
+
evidence: buildTaskCompletionEvidence({
|
|
5426
|
+
event: "agent:ready",
|
|
5427
|
+
nodeId,
|
|
5428
|
+
sessionId,
|
|
5429
|
+
providerType: providerType || void 0,
|
|
5430
|
+
providerSessionId,
|
|
5431
|
+
finalSummary,
|
|
5432
|
+
workerResult
|
|
5433
|
+
})
|
|
5434
|
+
}
|
|
5435
|
+
});
|
|
5436
|
+
} catch (e) {
|
|
5437
|
+
LOG.warn("MeshLedger", `Failed to record task_completed from ready: ${e?.message || e}`);
|
|
5438
|
+
}
|
|
5394
5439
|
}
|
|
5395
5440
|
}
|
|
5396
5441
|
if (sessionId && nodeId && providerType) {
|
|
5397
5442
|
sweepExpiredRemoteIdleSessions();
|
|
5398
|
-
|
|
5399
|
-
nodeId,
|
|
5400
|
-
|
|
5401
|
-
|
|
5402
|
-
expiresAt: Date.now() + REMOTE_IDLE_SESSION_TTL_MS
|
|
5403
|
-
});
|
|
5443
|
+
try {
|
|
5444
|
+
BeadsDB.getInstance().setRemoteIdleSession(nodeId, sessionId, providerType, Date.now() + REMOTE_IDLE_SESSION_TTL_MS);
|
|
5445
|
+
} catch {
|
|
5446
|
+
}
|
|
5404
5447
|
setImmediate(() => {
|
|
5405
5448
|
maybeAutoFastForwardIdleNode(components, { meshId: args.meshId, nodeId, sessionId, providerType }).finally(() => {
|
|
5406
5449
|
try {
|
|
5407
5450
|
const assigned = tryAssignQueueTask(components, args.meshId, nodeId, sessionId, providerType);
|
|
5408
|
-
if (assigned)
|
|
5451
|
+
if (assigned) BeadsDB.getInstance().deleteRemoteIdleSession(nodeId, sessionId);
|
|
5409
5452
|
} catch (e) {
|
|
5410
5453
|
LOG.warn("MeshQueue", `Failed to assign idle queue task after maintenance for ${nodeId}: ${e?.message || e}`);
|
|
5411
5454
|
}
|
|
@@ -5416,7 +5459,10 @@ function injectMeshSystemMessage(components, args) {
|
|
|
5416
5459
|
const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
|
|
5417
5460
|
const nodeId = readNonEmptyString2(args.nodeId) || readNonEmptyString2(args.metadataEvent.meshNodeId);
|
|
5418
5461
|
if (sessionId && nodeId) {
|
|
5419
|
-
|
|
5462
|
+
try {
|
|
5463
|
+
BeadsDB.getInstance().deleteRemoteIdleSession(nodeId, sessionId);
|
|
5464
|
+
} catch {
|
|
5465
|
+
}
|
|
5420
5466
|
}
|
|
5421
5467
|
if (sessionId) {
|
|
5422
5468
|
updateDirectDispatchStatus(args.meshId, sessionId, "acked");
|
|
@@ -5425,12 +5471,13 @@ function injectMeshSystemMessage(components, args) {
|
|
|
5425
5471
|
const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
|
|
5426
5472
|
const nodeId = readNonEmptyString2(args.nodeId) || readNonEmptyString2(args.metadataEvent.meshNodeId);
|
|
5427
5473
|
if (sessionId && nodeId) {
|
|
5428
|
-
|
|
5474
|
+
try {
|
|
5475
|
+
BeadsDB.getInstance().deleteRemoteIdleSession(nodeId, sessionId);
|
|
5476
|
+
} catch {
|
|
5477
|
+
}
|
|
5429
5478
|
}
|
|
5430
5479
|
if (sessionId) {
|
|
5431
|
-
|
|
5432
|
-
completedTaskForLedger = failedTask ? { id: failedTask.id } : null;
|
|
5433
|
-
updateDirectDispatchStatus(args.meshId, sessionId, "failed");
|
|
5480
|
+
completedTaskForLedger = markSessionTerminal(sessionId, "failed");
|
|
5434
5481
|
}
|
|
5435
5482
|
}
|
|
5436
5483
|
const ledgerKind = EVENT_TO_LEDGER_KIND[args.event];
|
|
@@ -5661,10 +5708,8 @@ function setupMeshEventForwarding(components) {
|
|
|
5661
5708
|
let meshIdFromDirectDispatch = "";
|
|
5662
5709
|
if (coordinatorMeshId) {
|
|
5663
5710
|
try {
|
|
5664
|
-
const
|
|
5665
|
-
if (
|
|
5666
|
-
meshIdFromDirectDispatch = coordinatorMeshId;
|
|
5667
|
-
}
|
|
5711
|
+
const hasActiveDispatch = getActiveDirectDispatches(coordinatorMeshId).some((d) => d.sessionId === instanceId) || hasUnterminalDirectDispatchLedgerEntry(coordinatorMeshId, instanceId);
|
|
5712
|
+
if (hasActiveDispatch) meshIdFromDirectDispatch = coordinatorMeshId;
|
|
5668
5713
|
} catch {
|
|
5669
5714
|
}
|
|
5670
5715
|
if (!meshIdFromDirectDispatch) return;
|
|
@@ -5689,7 +5734,7 @@ function setupMeshEventForwarding(components) {
|
|
|
5689
5734
|
});
|
|
5690
5735
|
});
|
|
5691
5736
|
}
|
|
5692
|
-
var import_fs9, import_path8, REMOTE_IDLE_SESSION_TTL_MS,
|
|
5737
|
+
var import_fs9, import_path8, REMOTE_IDLE_SESSION_TTL_MS, meshByWorkspaceCache, MESH_WORKSPACE_CACHE_TTL_MS, IDLE_AUTO_FAST_FORWARD_THROTTLE_MS, idleAutoFastForwardLastAttempt, REFINE_TERMINAL_EVENTS, MAX_PENDING_EVENTS_BYTES, MAX_PENDING_EVENTS_KEEP, MESH_COORDINATOR_EVENTS, EVENT_TO_LEDGER_KIND, INTENTIONAL_CLEANUP_STOP_SUPPRESSION_MS, RECENT_COMPLETION_FINGERPRINT_TTL_MS, autoLaunchInProgress, autoLaunchCooldownUntil, AUTO_LAUNCH_COOLDOWN_MS;
|
|
5693
5738
|
var init_mesh_events = __esm({
|
|
5694
5739
|
"src/mesh/mesh-events.ts"() {
|
|
5695
5740
|
"use strict";
|
|
@@ -5704,7 +5749,6 @@ var init_mesh_events = __esm({
|
|
|
5704
5749
|
init_beads_db();
|
|
5705
5750
|
init_mesh_fast_forward();
|
|
5706
5751
|
REMOTE_IDLE_SESSION_TTL_MS = 5 * 60 * 1e3;
|
|
5707
|
-
remoteIdleSessions = /* @__PURE__ */ new Map();
|
|
5708
5752
|
meshByWorkspaceCache = /* @__PURE__ */ new Map();
|
|
5709
5753
|
MESH_WORKSPACE_CACHE_TTL_MS = 5e3;
|
|
5710
5754
|
IDLE_AUTO_FAST_FORWARD_THROTTLE_MS = 30 * 60 * 1e3;
|