File size: 8,988 Bytes
5710dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Chat channel — owns the `ChatStore`, the `AuditTrail`, the REST history
 * pipeline and the `/api/v3/ws` subscription for one (session, agent) pair.
 *
 * Recovery per the protocol, all of it converging through idempotent
 * replace-by-id upserts (no buffering, no cursors beyond the two REST
 * page cursors, no reset frames):
 *
 *  - Initial load / full refresh: newest REST history page (`replace`),
 *    then re-cover the previously loaded window with `before_turn` pages.
 *  - Live + recovery payload: every WS message is applied to the store
 *    as it lands; recovery and live are the same path.
 *  - Subscribe ack (initial and every reconnect): `after_step` catch-up
 *    anchored at the newest TERMINAL step (the server answers with the
 *    slice after that step's last entity, so the step that was streaming
 *    at disconnect is re-read in full; overlap is idempotent). An empty
 *    catch-up is verified against the newest page — if the anchor itself
 *    is gone (undo/clear while disconnected), fall back to a full refresh.
 *  - `in_flight` on a history response means the WS replay re-sends that
 *    step's entities from the start; nothing to do but let them land.
 */

import type { WsLikeCtor } from '../channel/wsLike';
import { AuditTrail } from '../audit/trail';
import { fetchHistoryPage, HISTORY_PAGE_SIZE, type HistoryPage } from './api';
import {
  ChatStore,
  newestTerminalStepId,
  oldestTurnId,
  recoverLoadedWindow,
} from './store';
import { ChatWs } from './ws';

export interface ChatChannelOptions {
  readonly baseUrl: string;
  readonly token?: string;
  readonly sessionId: string;
  readonly agentId: string;
  readonly pageSize?: number;
  readonly WebSocketImpl?: WsLikeCtor;
  readonly fetchImpl?: typeof fetch;
  readonly reconnectDelayMs?: number;
  readonly notifyIntervalMs?: number;
  /** Fired before a replace-mode refresh drops the current window (scroll anchor hook). */
  readonly onWillReplace?: () => void;
  readonly onLoaded?: () => void;
  readonly onLoadError?: (error: unknown) => void;
}

export class ChatChannel {
  readonly store: ChatStore;
  readonly trail: AuditTrail;

  private readonly opts: ChatChannelOptions;
  private readonly pageSize: number;
  private readonly ws: ChatWs;
  private queue: Promise<void> = Promise.resolve();
  private refreshQueued = false;
  private catchUpQueued = false;
  private disposed = false;

  constructor(opts: ChatChannelOptions) {
    this.opts = opts;
    this.pageSize = opts.pageSize ?? HISTORY_PAGE_SIZE;
    this.store = new ChatStore({ notifyIntervalMs: opts.notifyIntervalMs });
    this.trail = new AuditTrail();
    this.ws = new ChatWs({
      url: opts.baseUrl,
      token: opts.token,
      sessionId: opts.sessionId,
      agentIds: [opts.agentId],
      WebSocketImpl: opts.WebSocketImpl,
      reconnectDelayMs: opts.reconnectDelayMs,
      handlers: {
        onMessage: (message) => {
          this.store.applyLive(message);
          this.trail.recordWs(message, this.store.getState());
        },
        onAck: (code, msg) => {
          if (code === 0) {
            this.trail.recordEvent('ack', undefined, this.store.getState());
            this.scheduleCatchUp();
            return;
          }
          this.trail.recordEvent('ack-error', msg, this.store.getState());
          this.opts.onLoadError?.(new Error(`subscribe rejected (${code}): ${msg ?? ''}`));
        },
        onProtocolError: (code, msg) => {
          this.trail.recordEvent('protocol-error', `${code}: ${msg}`, this.store.getState());
        },
        onInvalidFrame: () => {
          this.trail.recordEvent('invalid-frame', undefined, this.store.getState());
        },
        onReconnectScheduled: () => {
          this.trail.recordEvent('reconnect', undefined, this.store.getState());
        },
      },
    });
  }

  /** Kick the initial load (the socket is already connecting). */
  start(): void {
    this.scheduleRefresh();
  }

  /** Page one older slice into the window (`before_turn`); rejects on fetch failure. */
  async loadOlder(): Promise<void> {
    const oldest = oldestTurnId(this.store.getState().entries);
    if (oldest === undefined) return;
    const page = await this.fetchPage({ beforeTurn: oldest });
    if (this.disposed) return;
    this.store.applyHistoryPage(page.messages, 'prepend');
    this.store.setHasMoreOlder(page.messages.length === this.pageSize);
    this.trail.recordRest(
      { beforeTurn: oldest, pageSize: this.pageSize },
      'prepend',
      page.messages.length,
      page.inFlight,
      this.store.getState(),
    );
  }

  /** Force a WS reconnect (debug/testing): the ack re-triggers the after_step catch-up. */
  reconnect(delayMs = 0): void {
    this.ws.reconnect(delayMs);
  }

  close(): void {
    this.disposed = true;
    this.ws.close();
    this.store.flushNotify();
  }

  private scheduleRefresh(): void {
    if (this.refreshQueued) return;
    this.refreshQueued = true;
    this.enqueue(async () => {
      this.refreshQueued = false;
      await this.doRefresh();
    });
  }

  private scheduleCatchUp(): void {
    if (this.catchUpQueued) return;
    this.catchUpQueued = true;
    this.enqueue(async () => {
      this.catchUpQueued = false;
      await this.doCatchUp();
    });
  }

  private enqueue(task: () => Promise<void>): void {
    this.queue = this.queue.then(task).catch(() => {});
  }

  private async doRefresh(): Promise<void> {
    const prevOldest = oldestTurnId(this.store.getState().entries);
    if (prevOldest !== undefined) this.opts.onWillReplace?.();
    try {
      const page = await this.fetchPage({});
      if (this.disposed) return;
      this.store.applyHistoryPage(page.messages, 'replace');
      this.store.setHasMoreOlder(page.messages.length === this.pageSize);
      this.trail.recordRest(
        { pageSize: this.pageSize },
        'replace',
        page.messages.length,
        page.inFlight,
        this.store.getState(),
      );
      await recoverLoadedWindow(
        this.store,
        prevOldest,
        async (beforeTurn) => {
          const older = await this.fetchPage({ beforeTurn });
          if (this.disposed) return [];
          this.store.setHasMoreOlder(older.messages.length === this.pageSize);
          return older.messages;
        },
        () => this.disposed,
        (beforeTurn, messages) => {
          this.trail.recordRest(
            { beforeTurn, pageSize: this.pageSize },
            'prepend',
            messages.length,
            undefined,
            this.store.getState(),
          );
        },
      );
      if (!this.disposed) this.opts.onLoaded?.();
    } catch (error) {
      if (!this.disposed) this.opts.onLoadError?.(error);
    }
  }

  private async doCatchUp(): Promise<void> {
    const anchor = newestTerminalStepId(this.store.getState().entries);
    if (anchor === undefined) {
      this.scheduleRefresh();
      return;
    }
    let cursor = anchor;
    for (;;) {
      let page: HistoryPage;
      try {
        page = await this.fetchPage({ afterStep: cursor });
      } catch (error) {
        if (!this.disposed) this.opts.onLoadError?.(error);
        return;
      }
      if (this.disposed) return;
      if (page.messages.length === 0) {
        let probe: HistoryPage;
        try {
          probe = await this.fetchPage({});
        } catch {
          return;
        }
        if (this.disposed) return;
        if (!anchorAliveInPage(probe.messages, cursor)) {
          this.trail.recordEvent(
            'catchup-refresh',
            `anchor ${cursor} no longer exists`,
            this.store.getState(),
          );
          this.scheduleRefresh();
        }
        return;
      }
      this.store.applyHistoryPage(page.messages, 'tail');
      this.trail.recordRest(
        { afterStep: cursor, pageSize: this.pageSize },
        'tail',
        page.messages.length,
        page.inFlight,
        this.store.getState(),
      );
      if (page.messages.length < this.pageSize) return;
      const next = newestTerminalStepId(this.store.getState().entries);
      if (next === undefined || next === cursor) return;
      cursor = next;
    }
  }

  private fetchPage(cursor: {
    beforeTurn?: string;
    afterStep?: string;
    pageSize?: number;
  }): Promise<HistoryPage> {
    return fetchHistoryPage({
      baseUrl: this.opts.baseUrl,
      token: this.opts.token,
      sessionId: this.opts.sessionId,
      agentId: this.opts.agentId,
      beforeTurn: cursor.beforeTurn,
      afterStep: cursor.afterStep,
      pageSize: cursor.pageSize ?? this.pageSize,
      fetchImpl: this.opts.fetchImpl,
    });
  }
}

function anchorAliveInPage(messages: HistoryPage['messages'], cursor: string): boolean {
  const cursorTurn = cursor.split('.')[0]!;
  return messages.some(
    (message) =>
      ('step_id' in message && message.step_id === cursor) ||
      ('turn_id' in message && message.turn_id === cursorTurn),
  );
}