@myclaw163/clawclaw-cli 0.7.4 → 0.7.6
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/docs/desktop-event-push.md +120 -0
- package/docs/runtime/events-and-state.md +1 -1
- package/package.json +3 -1
- package/skills/clawclaw/SKILL.md +9 -9
- package/skills/clawclaw/references/GAME-MECHANICS.md +2 -2
- package/src/assets/clawclaw-spatial-model.txt +142 -0
- package/src/commands/game/index.test.ts +167 -1
- package/src/commands/game/index.ts +124 -35
- package/src/commands/game-map.test.ts +8 -4
- package/src/lib/game-client.test.ts +15 -0
- package/src/lib/game-client.ts +4 -0
- package/src/lib/game-context.ts +6 -6
- package/src/lib/participation-status.ts +113 -0
- package/src/pipeline/canonical-event-presentation.test.ts +5 -4
- package/src/pipeline/event-format.test.ts +17 -0
- package/src/pipeline/event-format.ts +15 -1
- package/src/pipeline/game-sync-event-normalization.test.ts +58 -1
- package/src/pipeline/game-sync-event-normalization.ts +51 -0
- package/src/runtime/clawclaw/owner.ts +56 -3
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +189 -0
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +532 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-push.integration.test.ts +339 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.test.ts +207 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +372 -0
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +3 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +166 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.test.ts +194 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +262 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +28 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +49 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +77 -259
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +5 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +52 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +4 -3
- package/src/assets/clawclaw-ascii-map.txt +0 -40
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
1
|
import {
|
|
3
2
|
presentCanonicalEventForCclEvents,
|
|
4
3
|
} from '../../../../pipeline/canonical-event-presentation.js';
|
|
5
4
|
import { compactStateForEvents } from '../../../../pipeline/event-format.js';
|
|
6
5
|
import {
|
|
7
6
|
DEFAULT_EVENT_CONSUMER_ID,
|
|
8
|
-
type CanonicalEvent,
|
|
9
|
-
type ConsumerLease,
|
|
10
7
|
type EventRepository,
|
|
11
8
|
} from '../../../framework/events/index.js';
|
|
12
9
|
import {
|
|
@@ -15,21 +12,25 @@ import {
|
|
|
15
12
|
} from '../../../framework/mailbox/index.js';
|
|
16
13
|
import type { RuntimeReadyView } from '../../../framework/runtime.js';
|
|
17
14
|
import { clawclawMonitorDelayMs } from '../../agent-event-policy.js';
|
|
15
|
+
import { AgentMailboxCoordinator } from './agent-mailbox-coordinator.js';
|
|
18
16
|
import {
|
|
19
17
|
composeClawclawMonitorNotification,
|
|
20
18
|
monitorItemPayload,
|
|
21
19
|
} from './mailbox-presentation.js';
|
|
22
20
|
|
|
23
|
-
const EVENTS_LEASE_TTL_MS = 30_000;
|
|
24
|
-
const EVENTS_PREPARE_WAIT_MS = 5_000;
|
|
25
21
|
const MONITOR_FORMATTER_VERSION = 'clawclaw.monitor.v2';
|
|
22
|
+
const CCL_EVENTS_LEASE_TTL_MS = 30_000;
|
|
26
23
|
|
|
27
24
|
type ClawclawMailboxRepository = EventRepository & MailboxRepository;
|
|
28
25
|
|
|
29
26
|
export interface ClawclawMailboxServiceOptions {
|
|
30
27
|
repository: ClawclawMailboxRepository;
|
|
31
28
|
readyView: () => RuntimeReadyView;
|
|
29
|
+
/** One shared coordinator must be injected when a push adapter is active. */
|
|
30
|
+
agentMailbox?: AgentMailboxCoordinator;
|
|
32
31
|
consumerId?: string;
|
|
32
|
+
/** Disable the legacy short Monitor adapter for this Owner binding. */
|
|
33
|
+
monitorEnabled?: boolean;
|
|
33
34
|
now?: () => number;
|
|
34
35
|
diagnostic?: (name: string, attributes?: Record<string, unknown>) => void;
|
|
35
36
|
}
|
|
@@ -44,81 +45,11 @@ export interface PreparedClawclawEventsResponse {
|
|
|
44
45
|
deliveryToken?: string;
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
interface PreparedDelivery {
|
|
48
|
-
lease: ConsumerLease;
|
|
49
|
-
eventIds: string[];
|
|
50
|
-
readyEpochSeq: number;
|
|
51
|
-
sessionId: string;
|
|
52
|
-
segmentIds: string[];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
48
|
interface MonitorDelayWindow {
|
|
56
49
|
scopeKey: string;
|
|
57
50
|
deadlineMs: number;
|
|
58
51
|
}
|
|
59
52
|
|
|
60
|
-
function nonEmptyString(value: unknown): string | undefined {
|
|
61
|
-
return typeof value === 'string' && value.trim().length > 0 ? value : undefined;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function compactStrategyStateForEvents(
|
|
65
|
-
value: unknown,
|
|
66
|
-
runtime: string | undefined,
|
|
67
|
-
lastError: string | undefined,
|
|
68
|
-
phase: string | undefined,
|
|
69
|
-
alive: boolean | undefined,
|
|
70
|
-
): Record<string, unknown> | null {
|
|
71
|
-
const status = value && typeof value === 'object' && !Array.isArray(value)
|
|
72
|
-
? value as Record<string, unknown>
|
|
73
|
-
: {};
|
|
74
|
-
const id = runtime === 'not_selected' ? undefined : nonEmptyString(status.strategy);
|
|
75
|
-
const stageDoing = alive === false
|
|
76
|
-
? '角色已死亡,自动策略已停止。'
|
|
77
|
-
: phase === 'meeting'
|
|
78
|
-
? '会议阶段,自动策略已暂停。'
|
|
79
|
-
: phase === 'game_over'
|
|
80
|
-
? '本局结束,自动策略已停止。'
|
|
81
|
-
: phase === 'preparing'
|
|
82
|
-
? '等待进入游走阶段。'
|
|
83
|
-
: undefined;
|
|
84
|
-
const runtimeDoing = runtime === 'error'
|
|
85
|
-
? '策略执行出错,等待恢复。'
|
|
86
|
-
: runtime === 'not_selected'
|
|
87
|
-
? '尚未选择自动策略。'
|
|
88
|
-
: undefined;
|
|
89
|
-
const doing = stageDoing
|
|
90
|
-
?? runtimeDoing
|
|
91
|
-
?? nonEmptyString(status.summary)
|
|
92
|
-
?? (runtime === 'waiting_for_state' ? '等待最新游戏状态后继续。' : undefined);
|
|
93
|
-
const parameters = runtime !== 'not_selected' && status.parameters
|
|
94
|
-
&& typeof status.parameters === 'object'
|
|
95
|
-
&& !Array.isArray(status.parameters)
|
|
96
|
-
&& Object.keys(status.parameters).length > 0
|
|
97
|
-
? structuredClone(status.parameters)
|
|
98
|
-
: undefined;
|
|
99
|
-
const availability = status.dataAvailability
|
|
100
|
-
&& typeof status.dataAvailability === 'object'
|
|
101
|
-
&& !Array.isArray(status.dataAvailability)
|
|
102
|
-
? status.dataAvailability as Record<string, unknown>
|
|
103
|
-
: {};
|
|
104
|
-
const issue = runtime === 'error'
|
|
105
|
-
? lastError ?? 'strategy_error'
|
|
106
|
-
: runtime === 'not_selected'
|
|
107
|
-
? 'not_selected'
|
|
108
|
-
: availability.tasks === 'unavailable'
|
|
109
|
-
? 'tasks_unavailable'
|
|
110
|
-
: runtime && runtime !== 'active'
|
|
111
|
-
? runtime
|
|
112
|
-
: undefined;
|
|
113
|
-
const result = {
|
|
114
|
-
...(id ? { id } : {}),
|
|
115
|
-
...(doing ? { doing } : {}),
|
|
116
|
-
...(parameters ? { params: parameters } : {}),
|
|
117
|
-
...(issue ? { issue } : {}),
|
|
118
|
-
};
|
|
119
|
-
return Object.keys(result).length > 0 ? result : null;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
53
|
function unavailableEventsOutput(): Record<string, unknown> {
|
|
123
54
|
return {
|
|
124
55
|
schema: 'ccl.events.v5',
|
|
@@ -127,16 +58,26 @@ function unavailableEventsOutput(): Record<string, unknown> {
|
|
|
127
58
|
};
|
|
128
59
|
}
|
|
129
60
|
|
|
130
|
-
|
|
61
|
+
function cclEventsCompatibilityError(error: unknown): never {
|
|
62
|
+
if (error instanceof Error && error.message === 'agent_mailbox_delivery_busy') {
|
|
63
|
+
throw new Error('mailbox_events_delivery_busy');
|
|
64
|
+
}
|
|
65
|
+
if (error instanceof Error && error.message === 'agent_mailbox_stopped') {
|
|
66
|
+
throw new Error('clawclaw_mailbox_stopped');
|
|
67
|
+
}
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Legacy Monitor and `ccl events` adapters over the Agent Mailbox core. */
|
|
131
72
|
export class ClawclawMailboxService {
|
|
132
73
|
private readonly repository: ClawclawMailboxRepository;
|
|
133
74
|
private readonly readyView: () => RuntimeReadyView;
|
|
75
|
+
private readonly agentMailbox: AgentMailboxCoordinator;
|
|
76
|
+
private readonly ownsAgentMailbox: boolean;
|
|
134
77
|
private readonly consumerId: string;
|
|
78
|
+
private readonly monitorEnabled: boolean;
|
|
135
79
|
private readonly now: () => number;
|
|
136
80
|
private readonly diagnostic?: ClawclawMailboxServiceOptions['diagnostic'];
|
|
137
|
-
private readonly prepared = new Map<string, PreparedDelivery>();
|
|
138
|
-
private readonly preparedSlotWaiters = new Set<() => void>();
|
|
139
|
-
private prepareTail: Promise<void> = Promise.resolve();
|
|
140
81
|
private delayWindow?: MonitorDelayWindow;
|
|
141
82
|
private lastObservedReadyEpochSeq = 0;
|
|
142
83
|
private stopped = false;
|
|
@@ -145,8 +86,19 @@ export class ClawclawMailboxService {
|
|
|
145
86
|
this.repository = options.repository;
|
|
146
87
|
this.readyView = options.readyView;
|
|
147
88
|
this.consumerId = options.consumerId ?? DEFAULT_EVENT_CONSUMER_ID;
|
|
89
|
+
this.monitorEnabled = options.monitorEnabled ?? true;
|
|
148
90
|
this.now = options.now ?? Date.now;
|
|
149
91
|
this.diagnostic = options.diagnostic;
|
|
92
|
+
this.ownsAgentMailbox = !options.agentMailbox;
|
|
93
|
+
this.agentMailbox = options.agentMailbox ?? new AgentMailboxCoordinator({
|
|
94
|
+
repository: options.repository,
|
|
95
|
+
readyView: options.readyView,
|
|
96
|
+
presentEvent: presentCanonicalEventForCclEvents,
|
|
97
|
+
compactState: compactStateForEvents,
|
|
98
|
+
consumerId: this.consumerId,
|
|
99
|
+
now: this.now,
|
|
100
|
+
diagnostic: this.diagnostic,
|
|
101
|
+
});
|
|
150
102
|
}
|
|
151
103
|
|
|
152
104
|
prepareEvents(
|
|
@@ -154,11 +106,9 @@ export class ClawclawMailboxService {
|
|
|
154
106
|
readyView?: RuntimeReadyView,
|
|
155
107
|
): PreparedClawclawEventsResponse {
|
|
156
108
|
this.assertRunning();
|
|
157
|
-
this.releaseExpiredPrepared();
|
|
158
109
|
const ready = this.captureReadyView(readyView);
|
|
159
110
|
if (ready.epochSeq <= 0) return { output: unavailableEventsOutput() };
|
|
160
111
|
|
|
161
|
-
const state = this.compactReadyState(ready);
|
|
162
112
|
if (request.eventType) {
|
|
163
113
|
const event = this.repository.listCommittedEvents({
|
|
164
114
|
sessionId: ready.scope.sessionId,
|
|
@@ -176,135 +126,77 @@ export class ClawclawMailboxService {
|
|
|
176
126
|
output: {
|
|
177
127
|
schema: 'ccl.events.v5',
|
|
178
128
|
query: { type: request.eventType },
|
|
179
|
-
state,
|
|
129
|
+
state: this.agentMailbox.stateForReadyView(ready),
|
|
180
130
|
found: !!presented,
|
|
181
131
|
event: presented,
|
|
182
132
|
},
|
|
183
133
|
};
|
|
184
134
|
}
|
|
185
135
|
|
|
186
|
-
|
|
187
|
-
const lease = this.repository.acquireLease({
|
|
188
|
-
consumerId: this.consumerId,
|
|
189
|
-
channel: 'details',
|
|
190
|
-
ownerId: `mailbox-events:${randomUUID()}`,
|
|
191
|
-
ttlMs: EVENTS_LEASE_TTL_MS,
|
|
192
|
-
nowMs: this.now(),
|
|
193
|
-
});
|
|
194
|
-
if (!lease) throw new Error('mailbox_events_delivery_busy');
|
|
195
|
-
|
|
136
|
+
let prepared;
|
|
196
137
|
try {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
order: 'desc',
|
|
204
|
-
}, this.consumerId);
|
|
205
|
-
const selected = candidates.map((row) => (
|
|
206
|
-
presentCanonicalEventForCclEvents(row.event, ready.state.state)
|
|
207
|
-
));
|
|
208
|
-
const selectedRows = candidates;
|
|
209
|
-
const output = {
|
|
210
|
-
schema: 'ccl.events.v5',
|
|
211
|
-
state,
|
|
212
|
-
events: selected,
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
if (selectedRows.length === 0) {
|
|
216
|
-
this.repository.releaseLease(lease);
|
|
217
|
-
return { output };
|
|
218
|
-
}
|
|
219
|
-
const deliveryToken = randomUUID();
|
|
220
|
-
this.prepared.set(deliveryToken, {
|
|
221
|
-
lease,
|
|
222
|
-
eventIds: selectedRows.map((row) => row.event.eventId),
|
|
223
|
-
readyEpochSeq: ready.epochSeq,
|
|
224
|
-
sessionId: ready.scope.sessionId,
|
|
225
|
-
segmentIds: [...new Set(selectedRows.map((row) => row.event.segmentId))],
|
|
226
|
-
});
|
|
227
|
-
this.diagnostic?.('runtime.mailbox.delivery_prepared', {
|
|
228
|
-
consumer: this.consumerId,
|
|
229
|
-
delivery_token: deliveryToken,
|
|
230
|
-
session_id: ready.scope.sessionId,
|
|
231
|
-
segment_ids: [...new Set(selectedRows.map((row) => row.event.segmentId))],
|
|
232
|
-
ready_epoch_seq: ready.epochSeq,
|
|
233
|
-
event_count: selectedRows.length,
|
|
138
|
+
prepared = this.agentMailbox.prepareEvents({
|
|
139
|
+
readyView: ready,
|
|
140
|
+
bypassDelay: true,
|
|
141
|
+
deliveryVia: request.mode === 'backlog' ? 'ccl_events_backlog' : 'ccl_events',
|
|
142
|
+
ownerIdPrefix: 'ccl_events',
|
|
143
|
+
leaseTtlMs: CCL_EVENTS_LEASE_TTL_MS,
|
|
234
144
|
});
|
|
235
|
-
return { output, deliveryToken };
|
|
236
145
|
} catch (error) {
|
|
237
|
-
|
|
238
|
-
throw error;
|
|
146
|
+
return cclEventsCompatibilityError(error);
|
|
239
147
|
}
|
|
148
|
+
return {
|
|
149
|
+
output: {
|
|
150
|
+
schema: 'ccl.events.v5',
|
|
151
|
+
...prepared.batch,
|
|
152
|
+
},
|
|
153
|
+
...(prepared.deliveryToken ? { deliveryToken: prepared.deliveryToken } : {}),
|
|
154
|
+
};
|
|
240
155
|
}
|
|
241
156
|
|
|
242
|
-
/** Serializes
|
|
243
|
-
prepareEventsWhenAvailable(
|
|
157
|
+
/** Serializes pull delivery while preserving the caller's immutable Ready View. */
|
|
158
|
+
async prepareEventsWhenAvailable(
|
|
244
159
|
request: PrepareClawclawEventsRequest = {},
|
|
245
160
|
readyView?: RuntimeReadyView,
|
|
246
|
-
timeoutMs =
|
|
161
|
+
timeoutMs = 5_000,
|
|
247
162
|
): Promise<PreparedClawclawEventsResponse> {
|
|
248
|
-
if (request.eventType) return
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
163
|
+
if (request.eventType) return this.prepareEvents(request, readyView);
|
|
164
|
+
this.assertRunning();
|
|
165
|
+
const ready = this.captureReadyView(readyView);
|
|
166
|
+
if (ready.epochSeq <= 0) return { output: unavailableEventsOutput() };
|
|
167
|
+
let prepared;
|
|
168
|
+
try {
|
|
169
|
+
prepared = await this.agentMailbox.prepareEventsWhenAvailable({
|
|
170
|
+
readyView: ready,
|
|
171
|
+
bypassDelay: true,
|
|
172
|
+
deliveryVia: request.mode === 'backlog' ? 'ccl_events_backlog' : 'ccl_events',
|
|
173
|
+
ownerIdPrefix: 'ccl_events',
|
|
174
|
+
leaseTtlMs: CCL_EVENTS_LEASE_TTL_MS,
|
|
175
|
+
}, timeoutMs);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
return cclEventsCompatibilityError(error);
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
output: {
|
|
181
|
+
schema: 'ccl.events.v5',
|
|
182
|
+
...prepared.batch,
|
|
183
|
+
},
|
|
184
|
+
...(prepared.deliveryToken ? { deliveryToken: prepared.deliveryToken } : {}),
|
|
185
|
+
};
|
|
255
186
|
}
|
|
256
187
|
|
|
257
188
|
ackEvents(deliveryToken: string): boolean {
|
|
258
|
-
|
|
259
|
-
if (!prepared) return false;
|
|
260
|
-
this.prepared.delete(deliveryToken);
|
|
261
|
-
const deliveredAt = new Date(this.now()).toISOString();
|
|
262
|
-
const changed = this.repository.transitionDetails({
|
|
263
|
-
eventIds: prepared.eventIds,
|
|
264
|
-
consumerId: this.consumerId,
|
|
265
|
-
from: ['pending', 'deferred'],
|
|
266
|
-
to: 'delivered',
|
|
267
|
-
deliveredAt,
|
|
268
|
-
deliveryVia: 'ccl_events',
|
|
269
|
-
lease: prepared.lease,
|
|
270
|
-
nowMs: this.now(),
|
|
271
|
-
});
|
|
272
|
-
this.repository.releaseLease(prepared.lease);
|
|
273
|
-
this.notifyPreparedSlotAvailable();
|
|
274
|
-
const accepted = changed === prepared.eventIds.length;
|
|
275
|
-
this.diagnostic?.('runtime.mailbox.delivery_acked', {
|
|
276
|
-
consumer: this.consumerId,
|
|
277
|
-
delivery_token: deliveryToken,
|
|
278
|
-
session_id: prepared.sessionId,
|
|
279
|
-
segment_ids: prepared.segmentIds,
|
|
280
|
-
ready_epoch_seq: prepared.readyEpochSeq,
|
|
281
|
-
event_count: prepared.eventIds.length,
|
|
282
|
-
accepted,
|
|
283
|
-
});
|
|
284
|
-
return accepted;
|
|
189
|
+
return this.agentMailbox.ack(deliveryToken);
|
|
285
190
|
}
|
|
286
191
|
|
|
287
192
|
releaseEvents(deliveryToken: string): boolean {
|
|
288
|
-
|
|
289
|
-
if (!prepared) return false;
|
|
290
|
-
this.prepared.delete(deliveryToken);
|
|
291
|
-
const released = this.repository.releaseLease(prepared.lease);
|
|
292
|
-
this.notifyPreparedSlotAvailable();
|
|
293
|
-
this.diagnostic?.('runtime.mailbox.delivery_released', {
|
|
294
|
-
consumer: this.consumerId,
|
|
295
|
-
delivery_token: deliveryToken,
|
|
296
|
-
session_id: prepared.sessionId,
|
|
297
|
-
segment_ids: prepared.segmentIds,
|
|
298
|
-
ready_epoch_seq: prepared.readyEpochSeq,
|
|
299
|
-
event_count: prepared.eventIds.length,
|
|
300
|
-
released,
|
|
301
|
-
});
|
|
302
|
-
return released;
|
|
193
|
+
return this.agentMailbox.release(deliveryToken);
|
|
303
194
|
}
|
|
304
195
|
|
|
305
196
|
/** Called by the Monitor Driver at its own cadence, never by each epoch. */
|
|
306
197
|
prepareMonitor(force = false): number {
|
|
307
198
|
this.assertRunning();
|
|
199
|
+
if (!this.monitorEnabled) return 0;
|
|
308
200
|
const ready = this.captureReadyView();
|
|
309
201
|
if (ready.epochSeq <= 0) return 0;
|
|
310
202
|
const scopeKey = `${ready.scope.sessionId}:${ready.scope.segmentId}`;
|
|
@@ -378,7 +270,7 @@ export class ClawclawMailboxService {
|
|
|
378
270
|
if (this.stopped) return;
|
|
379
271
|
await this.flush();
|
|
380
272
|
this.stopped = true;
|
|
381
|
-
|
|
273
|
+
if (this.ownsAgentMailbox) await this.agentMailbox.stop();
|
|
382
274
|
}
|
|
383
275
|
|
|
384
276
|
private captureReadyView(readyView?: RuntimeReadyView): RuntimeReadyView {
|
|
@@ -395,80 +287,6 @@ export class ClawclawMailboxService {
|
|
|
395
287
|
return ready;
|
|
396
288
|
}
|
|
397
289
|
|
|
398
|
-
private compactReadyState(ready: RuntimeReadyView): Record<string, any> | null {
|
|
399
|
-
if (!ready.state.state) return null;
|
|
400
|
-
const state = compactStateForEvents(ready.state.state);
|
|
401
|
-
if (!state) return null;
|
|
402
|
-
const strategyState = ready.pluginStates['strategy.status'];
|
|
403
|
-
const strategyOutcome = ready.pluginOutcomes['clawclaw.strategy'];
|
|
404
|
-
if (!strategyState && !strategyOutcome) return state;
|
|
405
|
-
const runtime = strategyOutcome?.outcome === 'failed'
|
|
406
|
-
? 'error'
|
|
407
|
-
: strategyOutcome?.reason === 'strategy_inactive_stage'
|
|
408
|
-
? 'inactive_stage'
|
|
409
|
-
: strategyOutcome?.reason === 'strategy_waiting_for_current_state'
|
|
410
|
-
? 'waiting_for_state'
|
|
411
|
-
: strategyOutcome?.reason === 'strategy_not_selected'
|
|
412
|
-
? 'not_selected'
|
|
413
|
-
: strategyOutcome
|
|
414
|
-
? 'active'
|
|
415
|
-
: undefined;
|
|
416
|
-
const lastError = strategyOutcome?.outcome === 'failed'
|
|
417
|
-
? strategyOutcome.reason
|
|
418
|
-
: undefined;
|
|
419
|
-
const strategy = compactStrategyStateForEvents(
|
|
420
|
-
strategyState?.value,
|
|
421
|
-
runtime,
|
|
422
|
-
lastError,
|
|
423
|
-
nonEmptyString(state.phase),
|
|
424
|
-
typeof state.you?.alive === 'boolean' ? state.you.alive : undefined,
|
|
425
|
-
);
|
|
426
|
-
if (!strategy) return state;
|
|
427
|
-
return {
|
|
428
|
-
...state,
|
|
429
|
-
strategy,
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
private releaseExpiredPrepared(): void {
|
|
434
|
-
const nowMs = this.now();
|
|
435
|
-
for (const [token, prepared] of this.prepared) {
|
|
436
|
-
if (prepared.lease.expiresAt > nowMs) continue;
|
|
437
|
-
this.prepared.delete(token);
|
|
438
|
-
this.repository.releaseLease(prepared.lease);
|
|
439
|
-
}
|
|
440
|
-
this.notifyPreparedSlotAvailable();
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
private waitForPreparedSlot(timeoutMs: number): Promise<void> {
|
|
444
|
-
this.assertRunning();
|
|
445
|
-
this.releaseExpiredPrepared();
|
|
446
|
-
if (this.prepared.size === 0) return Promise.resolve();
|
|
447
|
-
const boundedTimeoutMs = Math.max(1, Math.trunc(timeoutMs));
|
|
448
|
-
return new Promise((resolve, reject) => {
|
|
449
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
450
|
-
const cleanup = (): void => {
|
|
451
|
-
if (timer) clearTimeout(timer);
|
|
452
|
-
this.preparedSlotWaiters.delete(available);
|
|
453
|
-
};
|
|
454
|
-
const available = (): void => {
|
|
455
|
-
cleanup();
|
|
456
|
-
resolve();
|
|
457
|
-
};
|
|
458
|
-
this.preparedSlotWaiters.add(available);
|
|
459
|
-
timer = setTimeout(() => {
|
|
460
|
-
cleanup();
|
|
461
|
-
reject(new Error('mailbox_events_delivery_busy'));
|
|
462
|
-
}, boundedTimeoutMs);
|
|
463
|
-
timer.unref?.();
|
|
464
|
-
});
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
private notifyPreparedSlotAvailable(): void {
|
|
468
|
-
if (this.prepared.size > 0) return;
|
|
469
|
-
for (const waiter of [...this.preparedSlotWaiters]) waiter();
|
|
470
|
-
}
|
|
471
|
-
|
|
472
290
|
private assertRunning(): void {
|
|
473
291
|
if (this.stopped) throw new Error('clawclaw_mailbox_stopped');
|
|
474
292
|
}
|
|
@@ -407,6 +407,7 @@ describe('ClawClaw runtime capabilities', () => {
|
|
|
407
407
|
|
|
408
408
|
it('advances the Mailbox Event cursor without flushing delivery per epoch', async () => {
|
|
409
409
|
const driver = {
|
|
410
|
+
observeEvents: vi.fn(),
|
|
410
411
|
flush: vi.fn(async () => {}),
|
|
411
412
|
stop: vi.fn(async () => {}),
|
|
412
413
|
};
|
|
@@ -444,6 +445,10 @@ describe('ClawClaw runtime capabilities', () => {
|
|
|
444
445
|
eventCursor: 1,
|
|
445
446
|
completedThroughEpochSeq: 2,
|
|
446
447
|
});
|
|
448
|
+
expect(driver.observeEvents).toHaveBeenCalledOnce();
|
|
449
|
+
expect(driver.observeEvents).toHaveBeenCalledWith([
|
|
450
|
+
expect.objectContaining({ type: 'task_completed' }),
|
|
451
|
+
]);
|
|
447
452
|
expect(driver.flush).not.toHaveBeenCalled();
|
|
448
453
|
await runtime.close();
|
|
449
454
|
expect(driver.flush).toHaveBeenCalledOnce();
|
|
@@ -617,6 +617,58 @@ describe('ClawclawSourcePlugin lifecycle', () => {
|
|
|
617
617
|
await runtime.close();
|
|
618
618
|
});
|
|
619
619
|
|
|
620
|
+
it('enriches task_completed from a same-frame task_assigned while retaining the raw internal Event', async () => {
|
|
621
|
+
const { runtime, source, games } = await fixture([{
|
|
622
|
+
status: 'allocated', mode: 'match', game_id: 'G1', ws_url: 'wss://game.test/ws',
|
|
623
|
+
}]);
|
|
624
|
+
await waitUntil(() => games.length === 1, 'Game transport was not started.');
|
|
625
|
+
|
|
626
|
+
await games[0].emit(gameStateFrame('game-1', 1, {
|
|
627
|
+
tick: 42,
|
|
628
|
+
phase: 'wandering',
|
|
629
|
+
you: {
|
|
630
|
+
is_alive: true,
|
|
631
|
+
tasks: [{ name: '消毒淋浴器', room: '制氧舱', x: 123, y: 456, status: 'normal' }],
|
|
632
|
+
},
|
|
633
|
+
}, [{
|
|
634
|
+
event_id: 'completed-1',
|
|
635
|
+
tick: 42,
|
|
636
|
+
type: 'task_completed',
|
|
637
|
+
actor: { seat: 1, name: 'me' },
|
|
638
|
+
task_name: '擦拭酒瓶',
|
|
639
|
+
}, {
|
|
640
|
+
event_id: 'assigned-1',
|
|
641
|
+
tick: 42,
|
|
642
|
+
type: 'task_assigned',
|
|
643
|
+
actor: { seat: 1, name: 'me' },
|
|
644
|
+
task_name: '消毒淋浴器',
|
|
645
|
+
room: '制氧舱',
|
|
646
|
+
x: 123,
|
|
647
|
+
y: 456,
|
|
648
|
+
is_fake_shrimp: false,
|
|
649
|
+
}]));
|
|
650
|
+
await runtime.flush();
|
|
651
|
+
|
|
652
|
+
expect(runtime.repository.latestEventByType(
|
|
653
|
+
runtime.scope().sessionId,
|
|
654
|
+
'task_completed',
|
|
655
|
+
runtime.scope().segmentId,
|
|
656
|
+
)?.payload).toMatchObject({
|
|
657
|
+
task_name: '擦拭酒瓶',
|
|
658
|
+
newly_assigned_task: {
|
|
659
|
+
name: '消毒淋浴器', room: '制氧舱', x: 123, y: 456, is_fake_shrimp: false,
|
|
660
|
+
},
|
|
661
|
+
});
|
|
662
|
+
expect(runtime.repository.latestEventByType(
|
|
663
|
+
runtime.scope().sessionId,
|
|
664
|
+
'task_assigned',
|
|
665
|
+
runtime.scope().segmentId,
|
|
666
|
+
)?.deliveryClass).toBe('internal_only');
|
|
667
|
+
|
|
668
|
+
await source.stop();
|
|
669
|
+
await runtime.close();
|
|
670
|
+
});
|
|
671
|
+
|
|
620
672
|
it('forbids coalescing only on action-safety boundary frames', async () => {
|
|
621
673
|
const { runtime, source, games } = await fixture([{
|
|
622
674
|
status: 'allocated', mode: 'match', game_id: 'G1', ws_url: 'wss://game.test/ws',
|
|
@@ -40,7 +40,7 @@ import {
|
|
|
40
40
|
} from '../../../framework/plugins/index.js';
|
|
41
41
|
import type { RuntimeScope } from '../../../framework/state/index.js';
|
|
42
42
|
import type { PlayerProjection } from '../../../../pipeline/player-projection.js';
|
|
43
|
-
import {
|
|
43
|
+
import { normalizeGameSyncEvents } from '../../../../pipeline/game-sync-event-normalization.js';
|
|
44
44
|
import {
|
|
45
45
|
projectClawclawRoomState,
|
|
46
46
|
} from '../../../clawclaw-room-state.js';
|
|
@@ -1733,8 +1733,9 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
|
|
|
1733
1733
|
meta: Pick<WsFrameMeta, 'receivedAt' | 'sourceEventId' | 'sourceSeq'> & { replayed?: boolean },
|
|
1734
1734
|
): RuntimeSourceEvent<Record<string, unknown>>[] {
|
|
1735
1735
|
const events: RuntimeSourceEvent<Record<string, unknown>>[] = [];
|
|
1736
|
-
|
|
1737
|
-
|
|
1736
|
+
const normalizedValues = normalizeGameSyncEvents(values);
|
|
1737
|
+
normalizedValues.forEach((normalized, index) => {
|
|
1738
|
+
const value = values[index];
|
|
1738
1739
|
const sourceEventId = eventSourceId(value as Record<string, any>);
|
|
1739
1740
|
const event = (
|
|
1740
1741
|
(
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
┌──────┐
|
|
2
|
-
│ 浴室 │
|
|
3
|
-
└───┬──┘
|
|
4
|
-
1号走廊
|
|
5
|
-
│
|
|
6
|
-
┌────┴───┐
|
|
7
|
-
│ 健身房 │
|
|
8
|
-
└────┬───┘
|
|
9
|
-
│
|
|
10
|
-
│
|
|
11
|
-
┌──────┐ │
|
|
12
|
-
│ 酒吧 ├──────────┐ │ ┌────────┐
|
|
13
|
-
└───┬──┘ │ │ │ 导航仓 │
|
|
14
|
-
│ └───2号走廊──┴─────┬───────────┤ │
|
|
15
|
-
│ │ └────┬───┘
|
|
16
|
-
┌───3号走廊───────────────┘ │ │
|
|
17
|
-
│ │ │
|
|
18
|
-
│ ┌────────┐ ┌────────┬┘ │
|
|
19
|
-
┌──────┴─────┐ ┌────────┐ │ 监控室 ├──────┤ 居住区 ├──────┐ │
|
|
20
|
-
│ 动力监控室 ├────┤ 能源舱 │ └────┬───┘ └────┬───┘ │ │
|
|
21
|
-
└──────┬─────┘ └────────┘ │ │ │ │
|
|
22
|
-
│ │ │ │ │
|
|
23
|
-
4号走廊 │ │ │ │
|
|
24
|
-
│ │ ┌──────────┘ │ │
|
|
25
|
-
│ ┌────┴───┐│ │ │
|
|
26
|
-
└┐ │ 禁闭室 ││ │ │
|
|
27
|
-
│ └────────┘│ │ │
|
|
28
|
-
┌────┴───┐ ┌──────────┬┘ ┌────┴────┐ │
|
|
29
|
-
│ 制氧舱 ├─────────────────────┤ 自助餐厅 │ │ 休闲会所 ├─5号走廊─┬────────┐
|
|
30
|
-
└────────┘ └──────────┤ └────┬────┘ │ │ 情报室 ├────────┐
|
|
31
|
-
│ │ │ └────────┘ │
|
|
32
|
-
│ │ │ │
|
|
33
|
-
│ ┌────┴────┐ │ ┌────┴───┐
|
|
34
|
-
└─────────────────┤ 中央厨房 ├─────┘ │ 控制室 │
|
|
35
|
-
└────┬────┘ └────────┘
|
|
36
|
-
│
|
|
37
|
-
│
|
|
38
|
-
┌────┴┐
|
|
39
|
-
│ 冷库 │
|
|
40
|
-
└─────┘
|