File size: 10,258 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
import { enableMapSet, type Draft } from 'immer';
import type { z } from 'zod';

import { BugIndicatingError } from '#/_base/errors/errors';
import type { StateKey } from '#/_base/state/stateRegistry';
import { Event2, registerEvent2Class, type Event2Class } from '#/app/event/event2';
import type { PartsTransformer, RecordDehydrator } from '#/wire/record';

import { StateError, StateErrors } from './errors';

enableMapSet();

export type { StateKey } from '#/_base/state/stateRegistry';
export type { PartsTransformer } from '#/wire/record';

export interface StateBlobCodec<S> {
  dehydrate: RecordDehydrator;
  rehydrate(state: S, transform: PartsTransformer): S | Promise<S>;
}

export interface FoldContext {
  readonly silent: boolean;
  checkpoint(): void;
  clearCheckpoints(): void;
  undoToCheckpoint(count: number): void;
  emit(event: Event2): void;
}

export type StateFold<S, E extends Event2<any> = Event2<any>> = (
  state: Draft<S>,
  event: E,
  ctx: FoldContext,
) => S | void;

export interface ReplayableOptions<S> {
  readonly schema: z.ZodType<S>;
  readonly durable?: boolean;
  readonly blobs?: StateBlobCodec<S>;
}

export interface UndoableOptions<S> {
  readonly onUndo?: (state: Draft<S>, count: number) => S | void;
}

export interface ReplayableStateMeta<S> {
  readonly schema: z.ZodType<S>;
  readonly durable: boolean;
  readonly blobs?: StateBlobCodec<S>;
  readonly undoable?: UndoableOptions<S>;
  readonly folds: ReadonlyMap<Event2Class<any, any>, StateFold<S, any>>;
}

export interface ReplayableStateKey<S> extends StateKey<DeepReadonly<S>> {
  readonly replayable: ReplayableStateMeta<S>;
  undoable(opts?: UndoableOptions<S>): ReplayableStateKey<S>;
  on<P, E extends Event2<P>>(cls: Event2Class<P, E>, fold: StateFold<S, E>): ReplayableStateKey<S>;
}

export interface StateKeyBuilder<T> extends StateKey<T> {
  replayable(opts: ReplayableOptions<T>): ReplayableStateKey<T>;
}

class ReplayableStateKeyImpl<S> implements ReplayableStateKey<S> {
  readonly snapshotExcluded = true;
  readonly initial: () => DeepReadonly<S>;

  private readonly meta: {
    readonly schema: z.ZodType<S>;
    readonly durable: boolean;
    readonly blobs?: StateBlobCodec<S>;
    undoable?: UndoableOptions<S>;
    readonly folds: Map<Event2Class<any, any>, StateFold<S, any>>;
  };

  constructor(
    readonly name: string,
    initial: () => S,
    opts: ReplayableOptions<S>,
  ) {
    this.initial = () => Object.freeze(initial()) as DeepReadonly<S>;
    this.meta = {
      schema: opts.schema,
      durable: opts.durable ?? true,
      blobs: opts.blobs,
      folds: new Map(),
    };
  }

  get replayable(): ReplayableStateMeta<S> {
    return this.meta;
  }

  undoable(opts?: UndoableOptions<S>): ReplayableStateKey<S> {
    if (this.meta.undoable !== undefined) {
      throw new BugIndicatingError(`State key '${this.name}' is already undoable`);
    }
    if (!this.meta.durable) {
      throw new BugIndicatingError(`Transient state key '${this.name}' cannot be undoable`);
    }
    this.meta.undoable = opts ?? {};
    return this;
  }

  on<P, E extends Event2<P>>(cls: Event2Class<P, E>, fold: StateFold<S, E>): ReplayableStateKey<S> {
    if (this.meta.folds.has(cls)) {
      throw new StateError(
        StateErrors.codes.STATE_DUPLICATE_FOLD,
        `State '${this.name}' already folds event '${cls.type}'`,
        { details: { state: this.name, type: cls.type } },
      );
    }
    if (!this.meta.durable && cls.durable) {
      throw new StateError(
        StateErrors.codes.STATE_DURABILITY_MISMATCH,
        `Transient state '${this.name}' cannot fold durable event '${cls.type}'`,
        { details: { state: this.name, type: cls.type } },
      );
    }
    registerEvent2Class(cls);
    this.meta.folds.set(cls, fold as StateFold<S, any>);
    return this;
  }
}

class StateKeyBuilderImpl<T> implements StateKeyBuilder<T> {
  constructor(
    readonly name: string,
    readonly initial: () => T,
  ) {}

  replayable(opts: ReplayableOptions<T>): ReplayableStateKey<T> {
    return new ReplayableStateKeyImpl(this.name, this.initial, opts);
  }
}

export function defineState<T>(name: string, initial: () => T): StateKeyBuilder<T> {
  return new StateKeyBuilderImpl(name, initial);
}

export interface UndoableProtocol {
  readonly events: {
    readonly appendMessage: Event2Class<any, any>;
    readonly applyCompaction: Event2Class<any, any>;
    readonly clear: Event2Class<any, any>;
    readonly undo: Event2Class<any, any>;
  };
  readonly isUndoAnchor: (message: unknown) => boolean;
  readonly isValidUndoCount: (count: number) => boolean;
}

let undoableProtocol: UndoableProtocol | undefined;

export function registerUndoableProtocol(protocol: UndoableProtocol): void {
  if (undoableProtocol !== undefined) {
    throw new BugIndicatingError('The undoable protocol is already registered');
  }
  undoableProtocol = protocol;
  for (const cls of Object.values(protocol.events)) {
    registerEvent2Class(cls);
  }
}

export function expandedStateFolds(
  key: ReplayableStateKey<any>,
): ReadonlyMap<Event2Class<any, any>, StateFold<any, any>> {
  const meta = key.replayable;
  if (meta.undoable === undefined) return meta.folds;
  if (undoableProtocol === undefined) {
    throw new BugIndicatingError(
      `State key '${key.name}' is undoable but no undoable protocol is registered ` +
        '(the contextMemory domain registers it at import time)',
    );
  }
  const protocol = undoableProtocol;
  if (meta.folds.has(protocol.events.undo)) {
    throw new BugIndicatingError(
      `Undoable state key '${key.name}' must not fold the undo event itself; ` +
        'use .undoable({ onUndo }) to customize the rollback',
    );
  }
  const custom = meta.undoable.onUndo !== undefined;
  const folds = new Map<Event2Class<any, any>, StateFold<any, any>>(meta.folds);
  const domainAppend = folds.get(protocol.events.appendMessage);
  folds.set(protocol.events.appendMessage, (state, event, ctx) => {
    if (!custom && protocol.isUndoAnchor(event.message)) {
      ctx.checkpoint();
      return;
    }
    return domainAppend?.(state, event, ctx);
  });
  for (const cls of [protocol.events.applyCompaction, protocol.events.clear]) {
    const domain = folds.get(cls);
    folds.set(cls, (state, event, ctx) => {
      ctx.clearCheckpoints();
      return domain?.(state, event, ctx);
    });
  }
  folds.set(protocol.events.undo, (state, event, ctx) => {
    if (!protocol.isValidUndoCount(event.count)) return;
    if (meta.undoable?.onUndo !== undefined) {
      return meta.undoable.onUndo(state, event.count);
    }
    ctx.undoToCheckpoint(event.count);
  });
  return folds;
}

export type EventApplier = (event: any, ctx: FoldContext) => void;

export function expandedModelAppliers(
  owner: string,
  undoable: boolean,
  appliers: ReadonlyMap<Event2Class<any, any>, EventApplier>,
  onUndo: ((count: number) => void) | undefined,
): ReadonlyMap<Event2Class<any, any>, EventApplier> {
  if (!undoable) return appliers;
  if (undoableProtocol === undefined) {
    throw new BugIndicatingError(
      `Agent model '${owner}' is undoable but no undoable protocol is registered ` +
        '(the contextMemory domain registers it at import time)',
    );
  }
  const protocol = undoableProtocol;
  if (appliers.has(protocol.events.undo)) {
    throw new BugIndicatingError(
      `Undoable agent model '${owner}' must not apply the undo event itself; ` +
        'override onUndo on the model to customize the rollback',
    );
  }
  const custom = onUndo !== undefined;
  const expanded = new Map<Event2Class<any, any>, EventApplier>(appliers);
  const domainAppend = expanded.get(protocol.events.appendMessage);
  expanded.set(protocol.events.appendMessage, (event, ctx) => {
    if (!custom && protocol.isUndoAnchor(event.message)) {
      ctx.checkpoint();
      return;
    }
    domainAppend?.(event, ctx);
  });
  for (const cls of [protocol.events.applyCompaction, protocol.events.clear]) {
    const domain = expanded.get(cls);
    expanded.set(cls, (event, ctx) => {
      ctx.clearCheckpoints();
      domain?.(event, ctx);
    });
  }
  expanded.set(protocol.events.undo, (event, ctx) => {
    if (!protocol.isValidUndoCount(event.count)) return;
    if (onUndo !== undefined) {
      onUndo(event.count);
      return;
    }
    ctx.undoToCheckpoint(event.count);
  });
  return expanded;
}

export function expandedRuntimeFolds(
  owner: string,
  undoable: boolean,
  folds: ReadonlyMap<Event2Class<any, any>, StateFold<any, any>>,
): ReadonlyMap<Event2Class<any, any>, StateFold<any, any>> {
  if (!undoable) return folds;
  if (undoableProtocol === undefined) {
    throw new BugIndicatingError(
      `Agent runtime '${owner}' is undoable but no undoable protocol is registered ` +
        '(the contextMemory domain registers it at import time)',
    );
  }
  const protocol = undoableProtocol;
  if (folds.has(protocol.events.undo)) {
    throw new BugIndicatingError(
      `Undoable agent runtime '${owner}' must not fold the undo event itself`,
    );
  }
  const expanded = new Map(folds);
  const domainAppend = expanded.get(protocol.events.appendMessage);
  expanded.set(protocol.events.appendMessage, (state, event, ctx) => {
    if (protocol.isUndoAnchor(event.message)) {
      ctx.checkpoint();
      return;
    }
    return domainAppend?.(state, event, ctx);
  });
  for (const cls of [protocol.events.applyCompaction, protocol.events.clear]) {
    const domain = expanded.get(cls);
    expanded.set(cls, (state, event, ctx) => {
      ctx.clearCheckpoints();
      return domain?.(state, event, ctx);
    });
  }
  expanded.set(protocol.events.undo, (_state, event, ctx) => {
    if (protocol.isValidUndoCount(event.count)) ctx.undoToCheckpoint(event.count);
  });
  return expanded;
}

export type DeepReadonly<T> = T extends (...args: infer A) => infer R
  ? (...args: A) => R
  : T extends ReadonlyMap<infer K, infer V>
    ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>>
    : T extends ReadonlySet<infer V>
      ? ReadonlySet<DeepReadonly<V>>
      : T extends readonly (infer E)[]
        ? ReadonlyArray<DeepReadonly<E>>
        : T extends object
          ? { readonly [K in keyof T]: DeepReadonly<T[K]> }
          : T;