@spellkave/sdk 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/README.md +156 -0
- package/package.json +65 -0
- package/src/binding_rows.ts +186 -0
- package/src/connection.ts +964 -0
- package/src/core/actor.ts +62 -0
- package/src/core/actor_control.ts +193 -0
- package/src/core/actor_control_query_methods.ts +101 -0
- package/src/core/actor_control_reaction_methods.ts +79 -0
- package/src/core/actor_control_sense_methods.ts +81 -0
- package/src/core/actor_control_source.ts +57 -0
- package/src/core/actor_session_methods.ts +226 -0
- package/src/core/binding_drift.ts +301 -0
- package/src/core/credential.ts +259 -0
- package/src/core/driver_control.ts +54 -0
- package/src/core/effective_environment_state.ts +71 -0
- package/src/core/factored_manipulation_attempt.ts +158 -0
- package/src/core/ids.ts +60 -0
- package/src/core/interaction/canonical.ts +15 -0
- package/src/core/interaction/fingerprint.ts +118 -0
- package/src/core/interaction/index.ts +31 -0
- package/src/core/interaction/intent.ts +284 -0
- package/src/core/interaction/outcome.ts +67 -0
- package/src/core/interaction/rejection_ledger.ts +108 -0
- package/src/core/kernel_contract.generated.ts +10 -0
- package/src/core/log.ts +23 -0
- package/src/core/movement.ts +111 -0
- package/src/core/node-io.ts +208 -0
- package/src/core/paths.ts +220 -0
- package/src/core/profile.ts +270 -0
- package/src/core/profiles.ts +9 -0
- package/src/core/provenance_buffer.ts +102 -0
- package/src/core/readers/_helpers.ts +273 -0
- package/src/core/readers/action_description.ts +17 -0
- package/src/core/readers/action_economy_events.ts +149 -0
- package/src/core/readers/actor_body_swaps.ts +62 -0
- package/src/core/readers/actor_snapshot.ts +197 -0
- package/src/core/readers/actor_states.ts +163 -0
- package/src/core/readers/actors_and_bodies.ts +179 -0
- package/src/core/readers/affordance_cue.ts +59 -0
- package/src/core/readers/affordance_defaults.ts +158 -0
- package/src/core/readers/affordance_group_membership.ts +67 -0
- package/src/core/readers/affordance_input.ts +129 -0
- package/src/core/readers/affordance_reference.ts +41 -0
- package/src/core/readers/apply_consequences.ts +54 -0
- package/src/core/readers/audience_trace_presentation.ts +62 -0
- package/src/core/readers/bounded_collection.ts +29 -0
- package/src/core/readers/bundle_definitions.ts +74 -0
- package/src/core/readers/bundle_entities.ts +62 -0
- package/src/core/readers/co_location.ts +71 -0
- package/src/core/readers/combat_session.ts +124 -0
- package/src/core/readers/combat_session_active.ts +44 -0
- package/src/core/readers/combat_session_feedback.ts +37 -0
- package/src/core/readers/combat_session_grid.ts +117 -0
- package/src/core/readers/combat_session_roster.ts +98 -0
- package/src/core/readers/combat_session_types.ts +51 -0
- package/src/core/readers/condition_deltas.ts +53 -0
- package/src/core/readers/container_contents.ts +92 -0
- package/src/core/readers/dialogue_guide.ts +99 -0
- package/src/core/readers/dialogue_prompt_presentation.ts +90 -0
- package/src/core/readers/driver_control.ts +64 -0
- package/src/core/readers/entity_art.ts +219 -0
- package/src/core/readers/entity_containment.ts +119 -0
- package/src/core/readers/equipment.ts +99 -0
- package/src/core/readers/intervention_windows.ts +136 -0
- package/src/core/readers/inventory_query.ts +145 -0
- package/src/core/readers/item_detail.ts +198 -0
- package/src/core/readers/location_snapshot.ts +216 -0
- package/src/core/readers/manifest_apply_provenance.ts +32 -0
- package/src/core/readers/manifest_export.ts +81 -0
- package/src/core/readers/manipulation_context.ts +475 -0
- package/src/core/readers/map_layers.ts +49 -0
- package/src/core/readers/metadata_rows.ts +56 -0
- package/src/core/readers/object_state.ts +124 -0
- package/src/core/readers/observer_knowledge.ts +41 -0
- package/src/core/readers/observer_place_knowledge.ts +37 -0
- package/src/core/readers/observer_trace_knowledge.ts +45 -0
- package/src/core/readers/outcome_template.ts +41 -0
- package/src/core/readers/place_topology.ts +109 -0
- package/src/core/readers/reaction_options.ts +156 -0
- package/src/core/readers/resource_deltas.ts +47 -0
- package/src/core/readers/scene.ts +275 -0
- package/src/core/readers/seats.ts +33 -0
- package/src/core/readers/self.ts +245 -0
- package/src/core/readers/self_capabilities.ts +159 -0
- package/src/core/readers/self_economy.ts +101 -0
- package/src/core/readers/self_economy_budget.ts +93 -0
- package/src/core/readers/self_evaluations.ts +128 -0
- package/src/core/readers/self_identity.ts +104 -0
- package/src/core/readers/sense_context.ts +112 -0
- package/src/core/readers/session.ts +207 -0
- package/src/core/readers/session_detail.ts +211 -0
- package/src/core/readers/session_status.ts +68 -0
- package/src/core/readers/spatial_environment_state.ts +74 -0
- package/src/core/readers/spatial_niche_attachments.ts +28 -0
- package/src/core/readers/substrate_definitions.ts +27 -0
- package/src/core/readers/suggest_next_action.ts +72 -0
- package/src/core/readers/surface_membership.ts +78 -0
- package/src/core/readers/tag_reads.ts +54 -0
- package/src/core/readers/trace_query.ts +120 -0
- package/src/core/readers/trace_response.ts +80 -0
- package/src/core/readers/traces.ts +124 -0
- package/src/core/readers/travel_session.ts +88 -0
- package/src/core/readers/turn_state.ts +34 -0
- package/src/core/readers/world_events.ts +43 -0
- package/src/core/readers/world_status.ts +63 -0
- package/src/core/reducers/manipulation_args.ts +239 -0
- package/src/core/reducers/manipulation_receipt.ts +66 -0
- package/src/core/reducers/r_actor_wake_up.ts +32 -0
- package/src/core/reducers/r_inscribe_trace.ts +109 -0
- package/src/core/reducers/r_manipulate.ts +163 -0
- package/src/core/reducers/r_move.ts +155 -0
- package/src/core/reducers/r_react.ts +143 -0
- package/src/core/reducers/r_release_claim.ts +81 -0
- package/src/core/reducers/r_sense.ts +84 -0
- package/src/core/reducers/r_session_close.ts +130 -0
- package/src/core/reducers/r_session_move.ts +116 -0
- package/src/core/reducers/r_session_open.ts +91 -0
- package/src/core/reducers/session_lookup.ts +64 -0
- package/src/core/reducers/tick.ts +65 -0
- package/src/core/reducers/trace_lookup.ts +29 -0
- package/src/core/reducers/transport.ts +84 -0
- package/src/core/resolve.ts +228 -0
- package/src/core/shape_telemetry.ts +146 -0
- package/src/core/snapshot_rows.ts +109 -0
- package/src/core/tick_cadence.ts +61 -0
- package/src/core/types.ts +501 -0
- package/src/core/watch_provenance.ts +113 -0
- package/src/core/world-contract/classify.ts +38 -0
- package/src/core/world-contract/index.ts +12 -0
- package/src/core/world-contract/normalise.ts +574 -0
- package/src/core/world-contract/runtime.ts +144 -0
- package/src/core/world-contract/served.ts +173 -0
- package/src/core/world-contract/strict-reducers.ts +14 -0
- package/src/core/world-contract/typetext.ts +94 -0
- package/src/core/world_time.ts +22 -0
- package/src/index.ts +262 -0
- package/src/manifest-apply.ts +497 -0
- package/src/provenance.ts +568 -0
- package/src/reader-manifest.ts +338 -0
- package/src/readers.ts +392 -0
- package/src/replay.ts +515 -0
- package/src/watch-buffer.ts +438 -0
- package/src/world-events/builder.ts +1042 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Per-reducer wrapper for r_session_close.
|
|
2
|
+
// Success predicate: the existing session row disappears or reaches the
|
|
3
|
+
// requested terminal lifecycle in the subscribed session table.
|
|
4
|
+
|
|
5
|
+
import type { DbConnection } from "@spellkave/bindings";
|
|
6
|
+
import type { SnapshotDb } from "../../connection";
|
|
7
|
+
import { observeReducerAttempt } from "../../provenance";
|
|
8
|
+
import type { DriverToken } from "../driver_control";
|
|
9
|
+
import type { ProvenanceRingBuffer } from "../provenance_buffer";
|
|
10
|
+
import type { TickIntervalEstimator } from "../tick_cadence";
|
|
11
|
+
import { firstPopulatedHandle } from "./session_lookup";
|
|
12
|
+
|
|
13
|
+
export type RSessionCloseFn = DbConnection["reducers"]["rSessionClose"];
|
|
14
|
+
|
|
15
|
+
export interface SessionCloseSnapshot {
|
|
16
|
+
sessionId: bigint;
|
|
17
|
+
lifecycle: string;
|
|
18
|
+
lastAdvancedAt: bigint;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface SessionCloseResult {
|
|
22
|
+
status:
|
|
23
|
+
| "success"
|
|
24
|
+
| "rejected"
|
|
25
|
+
| "unobserved_timeout"
|
|
26
|
+
| "unobserved_no_provenance"
|
|
27
|
+
| "unobservable";
|
|
28
|
+
sessionId: bigint;
|
|
29
|
+
outcome: string;
|
|
30
|
+
session?: SessionCloseSnapshot;
|
|
31
|
+
reason?: string;
|
|
32
|
+
lagWarning: boolean;
|
|
33
|
+
observedAtTick: bigint;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function rSessionCloseWrapper(
|
|
37
|
+
actorId: string,
|
|
38
|
+
sessionId: bigint,
|
|
39
|
+
outcome: string,
|
|
40
|
+
snapshotDb: () => SnapshotDb,
|
|
41
|
+
rSessionClose: RSessionCloseFn,
|
|
42
|
+
timeoutMs: number,
|
|
43
|
+
pollMs: number,
|
|
44
|
+
tickEstimator?: TickIntervalEstimator,
|
|
45
|
+
provenanceBuffer?: ProvenanceRingBuffer,
|
|
46
|
+
driverToken?: DriverToken,
|
|
47
|
+
driverTokens?: readonly DriverToken[],
|
|
48
|
+
): Promise<SessionCloseResult> {
|
|
49
|
+
// Copy the observations supplied with this decision. Never refresh them
|
|
50
|
+
// from the live reader during dispatch or while polling the outcome.
|
|
51
|
+
const frozenToken = driverToken ? Object.freeze({ ...driverToken }) : undefined;
|
|
52
|
+
const frozenTokens = driverTokens?.map((token) => Object.freeze({ ...token }));
|
|
53
|
+
const existedBeforeInvoke = findSession(snapshotDb(), sessionId) !== undefined;
|
|
54
|
+
const result = await observeReducerAttempt<SessionCloseSnapshot | null>({
|
|
55
|
+
db: snapshotDb,
|
|
56
|
+
subjectId: actorId,
|
|
57
|
+
invoke: () =>
|
|
58
|
+
rSessionClose({
|
|
59
|
+
sessionId,
|
|
60
|
+
outcome,
|
|
61
|
+
...(frozenToken ? { driverToken: frozenToken } : {}),
|
|
62
|
+
...(frozenTokens ? { driverTokens: frozenTokens } : {}),
|
|
63
|
+
}),
|
|
64
|
+
successPredicate: (db) => {
|
|
65
|
+
if (!existedBeforeInvoke) return false;
|
|
66
|
+
const row = findSession(db, sessionId);
|
|
67
|
+
// Current server behavior updates the session in place; disappearance is
|
|
68
|
+
// reserved for a future terminal-row cleanup path.
|
|
69
|
+
return row === undefined || row.lifecycle === outcome;
|
|
70
|
+
},
|
|
71
|
+
collectSnapshot: (db) => findSession(db, sessionId) ?? null,
|
|
72
|
+
timeoutMs,
|
|
73
|
+
pollMs,
|
|
74
|
+
tickEstimator,
|
|
75
|
+
provenanceBuffer,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (result.status === "success") {
|
|
79
|
+
return {
|
|
80
|
+
status: "success",
|
|
81
|
+
sessionId,
|
|
82
|
+
outcome,
|
|
83
|
+
session: result.snapshot ?? undefined,
|
|
84
|
+
lagWarning: result.lagWarning,
|
|
85
|
+
observedAtTick: result.observedAtTick,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (result.status === "rejected") {
|
|
90
|
+
return {
|
|
91
|
+
status: "rejected",
|
|
92
|
+
sessionId,
|
|
93
|
+
outcome,
|
|
94
|
+
session: result.snapshot ?? undefined,
|
|
95
|
+
reason: "r_session_close rejected by server",
|
|
96
|
+
lagWarning: result.lagWarning,
|
|
97
|
+
observedAtTick: result.observedAtTick,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
status: result.status,
|
|
103
|
+
sessionId,
|
|
104
|
+
outcome,
|
|
105
|
+
session: result.snapshot ?? undefined,
|
|
106
|
+
reason: `r_session_close for session '${sessionId}' not observed within ${timeoutMs}ms`,
|
|
107
|
+
lagWarning: result.lagWarning,
|
|
108
|
+
observedAtTick: result.observedAtTick,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function findSession(db: SnapshotDb, sessionId: bigint): SessionCloseSnapshot | undefined {
|
|
113
|
+
const handle = firstPopulatedHandle(db, "visible_session", "session");
|
|
114
|
+
if (!handle) return undefined;
|
|
115
|
+
for (const raw of handle.iter()) {
|
|
116
|
+
const r = raw as unknown as { sessionId: unknown; lifecycle: string; lastAdvancedAt: unknown };
|
|
117
|
+
if (toBigInt(r.sessionId) === sessionId) {
|
|
118
|
+
return {
|
|
119
|
+
sessionId,
|
|
120
|
+
lifecycle: r.lifecycle,
|
|
121
|
+
lastAdvancedAt: toBigInt(r.lastAdvancedAt),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function toBigInt(value: unknown): bigint {
|
|
129
|
+
return typeof value === "bigint" ? value : BigInt(String(value ?? 0));
|
|
130
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Per-reducer wrapper for r_session_move.
|
|
2
|
+
// Success predicate: the session row advances from its pre-call lifecycle clock
|
|
3
|
+
// or transitions lifecycle, as observed in the subscribed session table.
|
|
4
|
+
|
|
5
|
+
import type { DbConnection } from "@spellkave/bindings";
|
|
6
|
+
import type { SnapshotDb } from "../../connection";
|
|
7
|
+
import { observeReducerAttempt } from "../../provenance";
|
|
8
|
+
import type { ProvenanceRingBuffer } from "../provenance_buffer";
|
|
9
|
+
import type { TickIntervalEstimator } from "../tick_cadence";
|
|
10
|
+
import { firstPopulatedHandle } from "./session_lookup";
|
|
11
|
+
|
|
12
|
+
export type RSessionMoveFn = DbConnection["reducers"]["rSessionMove"];
|
|
13
|
+
|
|
14
|
+
export interface SessionMoveSnapshot {
|
|
15
|
+
sessionId: bigint;
|
|
16
|
+
lifecycle: string;
|
|
17
|
+
lastAdvancedAt: bigint;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SessionMoveResult {
|
|
21
|
+
status:
|
|
22
|
+
| "success"
|
|
23
|
+
| "rejected"
|
|
24
|
+
| "unobserved_timeout"
|
|
25
|
+
| "unobserved_no_provenance"
|
|
26
|
+
| "unobservable";
|
|
27
|
+
sessionId: bigint;
|
|
28
|
+
moveTemplateId: string;
|
|
29
|
+
session: SessionMoveSnapshot | null;
|
|
30
|
+
reason?: string;
|
|
31
|
+
lagWarning: boolean;
|
|
32
|
+
observedAtTick: bigint;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function rSessionMoveWrapper(
|
|
36
|
+
actorId: string,
|
|
37
|
+
sessionId: bigint,
|
|
38
|
+
moveTemplateId: string,
|
|
39
|
+
snapshotDb: () => SnapshotDb,
|
|
40
|
+
rSessionMove: RSessionMoveFn,
|
|
41
|
+
timeoutMs: number,
|
|
42
|
+
pollMs: number,
|
|
43
|
+
tickEstimator?: TickIntervalEstimator,
|
|
44
|
+
provenanceBuffer?: ProvenanceRingBuffer,
|
|
45
|
+
): Promise<SessionMoveResult> {
|
|
46
|
+
const before = findSession(snapshotDb(), sessionId);
|
|
47
|
+
const result = await observeReducerAttempt<SessionMoveSnapshot | null>({
|
|
48
|
+
db: snapshotDb,
|
|
49
|
+
subjectId: actorId,
|
|
50
|
+
invoke: () => rSessionMove({ sessionId, moveTemplateId }),
|
|
51
|
+
successPredicate: (db) => {
|
|
52
|
+
if (before === undefined) return false;
|
|
53
|
+
const row = findSession(db, sessionId);
|
|
54
|
+
if (row === undefined) return false;
|
|
55
|
+
return row.lastAdvancedAt !== before.lastAdvancedAt || row.lifecycle !== before.lifecycle;
|
|
56
|
+
},
|
|
57
|
+
collectSnapshot: (db) => findSession(db, sessionId) ?? null,
|
|
58
|
+
timeoutMs,
|
|
59
|
+
pollMs,
|
|
60
|
+
tickEstimator,
|
|
61
|
+
provenanceBuffer,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (result.status === "success") {
|
|
65
|
+
return {
|
|
66
|
+
status: "success",
|
|
67
|
+
sessionId,
|
|
68
|
+
moveTemplateId,
|
|
69
|
+
session: result.snapshot,
|
|
70
|
+
lagWarning: result.lagWarning,
|
|
71
|
+
observedAtTick: result.observedAtTick,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (result.status === "rejected") {
|
|
76
|
+
return {
|
|
77
|
+
status: "rejected",
|
|
78
|
+
sessionId,
|
|
79
|
+
moveTemplateId,
|
|
80
|
+
session: result.snapshot,
|
|
81
|
+
reason: "r_session_move rejected by server",
|
|
82
|
+
lagWarning: result.lagWarning,
|
|
83
|
+
observedAtTick: result.observedAtTick,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
status: result.status,
|
|
89
|
+
sessionId,
|
|
90
|
+
moveTemplateId,
|
|
91
|
+
session: result.snapshot,
|
|
92
|
+
reason: `r_session_move '${moveTemplateId}' for session '${sessionId}' not observed within ${timeoutMs}ms`,
|
|
93
|
+
lagWarning: result.lagWarning,
|
|
94
|
+
observedAtTick: result.observedAtTick,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function findSession(db: SnapshotDb, sessionId: bigint): SessionMoveSnapshot | undefined {
|
|
99
|
+
const handle = firstPopulatedHandle(db, "visible_session", "session");
|
|
100
|
+
if (!handle) return undefined;
|
|
101
|
+
for (const raw of handle.iter()) {
|
|
102
|
+
const r = raw as unknown as { sessionId: unknown; lifecycle: string; lastAdvancedAt: unknown };
|
|
103
|
+
if (toBigInt(r.sessionId) === sessionId) {
|
|
104
|
+
return {
|
|
105
|
+
sessionId,
|
|
106
|
+
lifecycle: r.lifecycle,
|
|
107
|
+
lastAdvancedAt: toBigInt(r.lastAdvancedAt),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function toBigInt(value: unknown): bigint {
|
|
115
|
+
return typeof value === "bigint" ? value : BigInt(String(value ?? 0));
|
|
116
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Per-reducer wrapper for r_session_open (real wrapper — Phase 9 scenario A).
|
|
2
|
+
// Success predicate: a session row with matching sessionDefId appears in the
|
|
3
|
+
// session table cache (subscribed via READER_SCOPED_TABLES).
|
|
4
|
+
|
|
5
|
+
import type { DbConnection } from "@spellkave/bindings";
|
|
6
|
+
import type { SnapshotDb } from "../../connection";
|
|
7
|
+
import { observeReducerAttempt } from "../../provenance";
|
|
8
|
+
import type { ProvenanceRingBuffer } from "../provenance_buffer";
|
|
9
|
+
import type { TickIntervalEstimator } from "../tick_cadence";
|
|
10
|
+
import { findSessionId } from "./session_lookup";
|
|
11
|
+
import { actorEntityId } from "./transport";
|
|
12
|
+
|
|
13
|
+
type RSessionOpenFn = DbConnection["reducers"]["rSessionOpen"];
|
|
14
|
+
|
|
15
|
+
export interface SessionOpenResult {
|
|
16
|
+
status:
|
|
17
|
+
| "success"
|
|
18
|
+
| "rejected"
|
|
19
|
+
| "unobserved_timeout"
|
|
20
|
+
| "unobserved_no_provenance"
|
|
21
|
+
| "unobservable";
|
|
22
|
+
sessionDefId: string;
|
|
23
|
+
/** Session id from the session table row, if observable. */
|
|
24
|
+
sessionId?: bigint;
|
|
25
|
+
reason?: string;
|
|
26
|
+
lagWarning: boolean;
|
|
27
|
+
observedAtTick: bigint;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function rSessionOpenWrapper(
|
|
31
|
+
actorId: string,
|
|
32
|
+
sessionDefId: string,
|
|
33
|
+
participantsCsv: string,
|
|
34
|
+
snapshotDb: () => SnapshotDb,
|
|
35
|
+
rSessionOpen: RSessionOpenFn,
|
|
36
|
+
timeoutMs: number,
|
|
37
|
+
pollMs: number,
|
|
38
|
+
tickEstimator?: TickIntervalEstimator,
|
|
39
|
+
provenanceBuffer?: ProvenanceRingBuffer,
|
|
40
|
+
): Promise<SessionOpenResult> {
|
|
41
|
+
const participants = participantIds(actorId, participantsCsv);
|
|
42
|
+
const result = await observeReducerAttempt<bigint | null>({
|
|
43
|
+
db: snapshotDb,
|
|
44
|
+
subjectId: actorId,
|
|
45
|
+
invoke: () =>
|
|
46
|
+
rSessionOpen({ driverTokens: [], sessionDefId, participants, gridExtentJson: "" }),
|
|
47
|
+
successPredicate: (db) => findSessionId(db, sessionDefId, participants) !== undefined,
|
|
48
|
+
collectSnapshot: (db) => findSessionId(db, sessionDefId, participants) ?? null,
|
|
49
|
+
timeoutMs,
|
|
50
|
+
pollMs,
|
|
51
|
+
tickEstimator,
|
|
52
|
+
provenanceBuffer,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
if (result.status === "success") {
|
|
56
|
+
return {
|
|
57
|
+
status: "success",
|
|
58
|
+
sessionDefId,
|
|
59
|
+
sessionId: result.snapshot ?? undefined,
|
|
60
|
+
lagWarning: result.lagWarning,
|
|
61
|
+
observedAtTick: result.observedAtTick,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (result.status === "rejected") {
|
|
66
|
+
return {
|
|
67
|
+
status: "rejected",
|
|
68
|
+
sessionDefId,
|
|
69
|
+
reason: `r_session_open rejected by server`,
|
|
70
|
+
lagWarning: result.lagWarning,
|
|
71
|
+
observedAtTick: result.observedAtTick,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
status: result.status,
|
|
77
|
+
sessionDefId,
|
|
78
|
+
reason: `r_session_open for '${sessionDefId}' not observed within ${timeoutMs}ms`,
|
|
79
|
+
lagWarning: result.lagWarning,
|
|
80
|
+
observedAtTick: result.observedAtTick,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function participantIds(actorId: string, participantsCsv: string): bigint[] {
|
|
85
|
+
const explicit = participantsCsv
|
|
86
|
+
.split(",")
|
|
87
|
+
.map((part) => part.trim())
|
|
88
|
+
.filter((part) => part.length > 0)
|
|
89
|
+
.map(actorEntityId);
|
|
90
|
+
return explicit.length > 0 ? explicit : [actorEntityId(actorId)];
|
|
91
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Session-row lookup for r_session_open — split out to keep the wrapper
|
|
2
|
+
// itself under the sdk_loc_ceiling (LR-102).
|
|
3
|
+
|
|
4
|
+
import type { SnapshotDb } from "../../connection";
|
|
5
|
+
|
|
6
|
+
const TERMINAL_SESSION_LIFECYCLES = new Set(["completed", "abandoned", "failed"]);
|
|
7
|
+
|
|
8
|
+
/** First subscribed handle that already has rows, else the first existing
|
|
9
|
+
* empty handle. Live clients only subscribe `visible_session` / `visible_session_participant`
|
|
10
|
+
* (raw `session` is private, ADR-0057 / sk-2qt); unit fixtures may still inject
|
|
11
|
+
* the pre-privacy names. */
|
|
12
|
+
export function firstPopulatedHandle(db: SnapshotDb, ...accessors: string[]) {
|
|
13
|
+
let empty: { iter(): Iterable<unknown> } | undefined;
|
|
14
|
+
for (const name of accessors) {
|
|
15
|
+
const handle = db[name];
|
|
16
|
+
if (!handle) continue;
|
|
17
|
+
const rows = Array.from(handle.iter());
|
|
18
|
+
if (rows.length > 0) return handle;
|
|
19
|
+
empty = handle;
|
|
20
|
+
}
|
|
21
|
+
return empty;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function findSessionId(
|
|
25
|
+
db: SnapshotDb,
|
|
26
|
+
sessionDefId: string,
|
|
27
|
+
participants: readonly bigint[],
|
|
28
|
+
): bigint | undefined {
|
|
29
|
+
const handle = firstPopulatedHandle(db, "visible_session", "session");
|
|
30
|
+
if (!handle) return undefined;
|
|
31
|
+
const wanted = new Set(participants.map((participant) => String(participant)));
|
|
32
|
+
const participantHandle = firstPopulatedHandle(
|
|
33
|
+
db,
|
|
34
|
+
"visible_session_participant",
|
|
35
|
+
"session_participant",
|
|
36
|
+
);
|
|
37
|
+
const participantRows = participantHandle ? Array.from(participantHandle.iter()) : [];
|
|
38
|
+
const matching: bigint[] = [];
|
|
39
|
+
for (const raw of handle.iter()) {
|
|
40
|
+
const r = raw as unknown as { sessionDefId: string; sessionId: unknown; lifecycle?: unknown };
|
|
41
|
+
if (r.sessionDefId === sessionDefId && !TERMINAL_SESSION_LIFECYCLES.has(String(r.lifecycle))) {
|
|
42
|
+
const sessionId =
|
|
43
|
+
typeof r.sessionId === "bigint" ? r.sessionId : BigInt(String(r.sessionId ?? 0));
|
|
44
|
+
if (participantRows.length === 0) {
|
|
45
|
+
matching.push(sessionId);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const sessionIdText = String(sessionId);
|
|
49
|
+
const actual = new Set(
|
|
50
|
+
participantRows
|
|
51
|
+
.map((row) => row as unknown as { sessionId: unknown; actorId: unknown })
|
|
52
|
+
.filter((row) => String(row.sessionId) === sessionIdText)
|
|
53
|
+
.map((row) => String(row.actorId)),
|
|
54
|
+
);
|
|
55
|
+
if (
|
|
56
|
+
actual.size === wanted.size &&
|
|
57
|
+
[...wanted].every((participant) => actual.has(participant))
|
|
58
|
+
) {
|
|
59
|
+
matching.push(sessionId);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return matching.sort((a, b) => (a > b ? -1 : a < b ? 1 : 0))[0];
|
|
64
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { SpellkaveSession } from "../../connection";
|
|
2
|
+
|
|
3
|
+
export async function pauseWorldTicks(session: SpellkaveSession): Promise<void> {
|
|
4
|
+
await (
|
|
5
|
+
session.conn.reducers as unknown as {
|
|
6
|
+
pauseTick(args: Record<string, never>): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
).pauseTick({});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Advances one world clock tick at a logical scene boundary.
|
|
12
|
+
// Wraps the `manual_tick` reducer (ManualClockAdvance event kind), requiring
|
|
13
|
+
// the publisher credential or a directly bound body with live frontier.pace_time.
|
|
14
|
+
// `world_tick` remains scheduler-only.
|
|
15
|
+
// Convention (#675 CR-5): call once per logical scene boundary in walkthrough
|
|
16
|
+
// scripts so the per-tick consequence cascade (autonomy jobs, claim TTLs,
|
|
17
|
+
// affordance re-derivation) fires at each scene transition.
|
|
18
|
+
export async function advanceScene(session: SpellkaveSession): Promise<void> {
|
|
19
|
+
await (
|
|
20
|
+
session.conn.reducers as unknown as {
|
|
21
|
+
manualTick(args: Record<string, never>): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
).manualTick({});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function advanceScenes(session: SpellkaveSession, ticks: number): Promise<void> {
|
|
27
|
+
if (!Number.isInteger(ticks) || ticks < 0) {
|
|
28
|
+
throw new Error(`advanceScenes requires a non-negative integer, got ${ticks}`);
|
|
29
|
+
}
|
|
30
|
+
await (
|
|
31
|
+
session.conn.reducers as unknown as {
|
|
32
|
+
manualTicks(args: { ticks: number }): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
).manualTicks({ ticks });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function advanceTurns(
|
|
38
|
+
session: SpellkaveSession,
|
|
39
|
+
sessionId: bigint,
|
|
40
|
+
count: number,
|
|
41
|
+
): Promise<void> {
|
|
42
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
43
|
+
throw new Error(`advanceTurns requires a non-negative integer, got ${count}`);
|
|
44
|
+
}
|
|
45
|
+
await (
|
|
46
|
+
session.conn.reducers as unknown as {
|
|
47
|
+
rAdvanceTurns(args: { sessionId: bigint; count: number }): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
).rAdvanceTurns({ sessionId, count });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function setDebugRngSeed(
|
|
53
|
+
session: SpellkaveSession,
|
|
54
|
+
seed: bigint | number | string | null,
|
|
55
|
+
): Promise<void> {
|
|
56
|
+
const reducers = session.conn.reducers as unknown as {
|
|
57
|
+
debugSetRngSeed?: (args: { seed?: bigint }) => Promise<void>;
|
|
58
|
+
};
|
|
59
|
+
if (typeof reducers.debugSetRngSeed !== "function") {
|
|
60
|
+
throw new Error(
|
|
61
|
+
"debugSetRngSeed reducer is unavailable; publish the module with --features test-rng-seed",
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
await reducers.debugSetRngSeed({ seed: seed === null ? undefined : BigInt(seed) });
|
|
65
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Trace-row lookup for r_inscribe_trace — split out to keep the wrapper
|
|
2
|
+
// itself under the sdk_loc_ceiling (LR-102).
|
|
3
|
+
|
|
4
|
+
import type { SnapshotDb } from "../../connection";
|
|
5
|
+
|
|
6
|
+
export function findTraceId(
|
|
7
|
+
db: SnapshotDb,
|
|
8
|
+
kind: string,
|
|
9
|
+
locationRef: string,
|
|
10
|
+
inscribedBy: string,
|
|
11
|
+
): bigint | undefined {
|
|
12
|
+
// `trace` is private (ADR-0120, #915); read back through `visible_trace`. The
|
|
13
|
+
// inscriber perceives its own fresh trace at inscription time (ADR-0040 seam A),
|
|
14
|
+
// so the just-written row is in the inscriber's perception-bounded view.
|
|
15
|
+
const handle = db.visible_trace;
|
|
16
|
+
if (!handle) return undefined;
|
|
17
|
+
for (const raw of handle.iter()) {
|
|
18
|
+
const r = raw as unknown as {
|
|
19
|
+
kind: string;
|
|
20
|
+
locationRef: string;
|
|
21
|
+
inscribedBy: string;
|
|
22
|
+
traceId: unknown;
|
|
23
|
+
};
|
|
24
|
+
if (r.kind === kind && r.locationRef === locationRef && r.inscribedBy === inscribedBy) {
|
|
25
|
+
return typeof r.traceId === "bigint" ? r.traceId : BigInt(String(r.traceId ?? 0));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { DbConnection } from "@spellkave/bindings";
|
|
2
|
+
|
|
3
|
+
export type ReducerActorId = bigint;
|
|
4
|
+
export type RManipulateParams = Parameters<DbConnection["reducers"]["rManipulate"]>[0];
|
|
5
|
+
export type TargetRefArg = RManipulateParams["targetRef"];
|
|
6
|
+
export type ManipulationArgsArg = RManipulateParams["args"];
|
|
7
|
+
export type FrameItemDestinationArg = NonNullable<ManipulationArgsArg["destination"]>;
|
|
8
|
+
export type FrameSpatialRefArg = NonNullable<ManipulationArgsArg["buildTarget"]>;
|
|
9
|
+
|
|
10
|
+
export function actorEntityId(value: string | bigint): ReducerActorId {
|
|
11
|
+
if (typeof value === "bigint") return value;
|
|
12
|
+
return BigInt(stripEntityPrefix(value));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function targetRefFromString(value: string): TargetRefArg {
|
|
16
|
+
const trimmed = value.trim();
|
|
17
|
+
if (trimmed === "world") return { tag: "World" } as TargetRefArg;
|
|
18
|
+
const [kind, rawId] = splitRef(trimmed, "entity");
|
|
19
|
+
switch (kind) {
|
|
20
|
+
case "entity":
|
|
21
|
+
return { tag: "Entity", value: BigInt(rawId) } as TargetRefArg;
|
|
22
|
+
case "item":
|
|
23
|
+
return { tag: "Item", value: BigInt(rawId) } as TargetRefArg;
|
|
24
|
+
case "place_node":
|
|
25
|
+
return { tag: "PlaceNode", value: rawId } as TargetRefArg;
|
|
26
|
+
case "place_node_set": {
|
|
27
|
+
const [originPlaceNodeId, rangeRaw, ...templateParts] = rawId.split(":");
|
|
28
|
+
if (!originPlaceNodeId || !rangeRaw || templateParts.length === 0) {
|
|
29
|
+
throw new Error(`unsupported target ref '${value}'`);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
tag: "PlaceNodeSet",
|
|
33
|
+
value: {
|
|
34
|
+
originPlaceNodeId,
|
|
35
|
+
rangeMillimeters: Number(rangeRaw),
|
|
36
|
+
affordanceTemplateId: templateParts.join(":"),
|
|
37
|
+
},
|
|
38
|
+
} as TargetRefArg;
|
|
39
|
+
}
|
|
40
|
+
case "place_edge":
|
|
41
|
+
return { tag: "PlaceEdge", value: BigInt(rawId) } as TargetRefArg;
|
|
42
|
+
case "location":
|
|
43
|
+
return { tag: "Location", value: rawId } as TargetRefArg;
|
|
44
|
+
default:
|
|
45
|
+
throw new Error(`unsupported target ref '${value}'`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Inverse of `targetRefFromString`: serialize a typed `TargetRef` (e.g. the
|
|
51
|
+
* `affordance.target_ref` column — the source of truth for target kind,
|
|
52
|
+
* ADR-0033 D1) into the canonical string the reducer transport accepts.
|
|
53
|
+
* Round-trip: `targetRefFromString(targetRefToString(ref))` is identity.
|
|
54
|
+
*/
|
|
55
|
+
export function targetRefToString(ref: TargetRefArg): string {
|
|
56
|
+
switch (ref.tag) {
|
|
57
|
+
case "Entity":
|
|
58
|
+
return `entity:${ref.value}`;
|
|
59
|
+
case "Item":
|
|
60
|
+
return `item:${ref.value}`;
|
|
61
|
+
case "PlaceNode":
|
|
62
|
+
return `place_node:${ref.value}`;
|
|
63
|
+
case "PlaceNodeSet":
|
|
64
|
+
return `place_node_set:${ref.value.originPlaceNodeId}:${ref.value.rangeMillimeters}:${ref.value.affordanceTemplateId}`;
|
|
65
|
+
case "PlaceEdge":
|
|
66
|
+
return `place_edge:${ref.value}`;
|
|
67
|
+
case "Location":
|
|
68
|
+
return `location:${ref.value}`;
|
|
69
|
+
case "World":
|
|
70
|
+
return "world";
|
|
71
|
+
default:
|
|
72
|
+
throw new Error(`unsupported target ref tag '${(ref as { tag: string }).tag}'`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function stripEntityPrefix(value: string): string {
|
|
77
|
+
return value.startsWith("entity:") ? value.slice("entity:".length) : value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function splitRef(value: string, defaultKind: string): [string, string] {
|
|
81
|
+
const index = value.indexOf(":");
|
|
82
|
+
if (index < 0) return [defaultKind, value];
|
|
83
|
+
return [value.slice(0, index), value.slice(index + 1)];
|
|
84
|
+
}
|