clay-server 4.1.0-beta.8 → 4.1.0

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.
Files changed (119) hide show
  1. package/lib/context-usage.js +47 -0
  2. package/lib/issues-mcp-server.js +1 -1
  3. package/lib/issues-service.js +3 -1
  4. package/lib/loop-registry-occurrence.js +1 -0
  5. package/lib/notes.js +20 -3
  6. package/lib/os-users.js +6 -0
  7. package/lib/project-connection.js +14 -3
  8. package/lib/project-file-path.js +13 -0
  9. package/lib/project-file-watch.js +19 -8
  10. package/lib/project-filesystem.js +10 -16
  11. package/lib/project-http.js +53 -48
  12. package/lib/project-logs-mcp-server.js +8 -8
  13. package/lib/project-logs-schema.js +1 -1
  14. package/lib/project-logs-service.js +46 -13
  15. package/lib/project-logs-store.js +17 -15
  16. package/lib/project-logs.js +70 -0
  17. package/lib/project-loop.js +1 -0
  18. package/lib/project-notifications.js +1 -1
  19. package/lib/project-pending-message-queue.js +71 -7
  20. package/lib/project-scheduled-messages.js +235 -52
  21. package/lib/project-scheduled-result-store.js +131 -0
  22. package/lib/project-scheduled-task-execution.js +35 -2
  23. package/lib/project-session-notes.js +11 -4
  24. package/lib/project-sessions.js +7 -0
  25. package/lib/project-user-message.js +72 -5
  26. package/lib/project.js +60 -5
  27. package/lib/public/app.js +27 -12
  28. package/lib/public/css/avatar-imprints.css +1 -1
  29. package/lib/public/css/command-palette.css +20 -0
  30. package/lib/public/css/git-panel.css +13 -12
  31. package/lib/public/css/git-placard.css +4 -4
  32. package/lib/public/css/input.css +0 -109
  33. package/lib/public/css/issues.css +78 -31
  34. package/lib/public/css/mobile-nav.css +7 -0
  35. package/lib/public/css/notifications-center.css +3 -3
  36. package/lib/public/css/pending-message-queue.css +3 -0
  37. package/lib/public/css/project-logs.css +15 -0
  38. package/lib/public/css/sidebar.css +7 -0
  39. package/lib/public/css/sticky-notes.css +29 -0
  40. package/lib/public/modules/app-connection.js +9 -1
  41. package/lib/public/modules/app-favicon.js +10 -0
  42. package/lib/public/modules/app-messages.js +49 -37
  43. package/lib/public/modules/app-notifications.js +2 -0
  44. package/lib/public/modules/app-panels.js +77 -23
  45. package/lib/public/modules/app-projects.js +3 -0
  46. package/lib/public/modules/app-rate-limit.js +148 -243
  47. package/lib/public/modules/avatar-imprint.js +25 -70
  48. package/lib/public/modules/avatar.js +12 -4
  49. package/lib/public/modules/chat-render-runtime.js +20 -2
  50. package/lib/public/modules/clay-issue-links.js +23 -2
  51. package/lib/public/modules/clay-log-links.js +11 -1
  52. package/lib/public/modules/context-view-preference.js +143 -0
  53. package/lib/public/modules/default-vendor.js +116 -0
  54. package/lib/public/modules/filebrowser-tabs.js +1 -0
  55. package/lib/public/modules/filebrowser.js +45 -18
  56. package/lib/public/modules/input.js +81 -20
  57. package/lib/public/modules/issues-render.js +36 -10
  58. package/lib/public/modules/issues.js +95 -13
  59. package/lib/public/modules/markdown.js +3 -1
  60. package/lib/public/modules/pane-bridge.js +14 -8
  61. package/lib/public/modules/pane-issue-bridge.js +27 -0
  62. package/lib/public/modules/pending-message-queue.js +43 -1
  63. package/lib/public/modules/permission-hints.js +13 -0
  64. package/lib/public/modules/profile.js +9 -9
  65. package/lib/public/modules/project-activation.js +49 -0
  66. package/lib/public/modules/project-logs-render.js +13 -3
  67. package/lib/public/modules/project-logs.js +45 -15
  68. package/lib/public/modules/right-workbench.js +44 -0
  69. package/lib/public/modules/scheduled-message-state.js +12 -36
  70. package/lib/public/modules/scheduled-result-client.js +33 -0
  71. package/lib/public/modules/scheduled-result-display.js +50 -0
  72. package/lib/public/modules/scheduled-result-notification.js +31 -0
  73. package/lib/public/modules/scheduled-task-presentation.js +15 -0
  74. package/lib/public/modules/scheduled-tasks.js +9 -21
  75. package/lib/public/modules/sidebar-mobile.js +51 -3
  76. package/lib/public/modules/sidebar-sessions.js +65 -5
  77. package/lib/public/modules/split-session-boundary.js +28 -0
  78. package/lib/public/modules/split-view.js +42 -7
  79. package/lib/public/modules/sticky-notes-browser.js +130 -18
  80. package/lib/public/modules/sticky-notes.js +2 -3
  81. package/lib/public/modules/terminal.js +4 -4
  82. package/lib/public/modules/thinking-lifecycle.js +4 -1
  83. package/lib/public/modules/tools.js +5 -1
  84. package/lib/scheduled-run-state.js +10 -2
  85. package/lib/scheduler.js +1 -0
  86. package/lib/sdk-bridge.js +97 -61
  87. package/lib/sdk-message-processor.js +44 -13
  88. package/lib/sdk-skill-discovery.js +6 -28
  89. package/lib/sdk-turn-queue.js +14 -0
  90. package/lib/server-context-view.js +53 -0
  91. package/lib/server-default-vendor.js +60 -0
  92. package/lib/server.js +23 -6
  93. package/lib/session-notes-mcp-server.js +3 -5
  94. package/lib/session-pair-factory.js +2 -2
  95. package/lib/session-pair-mcp-server.js +5 -4
  96. package/lib/session-title-generator.js +89 -0
  97. package/lib/session-title-policy.js +8 -2
  98. package/lib/sessions.js +26 -1
  99. package/lib/user-presence.js +6 -0
  100. package/lib/users-context-view-preferences.js +65 -0
  101. package/lib/users-default-vendor-preferences.js +80 -0
  102. package/lib/users.js +12 -0
  103. package/lib/ws-schema.js +11 -0
  104. package/lib/yoke/acp-agent-profiles.js +15 -2
  105. package/lib/yoke/adapters/acp.js +3 -2
  106. package/lib/yoke/adapters/antigravity.js +2 -2
  107. package/lib/yoke/adapters/claude-worker.js +11 -12
  108. package/lib/yoke/adapters/claude.js +75 -22
  109. package/lib/yoke/adapters/codex.js +216 -29
  110. package/lib/yoke/adapters/kiro.js +6 -5
  111. package/lib/yoke/claude-context-usage.js +57 -0
  112. package/lib/yoke/claude-message-correlation.js +31 -0
  113. package/lib/yoke/claude-model-switch.js +15 -0
  114. package/lib/yoke/index.js +5 -1
  115. package/lib/yoke/mcp-bridge-server.js +18 -5
  116. package/lib/yoke/session-skill-catalog.js +45 -0
  117. package/lib/yoke/skill-discovery.js +60 -20
  118. package/lib/yoke/vendor-registry.js +1 -1
  119. package/package.json +5 -7
@@ -253,6 +253,11 @@ function attachProjectLogsService(ctx) {
253
253
 
254
254
  function createProjectBound(initial, revalidate) {
255
255
  var store = storeForStatus(initial.status);
256
+ var scheduledResultViewerId = initial.principal.userId || null;
257
+
258
+ function withScheduledResultViewer(args) {
259
+ return Object.assign({}, args || {}, { scheduledResultOwnerId: scheduledResultViewerId });
260
+ }
256
261
 
257
262
  // Identity, authorization, and the target store are all re-derived per
258
263
  // call. revalidate() re-checks exact session identity where there is one;
@@ -288,7 +293,15 @@ function attachProjectLogsService(ctx) {
288
293
 
289
294
  function scopedEntry(args) {
290
295
  var scope = currentScope();
291
- return visibleEntry(live(), args && args.ref, scope.status, scope.worktreeOnly);
296
+ var entry = visibleEntry(live(), args && args.ref, scope.status, scope.worktreeOnly);
297
+ if (entry.scheduledResult && (entry.scheduledResult.ownerId || null) !== scheduledResultViewerId) throw new Error("Log entry not found.");
298
+ return entry;
299
+ }
300
+
301
+ function mutableEntry(args) {
302
+ var entry = scopedEntry(args);
303
+ if (entry.scheduledResult) throw new Error("Scheduled results are server-owned records.");
304
+ return entry;
292
305
  }
293
306
 
294
307
  function mutationContext() {
@@ -312,7 +325,7 @@ function attachProjectLogsService(ctx) {
312
325
  listLogs: function (args) {
313
326
  var scope = currentScope();
314
327
  var status = scope.status;
315
- var options = scopedArgs(args, status, scope.worktreeOnly);
328
+ var options = withScheduledResultViewer(scopedArgs(args, status, scope.worktreeOnly));
316
329
  var result = live().list(options);
317
330
  result.contextMode = options.contextMode;
318
331
  result.currentChangeSetId = status.changeSetId || null;
@@ -322,7 +335,7 @@ function attachProjectLogsService(ctx) {
322
335
  searchLogs: function (args) {
323
336
  var scope = currentScope();
324
337
  var status = scope.status;
325
- var options = scopedArgs(args, status, scope.worktreeOnly);
338
+ var options = withScheduledResultViewer(scopedArgs(args, status, scope.worktreeOnly));
326
339
  var result = live().search(options);
327
340
  result.contextMode = options.contextMode;
328
341
  result.currentChangeSetId = status.changeSetId || null;
@@ -334,18 +347,20 @@ function attachProjectLogsService(ctx) {
334
347
  },
335
348
  logHistory: function (args) { scopedEntry(args); return live().history(args && args.ref, args || {}); },
336
349
  commentLog: function (args) { scopedEntry(args); return live().comment(args && args.ref, args || {}, author(), mutationContext()); },
350
+ scheduledResultsState: function () { var scope = currentScope(); return live().unreadScheduledResults(scheduledResultViewerId, scopedArgs({}, scope.status, scope.worktreeOnly)); },
351
+ markScheduledResultRead: function (args) { var scope = currentScope(); scopedEntry(args); return live().markScheduledResultRead(args && args.ref, scheduledResultViewerId, scopedArgs({}, scope.status, scope.worktreeOnly)); },
337
352
  readLogRevision: function (args) { scopedEntry(args); return live().readRevision(args && args.ref, args && args.revision); },
338
353
  listLogFeedback: canonical ? function (args) {
339
354
  var scope = currentScope();
340
- return live().feedback(scopedArgs(args, scope.status, scope.worktreeOnly));
355
+ return live().feedback(withScheduledResultViewer(scopedArgs(args, scope.status, scope.worktreeOnly)));
341
356
  } : driverOnly,
342
- reviewLogComment: canonical ? function (args) { scopedEntry(args); return live().review(args && args.ref, args || {}, author(), mutationContext()); } : driverOnly,
357
+ reviewLogComment: canonical ? function (args) { if (args && args.action === "incorporate") mutableEntry(args); else scopedEntry(args); return live().review(args && args.ref, args || {}, author(), mutationContext()); } : driverOnly,
343
358
  revertLog: canonical
344
- ? function (args) { scopedEntry(args); return live().revert(args && args.ref, args && args.revision, args && args.reason, author(), mutationContext()); }
359
+ ? function (args) { mutableEntry(args); return live().revert(args && args.ref, args && args.revision, args && args.reason, author(), mutationContext()); }
345
360
  : driverOnly,
346
361
  createLog: canonical ? function (args) { return live().create(args || {}, author(), mutationContext()); } : agentOnly,
347
- updateLog: canonical ? function (args) { scopedEntry(args); return live().update(args && args.ref, args || {}, author(), mutationContext()); } : agentOnly,
348
- linkLog: canonical ? function (args) { scopedEntry(args); return live().link(args && args.ref, args && args.links, author(), mutationContext()); } : agentOnly,
362
+ updateLog: canonical ? function (args) { mutableEntry(args); return live().update(args && args.ref, args || {}, author(), mutationContext()); } : agentOnly,
363
+ linkLog: canonical ? function (args) { mutableEntry(args); return live().link(args && args.ref, args && args.links, author(), mutationContext()); } : agentOnly,
349
364
  removeLog: canDelete ? function (args) {
350
365
  var current = revalidate();
351
366
  var project = projectFor(current.principal.projectSlug);
@@ -353,7 +368,7 @@ function attachProjectLogsService(ctx) {
353
368
  if (!project || !authorizedProject(current.principal, currentStatus)) {
354
369
  throw new Error("Project Logs are not available for this project.");
355
370
  }
356
- visibleEntry(storeForStatus(currentStatus), args && args.ref, currentStatus, projectAuthorization(current.principal, currentStatus).worktreeOnly);
371
+ mutableEntry(args);
357
372
  if (current.principal.kind !== "session" && !ownsProject(current.principal, currentStatus)) ownerOnly();
358
373
  return storeForStatus(currentStatus).remove(args && args.ref, current.author, recordContext(currentStatus));
359
374
  } : ownerOnly,
@@ -367,6 +382,20 @@ function attachProjectLogsService(ctx) {
367
382
  return storeForStatus(projectStatus).setContextState(recordContext(projectStatus), status);
368
383
  }
369
384
 
385
+ // Trusted server lifecycle path. It is deliberately not part of any bound
386
+ // human or MCP write capability, so system results cannot be impersonated.
387
+ function recordScheduledResult(source) {
388
+ var data = source || {};
389
+ var project = projectFor(data.projectSlug);
390
+ if (!project) throw new Error("Project not found.");
391
+ var status = project.getStatus();
392
+ if (!status || status.isMate === true) throw new Error("Scheduled results require a project.");
393
+ if (isMultiUser()) {
394
+ if (!data.ownerId || !findUserById(data.ownerId)) throw new Error("Scheduled result owner is unavailable.");
395
+ }
396
+ return storeForStatus(status).createScheduledResult(data, recordContext(status));
397
+ }
398
+
370
399
  function createClayBound(revalidate) {
371
400
  function target(args) {
372
401
  var principal = revalidate();
@@ -374,14 +403,16 @@ function attachProjectLogsService(ctx) {
374
403
  if (!slug) throw new Error("An exact project slug is required.");
375
404
  var status = statusFor(principal, slug);
376
405
  var decision = projectAuthorization(principal, status);
377
- return { store: storeForStatus(status), status: status, worktreeOnly: decision.worktreeOnly };
406
+ return { store: storeForStatus(status), status: status, worktreeOnly: decision.worktreeOnly, viewerId: principal.userId || null };
378
407
  }
379
408
 
380
409
  function targetEntry(args) {
381
410
  var selected = target(args);
411
+ var entry = visibleEntry(selected.store, args && args.ref, selected.status, selected.worktreeOnly);
412
+ if (entry.scheduledResult && (entry.scheduledResult.ownerId || null) !== selected.viewerId) throw new Error("Log entry not found.");
382
413
  return {
383
414
  selected: selected,
384
- entry: visibleEntry(selected.store, args && args.ref, selected.status, selected.worktreeOnly),
415
+ entry: entry,
385
416
  };
386
417
  }
387
418
 
@@ -405,6 +436,7 @@ function attachProjectLogsService(ctx) {
405
436
  seenScopes.add(scopeKey);
406
437
  var entry = null;
407
438
  try { entry = visibleEntry(storeForStatus(status), ref, status, decision.worktreeOnly); } catch (e) { return; }
439
+ if (entry && entry.scheduledResult && (entry.scheduledResult.ownerId || null) !== (principal.userId || null)) return;
408
440
  if (!entry) return;
409
441
  var targetSlug = decision.worktreeOnly
410
442
  ? slug
@@ -425,11 +457,11 @@ function attachProjectLogsService(ctx) {
425
457
  author: null,
426
458
  listLogs: function (args) {
427
459
  var selected = target(args);
428
- return selected.store.list(scopedArgs(args, selected.status, selected.worktreeOnly));
460
+ return selected.store.list(Object.assign({}, scopedArgs(args, selected.status, selected.worktreeOnly), { scheduledResultOwnerId: selected.viewerId }));
429
461
  },
430
462
  searchLogs: function (args) {
431
463
  var selected = target(args);
432
- return selected.store.search(scopedArgs(args, selected.status, selected.worktreeOnly));
464
+ return selected.store.search(Object.assign({}, scopedArgs(args, selected.status, selected.worktreeOnly), { scheduledResultOwnerId: selected.viewerId }));
433
465
  },
434
466
  readLog: function (args) {
435
467
  return targetEntry(args).entry;
@@ -458,6 +490,7 @@ function attachProjectLogsService(ctx) {
458
490
  bindProjectSession: bindProjectSession,
459
491
  bindUser: bindUser,
460
492
  bindMate: bindMate,
493
+ recordScheduledResult: recordScheduledResult,
461
494
  setContextState: setContextState,
462
495
  };
463
496
  }
@@ -1,5 +1,4 @@
1
1
  // Project Logs projection over the Clay-wide knowledge record backend.
2
-
3
2
  var fs = require("fs");
4
3
  var path = require("path");
5
4
  var crypto = require("crypto");
@@ -12,6 +11,7 @@ var logsVersioning = require("./project-logs-versioning");
12
11
  var logsRoot = require("./project-logs-root");
13
12
  var logContext = require("./project-log-context");
14
13
  var contextState = require("./project-logs-context-state");
14
+ var scheduledResultStore = require("./project-scheduled-result-store");
15
15
 
16
16
  var MAX_SUMMARY_CHARS = logsSchema.MAX_SUMMARY_CHARS;
17
17
  var MAX_COMMENT_CHARS = 4000;
@@ -34,7 +34,6 @@ var normalizeTags = logsSchema.normalizeTags;
34
34
  var normalizeLinks = logsSchema.normalizeLinks;
35
35
 
36
36
  var page = logsQuery.page;
37
-
38
37
  // Filters accept any well-formed category. An unknown but valid one matches
39
38
  // nothing, which is a real answer. A supplied malformed one is an error:
40
39
  // silently dropping it would widen the result set to everything the caller is
@@ -45,7 +44,6 @@ function validateKind(kind, fallback) {
45
44
  return logsSchema.normalizeCategory(kind);
46
45
  }
47
46
 
48
-
49
47
  function createProjectLogsStore(opts) {
50
48
  var options = opts || {};
51
49
  var root = options.root ? path.resolve(options.root) : logsRoot.resolveProjectRoot(options.cwd, options.runGit);
@@ -116,6 +114,7 @@ function createProjectLogsStore(opts) {
116
114
  deletedAt: history.deleted ? history.deleted.at : null,
117
115
  deletedBy: history.deleted ? history.deleted.author : null,
118
116
  context: logContext.normalizeRecordContext(originRecord && originRecord.context),
117
+ scheduledResult: scheduledResultStore.normalizeMetadata(originRecord && originRecord.scheduledResult),
119
118
  };
120
119
  if (entry.context.changeSetId) {
121
120
  var states = knownContextStates || contexts.states();
@@ -132,13 +131,14 @@ function createProjectLogsStore(opts) {
132
131
  return entry;
133
132
  }
134
133
 
135
- function entries(includeDeleted) {
134
+ function entries(includeDeleted, scheduledResultOwnerId) {
136
135
  var out = [];
137
136
  var states = contexts.states();
138
137
  chains().forEach(function (chain, rootId) {
139
138
  var entry = fold(rootId, chain, states);
140
139
  if (!entry) return;
141
140
  if (entry.deleted && !includeDeleted) return;
141
+ if (!scheduledResultStore.visibleTo(entry.scheduledResult, scheduledResultOwnerId)) return;
142
142
  out.push(entry);
143
143
  });
144
144
  // Latest canonical update first, ref as a deterministic tie-break. Only a
@@ -157,12 +157,13 @@ function createProjectLogsStore(opts) {
157
157
  return found;
158
158
  }
159
159
 
160
- function read(ref, includeDeleted) {
160
+ function read(ref, includeDeleted, scheduledResultOwnerId) {
161
161
  var located = findChain(ref);
162
162
  if (!located) return null;
163
163
  var entry = fold(located.rootId, located.chain);
164
164
  if (!entry) return null;
165
165
  if (entry.deleted && !includeDeleted) return null;
166
+ if (!scheduledResultStore.visibleTo(entry.scheduledResult, scheduledResultOwnerId)) return null;
166
167
  return entry;
167
168
  }
168
169
 
@@ -212,8 +213,7 @@ function createProjectLogsStore(opts) {
212
213
  return read(refFor(id), false);
213
214
  }
214
215
 
215
- // Build the complete snapshot a revision will store, from the current state
216
- // plus the requested changes. Every new canonical edit is self-contained.
216
+ // Build the complete snapshot a revision will store from current state.
217
217
  function nextSnapshot(current, validated, data) {
218
218
  var snapshot = logsSnapshot.cloneSnapshot(current);
219
219
  if (validated.category !== undefined) snapshot.category = validated.category;
@@ -251,7 +251,6 @@ function createProjectLogsStore(opts) {
251
251
  }
252
252
 
253
253
  // One append per canonical revision, always carrying a complete snapshot.
254
- // Legacy field names ride along so an older build can still read the record.
255
254
  function appendRevision(rootId, op, snapshot, author, extra) {
256
255
  var record = {
257
256
  id: recordStore.newRecordId(),
@@ -275,9 +274,7 @@ function createProjectLogsStore(opts) {
275
274
  return record;
276
275
  }
277
276
 
278
- // Human participation. Append-only for this slice: no edit, no delete, and
279
- // never a canonical revision. The author is supplied by the authorization
280
- // layer, never by the caller's payload.
277
+ // Human participation is append-only and never a canonical revision.
281
278
  function comment(ref, input, author, context) {
282
279
  var located = requireLive(ref);
283
280
  var data = input || {};
@@ -344,7 +341,7 @@ function createProjectLogsStore(opts) {
344
341
  function categories(options) {
345
342
  var seen = {};
346
343
  var out = [];
347
- var live = entries(false);
344
+ var live = entries(false, options && options.scheduledResultOwnerId);
348
345
  for (var i = 0; i < live.length; i++) {
349
346
  if (!logsQuery.matchesContext(live[i], options || {})) continue;
350
347
  var value = live[i].category;
@@ -379,7 +376,7 @@ function createProjectLogsStore(opts) {
379
376
  var limit = Number(options2.limit);
380
377
  if (!Number.isFinite(limit)) limit = MAX_FEEDBACK_ITEMS;
381
378
  limit = Math.max(1, Math.min(MAX_FEEDBACK_ITEMS, Math.floor(limit)));
382
- var live = entries(false);
379
+ var live = entries(false, options2.scheduledResultOwnerId);
383
380
  var out = [];
384
381
  var total = 0;
385
382
  for (var i = 0; i < live.length; i++) {
@@ -409,13 +406,13 @@ function createProjectLogsStore(opts) {
409
406
 
410
407
  function list(args) {
411
408
  var options2 = args || {};
412
- var result = logsQuery.listEntries(entries(options2.includeDeleted === true), options2, cleanLine, validateKind);
409
+ var result = logsQuery.listEntries(entries(options2.includeDeleted === true, options2.scheduledResultOwnerId), options2, cleanLine, validateKind);
413
410
  result.categories = categories(options2);
414
411
  return result;
415
412
  }
416
413
 
417
414
  function search(args) {
418
- var result = logsQuery.searchEntries(entries(false), args || {}, cleanLine, validateKind);
415
+ var result = logsQuery.searchEntries(entries(false, args && args.scheduledResultOwnerId), args || {}, cleanLine, validateKind);
419
416
  result.categories = categories(args || {});
420
417
  return result;
421
418
  }
@@ -457,6 +454,8 @@ function createProjectLogsStore(opts) {
457
454
  };
458
455
  }
459
456
 
457
+ var scheduledResults = scheduledResultStore.attachScheduledResultStore({ store: store, scopeId: scopeId, refFor: refFor, read: read, entries: entries, findChain: findChain, matchesContext: logsQuery.matchesContext });
458
+
460
459
  return {
461
460
  root: root,
462
461
  scopeId: scopeId,
@@ -477,6 +476,9 @@ function createProjectLogsStore(opts) {
477
476
  search: search,
478
477
  history: history,
479
478
  stats: stats,
479
+ createScheduledResult: scheduledResults.create,
480
+ unreadScheduledResults: scheduledResults.unread,
481
+ markScheduledResultRead: scheduledResults.markRead,
480
482
  };
481
483
  }
482
484
 
@@ -72,6 +72,8 @@ var HANDLED_TYPES = [
72
72
  "project_log_read",
73
73
  "project_log_comment",
74
74
  "project_log_delete",
75
+ "scheduled_task_result_mark_read",
76
+ "scheduled_task_result_open_session",
75
77
  // Retired human mutations. Still claimed so an older client gets an
76
78
  // explicit refusal rather than silence.
77
79
  "project_log_create",
@@ -155,6 +157,31 @@ function attachProjectLogs(ctx) {
155
157
  return service.bindUser({ projectSlug: slug, user: (ws && ws._clayUser) || null });
156
158
  }
157
159
 
160
+ function scheduledResultState(bound, autoFocus) {
161
+ var state = bound.scheduledResultsState();
162
+ return { type: "scheduled_task_results_state", unreadCount: state.count, newestUnreadRef: state.newestRef, autoFocus: autoFocus === true };
163
+ }
164
+
165
+ function sendScheduledResultState(ws) {
166
+ if (!ws || ws._clayPane) return false;
167
+ var bound = userBinding(ws);
168
+ if (!bound) return false;
169
+ try { sendTo(ws, scheduledResultState(bound, true)); return true; } catch (e) { return false; }
170
+ }
171
+
172
+ function broadcastScheduledResultState(sourceWs) {
173
+ var sourceUserId = sourceWs && sourceWs._clayUser ? sourceWs._clayUser.id : null;
174
+ var clients = getClients();
175
+ for (var client of clients) {
176
+ if (!client || client.readyState !== 1 || client._clayPane) continue;
177
+ var clientUserId = client._clayUser ? client._clayUser.id : null;
178
+ if (clientUserId !== sourceUserId) continue;
179
+ var bound = userBinding(client);
180
+ if (!bound) continue;
181
+ try { sendTo(client, scheduledResultState(bound)); } catch (e) {}
182
+ }
183
+ }
184
+
158
185
  // --- WebSocket surface -----------------------------------------------
159
186
 
160
187
  function fail(ws, requestId, message) {
@@ -253,6 +280,27 @@ function attachProjectLogs(ctx) {
253
280
  sendTo(ws, { type: "project_log_commented", requestId: requestId, entry: commented, reviewQueued: reviewQueued });
254
281
  return true;
255
282
  }
283
+
284
+ if (msg.type === "scheduled_task_result_mark_read") {
285
+ bound.markScheduledResultRead({ ref: msg.ref });
286
+ broadcastScheduledResultState(ws);
287
+ return true;
288
+ }
289
+
290
+ if (msg.type === "scheduled_task_result_open_session") {
291
+ var resultEntry = bound.readLog({ ref: msg.ref });
292
+ var resultMeta = resultEntry && resultEntry.scheduledResult;
293
+ if (!resultMeta) throw new Error("Scheduled result not found.");
294
+ var target = null;
295
+ sm.sessions.forEach(function (session) {
296
+ if (target || !session || session.sessionOriginId !== resultMeta.driverOriginId) return;
297
+ var marker = session.scheduledTaskRun;
298
+ if ((session.ownerId || null) === (resultMeta.ownerId || null) && marker && marker.role === "driver" && marker.runId === resultMeta.runId && marker.scheduleId === resultMeta.scheduleId) target = session;
299
+ });
300
+ if (!target) throw new Error("This scheduled session is no longer available.");
301
+ sendTo(ws, { type: "scheduled_task_result_session", requestId: requestId, ref: resultEntry.ref, sessionId: target.localId });
302
+ return true;
303
+ }
256
304
  } catch (e) {
257
305
  fail(ws, requestId, (e && e.message) || "Project Logs could not complete the request.");
258
306
  return true;
@@ -378,6 +426,26 @@ function attachProjectLogs(ctx) {
378
426
  "\n" + logsMcp.REVIEW_CONTRACT + pendingFeedbackSignal(session);
379
427
  }
380
428
 
429
+ function recordScheduledResult(data) {
430
+ if (!service || isMate) return null;
431
+ var result = service.recordScheduledResult(Object.assign({}, data || {}, { projectSlug: slug }));
432
+ if (!result || !result.entry || !result.created) return result;
433
+ var clients = getClients();
434
+ for (var client of clients) {
435
+ if (!client || client.readyState !== 1 || client._clayPane) continue;
436
+ if (result.entry.scheduledResult.ownerId && (!client._clayUser || client._clayUser.id !== result.entry.scheduledResult.ownerId)) continue;
437
+ var bound = userBinding(client);
438
+ if (!bound) continue;
439
+ try { bound.readLog({ ref: result.entry.ref }); } catch (e) { continue; }
440
+ sendTo(client, {
441
+ type: "scheduled_task_result_created", ref: result.entry.ref,
442
+ title: result.entry.title, summary: result.entry.summary,
443
+ outcome: result.entry.scheduledResult.outcome,
444
+ });
445
+ }
446
+ return result;
447
+ }
448
+
381
449
  return {
382
450
  handleLogsMessage: handleLogsMessage,
383
451
  createMcpServer: createMcpServer,
@@ -386,6 +454,8 @@ function attachProjectLogs(ctx) {
386
454
  getBridgeTools: getBridgeTools,
387
455
  callBridgeTool: callBridgeTool,
388
456
  getSystemPrompt: getSystemPrompt,
457
+ recordScheduledResult: recordScheduledResult,
458
+ sendScheduledResultState: sendScheduledResultState,
389
459
  };
390
460
  }
391
461
 
@@ -373,6 +373,7 @@ function attachLoop(ctx) {
373
373
  if (durableSchedule) durableSchedule.sync();
374
374
  send({ type: "loop_registry_updated", records: getHubSchedules() });
375
375
  },
376
+ onRunTerminal: ctx.onScheduledRunTerminal,
376
377
  });
377
378
  loopRegistry.load();
378
379
  if (ctx.durableScheduler) {
@@ -67,7 +67,7 @@ var formatters = {
67
67
  type: "permission_request",
68
68
  title: data.title || "Permission requested",
69
69
  body: data.body || "",
70
- meta: { requestId: data.requestId, toolName: data.toolName, toolInput: data.toolInput || null },
70
+ meta: { requestId: data.requestId, toolName: data.toolName, toolInput: data.toolInput || null, defaultToNo: data.defaultToNo === true, suppressAlwaysAllowRule: data.suppressAlwaysAllowRule === true },
71
71
  };
72
72
  },
73
73
 
@@ -37,6 +37,7 @@ function createPendingMessageQueue(opts) {
37
37
  for (var i = 0; i < parsed.items.length; i++) {
38
38
  var item = parsed.items[i];
39
39
  if (!item || typeof item.id !== "string" || typeof item.sessionKey !== "string" || typeof item.projectSlug !== "string" || typeof item.actorId !== "string" || !item.message || ["pending", "claimed", "consumed", "cancelled", "failed"].indexOf(item.state) < 0) return false;
40
+ if (item.schedule !== undefined && (!item.schedule || typeof item.schedule !== "object" || typeof item.schedule.jobId !== "string" || typeof item.schedule.revision !== "string" || typeof item.schedule.ready !== "boolean" || !Number.isFinite(Number(item.schedule.notBefore)))) return false;
40
41
  }
41
42
  return true;
42
43
  }
@@ -113,30 +114,42 @@ function createPendingMessageQueue(opts) {
113
114
  for (var i = 0; i < data.items.length; i++) if (sameTarget(data.items[i], session) && (data.items[i].state === "pending" || data.items[i].state === "claimed")) return true;
114
115
  return false;
115
116
  }
116
- function admit(session, message, actor) {
117
+ function admitInternal(session, message, actor, trustedSchedule) {
117
118
  if (loadError) return result(false, loadError, session, actor);
118
119
  if (!session || !canMutate(session, actor, null)) return result(false, "Not authorized", session, actor);
119
120
  var validationError = validateMessage(message);
120
121
  if (validationError) return result(false, validationError, session, actor);
122
+ var clientId = message.clientMessageId ? String(message.clientMessageId) : null;
123
+ var duplicate = inspect(session, actor, clientId);
124
+ if (duplicate) {
125
+ var duplicateResult = result(duplicate.state !== "cancelled" && duplicate.state !== "failed", duplicate.state === "cancelled" ? "Message was cancelled" : duplicate.state === "failed" ? "Message delivery failed" : null, session, actor);
126
+ duplicateResult.item = duplicate;
127
+ return duplicateResult;
128
+ }
121
129
  var activeCount = 0;
122
130
  for (var activeIndex = 0; activeIndex < data.items.length; activeIndex++) {
123
131
  if (sameTarget(data.items[activeIndex], session) && (data.items[activeIndex].state === "pending" || data.items[activeIndex].state === "claimed")) activeCount++;
124
132
  }
125
133
  if (activeCount >= MAX_ACTIVE_ITEMS) return result(false, "Pending message queue is full", session, actor);
126
- var clientId = message.clientMessageId ? String(message.clientMessageId) : null;
127
- var duplicate = inspect(session, actor, clientId);
128
- if (duplicate) return result(duplicate.state !== "cancelled" && duplicate.state !== "failed", duplicate.state === "cancelled" ? "Message was cancelled" : duplicate.state === "failed" ? "Message delivery failed" : null, session, actor);
129
134
  var before = copy(data);
130
135
  var storedMessage = copy(message);
136
+ delete storedMessage.schedule;
131
137
  if (Object.prototype.hasOwnProperty.call(storedMessage, "images")) storedMessage.images = sanitizeImages(storedMessage.images);
132
138
  var item = { id: "pm_" + data.nextId++, sessionKey: key(session), sessionLocalId: session.localId, projectSlug: opts.slug, actorId: actorId(actor), actorName: actor && (actor.displayName || actor.username || actor.name) ? String(actor.displayName || actor.username || actor.name).slice(0, 120) : null, clientMessageId: clientId, message: storedMessage, createdAt: Date.now(), state: "pending" };
139
+ if (trustedSchedule) item.schedule = copy(trustedSchedule);
133
140
  delete item.message._pendingConsumed;
141
+ delete item.message._scheduledJobId;
142
+ delete item.message._scheduledRevision;
143
+ delete item.message._scheduledNotBefore;
134
144
  data.items.push(item);
135
145
  data.revision++;
136
146
  try { persist(); } catch (e) { restore(before); return result(false, "Queue storage failed", session, actor); }
137
147
  broadcast(session, { type: "pending_message_queued", sessionId: session.localId, projectSlug: opts.slug, paused: isPaused(session), item: copy(item), revision: data.revision });
138
- return result(true, null, session, actor);
148
+ var admitted = result(true, null, session, actor);
149
+ admitted.item = copy(item);
150
+ return admitted;
139
151
  }
152
+ function admit(session, message, actor) { return admitInternal(session, message, actor, null); }
140
153
  function mutate(session, actor, id, expectedRevision, expectedSessionId, operation, allowedStates) {
141
154
  if (loadError) return result(false, loadError, session, actor);
142
155
  if (!session || session.localId !== expectedSessionId || !authorize(session, actor)) return result(false, "Session changed", session, actor);
@@ -151,6 +164,7 @@ function createPendingMessageQueue(opts) {
151
164
  try { persist(); } catch (e) { restore(before); return result(false, "Queue storage failed", session, actor); }
152
165
  if (data.items[index].state === "cancelled" && session._pendingMessageClaimId === data.items[index].id) delete session._pendingMessageClaimId;
153
166
  broadcast(session, { type: "pending_message_state", sessionId: session.localId, projectSlug: opts.slug, paused: isPaused(session), revision: data.revision, items: visible(session, actor) });
167
+ if (data.items[index].state === "cancelled" && typeof opts.onItemCancelled === "function") opts.onItemCancelled(copy(data.items[index]));
154
168
  return result(true, null, session, actor);
155
169
  }
156
170
  function edit(session, actor, id, revision, expectedSessionId, message) {
@@ -166,8 +180,9 @@ function createPendingMessageQueue(opts) {
166
180
  var validationError = validateMessage(proposed);
167
181
  if (validationError) return result(false, validationError, session, actor);
168
182
  }
183
+ if (current && (!session || session.localId !== expectedSessionId || !authorize(session, actor) || revision !== data.revision || current.state !== "pending" || !canMutate(session, actor, current))) return result(false, "Pending message not found", session, actor);
169
184
  return mutate(session, actor, id, revision, expectedSessionId, function (items, index) {
170
- var nextMessage = Object.assign({}, items[index].message, { text: text });
185
+ var nextMessage = Object.assign({}, items[index].message, proposed || { text: text });
171
186
  if (message && Object.prototype.hasOwnProperty.call(message, "images")) nextMessage.images = sanitizeImages(message.images);
172
187
  items[index].message = nextMessage;
173
188
  });
@@ -229,6 +244,7 @@ function createPendingMessageQueue(opts) {
229
244
  try { persist(); } catch (e) { restore(before); return false; }
230
245
  if (session._pendingMessageClaimId === id) delete session._pendingMessageClaimId;
231
246
  broadcast(session, { type: ok === "release" ? "pending_message_state" : "pending_message_consumed", sessionId: session.localId, projectSlug: opts.slug, paused: isPaused(session), id: id, state: data.items[index].state, items: ok === "release" ? visible(session, { id: data.items[index].actorId }) : undefined, revision: data.revision });
247
+ if (ok === true && typeof opts.onItemConsumed === "function") opts.onItemConsumed(copy(data.items[index]));
232
248
  return true;
233
249
  }
234
250
  function consumeOne(session, dispatch) {
@@ -237,6 +253,7 @@ function createPendingMessageQueue(opts) {
237
253
  for (var i = 0; i < data.items.length; i++) {
238
254
  var item = data.items[i];
239
255
  if (!sameTarget(item, session) || item.state !== "pending") continue;
256
+ if (item.schedule && !item.schedule.ready) continue;
240
257
  if (!canMutate(session, { id: item.actorId }, item)) continue;
241
258
  var before = copy(data);
242
259
  item.state = "claimed";
@@ -258,7 +275,54 @@ function createPendingMessageQueue(opts) {
258
275
  for (var i = 0; i < data.items.length; i++) if (data.items[i].id === id && sameTarget(data.items[i], session) && data.items[i].state === "claimed") return true;
259
276
  return false;
260
277
  }
261
- return { admit: admit, edit: edit, cancel: cancel, reorder: reorder, pause: pause, resume: resume, consumeOne: consumeOne, hydrate: hydrate, inspect: inspect, hasActive: hasActive, isClaimed: isClaimed, isPaused: isPaused, list: visible, getRevision: function () { return data.revision; }, getLoadError: function () { return loadError; }, filePath: filePath };
278
+ function upsertScheduled(session, message, actor, trustedSchedule) {
279
+ message = copy(message);
280
+ var schedule = trustedSchedule || message.schedule;
281
+ delete message.schedule;
282
+ if (!schedule || typeof schedule.jobId !== "string" || typeof schedule.revision !== "string" || typeof schedule.ready !== "boolean" || !Number.isFinite(Number(schedule.notBefore))) return result(false, "Invalid scheduled queue metadata", session, actor);
283
+ var validationError = validateMessage(message);
284
+ if (validationError) return result(false, validationError, session, actor);
285
+ if (Object.prototype.hasOwnProperty.call(message, "images")) message.images = sanitizeImages(message.images);
286
+ delete message._pendingConsumed;
287
+ delete message._scheduledJobId;
288
+ delete message._scheduledRevision;
289
+ delete message._scheduledNotBefore;
290
+ for (var i = 0; i < data.items.length; i++) {
291
+ if (sameTarget(data.items[i], session) && data.items[i].schedule && data.items[i].schedule.jobId === schedule.jobId && data.items[i].state !== "pending") return result(false, "Scheduled queue item is terminal", session, actor);
292
+ if (sameTarget(data.items[i], session) && data.items[i].schedule && data.items[i].schedule.jobId === schedule.jobId && data.items[i].state === "pending") {
293
+ if (!authorize(session, actor) || !canMutate(session, actor, data.items[i])) return result(false, "Not authorized", session, actor);
294
+ var before = copy(data);
295
+ data.items[i].message = copy(message);
296
+ data.items[i].schedule = copy(schedule);
297
+ data.revision++;
298
+ try { persist(); } catch (error) { restore(before); return result(false, "Queue storage failed", session, actor); }
299
+ broadcast(session, { type: "pending_message_state", sessionId: session.localId, projectSlug: opts.slug, paused: isPaused(session), revision: data.revision, items: visible(session, actor) });
300
+ return { ok: true, item: copy(data.items[i]), revision: data.revision };
301
+ }
302
+ }
303
+ return admitInternal(session, message, actor, schedule);
304
+ }
305
+ function cancelScheduled(session, jobId, actor) {
306
+ for (var i = 0; i < data.items.length; i++) {
307
+ if (sameTarget(data.items[i], session) && data.items[i].schedule && data.items[i].schedule.jobId === jobId && (data.items[i].state === "pending" || data.items[i].state === "claimed")) return cancel(session, actor, data.items[i].id, data.revision, session.localId);
308
+ }
309
+ return false;
310
+ }
311
+ function releaseScheduled(session, jobId, revision) {
312
+ for (var i = 0; i < data.items.length; i++) {
313
+ var item = data.items[i];
314
+ if (!sameTarget(item, session) || !item.schedule || item.schedule.jobId !== jobId || item.schedule.revision !== revision) continue;
315
+ if (item.state !== "pending" || item.schedule.ready) return item.state === "pending" ? copy(item) : null;
316
+ var before = copy(data);
317
+ item.schedule.ready = true;
318
+ data.revision++;
319
+ try { persist(); } catch (error) { restore(before); return null; }
320
+ broadcast(session, { type: "pending_message_state", sessionId: session.localId, projectSlug: opts.slug, paused: isPaused(session), revision: data.revision, items: visible(session, { id: item.actorId }) });
321
+ return copy(item);
322
+ }
323
+ return null;
324
+ }
325
+ return { admit: admit, upsertScheduled: upsertScheduled, releaseScheduled: releaseScheduled, cancelScheduled: cancelScheduled, edit: edit, cancel: cancel, reorder: reorder, pause: pause, resume: resume, consumeOne: consumeOne, hydrate: hydrate, inspect: inspect, hasActive: hasActive, isClaimed: isClaimed, isPaused: isPaused, list: visible, getRevision: function () { return data.revision; }, getLoadError: function () { return loadError; }, filePath: filePath };
262
326
  }
263
327
 
264
328
  module.exports = { createPendingMessageQueue: createPendingMessageQueue };