@sema-agent/core 7.57.0 → 7.58.0
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.
- package/CHANGELOG.md +385 -0
- package/dist/agents/subagent-steps.js +2 -4
- package/dist/brain/route-adjudicator.js +2 -1
- package/dist/core/a2a.d.ts +2 -1
- package/dist/core/a2a.js +23 -10
- package/dist/core/allow-entry.d.ts +2 -1
- package/dist/core/allow-entry.js +2 -1
- package/dist/core/ask-origin.d.ts +21 -2
- package/dist/core/ask-origin.js +5 -1
- package/dist/core/auto-compaction.d.ts +4 -0
- package/dist/core/auto-compaction.js +1 -1
- package/dist/core/auto-mode-directive.d.ts +29 -5
- package/dist/core/auto-mode-directive.js +38 -8
- package/dist/core/checkpoint-requirements.d.ts +7 -1
- package/dist/core/checkpoint-requirements.js +2 -0
- package/dist/core/checkpoint-store.d.ts +27 -11
- package/dist/core/checkpoint-store.js +13 -3
- package/dist/core/engine-notice.d.ts +1 -1
- package/dist/core/fs-write-gate-policy.d.ts +4 -2
- package/dist/core/fs-write-gate-policy.js +7 -3
- package/dist/core/gate-fold.js +6 -3
- package/dist/core/gate-lanes.js +11 -2
- package/dist/core/git-worktree-env.d.ts +53 -11
- package/dist/core/git-worktree-env.js +96 -12
- package/dist/core/hooks.d.ts +4 -1
- package/dist/core/hooks.js +12 -5
- package/dist/core/human-input-projection.d.ts +1 -0
- package/dist/core/human-input-projection.js +3 -0
- package/dist/core/locked-config.d.ts +15 -1
- package/dist/core/locked-config.js +38 -2
- package/dist/core/mcp-call-meta.d.ts +66 -0
- package/dist/core/mcp-call-meta.js +103 -0
- package/dist/core/mcp-connection-pool.d.ts +3 -1
- package/dist/core/mcp-connection-pool.js +3 -1
- package/dist/core/mcp-failure.d.ts +7 -1
- package/dist/core/mcp-failure.js +10 -0
- package/dist/core/mcp-probe.d.ts +4 -0
- package/dist/core/mcp-probe.js +1 -1
- package/dist/core/mcp-server-link.d.ts +5 -2
- package/dist/core/mcp-server-spec.d.ts +46 -18
- package/dist/core/mcp-server-spec.js +1 -18
- package/dist/core/mcp.d.ts +6 -14
- package/dist/core/mcp.js +77 -37
- package/dist/core/memory-engine/scope-contract.d.ts +45 -4
- package/dist/core/memory-engine/scope-contract.js +80 -11
- package/dist/core/mode-policy.d.ts +8 -5
- package/dist/core/mode-policy.js +5 -4
- package/dist/core/mods/host.js +1 -1
- package/dist/core/mods/kernel/dispatch.d.ts +3 -0
- package/dist/core/mods/kernel/dispatch.js +10 -8
- package/dist/core/mods/kernel/events.d.ts +5 -1
- package/dist/core/mods/kernel/events.js +1 -1
- package/dist/core/mods/kernel/registration.d.ts +5 -1
- package/dist/core/mods/kernel/site-rules.d.ts +3 -0
- package/dist/core/mods/kernel/site-rules.js +10 -0
- package/dist/core/mods/package.d.ts +23 -0
- package/dist/core/mods/package.js +118 -0
- package/dist/core/mods/stations.d.ts +2 -2
- package/dist/core/mods/stations.js +1 -1
- package/dist/core/mods/tool-call.d.ts +5 -1
- package/dist/core/mods/tool-call.js +12 -3
- package/dist/core/path-guard-rows.d.ts +20 -0
- package/dist/core/path-guard-rows.js +11 -0
- package/dist/core/peer-seat.d.ts +186 -0
- package/dist/core/peer-seat.js +307 -0
- package/dist/core/present-plan-tool.d.ts +10 -1
- package/dist/core/present-plan-tool.js +46 -2
- package/dist/core/protocol-http-auth.d.ts +40 -29
- package/dist/core/protocol-http-auth.js +30 -32
- package/dist/core/redaction-marks.d.ts +16 -0
- package/dist/core/redaction-marks.js +16 -0
- package/dist/core/redaction-passes.js +8 -9
- package/dist/core/retention.d.ts +41 -15
- package/dist/core/retention.js +59 -12
- package/dist/core/rewrite-rejudge.d.ts +37 -0
- package/dist/core/rewrite-rejudge.js +40 -0
- package/dist/core/run-origin.d.ts +40 -0
- package/dist/core/run-origin.js +37 -0
- package/dist/core/runner/advertised-writable-dirs.d.ts +57 -38
- package/dist/core/runner/advertised-writable-dirs.js +126 -0
- package/dist/core/runner/compaction-restate.d.ts +15 -6
- package/dist/core/runner/compaction-restate.js +28 -2
- package/dist/core/runner/contracts.d.ts +13 -5
- package/dist/core/runner/gate-exit.d.ts +18 -4
- package/dist/core/runner/gate-exit.js +5 -1
- package/dist/core/runner/prepare-caps-and-workflow.d.ts +2 -0
- package/dist/core/runner/prepare-caps-and-workflow.js +1 -1
- package/dist/core/runner/prepare-config-doors.d.ts +8 -5
- package/dist/core/runner/prepare-config-doors.js +12 -10
- package/dist/core/runner/prepare-delegation-surface.d.ts +4 -0
- package/dist/core/runner/prepare-delegation-surface.js +15 -18
- package/dist/core/runner/prepare-gate-stations.js +1 -1
- package/dist/core/runner/prepare-hands-readface.d.ts +7 -3
- package/dist/core/runner/prepare-hands-readface.js +4 -1
- package/dist/core/runner/prepare-memory.d.ts +3 -0
- package/dist/core/runner/prepare-memory.js +19 -23
- package/dist/core/runner/prepare-policy-chain.d.ts +4 -4
- package/dist/core/runner/prepare-policy-chain.js +2 -2
- package/dist/core/runner/prepare-protocol-tools.d.ts +4 -2
- package/dist/core/runner/prepare-protocol-tools.js +3 -3
- package/dist/core/runner/prepare-suspend-saga.d.ts +2 -0
- package/dist/core/runner/prepare-suspend-saga.js +2 -1
- package/dist/core/runner/prepare-task.js +25 -12
- package/dist/core/runner/prepare-wiring-manifest.d.ts +1 -0
- package/dist/core/runner/prepare-wiring-manifest.js +2 -2
- package/dist/core/runner/prepare-workspace-restore.d.ts +3 -0
- package/dist/core/runner/prepare-workspace-restore.js +4 -4
- package/dist/core/runner/resume-admission.js +1 -1
- package/dist/core/runner/resume-checkpoint-screen.js +2 -0
- package/dist/core/runner/run-harness-handlers.d.ts +3 -4
- package/dist/core/runner/run-harness-handlers.js +1 -8
- package/dist/core/runner/run-identity-wiring.js +1 -0
- package/dist/core/runner/run-leg.js +5 -1
- package/dist/core/runner/run-notification-lane.js +3 -0
- package/dist/core/runner/terminal-account.js +3 -3
- package/dist/core/runner/tool-end-body.js +1 -0
- package/dist/core/runner/tool-occurrence.d.ts +4 -1
- package/dist/core/runner/tool-occurrence.js +2 -1
- package/dist/core/runner/write-protection-station.d.ts +8 -2
- package/dist/core/runner/write-protection-station.js +2 -0
- package/dist/core/runner-deps.d.ts +2 -2
- package/dist/core/sensitive-path-policy.js +6 -7
- package/dist/core/session-log-transfer.d.ts +24 -0
- package/dist/core/session-log-transfer.js +166 -0
- package/dist/core/session-store.d.ts +84 -19
- package/dist/core/session-store.js +162 -24
- package/dist/core/session.d.ts +92 -1
- package/dist/core/shell-read-silence.js +1 -0
- package/dist/core/steer-replay.d.ts +4 -2
- package/dist/core/steer-replay.js +2 -1
- package/dist/core/store-contracts/checkpoint-store-contract.js +17 -0
- package/dist/core/store-contracts/session-repo-contract.js +112 -5
- package/dist/core/store-contracts/session-store-contract.d.ts +4 -1
- package/dist/core/store-contracts/session-store-contract.js +236 -2
- package/dist/core/task-event.d.ts +68 -8
- package/dist/core/task-registry-shared.d.ts +2 -1
- package/dist/core/task-spec.d.ts +37 -0
- package/dist/core/task-stream.d.ts +2 -1
- package/dist/core/terminal-cause.d.ts +1 -1
- package/dist/core/tool-catalog-entries.js +1 -0
- package/dist/core/tool-result-store.d.ts +17 -10
- package/dist/core/tool-result-store.js +3 -1
- package/dist/core/types.d.ts +1 -1
- package/dist/core/untrusted-egress.d.ts +0 -2
- package/dist/core/untrusted-egress.js +10 -14
- package/dist/core/wiring-manifest.d.ts +12 -3
- package/dist/core/wiring-manifest.js +4 -3
- package/dist/core/workspace-snapshot.d.ts +44 -0
- package/dist/core/workspace-snapshot.js +34 -0
- package/dist/core/write-protect.d.ts +43 -3
- package/dist/core/write-protect.js +43 -16
- package/dist/engine/execution-env/node-execution-env.js +15 -11
- package/dist/engine/execution-env/symlink-chain.d.ts +7 -5
- package/dist/engine/harness/agent-harness.d.ts +5 -0
- package/dist/engine/harness/agent-harness.js +2 -0
- package/dist/engine/harness/types.d.ts +48 -7
- package/dist/engine/harness/types.js +5 -1
- package/dist/engine/llm/types.d.ts +12 -3
- package/dist/engine/llm/types.js +1 -1
- package/dist/engine/session/import-validate.js +3 -0
- package/dist/engine/session/memory-repo.d.ts +17 -9
- package/dist/engine/session/memory-repo.js +44 -34
- package/dist/engine/session/repo-utils.d.ts +22 -4
- package/dist/engine/session/repo-utils.js +49 -1
- package/dist/engine/session/storage-base.d.ts +10 -1
- package/dist/engine/session/storage-base.js +7 -1
- package/dist/index.d.ts +13 -6
- package/dist/index.js +10 -4
- package/dist/internal/harness-types.d.ts +1 -1
- package/dist/internal/harness.d.ts +2 -2
- package/dist/internal/harness.js +2 -2
- package/dist/stores/file/index.d.ts +4 -1
- package/dist/stores/file/index.js +4 -1
- package/dist/stores/file/session-store.d.ts +15 -8
- package/dist/stores/file/session-store.js +28 -13
- package/dist/tools/fs/fs-write.d.ts +6 -3
- package/dist/tools/fs/fs-write.js +8 -7
- package/dist/tools/fs/index.d.ts +3 -0
- package/dist/tools/fs/index.js +1 -1
- package/dist/tools/fs/safety.js +40 -8
- package/dist/tools/fs/search.js +6 -24
- package/dist/tools/fs/shell-read-check.d.ts +8 -1
- package/dist/tools/fs/shell-read-check.js +59 -15
- package/dist/tools/fs/shell-read-establish.js +2 -1
- package/dist/tools/fs/shell-read-identity.js +1 -1
- package/dist/tools/fs/shell-read-judge.d.ts +10 -1
- package/dist/tools/worktree.d.ts +42 -15
- package/dist/tools/worktree.js +60 -32
- package/package.json +4 -2
- package/test/export-surface.snapshot.json +63 -1
- package/wire-changes/core-stores-sql@7.58.0.json +29 -0
- package/wire-changes/core@7.58.0.json +2188 -0
- package/wire-changes/mods-kernel@7.58.0.json +81 -0
- package/wire-changes/primitives@7.58.0.json +29 -0
- package/dist/primitives/credential-ref.d.ts +0 -89
- package/dist/primitives/credential-ref.js +0 -125
- package/dist/primitives/index.d.ts +0 -2
- package/dist/primitives/index.js +0 -1
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import { InMemorySessionRepo, ParentChainGuard, SessionError, findUnansweredToolBatch, compareOccupants, ownerCell, uuidv7, } from "../internal/harness.js";
|
|
1
|
+
import { InMemorySessionRepo, ParentChainGuard, SessionError, findUnansweredToolBatch, compareOccupants, ownerCell, sessionLogMarkOf, uuidv7, } from "../internal/harness.js";
|
|
2
2
|
import { isSessionConflict, readSessionListQuery } from "./session.js";
|
|
3
|
+
import { readRetentionDeclaration } from "./retention.js";
|
|
3
4
|
import { readMember } from "./thrown-value.js";
|
|
5
|
+
import { principalNameFault, principalNameOf } from "./principal-name.js";
|
|
6
|
+
import { receiptRefusal, receiveOwnProperties } from "./own-property-receipt.js";
|
|
7
|
+
import { repoLogTransfer } from "./session-log-transfer.js";
|
|
4
8
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
5
9
|
import { SESSION_DEFAULT_TTL_DAYS } from "../config/defaults.js";
|
|
6
10
|
export { SESSION_DEFAULT_TTL_DAYS };
|
|
@@ -19,22 +23,28 @@ export function admitSessionPlacement(sessionId, persisted, request, capability)
|
|
|
19
23
|
}
|
|
20
24
|
}
|
|
21
25
|
export class TtlSessionStore {
|
|
22
|
-
retention
|
|
26
|
+
retention;
|
|
23
27
|
durability;
|
|
24
28
|
placements;
|
|
25
29
|
listPlaced;
|
|
26
30
|
headOf;
|
|
27
31
|
repoHead;
|
|
32
|
+
logTransfer;
|
|
33
|
+
delete;
|
|
34
|
+
judgesRows = false;
|
|
35
|
+
beforeRowDelete;
|
|
28
36
|
repo;
|
|
29
37
|
entries = new Map();
|
|
30
38
|
pending = new Map();
|
|
31
39
|
pendingPlacedDeletes = new Set();
|
|
32
40
|
releasing = new Map();
|
|
41
|
+
resolutions = new Map();
|
|
33
42
|
pinned = new Set();
|
|
34
43
|
defaultTtlMs;
|
|
35
44
|
evictPolicy;
|
|
36
45
|
sweepTimer;
|
|
37
46
|
constructor(opts = {}) {
|
|
47
|
+
this.retention = readRetentionDeclaration("TtlSessionStoreOptions.retention", opts.retention);
|
|
38
48
|
this.repo = opts.repo ?? new InMemorySessionRepo();
|
|
39
49
|
const durability = opts.durability ?? (opts.repo === undefined ? "process-local" : undefined);
|
|
40
50
|
if (durability !== undefined)
|
|
@@ -49,9 +59,28 @@ export class TtlSessionStore {
|
|
|
49
59
|
}
|
|
50
60
|
const repoHeadOf = this.repo.headOf;
|
|
51
61
|
if (typeof repoHeadOf === "function") {
|
|
52
|
-
|
|
53
|
-
this.
|
|
62
|
+
const repoHead = (sessionId) => repoHeadOf.call(this.repo, sessionId);
|
|
63
|
+
this.repoHead = repoHead;
|
|
64
|
+
this.headOf = repoHead;
|
|
65
|
+
const { readRow, importEntries } = this.repo;
|
|
66
|
+
if (typeof readRow === "function") {
|
|
67
|
+
this.judgesRows = true;
|
|
68
|
+
this.delete = (sessionId, guard) => this.deleteOwned(sessionId, guard);
|
|
69
|
+
}
|
|
70
|
+
if (typeof readRow === "function" && typeof importEntries === "function") {
|
|
71
|
+
this.logTransfer = repoLogTransfer({
|
|
72
|
+
readRow: (id) => readRow.call(this.repo, id),
|
|
73
|
+
importEntries: (id, owner, entries, options) => importEntries.call(this.repo, id, owner, entries, options),
|
|
74
|
+
holding: (id, work) => this.holdingId(id, () => {
|
|
75
|
+
this.entries.delete(id);
|
|
76
|
+
return work();
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
54
80
|
}
|
|
81
|
+
const beforeRowDelete = readStoreHooks(opts.hooks);
|
|
82
|
+
if (beforeRowDelete !== undefined)
|
|
83
|
+
this.beforeRowDelete = (sessionId) => Reflect.apply(beforeRowDelete, opts.hooks, [sessionId]);
|
|
55
84
|
if (opts.sweepIntervalMs !== undefined && opts.sweepIntervalMs > 0) {
|
|
56
85
|
this.sweepTimer = setInterval(() => this.sweep(), opts.sweepIntervalMs);
|
|
57
86
|
this.sweepTimer.unref?.();
|
|
@@ -60,12 +89,10 @@ export class TtlSessionStore {
|
|
|
60
89
|
async acquire(sessionId, opts) {
|
|
61
90
|
this.sweep();
|
|
62
91
|
if (sessionId) {
|
|
63
|
-
|
|
64
|
-
if (inflightRelease !== undefined)
|
|
65
|
-
await inflightRelease;
|
|
92
|
+
await this.settledHolds(sessionId);
|
|
66
93
|
const existing = this.entries.get(sessionId);
|
|
67
94
|
if (existing) {
|
|
68
|
-
if (await this.
|
|
95
|
+
if ((await this.standingRow(sessionId, existing)) !== undefined) {
|
|
69
96
|
this.assertPlacementAdmission(sessionId, existing.placement, opts);
|
|
70
97
|
existing.lastActiveAt = Date.now();
|
|
71
98
|
return { session: existing.session, sessionId };
|
|
@@ -105,11 +132,11 @@ export class TtlSessionStore {
|
|
|
105
132
|
return false;
|
|
106
133
|
return this.repoHead === undefined;
|
|
107
134
|
}
|
|
108
|
-
async
|
|
135
|
+
async standingRow(sessionId, cached) {
|
|
109
136
|
if (this.repoHead === undefined)
|
|
110
|
-
return
|
|
111
|
-
const [shown, row] = await Promise.all([cached.session.getMetadata(), this.repoHead(sessionId)]);
|
|
112
|
-
return this.viewStandsFor(shown, row);
|
|
137
|
+
return null;
|
|
138
|
+
const [shown, leaf, row] = await Promise.all([cached.session.getMetadata(), cached.session.getStorage().getLeafId(), this.repoHead(sessionId)]);
|
|
139
|
+
return this.viewStandsFor(shown, row) && row !== undefined && row.leafId === leaf ? row : undefined;
|
|
113
140
|
}
|
|
114
141
|
assertPlacementAdmission(sessionId, placement, opts) {
|
|
115
142
|
const store = this;
|
|
@@ -120,6 +147,28 @@ export class TtlSessionStore {
|
|
|
120
147
|
});
|
|
121
148
|
}
|
|
122
149
|
async openOrCreate(id, requireExisting = false, placement) {
|
|
150
|
+
for (;;) {
|
|
151
|
+
const watch = { overtaken: this.releasing.has(id) };
|
|
152
|
+
const watching = this.resolutions.get(id) ?? new Set();
|
|
153
|
+
watching.add(watch);
|
|
154
|
+
this.resolutions.set(id, watching);
|
|
155
|
+
let session;
|
|
156
|
+
try {
|
|
157
|
+
session = await this.resolveRow(id, requireExisting, placement);
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
watching.delete(watch);
|
|
161
|
+
if (watching.size === 0 && this.resolutions.get(id) === watching)
|
|
162
|
+
this.resolutions.delete(id);
|
|
163
|
+
}
|
|
164
|
+
if (!watch.overtaken)
|
|
165
|
+
return session;
|
|
166
|
+
if (this.entries.get(id)?.session === session)
|
|
167
|
+
this.entries.delete(id);
|
|
168
|
+
await this.settledHolds(id);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async resolveRow(id, requireExisting, placement) {
|
|
123
172
|
try {
|
|
124
173
|
const session = await this.repo.open({ id, createdAt: "" });
|
|
125
174
|
const meta = await session.getMetadata();
|
|
@@ -234,7 +283,14 @@ export class TtlSessionStore {
|
|
|
234
283
|
return;
|
|
235
284
|
return this.holdingId(sessionId, () => this.releaseRow(sessionId, cached));
|
|
236
285
|
}
|
|
286
|
+
async settledHolds(sessionId) {
|
|
287
|
+
const held = this.releasing.get(sessionId);
|
|
288
|
+
if (held !== undefined)
|
|
289
|
+
await held;
|
|
290
|
+
}
|
|
237
291
|
async holdingId(sessionId, work) {
|
|
292
|
+
for (const watch of this.resolutions.get(sessionId) ?? [])
|
|
293
|
+
watch.overtaken = true;
|
|
238
294
|
const prior = this.releasing.get(sessionId);
|
|
239
295
|
let settle;
|
|
240
296
|
const gate = new Promise((r) => (settle = r));
|
|
@@ -242,7 +298,7 @@ export class TtlSessionStore {
|
|
|
242
298
|
try {
|
|
243
299
|
if (prior !== undefined)
|
|
244
300
|
await prior;
|
|
245
|
-
await work();
|
|
301
|
+
return await work();
|
|
246
302
|
}
|
|
247
303
|
finally {
|
|
248
304
|
if (this.releasing.get(sessionId) === gate)
|
|
@@ -253,17 +309,75 @@ export class TtlSessionStore {
|
|
|
253
309
|
async releaseRow(sessionId, cached) {
|
|
254
310
|
if (cached?.placement !== undefined)
|
|
255
311
|
this.pendingPlacedDeletes.add(sessionId);
|
|
256
|
-
const
|
|
312
|
+
const standing = cached !== undefined ? await this.standingRow(sessionId, cached) : undefined;
|
|
313
|
+
const view = standing !== undefined ? cached : undefined;
|
|
314
|
+
const judged = view === undefined && this.repoHead !== undefined && this.rowReadMatters(sessionId) ? await this.repoHead(sessionId) : undefined;
|
|
257
315
|
const placed = view !== undefined ? view.placement !== undefined : await this.rowIsPlaced(sessionId);
|
|
258
316
|
if (view === undefined && !placed)
|
|
259
317
|
return;
|
|
318
|
+
if (!placed) {
|
|
319
|
+
await this.deleteRow(await view.session.getMetadata(), this.idleViewExpectation(standing, view));
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
260
322
|
const meta = view !== undefined ? await view.session.getMetadata() : { id: sessionId, createdAt: "" };
|
|
261
|
-
|
|
262
|
-
return this.repo.delete(meta);
|
|
323
|
+
const occupant = view !== undefined ? standing : (judged ?? null);
|
|
263
324
|
this.pendingPlacedDeletes.add(sessionId);
|
|
264
|
-
await this.
|
|
325
|
+
await this.deleteRow(meta, this.judgesRows ? async () => ({ row: occupant }) : undefined);
|
|
265
326
|
this.pendingPlacedDeletes.delete(sessionId);
|
|
266
327
|
}
|
|
328
|
+
idleViewExpectation(standing, view) {
|
|
329
|
+
if (!this.judgesRows || standing === null)
|
|
330
|
+
return undefined;
|
|
331
|
+
return async () => ({ row: standing, log: sessionLogMarkOf(await view.session.getStorage().getEntries()) });
|
|
332
|
+
}
|
|
333
|
+
async deleteRow(meta, expect) {
|
|
334
|
+
if (this.beforeRowDelete !== undefined)
|
|
335
|
+
await this.beforeRowDelete(meta.id);
|
|
336
|
+
if (expect === undefined) {
|
|
337
|
+
await this.repo.delete(meta);
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
try {
|
|
341
|
+
await this.repo.delete(meta, await expect());
|
|
342
|
+
}
|
|
343
|
+
catch (err) {
|
|
344
|
+
if (isSessionConflict(err))
|
|
345
|
+
return false;
|
|
346
|
+
throw err;
|
|
347
|
+
}
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
async deleteOwned(sessionId, guard) {
|
|
351
|
+
const want = readDeleteGuard(guard);
|
|
352
|
+
return this.holdingId(sessionId, async () => {
|
|
353
|
+
this.entries.delete(sessionId);
|
|
354
|
+
const head = await this.repoHead(sessionId);
|
|
355
|
+
if (head === undefined || head.owner !== want)
|
|
356
|
+
return false;
|
|
357
|
+
if ((await this.readPlacement(sessionId)) !== undefined)
|
|
358
|
+
return false;
|
|
359
|
+
if (!(await this.deleteRow({ id: sessionId, createdAt: "" }, async () => ({ row: head }))))
|
|
360
|
+
return false;
|
|
361
|
+
this.pinned.delete(sessionId);
|
|
362
|
+
return true;
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
async rowMetadata(sessionId) {
|
|
366
|
+
try {
|
|
367
|
+
return await (await this.repo.open({ id: sessionId, createdAt: "" })).getMetadata();
|
|
368
|
+
}
|
|
369
|
+
catch (err) {
|
|
370
|
+
if (isNotFound(err))
|
|
371
|
+
return undefined;
|
|
372
|
+
throw err;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async readPlacement(sessionId) {
|
|
376
|
+
const probe = this.placementProbe();
|
|
377
|
+
if (probe !== undefined)
|
|
378
|
+
return probe(sessionId);
|
|
379
|
+
return (await this.rowMetadata(sessionId))?.placement;
|
|
380
|
+
}
|
|
267
381
|
placementProbe() {
|
|
268
382
|
const probe = this.repo.placementOf;
|
|
269
383
|
return typeof probe === "function" ? probe.bind(this.repo) : undefined;
|
|
@@ -274,10 +388,9 @@ export class TtlSessionStore {
|
|
|
274
388
|
async rowIsPlaced(sessionId) {
|
|
275
389
|
if (!this.rowReadMatters(sessionId))
|
|
276
390
|
return false;
|
|
277
|
-
const probe = this.placementProbe();
|
|
278
391
|
let placement;
|
|
279
392
|
try {
|
|
280
|
-
placement =
|
|
393
|
+
placement = await this.readPlacement(sessionId);
|
|
281
394
|
}
|
|
282
395
|
catch (err) {
|
|
283
396
|
if (!isNotFound(err))
|
|
@@ -296,9 +409,7 @@ export class TtlSessionStore {
|
|
|
296
409
|
this.pinned.delete(sessionId);
|
|
297
410
|
}
|
|
298
411
|
async register(sessionId, owner) {
|
|
299
|
-
|
|
300
|
-
if (inflightRelease !== undefined)
|
|
301
|
-
await inflightRelease;
|
|
412
|
+
await this.settledHolds(sessionId);
|
|
302
413
|
if (await this.hasRow(sessionId))
|
|
303
414
|
return;
|
|
304
415
|
if (owner === undefined)
|
|
@@ -335,8 +446,9 @@ export class TtlSessionStore {
|
|
|
335
446
|
this.entries.delete(id);
|
|
336
447
|
if (this.evictPolicy === "delete" && e.placement === undefined) {
|
|
337
448
|
void this.holdingId(id, async () => {
|
|
338
|
-
|
|
339
|
-
|
|
449
|
+
const standing = await this.standingRow(id, e);
|
|
450
|
+
if (standing !== undefined)
|
|
451
|
+
await this.deleteRow({ id, createdAt: "" }, this.idleViewExpectation(standing, e));
|
|
340
452
|
}).catch(() => { });
|
|
341
453
|
}
|
|
342
454
|
}
|
|
@@ -379,6 +491,32 @@ function newestFirst(a, b) {
|
|
|
379
491
|
return b.lastActiveAt - a.lastActiveAt;
|
|
380
492
|
return a.sessionId < b.sessionId ? 1 : a.sessionId > b.sessionId ? -1 : 0;
|
|
381
493
|
}
|
|
494
|
+
function readStoreHooks(hooks) {
|
|
495
|
+
if (hooks === undefined)
|
|
496
|
+
return undefined;
|
|
497
|
+
if (typeof hooks !== "object" || hooks === null)
|
|
498
|
+
throw receiptRefusal(`TtlSessionStoreOptions.hooks must be an object when given, got ${hooks === null ? "null" : typeof hooks}`);
|
|
499
|
+
const got = receiveOwnProperties(hooks, { subject: "TtlSessionStoreOptions.hooks", unit: "hook", moment: "at construction" });
|
|
500
|
+
for (const name of Object.keys(got)) {
|
|
501
|
+
if (name !== "beforeSessionDelete")
|
|
502
|
+
throw receiptRefusal(`TtlSessionStoreOptions.hooks.${name} is not a hook this store runs (it runs beforeSessionDelete)`);
|
|
503
|
+
}
|
|
504
|
+
const hook = got["beforeSessionDelete"];
|
|
505
|
+
if (hook !== undefined && typeof hook !== "function")
|
|
506
|
+
throw receiptRefusal(`TtlSessionStoreOptions.hooks.beforeSessionDelete must be a function when given, got ${hook === null ? "null" : typeof hook}`);
|
|
507
|
+
return hook;
|
|
508
|
+
}
|
|
509
|
+
function readDeleteGuard(guard) {
|
|
510
|
+
if (typeof guard !== "object" || guard === null)
|
|
511
|
+
throw new TypeError(`SessionStore.delete: the guard must be an object { owner }, got ${guard === null ? "null" : typeof guard}`);
|
|
512
|
+
const owner = readMember(guard, "owner");
|
|
513
|
+
if (owner === null)
|
|
514
|
+
return null;
|
|
515
|
+
const name = principalNameOf(owner);
|
|
516
|
+
if (name === undefined)
|
|
517
|
+
throw new TypeError(`SessionStore.delete: guard.owner must be a principal name or null (anonymous), got ${principalNameFault(owner)}`);
|
|
518
|
+
return name;
|
|
519
|
+
}
|
|
382
520
|
function isNotFound(err) {
|
|
383
521
|
return readMember(err, "code") === "not_found";
|
|
384
522
|
}
|
package/dist/core/session.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type { SessionWriteOptions } from "../internal/harness.js";
|
|
|
8
8
|
import type { Session } from "../internal/harness.js";
|
|
9
9
|
import type { SessionHead, SessionTreeEntry } from "../internal/harness.js";
|
|
10
10
|
export type { SessionPlacementRecord } from "../internal/harness.js";
|
|
11
|
-
export type { SessionHead } from "../internal/harness.js";
|
|
11
|
+
export type { SessionHead, SessionLogMark, SessionRowExpectation, SessionRowRead } from "../internal/harness.js";
|
|
12
12
|
/** True when `err` is a session optimistic-lock conflict (a concurrent writer won the branch leaf). Both
|
|
13
13
|
* halves read what a HOST `SessionStorage` threw, so both are guarded — unreadable is not a conflict,
|
|
14
14
|
* never a raise inside the classify-and-rethrow guard. The class test is KEPT: widening the verdict to
|
|
@@ -340,11 +340,102 @@ export interface SessionStore<S extends SessionStoreSummary = SessionStoreSummar
|
|
|
340
340
|
* authorization decision — the caller re-reads {@link headOf} and judges there. The owner lives and dies with its row.
|
|
341
341
|
*/
|
|
342
342
|
register?(sessionId: string, owner: string | null): Promise<void>;
|
|
343
|
+
/**
|
|
344
|
+
* The storage half of moving a session's log between backends (session-sync): read it out ({@link SessionLogTransfer.listEntryIds},
|
|
345
|
+
* {@link SessionLogTransfer.exportStream}) and replace it whole through a staging the readers never see
|
|
346
|
+
* ({@link SessionLogTransfer.beginReplace}). One write law for every store: a commit onto an existing row keeps that row's occupant and
|
|
347
|
+
* owner and lands only on a row that is already the committer's (compared null-safe; another owner's row is refused `conflict`, a placed
|
|
348
|
+
* row `placement_refused`, nothing written); onto an absent id it creates the row recording the committer and a fresh occupant; every
|
|
349
|
+
* entry passes the engine's import gate ({@link StreamingImportValidator}); and the owner check, the relation the committer judges, the
|
|
350
|
+
* write and the head the commit answers are one step — against every writer of the row, not only this instance's: the commit reads the
|
|
351
|
+
* row once (owner, log and head of one generation), `decide` weighs that log, and the write lands only while the row is still that
|
|
352
|
+
* generation with that log (a write through another instance or the repo since then refuses the commit `conflict`, nothing written);
|
|
353
|
+
* the head answered is the one the write committed (on `"keep"`, the one read). A refusal, or a throw from `decide`, writes nothing.
|
|
354
|
+
* **Optional**: absent = this store does not take part in a cross-backend move.
|
|
355
|
+
*/
|
|
356
|
+
readonly logTransfer?: SessionLogTransfer;
|
|
357
|
+
/**
|
|
358
|
+
* Delete a session at its owner's request — the row and its whole log. Only when the row's owner equals `guard.owner` (null-safe:
|
|
359
|
+
* `null` deletes only an anonymous row); a placed row is not deleted here (its deletion is `release`'s, through the joint reap). `true`
|
|
360
|
+
* = this call deleted the row; `false` = there was no row, the guard did not hold, or the row is placed — nothing was deleted, so a
|
|
361
|
+
* repeat is harmless. The id answers nothing afterwards, and a later create under it is a new occupant. A store wired with
|
|
362
|
+
* {@link SessionStoreHooks.beforeSessionDelete} runs it first and deletes nothing when it rejects (the call rejects with it).
|
|
363
|
+
* **Optional**: absent = this store has no owner-guarded delete.
|
|
364
|
+
*/
|
|
365
|
+
delete?(sessionId: string, guard: {
|
|
366
|
+
owner: string | null;
|
|
367
|
+
}): Promise<boolean>;
|
|
343
368
|
/** Number of live/cached sessions (best-effort). */
|
|
344
369
|
readonly size: number;
|
|
345
370
|
/** Release resources (timers, connections). Best-effort; must not throw. */
|
|
346
371
|
dispose(): void | Promise<void>;
|
|
347
372
|
}
|
|
373
|
+
/** What {@link SessionLogTransfer.exportStream} reads: `afterSeq` resumes after that 0-based position of the oldest-first log (absent
|
|
374
|
+
* = from the first entry; an integer ≥ -1); `batchSize` is how many entries a store reads per page (an integer ≥ 1; absent = the store's
|
|
375
|
+
* own page) — a store holding the log whole reads it once whatever the page, the stream is the same. A bad value is refused. */
|
|
376
|
+
export interface SessionLogExportOptions {
|
|
377
|
+
afterSeq?: number;
|
|
378
|
+
batchSize?: number;
|
|
379
|
+
}
|
|
380
|
+
/** The read and replace halves of {@link SessionStore.logTransfer}. */
|
|
381
|
+
export interface SessionLogTransfer {
|
|
382
|
+
/** The ids of the session's whole log, oldest-first (no compaction floor applied); `null` = no such session. */
|
|
383
|
+
listEntryIds(sessionId: string): Promise<readonly string[] | null>;
|
|
384
|
+
/** The session's whole log, oldest-first, as a stream; `null` = no such session (see {@link SessionLogExportOptions}). */
|
|
385
|
+
exportStream(sessionId: string, opts?: SessionLogExportOptions): Promise<AsyncIterable<SessionTreeEntry> | null>;
|
|
386
|
+
/** Open a staging for a replacement of `sessionId`'s log: entries appended to it are invisible to every reader until its commit.
|
|
387
|
+
* `preserveActorAssertions` is the import gate's trusted-source declaration (see {@link StreamingImportValidator}), read here
|
|
388
|
+
* because the gate reads each batch as it is appended. */
|
|
389
|
+
beginReplace(sessionId: string, opts?: {
|
|
390
|
+
preserveActorAssertions?: boolean;
|
|
391
|
+
}): SessionLogReplace;
|
|
392
|
+
}
|
|
393
|
+
/** What a commit's `decide` sees, inside the commit's one step: the destination's entry ids as they stand now (`null` = no row) and the
|
|
394
|
+
* staged ids, and a read of either side's entries by id. */
|
|
395
|
+
export interface SessionLogCommitView {
|
|
396
|
+
readonly current: readonly string[] | null;
|
|
397
|
+
readonly staged: readonly string[];
|
|
398
|
+
read(side: "current" | "staged", ids: readonly string[]): Promise<SessionTreeEntry[]>;
|
|
399
|
+
}
|
|
400
|
+
/** A commit of a staged replacement. */
|
|
401
|
+
export interface SessionLogCommit {
|
|
402
|
+
/** The committing principal (`null` = anonymous) — the authenticated importer, never a value read from the staged log. */
|
|
403
|
+
owner: string | null;
|
|
404
|
+
/** The committer's judgement of the relation between the two logs, inside the commit's one step: `"replace"` writes the staged log,
|
|
405
|
+
* `"keep"` leaves the destination's log as it is (an identical push); a throw writes nothing and the commit rejects with it. */
|
|
406
|
+
decide(view: SessionLogCommitView): "replace" | "keep" | Promise<"replace" | "keep">;
|
|
407
|
+
}
|
|
408
|
+
/** One staging opened by {@link SessionLogTransfer.beginReplace}. After its commit settles (either way) or its abort, it takes nothing more. */
|
|
409
|
+
export interface SessionLogReplace {
|
|
410
|
+
/** An opaque name of this staging. */
|
|
411
|
+
readonly stagingId: string;
|
|
412
|
+
/** Stage a batch, after the batches before it; each entry passes the import gate here, and a refused batch refuses the staging. */
|
|
413
|
+
append(entries: readonly SessionTreeEntry[]): Promise<void>;
|
|
414
|
+
/** The commit point: the write law of {@link SessionStore.logTransfer}, then `decide`, then the write and the head the row holds
|
|
415
|
+
* after it, in one step. Before it settles, no reader sees the staged log; a refusal, or a throw from `decide`, writes nothing; a row
|
|
416
|
+
* written by anyone else since the commit read it refuses it `conflict` (read the row again and stage again). The first commit call
|
|
417
|
+
* ends the staging, whatever its outcome. */
|
|
418
|
+
commit(input: SessionLogCommit): Promise<{
|
|
419
|
+
outcome: "replaced" | "kept";
|
|
420
|
+
head: SessionHead;
|
|
421
|
+
}>;
|
|
422
|
+
/** Drop the staging; nothing is written. Idempotent. */
|
|
423
|
+
abort(): Promise<void>;
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* A deployment's hooks on a session store's row deletions — the cleanup of the deployment's own records keyed by session id (a title, a
|
|
427
|
+
* pin, an instruction baseline). `beforeSessionDelete` runs, and is awaited, before every deletion of a row the store makes (an owner's
|
|
428
|
+
* {@link SessionStore.delete}, a placed `release`, an `evict: "delete"` release or idle sweep); when it rejects the row is not deleted and
|
|
429
|
+
* the deletion fails with it — an owner's `delete` or a placed `release` called again runs both again, while an ordinary release has
|
|
430
|
+
* dropped its view and, like any failed ordinary release, has nothing more to delete. The deletion after the hook lands only on the row
|
|
431
|
+
* the store judged (the repo judges it in the step that removes the row — another writer may have deleted the row, created another under
|
|
432
|
+
* the id, or written it while the hook ran); a row that is no longer the one judged is not deleted. The row is still there while the hook
|
|
433
|
+
* runs, so no create under the same id can meet the deployment's records for the occupant being deleted. Called with the id only; it
|
|
434
|
+
* must be idempotent.
|
|
435
|
+
*/
|
|
436
|
+
export interface SessionStoreHooks {
|
|
437
|
+
beforeSessionDelete?(sessionId: string): void | Promise<void>;
|
|
438
|
+
}
|
|
348
439
|
/** Structural detection: does this store expose the optional {@link SessionStore.fork}? Mounts the fork
|
|
349
440
|
* capability only when true (INERT otherwise — mirrors `hasScheduler`/`hasBackgroundShell`). */
|
|
350
441
|
export declare function hasSessionFork(store: SessionStore): store is SessionStore & {
|
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
* steer under its key — the parked queue (`appendPendingSteer`) and the live verb's accepted-input domain — and that
|
|
4
4
|
* one answer feeds the replay decision, the refusal's machine-readable `detail.differs` and its sentence.
|
|
5
5
|
*
|
|
6
|
-
* Both seats compare the record the CALLER sent (text, trusted, tier, actor) — never the delivered bytes: the live
|
|
6
|
+
* Both seats compare the record the CALLER sent (text, trusted, tier, actor, run origin) — never the delivered bytes: the live
|
|
7
7
|
* delivery folds `trusted` (the framing) and the actor (the speaker label) into its payload, so comparing payloads
|
|
8
8
|
* would report one changed facet as two.
|
|
9
9
|
*/
|
|
10
10
|
import type { ActorAssertion } from "../internal/llm.js";
|
|
11
11
|
import { type SystemInjectionPriority } from "./task-notification.js";
|
|
12
|
+
import type { RunOrigin } from "./run-origin.js";
|
|
12
13
|
/** The facets a steer's replay identity is made of, in the order a refusal lists them. Closed. */
|
|
13
|
-
export declare const STEER_REPLAY_FACETS: readonly ["text", "trusted", "priority", "actor"];
|
|
14
|
+
export declare const STEER_REPLAY_FACETS: readonly ["text", "trusted", "priority", "actor", "runOrigin"];
|
|
14
15
|
export type SteerReplayFacet = (typeof STEER_REPLAY_FACETS)[number];
|
|
15
16
|
/** What a seat remembers a steer by: the caller's own statement, as validated. The parked entry and the live park
|
|
16
17
|
* record both are this shape. An absent `priority` reads as `"next"` ({@link steerPriorityOf}). */
|
|
@@ -19,6 +20,7 @@ export interface SteerReplayRecord {
|
|
|
19
20
|
readonly trusted: boolean;
|
|
20
21
|
readonly priority?: SystemInjectionPriority;
|
|
21
22
|
readonly actor?: ActorAssertion;
|
|
23
|
+
readonly runOrigin?: RunOrigin;
|
|
22
24
|
}
|
|
23
25
|
/** The facets `repeat` differs from `held` in, in {@link STEER_REPLAY_FACETS} order. Empty = the idempotent replay. */
|
|
24
26
|
export declare function steerReplayDiffers(held: SteerReplayRecord, repeat: SteerReplayRecord): SteerReplayFacet[];
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { steerPriorityOf } from "./task-notification.js";
|
|
2
|
-
export const STEER_REPLAY_FACETS = Object.freeze(["text", "trusted", "priority", "actor"]);
|
|
2
|
+
export const STEER_REPLAY_FACETS = Object.freeze(["text", "trusted", "priority", "actor", "runOrigin"]);
|
|
3
3
|
const FACET_IS_SAME = {
|
|
4
4
|
text: (a, b) => a.text === b.text,
|
|
5
5
|
trusted: (a, b) => a.trusted === b.trusted,
|
|
6
6
|
priority: (a, b) => steerPriorityOf(a) === steerPriorityOf(b),
|
|
7
7
|
actor: (a, b) => a.actor?.id === b.actor?.id && a.actor?.hostAsserted === b.actor?.hostAsserted && a.actor?.issuer === b.actor?.issuer,
|
|
8
|
+
runOrigin: (a, b) => a.runOrigin === b.runOrigin,
|
|
8
9
|
};
|
|
9
10
|
export function steerReplayDiffers(held, repeat) {
|
|
10
11
|
return STEER_REPLAY_FACETS.filter((facet) => !FACET_IS_SAME[facet](held, repeat));
|
|
@@ -429,6 +429,23 @@ export async function checkpointStoreContract(make, runAssertion, options = {})
|
|
|
429
429
|
const bareBack = (await store.get(bare.token));
|
|
430
430
|
assert.equal("requires" in bareBack || "requirementsDigest" in bareBack, false, "a row without a list or digest must stay without (never [] / null / \"\")");
|
|
431
431
|
});
|
|
432
|
+
run("run origin: a row's `state.runOrigin` survives the round-trip verbatim and rides the listing; absent stays absent; a word outside the set is kept on the row and reads as absent in the listing", async () => {
|
|
433
|
+
const store = make();
|
|
434
|
+
const member = createCheckpointFixture({ token: mintCheckpointToken(), sessionId: "ro-member" });
|
|
435
|
+
Object.assign(member.state, { runOrigin: "scheduled" });
|
|
436
|
+
const absent = createCheckpointFixture({ token: mintCheckpointToken(), sessionId: "ro-absent" });
|
|
437
|
+
const bogus = createCheckpointFixture({ token: mintCheckpointToken(), sessionId: "ro-bogus" });
|
|
438
|
+
Object.assign(bogus.state, { runOrigin: "weekly" });
|
|
439
|
+
for (const cp of [member, absent, bogus])
|
|
440
|
+
await store.put(cp.token, cp);
|
|
441
|
+
const byId = new Map((await store.listByScope("tenant-a")).map((s) => [s.sessionId, s]));
|
|
442
|
+
assert.equal((await store.get(member.token)).state.runOrigin, "scheduled", "the row's runOrigin must survive the round-trip verbatim");
|
|
443
|
+
assert.equal(byId.get("ro-member")?.runOrigin, "scheduled", "the listing must echo the row's runOrigin");
|
|
444
|
+
assert.equal("runOrigin" in (await store.get(absent.token)).state, false, "an absent runOrigin must stay OMITTED on the row, never a fabricated member");
|
|
445
|
+
assert.equal("runOrigin" in byId.get("ro-absent"), false, "absent must project the key OMITTED, not null / empty");
|
|
446
|
+
assert.equal((await store.get(bogus.token)).state.runOrigin, "weekly", "a word outside the set must be kept on the row verbatim");
|
|
447
|
+
assert.equal("runOrigin" in byId.get("ro-bogus"), false, "a word outside the closed set must read as absent in the listing, never project verbatim");
|
|
448
|
+
});
|
|
432
449
|
run("decider pair: a policy-decided allow resolves like a person's — its `policy_allowed` record (party `policy`, the attribution) round-trips verbatim on the resolved winner and as the execution record; the same record is already_recorded, a person's record for that row conflicts", async () => {
|
|
433
450
|
const store = make();
|
|
434
451
|
const cp = createCheckpointFixture();
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
|
+
import { sessionLogMarkOf } from "../../internal/harness.js";
|
|
2
3
|
import { TtlSessionStore } from "../session-store.js";
|
|
3
4
|
import { beginContract } from "./contract-harness.js";
|
|
4
5
|
const SESSION_ENTRY = (id, parentId) => ({
|
|
@@ -142,7 +143,7 @@ export async function sessionRepoContract(make, runAssertion) {
|
|
|
142
143
|
assert.equal((await again.getMetadata()).incarnation, token, "and the same occupant");
|
|
143
144
|
assert.equal((await (await repo.open({ id: A, createdAt: "" })).getMetadata()).owner, "kit-x");
|
|
144
145
|
});
|
|
145
|
-
run("fork records the owner its options name in the fork row, else (absent or null) the source row's; importEntries onto an existing row keeps its metadata whole, onto an absent id records the importer", async () => {
|
|
146
|
+
run("fork records the owner its options name in the fork row, else (absent or null) the source row's; importEntries onto an existing row keeps its metadata whole and lands only when the row is the importer's (another owner's, or an owned row by an anonymous import, is refused conflict with nothing written), onto an absent id records the importer", async () => {
|
|
146
147
|
const repo = make();
|
|
147
148
|
const src = await repo.create({ id: A, owner: "kit-alice" });
|
|
148
149
|
await src.getStorage().appendEntry(SESSION_ENTRY("e1", null));
|
|
@@ -155,15 +156,28 @@ export async function sessionRepoContract(make, runAssertion) {
|
|
|
155
156
|
if (repo.importEntries === undefined)
|
|
156
157
|
return;
|
|
157
158
|
const token = (await src.getMetadata()).incarnation;
|
|
158
|
-
await repo.
|
|
159
|
+
const logOf = async (id) => (await (await repo.open({ id, createdAt: "" })).getStorage().getEntries()).map((e) => e.id);
|
|
160
|
+
const refusedAsConflict = async (owner, what) => {
|
|
161
|
+
let code;
|
|
162
|
+
try {
|
|
163
|
+
await repo.importEntries(A, owner, [SESSION_ENTRY("x1", null)]);
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
code = err?.code;
|
|
167
|
+
}
|
|
168
|
+
assert.equal(code, "conflict", `${what}: an import lands only on the importer's row`);
|
|
169
|
+
assert.deepEqual(await logOf(A), ["e1"], `${what}: a refused import writes nothing`);
|
|
170
|
+
};
|
|
171
|
+
await refusedAsConflict("kit-carol", "an import by another principal");
|
|
172
|
+
await refusedAsConflict(undefined, "an import with no principal onto an owned row");
|
|
173
|
+
await repo.importEntries(A, "kit-alice", [SESSION_ENTRY("n1", null)]);
|
|
159
174
|
const imported = await (await repo.open({ id: A, createdAt: "" })).getMetadata();
|
|
175
|
+
assert.deepEqual(await logOf(A), ["n1"], "the owner's import replaces the log");
|
|
160
176
|
assert.equal(imported.owner, "kit-alice", "the owner is recorded only by the write that creates the row: an import never re-stamps it");
|
|
161
177
|
assert.equal(imported.incarnation, token, "the log is replaced, not the occupant");
|
|
162
|
-
await repo.importEntries(A, undefined, []);
|
|
163
|
-
assert.equal((await (await repo.open({ id: A, createdAt: "" })).getMetadata()).owner, "kit-alice", "an import with no principal never makes an owned row anonymous");
|
|
164
178
|
const cells = (m) => [m.createdAt, m.forkedFrom, m.forkedFromIncarnation, m.incarnation, m.owner, JSON.stringify(m.placement)];
|
|
165
179
|
const forkRow = await (await repo.open({ id: B, createdAt: "" })).getMetadata();
|
|
166
|
-
await repo.importEntries(B, "kit-
|
|
180
|
+
await repo.importEntries(B, "kit-bob", [SESSION_ENTRY("e1", null)]);
|
|
167
181
|
assert.deepEqual(cells(await (await repo.open({ id: B, createdAt: "" })).getMetadata()), cells(forkRow), "the log is replaced, never the row: a fork row keeps its creation time and its lineage");
|
|
168
182
|
const E = "0198fa00-0000-7000-8000-00000000000e";
|
|
169
183
|
await repo.importEntries(E, "kit-carol", [SESSION_ENTRY("e1", null)]);
|
|
@@ -218,6 +232,99 @@ export async function sessionRepoContract(make, runAssertion) {
|
|
|
218
232
|
store.dispose();
|
|
219
233
|
}
|
|
220
234
|
});
|
|
235
|
+
const conflictOf = async (write) => {
|
|
236
|
+
try {
|
|
237
|
+
await write();
|
|
238
|
+
}
|
|
239
|
+
catch (err) {
|
|
240
|
+
return err?.code;
|
|
241
|
+
}
|
|
242
|
+
return "resolved";
|
|
243
|
+
};
|
|
244
|
+
const logIds = async (repo, id) => {
|
|
245
|
+
try {
|
|
246
|
+
return (await (await repo.open({ id, createdAt: "" })).getStorage().getEntries()).map((e) => e.id);
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
return undefined;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
run("importEntries answers the head its write committed — the same head a head read answers right after it", async () => {
|
|
253
|
+
const repo = make();
|
|
254
|
+
if (repo.importEntries === undefined || repo.headOf === undefined)
|
|
255
|
+
return;
|
|
256
|
+
const fresh = await repo.importEntries(A, "kit-alice", [SESSION_ENTRY("e1", null), SESSION_ENTRY("e2", "e1")]);
|
|
257
|
+
assert.deepEqual(fresh, await repo.headOf(A), "an import onto an absent id answers the new row's head");
|
|
258
|
+
assert.equal(fresh.leafId, "e2");
|
|
259
|
+
const again = await repo.importEntries(A, "kit-alice", [SESSION_ENTRY("n1", null)]);
|
|
260
|
+
assert.deepEqual(again, await repo.headOf(A), "an import onto the importer's row answers that row's head after the write");
|
|
261
|
+
assert.equal(again.incarnation, fresh.incarnation, "the same occupant");
|
|
262
|
+
});
|
|
263
|
+
run("readRow (where the repo answers it): no row ⇒ undefined; a row ⇒ its metadata, its whole log oldest-first and the head a head read answers, of one row", async () => {
|
|
264
|
+
const repo = make();
|
|
265
|
+
if (repo.readRow === undefined)
|
|
266
|
+
return;
|
|
267
|
+
assert.equal(await repo.readRow(A), undefined);
|
|
268
|
+
const sess = await repo.create({ id: A, owner: "kit-alice" });
|
|
269
|
+
await sess.getStorage().appendEntry(SESSION_ENTRY("e1", null));
|
|
270
|
+
await sess.getStorage().appendEntry(SESSION_ENTRY("e2", "e1"));
|
|
271
|
+
const row = await repo.readRow(A);
|
|
272
|
+
assert.ok(row !== undefined);
|
|
273
|
+
assert.equal(row.metadata.owner, "kit-alice");
|
|
274
|
+
assert.deepEqual(row.entries.map((e) => e.id), ["e1", "e2"]);
|
|
275
|
+
if (repo.headOf !== undefined)
|
|
276
|
+
assert.deepEqual(row.head, await repo.headOf(A), "the head of the row read is the head read's");
|
|
277
|
+
if (repo.exportEntries !== undefined)
|
|
278
|
+
assert.deepEqual(row.entries, await repo.exportEntries(A), "the log of the row read is the export's");
|
|
279
|
+
});
|
|
280
|
+
run("a conditional import lands only on the row it expects: the row and log as read ⇒ written; after an append, a delete, a delete and create again, or with no row expected while one stands ⇒ conflict, nothing written", async () => {
|
|
281
|
+
const repo = make();
|
|
282
|
+
if (repo.readRow === undefined || repo.importEntries === undefined)
|
|
283
|
+
return;
|
|
284
|
+
const sess = await repo.create({ id: A, owner: "kit-alice" });
|
|
285
|
+
await sess.getStorage().appendEntry(SESSION_ENTRY("e1", null));
|
|
286
|
+
const read = (await repo.readRow(A));
|
|
287
|
+
const asRead = { row: read.head, log: sessionLogMarkOf(read.entries) };
|
|
288
|
+
await sess.getStorage().appendEntry(SESSION_ENTRY("e2", "e1"));
|
|
289
|
+
assert.equal(await conflictOf(() => repo.importEntries(A, "kit-alice", [SESSION_ENTRY("x1", null)], { expect: asRead })), "conflict", "an append since the read: the log is not the one read");
|
|
290
|
+
assert.deepEqual(await logIds(repo, A), ["e1", "e2"], "the refused import wrote nothing");
|
|
291
|
+
assert.equal(await conflictOf(() => repo.importEntries(A, "kit-alice", [SESSION_ENTRY("x1", null)], { expect: { row: null } })), "conflict", "no row expected, a row stands");
|
|
292
|
+
const now = (await repo.readRow(A));
|
|
293
|
+
await repo.importEntries(A, "kit-alice", [SESSION_ENTRY("n1", null)], { expect: { row: now.head, log: sessionLogMarkOf(now.entries) } });
|
|
294
|
+
assert.deepEqual(await logIds(repo, A), ["n1"], "the row and log as read: written");
|
|
295
|
+
const before = (await repo.readRow(A));
|
|
296
|
+
await repo.delete({ id: A, createdAt: "" });
|
|
297
|
+
assert.equal(await conflictOf(() => repo.importEntries(A, "kit-alice", [SESSION_ENTRY("x1", null)], { expect: { row: before.head } })), "conflict", "the row was deleted since the read");
|
|
298
|
+
assert.equal(await logIds(repo, A), undefined, "and no row was created");
|
|
299
|
+
await repo.create({ id: A, owner: "kit-alice" });
|
|
300
|
+
assert.equal(await conflictOf(() => repo.importEntries(A, "kit-alice", [SESSION_ENTRY("x1", null)], { expect: { row: before.head } })), "conflict", "the row was created again: another occupant, same owner");
|
|
301
|
+
assert.deepEqual(await logIds(repo, A), [], "the new row's log is untouched");
|
|
302
|
+
const noShape = { row: before.head, log: "a mark of no shape" };
|
|
303
|
+
await assert.rejects(() => repo.importEntries(A, "kit-alice", [SESSION_ENTRY("x1", null)], { expect: noShape }), TypeError, "an expectation of no shape is refused, never read as none");
|
|
304
|
+
assert.deepEqual(await logIds(repo, A), [], "and wrote nothing");
|
|
305
|
+
});
|
|
306
|
+
run("a conditional delete removes only the row it expects: another occupant or no row ⇒ conflict, nothing removed; an occupant-only expectation survives an append, an expectation binding the log does not", async () => {
|
|
307
|
+
const repo = make();
|
|
308
|
+
if (repo.readRow === undefined)
|
|
309
|
+
return;
|
|
310
|
+
const sess = await repo.create({ id: A, owner: "kit-alice" });
|
|
311
|
+
await sess.getStorage().appendEntry(SESSION_ENTRY("e1", null));
|
|
312
|
+
const read = (await repo.readRow(A));
|
|
313
|
+
await sess.getStorage().appendEntry(SESSION_ENTRY("e2", "e1"));
|
|
314
|
+
assert.equal(await conflictOf(() => repo.delete({ id: A, createdAt: "" }, { row: read.head, log: sessionLogMarkOf(read.entries) })), "conflict", "the log bound and appended to since: not deleted");
|
|
315
|
+
assert.deepEqual(await logIds(repo, A), ["e1", "e2"]);
|
|
316
|
+
await repo.delete({ id: A, createdAt: "" });
|
|
317
|
+
await repo.create({ id: A, owner: "kit-alice" });
|
|
318
|
+
assert.equal(await conflictOf(() => repo.delete({ id: A, createdAt: "" }, { row: read.head })), "conflict", "another occupant under the id: not deleted");
|
|
319
|
+
assert.deepEqual(await logIds(repo, A), [], "the new occupant stands");
|
|
320
|
+
const current = (await repo.readRow(A));
|
|
321
|
+
await (await repo.open({ id: A, createdAt: "" })).getStorage().appendEntry(SESSION_ENTRY("f1", null));
|
|
322
|
+
await repo.delete({ id: A, createdAt: "" }, { row: current.head });
|
|
323
|
+
assert.equal(await logIds(repo, A), undefined, "the occupant expected, appended to since: deleted (an append does not make it another row)");
|
|
324
|
+
assert.equal(await conflictOf(() => repo.delete({ id: A, createdAt: "" }, { row: current.head })), "conflict", "no row: an occupant expected is not there");
|
|
325
|
+
const noShape = { row: undefined };
|
|
326
|
+
await assert.rejects(() => repo.delete({ id: A, createdAt: "" }, noShape), TypeError, "an expectation of no shape is refused, never read as none");
|
|
327
|
+
});
|
|
221
328
|
run("the store over a repo answers headOf exactly when the repo does: present ⇒ the repo's own answer; absent ⇒ no headOf on the store, which still works", async () => {
|
|
222
329
|
const repo = make();
|
|
223
330
|
const store = new TtlSessionStore({ repo });
|