File size: 24,715 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
import { join } from 'node:path';

import {
  IAgentGoalService,
  IAgentLifecycleService,
  IAgentLoopService,
  IAgentPermissionModeService,
  IAgentProfileService,
  IAgentScopeContext,
  IAgentStateService,
  IAgentTaskService,
  IAgentTodoService,
  IEventBus,
  INTERACTION_TAG_AGENT_ID,
  INTERACTION_TAG_SESSION_ID,
  ISessionActivityView,
  ISessionIndex,
  IWireService,
  MAIN_AGENT_ID,
  interactions,
  toDisposable,
  type AgentTaskInfo,
  type IAgentScopeHandle,
  type IDisposable,
  type Interaction,
  type ISessionScopeHandle,
  type Scope,
} from '@moonshot-ai/agent-core-v2';
import { planKey } from '@moonshot-ai/agent-core-v2/features/plan/planOps';
import { swarmKey } from '@moonshot-ai/agent-core-v2/features/swarm/swarmOps';

import { serverMessageSchema, type ServerMessage } from '../../protocol/messages';
import { readLegacyStatus } from '../legacyStatus/legacyStatus';
import { AgentStateTracker } from './agentState';
import { AgentMessageProjector, toTurnOrigin, type ProjectorInteraction } from './agentProjector';
import type { ProjectionBusEvent } from './events';
import { foldTimelineSeed, foldWireTurn, readWireRecords, type ContextRecord } from './heal';
import { isUndoAnchorOrigin } from './ids';
import { SessionStateAggregator } from './sessionState';

const TURN_HEAL_DEBOUNCE_MS = 250;
const TASK_OUTPUT_TAIL_CHARS = 4096;

export interface ProjectionLogger {
  warn(obj: unknown, msg: string): void;
}

export interface SessionProjectionDeps {
  readonly homeDir: string;
  readonly core: Scope;
  readonly logger?: ProjectionLogger;
}

export class SessionProjection {
  private readonly projectors = new Map<string, AgentMessageProjector>();
  private readonly agentDisposables = new Map<string, IDisposable[]>();
  private readonly disposables: IDisposable[] = [];
  private readonly listeners = new Set<(message: ServerMessage) => void>();
  private readonly aggregator = new SessionStateAggregator();
  private readonly agentStates = new Map<string, AgentStateTracker>();
  private readonly subagentTaskIds = new Map<string, string>();
  private readonly interactionAgents = new Map<string, string>();
  private readonly knownInteractions = new Set<string>();
  private readonly unknownEventTypes = new Set<string>();
  private readonly validationFailures = new Map<string, number>();
  private readonly healTimers = new Map<string, { ordinals: Set<number>; timer: NodeJS.Timeout }>();
  private disposed = false;

  constructor(
    readonly sessionId: string,
    private readonly session: ISessionScopeHandle,
    private readonly deps: SessionProjectionDeps,
  ) {
    const agents = session.accessor.get(IAgentLifecycleService);
    for (const context of agents.list()) {
      const handle = agents.handleOf(context.agentId);
      if (handle !== undefined) this.subscribeAgent(handle);
    }
    this.disposables.push(
      agents.onDidCreate(
        this.guard((context) => {
          const handle = agents.handleOf(context.agentId);
          if (handle !== undefined) this.subscribeAgent(handle);
        }),
      ),
      agents.onDidClose(
        this.guard((context) => {
          this.dropAgent(context.agentId);
        }),
      ),
      toDisposable(
        interactions.onDidChangePending(
          this.guard(() => {
            this.onInteractionsChanged();
          }),
        ),
      ),
      toDisposable(
        interactions.onDidResolve(
          this.guard(({ id, response }) => {
            this.onInteractionResolve(id, response);
          }),
        ),
      ),
    );
    for (const pending of this.pendingInteractions()) {
      this.announce(pending, false);
    }
    const activity = session.accessor.get(ISessionActivityView) as
      | ISessionActivityView
      | undefined;
    if (activity !== undefined) {
      this.aggregator.feedSessionActivity(activity.state());
      this.disposables.push(
        activity.onDidChange(
          this.guard((event) => {
            this.aggregator.feedSessionActivity(event.state);
            this.emitState();
          }),
        ),
      );
    }
  }

  onMessage(listener: (message: ServerMessage) => void): IDisposable {
    this.listeners.add(listener);
    return {
      dispose: () => {
        this.listeners.delete(listener);
      },
    };
  }

  recoveryMessages(): ServerMessage[] {
    const messages: ServerMessage[] = [this.aggregator.snapshot(this.sessionId)];
    for (const tracker of this.agentStates.values()) {
      const state = tracker.snapshot(this.sessionId);
      if (state !== undefined) messages.push(state);
    }
    for (const projector of this.projectors.values()) {
      messages.push(...projector.recoveryMessages());
    }
    return messages.filter((message) => this.validate(message) !== undefined);
  }

  notifyContextCleared(agentId: string): void {
    const projector = this.projectors.get(agentId);
    if (projector === undefined) return;
    this.emitAll(projector.notifyContextCleared());
  }

  inFlight(agentId: string): { turn_id: string; step_id: string } | undefined {
    return this.projectors.get(agentId)?.inFlight();
  }

  dispose(): void {
    if (this.disposed) return;
    this.disposed = true;
    for (const pending of this.healTimers.values()) clearTimeout(pending.timer);
    this.healTimers.clear();
    for (const list of this.agentDisposables.values()) {
      for (const d of list) d.dispose();
    }
    this.agentDisposables.clear();
    for (const d of this.disposables) d.dispose();
    for (const projector of this.projectors.values()) projector.dispose();
    this.projectors.clear();
    this.agentStates.clear();
    this.listeners.clear();
    this.interactionAgents.clear();
    this.knownInteractions.clear();
  }

  private subscribeAgent(handle: IAgentScopeHandle): void {
    const agentId = handle.id;
    if (this.projectors.has(agentId)) return;
    const loop = handle.accessor.get(IAgentLoopService) as IAgentLoopService | undefined;
    this.trackAgentState(handle, loop);
    const projector = new AgentMessageProjector(
      agentId,
      this.sessionId,
      this.subagentTaskIds,
      {
        stepOrdinal: (turnId) => {
          const turn = loop?.snapshot().turn;
          return turn === undefined || `t${turn.turnId}` !== turnId ? undefined : turn.step;
        },
        resolvePlanRevisionKey: (key) =>
          handle.accessor.get(IAgentScopeContext).scope(key),
      },
      {
        onUnknownEvent: (type) => {
          if (this.unknownEventTypes.has(type)) return;
          this.unknownEventTypes.add(type);
          this.deps.logger?.warn(
            { sessionId: this.sessionId, agentId, type },
            'projection: unhandled engine event type, dropped',
          );
        },
        onDeferred: (messages) => {
          if (!this.disposed) this.emitAll(messages);
        },
      },
    );
    this.projectors.set(agentId, projector);
    const agentState = handle.accessor.get(IAgentStateService) as IAgentStateService | undefined;
    const planMode =
      agentState?.has(planKey) === true ? agentState.get(planKey).active : undefined;
    const swarmMode =
      agentState?.has(swarmKey) === true ? agentState.get(swarmKey) !== null : undefined;
    projector.seedModes({ planMode, swarmMode });
    const disposables: IDisposable[] = [];
    const bus = handle.accessor.get(IEventBus) as IEventBus | undefined;
    if (bus !== undefined) {
      disposables.push(
        bus.subscribe(
          this.guard((event) => {
            this.onBusEvent(agentId, event as ProjectionBusEvent);
          }),
        ),
      );
    }
    const todo = handle.accessor.get(IAgentTodoService) as IAgentTodoService | undefined;
    if (todo !== undefined) {
      projector.seedTodo(todo.get());
      disposables.push(
        todo.onDidChange(
          this.guard((items) => {
            this.emitAll(projector.todoChanged(items));
          }),
        ),
      );
    }
    const tasks = handle.accessor.get(IAgentTaskService) as IAgentTaskService | undefined;
    for (const info of tasks?.list() ?? []) projector.seedTask(info);
    const status = loop?.snapshot();
    if (status?.state === 'running' && status.activeTurnId !== undefined) {
      const prompts = handle.accessor.get(IAgentLoopService) as IAgentLoopService | undefined;
      const active =
        status.activePromptId === undefined ? undefined : prompts?.promptHandle(status.activePromptId);
      const rawOrigin = active?.message.origin;
      projector.seedActiveTurn({
        turnId: status.activeTurnId,
        promptId: active?.id,
        userMessageId: active?.userMessageId,
        origin:
          rawOrigin === undefined
            ? undefined
            : toTurnOrigin(rawOrigin, agentId, this.subagentTaskIds),
        anchor: isUndoAnchorOrigin(rawOrigin),
      });
    }
    if (agentId === MAIN_AGENT_ID) {
      this.seedMainAgent(handle, disposables, { planMode, swarmMode });
    }
    this.agentDisposables.set(agentId, disposables);
    void this.seedTimelineFromWire(agentId, projector);
  }

  private seedMainAgent(
    handle: IAgentScopeHandle,
    disposables: IDisposable[],
    modes: { planMode?: boolean; swarmMode?: boolean },
  ): void {
    this.aggregator.feedMainStatus({ planMode: modes.planMode, swarmMode: modes.swarmMode });
    const legacy = readLegacyStatus(handle);
    if (legacy !== undefined) {
      this.aggregator.feedSeed({
        model: legacy.model.length > 0 ? legacy.model : undefined,
        usage: legacy.usage,
        contextTokens: legacy.contextTokens,
        maxContextTokens: legacy.maxContextTokens,
      });
    }
    const profile = handle.accessor.get(IAgentProfileService) as IAgentProfileService | undefined;
    if (profile !== undefined) {
      this.aggregator.feedSeed({ thinkingEffort: profile.getEffectiveThinkingLevel() });
    }
    const permission = handle.accessor.get(IAgentPermissionModeService) as
      | IAgentPermissionModeService
      | undefined;
    if (permission !== undefined) {
      this.aggregator.feedSeed({ permission: permission.mode });
      disposables.push(
        permission.onDidChangeMode(
          this.guard(({ mode }) => {
            this.aggregator.feedSeed({ permission: mode });
            this.emitState();
          }),
        ),
      );
    }
    const goal = handle.accessor.get(IAgentGoalService) as IAgentGoalService | undefined;
    if (goal !== undefined) {
      this.aggregator.feedGoal(goal.getGoal().goal);
    }
  }

  private trackAgentState(handle: IAgentScopeHandle, loop: IAgentLoopService | undefined): void {
    const agentId = handle.id;
    if (this.agentStates.has(agentId)) return;
    const tracker = new AgentStateTracker(agentId, new Date().toISOString());
    this.agentStates.set(agentId, tracker);
    const running = loop?.snapshot().state === 'running';
    const createdAt = new Date().toISOString();
    if (agentId === MAIN_AGENT_ID) {
      const profile = handle.accessor.get(IAgentProfileService) as IAgentProfileService | undefined;
      tracker.seedMain(profile?.data().profileName ?? '', createdAt, running);
    } else {
      const scopeContext = handle.accessor.get(IAgentScopeContext) as
        | { forkedFrom?: string }
        | undefined;
      if (scopeContext?.forkedFrom !== undefined && scopeContext.forkedFrom.length > 0) {
        const profile = handle.accessor.get(IAgentProfileService) as
          | IAgentProfileService
          | undefined;
        tracker.seedBtw(profile?.data().profileName ?? '', createdAt, running);
      } else {
        const mainHandle = this.agentHandle(MAIN_AGENT_ID);
        const tasks = mainHandle?.accessor.get(IAgentTaskService) as IAgentTaskService | undefined;
        const link = tasks
          ?.list(false)
          .find(
            (info) =>
              info.kind === 'agent' && (info as { agentId?: string }).agentId === agentId,
          );
        const profile = handle.accessor.get(IAgentProfileService) as
          | IAgentProfileService
          | undefined;
        tracker.seedToolFromTask(profile?.data().profileName ?? '', createdAt, link);
      }
    }
    this.emitAgentState(agentId);
  }

  private emitAgentState(agentId: string): void {
    const message = this.agentStates.get(agentId)?.snapshot(this.sessionId);
    if (message !== undefined) this.emit(message);
  }

  private dropAgent(agentId: string): void {
    for (const d of this.agentDisposables.get(agentId) ?? []) d.dispose();
    this.agentDisposables.delete(agentId);
    this.projectors.get(agentId)?.dispose();
    this.projectors.delete(agentId);
    const timer = this.healTimers.get(agentId);
    if (timer !== undefined) {
      clearTimeout(timer.timer);
      this.healTimers.delete(agentId);
    }
    const tracker = this.agentStates.get(agentId);
    if (tracker !== undefined) {
      if (tracker.close(new Date().toISOString())) this.emitAgentState(agentId);
      this.agentStates.delete(agentId);
    }
  }

  private onBusEvent(agentId: string, event: ProjectionBusEvent): void {
    if (this.disposed) return;
    const projector = this.projectors.get(agentId);
    if (projector === undefined) return;
    this.emitAll(projector.map(event));
    this.onAgentStateEvent(agentId, event);
    if (event.type === 'task.terminated') {
      const info = (event as { info?: AgentTaskInfo }).info;
      if (info !== undefined) void this.patchTaskOutputTail(agentId, info.taskId);
    }
    for (const ordinal of projector.takeEndedTurnOrdinals()) {
      this.scheduleHeal(agentId, ordinal);
    }
    if (agentId === MAIN_AGENT_ID) {
      if (event.type === 'agent.status.updated') {
        this.aggregator.feedMainStatus(event);
      } else if (event.type === 'goal.updated') {
        this.aggregator.feedGoal(event.snapshot);
      } else if (event.type === 'plan.revision') {
        const handle = this.agentHandle(MAIN_AGENT_ID);
        const path = handle?.accessor.get(IAgentScopeContext).scope(event.key) ?? event.key;
        this.aggregator.feedPlanRevision(path, event.version);
      }
    }
    this.emitState();
  }

  private onAgentStateEvent(agentId: string, event: ProjectionBusEvent): void {
    switch (event.type) {
      case 'subagent.spawned': {
        const spawned = event as {
          subagentId: string;
          subagentName: string;
          parentToolCallId: string;
          parentAgentId?: string;
          swarmIndex?: number;
        };
        const tracker = this.agentStates.get(spawned.subagentId);
        if (tracker?.seedToolSpawned(spawned) === true) this.emitAgentState(spawned.subagentId);
        return;
      }
      case 'subagent.started': {
        const tracker = this.agentStates.get((event as { subagentId: string }).subagentId);
        if (tracker?.runStarted() === true) this.emitAgentState(tracker.agentId);
        return;
      }
      case 'subagent.completed': {
        const tracker = this.agentStates.get((event as { subagentId: string }).subagentId);
        if (
          tracker?.runFinished(
            'completed',
            new Date((event as { time?: number }).time ?? Date.now()).toISOString(),
          ) === true
        ) {
          this.emitAgentState(tracker.agentId);
        }
        return;
      }
      case 'subagent.failed': {
        const tracker = this.agentStates.get((event as { subagentId: string }).subagentId);
        if (
          tracker?.runFinished(
            'failed',
            new Date((event as { time?: number }).time ?? Date.now()).toISOString(),
          ) === true
        ) {
          this.emitAgentState(tracker.agentId);
        }
        return;
      }
      case 'subagent.cancelled': {
        const tracker = this.agentStates.get((event as { subagentId: string }).subagentId);
        if (
          tracker?.runFinished(
            'interrupted',
            new Date((event as { time?: number }).time ?? Date.now()).toISOString(),
          ) === true
        ) {
          this.emitAgentState(tracker.agentId);
        }
        return;
      }
      case 'turn.started': {
        const tracker = this.agentStates.get(agentId);
        if (tracker === undefined) return;
        if (tracker.turnStarted()) this.emitAgentState(agentId);
        this.recomputeAgentTurn(agentId);
        return;
      }
      case 'turn.ended': {
        const tracker = this.agentStates.get(agentId);
        if (tracker === undefined) return;
        if (tracker.turnEnded()) this.emitAgentState(agentId);
        this.recomputeAgentTurn(agentId);
        return;
      }
      case 'turn.step.started':
      case 'tool.call.started':
      case 'tool.result':
      case 'turn.step.interrupted':
        this.recomputeAgentTurn(agentId);
        return;
      case 'turn.step.retrying':
        queueMicrotask(() => {
          if (!this.disposed) this.recomputeAgentTurn(agentId);
        });
        return;
      default:
        return;
    }
  }

  private recomputeAgentTurn(agentId: string): void {
    const tracker = this.agentStates.get(agentId);
    if (tracker === undefined) return;
    const loop = this.agentHandle(agentId)?.accessor.get(IAgentLoopService) as
      | IAgentLoopService
      | undefined;
    if (loop === undefined) return;
    const snapshot = loop.snapshot();
    if (tracker.recompute(snapshot)) this.emitAgentState(agentId);
  }

  private agentHandle(agentId: string): IAgentScopeHandle | undefined {
    return this.session.accessor.get(IAgentLifecycleService).handleOf(agentId);
  }

  private guard<A extends unknown[]>(fn: (...args: A) => void): (...args: A) => void {
    return (...args: A) => {
      try {
        fn(...args);
      } catch (error) {
        this.deps.logger?.warn(
          {
            sessionId: this.sessionId,
            err: error instanceof Error ? error.message : String(error),
          },
          'projection: event callback failed, continuing',
        );
      }
    };
  }

  private pendingInteractions(): readonly Interaction[] {
    return interactions.findAll({
      resolved: false,
      tags: { [INTERACTION_TAG_SESSION_ID]: this.sessionId },
    });
  }

  private onInteractionsChanged(): void {
    for (const pending of this.pendingInteractions()) {
      if (this.knownInteractions.has(pending.id)) continue;
      this.announce(pending, true);
    }
  }

  private announce(interaction: Interaction, emit: boolean): void {
    if (interaction.kind !== 'approval' && interaction.kind !== 'question') return;
    this.knownInteractions.add(interaction.id);
    const agentId = interactionAgentId(interaction);
    this.interactionAgents.set(interaction.id, agentId);
    const projector = this.projectorFor(agentId);
    if (projector === undefined) return;
    const request: ProjectorInteraction = {
      id: interaction.id,
      kind: interaction.kind,
      payload: interaction.payload,
      createdAt: interaction.createdAt,
    };
    const ops = projector.interactionRequested(request);
    if (emit) this.emitAll(ops);
  }

  private onInteractionResolve(id: string, response: unknown): void {
    this.knownInteractions.delete(id);
    const agentId = this.interactionAgents.get(id);
    if (agentId === undefined) return;
    this.interactionAgents.delete(id);
    const projector = this.projectors.get(agentId);
    if (projector === undefined) return;
    this.emitAll(projector.interactionResolved(id, response));
    this.emitState();
  }

  private projectorFor(agentId: string): AgentMessageProjector | undefined {
    const existing = this.projectors.get(agentId);
    if (existing !== undefined) return existing;
    const handle = this.agentHandle(agentId);
    if (handle === undefined) return undefined;
    this.subscribeAgent(handle);
    return this.projectors.get(agentId);
  }

  private scheduleHeal(agentId: string, ordinal: number): void {
    const existing = this.healTimers.get(agentId);
    if (existing !== undefined) {
      existing.ordinals.add(ordinal);
      existing.timer.refresh();
      return;
    }
    const ordinals = new Set([ordinal]);
    const timer = setTimeout(() => {
      this.healTimers.delete(agentId);
      void this.healTurns(agentId, ordinals);
    }, TURN_HEAL_DEBOUNCE_MS);
    timer.unref();
    this.healTimers.set(agentId, { ordinals, timer });
  }

  private async seedTimelineFromWire(
    agentId: string,
    projector: AgentMessageProjector,
  ): Promise<void> {
    const records = await this.readAgentWire(agentId);
    if (records === undefined) return;
    if (this.disposed || this.projectors.get(agentId) !== projector) return;
    projector.applyTimelineSeed(foldTimelineSeed(records));
  }

  private async healTurns(agentId: string, ordinals: ReadonlySet<number>): Promise<void> {
    const projector = this.projectors.get(agentId);
    if (projector === undefined || this.disposed) return;
    const records = await this.readAgentWire(agentId);
    if (records === undefined) return;
    if (this.disposed || this.projectors.get(agentId) !== projector) return;
    for (const ordinal of ordinals) {
      this.emitAll(projector.healTurn(ordinal, foldWireTurn(records, ordinal)));
    }
  }

  private async patchTaskOutputTail(agentId: string, taskId: string): Promise<void> {
    const tasks = this.agentHandle(agentId)?.accessor.get(IAgentTaskService);
    if (tasks === undefined) return;
    let tail: string;
    try {
      tail = await tasks.readOutput(taskId, TASK_OUTPUT_TAIL_CHARS);
    } catch {
      return;
    }
    if (this.disposed || tail.length === 0) return;
    this.emitAll(this.projectors.get(agentId)?.taskOutputUpdated(taskId, tail) ?? []);
  }

  private async readAgentWire(agentId: string): Promise<ContextRecord[] | undefined> {
    const index = this.deps.core.accessor.get(ISessionIndex) as ISessionIndex | undefined;
    if (index === undefined) return undefined;
    const summary = await index.get(this.sessionId);
    if (summary === undefined) return undefined;
    const wire = this.agentHandle(agentId)?.accessor.get(IWireService);
    if (wire !== undefined) {
      try {
        await wire.flush();
      } catch (error) {
        this.deps.logger?.warn(
          {
            sessionId: this.sessionId,
            agentId,
            err: error instanceof Error ? error.message : error,
          },
          'projection: wire flush failed, reading what is on disk',
        );
      }
    }
    try {
      return await readWireRecords(
        join(
          this.deps.homeDir,
          'sessions',
          summary.workspaceId,
          this.sessionId,
          'agents',
          agentId,
          'wire.jsonl',
        ),
      );
    } catch (error) {
      if ((error as NodeJS.ErrnoException).code === 'ENOENT') return undefined;
      this.deps.logger?.warn(
        {
          sessionId: this.sessionId,
          agentId,
          err: error instanceof Error ? error.message : error,
        },
        'projection: wire read failed, continuing without it',
      );
      return undefined;
    }
  }

  private emitAll(messages: ServerMessage[]): void {
    for (const message of messages) this.emit(message);
  }

  private emitState(): void {
    const state = this.aggregator.changed(this.sessionId);
    if (state !== undefined) this.emit(state);
  }

  private emit(message: ServerMessage): void {
    const parsed = this.validate(message);
    if (parsed === undefined) return;
    for (const listener of this.listeners) {
      try {
        listener(parsed);
      } catch {
      }
    }
  }

  private validate(message: ServerMessage): ServerMessage | undefined {
    const parsed = serverMessageSchema.safeParse(message);
    if (parsed.success) return parsed.data;
    const type = String((message as { type?: unknown }).type);
    const count = (this.validationFailures.get(type) ?? 0) + 1;
    this.validationFailures.set(type, count);
    if (count === 1 || count % 100 === 0) {
      this.deps.logger?.warn(
        {
          sessionId: this.sessionId,
          type,
          count,
          issues: parsed.error.issues.map((issue) => ({
            path: issue.path.join('.'),
            message: issue.message,
          })),
        },
        'projection: outbound message failed schema validation, dropped',
      );
    }
    return undefined;
  }
}

function interactionAgentId(interaction: Interaction): string {
  const payloadAgent = (interaction.payload as { agentId?: unknown }).agentId;
  const tag = interaction.tags[INTERACTION_TAG_AGENT_ID];
  return (
    (typeof tag === 'string' ? tag : undefined) ??
    (typeof payloadAgent === 'string' ? payloadAgent : undefined) ??
    MAIN_AGENT_ID
  );
}