@adhdev/daemon-standalone 0.9.77-rc.49 → 0.9.77-rc.50

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 CHANGED
@@ -22897,13 +22897,20 @@ Follow these recovery rules:
22897
22897
  MAX_LEDGER_SLICE_LIMIT: () => MAX_LEDGER_SLICE_LIMIT,
22898
22898
  appendLedgerEntry: () => appendLedgerEntry,
22899
22899
  appendRemoteLedgerEntries: () => appendRemoteLedgerEntries,
22900
+ buildTaskCompletionEvidence: () => buildTaskCompletionEvidence,
22900
22901
  getLedgerDir: () => getLedgerDir,
22901
22902
  getLedgerSummary: () => getLedgerSummary,
22902
22903
  getSessionRecoveryContext: () => getSessionRecoveryContext,
22904
+ isIntentionalCleanupStopEntry: () => isIntentionalCleanupStopEntry,
22903
22905
  meshLedgerEvents: () => meshLedgerEvents,
22904
22906
  readLedgerEntries: () => readLedgerEntries,
22905
22907
  readLedgerSlice: () => readLedgerSlice
22906
22908
  });
22909
+ function isIntentionalCleanupStopEntry(entry) {
22910
+ if (entry.kind !== "session_stopped" && entry.kind !== "task_failed" && entry.kind !== "task_stalled") return false;
22911
+ const payload = entry.payload && typeof entry.payload === "object" && !Array.isArray(entry.payload) ? entry.payload : {};
22912
+ return payload.intentional === true && (payload.reason === "operator_cleanup" || payload.intentionalStopReason === "operator_cleanup" || payload.source === "mesh_cleanup_sessions" || payload.source === "mesh_remove_node");
22913
+ }
22907
22914
  function getLedgerDir() {
22908
22915
  const dir = (0, import_path3.join)(getConfigDir(), LEDGER_DIR_NAME);
22909
22916
  if (!(0, import_fs3.existsSync)(dir)) {
@@ -22919,6 +22926,37 @@ Follow these recovery rules:
22919
22926
  const safe = meshId.replace(/[^a-zA-Z0-9_-]/g, "_");
22920
22927
  return (0, import_path3.join)(getLedgerDir(), `${safe}.${index}.jsonl`);
22921
22928
  }
22929
+ function buildTaskCompletionEvidence(opts) {
22930
+ const providerSessionId = opts.providerSessionId?.trim() || void 0;
22931
+ const providerType = opts.providerType?.trim() || void 0;
22932
+ return {
22933
+ source: "agent_status_event",
22934
+ event: opts.event,
22935
+ nodeId: opts.nodeId,
22936
+ sessionId: opts.sessionId,
22937
+ providerType,
22938
+ completedAt: opts.completedAt || (/* @__PURE__ */ new Date()).toISOString(),
22939
+ transcriptHandle: {
22940
+ kind: providerSessionId ? "provider_session" : "runtime_session",
22941
+ sessionId: opts.sessionId,
22942
+ providerSessionId,
22943
+ finalSummaryAvailable: typeof opts.finalSummary === "string" && opts.finalSummary.trim().length > 0
22944
+ },
22945
+ git: {
22946
+ status: "deferred",
22947
+ reason: "ordinary_completion_git_status_not_checked"
22948
+ },
22949
+ validation: {
22950
+ status: "deferred",
22951
+ commandsRun: [],
22952
+ reason: "ordinary_completion_validation_not_run"
22953
+ },
22954
+ checkpoint: {
22955
+ attempted: false,
22956
+ reason: "not_attempted_for_ordinary_completion"
22957
+ }
22958
+ };
22959
+ }
22922
22960
  function appendLedgerEntry(meshId, partial2) {
22923
22961
  const entry = {
22924
22962
  id: (0, import_crypto4.randomUUID)(),
@@ -23079,15 +23117,17 @@ Follow these recovery rules:
23079
23117
  summary.taskCompleted++;
23080
23118
  break;
23081
23119
  case "task_failed": {
23120
+ if (isIntentionalCleanupStopEntry(entry)) break;
23082
23121
  summary.taskFailed++;
23083
23122
  if (new Date(entry.timestamp).getTime() >= recentFailureCutoff) {
23084
23123
  summary.recentFailures++;
23085
23124
  }
23086
23125
  break;
23087
23126
  }
23088
- case "task_stalled":
23089
- summary.taskStalled++;
23127
+ case "task_stalled": {
23128
+ if (!isIntentionalCleanupStopEntry(entry)) summary.taskStalled++;
23090
23129
  break;
23130
+ }
23091
23131
  case "session_launched":
23092
23132
  summary.sessionLaunched++;
23093
23133
  break;
@@ -23126,6 +23166,7 @@ Follow these recovery rules:
23126
23166
  if (new Date(e.timestamp).getTime() < recentWindow) break;
23127
23167
  if (opts.nodeId && e.nodeId !== opts.nodeId) continue;
23128
23168
  if (e.kind === "task_failed") {
23169
+ if (isIntentionalCleanupStopEntry(e)) continue;
23129
23170
  consecutiveNodeFailures++;
23130
23171
  } else if (e.kind === "task_completed" || e.kind === "task_dispatched") {
23131
23172
  break;
@@ -23795,6 +23836,28 @@ Follow these recovery rules:
23795
23836
  if (localMesh) return localMesh;
23796
23837
  return components.router?.getCachedInlineMesh(meshId);
23797
23838
  }
23839
+ function isIntentionalCleanupStopMetadata(event) {
23840
+ return event.intentional === true || event.intentionalStop === true || event.operatorCleanup === true || event.reason === "operator_cleanup" || event.stopReason === "operator_cleanup" || event.cleanupReason === "operator_cleanup" || event.source === "mesh_cleanup_sessions" || event.source === "mesh_remove_node";
23841
+ }
23842
+ function hasRecentIntentionalCleanupStop(meshId, sessionId, nodeId) {
23843
+ if (!sessionId && !nodeId) return false;
23844
+ const cutoff = Date.now() - INTENTIONAL_CLEANUP_STOP_SUPPRESSION_MS;
23845
+ const entries = readLedgerEntries(meshId);
23846
+ for (let i = entries.length - 1; i >= 0; i--) {
23847
+ const entry = entries[i];
23848
+ const timestamp = new Date(entry.timestamp).getTime();
23849
+ if (!Number.isNaN(timestamp) && timestamp < cutoff) break;
23850
+ if (!isIntentionalCleanupStopEntry(entry)) continue;
23851
+ if (sessionId && entry.sessionId === sessionId) return true;
23852
+ if (!sessionId && nodeId && entry.nodeId === nodeId) return true;
23853
+ }
23854
+ return false;
23855
+ }
23856
+ function shouldSuppressIntentionalCleanupStop(args) {
23857
+ if (args.event !== "agent:stopped" && args.event !== "monitor:long_generating") return false;
23858
+ if (isIntentionalCleanupStopMetadata(args.metadataEvent)) return true;
23859
+ return hasRecentIntentionalCleanupStop(args.meshId, args.sessionId, args.nodeId);
23860
+ }
23798
23861
  function tryAssignQueueTask(components, meshId, nodeId, sessionId, providerType) {
23799
23862
  const task = claimNextTask(meshId, nodeId, sessionId);
23800
23863
  if (!task) {
@@ -24131,6 +24194,22 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
24131
24194
  return "";
24132
24195
  }
24133
24196
  function injectMeshSystemMessage(components, args) {
24197
+ const eventSessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
24198
+ const eventNodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
24199
+ const intentionalCleanupStop = shouldSuppressIntentionalCleanupStop({
24200
+ event: args.event,
24201
+ meshId: args.meshId,
24202
+ metadataEvent: args.metadataEvent,
24203
+ sessionId: eventSessionId || void 0,
24204
+ nodeId: eventNodeId || void 0
24205
+ });
24206
+ if (intentionalCleanupStop) {
24207
+ if (eventSessionId && eventNodeId) {
24208
+ remoteIdleSessions.delete(`${eventNodeId}:${eventSessionId}`);
24209
+ }
24210
+ LOG2.info("MeshEvents", `Suppressed ${args.event} for intentionally cleanup-stopped session ${eventSessionId || "(unknown session)"}`);
24211
+ return { success: true, forwarded: 0, suppressed: true, intentionalCleanupStop: true };
24212
+ }
24134
24213
  let completedTaskForLedger = null;
24135
24214
  if (args.event === "agent:generating_completed") {
24136
24215
  const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
@@ -24164,7 +24243,15 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
24164
24243
  taskId: completedTask.id,
24165
24244
  completedViaReady: true,
24166
24245
  providerSessionId: readNonEmptyString(args.metadataEvent.providerSessionId) || void 0,
24167
- finalSummary: readNonEmptyString(args.metadataEvent.finalSummary) || void 0
24246
+ finalSummary: readNonEmptyString(args.metadataEvent.finalSummary) || void 0,
24247
+ evidence: buildTaskCompletionEvidence({
24248
+ event: "agent:ready",
24249
+ nodeId,
24250
+ sessionId,
24251
+ providerType: providerType || void 0,
24252
+ providerSessionId: readNonEmptyString(args.metadataEvent.providerSessionId) || void 0,
24253
+ finalSummary: readNonEmptyString(args.metadataEvent.finalSummary) || void 0
24254
+ })
24168
24255
  }
24169
24256
  });
24170
24257
  } catch (e) {
@@ -24199,17 +24286,29 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
24199
24286
  const ledgerKind = EVENT_TO_LEDGER_KIND[args.event];
24200
24287
  if (ledgerKind) {
24201
24288
  try {
24289
+ const ledgerNodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0;
24290
+ const ledgerSessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0;
24291
+ const ledgerProviderType = readNonEmptyString(args.metadataEvent.providerType) || void 0;
24292
+ const completionEvidence = ledgerKind === "task_completed" && ledgerNodeId && ledgerSessionId ? buildTaskCompletionEvidence({
24293
+ event: "agent:generating_completed",
24294
+ nodeId: ledgerNodeId,
24295
+ sessionId: ledgerSessionId,
24296
+ providerType: ledgerProviderType,
24297
+ providerSessionId: readNonEmptyString(args.metadataEvent.providerSessionId) || void 0,
24298
+ finalSummary: readNonEmptyString(args.metadataEvent.finalSummary) || void 0
24299
+ }) : void 0;
24202
24300
  appendLedgerEntry(args.meshId, {
24203
24301
  kind: ledgerKind,
24204
- nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
24205
- sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
24206
- providerType: readNonEmptyString(args.metadataEvent.providerType) || void 0,
24302
+ nodeId: ledgerNodeId,
24303
+ sessionId: ledgerSessionId,
24304
+ providerType: ledgerProviderType,
24207
24305
  payload: {
24208
24306
  event: args.event,
24209
24307
  nodeLabel: args.nodeLabel,
24210
24308
  taskId: completedTaskForLedger?.id || void 0,
24211
24309
  providerSessionId: readNonEmptyString(args.metadataEvent.providerSessionId) || void 0,
24212
- finalSummary: readNonEmptyString(args.metadataEvent.finalSummary) || void 0
24310
+ finalSummary: readNonEmptyString(args.metadataEvent.finalSummary) || void 0,
24311
+ evidence: completionEvidence
24213
24312
  }
24214
24313
  });
24215
24314
  } catch (e) {
@@ -24325,7 +24424,14 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
24325
24424
  targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId) || readNonEmptyString(payload.instanceId),
24326
24425
  providerType: readNonEmptyString(payload.providerType),
24327
24426
  providerSessionId: readNonEmptyString(payload.providerSessionId),
24328
- finalSummary: readNonEmptyString(payload.finalSummary) || readNonEmptyString(payload.summary)
24427
+ finalSummary: readNonEmptyString(payload.finalSummary) || readNonEmptyString(payload.summary),
24428
+ intentional: payload.intentional === true,
24429
+ intentionalStop: payload.intentionalStop === true,
24430
+ operatorCleanup: payload.operatorCleanup === true,
24431
+ reason: readNonEmptyString(payload.reason),
24432
+ stopReason: readNonEmptyString(payload.stopReason),
24433
+ cleanupReason: readNonEmptyString(payload.cleanupReason),
24434
+ source: readNonEmptyString(payload.source)
24329
24435
  }
24330
24436
  });
24331
24437
  }
@@ -24366,6 +24472,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
24366
24472
  var pendingMeshCoordinatorEvents;
24367
24473
  var MESH_COORDINATOR_EVENTS;
24368
24474
  var EVENT_TO_LEDGER_KIND;
24475
+ var INTENTIONAL_CLEANUP_STOP_SUPPRESSION_MS;
24369
24476
  var autoLaunchInProgress;
24370
24477
  var autoLaunchCooldownUntil;
24371
24478
  var AUTO_LAUNCH_COOLDOWN_MS;
@@ -24395,6 +24502,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
24395
24502
  "agent:stopped": "task_failed",
24396
24503
  "monitor:long_generating": "task_stalled"
24397
24504
  };
24505
+ INTENTIONAL_CLEANUP_STOP_SUPPRESSION_MS = 30 * 60 * 1e3;
24398
24506
  autoLaunchInProgress = /* @__PURE__ */ new Set();
24399
24507
  autoLaunchCooldownUntil = /* @__PURE__ */ new Map();
24400
24508
  AUTO_LAUNCH_COOLDOWN_MS = 5e3;
@@ -46163,6 +46271,27 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
46163
46271
  isCompletedHostedSession(record2) {
46164
46272
  return record2?.lifecycle === "stopped" || record2?.lifecycle === "failed" || record2?.lifecycle === "interrupted";
46165
46273
  }
46274
+ async recordIntentionalMeshSessionStop(args) {
46275
+ try {
46276
+ const { appendLedgerEntry: appendLedgerEntry2 } = await Promise.resolve().then(() => (init_mesh_ledger(), mesh_ledger_exports));
46277
+ appendLedgerEntry2(args.meshId, {
46278
+ kind: "session_stopped",
46279
+ nodeId: args.nodeId,
46280
+ sessionId: args.sessionId,
46281
+ payload: {
46282
+ intentional: true,
46283
+ reason: "operator_cleanup",
46284
+ intentionalStopReason: "operator_cleanup",
46285
+ source: args.source,
46286
+ cleanupMode: args.mode,
46287
+ action: args.action,
46288
+ workspace: typeof args.node?.workspace === "string" ? args.node.workspace : void 0
46289
+ }
46290
+ });
46291
+ } catch (e) {
46292
+ LOG2.warn("MeshCleanup", `Failed to record intentional cleanup stop for ${args.sessionId}: ${e?.message || e}`);
46293
+ }
46294
+ }
46166
46295
  async cleanupMeshSessions(args) {
46167
46296
  if (args.mode === "preserve") {
46168
46297
  return { success: true, mode: "preserve", matchedCount: 0, stoppedSessionIds: [], deletedSessionIds: [], skippedSessionIds: [] };
@@ -46179,6 +46308,21 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
46179
46308
  const deleteUnsupportedSessionIds = [];
46180
46309
  const recordsRemainSessionIds = [];
46181
46310
  const errors = [];
46311
+ const cleanupSource = args.source || "mesh_cleanup_sessions";
46312
+ const markedIntentionalStopSessionIds = /* @__PURE__ */ new Set();
46313
+ const markIntentionalStop = async (sessionId, action) => {
46314
+ if (args.dryRun || markedIntentionalStopSessionIds.has(sessionId)) return;
46315
+ markedIntentionalStopSessionIds.add(sessionId);
46316
+ await this.recordIntentionalMeshSessionStop({
46317
+ meshId: args.meshId,
46318
+ nodeId: args.nodeId,
46319
+ node: args.node,
46320
+ sessionId,
46321
+ mode: args.mode,
46322
+ source: cleanupSource,
46323
+ action
46324
+ });
46325
+ };
46182
46326
  const matchedBySurfaceKind = {
46183
46327
  live_runtime: 0,
46184
46328
  recovery_snapshot: 0,
@@ -46201,7 +46345,10 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
46201
46345
  try {
46202
46346
  if (args.mode === "stop") {
46203
46347
  if (!completed) {
46204
- if (!args.dryRun) await this.deps.sessionHostControl.stopSession(sessionId);
46348
+ if (!args.dryRun) {
46349
+ await markIntentionalStop(sessionId, "stop_session");
46350
+ await this.deps.sessionHostControl.stopSession(sessionId);
46351
+ }
46205
46352
  stoppedSessionIds.push(sessionId);
46206
46353
  } else {
46207
46354
  skippedSessionIds.push(sessionId);
@@ -46218,6 +46365,7 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
46218
46365
  continue;
46219
46366
  }
46220
46367
  if (args.mode === "stop_and_delete") {
46368
+ if (!completed) await markIntentionalStop(sessionId, "delete_session_force");
46221
46369
  if (!args.dryRun) await this.deps.sessionHostControl.deleteSession(sessionId, { force: true });
46222
46370
  deletedSessionIds.push(sessionId);
46223
46371
  continue;
@@ -46229,6 +46377,7 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
46229
46377
  recordsRemainSessionIds.push(sessionId);
46230
46378
  if (args.mode === "stop_and_delete" && !completed) {
46231
46379
  try {
46380
+ await markIntentionalStop(sessionId, "stop_session");
46232
46381
  await this.deps.sessionHostControl.stopSession(sessionId);
46233
46382
  stoppedSessionIds.push(sessionId);
46234
46383
  } catch (stopError) {
@@ -47129,7 +47278,8 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
47129
47278
  node,
47130
47279
  mode,
47131
47280
  sessionIds,
47132
- dryRun: args?.dryRun === true
47281
+ dryRun: args?.dryRun === true,
47282
+ source: "mesh_cleanup_sessions"
47133
47283
  });
47134
47284
  return result;
47135
47285
  } catch (e) {
@@ -47264,7 +47414,7 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
47264
47414
  );
47265
47415
  let sessionCleanup;
47266
47416
  if (node && sessionCleanupMode !== "preserve") {
47267
- sessionCleanup = await this.cleanupMeshSessions({ meshId, nodeId, node, mode: sessionCleanupMode });
47417
+ sessionCleanup = await this.cleanupMeshSessions({ meshId, nodeId, node, mode: sessionCleanupMode, source: "mesh_remove_node" });
47268
47418
  if (sessionCleanup.success === false) return { success: false, removed: false, sessionCleanup };
47269
47419
  }
47270
47420
  let worktreeCleanup;