@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,34 @@
|
|
|
1
|
+
import type { TurnStateRow as DbTurnStateRow } from "../../binding_rows";
|
|
2
|
+
import {
|
|
3
|
+
type RequiredTables,
|
|
4
|
+
registerReaderManifest,
|
|
5
|
+
type SnapshotDb,
|
|
6
|
+
} from "../../reader-manifest";
|
|
7
|
+
import { asNumber, asString, rows } from "./_helpers";
|
|
8
|
+
|
|
9
|
+
export const requiredTables: RequiredTables<["visible_turn_state"]> = ["visible_turn_state"];
|
|
10
|
+
registerReaderManifest({ requiredTables });
|
|
11
|
+
|
|
12
|
+
/** The kernel-authoritative turn scheduler's current state. `current_actor`/
|
|
13
|
+
* `scope_session_id` are entity/session ids as decimal strings (comparison-
|
|
14
|
+
* friendly, matching sibling readers' id-field convention); `turn` is the
|
|
15
|
+
* monotonic turn counter. */
|
|
16
|
+
export interface TurnStateSnapshot {
|
|
17
|
+
turn: number;
|
|
18
|
+
current_actor: string;
|
|
19
|
+
active_scheduler_id: string;
|
|
20
|
+
scope_session_id: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Read the singleton turn-scheduler row, or `null` before it has applied —
|
|
24
|
+
* or while its scope is a session this caller is not in. */
|
|
25
|
+
export function readTurnState(db: SnapshotDb): TurnStateSnapshot | null {
|
|
26
|
+
const row = rows<DbTurnStateRow>(db, "visible_turn_state")[0];
|
|
27
|
+
if (row === undefined) return null;
|
|
28
|
+
return {
|
|
29
|
+
turn: asNumber(row.turn),
|
|
30
|
+
current_actor: asString(row.currentActor),
|
|
31
|
+
active_scheduler_id: asString(row.activeSchedulerId),
|
|
32
|
+
scope_session_id: asString(row.scopeSessionId),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Reader: recent world events.
|
|
2
|
+
// Mechanical split from readers.ts; no new functionality.
|
|
3
|
+
|
|
4
|
+
import type { WorldEventRow as DbWorldEventRow } from "../../binding_rows";
|
|
5
|
+
import {
|
|
6
|
+
type RequiredTables,
|
|
7
|
+
registerReaderManifest,
|
|
8
|
+
type SnapshotDb,
|
|
9
|
+
} from "../../reader-manifest";
|
|
10
|
+
import { asNumber, asString, kindToString, optString, rows } from "./_helpers";
|
|
11
|
+
|
|
12
|
+
// `world_event` is private (#915 PR-8); read the per-caller
|
|
13
|
+
// `visible_world_event` dispatch view instead.
|
|
14
|
+
export const requiredTables: RequiredTables<["visible_world_event"]> = ["visible_world_event"];
|
|
15
|
+
registerReaderManifest({ requiredTables });
|
|
16
|
+
|
|
17
|
+
export interface WorldEventRow {
|
|
18
|
+
id: string;
|
|
19
|
+
tick: number;
|
|
20
|
+
kind: string;
|
|
21
|
+
summary?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Return the most recent `n` events ordered newest-first by tick (id-desc tiebreaker).
|
|
26
|
+
*/
|
|
27
|
+
export function readRecentEvents(db: SnapshotDb, n: number): WorldEventRow[] {
|
|
28
|
+
if (n <= 0) return [];
|
|
29
|
+
const all = rows<DbWorldEventRow>(db, "visible_world_event").map(toEventRow);
|
|
30
|
+
all.sort((a, b) => (b.tick !== a.tick ? b.tick - a.tick : b.id.localeCompare(a.id)));
|
|
31
|
+
return all.slice(0, n);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function toEventRow(r: DbWorldEventRow): WorldEventRow {
|
|
35
|
+
const summary = optString(r.summary);
|
|
36
|
+
const out: WorldEventRow = {
|
|
37
|
+
id: asString(r.id),
|
|
38
|
+
tick: asNumber(r.tick),
|
|
39
|
+
kind: kindToString(r.kind),
|
|
40
|
+
};
|
|
41
|
+
if (summary !== undefined) out.summary = summary;
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Reader: world status (world_time + config).
|
|
2
|
+
// Mechanical split from readers.ts; no new functionality.
|
|
3
|
+
|
|
4
|
+
import type { ConfigRow, WorldTimeRow } from "../../binding_rows";
|
|
5
|
+
import {
|
|
6
|
+
type RequiredTables,
|
|
7
|
+
registerReaderManifest,
|
|
8
|
+
type SnapshotDb,
|
|
9
|
+
} from "../../reader-manifest";
|
|
10
|
+
import { asNumber, optNumber, optString, rows } from "./_helpers";
|
|
11
|
+
|
|
12
|
+
export const requiredTables: RequiredTables<["world_time", "config"]> = ["world_time", "config"];
|
|
13
|
+
registerReaderManifest({ requiredTables });
|
|
14
|
+
|
|
15
|
+
export interface WorldStatus {
|
|
16
|
+
tick: number;
|
|
17
|
+
worldName?: string;
|
|
18
|
+
// agentsEnabled removed — Item 1 (01-remove-agents-enabled.md) dropped this
|
|
19
|
+
// config column; it no longer appears in the generated config_table binding.
|
|
20
|
+
eventRetentionLimit?: number;
|
|
21
|
+
// The live autonomous-tick cadence (config.tick_interval_secs). Lets a control
|
|
22
|
+
// reflect the server's actual interval instead of a hardcoded default (#1133).
|
|
23
|
+
tickIntervalSecs?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Identity of THIS world instance (`config.world_instance_id`), minted once
|
|
26
|
+
* by the kernel's `init` reducer: preserved by a re-publish and a host
|
|
27
|
+
* restart, rotated by every wipe/reset that re-creates the database
|
|
28
|
+
* (sk-jay0.1.23.35.5).
|
|
29
|
+
*
|
|
30
|
+
* Absent when the module predates the column, or when the row carries the
|
|
31
|
+
* empty sentinel. Durable state kept OUTSIDE the database (npc brain
|
|
32
|
+
* checkpoints) treats absence as an unknown world and refuses to reuse
|
|
33
|
+
* memory bound to it — the same answer as a mismatch.
|
|
34
|
+
*/
|
|
35
|
+
worldInstanceId?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Read the world status header. Returns `null` only if neither `world_time`
|
|
40
|
+
* nor `config` have applied yet — callers should treat that as "still connecting".
|
|
41
|
+
*/
|
|
42
|
+
export function readWorldStatus(db: SnapshotDb): WorldStatus | null {
|
|
43
|
+
const timeRows = rows<WorldTimeRow>(db, "world_time");
|
|
44
|
+
const configRows = rows<ConfigRow>(db, "config");
|
|
45
|
+
if (timeRows.length === 0 && configRows.length === 0) return null;
|
|
46
|
+
|
|
47
|
+
const t = timeRows[0];
|
|
48
|
+
const c = configRows[0];
|
|
49
|
+
const status: WorldStatus = { tick: t ? asNumber(t.tick) : 0 };
|
|
50
|
+
if (c) {
|
|
51
|
+
const worldName = optString(c.worldName);
|
|
52
|
+
if (worldName !== undefined) status.worldName = worldName;
|
|
53
|
+
const evtLimit = optNumber(c.eventRetentionLimit);
|
|
54
|
+
if (evtLimit !== undefined && Number.isFinite(evtLimit)) status.eventRetentionLimit = evtLimit;
|
|
55
|
+
const interval = optNumber(c.tickIntervalSecs);
|
|
56
|
+
if (interval !== undefined && Number.isFinite(interval)) status.tickIntervalSecs = interval;
|
|
57
|
+
const instance = optString(c.worldInstanceId);
|
|
58
|
+
// The empty sentinel means "minted before this column existed"; report
|
|
59
|
+
// it as absent rather than as an instance nobody can match.
|
|
60
|
+
if (instance !== undefined && instance.length > 0) status.worldInstanceId = instance;
|
|
61
|
+
}
|
|
62
|
+
return status;
|
|
63
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import type { FrameItemDestinationArg, FrameSpatialRefArg, ManipulationArgsArg } from "./transport";
|
|
2
|
+
|
|
3
|
+
/** Every `args_json` key this parser reads (both spellings). A key outside
|
|
4
|
+
* this list never reaches the reducer; PLAYER tools use it to tell a caller
|
|
5
|
+
* which of its keys the world will not read (sk-jay0.1.46: `{"name": ...}`
|
|
6
|
+
* on a creation action was accepted and silently ignored). */
|
|
7
|
+
export const MANIPULATION_ARGS_WIRE_KEYS: readonly string[] = [
|
|
8
|
+
"selectedItems",
|
|
9
|
+
"selected_items",
|
|
10
|
+
"quantity",
|
|
11
|
+
"destination",
|
|
12
|
+
"stationRef",
|
|
13
|
+
"station_ref",
|
|
14
|
+
"buildTarget",
|
|
15
|
+
"build_target",
|
|
16
|
+
"requiredAbsentStateIds",
|
|
17
|
+
"required_absent_state_ids",
|
|
18
|
+
"dispatchText",
|
|
19
|
+
"dispatch_text",
|
|
20
|
+
"expectedParticipants",
|
|
21
|
+
"expected_participants",
|
|
22
|
+
"participantContext",
|
|
23
|
+
"participant_context",
|
|
24
|
+
"semanticContext",
|
|
25
|
+
"semantic_context",
|
|
26
|
+
"definitionRef",
|
|
27
|
+
"definition_ref",
|
|
28
|
+
"participantEntityIds",
|
|
29
|
+
"participant_entity_ids",
|
|
30
|
+
"privateInputRef",
|
|
31
|
+
"private_input_ref",
|
|
32
|
+
"dispatchOptions",
|
|
33
|
+
"dispatch_options",
|
|
34
|
+
"dispatchOptionRefs",
|
|
35
|
+
"dispatch_option_refs",
|
|
36
|
+
"dispatchContext",
|
|
37
|
+
"dispatch_context",
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
export function manipulationArgsFromJson(argsJson: string): ManipulationArgsArg {
|
|
41
|
+
const raw = parseObject(argsJson);
|
|
42
|
+
return {
|
|
43
|
+
selectedItems: arrayField(raw, "selectedItems", "selected_items").map(selectedItemFromUnknown),
|
|
44
|
+
quantity: optionalNumber(raw, "quantity"),
|
|
45
|
+
destination: optionalDestination(raw),
|
|
46
|
+
stationRef: optionalString(raw, "stationRef", "station_ref"),
|
|
47
|
+
buildTarget: optionalSpatialRef(raw, "buildTarget", "build_target"),
|
|
48
|
+
requiredAbsentStateIds: arrayField(
|
|
49
|
+
raw,
|
|
50
|
+
"requiredAbsentStateIds",
|
|
51
|
+
"required_absent_state_ids",
|
|
52
|
+
).map((item) => String(item)),
|
|
53
|
+
dispatchText: optionalString(raw, "dispatchText", "dispatch_text"),
|
|
54
|
+
expectedParticipants: optionalParticipants(raw),
|
|
55
|
+
participantContext: optionalString(raw, "participantContext", "participant_context"),
|
|
56
|
+
semanticContext: optionalSemanticContext(raw),
|
|
57
|
+
definitionRef: optionalString(raw, "definitionRef", "definition_ref"),
|
|
58
|
+
participantEntityIds: optionalArray(
|
|
59
|
+
raw,
|
|
60
|
+
"participantEntityIds",
|
|
61
|
+
"participant_entity_ids",
|
|
62
|
+
enrollmentIdFromUnknown,
|
|
63
|
+
),
|
|
64
|
+
privateInputRef: optionalString(raw, "privateInputRef", "private_input_ref"),
|
|
65
|
+
dispatchOptions: optionalArray(raw, "dispatchOptions", "dispatch_options", String),
|
|
66
|
+
dispatchOptionRefs: optionalArray(
|
|
67
|
+
raw,
|
|
68
|
+
"dispatchOptionRefs",
|
|
69
|
+
"dispatch_option_refs",
|
|
70
|
+
(value) => {
|
|
71
|
+
if (value === null || value === undefined) return undefined;
|
|
72
|
+
if (typeof value !== "string")
|
|
73
|
+
throw new Error("dispatch_option_refs must contain definition refs or null");
|
|
74
|
+
return value;
|
|
75
|
+
},
|
|
76
|
+
),
|
|
77
|
+
dispatchContext: optionalString(raw, "dispatchContext", "dispatch_context"),
|
|
78
|
+
} as ManipulationArgsArg;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function emptyManipulationArgs(): ManipulationArgsArg {
|
|
82
|
+
return manipulationArgsFromJson("{}");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function parseObject(json: string): Record<string, unknown> {
|
|
86
|
+
const parsed = JSON.parse(json.length === 0 ? "{}" : json) as unknown;
|
|
87
|
+
if (!isRecord(parsed)) throw new Error("manipulation args must be a JSON object");
|
|
88
|
+
return parsed;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function selectedItemFromUnknown(value: unknown): ManipulationArgsArg["selectedItems"][number] {
|
|
92
|
+
if (!isRecord(value)) throw new Error("selectedItems entries must be objects");
|
|
93
|
+
return {
|
|
94
|
+
binding: String(value.binding ?? ""),
|
|
95
|
+
itemId: u64FromUnknown(value.itemId ?? value.item_id ?? 0),
|
|
96
|
+
quantity: Number(value.quantity ?? 1),
|
|
97
|
+
itemDefId: optionalString(value, "itemDefId", "item_def_id"),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function optionalDestination(raw: Record<string, unknown>): FrameItemDestinationArg | undefined {
|
|
102
|
+
const value = raw.destination;
|
|
103
|
+
if (value === undefined || value === null) return undefined;
|
|
104
|
+
if (typeof value === "string")
|
|
105
|
+
return { tag: "ObjectContainer", value } as FrameItemDestinationArg;
|
|
106
|
+
if (!isRecord(value)) throw new Error("destination must be a string or object");
|
|
107
|
+
const tag = String(value.tag ?? value.kind ?? "");
|
|
108
|
+
const payload = value.value ?? value.id ?? "";
|
|
109
|
+
switch (tag) {
|
|
110
|
+
case "ActorInventory":
|
|
111
|
+
case "actor_inventory":
|
|
112
|
+
return { tag: "ActorInventory", value: String(payload) } as FrameItemDestinationArg;
|
|
113
|
+
case "Ground":
|
|
114
|
+
case "ground":
|
|
115
|
+
return { tag: "Ground", value: spatialRefFromUnknown(payload) } as FrameItemDestinationArg;
|
|
116
|
+
case "ObjectContainer":
|
|
117
|
+
case "object_container":
|
|
118
|
+
return { tag: "ObjectContainer", value: String(payload) } as FrameItemDestinationArg;
|
|
119
|
+
case "MachinePort":
|
|
120
|
+
case "machine_port":
|
|
121
|
+
return { tag: "MachinePort", value: String(payload) } as FrameItemDestinationArg;
|
|
122
|
+
default:
|
|
123
|
+
throw new Error(`unsupported destination '${tag}'`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function optionalSpatialRef(
|
|
128
|
+
raw: Record<string, unknown>,
|
|
129
|
+
camelKey: string,
|
|
130
|
+
snakeKey: string,
|
|
131
|
+
): FrameSpatialRefArg | undefined {
|
|
132
|
+
const value = raw[camelKey] ?? raw[snakeKey];
|
|
133
|
+
if (value === undefined || value === null) return undefined;
|
|
134
|
+
return spatialRefFromUnknown(value);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function spatialRefFromUnknown(value: unknown): FrameSpatialRefArg {
|
|
138
|
+
if (!isRecord(value)) throw new Error("spatial ref must be an object");
|
|
139
|
+
return { kind: String(value.kind ?? ""), id: String(value.id ?? "") } as FrameSpatialRefArg;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function optionalString(
|
|
143
|
+
raw: Record<string, unknown>,
|
|
144
|
+
camelKey: string,
|
|
145
|
+
snakeKey: string,
|
|
146
|
+
): string | undefined {
|
|
147
|
+
const value = raw[camelKey] ?? raw[snakeKey];
|
|
148
|
+
return value === undefined || value === null ? undefined : String(value);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function optionalParticipants(
|
|
152
|
+
raw: Record<string, unknown>,
|
|
153
|
+
): ManipulationArgsArg["expectedParticipants"] {
|
|
154
|
+
const value = raw.expectedParticipants ?? raw.expected_participants;
|
|
155
|
+
if (value === undefined || value === null) return undefined;
|
|
156
|
+
if (!Array.isArray(value)) throw new Error("expected_participants must be an array");
|
|
157
|
+
return value.map((item) => {
|
|
158
|
+
if (!isRecord(item)) throw new Error("expected_participants entries must be objects");
|
|
159
|
+
return {
|
|
160
|
+
roleId: String(item.roleId ?? item.role_id ?? ""),
|
|
161
|
+
entityId: u64FromUnknown(item.entityId ?? item.entity_id ?? 0),
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** ADR-0142 D-3 wire shape: `{"semantic_context": {"source_ref": "entity:<id>",
|
|
167
|
+
* "occurrence_id": <u64 or its decimal string form>}}`. Without this, a
|
|
168
|
+
* caller's `semantic_context` was silently dropped before ever reaching the
|
|
169
|
+
* `rManipulate` reducer call (`r_manipulate.ts`), which reads its args only
|
|
170
|
+
* from this function's return value — the server's prompt-gate (D-3) would
|
|
171
|
+
* see no `semantic_context` at all and reject every prompt-bound response as
|
|
172
|
+
* `prompt_context_required`. */
|
|
173
|
+
function optionalSemanticContext(
|
|
174
|
+
raw: Record<string, unknown>,
|
|
175
|
+
): ManipulationArgsArg["semanticContext"] {
|
|
176
|
+
const value = raw.semanticContext ?? raw.semantic_context;
|
|
177
|
+
if (value === undefined || value === null) return undefined;
|
|
178
|
+
if (!isRecord(value)) throw new Error("semantic_context must be an object");
|
|
179
|
+
const sourceRef = value.sourceRef ?? value.source_ref;
|
|
180
|
+
const occurrenceId = value.occurrenceId ?? value.occurrence_id;
|
|
181
|
+
if (sourceRef === undefined || sourceRef === null) {
|
|
182
|
+
throw new Error("semantic_context requires source_ref");
|
|
183
|
+
}
|
|
184
|
+
if (occurrenceId === undefined || occurrenceId === null) {
|
|
185
|
+
throw new Error("semantic_context requires occurrence_id");
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
sourceRef: String(sourceRef),
|
|
189
|
+
occurrenceId: u64FromUnknown(occurrenceId),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function optionalNumber(raw: Record<string, unknown>, key: string): number | undefined {
|
|
194
|
+
const value = raw[key];
|
|
195
|
+
return value === undefined || value === null ? undefined : Number(value);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function arrayField(raw: Record<string, unknown>, camelKey: string, snakeKey: string): unknown[] {
|
|
199
|
+
const value = raw[camelKey] ?? raw[snakeKey] ?? [];
|
|
200
|
+
if (!Array.isArray(value)) throw new Error(`${camelKey} must be an array`);
|
|
201
|
+
return value;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function u64FromUnknown(value: unknown): bigint {
|
|
205
|
+
return typeof value === "bigint" ? value : BigInt(String(value));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function enrollmentIdFromUnknown(value: unknown): bigint {
|
|
209
|
+
if (typeof value === "number" && !Number.isSafeInteger(value))
|
|
210
|
+
throw new Error("participant_entity_ids requires safe integers or decimal strings");
|
|
211
|
+
if (
|
|
212
|
+
!(
|
|
213
|
+
typeof value === "number" ||
|
|
214
|
+
typeof value === "bigint" ||
|
|
215
|
+
(typeof value === "string" && /^[0-9]+$/.test(value))
|
|
216
|
+
)
|
|
217
|
+
)
|
|
218
|
+
throw new Error("participant_entity_ids requires integer ids");
|
|
219
|
+
const id = u64FromUnknown(value);
|
|
220
|
+
if (id <= 0n || id > 18446744073709551615n)
|
|
221
|
+
throw new Error("participant_entity_ids requires nonzero u64 ids");
|
|
222
|
+
return id;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
226
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function optionalArray<T>(
|
|
230
|
+
raw: Record<string, unknown>,
|
|
231
|
+
camel: string,
|
|
232
|
+
snake: string,
|
|
233
|
+
convert: (value: unknown) => T,
|
|
234
|
+
): T[] | undefined {
|
|
235
|
+
const value = raw[camel] ?? raw[snake];
|
|
236
|
+
if (value === undefined || value === null) return undefined;
|
|
237
|
+
if (!Array.isArray(value)) throw new Error(`${snake} must be an array`);
|
|
238
|
+
return value.map(convert);
|
|
239
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { SnapshotDb } from "../../connection";
|
|
2
|
+
import type { ProvenanceRow } from "../../provenance";
|
|
3
|
+
import type { RManipulateFn } from "../actor_control_source";
|
|
4
|
+
|
|
5
|
+
function record(value: unknown): Record<string, unknown> | undefined {
|
|
6
|
+
return value !== null && typeof value === "object"
|
|
7
|
+
? (value as Record<string, unknown>)
|
|
8
|
+
: undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The pinned SpacetimeDB transport retains params.args by reference in the
|
|
12
|
+
* caller's ReducerResult event. Other sockets deliver Transaction events.
|
|
13
|
+
* Match this per-call object, never serialized values, subject, or timing.
|
|
14
|
+
* Driver-token wrappers copy the outer params but retain this inner object.
|
|
15
|
+
* Missing/ambiguous delivery context cannot establish a receipt. */
|
|
16
|
+
export function manipulationReceipt(
|
|
17
|
+
db: SnapshotDb,
|
|
18
|
+
subjectId: string,
|
|
19
|
+
args: () => Parameters<RManipulateFn>[0]["args"] | undefined,
|
|
20
|
+
) {
|
|
21
|
+
const table = db.visibleEvaluationProvenance ?? db.visible_evaluation_provenance;
|
|
22
|
+
const terminal = new Map<string, string>();
|
|
23
|
+
let settled = false;
|
|
24
|
+
const onInsert = (context?: unknown, value?: unknown) => {
|
|
25
|
+
const event = record(record(context)?.event);
|
|
26
|
+
const reducer = record(record(event?.value)?.reducer);
|
|
27
|
+
const payload = record(reducer?.args);
|
|
28
|
+
const row = record(value);
|
|
29
|
+
const expected = args();
|
|
30
|
+
if (
|
|
31
|
+
!row ||
|
|
32
|
+
row.subjectId !== subjectId ||
|
|
33
|
+
!expected ||
|
|
34
|
+
event?.tag !== "Reducer" ||
|
|
35
|
+
reducer?.name !== "r_manipulate" ||
|
|
36
|
+
payload?.args !== expected ||
|
|
37
|
+
(row?.kind !== "Affordance" && record(row?.kind)?.tag !== "Affordance")
|
|
38
|
+
)
|
|
39
|
+
return;
|
|
40
|
+
const status = typeof row.status === "string" ? row.status : record(row.status)?.tag;
|
|
41
|
+
if (
|
|
42
|
+
typeof row.provenanceId === "string" &&
|
|
43
|
+
typeof status === "string" &&
|
|
44
|
+
["Succeeded", "Failed", "RolledBack"].includes(status)
|
|
45
|
+
)
|
|
46
|
+
terminal.set(row.provenanceId, status);
|
|
47
|
+
};
|
|
48
|
+
if (table?.onInsert && table.removeOnInsert) table.onInsert(onInsert);
|
|
49
|
+
return {
|
|
50
|
+
settle: () => {
|
|
51
|
+
settled = true;
|
|
52
|
+
},
|
|
53
|
+
fail: () => {
|
|
54
|
+
terminal.clear();
|
|
55
|
+
settled = true;
|
|
56
|
+
},
|
|
57
|
+
matches: (row: ProvenanceRow) => settled && terminal.size === 1 && terminal.has(row.id),
|
|
58
|
+
succeeded: () => settled && terminal.size === 1 && [...terminal.values()][0] === "Succeeded",
|
|
59
|
+
// Caller ReducerResult rows arrive before the reducer promise resolves.
|
|
60
|
+
// A settled call cannot gain an attributable terminal receipt later.
|
|
61
|
+
unattributable: () => settled && terminal.size !== 1,
|
|
62
|
+
close: () => {
|
|
63
|
+
table?.removeOnInsert?.(onInsert);
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Per-reducer wrapper for r_actor_wake_up — connect-path perception seeding.
|
|
2
|
+
//
|
|
3
|
+
// The server reducer is idempotent and emits no provenance (it seeds
|
|
4
|
+
// observer_knowledge for the actor's current node). There is therefore no
|
|
5
|
+
// terminal signal to observe: this wrapper is fire-and-confirm. It reports
|
|
6
|
+
// "submitted" once the reducer invocation resolves, or "rejected" if the
|
|
7
|
+
// invocation itself throws. Callers that need to confirm perception should read
|
|
8
|
+
// observer_knowledge / observer_place_knowledge afterwards.
|
|
9
|
+
|
|
10
|
+
import type { DbConnection } from "@spellkave/bindings";
|
|
11
|
+
import type { EntityRef } from "../ids";
|
|
12
|
+
import { actorEntityId } from "./transport";
|
|
13
|
+
|
|
14
|
+
type RActorWakeUpFn = DbConnection["reducers"]["rActorWakeUp"];
|
|
15
|
+
|
|
16
|
+
export interface ActorWakeUpResult {
|
|
17
|
+
status: "submitted" | "rejected";
|
|
18
|
+
actor_id: EntityRef;
|
|
19
|
+
reason?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function rActorWakeUpWrapper(
|
|
23
|
+
actorId: EntityRef,
|
|
24
|
+
rActorWakeUp: RActorWakeUpFn,
|
|
25
|
+
): Promise<ActorWakeUpResult> {
|
|
26
|
+
try {
|
|
27
|
+
await rActorWakeUp({ actorId: actorEntityId(actorId) });
|
|
28
|
+
return { status: "submitted", actor_id: actorId };
|
|
29
|
+
} catch (err) {
|
|
30
|
+
return { status: "rejected", actor_id: actorId, reason: String(err) };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Per-reducer wrapper for r_inscribe_trace (#510 — leave-a-mark).
|
|
2
|
+
//
|
|
3
|
+
// STATE-OBSERVED. The reducer inserts into private `trace`; success is observed
|
|
4
|
+
// through subscribed `visible_trace` when the matching row reaches the cache.
|
|
5
|
+
|
|
6
|
+
import type { DbConnection } from "@spellkave/bindings";
|
|
7
|
+
import type { SnapshotDb } from "../../connection";
|
|
8
|
+
import { observeReducerAttempt } from "../../provenance";
|
|
9
|
+
import type { ProvenanceRingBuffer } from "../provenance_buffer";
|
|
10
|
+
import type { TickIntervalEstimator } from "../tick_cadence";
|
|
11
|
+
import { findTraceId } from "./trace_lookup";
|
|
12
|
+
|
|
13
|
+
export type RInscribeTraceFn = DbConnection["reducers"]["rInscribeTrace"];
|
|
14
|
+
|
|
15
|
+
// Derive the VisibilityClass enum shape from the generated reducer accessor's
|
|
16
|
+
// argument type rather than reaching into a non-barrel submodule (CLAUDE.md §5,
|
|
17
|
+
// §2 — types flow from generated bindings, drift-free).
|
|
18
|
+
export type VisibilityClass = Parameters<RInscribeTraceFn>[0]["visibility"];
|
|
19
|
+
|
|
20
|
+
export interface InscribeTraceResult {
|
|
21
|
+
status:
|
|
22
|
+
| "success"
|
|
23
|
+
| "rejected"
|
|
24
|
+
| "unobserved_timeout"
|
|
25
|
+
| "unobserved_no_provenance"
|
|
26
|
+
| "unobservable";
|
|
27
|
+
kind: string;
|
|
28
|
+
location_ref: string;
|
|
29
|
+
inscribed_by: string;
|
|
30
|
+
/** trace_id of the inscribed row, if observable. */
|
|
31
|
+
trace_id?: bigint;
|
|
32
|
+
reason?: string;
|
|
33
|
+
lagWarning: boolean;
|
|
34
|
+
observedAtTick: bigint;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function rInscribeTraceWrapper(
|
|
38
|
+
actorId: string,
|
|
39
|
+
kind: string,
|
|
40
|
+
locationKind: string,
|
|
41
|
+
locationRef: string,
|
|
42
|
+
payloadJson: string,
|
|
43
|
+
expiresAtTick: bigint | undefined,
|
|
44
|
+
visibility: VisibilityClass,
|
|
45
|
+
bundleId: string,
|
|
46
|
+
snapshotDb: () => SnapshotDb,
|
|
47
|
+
rInscribeTrace: RInscribeTraceFn,
|
|
48
|
+
timeoutMs: number,
|
|
49
|
+
pollMs: number,
|
|
50
|
+
tickEstimator?: TickIntervalEstimator,
|
|
51
|
+
provenanceBuffer?: ProvenanceRingBuffer,
|
|
52
|
+
): Promise<InscribeTraceResult> {
|
|
53
|
+
const inscribedBy = actorId;
|
|
54
|
+
const result = await observeReducerAttempt<bigint | null>({
|
|
55
|
+
db: snapshotDb,
|
|
56
|
+
subjectId: actorId,
|
|
57
|
+
invoke: () =>
|
|
58
|
+
rInscribeTrace({
|
|
59
|
+
kind,
|
|
60
|
+
locationKind,
|
|
61
|
+
locationRef,
|
|
62
|
+
payloadJson,
|
|
63
|
+
expiresAtTick: expiresAtTick ?? undefined,
|
|
64
|
+
inscribedBy,
|
|
65
|
+
visibility,
|
|
66
|
+
bundleId,
|
|
67
|
+
}),
|
|
68
|
+
successPredicate: (db) => findTraceId(db, kind, locationRef, inscribedBy) !== undefined,
|
|
69
|
+
collectSnapshot: (db) => findTraceId(db, kind, locationRef, inscribedBy) ?? null,
|
|
70
|
+
timeoutMs,
|
|
71
|
+
pollMs,
|
|
72
|
+
tickEstimator,
|
|
73
|
+
provenanceBuffer,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
if (result.status === "success") {
|
|
77
|
+
return {
|
|
78
|
+
status: "success",
|
|
79
|
+
kind,
|
|
80
|
+
location_ref: locationRef,
|
|
81
|
+
inscribed_by: inscribedBy,
|
|
82
|
+
trace_id: result.snapshot ?? undefined,
|
|
83
|
+
lagWarning: result.lagWarning,
|
|
84
|
+
observedAtTick: result.observedAtTick,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (result.status === "rejected") {
|
|
89
|
+
return {
|
|
90
|
+
status: "rejected",
|
|
91
|
+
kind,
|
|
92
|
+
location_ref: locationRef,
|
|
93
|
+
inscribed_by: inscribedBy,
|
|
94
|
+
reason: "r_inscribe_trace rejected by server",
|
|
95
|
+
lagWarning: result.lagWarning,
|
|
96
|
+
observedAtTick: result.observedAtTick,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
status: result.status,
|
|
102
|
+
kind,
|
|
103
|
+
location_ref: locationRef,
|
|
104
|
+
inscribed_by: inscribedBy,
|
|
105
|
+
reason: `r_inscribe_trace '${kind}@${locationRef}' not observed within ${timeoutMs}ms`,
|
|
106
|
+
lagWarning: result.lagWarning,
|
|
107
|
+
observedAtTick: result.observedAtTick,
|
|
108
|
+
};
|
|
109
|
+
}
|