@zelari/core 1.48.1 → 2.0.0-alpha.2

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 (117) hide show
  1. package/dist/agents/promptModules.d.ts +9 -0
  2. package/dist/agents/promptModules.d.ts.map +1 -1
  3. package/dist/agents/promptModules.js +34 -0
  4. package/dist/agents/promptModules.js.map +1 -1
  5. package/dist/experimental.d.ts +17 -0
  6. package/dist/experimental.d.ts.map +1 -0
  7. package/dist/experimental.js +31 -0
  8. package/dist/experimental.js.map +1 -0
  9. package/dist/index.d.ts +5 -0
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +10 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/mission/index.d.ts +5 -0
  14. package/dist/mission/index.d.ts.map +1 -0
  15. package/dist/mission/index.js +5 -0
  16. package/dist/mission/index.js.map +1 -0
  17. package/dist/mission/missionState.d.ts +37 -0
  18. package/dist/mission/missionState.d.ts.map +1 -0
  19. package/dist/mission/missionState.js +61 -0
  20. package/dist/mission/missionState.js.map +1 -0
  21. package/dist/runtime/executionContext.d.ts +46 -0
  22. package/dist/runtime/executionContext.d.ts.map +1 -0
  23. package/dist/runtime/executionContext.js +63 -0
  24. package/dist/runtime/executionContext.js.map +1 -0
  25. package/dist/runtime/index.d.ts +10 -0
  26. package/dist/runtime/index.d.ts.map +1 -0
  27. package/dist/runtime/index.js +10 -0
  28. package/dist/runtime/index.js.map +1 -0
  29. package/dist/runtime/memoryProviders.d.ts +27 -0
  30. package/dist/runtime/memoryProviders.d.ts.map +1 -0
  31. package/dist/runtime/memoryProviders.js +64 -0
  32. package/dist/runtime/memoryProviders.js.map +1 -0
  33. package/dist/runtime/nodeProviders.d.ts +21 -0
  34. package/dist/runtime/nodeProviders.d.ts.map +1 -0
  35. package/dist/runtime/nodeProviders.js +138 -0
  36. package/dist/runtime/nodeProviders.js.map +1 -0
  37. package/dist/runtime/profiles.d.ts +44 -0
  38. package/dist/runtime/profiles.d.ts.map +1 -0
  39. package/dist/runtime/profiles.js +91 -0
  40. package/dist/runtime/profiles.js.map +1 -0
  41. package/dist/runtime/providers.d.ts +85 -0
  42. package/dist/runtime/providers.d.ts.map +1 -0
  43. package/dist/runtime/providers.js +58 -0
  44. package/dist/runtime/providers.js.map +1 -0
  45. package/dist/runtime/worktreeWorkspace.d.ts +38 -0
  46. package/dist/runtime/worktreeWorkspace.d.ts.map +1 -0
  47. package/dist/runtime/worktreeWorkspace.js +80 -0
  48. package/dist/runtime/worktreeWorkspace.js.map +1 -0
  49. package/dist/session/exportSession.d.ts +31 -0
  50. package/dist/session/exportSession.d.ts.map +1 -0
  51. package/dist/session/exportSession.js +37 -0
  52. package/dist/session/exportSession.js.map +1 -0
  53. package/dist/session/index.d.ts +15 -0
  54. package/dist/session/index.d.ts.map +1 -0
  55. package/dist/session/index.js +15 -0
  56. package/dist/session/index.js.map +1 -0
  57. package/dist/session/lineage.d.ts +38 -0
  58. package/dist/session/lineage.d.ts.map +1 -0
  59. package/dist/session/lineage.js +69 -0
  60. package/dist/session/lineage.js.map +1 -0
  61. package/dist/session/modelSurface.d.ts +43 -0
  62. package/dist/session/modelSurface.d.ts.map +1 -0
  63. package/dist/session/modelSurface.js +96 -0
  64. package/dist/session/modelSurface.js.map +1 -0
  65. package/dist/session/replay.d.ts +61 -0
  66. package/dist/session/replay.d.ts.map +1 -0
  67. package/dist/session/replay.js +137 -0
  68. package/dist/session/replay.js.map +1 -0
  69. package/dist/session/store.d.ts +60 -0
  70. package/dist/session/store.d.ts.map +1 -0
  71. package/dist/session/store.js +117 -0
  72. package/dist/session/store.js.map +1 -0
  73. package/dist/session/types.d.ts +85 -0
  74. package/dist/session/types.d.ts.map +1 -0
  75. package/dist/session/types.js +62 -0
  76. package/dist/session/types.js.map +1 -0
  77. package/dist/session/writer.d.ts +44 -0
  78. package/dist/session/writer.d.ts.map +1 -0
  79. package/dist/session/writer.js +114 -0
  80. package/dist/session/writer.js.map +1 -0
  81. package/dist/shared/events.d.ts +76 -2
  82. package/dist/shared/events.d.ts.map +1 -1
  83. package/dist/shared/events.js +6 -0
  84. package/dist/shared/events.js.map +1 -1
  85. package/dist/skills/index.d.ts +2 -2
  86. package/dist/skills/index.d.ts.map +1 -1
  87. package/dist/skills/index.js +1 -1
  88. package/dist/skills/index.js.map +1 -1
  89. package/dist/verification/completionPolicy.d.ts +33 -0
  90. package/dist/verification/completionPolicy.d.ts.map +1 -0
  91. package/dist/verification/completionPolicy.js +68 -0
  92. package/dist/verification/completionPolicy.js.map +1 -0
  93. package/dist/verification/criteriaPack.v1.d.ts +27 -0
  94. package/dist/verification/criteriaPack.v1.d.ts.map +1 -0
  95. package/dist/verification/criteriaPack.v1.js +58 -0
  96. package/dist/verification/criteriaPack.v1.js.map +1 -0
  97. package/dist/verification/engine.d.ts +38 -0
  98. package/dist/verification/engine.d.ts.map +1 -0
  99. package/dist/verification/engine.js +163 -0
  100. package/dist/verification/engine.js.map +1 -0
  101. package/dist/verification/index.d.ts +11 -0
  102. package/dist/verification/index.d.ts.map +1 -0
  103. package/dist/verification/index.js +11 -0
  104. package/dist/verification/index.js.map +1 -0
  105. package/dist/verification/metrics.d.ts +26 -0
  106. package/dist/verification/metrics.d.ts.map +1 -0
  107. package/dist/verification/metrics.js +29 -0
  108. package/dist/verification/metrics.js.map +1 -0
  109. package/dist/verification/types.d.ts +161 -0
  110. package/dist/verification/types.d.ts.map +1 -0
  111. package/dist/verification/types.js +92 -0
  112. package/dist/verification/types.js.map +1 -0
  113. package/dist/verification/verifier.d.ts +133 -0
  114. package/dist/verification/verifier.d.ts.map +1 -0
  115. package/dist/verification/verifier.js +215 -0
  116. package/dist/verification/verifier.js.map +1 -0
  117. package/package.json +17 -1
@@ -0,0 +1,37 @@
1
+ /**
2
+ * session/exportSession.ts — portable, machine-readable session export.
3
+ *
4
+ * The export embeds the full event log plus a compact summary; it requires
5
+ * no lock and is safe to produce from a crashed/interrupted session.
6
+ */
7
+ import { buildProjection } from './replay.js';
8
+ export const SESSION_EXPORT_FORMAT = 'zelari-session-export';
9
+ export const SESSION_EXPORT_VERSION = 1;
10
+ export async function exportSession(store, sessionId) {
11
+ const report = await store.read(sessionId);
12
+ const projection = buildProjection(report.events, report.issues);
13
+ return {
14
+ format: SESSION_EXPORT_FORMAT,
15
+ version: SESSION_EXPORT_VERSION,
16
+ sessionId,
17
+ exportedAt: Date.now(),
18
+ summary: {
19
+ lastSeq: projection.lastSeq,
20
+ eventCount: projection.eventCount,
21
+ startedAt: projection.startedAt,
22
+ endedAt: projection.endedAt,
23
+ forkParent: projection.fork?.parentSessionId,
24
+ toolCalls: projection.toolCalls,
25
+ toolResults: projection.toolResults,
26
+ verifications: projection.verifications.length,
27
+ issues: report.issues.length,
28
+ },
29
+ events: report.events,
30
+ };
31
+ }
32
+ /** Pretty-printed JSON export (headless `--session-export` output). */
33
+ export async function exportSessionJson(store, sessionId) {
34
+ const exported = await exportSession(store, sessionId);
35
+ return JSON.stringify(exported, null, 2);
36
+ }
37
+ //# sourceMappingURL=exportSession.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exportSession.js","sourceRoot":"","sources":["../../src/session/exportSession.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAG9C,MAAM,CAAC,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AAC7D,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,CAAC;AAqBxC,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,KAAmB,EAAE,SAAiB;IACxE,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC3C,MAAM,UAAU,GAAG,eAAe,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IACjE,OAAO;QACL,MAAM,EAAE,qBAAqB;QAC7B,OAAO,EAAE,sBAAsB;QAC/B,SAAS;QACT,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE;QACtB,OAAO,EAAE;YACP,OAAO,EAAE,UAAU,CAAC,OAAO;YAC3B,UAAU,EAAE,UAAU,CAAC,UAAU;YACjC,SAAS,EAAE,UAAU,CAAC,SAAS;YAC/B,OAAO,EAAE,UAAU,CAAC,OAAO;YAC3B,UAAU,EAAE,UAAU,CAAC,IAAI,EAAE,eAAe;YAC5C,SAAS,EAAE,UAAU,CAAC,SAAS;YAC/B,WAAW,EAAE,UAAU,CAAC,WAAW;YACnC,aAAa,EAAE,UAAU,CAAC,aAAa,CAAC,MAAM;YAC9C,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM;SAC7B;QACD,MAAM,EAAE,MAAM,CAAC,MAAM;KACtB,CAAC;AACJ,CAAC;AAED,uEAAuE;AACvE,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,KAAmB,EAAE,SAAiB;IAC5E,MAAM,QAAQ,GAAG,MAAM,aAAa,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IACvD,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @zelari/core/session — the session spine (ADR-0016/0021).
3
+ *
4
+ * Append-only JSONL log, single writer with ownership lock, tolerant replay,
5
+ * deriveMessages as the only model-history path, fork/resume lineage and
6
+ * portable export.
7
+ */
8
+ export * from './types.js';
9
+ export * from './modelSurface.js';
10
+ export * from './writer.js';
11
+ export * from './replay.js';
12
+ export * from './store.js';
13
+ export * from './lineage.js';
14
+ export * from './exportSession.js';
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @zelari/core/session — the session spine (ADR-0016/0021).
3
+ *
4
+ * Append-only JSONL log, single writer with ownership lock, tolerant replay,
5
+ * deriveMessages as the only model-history path, fork/resume lineage and
6
+ * portable export.
7
+ */
8
+ export * from './types.js';
9
+ export * from './modelSurface.js';
10
+ export * from './writer.js';
11
+ export * from './replay.js';
12
+ export * from './store.js';
13
+ export * from './lineage.js';
14
+ export * from './exportSession.js';
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * session/lineage.ts — fork / resume on top of the spine.
3
+ *
4
+ * Fork copies the parent trajectory up to `fromSeq` into a NEW session
5
+ * (re-sequenced 1..n; kinds/actors/data preserved verbatim, timestamps
6
+ * re-stamped by the writer) and appends `session.forked {parentSessionId,
7
+ * parentSeq}`. Resume reopens the log and appends `session.resumed`. Resume
8
+ * fidelity is deterministic on the trajectory plane: same events → same
9
+ * projection.
10
+ */
11
+ import { type SessionProjection } from './replay.js';
12
+ import type { SessionStore } from './store.js';
13
+ import type { SessionLogWriter } from './writer.js';
14
+ export interface ForkOptions {
15
+ /** Copy events with seq <= fromSeq (default: all). */
16
+ fromSeq?: number;
17
+ reason?: string;
18
+ }
19
+ export interface ForkResult {
20
+ sessionId: string;
21
+ writer: SessionLogWriter;
22
+ copiedEvents: number;
23
+ }
24
+ /** Fork a session into a new one, preserving the trajectory up to fromSeq. */
25
+ export declare function forkSession(store: SessionStore, parentSessionId: string, options?: ForkOptions): Promise<ForkResult>;
26
+ export interface ResumeResult {
27
+ writer: SessionLogWriter;
28
+ /** Projection including the `session.resumed` marker. */
29
+ projection: SessionProjection;
30
+ }
31
+ /** Resume a session: reopen the log (seq continues) and mark the resume. */
32
+ export declare function resumeSession(store: SessionStore, sessionId: string): Promise<ResumeResult>;
33
+ /**
34
+ * Ancestor chain of a session (root first). Walks `session.forked` markers
35
+ * across the store; stops on cycles or missing parents.
36
+ */
37
+ export declare function lineageOf(store: SessionStore, sessionId: string): Promise<string[]>;
38
+ //# sourceMappingURL=lineage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lineage.d.ts","sourceRoot":"","sources":["../../src/session/lineage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,EAAmB,KAAK,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD,MAAM,WAAW,WAAW;IAC1B,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,gBAAgB,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,8EAA8E;AAC9E,wBAAsB,WAAW,CAC/B,KAAK,EAAE,YAAY,EACnB,eAAe,EAAE,MAAM,EACvB,OAAO,GAAE,WAAgB,GACxB,OAAO,CAAC,UAAU,CAAC,CAiBrB;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,gBAAgB,CAAC;IACzB,yDAAyD;IACzD,UAAU,EAAE,iBAAiB,CAAC;CAC/B;AAED,4EAA4E;AAC5E,wBAAsB,aAAa,CAAC,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CASjG;AAED;;;GAGG;AACH,wBAAsB,SAAS,CAAC,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAkBzF"}
@@ -0,0 +1,69 @@
1
+ /**
2
+ * session/lineage.ts — fork / resume on top of the spine.
3
+ *
4
+ * Fork copies the parent trajectory up to `fromSeq` into a NEW session
5
+ * (re-sequenced 1..n; kinds/actors/data preserved verbatim, timestamps
6
+ * re-stamped by the writer) and appends `session.forked {parentSessionId,
7
+ * parentSeq}`. Resume reopens the log and appends `session.resumed`. Resume
8
+ * fidelity is deterministic on the trajectory plane: same events → same
9
+ * projection.
10
+ */
11
+ import { ACTOR_SYSTEM } from './types.js';
12
+ import { buildProjection } from './replay.js';
13
+ /** Fork a session into a new one, preserving the trajectory up to fromSeq. */
14
+ export async function forkSession(store, parentSessionId, options = {}) {
15
+ const parent = await store.read(parentSessionId);
16
+ if (parent.events.length === 0 && parent.issues.length === 0) {
17
+ throw new Error(`Cannot fork — session not found: ${parentSessionId}`);
18
+ }
19
+ const fromSeq = options.fromSeq ?? parent.events[parent.events.length - 1]?.seq ?? 0;
20
+ const copied = parent.events.filter((e) => e.seq <= fromSeq);
21
+ const created = await store.create({ reason: options.reason ?? 'fork' });
22
+ for (const e of copied) {
23
+ await created.writer.append({ kind: e.kind, actor: e.actor, data: e.data });
24
+ }
25
+ await created.writer.append({
26
+ kind: 'session.forked',
27
+ actor: ACTOR_SYSTEM,
28
+ data: { parentSessionId, parentSeq: fromSeq },
29
+ });
30
+ return { sessionId: created.sessionId, writer: created.writer, copiedEvents: copied.length };
31
+ }
32
+ /** Resume a session: reopen the log (seq continues) and mark the resume. */
33
+ export async function resumeSession(store, sessionId) {
34
+ const opened = await store.open(sessionId);
35
+ const resumed = await opened.writer.append({
36
+ kind: 'session.resumed',
37
+ actor: ACTOR_SYSTEM,
38
+ data: { fromSeq: opened.projection.lastSeq },
39
+ });
40
+ const projection = buildProjection([...opened.report.events, resumed]);
41
+ return { writer: opened.writer, projection };
42
+ }
43
+ /**
44
+ * Ancestor chain of a session (root first). Walks `session.forked` markers
45
+ * across the store; stops on cycles or missing parents.
46
+ */
47
+ export async function lineageOf(store, sessionId) {
48
+ const chain = [sessionId];
49
+ const seen = new Set([sessionId]);
50
+ let current = sessionId;
51
+ for (let depth = 0; depth < 64; depth++) {
52
+ const report = await store.read(current);
53
+ const forkEvent = [...report.events]
54
+ .reverse()
55
+ .find((e) => e.kind === 'session.forked');
56
+ if (!forkEvent)
57
+ break;
58
+ const parent = typeof forkEvent.data.parentSessionId === 'string' ? forkEvent.data.parentSessionId : '';
59
+ if (!parent || seen.has(parent))
60
+ break;
61
+ if (!(await store.exists(parent)))
62
+ break;
63
+ chain.unshift(parent);
64
+ seen.add(parent);
65
+ current = parent;
66
+ }
67
+ return chain;
68
+ }
69
+ //# sourceMappingURL=lineage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lineage.js","sourceRoot":"","sources":["../../src/session/lineage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,eAAe,EAA0B,MAAM,aAAa,CAAC;AAgBtE,8EAA8E;AAC9E,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,KAAmB,EACnB,eAAuB,EACvB,UAAuB,EAAE;IAEzB,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;IACjD,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC7D,MAAM,IAAI,KAAK,CAAC,oCAAoC,eAAe,EAAE,CAAC,CAAC;IACzE,CAAC;IACD,MAAM,OAA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QACtB,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACjB,OAAO,GAAG,MAAM,CAAC;IACnB,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * session/modelSurface.ts — the ONLY path from session events to model history.
3
+ *
4
+ * `isModelSurfaceEvent` decides what counts as model-visible; `deriveMessages`
5
+ * projects surface events into a provider-neutral message list. The CLI maps
6
+ * these onto its provider-specific request format — nothing else may invent
7
+ * model-visible history (ADR-0016 invariant: model-visible ⟺ logged).
8
+ */
9
+ import type { SessionEventEnvelope, SessionEventKind } from './types.js';
10
+ /** Event kinds that (may) feed the model input. Everything else is state. */
11
+ export declare const MODEL_SURFACE_KINDS: ReadonlySet<SessionEventKind>;
12
+ export declare function isModelSurfaceEvent(event: {
13
+ kind: string;
14
+ }): boolean;
15
+ /** Provider-neutral derived message with provenance (source event seq). */
16
+ export interface DerivedMessage {
17
+ role: 'user' | 'assistant' | 'system' | 'tool';
18
+ content: string;
19
+ /** Present for tool results (and tool calls when includeToolCalls). */
20
+ toolCallId?: string;
21
+ toolName?: string;
22
+ isError?: boolean;
23
+ /** Seq of the source envelope — provenance for audit/replay. */
24
+ seq: number;
25
+ }
26
+ export interface DeriveMessagesOptions {
27
+ /**
28
+ * Emit tool.call events as assistant messages (JSON-encoded args). Default
29
+ * false: the neutral history pairs calls via tool.result only; providers
30
+ * that need explicit call blocks build them from pairToolCalls().
31
+ */
32
+ includeToolCalls?: boolean;
33
+ }
34
+ /** Project surface events into the neutral model history. Deterministic. */
35
+ export declare function deriveMessages(events: readonly SessionEventEnvelope[], options?: DeriveMessagesOptions): DerivedMessage[];
36
+ /** A tool call paired with its result (when both exist in the log). */
37
+ export interface ToolCallPair {
38
+ call: SessionEventEnvelope;
39
+ result?: SessionEventEnvelope;
40
+ }
41
+ /** Pair tool.call events with the matching tool.result by callId, in order. */
42
+ export declare function pairToolCalls(events: readonly SessionEventEnvelope[]): ToolCallPair[];
43
+ //# sourceMappingURL=modelSurface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"modelSurface.d.ts","sourceRoot":"","sources":["../../src/session/modelSurface.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEzE,6EAA6E;AAC7E,eAAO,MAAM,mBAAmB,EAAE,WAAW,CAAC,gBAAgB,CAM5D,CAAC;AAEH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAEpE;AAED,2EAA2E;AAC3E,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,QAAQ,GAAG,MAAM,CAAC;IAC/C,OAAO,EAAE,MAAM,CAAC;IAChB,uEAAuE;IACvE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,gEAAgE;IAChE,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAMD,4EAA4E;AAC5E,wBAAgB,cAAc,CAC5B,MAAM,EAAE,SAAS,oBAAoB,EAAE,EACvC,OAAO,GAAE,qBAA0B,GAClC,cAAc,EAAE,CA+ClB;AAED,uEAAuE;AACvE,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,oBAAoB,CAAC;IAC3B,MAAM,CAAC,EAAE,oBAAoB,CAAC;CAC/B;AAED,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,oBAAoB,EAAE,GAAG,YAAY,EAAE,CAoBrF"}
@@ -0,0 +1,96 @@
1
+ /**
2
+ * session/modelSurface.ts — the ONLY path from session events to model history.
3
+ *
4
+ * `isModelSurfaceEvent` decides what counts as model-visible; `deriveMessages`
5
+ * projects surface events into a provider-neutral message list. The CLI maps
6
+ * these onto its provider-specific request format — nothing else may invent
7
+ * model-visible history (ADR-0016 invariant: model-visible ⟺ logged).
8
+ */
9
+ /** Event kinds that (may) feed the model input. Everything else is state. */
10
+ export const MODEL_SURFACE_KINDS = new Set([
11
+ 'user.message',
12
+ 'assistant.message',
13
+ 'tool.call',
14
+ 'tool.result',
15
+ 'session.compacted',
16
+ ]);
17
+ export function isModelSurfaceEvent(event) {
18
+ return MODEL_SURFACE_KINDS.has(event.kind);
19
+ }
20
+ function asString(value) {
21
+ return typeof value === 'string' ? value : undefined;
22
+ }
23
+ /** Project surface events into the neutral model history. Deterministic. */
24
+ export function deriveMessages(events, options = {}) {
25
+ const messages = [];
26
+ for (const e of events) {
27
+ if (!isModelSurfaceEvent(e))
28
+ continue;
29
+ const d = e.data;
30
+ switch (e.kind) {
31
+ case 'user.message':
32
+ messages.push({ role: 'user', content: String(d.text ?? ''), seq: e.seq });
33
+ break;
34
+ case 'assistant.message':
35
+ messages.push({
36
+ role: 'assistant',
37
+ content: String(d.text ?? ''),
38
+ seq: e.seq,
39
+ });
40
+ break;
41
+ case 'tool.call':
42
+ if (options.includeToolCalls) {
43
+ messages.push({
44
+ role: 'assistant',
45
+ content: JSON.stringify({ tool: d.tool, args: d.args ?? {} }),
46
+ toolCallId: asString(d.callId),
47
+ toolName: asString(d.tool),
48
+ seq: e.seq,
49
+ });
50
+ }
51
+ break;
52
+ case 'tool.result':
53
+ messages.push({
54
+ role: 'tool',
55
+ content: String(d.output ?? ''),
56
+ toolCallId: asString(d.callId),
57
+ toolName: asString(d.tool),
58
+ isError: d.ok === false,
59
+ seq: e.seq,
60
+ });
61
+ break;
62
+ case 'session.compacted':
63
+ messages.push({
64
+ role: 'system',
65
+ content: String(d.summary ?? '[session compacted]'),
66
+ seq: e.seq,
67
+ });
68
+ break;
69
+ }
70
+ }
71
+ return messages;
72
+ }
73
+ /** Pair tool.call events with the matching tool.result by callId, in order. */
74
+ export function pairToolCalls(events) {
75
+ const pending = new Map();
76
+ const ordered = [];
77
+ for (const e of events) {
78
+ if (e.kind === 'tool.call') {
79
+ const callId = typeof e.data.callId === 'string' ? e.data.callId : `seq:${e.seq}`;
80
+ const pair = { call: e };
81
+ ordered.push(pair);
82
+ pending.set(callId, pair);
83
+ }
84
+ else if (e.kind === 'tool.result') {
85
+ if (typeof e.data.callId === 'string') {
86
+ const pair = pending.get(e.data.callId);
87
+ if (pair) {
88
+ pair.result = e;
89
+ pending.delete(e.data.callId);
90
+ }
91
+ }
92
+ }
93
+ }
94
+ return ordered;
95
+ }
96
+ //# sourceMappingURL=modelSurface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"modelSurface.js","sourceRoot":"","sources":["../../src/session/modelSurface.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,6EAA6E;AAC7E,MAAM,CAAC,MAAM,mBAAmB,GAAkC,IAAI,GAAG,CAAC;IACxE,cAAc;IACd,mBAAmB;IACnB,WAAW;IACX,aAAa;IACb,mBAAmB;CACpB,CAAC,CAAC;AAEH,MAAM,UAAU,mBAAmB,CAAC,KAAuB;IACzD,OAAO,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAwB,CAAC,CAAC;AACjE,CAAC;AAuBD,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACvD,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,cAAc,CAC5B,MAAuC,EACvC,UAAiC,EAAE;IAEnC,MAAM,QAAQ,GAAqB,EAAE,CAAC;IACtC,KAAK,MAAM,CAAC,IAAI,MAAM,EAAE,CAAC;QACvB,IAAI,CAAC,mBAAmB,CAAC,CAAC,CAAC;YAAE,SAAS;QACtC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;QACjB,QAAQ,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CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAChC,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * session/replay.ts — tolerant log reader + projection.
3
+ *
4
+ * Replay never throws on a damaged log: corrupt lines, seq gaps, duplicates
5
+ * and schema mismatches are reported as ReplayIssue so the trajectory can be
6
+ * audited (replay reconstructs the trajectory, not model-output determinism).
7
+ */
8
+ import { type SessionEventEnvelope } from './types.js';
9
+ import { type DerivedMessage } from './modelSurface.js';
10
+ export type ReplayIssueType = 'corrupt-line' | 'schema-mismatch' | 'seq-duplicate' | 'seq-gap' | 'seq-nonmonotonic';
11
+ export interface ReplayIssue {
12
+ type: ReplayIssueType;
13
+ /** 1-based line number in the JSONL file. */
14
+ line: number;
15
+ seq?: number;
16
+ detail?: string;
17
+ }
18
+ export interface ReplayReport {
19
+ path: string;
20
+ events: SessionEventEnvelope[];
21
+ issues: ReplayIssue[];
22
+ /** True when every line parsed and seq is 1..n gap-free. */
23
+ ok: boolean;
24
+ }
25
+ export declare function readSessionLog(filePath: string): Promise<ReplayReport>;
26
+ /** Loose summary of a verification.run event (defensive reads). */
27
+ export interface VerificationRunSummary {
28
+ seq: number;
29
+ at: number;
30
+ results: Array<{
31
+ criterionId: string;
32
+ status: string;
33
+ evidenceCount: number;
34
+ }>;
35
+ complete?: boolean;
36
+ }
37
+ export interface SessionProjection {
38
+ sessionId: string;
39
+ lastSeq: number;
40
+ eventCount: number;
41
+ startedAt?: number;
42
+ endedAt?: number;
43
+ fork?: {
44
+ parentSessionId: string;
45
+ parentSeq: number;
46
+ };
47
+ resumedCount: number;
48
+ messages: DerivedMessage[];
49
+ toolCalls: number;
50
+ toolResults: number;
51
+ verifications: VerificationRunSummary[];
52
+ missionPhases: Array<{
53
+ seq: number;
54
+ phase: string;
55
+ }>;
56
+ replans: number;
57
+ issues: ReplayIssue[];
58
+ }
59
+ /** Build the materialized view of a session from its (valid) events. */
60
+ export declare function buildProjection(events: readonly SessionEventEnvelope[], issues?: ReplayIssue[]): SessionProjection;
61
+ //# sourceMappingURL=replay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../../src/session/replay.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAEL,KAAK,oBAAoB,EAC1B,MAAM,YAAY,CAAC;AACpB,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExE,MAAM,MAAM,eAAe,GACvB,cAAc,GACd,iBAAiB,GACjB,eAAe,GACf,SAAS,GACT,kBAAkB,CAAC;AAEvB,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,eAAe,CAAC;IACtB,6CAA6C;IAC7C,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,oBAAoB,EAAE,CAAC;IAC/B,MAAM,EAAE,WAAW,EAAE,CAAC;IACtB,4DAA4D;IAC5D,EAAE,EAAE,OAAO,CAAC;CACb;AAED,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAoD5E;AAED,mEAAmE;AACnE,MAAM,WAAW,sBAAsB;IACrC,GAAG,EAAE,MAAM,CAAC;IACZ,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,KAAK,CAAC;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC/E,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,iBAAiB;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE;QAAE,eAAe,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;IACtD,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,cAAc,EAAE,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,sBAAsB,EAAE,CAAC;IACxC,aAAa,EAAE,KAAK,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACrD,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,WAAW,EAAE,CAAC;CACvB;AAmBD,wEAAwE;AACxE,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,oBAAoB,EAAE,EAAE,MAAM,GAAE,WAAW,EAAO,GAAG,iBAAiB,CAkDtH"}
@@ -0,0 +1,137 @@
1
+ /**
2
+ * session/replay.ts — tolerant log reader + projection.
3
+ *
4
+ * Replay never throws on a damaged log: corrupt lines, seq gaps, duplicates
5
+ * and schema mismatches are reported as ReplayIssue so the trajectory can be
6
+ * audited (replay reconstructs the trajectory, not model-output determinism).
7
+ */
8
+ import { promises as fs } from 'node:fs';
9
+ import { SessionEventEnvelopeSchema, } from './types.js';
10
+ import { deriveMessages } from './modelSurface.js';
11
+ export async function readSessionLog(filePath) {
12
+ let content;
13
+ try {
14
+ content = await fs.readFile(filePath, 'utf-8');
15
+ }
16
+ catch (err) {
17
+ if (err.code === 'ENOENT') {
18
+ return { path: filePath, events: [], issues: [], ok: true };
19
+ }
20
+ throw err;
21
+ }
22
+ const events = [];
23
+ const issues = [];
24
+ let expected = 1;
25
+ const lines = content.split('\n');
26
+ for (let i = 0; i < lines.length; i++) {
27
+ const trimmed = lines[i].trim();
28
+ if (!trimmed)
29
+ continue;
30
+ const lineNo = i + 1;
31
+ let parsed;
32
+ try {
33
+ parsed = JSON.parse(trimmed);
34
+ }
35
+ catch {
36
+ issues.push({ type: 'corrupt-line', line: lineNo });
37
+ continue;
38
+ }
39
+ const result = SessionEventEnvelopeSchema.safeParse(parsed);
40
+ if (!result.success) {
41
+ issues.push({
42
+ type: 'schema-mismatch',
43
+ line: lineNo,
44
+ detail: result.error.issues[0]?.message ?? 'schema validation failed',
45
+ });
46
+ continue;
47
+ }
48
+ const envelope = result.data;
49
+ if (envelope.seq === expected) {
50
+ events.push(envelope);
51
+ expected += 1;
52
+ }
53
+ else if (envelope.seq < expected) {
54
+ issues.push({ type: 'seq-duplicate', line: lineNo, seq: envelope.seq });
55
+ }
56
+ else {
57
+ issues.push({
58
+ type: 'seq-gap',
59
+ line: lineNo,
60
+ seq: envelope.seq,
61
+ detail: `missing seq ${expected}..${envelope.seq - 1}`,
62
+ });
63
+ events.push(envelope);
64
+ expected = envelope.seq + 1;
65
+ }
66
+ }
67
+ return { path: filePath, events, issues, ok: issues.length === 0 };
68
+ }
69
+ function parseVerification(e) {
70
+ const raw = Array.isArray(e.data.results) ? e.data.results : [];
71
+ const results = raw
72
+ .filter((r) => typeof r === 'object' && r !== null)
73
+ .map((r) => ({
74
+ criterionId: String(r.criterionId ?? 'unknown'),
75
+ status: String(r.status ?? 'unknown'),
76
+ evidenceCount: Array.isArray(r.evidence) ? r.evidence.length : 0,
77
+ }));
78
+ return {
79
+ seq: e.seq,
80
+ at: e.ts,
81
+ results,
82
+ complete: typeof e.data.complete === 'boolean' ? e.data.complete : undefined,
83
+ };
84
+ }
85
+ /** Build the materialized view of a session from its (valid) events. */
86
+ export function buildProjection(events, issues = []) {
87
+ const last = events[events.length - 1];
88
+ const projection = {
89
+ sessionId: last?.sessionId ?? '',
90
+ lastSeq: last?.seq ?? 0,
91
+ eventCount: events.length,
92
+ resumedCount: 0,
93
+ messages: deriveMessages(events),
94
+ toolCalls: 0,
95
+ toolResults: 0,
96
+ verifications: [],
97
+ missionPhases: [],
98
+ replans: 0,
99
+ issues,
100
+ };
101
+ for (const e of events) {
102
+ switch (e.kind) {
103
+ case 'session.started':
104
+ projection.startedAt = e.ts;
105
+ break;
106
+ case 'session.ended':
107
+ projection.endedAt = e.ts;
108
+ break;
109
+ case 'session.resumed':
110
+ projection.resumedCount += 1;
111
+ break;
112
+ case 'session.forked':
113
+ projection.fork = {
114
+ parentSessionId: String(e.data.parentSessionId ?? ''),
115
+ parentSeq: Number(e.data.parentSeq ?? 0),
116
+ };
117
+ break;
118
+ case 'tool.call':
119
+ projection.toolCalls += 1;
120
+ break;
121
+ case 'tool.result':
122
+ projection.toolResults += 1;
123
+ break;
124
+ case 'verification.run':
125
+ projection.verifications.push(parseVerification(e));
126
+ break;
127
+ case 'mission.phase':
128
+ projection.missionPhases.push({ seq: e.seq, phase: String(e.data.phase ?? '') });
129
+ break;
130
+ case 'mission.replan':
131
+ projection.replans += 1;
132
+ break;
133
+ }
134
+ }
135
+ return projection;
136
+ }
137
+ //# sourceMappingURL=replay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replay.js","sourceRoot":"","sources":["../../src/session/replay.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EACL,0BAA0B,GAE3B,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,cAAc,EAAuB,MAAM,mBAAmB,CAAC;AAyBxE,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,QAAgB;IACnD,IAAI,OAAe,CAAC;IACpB,IAAI,CAAC;QACH,OAAO,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAK,GAA6B,CAAC,IAAI,K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C,CAAC,EAAE,CAAC;gBAC5B,MAAM;YACR,KAAK,eAAe;gBAClB,UAAU,CAAC,OAAO,GAAG,CAAC,CAAC,EAAE,CAAC;gBAC1B,MAAM;YACR,KAAK,iBAAiB;gBACpB,UAAU,CAAC,YAAY,IAAI,CAAC,CAAC;gBAC7B,MAAM;YACR,KAAK,gBAAgB;gBACnB,UAAU,CAAC,IAAI,GAAG;oBAChB,eAAe,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,eAAe,IAAI,EAAE,CAAC;oBACrD,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,IAAI,CAAC,CAAC;iBACzC,CAAC;gBACF,MAAM;YACR,KAAK,WAAW;gBACd,UAAU,CAAC,SAAS,IAAI,CAAC,CAAC;gBAC1B,MAAM;YACR,KAAK,aAAa;gBAChB,UAAU,CAAC,WAAW,IAAI,CAAC,CAAC;gBAC5B,MAAM;YACR,KAAK,kBAAkB;gBACrB,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC;gBACpD,MAAM;YACR,KAAK,eAAe;gBAClB,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;gBACjF,MAAM;YACR,KAAK,gBAAgB;gBACnB,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC;gBACxB,MAAM;QACV,CAAC;IACH,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * session/store.ts — session directory management.
3
+ *
4
+ * Default location (ADR-0016 ratification): `<workspaceRoot>/.zelari/sessions/
5
+ * <sessionId>/events.jsonl`, overridable via `ZELARI_SESSIONS_DIR` or the
6
+ * explicit option. The legacy home-level sidecar remains read-only compat.
7
+ */
8
+ import { SessionLogWriter } from './writer.js';
9
+ import { type ReplayReport, type SessionProjection } from './replay.js';
10
+ export interface SessionsDirOptions {
11
+ /** Explicit base dir (tests / Desktop multi-cwd). */
12
+ baseDir?: string;
13
+ /** Workspace root — defaults to process.cwd(). */
14
+ workspaceRoot?: string;
15
+ /** Env override source (defaults to process.env). */
16
+ env?: Record<string, string | undefined>;
17
+ }
18
+ export declare function resolveSessionsDir(options?: SessionsDirOptions): string;
19
+ export interface SessionCreateOptions {
20
+ reason?: string;
21
+ profile?: string;
22
+ workspace?: string;
23
+ }
24
+ export interface SessionListEntry {
25
+ sessionId: string;
26
+ lastSeq: number;
27
+ eventCount: number;
28
+ startedAt?: number;
29
+ endedAt?: number;
30
+ forkParent?: string;
31
+ }
32
+ export declare class SessionStore {
33
+ private readonly baseDir;
34
+ constructor(baseDir: string);
35
+ static withDefaults(options?: SessionsDirOptions): SessionStore;
36
+ get dir(): string;
37
+ sessionDir(sessionId: string): string;
38
+ eventsPath(sessionId: string): string;
39
+ /** Create a new session and its writer; appends `session.started`. */
40
+ create(options?: SessionCreateOptions): Promise<{
41
+ sessionId: string;
42
+ writer: SessionLogWriter;
43
+ }>;
44
+ /** Replay a session log (tolerant read). */
45
+ read(sessionId: string): Promise<ReplayReport>;
46
+ /** Materialized view of a session. */
47
+ projection(sessionId: string): Promise<SessionProjection>;
48
+ /** Reopen a session for writing (continues seq after lastSeq). Pure: no event appended. */
49
+ open(sessionId: string): Promise<{
50
+ writer: SessionLogWriter;
51
+ report: ReplayReport;
52
+ projection: SessionProjection;
53
+ }>;
54
+ exists(sessionId: string): Promise<boolean>;
55
+ /** Summaries of all stored sessions (newest first by startedAt). */
56
+ list(): Promise<SessionListEntry[]>;
57
+ /** End a session cleanly (appends `session.ended` and releases the writer). */
58
+ end(writer: SessionLogWriter, reason?: string): Promise<void>;
59
+ }
60
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceR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