@open-mercato/cezar 0.20.0-pr2175.5218 → 0.20.0-pr2176.5215

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.
@@ -170,7 +170,7 @@ export declare class ManagerEngine {
170
170
  /** A project's whole run list, at attach and at boot: what happened while nobody listened. */
171
171
  reconcileProject(projectId: string, dataDir: string, runs: RunRecord[]): void;
172
172
  private reconcileFollows;
173
- /** A dispatched child reaches its root's followers only when it needs the user. */
173
+ /** A dispatched child reaches its root's followers only when it needs the user; a follow of the child itself hears all of it. */
174
174
  private deliverFollowed;
175
175
  /** The card at once; then one follow-up turn per burst of events, unless the manager turned them off. */
176
176
  private postTaskEvent;
@@ -30,6 +30,11 @@ const PENDING_FOLLOWUP_LIMIT = 20;
30
30
  const FOLLOWUP_EXCERPT_LIMIT = 4_000;
31
31
  const FINAL_MESSAGE_MARK = '\nIts final message:\n';
32
32
  const followupHeadline = (entry) => entry.split(FINAL_MESSAGE_MARK)[0] ?? entry;
33
+ /** At most this many events owed a follow-up turn per followed task per window; past it, cards and notes only. */
34
+ const FOLLOWUP_TURNS_PER_TASK = 5;
35
+ const FOLLOWUP_TURN_WINDOW_MS = 60 * 60_000;
36
+ /** A follow-up another cockpit's long turn kept waiting tries again this much later. */
37
+ const FOLLOWUP_LEASE_RETRY_MS = 60_000;
33
38
  const TASK_STATE_LINE = {
34
39
  done: 'Finished',
35
40
  review: 'Finished, ready for review',
@@ -38,6 +43,11 @@ const TASK_STATE_LINE = {
38
43
  cancelled: 'Cancelled',
39
44
  };
40
45
  const firstLine = (text, limit) => text?.split('\n').find((line) => line.trim())?.trim().slice(0, limit);
46
+ /** By event identity, never text: the same task waiting again mid-turn reads the same and is still news. */
47
+ const withoutHeard = (pending, heard) => {
48
+ const ledgers = new Set(heard.map((entry) => entry.ledger));
49
+ return pending.filter((entry) => !ledgers.has(entry.ledger));
50
+ };
41
51
  const followKey = (projectId, runId) => `${projectId}/${runId}`;
42
52
  const connectedTo = (manager, projectId) => manager.projects === 'all' || manager.projects.includes(projectId);
43
53
  const STREAM_THROTTLE_MS = 100;
@@ -723,9 +733,15 @@ export class ManagerEngine {
723
733
  return;
724
734
  if (runtime.retries >= LEASE_RETRY_LIMIT) {
725
735
  const sent = runtime.queue.some((item) => item.kind === 'chat');
736
+ const asked = sent || runtime.queue.some((item) => item.kind === 'watch');
737
+ const followup = runtime.queue.some((item) => item.kind === 'followup');
726
738
  runtime.queue = [];
727
739
  runtime.retries = 0;
728
- this.addNotice(id, `Another cezar on this machine kept this manager busy. ${sent ? 'Send that again.' : 'Check again later.'}`);
740
+ // The user asked for nothing here: its events wait in state, and it tries again later.
741
+ if (followup)
742
+ this.scheduleFollowup(id, FOLLOWUP_LEASE_RETRY_MS);
743
+ if (asked)
744
+ this.addNotice(id, `Another cezar on this machine kept this manager busy. ${sent ? 'Send that again.' : 'Check again later.'}`);
729
745
  return;
730
746
  }
731
747
  runtime.retries += 1;
@@ -818,7 +834,7 @@ export class ManagerEngine {
818
834
  // Only the session the user talks to needs them, and only a turn that landed has heard them.
819
835
  // Task events still owed a follow-up ride along: the user asking first answers them too.
820
836
  const { notes, followups } = store.state(id);
821
- const heard = (await this.visibleTurn(id, manager, chatPrompt(item.text, notes, item.paths, item.messageIds, followups), 'chat', turn, undefined, item)) === 'heard';
837
+ const heard = (await this.visibleTurn(id, manager, chatPrompt(item.text, notes, item.paths, item.messageIds, followups.map((entry) => entry.line)), 'chat', turn, undefined, item)) === 'heard';
822
838
  if (heard)
823
839
  this.consumeNotes(id, notes, followups);
824
840
  return;
@@ -828,7 +844,7 @@ export class ManagerEngine {
828
844
  // Nothing left (a chat took them up), or a usage limit holds: `tick` brings it back after.
829
845
  if (followups.length === 0 || (resume && msOf(resume.at) > this.now()))
830
846
  return;
831
- const outcome = await this.visibleTurn(id, manager, followupPrompt(followups, notes), 'chat', turn);
847
+ const outcome = await this.visibleTurn(id, manager, followupPrompt(followups.map((entry) => entry.line), notes), 'chat', turn);
832
848
  if (outcome === 'heard')
833
849
  this.consumeNotes(id, notes, followups);
834
850
  // Failed: no retry that could bill in a loop; the next chat hears them instead.
@@ -836,8 +852,8 @@ export class ManagerEngine {
836
852
  store.updateState(id, (state) => ({
837
853
  ...state,
838
854
  // Only the status line: notes reach every later prompt unfenced, the task's words must not.
839
- notes: [...state.notes, ...followups.map(followupHeadline)].slice(-20),
840
- followups: state.followups.filter((line) => !followups.includes(line)),
855
+ notes: [...state.notes, ...followups.map((entry) => followupHeadline(entry.line))].slice(-20),
856
+ followups: withoutHeard(state.followups, followups),
841
857
  }));
842
858
  }
843
859
  return;
@@ -867,7 +883,7 @@ export class ManagerEngine {
867
883
  this.deps.store.updateState(id, (state) => ({
868
884
  ...state,
869
885
  notes: state.notes.slice(notes.length),
870
- followups: state.followups.filter((line) => !followups.includes(line)),
886
+ followups: withoutHeard(state.followups, followups),
871
887
  }));
872
888
  }
873
889
  addNote(id, note) {
@@ -1047,7 +1063,8 @@ export class ManagerEngine {
1047
1063
  const key = followKey(projectId, runId);
1048
1064
  const kept = Object.entries(state.follows).filter(([other]) => other !== key)
1049
1065
  .sort(([, a], [, b]) => msOf(a.at) - msOf(b.at)).slice(-(FOLLOW_LIMIT - 1));
1050
- return { ...state, follows: Object.fromEntries([...kept, [key, { at: state.follows[key]?.at ?? at, ...(created ? { created } : {}) }]]) };
1066
+ // Re-following keeps the entry whole: its `turns` bound must not reset.
1067
+ return { ...state, follows: Object.fromEntries([...kept, [key, { ...state.follows[key], at: state.follows[key]?.at ?? at, ...(created ? { created } : {}) }]]) };
1051
1068
  });
1052
1069
  // A task that settled before the follow was written passed the live listener unheard.
1053
1070
  void this.catchUp(id, projectId, runId).catch(() => undefined);
@@ -1080,7 +1097,7 @@ export class ManagerEngine {
1080
1097
  this.reconcileProject(source.id, join(source.root, '.ai', 'cezar'), source.runs());
1081
1098
  }
1082
1099
  }
1083
- /** A dispatched child reaches its root's followers only when it needs the user. */
1100
+ /** A dispatched child reaches its root's followers only when it needs the user; a follow of the child itself hears all of it. */
1084
1101
  deliverFollowed(projectId, dataDir, runs, getRun, only) {
1085
1102
  if (this.disposed)
1086
1103
  return;
@@ -1097,14 +1114,15 @@ export class ManagerEngine {
1097
1114
  if (!event)
1098
1115
  continue;
1099
1116
  const rootId = run.dispatch?.parentRunId ? run.dispatch.rootRunId : run.id;
1100
- if (rootId !== run.id && event.state !== 'needs-you')
1101
- continue;
1102
1117
  for (const { manager, follows } of followers) {
1103
- const follow = follows[followKey(projectId, rootId)];
1104
- if (!follow || msOf(event.at) < msOf(follow.at))
1118
+ const key = follows[followKey(projectId, run.id)]
1119
+ ? followKey(projectId, run.id)
1120
+ : rootId !== run.id && event.state === 'needs-you' ? followKey(projectId, rootId) : undefined;
1121
+ const follow = key ? follows[key] : undefined;
1122
+ if (!key || !follow || msOf(event.at) < msOf(follow.at))
1105
1123
  continue;
1106
1124
  try {
1107
- this.postTaskEvent(manager, projectId, dataDir, run, event, rootId !== run.id ? getRun(rootId) ?? { id: rootId } : undefined);
1125
+ this.postTaskEvent(manager, projectId, dataDir, run, event, key, rootId !== run.id ? getRun(rootId) ?? { id: rootId } : undefined);
1108
1126
  }
1109
1127
  catch (error) {
1110
1128
  console.warn(`[cezar] manager ${manager.id} task card: ${error instanceof Error ? error.message : String(error)}`);
@@ -1113,7 +1131,7 @@ export class ManagerEngine {
1113
1131
  }
1114
1132
  }
1115
1133
  /** The card at once; then one follow-up turn per burst of events, unless the manager turned them off. */
1116
- postTaskEvent(manager, projectId, dataDir, run, event, root) {
1134
+ postTaskEvent(manager, projectId, dataDir, run, event, follow, root) {
1117
1135
  const { store } = this.deps;
1118
1136
  const id = manager.id;
1119
1137
  const messageId = `task-report:${event.ledger}`;
@@ -1147,12 +1165,23 @@ export class ManagerEngine {
1147
1165
  this.addNote(id, `Task update, posted to the chat by cezar: ${line}`);
1148
1166
  return true;
1149
1167
  }
1150
- store.updateState(id, (current) => ({ ...current, followups: [...current.followups, followup].slice(-PENDING_FOLLOWUP_LIMIT) }));
1168
+ // A manager and a task answering each other is one turn per round: past the bound, the user decides.
1169
+ const now = this.now();
1170
+ const recent = (store.state(id).follows[follow]?.turns ?? []).filter((at) => now - msOf(at) < FOLLOWUP_TURN_WINDOW_MS);
1171
+ if (recent.length >= FOLLOWUP_TURNS_PER_TASK) {
1172
+ this.addNote(id, `Task update, posted to the chat by cezar (no follow-up turn: this task already had ${FOLLOWUP_TURNS_PER_TASK} follow-ups within the hour): ${line}`);
1173
+ return true;
1174
+ }
1175
+ store.updateState(id, (current) => ({
1176
+ ...current,
1177
+ follows: current.follows[follow] ? { ...current.follows, [follow]: { ...current.follows[follow], turns: [...recent, this.iso()] } } : current.follows,
1178
+ followups: [...current.followups.filter((entry) => entry.ledger !== event.ledger), { ledger: event.ledger, line: followup }].slice(-PENDING_FOLLOWUP_LIMIT),
1179
+ }));
1151
1180
  this.scheduleFollowup(id);
1152
1181
  return true;
1153
1182
  }
1154
1183
  /** Coalesced: a burst of events, or a tree settling, is one turn. The queue keeps it behind a chat. */
1155
- scheduleFollowup(id) {
1184
+ scheduleFollowup(id, delay = FOLLOWUP_COALESCE_MS) {
1156
1185
  if (this.disposed || this.followupTimers.has(id))
1157
1186
  return;
1158
1187
  const timer = setTimeout(() => {
@@ -1161,7 +1190,7 @@ export class ManagerEngine {
1161
1190
  if (runtime.deleted || !this.deps.store.get(id) || runtime.queue.some((item) => item.kind === 'followup'))
1162
1191
  return;
1163
1192
  this.enqueue(id, { kind: 'followup' });
1164
- }, FOLLOWUP_COALESCE_MS);
1193
+ }, delay);
1165
1194
  timer.unref?.();
1166
1195
  this.followupTimers.set(id, timer);
1167
1196
  }