@adhdev/daemon-core 0.9.82-rc.364 → 0.9.82-rc.365

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.
@@ -8,7 +8,8 @@
8
8
 
9
9
  import { existsSync, readFileSync, writeFileSync } from 'fs';
10
10
  import { join } from 'path';
11
- import { createHash, randomBytes, randomUUID } from 'crypto';
11
+ import { randomBytes, randomUUID } from 'crypto';
12
+ import { shortHash } from '../system/hash.js';
12
13
  import { getConfigDir } from './config.js';
13
14
  import type {
14
15
  LocalMeshConfig,
@@ -309,7 +310,7 @@ function normalizeManualHostAddress(hostAddress: string): string {
309
310
  }
310
311
 
311
312
  export function tokenIdForManualPairing(token: string): string {
312
- return `tok_${createHash('sha256').update(token).digest('hex').slice(0, 16)}`;
313
+ return `tok_${shortHash(token)}`;
313
314
  }
314
315
 
315
316
  function normalizeTokenExpiry(value: unknown): string | undefined {
@@ -208,6 +208,60 @@ function classifyDirectDispatch(params: {
208
208
  return { ledgerOnlyStaleReason, isFreshUnacknowledged };
209
209
  }
210
210
 
211
+ /**
212
+ * Build a direct-dispatch MeshActiveWorkRecord from a `task_dispatched` ledger entry,
213
+ * matching it against the terminal ledger entries and live mesh nodes. Shared by the
214
+ * remote-ledger scan (inside the MeshRuntimeStore branch) and the full-ledger scan
215
+ * (standalone branch) — those two loops were previously byte-identical except for a
216
+ * single `dbTaskIds.has(taskId)` skip guard that stays in the caller. Returns the record
217
+ * plus `terminalRow` so the caller can route it into terminal/stale/active buckets.
218
+ */
219
+ function buildLedgerDirectDispatchRecord(
220
+ dispatch: MeshLedgerEntry,
221
+ ctx: { terminals: MeshLedgerEntry[]; nodes: any[] | undefined; now: number },
222
+ ): { record: MeshActiveWorkRecord; terminalRow: boolean } {
223
+ const taskId = directDispatchTaskId(dispatch);
224
+ const terminal = ctx.terminals
225
+ .filter(entry => new Date(entry.timestamp).getTime() >= new Date(dispatch.timestamp).getTime())
226
+ .find(entry => terminalMatchesDispatch(entry, dispatch, taskId));
227
+ const terminalStatus = terminal ? statusFromTerminal(terminal) : undefined;
228
+ const live = sessionStatusFromNodes(ctx.nodes, dispatch.nodeId, dispatch.sessionId);
229
+ const status = terminalStatus || live.status || 'assigned';
230
+ const terminalRow = Boolean(terminal && terminal.kind !== 'task_approval_needed');
231
+ const { ledgerOnlyStaleReason, isFreshUnacknowledged } = classifyDirectDispatch({
232
+ status,
233
+ isTerminalRow: terminalRow,
234
+ hasTerminalStatus: Boolean(terminalStatus),
235
+ liveStatus: live.status,
236
+ liveStaleReason: live.staleReason,
237
+ dispatchedToIdleSession: dispatch.payload?.dispatchedToIdleSession === true,
238
+ });
239
+ const message = readString(dispatch.payload?.message) || readString(dispatch.payload?.summary) || '';
240
+ const { title, summary } = summarizeMessage(message);
241
+ const record: MeshActiveWorkRecord = {
242
+ taskId,
243
+ source: 'direct',
244
+ status,
245
+ nodeId: dispatch.nodeId,
246
+ sessionId: dispatch.sessionId,
247
+ providerType: dispatch.providerType || readString(dispatch.payload?.providerType),
248
+ taskTitle: readString(dispatch.payload?.taskTitle) || title,
249
+ taskSummary: readString(dispatch.payload?.taskSummary) || summary,
250
+ message,
251
+ taskMode: readString(dispatch.payload?.taskMode),
252
+ createdAt: dispatch.timestamp,
253
+ updatedAt: terminal?.timestamp || dispatch.timestamp,
254
+ dispatchedAt: dispatch.timestamp,
255
+ elapsedMs: elapsedSince(dispatch.timestamp, ctx.now),
256
+ terminal: terminalRow,
257
+ terminalKind: terminal?.kind,
258
+ terminalAt: terminal?.timestamp,
259
+ staleReason: live.staleReason || ledgerOnlyStaleReason,
260
+ ...(isFreshUnacknowledged ? { staleDispatchUnacknowledged: true } : {}),
261
+ };
262
+ return { record, terminalRow };
263
+ }
264
+
211
265
  export function buildMeshActiveWorkSummary(activeWork: MeshActiveWorkRecord[]): MeshActiveWorkSummary {
212
266
  const statusCounts: Record<MeshActiveWorkStatus, number> = {
213
267
  pending: 0,
@@ -324,51 +378,13 @@ export function buildMeshActiveWork(opts: BuildMeshActiveWorkOptions): { activeW
324
378
  const ledgerEntries = (opts.ledgerEntries || []).slice().sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime());
325
379
  const terminals = ledgerEntries.filter(entry => TERMINAL_LEDGER_KINDS.has(entry.kind) || entry.kind === 'task_approval_needed');
326
380
  for (const dispatch of ledgerEntries.filter(isDirectDispatch)) {
327
- const taskId = directDispatchTaskId(dispatch);
328
- if (dbTaskIds.has(taskId)) continue; // already covered by MeshRuntimeStore path above
329
- const terminal = terminals
330
- .filter(entry => new Date(entry.timestamp).getTime() >= new Date(dispatch.timestamp).getTime())
331
- .find(entry => terminalMatchesDispatch(entry, dispatch, taskId));
332
- const terminalStatus = terminal ? statusFromTerminal(terminal) : undefined;
333
- const live = sessionStatusFromNodes(opts.nodes, dispatch.nodeId, dispatch.sessionId);
334
- const status = terminalStatus || live.status || 'assigned';
335
- const terminalRow = Boolean(terminal && terminal.kind !== 'task_approval_needed');
336
- const { ledgerOnlyStaleReason, isFreshUnacknowledged } = classifyDirectDispatch({
337
- status,
338
- isTerminalRow: terminalRow,
339
- hasTerminalStatus: Boolean(terminalStatus),
340
- liveStatus: live.status,
341
- liveStaleReason: live.staleReason,
342
- dispatchedToIdleSession: dispatch.payload?.dispatchedToIdleSession === true,
343
- });
344
- const message = readString(dispatch.payload?.message) || readString(dispatch.payload?.summary) || '';
345
- const { title, summary } = summarizeMessage(message);
346
- const record: MeshActiveWorkRecord = {
347
- taskId,
348
- source: 'direct',
349
- status,
350
- nodeId: dispatch.nodeId,
351
- sessionId: dispatch.sessionId,
352
- providerType: dispatch.providerType || readString(dispatch.payload?.providerType),
353
- taskTitle: readString(dispatch.payload?.taskTitle) || title,
354
- taskSummary: readString(dispatch.payload?.taskSummary) || summary,
355
- message,
356
- taskMode: readString(dispatch.payload?.taskMode),
357
- createdAt: dispatch.timestamp,
358
- updatedAt: terminal?.timestamp || dispatch.timestamp,
359
- dispatchedAt: dispatch.timestamp,
360
- elapsedMs: elapsedSince(dispatch.timestamp, now),
361
- terminal: terminalRow,
362
- terminalKind: terminal?.kind,
363
- terminalAt: terminal?.timestamp,
364
- staleReason: live.staleReason || ledgerOnlyStaleReason,
365
- ...(isFreshUnacknowledged ? { staleDispatchUnacknowledged: true } : {}),
366
- };
381
+ if (dbTaskIds.has(directDispatchTaskId(dispatch))) continue; // already covered by MeshRuntimeStore path above
382
+ const { record, terminalRow } = buildLedgerDirectDispatchRecord(dispatch, { terminals, nodes: opts.nodes, now });
367
383
  if (terminalRow) {
368
384
  terminalDirectWork.push(record);
369
385
  if (opts.includeTerminalDirect !== true) continue;
370
386
  }
371
- if ((live.staleReason || ledgerOnlyStaleReason) && !terminalRow) {
387
+ if (record.staleReason && !terminalRow) {
372
388
  staleDirectWork.push(record);
373
389
  continue;
374
390
  }
@@ -379,50 +395,12 @@ export function buildMeshActiveWork(opts: BuildMeshActiveWorkOptions): { activeW
379
395
  const ledgerEntries = (opts.ledgerEntries || []).slice().sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime());
380
396
  const terminals = ledgerEntries.filter(entry => TERMINAL_LEDGER_KINDS.has(entry.kind) || entry.kind === 'task_approval_needed');
381
397
  for (const dispatch of ledgerEntries.filter(isDirectDispatch)) {
382
- const taskId = directDispatchTaskId(dispatch);
383
- const terminal = terminals
384
- .filter(entry => new Date(entry.timestamp).getTime() >= new Date(dispatch.timestamp).getTime())
385
- .find(entry => terminalMatchesDispatch(entry, dispatch, taskId));
386
- const terminalStatus = terminal ? statusFromTerminal(terminal) : undefined;
387
- const live = sessionStatusFromNodes(opts.nodes, dispatch.nodeId, dispatch.sessionId);
388
- const status = terminalStatus || live.status || 'assigned';
389
- const terminalRow = Boolean(terminal && terminal.kind !== 'task_approval_needed');
390
- const { ledgerOnlyStaleReason, isFreshUnacknowledged } = classifyDirectDispatch({
391
- status,
392
- isTerminalRow: terminalRow,
393
- hasTerminalStatus: Boolean(terminalStatus),
394
- liveStatus: live.status,
395
- liveStaleReason: live.staleReason,
396
- dispatchedToIdleSession: dispatch.payload?.dispatchedToIdleSession === true,
397
- });
398
- const message = readString(dispatch.payload?.message) || readString(dispatch.payload?.summary) || '';
399
- const { title, summary } = summarizeMessage(message);
400
- const record: MeshActiveWorkRecord = {
401
- taskId,
402
- source: 'direct',
403
- status,
404
- nodeId: dispatch.nodeId,
405
- sessionId: dispatch.sessionId,
406
- providerType: dispatch.providerType || readString(dispatch.payload?.providerType),
407
- taskTitle: readString(dispatch.payload?.taskTitle) || title,
408
- taskSummary: readString(dispatch.payload?.taskSummary) || summary,
409
- message,
410
- taskMode: readString(dispatch.payload?.taskMode),
411
- createdAt: dispatch.timestamp,
412
- updatedAt: terminal?.timestamp || dispatch.timestamp,
413
- dispatchedAt: dispatch.timestamp,
414
- elapsedMs: elapsedSince(dispatch.timestamp, now),
415
- terminal: terminalRow,
416
- terminalKind: terminal?.kind,
417
- terminalAt: terminal?.timestamp,
418
- staleReason: live.staleReason || ledgerOnlyStaleReason,
419
- ...(isFreshUnacknowledged ? { staleDispatchUnacknowledged: true } : {}),
420
- };
398
+ const { record, terminalRow } = buildLedgerDirectDispatchRecord(dispatch, { terminals, nodes: opts.nodes, now });
421
399
  if (terminalRow) {
422
400
  terminalDirectWork.push(record);
423
401
  if (opts.includeTerminalDirect !== true) continue;
424
402
  }
425
- if ((live.staleReason || ledgerOnlyStaleReason) && !terminalRow) {
403
+ if (record.staleReason && !terminalRow) {
426
404
  staleDirectWork.push(record);
427
405
  continue;
428
406
  }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Shared SHA-256 hashing helpers for daemon-core (Node runtime).
3
+ *
4
+ * daemon-core runs under Node, so it uses `node:crypto` directly and must NOT
5
+ * import the server's WebCrypto-based `utils/crypto.ts` (different runtime +
6
+ * cross-package dependency direction). Output is byte-identical to that helper
7
+ * for the same input — lowercase hex SHA-256.
8
+ */
9
+ import { createHash } from 'node:crypto';
10
+
11
+ /** Full lowercase hex SHA-256 digest of `input`. */
12
+ export function sha256Hex(input: string): string {
13
+ return createHash('sha256').update(input).digest('hex');
14
+ }
15
+
16
+ /**
17
+ * Truncated SHA-256 hex digest — the first `length` hex chars (default 16).
18
+ * Used for stable short identifiers (workspace hashes, token ids, coordinator
19
+ * home dirs) where collision risk at 16 hex chars (64 bits) is negligible.
20
+ */
21
+ export function shortHash(input: string, length = 16): string {
22
+ return sha256Hex(input).slice(0, length);
23
+ }