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

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
@@ -22894,13 +22894,15 @@ Follow these recovery rules:
22894
22894
  });
22895
22895
  var mesh_ledger_exports = {};
22896
22896
  __export2(mesh_ledger_exports, {
22897
+ MAX_LEDGER_SLICE_LIMIT: () => MAX_LEDGER_SLICE_LIMIT,
22897
22898
  appendLedgerEntry: () => appendLedgerEntry,
22898
22899
  appendRemoteLedgerEntries: () => appendRemoteLedgerEntries,
22899
22900
  getLedgerDir: () => getLedgerDir,
22900
22901
  getLedgerSummary: () => getLedgerSummary,
22901
22902
  getSessionRecoveryContext: () => getSessionRecoveryContext,
22902
22903
  meshLedgerEvents: () => meshLedgerEvents,
22903
- readLedgerEntries: () => readLedgerEntries
22904
+ readLedgerEntries: () => readLedgerEntries,
22905
+ readLedgerSlice: () => readLedgerSlice
22904
22906
  });
22905
22907
  function getLedgerDir() {
22906
22908
  const dir = (0, import_path3.join)(getConfigDir(), LEDGER_DIR_NAME);
@@ -22943,15 +22945,49 @@ Follow these recovery rules:
22943
22945
  throw new Error(`Failed to append to ledger for mesh ${meshId}: ${e.message}`);
22944
22946
  }
22945
22947
  }
22948
+ function clampLedgerSliceLimit(limit) {
22949
+ if (typeof limit !== "number" || !Number.isFinite(limit)) return DEFAULT_LEDGER_SLICE_LIMIT;
22950
+ return Math.max(1, Math.min(MAX_LEDGER_SLICE_LIMIT, Math.floor(limit)));
22951
+ }
22952
+ function isValidRemoteLedgerEntry(meshId, value) {
22953
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
22954
+ const entry = value;
22955
+ if (typeof entry.id !== "string" || !entry.id.trim()) return false;
22956
+ if (entry.meshId !== meshId) return false;
22957
+ if (typeof entry.timestamp !== "string" || Number.isNaN(new Date(entry.timestamp).getTime())) return false;
22958
+ if (typeof entry.kind !== "string" || !entry.kind.trim()) return false;
22959
+ if (!entry.payload || typeof entry.payload !== "object" || Array.isArray(entry.payload)) return false;
22960
+ return true;
22961
+ }
22946
22962
  function appendRemoteLedgerEntries(meshId, entries) {
22947
- if (entries.length === 0) return;
22963
+ if (entries.length === 0) return { accepted: 0, skippedDuplicate: 0, rejectedInvalid: 0, entries: [] };
22948
22964
  const ledgerPath = getLedgerPath(meshId);
22949
22965
  const existing = new Set(readLedgerEntries(meshId).map((e) => e.id));
22950
- const newEntries = entries.filter((e) => !existing.has(e.id));
22951
- if (newEntries.length === 0) return;
22966
+ const validEntries = [];
22967
+ let rejectedInvalid = 0;
22968
+ let skippedDuplicate = 0;
22969
+ for (const entry of entries) {
22970
+ if (!isValidRemoteLedgerEntry(meshId, entry)) {
22971
+ rejectedInvalid++;
22972
+ continue;
22973
+ }
22974
+ if (existing.has(entry.id)) {
22975
+ skippedDuplicate++;
22976
+ continue;
22977
+ }
22978
+ existing.add(entry.id);
22979
+ validEntries.push(entry);
22980
+ }
22981
+ if (validEntries.length === 0) {
22982
+ return { accepted: 0, skippedDuplicate, rejectedInvalid, entries: [] };
22983
+ }
22952
22984
  try {
22953
- const lines = newEntries.map((e) => JSON.stringify(e)).join("\n") + "\n";
22985
+ const lines = validEntries.map((e) => JSON.stringify(e)).join("\n") + "\n";
22954
22986
  (0, import_fs3.appendFileSync)(ledgerPath, lines, { encoding: "utf-8", mode: 384 });
22987
+ for (const entry of validEntries) {
22988
+ meshLedgerEvents.emit("append", meshId, entry);
22989
+ }
22990
+ return { accepted: validEntries.length, skippedDuplicate, rejectedInvalid, entries: validEntries };
22955
22991
  } catch (e) {
22956
22992
  throw new Error(`Failed to append remote ledger entries for mesh ${meshId}: ${e.message}`);
22957
22993
  }
@@ -22990,6 +23026,34 @@ Follow these recovery rules:
22990
23026
  }
22991
23027
  return entries;
22992
23028
  }
23029
+ function readLedgerSlice(meshId, opts) {
23030
+ const limit = clampLedgerSliceLimit(opts?.limit);
23031
+ let entries = readLedgerEntries(meshId, { since: opts?.since, kind: opts?.kind });
23032
+ const afterId = typeof opts?.afterId === "string" && opts.afterId.trim() ? opts.afterId.trim() : null;
23033
+ if (afterId) {
23034
+ const index = entries.findIndex((entry) => entry.id === afterId);
23035
+ entries = index >= 0 ? entries.slice(index + 1) : entries;
23036
+ }
23037
+ const bounded = entries.slice(0, limit);
23038
+ return {
23039
+ protocol: "adhdev.mesh.ledger.slice.v1",
23040
+ meshId,
23041
+ entries: bounded,
23042
+ cursor: {
23043
+ afterId,
23044
+ nextAfterId: bounded.length ? bounded[bounded.length - 1].id : afterId,
23045
+ limit,
23046
+ hasMore: entries.length > bounded.length
23047
+ },
23048
+ summary: getLedgerSummary(meshId),
23049
+ sourceOfTruth: {
23050
+ kind: "local_jsonl",
23051
+ path: getLedgerPath(meshId),
23052
+ bounded: true,
23053
+ maxLimit: MAX_LEDGER_SLICE_LIMIT
23054
+ }
23055
+ };
23056
+ }
22993
23057
  function getLedgerSummary(meshId) {
22994
23058
  const entries = readLedgerEntries(meshId);
22995
23059
  const now = Date.now();
@@ -23119,6 +23183,8 @@ Follow these recovery rules:
23119
23183
  var LEDGER_DIR_NAME;
23120
23184
  var MAX_FILE_SIZE_BYTES;
23121
23185
  var RECENT_FAILURE_WINDOW_MS;
23186
+ var DEFAULT_LEDGER_SLICE_LIMIT;
23187
+ var MAX_LEDGER_SLICE_LIMIT;
23122
23188
  var meshLedgerEvents;
23123
23189
  var init_mesh_ledger = __esm2({
23124
23190
  "src/mesh/mesh-ledger.ts"() {
@@ -23131,6 +23197,8 @@ Follow these recovery rules:
23131
23197
  LEDGER_DIR_NAME = "mesh-ledger";
23132
23198
  MAX_FILE_SIZE_BYTES = 10 * 1024 * 1024;
23133
23199
  RECENT_FAILURE_WINDOW_MS = 30 * 60 * 1e3;
23200
+ DEFAULT_LEDGER_SLICE_LIMIT = 100;
23201
+ MAX_LEDGER_SLICE_LIMIT = 500;
23134
23202
  meshLedgerEvents = new import_events.EventEmitter();
23135
23203
  }
23136
23204
  });
@@ -27444,6 +27512,7 @@ ${lastSnapshot}`;
27444
27512
  IdeProviderInstance: () => IdeProviderInstance,
27445
27513
  InMemoryGitSnapshotStore: () => InMemoryGitSnapshotStore,
27446
27514
  LOG: () => LOG2,
27515
+ MAX_LEDGER_SLICE_LIMIT: () => MAX_LEDGER_SLICE_LIMIT,
27447
27516
  MIN_GIT_WORKSPACE_POLL_INTERVAL_MS: () => MIN_GIT_WORKSPACE_POLL_INTERVAL_MS,
27448
27517
  MIN_MACHINE_RUNTIME_SUBSCRIPTION_INTERVAL_MS: () => MIN_MACHINE_RUNTIME_SUBSCRIPTION_INTERVAL_MS2,
27449
27518
  MIN_SESSION_HOST_DIAGNOSTICS_SUBSCRIPTION_INTERVAL_MS: () => MIN_SESSION_HOST_DIAGNOSTICS_SUBSCRIPTION_INTERVAL_MS2,
@@ -27459,12 +27528,15 @@ ${lastSnapshot}`;
27459
27528
  addNode: () => addNode,
27460
27529
  appendLedgerEntry: () => appendLedgerEntry,
27461
27530
  appendRecentActivity: () => appendRecentActivity,
27531
+ appendRemoteLedgerEntries: () => appendRemoteLedgerEntries,
27462
27532
  buildAssistantChatMessage: () => buildAssistantChatMessage,
27463
27533
  buildChatMessage: () => buildChatMessage,
27464
27534
  buildChatMessageSignature: () => buildChatMessageSignature,
27465
27535
  buildChatTailDeliverySignature: () => buildChatTailDeliverySignature,
27466
27536
  buildCoordinatorSystemPrompt: () => buildCoordinatorSystemPrompt,
27467
27537
  buildMachineInfo: () => buildMachineInfo2,
27538
+ buildMeshLedgerReconciliationEvidence: () => buildMeshLedgerReconciliationEvidence,
27539
+ buildMeshLedgerReplicaEvidence: () => buildMeshLedgerReplicaEvidence,
27468
27540
  buildP2pRelayFailurePayload: () => buildP2pRelayFailurePayload,
27469
27541
  buildPinnedGlobalInstallCommand: () => buildPinnedGlobalInstallCommand,
27470
27542
  buildRuntimeSystemChatMessage: () => buildRuntimeSystemChatMessage,
@@ -27587,6 +27659,7 @@ ${lastSnapshot}`;
27587
27659
  probeCdpPort: () => probeCdpPort,
27588
27660
  readChatHistory: () => readChatHistory,
27589
27661
  readLedgerEntries: () => readLedgerEntries,
27662
+ readLedgerSlice: () => readLedgerSlice,
27590
27663
  recordDebugTrace: () => recordDebugTrace,
27591
27664
  registerExtensionProviders: () => registerExtensionProviders,
27592
27665
  removeNode: () => removeNode,
@@ -29457,6 +29530,62 @@ ${lastSnapshot}`;
29457
29530
  return result;
29458
29531
  }
29459
29532
  init_mesh_ledger();
29533
+ function lastTimestamp(slice) {
29534
+ const entries = Array.isArray(slice?.entries) ? slice.entries : [];
29535
+ return entries.length ? entries[entries.length - 1].timestamp : null;
29536
+ }
29537
+ function buildMeshLedgerReplicaEvidence(args) {
29538
+ const entriesReceived = Array.isArray(args.slice?.entries) ? args.slice.entries.length : 0;
29539
+ return {
29540
+ nodeId: args.nodeId,
29541
+ ...args.daemonId ? { daemonId: args.daemonId } : {},
29542
+ status: args.status ?? (args.importResult && args.importResult.accepted > 0 ? "imported" : "queried"),
29543
+ transport: args.transport,
29544
+ protocol: "adhdev.mesh.ledger.slice.v1",
29545
+ entriesReceived,
29546
+ entriesImported: args.importResult?.accepted ?? 0,
29547
+ skippedDuplicate: args.importResult?.skippedDuplicate ?? 0,
29548
+ rejectedInvalid: args.importResult?.rejectedInvalid ?? 0,
29549
+ hasMore: args.slice?.cursor?.hasMore === true,
29550
+ nextAfterId: args.slice?.cursor?.nextAfterId ?? null,
29551
+ lastTimestamp: lastTimestamp(args.slice),
29552
+ ...args.slice?.summary ? { summary: args.slice.summary } : {},
29553
+ ...args.error ? {
29554
+ error: args.error,
29555
+ noFallbackReason: "Ledger reconciliation is P2P/local-first only; Cloud/D1 ledger sync is intentionally disabled."
29556
+ } : {}
29557
+ };
29558
+ }
29559
+ function buildMeshLedgerReconciliationEvidence(meshId, replicas) {
29560
+ const failedNodes = replicas.filter((replica) => replica.status === "failed").map((replica) => replica.nodeId);
29561
+ const pendingNodes = replicas.filter((replica) => replica.hasMore && replica.status !== "failed").map((replica) => replica.nodeId);
29562
+ return {
29563
+ protocol: "adhdev.mesh.ledger.reconciliation.v1",
29564
+ meshId,
29565
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
29566
+ sourceOfTruth: {
29567
+ kind: "coordinator_local_jsonl",
29568
+ p2pOnly: true,
29569
+ cloudD1LedgerSync: false,
29570
+ notes: "Coordinator reconciles bounded slices from daemon-local JSONL ledgers over P2P DataChannel; Cloud/D1 is not a ledger source of truth."
29571
+ },
29572
+ replicas,
29573
+ totals: {
29574
+ replicas: replicas.length,
29575
+ queried: replicas.filter((replica) => replica.status !== "failed").length,
29576
+ failed: failedNodes.length,
29577
+ entriesReceived: replicas.reduce((sum, replica) => sum + replica.entriesReceived, 0),
29578
+ entriesImported: replicas.reduce((sum, replica) => sum + replica.entriesImported, 0),
29579
+ skippedDuplicate: replicas.reduce((sum, replica) => sum + replica.skippedDuplicate, 0),
29580
+ rejectedInvalid: replicas.reduce((sum, replica) => sum + replica.rejectedInvalid, 0)
29581
+ },
29582
+ convergence: {
29583
+ complete: failedNodes.length === 0 && pendingNodes.length === 0,
29584
+ pendingNodes,
29585
+ failedNodes
29586
+ }
29587
+ };
29588
+ }
29460
29589
  init_mesh_work_queue();
29461
29590
  init_mesh_events();
29462
29591
  var NO_FALLBACK_REASON = "Repo Mesh command/data-plane is P2P-only; WS/REST command fallback is intentionally disabled to preserve the transport boundary.";
@@ -46854,6 +46983,35 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
46854
46983
  return { success: false, error: e.message };
46855
46984
  }
46856
46985
  }
46986
+ case "get_mesh_ledger_slice": {
46987
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
46988
+ if (!meshId) return { success: false, error: "meshId required" };
46989
+ try {
46990
+ const { readLedgerSlice: readLedgerSlice2 } = await Promise.resolve().then(() => (init_mesh_ledger(), mesh_ledger_exports));
46991
+ const kind = Array.isArray(args?.kind) ? args.kind.filter((k) => typeof k === "string") : void 0;
46992
+ const slice = readLedgerSlice2(meshId, {
46993
+ afterId: typeof args?.afterId === "string" ? args.afterId : void 0,
46994
+ since: typeof args?.since === "string" ? args.since : void 0,
46995
+ kind,
46996
+ limit: typeof args?.limit === "number" ? args.limit : void 0
46997
+ });
46998
+ return { success: true, slice };
46999
+ } catch (e) {
47000
+ return { success: false, error: e.message };
47001
+ }
47002
+ }
47003
+ case "import_mesh_ledger_slice": {
47004
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
47005
+ if (!meshId) return { success: false, error: "meshId required" };
47006
+ try {
47007
+ const { appendRemoteLedgerEntries: appendRemoteLedgerEntries2, getLedgerSummary: getLedgerSummary2 } = await Promise.resolve().then(() => (init_mesh_ledger(), mesh_ledger_exports));
47008
+ const entries = Array.isArray(args?.entries) ? args.entries : Array.isArray(args?.slice?.entries) ? args.slice.entries : [];
47009
+ const result = appendRemoteLedgerEntries2(meshId, entries);
47010
+ return { success: true, result, summary: getLedgerSummary2(meshId) };
47011
+ } catch (e) {
47012
+ return { success: false, error: e.message };
47013
+ }
47014
+ }
46857
47015
  case "get_mesh_queue": {
46858
47016
  const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
46859
47017
  if (!meshId) return { success: false, error: "meshId required" };