@myclaw163/game-agent-runtime 0.0.0-stage → 0.1.1
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/README.md +67 -2
- package/dist/framework/action-diagnostics.d.ts +11 -0
- package/dist/framework/action-diagnostics.js +67 -0
- package/dist/framework/capability-registry.d.ts +58 -0
- package/dist/framework/capability-registry.js +325 -0
- package/dist/framework/contracts.d.ts +121 -0
- package/dist/framework/contracts.js +2 -0
- package/dist/framework/control/control-scheduler.d.ts +69 -0
- package/dist/framework/control/control-scheduler.js +1016 -0
- package/dist/framework/control/domain-set.d.ts +18 -0
- package/dist/framework/control/domain-set.js +70 -0
- package/dist/framework/control/index.d.ts +3 -0
- package/dist/framework/control/index.js +3 -0
- package/dist/framework/control/types.d.ts +142 -0
- package/dist/framework/control/types.js +13 -0
- package/dist/framework/control-coordinator.d.ts +7 -0
- package/dist/framework/control-coordinator.js +7 -0
- package/dist/framework/events/event-catalog.d.ts +23 -0
- package/dist/framework/events/event-catalog.js +52 -0
- package/dist/framework/events/event-delivery-service.d.ts +33 -0
- package/dist/framework/events/event-delivery-service.js +165 -0
- package/dist/framework/events/event-ingress.d.ts +28 -0
- package/dist/framework/events/event-ingress.js +76 -0
- package/dist/framework/events/event-repository.d.ts +38 -0
- package/dist/framework/events/event-repository.js +20 -0
- package/dist/framework/events/index.d.ts +7 -0
- package/dist/framework/events/index.js +7 -0
- package/dist/framework/events/memory-event-repository.d.ts +77 -0
- package/dist/framework/events/memory-event-repository.js +909 -0
- package/dist/framework/events/runtime-resume.d.ts +11 -0
- package/dist/framework/events/runtime-resume.js +7 -0
- package/dist/framework/events/sqlite-event-repository.d.ts +94 -0
- package/dist/framework/events/sqlite-event-repository.js +2044 -0
- package/dist/framework/events/store-compatibility.d.ts +17 -0
- package/dist/framework/events/store-compatibility.js +17 -0
- package/dist/framework/events/types.d.ts +244 -0
- package/dist/framework/events/types.js +39 -0
- package/dist/framework/index.d.ts +7 -0
- package/dist/framework/index.js +7 -0
- package/dist/framework/load-node-sqlite.d.ts +5 -0
- package/dist/framework/load-node-sqlite.js +19 -0
- package/dist/framework/mailbox/coalescer.d.ts +30 -0
- package/dist/framework/mailbox/coalescer.js +45 -0
- package/dist/framework/mailbox/host-notification-sink.d.ts +31 -0
- package/dist/framework/mailbox/host-notification-sink.js +13 -0
- package/dist/framework/mailbox/identity.d.ts +15 -0
- package/dist/framework/mailbox/identity.js +28 -0
- package/dist/framework/mailbox/index.d.ts +7 -0
- package/dist/framework/mailbox/index.js +7 -0
- package/dist/framework/mailbox/mailbox-dispatcher.d.ts +65 -0
- package/dist/framework/mailbox/mailbox-dispatcher.js +242 -0
- package/dist/framework/mailbox/mailbox-repository.d.ts +17 -0
- package/dist/framework/mailbox/mailbox-repository.js +9 -0
- package/dist/framework/mailbox/notification-projector.d.ts +25 -0
- package/dist/framework/mailbox/notification-projector.js +50 -0
- package/dist/framework/mailbox/types.d.ts +128 -0
- package/dist/framework/mailbox/types.js +1 -0
- package/dist/framework/modules/runtime-module-loader-hooks.mjs +156 -0
- package/dist/framework/modules/runtime-module-loader.d.ts +14 -0
- package/dist/framework/modules/runtime-module-loader.js +64 -0
- package/dist/framework/modules/runtime-module-output.d.ts +7 -0
- package/dist/framework/modules/runtime-module-output.js +15 -0
- package/dist/framework/modules/runtime-module-worker-bootstrap.mjs +23 -0
- package/dist/framework/participation-manager.d.ts +94 -0
- package/dist/framework/participation-manager.js +185 -0
- package/dist/framework/persistence/async-sqlite-persistence-worker.d.ts +1 -0
- package/dist/framework/persistence/async-sqlite-persistence-worker.js +62 -0
- package/dist/framework/persistence/memory-first-persistence.d.ts +36 -0
- package/dist/framework/persistence/memory-first-persistence.js +459 -0
- package/dist/framework/plugins/contracts.d.ts +345 -0
- package/dist/framework/plugins/contracts.js +1 -0
- package/dist/framework/plugins/index.d.ts +4 -0
- package/dist/framework/plugins/index.js +4 -0
- package/dist/framework/plugins/runtime-event-identity.d.ts +3 -0
- package/dist/framework/plugins/runtime-event-identity.js +11 -0
- package/dist/framework/plugins/runtime-input-journal.d.ts +85 -0
- package/dist/framework/plugins/runtime-input-journal.js +716 -0
- package/dist/framework/plugins/runtime-plugin-scheduler.d.ts +188 -0
- package/dist/framework/plugins/runtime-plugin-scheduler.js +1660 -0
- package/dist/framework/runtime.d.ts +264 -0
- package/dist/framework/runtime.js +1811 -0
- package/dist/framework/serial-task-queue.d.ts +6 -0
- package/dist/framework/serial-task-queue.js +12 -0
- package/dist/framework/state/contracts.d.ts +30 -0
- package/dist/framework/state/contracts.js +1 -0
- package/dist/framework/state/current-state-store.d.ts +17 -0
- package/dist/framework/state/current-state-store.js +153 -0
- package/dist/framework/state/durable-agent-state-snapshot.d.ts +61 -0
- package/dist/framework/state/durable-agent-state-snapshot.js +162 -0
- package/dist/framework/state/freshness.d.ts +7 -0
- package/dist/framework/state/freshness.js +52 -0
- package/dist/framework/state/history/contracts.d.ts +133 -0
- package/dist/framework/state/history/contracts.js +1 -0
- package/dist/framework/state/history/history-writer.d.ts +55 -0
- package/dist/framework/state/history/history-writer.js +157 -0
- package/dist/framework/state/history/memory-state-history-repository.d.ts +25 -0
- package/dist/framework/state/history/memory-state-history-repository.js +199 -0
- package/dist/framework/state/history/sqlite-state-history-repository.d.ts +28 -0
- package/dist/framework/state/history/sqlite-state-history-repository.js +565 -0
- package/dist/framework/state/history/trajectory-projector.d.ts +53 -0
- package/dist/framework/state/history/trajectory-projector.js +130 -0
- package/dist/framework/state/index.d.ts +12 -0
- package/dist/framework/state/index.js +12 -0
- package/dist/framework/state/state-ingress.d.ts +18 -0
- package/dist/framework/state/state-ingress.js +68 -0
- package/dist/framework/state/state-provider.d.ts +11 -0
- package/dist/framework/state/state-provider.js +56 -0
- package/dist/framework/state/types.d.ts +107 -0
- package/dist/framework/state/types.js +19 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mailbox/adapters/claude-channel-adapter.d.ts +34 -0
- package/dist/mailbox/adapters/claude-channel-adapter.js +71 -0
- package/dist/mailbox/adapters/claude-channel-socket.d.ts +56 -0
- package/dist/mailbox/adapters/claude-channel-socket.js +279 -0
- package/dist/mailbox/adapters/desktop-event-socket.d.ts +81 -0
- package/dist/mailbox/adapters/desktop-event-socket.js +339 -0
- package/dist/mailbox/agent-mailbox-coordinator.d.ts +104 -0
- package/dist/mailbox/agent-mailbox-coordinator.js +412 -0
- package/dist/mailbox/host-monitor-rate-gate.d.ts +60 -0
- package/dist/mailbox/host-monitor-rate-gate.js +93 -0
- package/dist/mailbox/mailbox-event-batch-delivery.d.ts +73 -0
- package/dist/mailbox/mailbox-event-batch-delivery.js +251 -0
- package/dist/mailbox/mailbox-host-relay.d.ts +52 -0
- package/dist/mailbox/mailbox-host-relay.js +299 -0
- package/dist/mailbox/mailbox-plugin.d.ts +58 -0
- package/dist/mailbox/mailbox-plugin.js +128 -0
- package/dist/mailbox/mailbox-service.d.ts +128 -0
- package/dist/mailbox/mailbox-service.js +582 -0
- package/dist/memory/contracts.d.ts +12 -0
- package/dist/memory/contracts.js +1 -0
- package/dist/memory/strategy-memory-rpc.d.ts +21 -0
- package/dist/memory/strategy-memory-rpc.js +86 -0
- package/dist/memory/strategy-memory.d.ts +15 -0
- package/dist/memory/strategy-memory.js +95 -0
- package/dist/observability/detail-capture.d.ts +25 -0
- package/dist/observability/detail-capture.js +136 -0
- package/dist/observability/index.d.ts +5 -0
- package/dist/observability/index.js +5 -0
- package/dist/observability/process-timing.d.ts +14 -0
- package/dist/observability/process-timing.js +56 -0
- package/dist/observability/recorder.d.ts +56 -0
- package/dist/observability/recorder.js +202 -0
- package/dist/observability/redaction.d.ts +30 -0
- package/dist/observability/redaction.js +166 -0
- package/dist/observability/reporter.d.ts +32 -0
- package/dist/observability/reporter.js +140 -0
- package/dist/observability/sinks.d.ts +36 -0
- package/dist/observability/sinks.js +121 -0
- package/dist/observability/types.d.ts +81 -0
- package/dist/observability/types.js +1 -0
- package/dist/owner/control-channel.d.ts +42 -0
- package/dist/owner/control-channel.js +188 -0
- package/dist/owner/parent-process-watch.d.ts +15 -0
- package/dist/owner/parent-process-watch.js +40 -0
- package/dist/owner/process-identity.d.ts +10 -0
- package/dist/owner/process-identity.js +65 -0
- package/dist/owner/runtime-lock.d.ts +5 -0
- package/dist/owner/runtime-lock.js +47 -0
- package/dist/perception/binding-resolution.d.ts +9 -0
- package/dist/perception/binding-resolution.js +14 -0
- package/dist/perception/contracts.d.ts +85 -0
- package/dist/perception/contracts.js +1 -0
- package/dist/perception/map-concurrent.d.ts +2 -0
- package/dist/perception/map-concurrent.js +12 -0
- package/dist/perception/perception-host.d.ts +114 -0
- package/dist/perception/perception-host.js +338 -0
- package/dist/perception/perception-worker-host.d.ts +11 -0
- package/dist/perception/perception-worker-host.js +44 -0
- package/dist/perception/perception-worker.d.ts +21 -0
- package/dist/perception/perception-worker.js +77 -0
- package/dist/perception/strategy-names.d.ts +7 -0
- package/dist/perception/strategy-names.js +22 -0
- package/dist/sdk/perception.d.ts +5 -0
- package/dist/sdk/perception.js +6 -0
- package/dist/sdk/strategy.d.ts +6 -0
- package/dist/sdk/strategy.js +3 -0
- package/dist/strategy/activation-manager.d.ts +105 -0
- package/dist/strategy/activation-manager.js +367 -0
- package/dist/strategy/behavior-decision-history.d.ts +20 -0
- package/dist/strategy/behavior-decision-history.js +59 -0
- package/dist/strategy/behavior-instance-snapshot.d.ts +31 -0
- package/dist/strategy/behavior-instance-snapshot.js +145 -0
- package/dist/strategy/contracts.d.ts +189 -0
- package/dist/strategy/contracts.js +1 -0
- package/dist/strategy/control-driver.d.ts +25 -0
- package/dist/strategy/control-driver.js +131 -0
- package/dist/strategy/in-process-runner.d.ts +25 -0
- package/dist/strategy/in-process-runner.js +40 -0
- package/dist/strategy/strategy-host.d.ts +165 -0
- package/dist/strategy/strategy-host.js +1456 -0
- package/dist/strategy/strategy-script-runner.d.ts +81 -0
- package/dist/strategy/strategy-script-runner.js +235 -0
- package/dist/strategy/strategy-worker-host.d.ts +27 -0
- package/dist/strategy/strategy-worker-host.js +96 -0
- package/dist/strategy/thread-strategy-executor.d.ts +120 -0
- package/dist/strategy/thread-strategy-executor.js +778 -0
- package/examples/grid-game.mjs +129 -0
- package/examples/grid-perception-worker.mjs +6 -0
- package/examples/grid-perception.mjs +12 -0
- package/examples/grid-strategy-worker.mjs +11 -0
- package/examples/grid-strategy.mjs +13 -0
- package/examples/run-grid-game.mjs +24 -0
- package/package.json +53 -4
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import { cloneCanonical, runtimeScopeKey } from '../types.js';
|
|
3
|
+
function defaultShouldStore(previous, current) {
|
|
4
|
+
return previous.spaceId !== current.spaceId
|
|
5
|
+
|| previous.areaId !== current.areaId
|
|
6
|
+
|| JSON.stringify(previous.position) !== JSON.stringify(current.position)
|
|
7
|
+
|| JSON.stringify(previous.attributes) !== JSON.stringify(current.attributes);
|
|
8
|
+
}
|
|
9
|
+
export class TrajectoryProjector {
|
|
10
|
+
activeScopeKey = null;
|
|
11
|
+
lastStored = new Map();
|
|
12
|
+
visiblePlayers = new Set();
|
|
13
|
+
maxAnchorIntervalMs;
|
|
14
|
+
shouldStorePosition;
|
|
15
|
+
idFactory;
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
this.maxAnchorIntervalMs = options.maxAnchorIntervalMs ?? 30_000;
|
|
18
|
+
this.shouldStorePosition = options.shouldStorePosition ?? defaultShouldStore;
|
|
19
|
+
this.idFactory = options.idFactory ?? randomUUID;
|
|
20
|
+
}
|
|
21
|
+
project(frame) {
|
|
22
|
+
this.ensureScope(frame.scope);
|
|
23
|
+
const records = [];
|
|
24
|
+
const visibleThisFrame = new Set();
|
|
25
|
+
for (const observation of frame.observations) {
|
|
26
|
+
if (!observation.subjectId)
|
|
27
|
+
continue;
|
|
28
|
+
if (observation.subjectKind === 'player' && observation.visible === false) {
|
|
29
|
+
if (this.visiblePlayers.delete(observation.subjectId)) {
|
|
30
|
+
records.push(this.makeRecord(frame, observation, 'visibility_lost'));
|
|
31
|
+
}
|
|
32
|
+
this.lastStored.delete(observation.subjectId);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (observation.subjectKind === 'player') {
|
|
36
|
+
visibleThisFrame.add(observation.subjectId);
|
|
37
|
+
if (!this.visiblePlayers.has(observation.subjectId)) {
|
|
38
|
+
records.push(this.makeRecord(frame, observation, 'visibility_gained'));
|
|
39
|
+
}
|
|
40
|
+
this.visiblePlayers.add(observation.subjectId);
|
|
41
|
+
}
|
|
42
|
+
if (observation.position === undefined)
|
|
43
|
+
continue;
|
|
44
|
+
const current = {
|
|
45
|
+
position: cloneCanonical(observation.position),
|
|
46
|
+
spaceId: observation.spaceId,
|
|
47
|
+
areaId: observation.areaId,
|
|
48
|
+
attributes: observation.attributes ? cloneCanonical(observation.attributes) : undefined,
|
|
49
|
+
receivedAtMs: frame.receivedAtMs,
|
|
50
|
+
};
|
|
51
|
+
const previous = this.lastStored.get(observation.subjectId);
|
|
52
|
+
const anchorExpired = !!previous
|
|
53
|
+
&& frame.receivedAtMs - previous.receivedAtMs >= this.maxAnchorIntervalMs;
|
|
54
|
+
if (!previous || anchorExpired || this.shouldStorePosition(previous, current)) {
|
|
55
|
+
records.push(this.makeRecord(frame, observation, 'position'));
|
|
56
|
+
this.lastStored.set(observation.subjectId, current);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (frame.visibleSetComplete) {
|
|
60
|
+
for (const subjectId of Array.from(this.visiblePlayers)) {
|
|
61
|
+
if (visibleThisFrame.has(subjectId))
|
|
62
|
+
continue;
|
|
63
|
+
records.push(this.makeRecord(frame, {
|
|
64
|
+
subjectId,
|
|
65
|
+
subjectKind: 'player',
|
|
66
|
+
visible: false,
|
|
67
|
+
}, 'visibility_lost'));
|
|
68
|
+
this.visiblePlayers.delete(subjectId);
|
|
69
|
+
this.lastStored.delete(subjectId);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return records;
|
|
73
|
+
}
|
|
74
|
+
streamGapStarted(scope, connectionId, sourceKey, receivedAtMs = Date.now()) {
|
|
75
|
+
this.ensureScope(scope);
|
|
76
|
+
this.visiblePlayers.clear();
|
|
77
|
+
this.lastStored.clear();
|
|
78
|
+
return [this.systemRecord(scope, connectionId, sourceKey, 'stream_gap_started', receivedAtMs)];
|
|
79
|
+
}
|
|
80
|
+
streamGapEnded(scope, connectionId, sourceKey, receivedAtMs = Date.now()) {
|
|
81
|
+
this.ensureScope(scope);
|
|
82
|
+
return [this.systemRecord(scope, connectionId, sourceKey, 'stream_gap_ended', receivedAtMs)];
|
|
83
|
+
}
|
|
84
|
+
reset() {
|
|
85
|
+
this.activeScopeKey = null;
|
|
86
|
+
this.lastStored.clear();
|
|
87
|
+
this.visiblePlayers.clear();
|
|
88
|
+
}
|
|
89
|
+
ensureScope(scope) {
|
|
90
|
+
const key = runtimeScopeKey(scope);
|
|
91
|
+
if (this.activeScopeKey === key)
|
|
92
|
+
return;
|
|
93
|
+
this.activeScopeKey = key;
|
|
94
|
+
this.lastStored.clear();
|
|
95
|
+
this.visiblePlayers.clear();
|
|
96
|
+
}
|
|
97
|
+
makeRecord(frame, observation, kind) {
|
|
98
|
+
return {
|
|
99
|
+
recordId: this.idFactory(),
|
|
100
|
+
scope: cloneCanonical(frame.scope),
|
|
101
|
+
connectionId: frame.connectionId,
|
|
102
|
+
subjectId: observation.subjectId,
|
|
103
|
+
subjectKind: observation.subjectKind,
|
|
104
|
+
kind,
|
|
105
|
+
sourceType: frame.sourceType,
|
|
106
|
+
sourceKey: frame.sourceKey,
|
|
107
|
+
sourceSeq: frame.sourceSeq,
|
|
108
|
+
backendTick: frame.backendTick,
|
|
109
|
+
observedAtMs: frame.observedAtMs,
|
|
110
|
+
receivedAtMs: frame.receivedAtMs,
|
|
111
|
+
spaceId: observation.spaceId,
|
|
112
|
+
areaId: observation.areaId,
|
|
113
|
+
position: observation.position === undefined ? undefined : cloneCanonical(observation.position),
|
|
114
|
+
attributes: observation.attributes ? cloneCanonical(observation.attributes) : undefined,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
systemRecord(scope, connectionId, sourceKey, kind, receivedAtMs) {
|
|
118
|
+
return {
|
|
119
|
+
recordId: this.idFactory(),
|
|
120
|
+
scope: cloneCanonical(scope),
|
|
121
|
+
connectionId,
|
|
122
|
+
subjectId: '__stream__',
|
|
123
|
+
subjectKind: 'system',
|
|
124
|
+
kind,
|
|
125
|
+
sourceType: 'local_lifecycle',
|
|
126
|
+
sourceKey,
|
|
127
|
+
receivedAtMs,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './contracts.js';
|
|
3
|
+
export * from './current-state-store.js';
|
|
4
|
+
export * from './freshness.js';
|
|
5
|
+
export * from './state-ingress.js';
|
|
6
|
+
export * from './state-provider.js';
|
|
7
|
+
export * from './durable-agent-state-snapshot.js';
|
|
8
|
+
export * from './history/contracts.js';
|
|
9
|
+
export * from './history/memory-state-history-repository.js';
|
|
10
|
+
export * from './history/sqlite-state-history-repository.js';
|
|
11
|
+
export * from './history/trajectory-projector.js';
|
|
12
|
+
export * from './history/history-writer.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './contracts.js';
|
|
3
|
+
export * from './current-state-store.js';
|
|
4
|
+
export * from './freshness.js';
|
|
5
|
+
export * from './state-ingress.js';
|
|
6
|
+
export * from './state-provider.js';
|
|
7
|
+
export * from './durable-agent-state-snapshot.js';
|
|
8
|
+
export * from './history/contracts.js';
|
|
9
|
+
export * from './history/memory-state-history-repository.js';
|
|
10
|
+
export * from './history/sqlite-state-history-repository.js';
|
|
11
|
+
export * from './history/trajectory-projector.js';
|
|
12
|
+
export * from './history/history-writer.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CurrentStateWriter, StateIngressObserver } from './contracts.js';
|
|
2
|
+
import type { ApplyStateResult, CurrentStateSnapshot, RuntimeConnectionState, RuntimeScope, StateFactPatch, StateFrameEnvelope } from './types.js';
|
|
3
|
+
export declare class StateIngress<TState = unknown> {
|
|
4
|
+
private readonly store;
|
|
5
|
+
private readonly observers;
|
|
6
|
+
constructor(store: CurrentStateWriter<TState>, observers?: StateIngressObserver<TState>[]);
|
|
7
|
+
addObserver(observer: StateIngressObserver<TState>): () => void;
|
|
8
|
+
openScope(scope: RuntimeScope, atMs?: number): CurrentStateSnapshot<TState>;
|
|
9
|
+
applyFacts(patches: StateFactPatch[], atMs?: number): CurrentStateSnapshot<TState>;
|
|
10
|
+
connectionOpened(connectionId: string, atMs?: number): CurrentStateSnapshot<TState>;
|
|
11
|
+
connectionChanged(connectionId: string, state: RuntimeConnectionState, atMs?: number): CurrentStateSnapshot<TState>;
|
|
12
|
+
livenessReceived(connectionId: string, atMs?: number): boolean;
|
|
13
|
+
accept(frame: StateFrameEnvelope<TState>): ApplyStateResult<TState>;
|
|
14
|
+
snapshot(): CurrentStateSnapshot<TState>;
|
|
15
|
+
private notifyConnection;
|
|
16
|
+
private runObserver;
|
|
17
|
+
private reportObserverError;
|
|
18
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export class StateIngress {
|
|
2
|
+
store;
|
|
3
|
+
observers = new Set();
|
|
4
|
+
constructor(store, observers = []) {
|
|
5
|
+
this.store = store;
|
|
6
|
+
for (const observer of observers)
|
|
7
|
+
this.observers.add(observer);
|
|
8
|
+
}
|
|
9
|
+
addObserver(observer) {
|
|
10
|
+
this.observers.add(observer);
|
|
11
|
+
return () => this.observers.delete(observer);
|
|
12
|
+
}
|
|
13
|
+
openScope(scope, atMs = Date.now()) {
|
|
14
|
+
return this.store.openScope(scope, atMs);
|
|
15
|
+
}
|
|
16
|
+
applyFacts(patches, atMs = Date.now()) {
|
|
17
|
+
return this.store.applyFacts(patches, atMs);
|
|
18
|
+
}
|
|
19
|
+
connectionOpened(connectionId, atMs = Date.now()) {
|
|
20
|
+
const snapshot = this.store.openConnection(connectionId, atMs);
|
|
21
|
+
this.notifyConnection(snapshot.scope, connectionId, 'connected', atMs);
|
|
22
|
+
return snapshot;
|
|
23
|
+
}
|
|
24
|
+
connectionChanged(connectionId, state, atMs = Date.now()) {
|
|
25
|
+
const snapshot = this.store.markConnectionState(connectionId, state, atMs);
|
|
26
|
+
if (snapshot.meta.connectionId === connectionId && snapshot.meta.connectionState === state) {
|
|
27
|
+
this.notifyConnection(snapshot.scope, connectionId, state, atMs);
|
|
28
|
+
}
|
|
29
|
+
return snapshot;
|
|
30
|
+
}
|
|
31
|
+
livenessReceived(connectionId, atMs = Date.now()) {
|
|
32
|
+
return this.store.markLiveness(connectionId, atMs);
|
|
33
|
+
}
|
|
34
|
+
accept(frame) {
|
|
35
|
+
const result = this.store.applyBackendState(frame);
|
|
36
|
+
if (result.accepted) {
|
|
37
|
+
for (const observer of this.observers) {
|
|
38
|
+
this.runObserver(observer, () => observer.onFrameAccepted?.(frame, result));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
snapshot() {
|
|
44
|
+
return this.store.snapshot();
|
|
45
|
+
}
|
|
46
|
+
notifyConnection(scope, connectionId, state, atMs) {
|
|
47
|
+
for (const observer of this.observers) {
|
|
48
|
+
this.runObserver(observer, () => observer.onConnectionChanged?.(scope, connectionId, state, atMs));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
runObserver(observer, run) {
|
|
52
|
+
try {
|
|
53
|
+
const pending = run();
|
|
54
|
+
if (pending && typeof pending.then === 'function') {
|
|
55
|
+
void pending.catch((error) => this.reportObserverError(observer, error));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
this.reportObserverError(observer, error);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
reportObserverError(observer, error) {
|
|
63
|
+
try {
|
|
64
|
+
observer.onObserverError?.(error);
|
|
65
|
+
}
|
|
66
|
+
catch { }
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CurrentStateReader, StateViewProjector } from './contracts.js';
|
|
2
|
+
import { StateFreshnessEvaluator } from './freshness.js';
|
|
3
|
+
import type { CurrentStateSnapshot, RuntimeScope, StateProviderResult } from './types.js';
|
|
4
|
+
export declare class StateProvider<TState = unknown, TView = CurrentStateSnapshot<TState>> {
|
|
5
|
+
private readonly reader;
|
|
6
|
+
private readonly freshness;
|
|
7
|
+
private readonly projector;
|
|
8
|
+
constructor(reader: CurrentStateReader<TState> | null, freshness?: StateFreshnessEvaluator, projector?: StateViewProjector<TState, TView>);
|
|
9
|
+
read(expectedScope?: RuntimeScope | null, nowMs?: number): StateProviderResult<TView>;
|
|
10
|
+
private ownerUnavailable;
|
|
11
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { StateFreshnessEvaluator } from './freshness.js';
|
|
2
|
+
const identityProjector = (snapshot) => snapshot;
|
|
3
|
+
export class StateProvider {
|
|
4
|
+
reader;
|
|
5
|
+
freshness;
|
|
6
|
+
projector;
|
|
7
|
+
constructor(reader, freshness = new StateFreshnessEvaluator(), projector = identityProjector) {
|
|
8
|
+
this.reader = reader;
|
|
9
|
+
this.freshness = freshness;
|
|
10
|
+
this.projector = projector;
|
|
11
|
+
}
|
|
12
|
+
read(expectedScope, nowMs = Date.now()) {
|
|
13
|
+
if (!this.reader)
|
|
14
|
+
return this.ownerUnavailable(expectedScope ?? null);
|
|
15
|
+
try {
|
|
16
|
+
const snapshot = this.reader.snapshot();
|
|
17
|
+
const stateStatus = this.freshness.evaluate(snapshot, expectedScope, nowMs);
|
|
18
|
+
if (stateStatus.status === 'unavailable') {
|
|
19
|
+
return {
|
|
20
|
+
state: null,
|
|
21
|
+
stateStatus,
|
|
22
|
+
stateError: {
|
|
23
|
+
code: stateStatus.reason === 'scope_mismatch' ? 'STATE_SCOPE_MISMATCH' : 'STATE_NOT_READY',
|
|
24
|
+
message: 'Current owner state is unavailable for the requested scope.',
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return { state: this.projector(snapshot), stateStatus };
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return this.ownerUnavailable(expectedScope ?? null, 'provider_error');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
ownerUnavailable(scope, reason = 'owner_unavailable') {
|
|
35
|
+
const stateStatus = {
|
|
36
|
+
status: 'unavailable',
|
|
37
|
+
semantics: 'latest_received',
|
|
38
|
+
source: null,
|
|
39
|
+
scope,
|
|
40
|
+
connectionId: null,
|
|
41
|
+
connectionState: 'owner_unavailable',
|
|
42
|
+
receivedAtMs: null,
|
|
43
|
+
ageMs: null,
|
|
44
|
+
backendTick: null,
|
|
45
|
+
reason,
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
state: null,
|
|
49
|
+
stateStatus,
|
|
50
|
+
stateError: {
|
|
51
|
+
code: 'OWNER_NOT_READY',
|
|
52
|
+
message: 'Active game owner is unavailable.',
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export interface RuntimeScope {
|
|
2
|
+
adapterId: string;
|
|
3
|
+
accountId: string;
|
|
4
|
+
sessionId: string;
|
|
5
|
+
segmentId: string;
|
|
6
|
+
segmentSeq?: number;
|
|
7
|
+
}
|
|
8
|
+
export type FactLifetime = 'session' | 'segment';
|
|
9
|
+
export interface FactProvenance {
|
|
10
|
+
sourceType: string;
|
|
11
|
+
sourceKey?: string;
|
|
12
|
+
receivedAtMs?: number;
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
}
|
|
15
|
+
export interface StateFactPatch {
|
|
16
|
+
key: string;
|
|
17
|
+
value: unknown;
|
|
18
|
+
lifetime: FactLifetime;
|
|
19
|
+
provenance: FactProvenance;
|
|
20
|
+
}
|
|
21
|
+
export interface StoredStateFact extends StateFactPatch {
|
|
22
|
+
updatedAtMs: number;
|
|
23
|
+
}
|
|
24
|
+
export type RuntimeConnectionState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting';
|
|
25
|
+
export interface StateFrameEnvelope<TState = unknown> {
|
|
26
|
+
scope: RuntimeScope;
|
|
27
|
+
sourceId?: string;
|
|
28
|
+
connectionId: string;
|
|
29
|
+
payload: TState;
|
|
30
|
+
/** Stable key for persistence/idempotency within this scope. */
|
|
31
|
+
sourceKey: string;
|
|
32
|
+
/** Server sequence when the transport provides one. */
|
|
33
|
+
sourceSeq?: number | string;
|
|
34
|
+
backendTick?: number;
|
|
35
|
+
observedAtMs?: number;
|
|
36
|
+
receivedAtMs: number;
|
|
37
|
+
receivedMonotonicMs?: number;
|
|
38
|
+
}
|
|
39
|
+
export interface CurrentStateMeta {
|
|
40
|
+
sourceId: string | null;
|
|
41
|
+
connectionId: string | null;
|
|
42
|
+
connectionState: RuntimeConnectionState;
|
|
43
|
+
awaitingFirstFrame: boolean;
|
|
44
|
+
receiveSeq: number;
|
|
45
|
+
lastSourceSeq: number | string | null;
|
|
46
|
+
backendTick: number | null;
|
|
47
|
+
lastStateSourceKey: string | null;
|
|
48
|
+
lastStateReceivedAtMs: number | null;
|
|
49
|
+
lastStateReceivedMonotonicMs: number | null;
|
|
50
|
+
lastLivenessAtMs: number | null;
|
|
51
|
+
gapSinceMs: number | null;
|
|
52
|
+
}
|
|
53
|
+
export interface CurrentStateSnapshot<TState = unknown> {
|
|
54
|
+
scope: RuntimeScope | null;
|
|
55
|
+
backendState: TState | null;
|
|
56
|
+
globalFacts: Record<string, unknown>;
|
|
57
|
+
facts: {
|
|
58
|
+
session: Record<string, StoredStateFact>;
|
|
59
|
+
segment: Record<string, StoredStateFact>;
|
|
60
|
+
};
|
|
61
|
+
meta: CurrentStateMeta;
|
|
62
|
+
}
|
|
63
|
+
export type ApplyStateRejectedReason = 'scope_not_open' | 'scope_mismatch' | 'connection_not_open' | 'stale_connection' | 'duplicate_or_out_of_order';
|
|
64
|
+
export type ApplyStateResult<TState = unknown> = {
|
|
65
|
+
accepted: true;
|
|
66
|
+
receiveSeq: number;
|
|
67
|
+
firstFrameAfterConnect: boolean;
|
|
68
|
+
snapshot: CurrentStateSnapshot<TState>;
|
|
69
|
+
} | {
|
|
70
|
+
accepted: false;
|
|
71
|
+
reason: ApplyStateRejectedReason;
|
|
72
|
+
snapshot: CurrentStateSnapshot<TState>;
|
|
73
|
+
};
|
|
74
|
+
export type StateAvailability = 'fresh' | 'stale' | 'unavailable';
|
|
75
|
+
export interface StateFreshnessPolicy {
|
|
76
|
+
/** Optional heartbeat/liveness deadline. Omit for transports without liveness evidence. */
|
|
77
|
+
maxLivenessSilenceMs?: number;
|
|
78
|
+
/** Use only when the adapter promises periodic state frames. */
|
|
79
|
+
maxStateSilenceMs?: number;
|
|
80
|
+
}
|
|
81
|
+
export type StateFreshnessPolicyResolver = (sourceId: string | null, snapshot: CurrentStateSnapshot) => StateFreshnessPolicy | undefined;
|
|
82
|
+
export interface StateStatus {
|
|
83
|
+
status: StateAvailability;
|
|
84
|
+
semantics: 'latest_received';
|
|
85
|
+
source: 'owner_memory' | 'durable_snapshot' | null;
|
|
86
|
+
scope: RuntimeScope | null;
|
|
87
|
+
connectionId: string | null;
|
|
88
|
+
connectionState: RuntimeConnectionState | 'owner_unavailable';
|
|
89
|
+
receivedAtMs: number | null;
|
|
90
|
+
ageMs: number | null;
|
|
91
|
+
backendTick: number | null;
|
|
92
|
+
reason: string | null;
|
|
93
|
+
/** True only when the adapter or the durable participation says this is a final snapshot. */
|
|
94
|
+
terminal?: boolean;
|
|
95
|
+
}
|
|
96
|
+
export interface StateProviderResult<TView = CurrentStateSnapshot> {
|
|
97
|
+
state: TView | null;
|
|
98
|
+
stateStatus: StateStatus;
|
|
99
|
+
stateError?: {
|
|
100
|
+
code: string;
|
|
101
|
+
message: string;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
export declare function runtimeScopeKey(scope: RuntimeScope): string;
|
|
105
|
+
export declare function sameRuntimeScope(a: RuntimeScope | null | undefined, b: RuntimeScope | null | undefined): boolean;
|
|
106
|
+
export declare function sameRuntimeSession(a: RuntimeScope | null | undefined, b: RuntimeScope | null | undefined): boolean;
|
|
107
|
+
export declare function cloneCanonical<T>(value: T): T;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function runtimeScopeKey(scope) {
|
|
2
|
+
return [scope.adapterId, scope.accountId, scope.sessionId, scope.segmentId].join('\u001f');
|
|
3
|
+
}
|
|
4
|
+
export function sameRuntimeScope(a, b) {
|
|
5
|
+
return !!a && !!b
|
|
6
|
+
&& a.adapterId === b.adapterId
|
|
7
|
+
&& a.accountId === b.accountId
|
|
8
|
+
&& a.sessionId === b.sessionId
|
|
9
|
+
&& a.segmentId === b.segmentId;
|
|
10
|
+
}
|
|
11
|
+
export function sameRuntimeSession(a, b) {
|
|
12
|
+
return !!a && !!b
|
|
13
|
+
&& a.adapterId === b.adapterId
|
|
14
|
+
&& a.accountId === b.accountId
|
|
15
|
+
&& a.sessionId === b.sessionId;
|
|
16
|
+
}
|
|
17
|
+
export function cloneCanonical(value) {
|
|
18
|
+
return structuredClone(value);
|
|
19
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './framework/index.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './framework/index.js';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { CanonicalEvent, EventRepository } from '../../framework/events/index.js';
|
|
2
|
+
import type { RuntimeReadyView } from '../../framework/runtime.js';
|
|
3
|
+
import { type PreparedAgentMailboxBatch } from '../agent-mailbox-coordinator.js';
|
|
4
|
+
import type { MailboxDriver } from '../mailbox-plugin.js';
|
|
5
|
+
export declare const CLAUDE_CHANNEL_COALESCE_MS = 220;
|
|
6
|
+
export interface ClaudeChannelMailboxAdapterOptions {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly presentEvent: import('../agent-mailbox-coordinator.js').AgentMailboxCoordinatorOptions['presentEvent'];
|
|
9
|
+
readonly admitEvent?: (event: CanonicalEvent) => boolean;
|
|
10
|
+
onDelivered?: (events: readonly CanonicalEvent[]) => void;
|
|
11
|
+
repository: EventRepository;
|
|
12
|
+
readyView: () => RuntimeReadyView;
|
|
13
|
+
consumerId: string;
|
|
14
|
+
now?: () => number;
|
|
15
|
+
diagnostic?: (name: string, attributes?: Record<string, unknown>) => void;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Mailbox projection and delivery state for the Claude Channel consumer.
|
|
19
|
+
* Transport is handled separately by the Owner-side Channel socket sink.
|
|
20
|
+
*/
|
|
21
|
+
export declare class ClaudeChannelMailboxAdapter implements MailboxDriver {
|
|
22
|
+
readonly consumerId: string;
|
|
23
|
+
readonly adapter: MailboxDriver['adapter'];
|
|
24
|
+
private readonly coordinator;
|
|
25
|
+
constructor(options: ClaudeChannelMailboxAdapterOptions);
|
|
26
|
+
prepare(readyView?: RuntimeReadyView): PreparedAgentMailboxBatch;
|
|
27
|
+
ack(deliveryToken: string, acceptanceLevel?: import('../../framework/mailbox/types.js').MailboxAcceptanceLevel): boolean;
|
|
28
|
+
renew(deliveryToken: string): boolean;
|
|
29
|
+
release(deliveryToken: string): boolean;
|
|
30
|
+
observeEvents(events: readonly CanonicalEvent[]): void;
|
|
31
|
+
flush(): Promise<void>;
|
|
32
|
+
waitForDrain(shouldAbort?: () => boolean): Promise<boolean>;
|
|
33
|
+
stop(): Promise<void>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { AgentMailboxCoordinator, } from '../agent-mailbox-coordinator.js';
|
|
2
|
+
export const CLAUDE_CHANNEL_COALESCE_MS = 220;
|
|
3
|
+
/**
|
|
4
|
+
* Mailbox projection and delivery state for the Claude Channel consumer.
|
|
5
|
+
* Transport is handled separately by the Owner-side Channel socket sink.
|
|
6
|
+
*/
|
|
7
|
+
export class ClaudeChannelMailboxAdapter {
|
|
8
|
+
consumerId;
|
|
9
|
+
adapter;
|
|
10
|
+
coordinator;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.consumerId = options.consumerId;
|
|
13
|
+
this.adapter = {
|
|
14
|
+
id: options.id,
|
|
15
|
+
acknowledgement: 'on_delivery',
|
|
16
|
+
consumerId: this.consumerId,
|
|
17
|
+
};
|
|
18
|
+
this.coordinator = new AgentMailboxCoordinator({
|
|
19
|
+
onDelivered: options.onDelivered,
|
|
20
|
+
admitEvent: options.admitEvent,
|
|
21
|
+
repository: options.repository,
|
|
22
|
+
readyView: options.readyView,
|
|
23
|
+
presentEvent: options.presentEvent,
|
|
24
|
+
// Channel v2 is Event-only. Other Mailbox adapters keep their own
|
|
25
|
+
// compatibility projections instead of repeating Ready State here.
|
|
26
|
+
compactState: () => null,
|
|
27
|
+
consumerId: this.consumerId,
|
|
28
|
+
coalesceMs: CLAUDE_CHANNEL_COALESCE_MS,
|
|
29
|
+
now: options.now,
|
|
30
|
+
diagnostic: options.diagnostic,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
prepare(readyView) {
|
|
34
|
+
const prepared = this.coordinator.prepareEvents({
|
|
35
|
+
readyView,
|
|
36
|
+
deliveryVia: 'claude_channel',
|
|
37
|
+
ownerIdPrefix: 'claude_channel',
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
...prepared,
|
|
41
|
+
batch: {
|
|
42
|
+
...prepared.batch,
|
|
43
|
+
// The repository selects newest-first for legacy pull consumers.
|
|
44
|
+
// Channel Events must be applied in occurrence order now that there is
|
|
45
|
+
// no repeated State snapshot masking backwards transitions.
|
|
46
|
+
events: [...prepared.batch.events].reverse(),
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
ack(deliveryToken, acceptanceLevel) {
|
|
51
|
+
return this.coordinator.ack(deliveryToken, acceptanceLevel);
|
|
52
|
+
}
|
|
53
|
+
renew(deliveryToken) {
|
|
54
|
+
return this.coordinator.renew(deliveryToken);
|
|
55
|
+
}
|
|
56
|
+
release(deliveryToken) {
|
|
57
|
+
return this.coordinator.release(deliveryToken);
|
|
58
|
+
}
|
|
59
|
+
observeEvents(events) {
|
|
60
|
+
this.coordinator.observeEvents(events);
|
|
61
|
+
}
|
|
62
|
+
flush() {
|
|
63
|
+
return this.coordinator.flush();
|
|
64
|
+
}
|
|
65
|
+
waitForDrain(shouldAbort = () => false) {
|
|
66
|
+
return this.coordinator.waitForDrain(shouldAbort);
|
|
67
|
+
}
|
|
68
|
+
stop() {
|
|
69
|
+
return this.coordinator.stop();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { MailboxEventBatchAcceptance, MailboxEventBatchEnvelope, MailboxEventBatchSink } from '../mailbox-event-batch-delivery.js';
|
|
2
|
+
export interface ChannelSocketEndpoint {
|
|
3
|
+
readonly socket_path: string;
|
|
4
|
+
readonly token: string;
|
|
5
|
+
}
|
|
6
|
+
export interface ChannelSocketProtocol {
|
|
7
|
+
readonly schema: string;
|
|
8
|
+
readonly payloadSchema: string;
|
|
9
|
+
readonly maxFrameCharacters: number;
|
|
10
|
+
}
|
|
11
|
+
export interface ClaudeChannelSocketOptions {
|
|
12
|
+
readonly protocol: ChannelSocketProtocol;
|
|
13
|
+
receiptTimeoutMs?: number;
|
|
14
|
+
reconnectBaseMs?: number;
|
|
15
|
+
reconnectMaxMs?: number;
|
|
16
|
+
now?: () => number;
|
|
17
|
+
endpoint: () => ChannelSocketEndpoint | null;
|
|
18
|
+
onConnected?: () => void;
|
|
19
|
+
diagnostic?: (name: string, attributes?: Record<string, unknown>) => void;
|
|
20
|
+
}
|
|
21
|
+
export declare class ClaudeChannelSocketDeliveryError extends Error {
|
|
22
|
+
readonly code: string;
|
|
23
|
+
readonly retryable: boolean;
|
|
24
|
+
constructor(code: string, message: string, retryable?: boolean);
|
|
25
|
+
}
|
|
26
|
+
/** Owner-side IPC client. Mailbox batches flow outward to the Channel MCP process. */
|
|
27
|
+
export declare class ClaudeChannelSocketSink implements MailboxEventBatchSink {
|
|
28
|
+
private readonly protocol;
|
|
29
|
+
private readonly receiptTimeoutMs;
|
|
30
|
+
private readonly reconnectBaseMs;
|
|
31
|
+
private readonly reconnectMaxMs;
|
|
32
|
+
private readonly now;
|
|
33
|
+
private readonly endpoint;
|
|
34
|
+
private readonly onConnected?;
|
|
35
|
+
private readonly diagnostic?;
|
|
36
|
+
private socket?;
|
|
37
|
+
private ready;
|
|
38
|
+
private stopped;
|
|
39
|
+
private connectionId?;
|
|
40
|
+
private sequence;
|
|
41
|
+
private reconnectAttempt;
|
|
42
|
+
private reconnectTimer?;
|
|
43
|
+
private inputBuffer;
|
|
44
|
+
private pendingReceipt?;
|
|
45
|
+
constructor(options: ClaudeChannelSocketOptions);
|
|
46
|
+
start(): void;
|
|
47
|
+
deliver(envelope: MailboxEventBatchEnvelope, signal: AbortSignal): Promise<MailboxEventBatchAcceptance>;
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
private connect;
|
|
50
|
+
private onData;
|
|
51
|
+
private handleFrame;
|
|
52
|
+
private onClose;
|
|
53
|
+
private scheduleReconnect;
|
|
54
|
+
private rejectPending;
|
|
55
|
+
private abortError;
|
|
56
|
+
}
|