@spellkave/session 0.0.0-bootstrap
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/LICENSE +89 -0
- package/bin/spellkave-session-daemon.js +5 -0
- package/package.json +60 -0
- package/src/acting-context.ts +222 -0
- package/src/authoring/caller-tools.ts +227 -0
- package/src/authoring/index.ts +28 -0
- package/src/authoring/live-applyset-port.ts +432 -0
- package/src/authoring/live-author-target.ts +995 -0
- package/src/authoring/live-can-i-port.ts +152 -0
- package/src/authoring/live-exploration-port.ts +274 -0
- package/src/authoring/live-grants-port.ts +172 -0
- package/src/authoring/live-scenes-port.ts +84 -0
- package/src/authoring/live-scoped-instance-port.ts +355 -0
- package/src/binding.ts +125 -0
- package/src/index.ts +24 -0
- package/src/live/discovery-closure-walk.ts +318 -0
- package/src/live/kernel-convergence-support.ts +122 -0
- package/src/live/seed.ts +37 -0
- package/src/live/support.ts +82 -0
- package/src/node/client.ts +233 -0
- package/src/node/connection-controller.ts +469 -0
- package/src/node/daemon-entry.ts +57 -0
- package/src/node/daemon.ts +629 -0
- package/src/node/index.ts +110 -0
- package/src/node/live-delta.ts +229 -0
- package/src/node/observability.ts +481 -0
- package/src/node/paths.ts +83 -0
- package/src/node/player-run-observer.ts +122 -0
- package/src/node/protocol.ts +200 -0
- package/src/node/spawn.ts +225 -0
- package/src/node/world-events.ts +270 -0
- package/src/player-tools/delta-tracker.ts +1 -0
- package/src/player-tools/index.ts +10 -0
- package/src/player-tools/node-adapter.ts +9 -0
- package/src/player-tools/observation.ts +1 -0
- package/src/player-tools/player-host.ts +92 -0
- package/src/player-tools/push-events.ts +1 -0
- package/src/player-tools/query-protocol.ts +1 -0
- package/src/player-tools/refusal-prose.ts +1 -0
- package/src/player-tools/resolution.ts +1 -0
- package/src/player-tools/tool-registry.ts +1 -0
- package/src/player-tools/tools.ts +42 -0
- package/src/player-tools/world-events.ts +119 -0
- package/src/session.ts +604 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { manipulationArgsFromJson } from "@spellkave/sdk";
|
|
2
|
+
import type { CanIPort, CanIProbeAnswer, CanITarget } from "@spellkave/tool-authoring";
|
|
3
|
+
|
|
4
|
+
type Tagged = { tag: string; value?: unknown };
|
|
5
|
+
|
|
6
|
+
function asTagged(value: unknown): Tagged {
|
|
7
|
+
if (typeof value === "object" && value !== null && "tag" in value) {
|
|
8
|
+
return value as Tagged;
|
|
9
|
+
}
|
|
10
|
+
throw new Error("probe returned a value this client does not understand");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function checkResult(
|
|
14
|
+
tag: string,
|
|
15
|
+
): CanIProbeAnswer extends never
|
|
16
|
+
? never
|
|
17
|
+
: "passed" | "refused" | "errored" | "not_run" | "not_reached" {
|
|
18
|
+
switch (tag) {
|
|
19
|
+
case "Passed":
|
|
20
|
+
return "passed";
|
|
21
|
+
case "Refused":
|
|
22
|
+
return "refused";
|
|
23
|
+
case "Errored":
|
|
24
|
+
return "errored";
|
|
25
|
+
case "NotRun":
|
|
26
|
+
return "not_run";
|
|
27
|
+
default:
|
|
28
|
+
return "not_reached";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function mapProcedureResult(raw: unknown): CanIProbeAnswer {
|
|
33
|
+
const tagged = asTagged(raw);
|
|
34
|
+
if (tagged.tag === "RefusedCaller") return { kind: "refused_caller" };
|
|
35
|
+
if (tagged.tag === "Errored") {
|
|
36
|
+
return {
|
|
37
|
+
kind: "verdict",
|
|
38
|
+
verdict: {
|
|
39
|
+
admitted: false,
|
|
40
|
+
checks: [],
|
|
41
|
+
refusing_check: "",
|
|
42
|
+
failure_kind: "",
|
|
43
|
+
errored_check: String(tagged.value ?? ""),
|
|
44
|
+
template_id: "",
|
|
45
|
+
niche_id: "",
|
|
46
|
+
draws: [],
|
|
47
|
+
repeat_may_differ: false,
|
|
48
|
+
disclosed: false,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const verdict = tagged.value as {
|
|
53
|
+
admitted: boolean;
|
|
54
|
+
checks: Array<{ label: string; result: Tagged }>;
|
|
55
|
+
refusingCheck: string;
|
|
56
|
+
failureKind: string;
|
|
57
|
+
templateId: string;
|
|
58
|
+
nicheId: string;
|
|
59
|
+
draws: Array<{ value: bigint | number }>;
|
|
60
|
+
repeatMayDiffer: boolean;
|
|
61
|
+
disclosed: boolean;
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
kind: "verdict",
|
|
65
|
+
verdict: {
|
|
66
|
+
admitted: verdict.admitted,
|
|
67
|
+
checks: verdict.checks.map((check) => ({
|
|
68
|
+
label: check.label,
|
|
69
|
+
result: checkResult(check.result.tag),
|
|
70
|
+
})),
|
|
71
|
+
refusing_check: verdict.refusingCheck,
|
|
72
|
+
failure_kind: verdict.failureKind,
|
|
73
|
+
errored_check: "",
|
|
74
|
+
template_id: verdict.templateId,
|
|
75
|
+
niche_id: verdict.nicheId,
|
|
76
|
+
draws: verdict.draws.map((draw) => Number(draw.value)),
|
|
77
|
+
repeat_may_differ: verdict.repeatMayDiffer,
|
|
78
|
+
disclosed: verdict.disclosed,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function targetRef(target: CanITarget): { tag: string; value?: unknown } {
|
|
84
|
+
if (target.kind === "world") return { tag: "World" };
|
|
85
|
+
if (target.kind === "place_node") return { tag: "PlaceNode", value: target.id ?? "" };
|
|
86
|
+
if (target.kind === "place_edge") return { tag: "PlaceEdge", value: BigInt(target.id ?? "0") };
|
|
87
|
+
if (target.kind === "location") return { tag: "Location", value: target.id ?? "" };
|
|
88
|
+
return { tag: "Entity", value: BigInt(target.id ?? "0") };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function createLiveCanIPort(session: {
|
|
92
|
+
conn: {
|
|
93
|
+
procedures: {
|
|
94
|
+
probeManipulate: (args: {
|
|
95
|
+
actorId: bigint;
|
|
96
|
+
targetRef: { tag: string; value?: unknown };
|
|
97
|
+
actionId: string;
|
|
98
|
+
args: Record<string, unknown>;
|
|
99
|
+
}) => Promise<unknown>;
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
db: () => {
|
|
103
|
+
visible_manifest_authored_entity_coverage?: {
|
|
104
|
+
iter: () => Iterable<{ status: string; projectionVersion: number }>;
|
|
105
|
+
};
|
|
106
|
+
visibleManifestAuthoredEntityCoverage?: {
|
|
107
|
+
iter: () => Iterable<{ status: string; projectionVersion: number }>;
|
|
108
|
+
};
|
|
109
|
+
visible_manifest_authored_entity?: {
|
|
110
|
+
iter: () => Iterable<{ manifestKey: string; entityId: bigint }>;
|
|
111
|
+
};
|
|
112
|
+
visibleManifestAuthoredEntity?: {
|
|
113
|
+
iter: () => Iterable<{ manifestKey: string; entityId: bigint }>;
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
resolveAuthoredEntity?: (key: string) => Promise<string | null>;
|
|
117
|
+
}): CanIPort {
|
|
118
|
+
return {
|
|
119
|
+
async resolveAuthoredEntity(key) {
|
|
120
|
+
if (session.resolveAuthoredEntity !== undefined) return session.resolveAuthoredEntity(key);
|
|
121
|
+
const db = session.db();
|
|
122
|
+
const handle = db.visibleManifestAuthoredEntity ?? db.visible_manifest_authored_entity;
|
|
123
|
+
const coverage = [
|
|
124
|
+
...((
|
|
125
|
+
db.visibleManifestAuthoredEntityCoverage ?? db.visible_manifest_authored_entity_coverage
|
|
126
|
+
)?.iter() ?? []),
|
|
127
|
+
];
|
|
128
|
+
if (
|
|
129
|
+
handle === undefined ||
|
|
130
|
+
coverage.length !== 1 ||
|
|
131
|
+
coverage[0]?.status !== "complete" ||
|
|
132
|
+
coverage[0]?.projectionVersion !== 1
|
|
133
|
+
)
|
|
134
|
+
throw new Error("reader_unavailable: authored mapping coverage missing or incomplete");
|
|
135
|
+
for (const row of handle.iter()) {
|
|
136
|
+
if (row.manifestKey === key || row.manifestKey.endsWith(`:${key}`)) {
|
|
137
|
+
return row.entityId.toString();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
},
|
|
142
|
+
async probe(input) {
|
|
143
|
+
const raw = await session.conn.procedures.probeManipulate({
|
|
144
|
+
actorId: BigInt(input.actorId),
|
|
145
|
+
targetRef: targetRef(input.target),
|
|
146
|
+
actionId: input.actionId,
|
|
147
|
+
args: manipulationArgsFromJson(JSON.stringify(input.args)),
|
|
148
|
+
});
|
|
149
|
+
return mapProcedureResult(raw);
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live exploration port (persona-world-exploration, task 2.2).
|
|
3
|
+
*
|
|
4
|
+
* Reads places, public catalogs and the caller-scoped instance kinds through ONE
|
|
5
|
+
* isolated caller-authenticated subscription group (the `.44` scoped snapshot
|
|
6
|
+
* port with extra families), then projects the detached copy with the same
|
|
7
|
+
* projector the file source uses. It never reads `session.conn.db` and never
|
|
8
|
+
* falls back to an owner/publisher read: any reader failure is `unavailable`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { SpellkaveSession } from "@spellkave/sdk";
|
|
12
|
+
import {
|
|
13
|
+
type ExplorationPort,
|
|
14
|
+
type ExplorationSnapshot,
|
|
15
|
+
explorationDigest,
|
|
16
|
+
projectExplorationManifest,
|
|
17
|
+
} from "@spellkave/tool-authoring";
|
|
18
|
+
import { AUTHORED_ENTITIES_ONLY, type ExplorationSource } from "@spellkave/tool-contracts";
|
|
19
|
+
import { createInstanceProjector, type OwnerSnapshot } from "@spellkave/world-author";
|
|
20
|
+
import { LIVE_TABLE_ACCESSORS } from "./live-author-target";
|
|
21
|
+
import { createLiveScopedSnapshotPort } from "./live-scoped-instance-port";
|
|
22
|
+
|
|
23
|
+
/** Families the live port reads beyond the scoped eight kinds: projection kind -> live table. */
|
|
24
|
+
const CATALOG_KINDS = [
|
|
25
|
+
// Bundle titles: the engine names only bundles of admitted rows (WE-06).
|
|
26
|
+
"bundles",
|
|
27
|
+
"place_graphs",
|
|
28
|
+
"place_nodes",
|
|
29
|
+
"place_edges",
|
|
30
|
+
"entity_definitions",
|
|
31
|
+
"capability_definitions",
|
|
32
|
+
"action_definitions",
|
|
33
|
+
"affordance_templates",
|
|
34
|
+
"outcome_templates",
|
|
35
|
+
"consequence_templates",
|
|
36
|
+
"entity_definition_tag",
|
|
37
|
+
"capability_definition_tag",
|
|
38
|
+
"object_state_definitions",
|
|
39
|
+
"resource_definitions",
|
|
40
|
+
"resource_definition_tag",
|
|
41
|
+
] as const;
|
|
42
|
+
const text = (value: unknown) => (typeof value === "string" ? value : undefined);
|
|
43
|
+
/**
|
|
44
|
+
* Public catalogs only exploration reads, mapped field by field from the server
|
|
45
|
+
* structs (no authoring-target accessor exists for them). Each maps the storage-key
|
|
46
|
+
* fields the projector joins on.
|
|
47
|
+
*/
|
|
48
|
+
const EXPLORATION_ONLY_KINDS: Readonly<
|
|
49
|
+
Record<
|
|
50
|
+
string,
|
|
51
|
+
{ table: string; toRow: (live: Record<string, unknown>) => Record<string, unknown> }
|
|
52
|
+
>
|
|
53
|
+
> = {
|
|
54
|
+
niche_definitions: {
|
|
55
|
+
table: "niche_definition",
|
|
56
|
+
toRow: (live) => ({
|
|
57
|
+
niche_id: text(live.nicheId ?? live.niche_id),
|
|
58
|
+
bundle_id: text(live.bundleId ?? live.bundle_id),
|
|
59
|
+
display_name: text(live.displayName ?? live.display_name),
|
|
60
|
+
description: text(live.description),
|
|
61
|
+
predicate_source: text(live.predicateSource ?? live.predicate_source),
|
|
62
|
+
}),
|
|
63
|
+
},
|
|
64
|
+
...Object.fromEntries(
|
|
65
|
+
(
|
|
66
|
+
[
|
|
67
|
+
["action_definition_tag", "action_id"],
|
|
68
|
+
["niche_definition_tag", "niche_id"],
|
|
69
|
+
["outcome_template_tag", "outcome_template_id"],
|
|
70
|
+
["object_state_definition_tag", "state_id"],
|
|
71
|
+
] as const
|
|
72
|
+
).map(([kind, parent]) => [
|
|
73
|
+
kind,
|
|
74
|
+
{
|
|
75
|
+
table: kind,
|
|
76
|
+
toRow: (live: Record<string, unknown>) => ({
|
|
77
|
+
[parent]: text(
|
|
78
|
+
live[parent.replace(/_([a-z])/g, (_, c: string) => c.toUpperCase())] ?? live[parent],
|
|
79
|
+
),
|
|
80
|
+
tag: text(live.tag),
|
|
81
|
+
bundle_id: text(live.bundleId ?? live.bundle_id),
|
|
82
|
+
source: text(live.source),
|
|
83
|
+
}),
|
|
84
|
+
},
|
|
85
|
+
]),
|
|
86
|
+
),
|
|
87
|
+
entity_definition_initial_state: {
|
|
88
|
+
table: "entity_definition_initial_state",
|
|
89
|
+
toRow: (live) => ({
|
|
90
|
+
entity_def_id: text(live.entityDefId ?? live.entity_def_id),
|
|
91
|
+
state_id: text(live.stateId ?? live.state_id),
|
|
92
|
+
bundle_id: text(live.bundleId ?? live.bundle_id),
|
|
93
|
+
}),
|
|
94
|
+
},
|
|
95
|
+
movement_defaults: {
|
|
96
|
+
table: "movement_default",
|
|
97
|
+
toRow: (live) => ({
|
|
98
|
+
bundle_id: text(live.bundleId ?? live.bundle_id),
|
|
99
|
+
place_graph_id: text(live.placeGraphId ?? live.place_graph_id),
|
|
100
|
+
mode_id: text(live.modeId ?? live.mode_id),
|
|
101
|
+
affordance_template_id: text(live.affordanceTemplateId ?? live.affordance_template_id),
|
|
102
|
+
bindings_json: text(live.bindingsJson ?? live.bindings_json),
|
|
103
|
+
}),
|
|
104
|
+
},
|
|
105
|
+
place_edge_affordance: {
|
|
106
|
+
table: "place_edge_affordance",
|
|
107
|
+
toRow: (live) => ({
|
|
108
|
+
edge_key: text(live.edgeKey ?? live.edge_key),
|
|
109
|
+
affordance_template_id: text(live.affordanceTemplateId ?? live.affordance_template_id),
|
|
110
|
+
mode_id: text(live.modeId ?? live.mode_id),
|
|
111
|
+
bindings_json: text(live.bindingsJson ?? live.bindings_json),
|
|
112
|
+
}),
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
const INITIAL_CAPABILITY = "entity_definition_initial_capability";
|
|
116
|
+
/** Instance kinds served by the scoped reader that the exploration projector consumes. */
|
|
117
|
+
const INSTANCE_KINDS = [
|
|
118
|
+
"entities",
|
|
119
|
+
"actors",
|
|
120
|
+
"actor_spatial_locations",
|
|
121
|
+
"actor_capabilities",
|
|
122
|
+
"entity_containment",
|
|
123
|
+
"entity_tag",
|
|
124
|
+
"object_state",
|
|
125
|
+
] as const;
|
|
126
|
+
const EXAMINED = 10_000;
|
|
127
|
+
|
|
128
|
+
function extraTables(): Record<string, string> {
|
|
129
|
+
const out: Record<string, string> = {
|
|
130
|
+
config: "config",
|
|
131
|
+
[INITIAL_CAPABILITY]: INITIAL_CAPABILITY,
|
|
132
|
+
};
|
|
133
|
+
for (const [kind, { table }] of Object.entries(EXPLORATION_ONLY_KINDS))
|
|
134
|
+
out[`explore_${kind}`] = table;
|
|
135
|
+
for (const kind of CATALOG_KINDS) {
|
|
136
|
+
const accessor = LIVE_TABLE_ACCESSORS[kind];
|
|
137
|
+
if (!accessor) throw new Error(`live exploration: no live mapping for ${kind}`);
|
|
138
|
+
out[`explore_${kind}`] = accessor.table;
|
|
139
|
+
}
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
function rows(snapshot: OwnerSnapshot, name: string): Record<string, unknown>[] {
|
|
143
|
+
return [...(snapshot[name]?.iter() ?? [])] as Record<string, unknown>[];
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The scoped reader's per-kind status (server `authoring_coverage`) onto
|
|
147
|
+
* exploration coverage. For a caller with at least one authorized scope,
|
|
148
|
+
* `masked` and `outside_read_scope` only say that rows or facts outside the
|
|
149
|
+
* caller's visible scope were left out: exploration counts and pages that
|
|
150
|
+
* visible scope, so those row-dependent codes are not forwarded
|
|
151
|
+
* (Decision 4, WE-06). A caller with no scope at all gets `outside_read_scope`
|
|
152
|
+
* for every kind from the server; that is no read, never an empty world (WE-07).
|
|
153
|
+
* The fixed authored-instance projection is still incomplete for inventory:
|
|
154
|
+
* runtime bodies lack authored keys and graph scopes omit containment-only rows.
|
|
155
|
+
* Retain the existing authored_entities_only contract label as an open-string reason
|
|
156
|
+
* (world-authoring-cli/spec.md, requirement `sk get`). This depends on the reader,
|
|
157
|
+
* never on whether inaccessible rows exist. Bounds and unknown codes stay incomplete.
|
|
158
|
+
*/
|
|
159
|
+
export function explorationCoverageFromScoped(
|
|
160
|
+
family: string,
|
|
161
|
+
status: string | undefined,
|
|
162
|
+
hasScope: boolean,
|
|
163
|
+
): ExplorationSource["coverage"][number] {
|
|
164
|
+
if (!hasScope) return { family, state: "incomplete", reason: "reader_unavailable" };
|
|
165
|
+
if (status === "complete" || status === "masked" || status === "outside_read_scope")
|
|
166
|
+
return { family, state: "incomplete", reason: AUTHORED_ENTITIES_ONLY };
|
|
167
|
+
if (status === "incomplete") return { family, state: "incomplete", reason: "examined_row_bound" };
|
|
168
|
+
return { family, state: "incomplete", reason: "reader_unavailable" };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Movement defaults and edge affordances are public tables that can name a place the
|
|
173
|
+
* caller may not see: keep only rows on a visible graph or edge (a bundle-wide default
|
|
174
|
+
* names no graph), so a hidden graph or edge key never reaches an answer (WE-06). A row whose
|
|
175
|
+
* graph field is missing is dropped too: the filter fails closed.
|
|
176
|
+
*/
|
|
177
|
+
export function keepVisiblePlaceLinks(manifest: Record<string, unknown>): void {
|
|
178
|
+
const list = (kind: string) => (manifest[kind] ?? []) as Record<string, unknown>[];
|
|
179
|
+
const graphs = new Set(list("place_graphs").map((row) => row.place_graph_id));
|
|
180
|
+
const edges = new Set(list("place_edges").map((row) => row.edge_key));
|
|
181
|
+
manifest.movement_defaults = list("movement_defaults").filter(
|
|
182
|
+
(row) => row.place_graph_id === "" || graphs.has(row.place_graph_id),
|
|
183
|
+
);
|
|
184
|
+
manifest.place_edge_affordance = list("place_edge_affordance").filter((row) =>
|
|
185
|
+
edges.has(row.edge_key),
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function createLiveExplorationPort(deps: {
|
|
190
|
+
session: SpellkaveSession | (() => SpellkaveSession);
|
|
191
|
+
world: { url: string; db: string } | (() => { url: string; db: string });
|
|
192
|
+
}): ExplorationPort & { close(): void } {
|
|
193
|
+
const scoped = createLiveScopedSnapshotPort({ ...deps, extraTables: extraTables() });
|
|
194
|
+
return {
|
|
195
|
+
async snapshot(source): Promise<ExplorationSnapshot> {
|
|
196
|
+
if (source !== "live") throw new Error("live exploration port serves only the live source");
|
|
197
|
+
const captured = await scoped.read();
|
|
198
|
+
const { snapshot } = captured;
|
|
199
|
+
// No authorized scope is no read at all (WE-07), whatever the codes say.
|
|
200
|
+
const hasScope = captured.source.authorized_scopes.length > 0;
|
|
201
|
+
if (!hasScope) throw new Error("reader_unavailable: no authorized scope");
|
|
202
|
+
const manifest: Record<string, unknown> = {};
|
|
203
|
+
for (const kind of CATALOG_KINDS) {
|
|
204
|
+
const accessor = LIVE_TABLE_ACCESSORS[kind];
|
|
205
|
+
if (!accessor) continue;
|
|
206
|
+
manifest[kind] = rows(snapshot, `explore_${kind}`).map(accessor.toManifestRow);
|
|
207
|
+
}
|
|
208
|
+
for (const [kind, { toRow }] of Object.entries(EXPLORATION_ONLY_KINDS))
|
|
209
|
+
manifest[kind] = rows(snapshot, `explore_${kind}`).map(toRow);
|
|
210
|
+
keepVisiblePlaceLinks(manifest);
|
|
211
|
+
manifest[INITIAL_CAPABILITY] = rows(snapshot, INITIAL_CAPABILITY).map((live) => ({
|
|
212
|
+
key: live.key,
|
|
213
|
+
entity_def_id: live.entityDefId ?? live.entity_def_id,
|
|
214
|
+
capability_id: live.capabilityId ?? live.capability_id,
|
|
215
|
+
bundle_id: live.bundleId ?? live.bundle_id,
|
|
216
|
+
}));
|
|
217
|
+
const coverage: ExplorationSource["coverage"] = [];
|
|
218
|
+
const projector = createInstanceProjector({
|
|
219
|
+
snapshot: () => snapshot,
|
|
220
|
+
worldTick: () => BigInt(captured.source.tick),
|
|
221
|
+
});
|
|
222
|
+
for (const kind of INSTANCE_KINDS) {
|
|
223
|
+
const answer = projector.get({ kind, limit: EXAMINED });
|
|
224
|
+
const state = explorationCoverageFromScoped(
|
|
225
|
+
kind,
|
|
226
|
+
captured.source.coverage?.[kind],
|
|
227
|
+
hasScope,
|
|
228
|
+
);
|
|
229
|
+
if (!answer.ok) {
|
|
230
|
+
coverage.push({ family: kind, state: "incomplete", reason: "reader_unavailable" });
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
manifest[kind] = answer.rows ?? (answer.row ? [answer.row] : []);
|
|
234
|
+
coverage.push(
|
|
235
|
+
(answer.left_out ?? 0) > 0
|
|
236
|
+
? { family: kind, state: "incomplete", reason: "examined_row_bound" }
|
|
237
|
+
: state,
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
// Place families are complete relative to the caller's spatial admission:
|
|
241
|
+
// the visible_place_* views ARE that admitted projection (design inventory).
|
|
242
|
+
// Public catalogs are complete up to the projector's examined-row bound.
|
|
243
|
+
for (const kind of [
|
|
244
|
+
...CATALOG_KINDS,
|
|
245
|
+
...Object.keys(EXPLORATION_ONLY_KINDS),
|
|
246
|
+
INITIAL_CAPABILITY,
|
|
247
|
+
])
|
|
248
|
+
coverage.push({ family: kind, state: "complete" });
|
|
249
|
+
const config = rows(snapshot, "config")[0];
|
|
250
|
+
const worldInstance = config?.worldInstanceId ?? config?.world_instance_id;
|
|
251
|
+
if (typeof worldInstance !== "string" || worldInstance === "")
|
|
252
|
+
throw new Error("reader_unavailable: world_instance_id missing");
|
|
253
|
+
const endpoint = typeof deps.world === "function" ? deps.world() : deps.world;
|
|
254
|
+
return projectExplorationManifest(
|
|
255
|
+
manifest,
|
|
256
|
+
{
|
|
257
|
+
source: "live",
|
|
258
|
+
// Opaque: never a URL with credentials; stable per endpoint and caller.
|
|
259
|
+
source_id: await explorationDigest({
|
|
260
|
+
world: `${endpoint.url}/${endpoint.db}`,
|
|
261
|
+
identity: captured.source.identity,
|
|
262
|
+
}),
|
|
263
|
+
world: endpoint.db,
|
|
264
|
+
world_instance: worldInstance,
|
|
265
|
+
connection_epoch: String(captured.source.connection_epoch),
|
|
266
|
+
},
|
|
267
|
+
{ coverage },
|
|
268
|
+
);
|
|
269
|
+
},
|
|
270
|
+
close() {
|
|
271
|
+
scoped.close();
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/** Caller-only adapter; complete inventory is a server projection, never owner-token fallback. */
|
|
2
|
+
import type { SpellkaveSession } from "@spellkave/sdk";
|
|
3
|
+
import type { GrantSnapshot, GrantsPort } from "@spellkave/tool-authoring";
|
|
4
|
+
|
|
5
|
+
function identity(value: unknown): string {
|
|
6
|
+
if (value && typeof value === "object" && "toHexString" in value)
|
|
7
|
+
return (value as { toHexString(): string }).toHexString();
|
|
8
|
+
return String(value ?? "");
|
|
9
|
+
}
|
|
10
|
+
export function createLiveGrantsPort(deps: {
|
|
11
|
+
session(): SpellkaveSession;
|
|
12
|
+
endpoint(): string;
|
|
13
|
+
apply(toml: string): Promise<{ ok: boolean; detail?: string }>;
|
|
14
|
+
writeArtifact?: (path: string, text: string) => Promise<void>;
|
|
15
|
+
}): GrantsPort {
|
|
16
|
+
let observed: SpellkaveSession | undefined;
|
|
17
|
+
let barrier: Promise<void> | undefined;
|
|
18
|
+
let handoverSession: SpellkaveSession | undefined;
|
|
19
|
+
let handoverBarrier: Promise<void> | undefined;
|
|
20
|
+
async function handoverReady() {
|
|
21
|
+
const session = deps.session();
|
|
22
|
+
if (!session.handOverProtocolSupported)
|
|
23
|
+
throw new Error("handover protocol unsupported by this module");
|
|
24
|
+
if (handoverSession !== session) {
|
|
25
|
+
handoverSession = session;
|
|
26
|
+
handoverBarrier = new Promise<void>((resolve, reject) =>
|
|
27
|
+
session.conn
|
|
28
|
+
.subscriptionBuilder()
|
|
29
|
+
.onApplied(() => resolve())
|
|
30
|
+
.onError(() => reject(new Error("handover subscription failed")))
|
|
31
|
+
.subscribe((tables) => [
|
|
32
|
+
tables.roleBindingHandOverContext!.build(),
|
|
33
|
+
tables.visibleRoleBindingTransfer!.build(),
|
|
34
|
+
]),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
await handoverBarrier;
|
|
38
|
+
return session;
|
|
39
|
+
}
|
|
40
|
+
const json = (value: unknown): Record<string, unknown> =>
|
|
41
|
+
JSON.parse(
|
|
42
|
+
JSON.stringify(value, (_key, item: unknown) =>
|
|
43
|
+
typeof item === "bigint" ? String(item) : item,
|
|
44
|
+
),
|
|
45
|
+
);
|
|
46
|
+
return {
|
|
47
|
+
async snapshot(): Promise<GrantSnapshot> {
|
|
48
|
+
const session = deps.session();
|
|
49
|
+
if (!session.grantProtocolSupported)
|
|
50
|
+
throw new Error("grant protocol unsupported by this module");
|
|
51
|
+
if (observed !== session) {
|
|
52
|
+
observed = session;
|
|
53
|
+
barrier = new Promise<void>((resolve, reject) =>
|
|
54
|
+
session.conn
|
|
55
|
+
.subscriptionBuilder()
|
|
56
|
+
.onApplied(() => resolve())
|
|
57
|
+
.onError(() => reject(new Error("grant protocol subscription failed")))
|
|
58
|
+
.subscribe((tables) => [
|
|
59
|
+
tables.grantInventory!.build(),
|
|
60
|
+
tables.visibleRoleBinding!.build(),
|
|
61
|
+
]),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
await barrier;
|
|
65
|
+
const db = session.conn.db;
|
|
66
|
+
const inventory = [...db.grant_inventory!.iter()];
|
|
67
|
+
const first = inventory[0];
|
|
68
|
+
if (!first) throw new Error("grant inventory unavailable; no complete snapshot");
|
|
69
|
+
if (
|
|
70
|
+
inventory.some(
|
|
71
|
+
(row) =>
|
|
72
|
+
row.protocolVersion !== first.protocolVersion ||
|
|
73
|
+
row.worldInstanceId !== first.worldInstanceId ||
|
|
74
|
+
row.observedTick !== first.observedTick,
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
throw new Error("incoherent grant inventory; reconnect and preview again");
|
|
78
|
+
return {
|
|
79
|
+
protocolVersion: first.protocolVersion,
|
|
80
|
+
identity: identity(session.authenticatedIdentity),
|
|
81
|
+
endpoint: deps.endpoint(),
|
|
82
|
+
worldInstanceId: first.worldInstanceId,
|
|
83
|
+
tick: String(first.observedTick),
|
|
84
|
+
publisher: first.publisher,
|
|
85
|
+
completeInventory: true,
|
|
86
|
+
inventory: inventory.map((r) => ({
|
|
87
|
+
role: r.roleId,
|
|
88
|
+
kind: r.extentKindRef,
|
|
89
|
+
reference: r.extentRef,
|
|
90
|
+
canBind: r.canBind,
|
|
91
|
+
expansionMember: r.expansionMember,
|
|
92
|
+
expansionComplete: r.expansionComplete,
|
|
93
|
+
})),
|
|
94
|
+
bindings: [...db.visible_role_binding!.iter()].map((r) => ({
|
|
95
|
+
binding_id: r.bindingId,
|
|
96
|
+
bundle_id: r.bundleId,
|
|
97
|
+
subject: r.subject,
|
|
98
|
+
role_id: r.roleId,
|
|
99
|
+
extent_kind_ref: r.extentKindRef,
|
|
100
|
+
extent_ref: r.extentRef,
|
|
101
|
+
expires_at_tick:
|
|
102
|
+
r.expiresAtTick === undefined || r.expiresAtTick === 0n
|
|
103
|
+
? null
|
|
104
|
+
: String(r.expiresAtTick),
|
|
105
|
+
description: r.description,
|
|
106
|
+
handover_note: r.handoverNote ?? null,
|
|
107
|
+
})),
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
async prepareHandOver(bindingId) {
|
|
111
|
+
const session = await handoverReady();
|
|
112
|
+
const row = [...session.conn.db.role_binding_hand_over_context!.iter()].find(
|
|
113
|
+
(r) => r.binding.bindingId === bindingId,
|
|
114
|
+
);
|
|
115
|
+
if (!row) return undefined;
|
|
116
|
+
const r = row.binding;
|
|
117
|
+
return {
|
|
118
|
+
binding: {
|
|
119
|
+
binding_id: r.bindingId,
|
|
120
|
+
bundle_id: r.bundleId,
|
|
121
|
+
subject: r.subject,
|
|
122
|
+
role_id: r.roleId,
|
|
123
|
+
extent_kind_ref: r.extentKindRef,
|
|
124
|
+
extent_ref: r.extentRef,
|
|
125
|
+
expires_at_tick: r.expiresAtTick === undefined ? null : String(r.expiresAtTick),
|
|
126
|
+
description: r.description,
|
|
127
|
+
handover_note: r.handoverNote ?? null,
|
|
128
|
+
},
|
|
129
|
+
expected_digest: row.authorityDigest,
|
|
130
|
+
expected_merged_role_digest: row.mergedRoleDigest,
|
|
131
|
+
expected_incarnation: r.incarnation,
|
|
132
|
+
expected_revision: String(r.revision),
|
|
133
|
+
source_tick: String(row.sourceTick),
|
|
134
|
+
context: json({
|
|
135
|
+
pending_requests: row.pendingRequests,
|
|
136
|
+
scheduled_jobs: row.scheduledJobs,
|
|
137
|
+
coverage: row.coverage,
|
|
138
|
+
held_blockers: row.heldBlockers,
|
|
139
|
+
held_status: row.heldStatus,
|
|
140
|
+
clock_paused: row.clockPaused,
|
|
141
|
+
}),
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
async transferReceipt(requestId, bindingId) {
|
|
145
|
+
const session = await handoverReady();
|
|
146
|
+
const row = [...session.conn.db.visible_role_binding_transfer!.iter()].find(
|
|
147
|
+
(r) => r.requestId === requestId && r.bindingId === bindingId,
|
|
148
|
+
);
|
|
149
|
+
if (!row) return undefined;
|
|
150
|
+
return {
|
|
151
|
+
key: row.key,
|
|
152
|
+
request_id: row.requestId,
|
|
153
|
+
binding_id: row.bindingId,
|
|
154
|
+
presence_entity_id: String(row.presenceEntityId),
|
|
155
|
+
old_subject: row.oldSubject,
|
|
156
|
+
new_subject: row.newSubject,
|
|
157
|
+
applied_by: identity(row.appliedBy),
|
|
158
|
+
applied_via: row.appliedVia,
|
|
159
|
+
tick: String(row.tick),
|
|
160
|
+
reason: row.reason,
|
|
161
|
+
incarnation: row.incarnation,
|
|
162
|
+
old_revision: String(row.oldRevision),
|
|
163
|
+
new_revision: String(row.newRevision),
|
|
164
|
+
old_digest: row.oldDigest,
|
|
165
|
+
new_digest: row.newDigest,
|
|
166
|
+
merged_role_digest: row.mergedRoleDigest,
|
|
167
|
+
};
|
|
168
|
+
},
|
|
169
|
+
apply: deps.apply,
|
|
170
|
+
...(deps.writeArtifact === undefined ? {} : { writeArtifact: deps.writeArtifact }),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/** Scene snapshots and applies stay on the selected caller's connection. */
|
|
2
|
+
import type { SpellkaveSession } from "@spellkave/sdk";
|
|
3
|
+
import type { SceneSourceSnapshot, ScenesPort } from "@spellkave/tool-authoring";
|
|
4
|
+
export function createLiveScenesPort(deps: {
|
|
5
|
+
session(): SpellkaveSession;
|
|
6
|
+
endpoint(): string;
|
|
7
|
+
apply(toml: string): Promise<{ ok: boolean; detail?: string }>;
|
|
8
|
+
}): ScenesPort {
|
|
9
|
+
return {
|
|
10
|
+
async snapshot(blueprintId) {
|
|
11
|
+
const session = deps.session();
|
|
12
|
+
const endpoint = deps.endpoint();
|
|
13
|
+
if (!session.authenticatedIdentity || !session.conn.isActive)
|
|
14
|
+
throw new Error("scene source unavailable; reconnect as the caller");
|
|
15
|
+
return new Promise<SceneSourceSnapshot>((resolve, reject) => {
|
|
16
|
+
let settled = false;
|
|
17
|
+
// Snapshot completion must not wait for an unsubscribe acknowledgement.
|
|
18
|
+
// The parent SDK owns disconnect callbacks; observe its active state here.
|
|
19
|
+
const watch = setInterval(() => {
|
|
20
|
+
try {
|
|
21
|
+
if (!current()) fail(new Error("scene caller connection changed or disconnected"));
|
|
22
|
+
} catch (error) {
|
|
23
|
+
fail(error);
|
|
24
|
+
}
|
|
25
|
+
}, 40);
|
|
26
|
+
function current() {
|
|
27
|
+
return (
|
|
28
|
+
session.conn.isActive &&
|
|
29
|
+
deps.session().conn === session.conn &&
|
|
30
|
+
deps.endpoint() === endpoint
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
function fail(error: unknown) {
|
|
34
|
+
if (settled) return;
|
|
35
|
+
settled = true;
|
|
36
|
+
clearInterval(watch);
|
|
37
|
+
reject(error);
|
|
38
|
+
try {
|
|
39
|
+
if (handle.isActive()) handle.unsubscribe();
|
|
40
|
+
} catch {
|
|
41
|
+
/* Transport already ended. */
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const handle = session.conn
|
|
45
|
+
.subscriptionBuilder()
|
|
46
|
+
.onError(() => fail(new Error("scene source protocol unavailable or incomplete")))
|
|
47
|
+
.onApplied(() => {
|
|
48
|
+
if (settled) return;
|
|
49
|
+
try {
|
|
50
|
+
if (!current()) throw new Error("scene caller connection changed or disconnected");
|
|
51
|
+
const rows = [...session.conn.db.scene_source.iter()].filter(
|
|
52
|
+
(row) => row.graphId === blueprintId,
|
|
53
|
+
);
|
|
54
|
+
if (
|
|
55
|
+
rows.length !== 1 ||
|
|
56
|
+
rows[0]?.protocolVersion !== 1 ||
|
|
57
|
+
rows[0]?.status !== "complete-empty"
|
|
58
|
+
)
|
|
59
|
+
throw new Error("scene source unavailable or incomplete");
|
|
60
|
+
const row = rows[0];
|
|
61
|
+
const snapshot: SceneSourceSnapshot = {
|
|
62
|
+
protocol_version: 1,
|
|
63
|
+
caller: session.authenticatedIdentity!.toHexString(),
|
|
64
|
+
endpoint,
|
|
65
|
+
world_instance_id: row.worldInstanceId,
|
|
66
|
+
source_digest: row.sourceDigest,
|
|
67
|
+
snapshot_json: row.snapshotJson,
|
|
68
|
+
};
|
|
69
|
+
settled = true;
|
|
70
|
+
clearInterval(watch);
|
|
71
|
+
resolve(snapshot);
|
|
72
|
+
handle.unsubscribe();
|
|
73
|
+
} catch (error) {
|
|
74
|
+
fail(error);
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
.subscribe((tables) =>
|
|
78
|
+
tables.sceneSource!.where((row) => row.graphId!.eq(blueprintId)).build(),
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
apply: deps.apply,
|
|
83
|
+
};
|
|
84
|
+
}
|