@myclaw163/clawclaw-cli 0.7.8 → 0.7.10
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 +14 -2
- package/docs//350/207/252/345/256/232/344/271/211/346/204/237/347/237/245.md +15 -1
- package/package.json +1 -1
- package/skills/clawclaw/SKILL.md +5 -5
- package/skills/clawclaw/references/COMMANDS.md +4 -4
- package/skills/clawclaw/references/GAME-MECHANICS.md +1 -1
- package/src/commands/communication.test.ts +14 -3
- package/src/commands/communication.ts +12 -4
- package/src/pipeline/event-format.test.ts +0 -21
- package/src/pipeline/event-format.ts +0 -33
- package/src/pipeline/event-hints.test.ts +2 -5
- package/src/pipeline/event-hints.ts +0 -17
- package/src/pipeline/game-sync-event-normalization.test.ts +34 -12
- package/src/pipeline/game-sync-event-normalization.ts +52 -22
- package/src/runtime/clawclaw/owner.ts +8 -0
- package/src/runtime/clawclaw/plugins/assembly.test.ts +13 -0
- package/src/runtime/clawclaw/plugins/assembly.ts +10 -0
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-push.integration.test.ts +17 -5
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.test.ts +37 -3
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +49 -14
- package/src/runtime/clawclaw/plugins/perception/contracts.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +16 -0
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +16 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +16 -0
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +23 -0
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +16 -0
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +16 -0
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +23 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +19 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +41 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +49 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +105 -5
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +174 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +146 -66
- package/src/runtime/clawclaw/plugins/strategy/strategy-action.ts +11 -4
- package/src/runtime/clawclaw-event-catalog.ts +0 -2
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +36 -0
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +0 -5
- package/src/sdk/action.ts +9 -8
- package/src/sdk/perception-plugin/index.ts +8 -0
- package/src/sdk/strategy-plugin/index.test.ts +3 -3
- package/src/sdk/strategy-plugin/index.ts +25 -24
- package/src/strategies/custom-module.test.ts +1 -1
- package/src/strategies/game-utils.test.ts +8 -8
- package/src/strategies/game-utils.ts +39 -28
- package/src/strategies/goals/crab-octopus-reflexes.ts +11 -7
- package/src/strategies/goals/hide-top.ts +2 -1
- package/src/strategies/goals/kill-target-goal.ts +2 -1
- package/src/strategies/goals/kill-target-top.ts +3 -3
- package/src/strategies/goals/lone-kill-core.ts +3 -1
- package/src/strategies/goals/task-kill-report-top.ts +2 -1
- package/src/strategies/goals/warrior-shrimp-top.ts +2 -1
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
getUserDataPerceptionsDir,
|
|
17
17
|
} from '../../../../lib/user-data.js';
|
|
18
18
|
import type {
|
|
19
|
+
ClawclawPerceptionEventMetadata,
|
|
19
20
|
ClawclawPerceptionManifest,
|
|
20
21
|
ClawclawResourceLocalizations,
|
|
21
22
|
} from './contracts.js';
|
|
@@ -89,6 +90,66 @@ function balancedBlock(source: string, open: number, openChar: '{' | '[', closeC
|
|
|
89
90
|
return null;
|
|
90
91
|
}
|
|
91
92
|
|
|
93
|
+
function topLevelObjectPropertyBlock(source: string, property: string): string | null | undefined {
|
|
94
|
+
let depth = 0;
|
|
95
|
+
let quote: "'" | '"' | '`' | undefined;
|
|
96
|
+
let escaped = false;
|
|
97
|
+
let lineComment = false;
|
|
98
|
+
let blockComment = false;
|
|
99
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
100
|
+
const char = source[index];
|
|
101
|
+
const next = source[index + 1];
|
|
102
|
+
if (lineComment) {
|
|
103
|
+
if (char === '\n' || char === '\r') lineComment = false;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (blockComment) {
|
|
107
|
+
if (char === '*' && next === '/') {
|
|
108
|
+
blockComment = false;
|
|
109
|
+
index += 1;
|
|
110
|
+
}
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (quote) {
|
|
114
|
+
if (escaped) escaped = false;
|
|
115
|
+
else if (char === '\\') escaped = true;
|
|
116
|
+
else if (char === quote) quote = undefined;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (char === '/' && next === '/') {
|
|
120
|
+
lineComment = true;
|
|
121
|
+
index += 1;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (char === '/' && next === '*') {
|
|
125
|
+
blockComment = true;
|
|
126
|
+
index += 1;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (char === "'" || char === '"' || char === '`') {
|
|
130
|
+
quote = char;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (char === '{') {
|
|
134
|
+
depth += 1;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (char === '}') {
|
|
138
|
+
depth -= 1;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (depth !== 1 || !source.startsWith(property, index)) continue;
|
|
142
|
+
const before = source[index - 1] ?? '';
|
|
143
|
+
const afterName = source[index + property.length] ?? '';
|
|
144
|
+
if (/[A-Za-z0-9_$]/.test(before) || /[A-Za-z0-9_$]/.test(afterName)) continue;
|
|
145
|
+
const match = /^\s*:\s*\{/.exec(source.slice(index + property.length));
|
|
146
|
+
if (!match) continue;
|
|
147
|
+
const open = index + property.length + match[0].lastIndexOf('{');
|
|
148
|
+
return balancedBlock(source, open, '{', '}');
|
|
149
|
+
}
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
92
153
|
function decodeString(raw: string): string | null {
|
|
93
154
|
try {
|
|
94
155
|
return JSON.parse(`"${raw.replace(/"/g, '\\"')}"`) as string;
|
|
@@ -143,11 +204,8 @@ function literalStringArray(source: string, property: string): string[] | null {
|
|
|
143
204
|
function literalLocalizations(
|
|
144
205
|
source: string,
|
|
145
206
|
): ClawclawResourceLocalizations | null | undefined {
|
|
146
|
-
const
|
|
147
|
-
if (
|
|
148
|
-
const open = property.index + property[0].lastIndexOf('{');
|
|
149
|
-
const block = balancedBlock(source, open, '{', '}');
|
|
150
|
-
if (!block) return null;
|
|
207
|
+
const block = topLevelObjectPropertyBlock(source, 'localizations');
|
|
208
|
+
if (block === undefined || block === null) return block;
|
|
151
209
|
const values: Record<string, { name: string; description: string }> = {};
|
|
152
210
|
const entry = /(["'\x60])((?:\\.|(?!\1)[\s\S])*)\1\s*:\s*\{/g;
|
|
153
211
|
let cursor = 1;
|
|
@@ -170,6 +228,38 @@ function literalLocalizations(
|
|
|
170
228
|
return values;
|
|
171
229
|
}
|
|
172
230
|
|
|
231
|
+
function literalEventMetadata(
|
|
232
|
+
source: string,
|
|
233
|
+
): Readonly<Record<string, ClawclawPerceptionEventMetadata>> | null | undefined {
|
|
234
|
+
const block = topLevelObjectPropertyBlock(source, 'eventMetadata');
|
|
235
|
+
if (block === undefined || block === null) return block;
|
|
236
|
+
const values: Record<string, ClawclawPerceptionEventMetadata> = {};
|
|
237
|
+
const entry = /(["'\x60])((?:\\.|(?!\1)[\s\S])*)\1\s*:\s*\{/g;
|
|
238
|
+
let cursor = 1;
|
|
239
|
+
let match: RegExpExecArray | null;
|
|
240
|
+
while ((match = entry.exec(block)) !== null) {
|
|
241
|
+
if (block.slice(cursor, match.index).replace(/[\s,]/g, '')) return null;
|
|
242
|
+
if (match[1] === '`' && match[2].includes('${')) return null;
|
|
243
|
+
const type = decodeString(match[2])?.trim();
|
|
244
|
+
const entryOpen = match.index + match[0].lastIndexOf('{');
|
|
245
|
+
const entryBlock = balancedBlock(block, entryOpen, '{', '}');
|
|
246
|
+
if (!type || !entryBlock || values[type]) return null;
|
|
247
|
+
const name = literalProperty(entryBlock, 'name')?.trim();
|
|
248
|
+
const description = literalProperty(entryBlock, 'description')?.trim();
|
|
249
|
+
const localizations = literalLocalizations(entryBlock);
|
|
250
|
+
if (!name || !description || localizations === null) return null;
|
|
251
|
+
values[type] = {
|
|
252
|
+
name,
|
|
253
|
+
description,
|
|
254
|
+
...(localizations === undefined ? {} : { localizations }),
|
|
255
|
+
};
|
|
256
|
+
cursor = entryOpen + entryBlock.length;
|
|
257
|
+
entry.lastIndex = cursor;
|
|
258
|
+
}
|
|
259
|
+
if (block.slice(cursor, -1).replace(/[\s,]/g, '')) return null;
|
|
260
|
+
return values;
|
|
261
|
+
}
|
|
262
|
+
|
|
173
263
|
function parseSource(sourcePath: string, sourceRoot: string): ResolvedClawclawPerceptionSource | null {
|
|
174
264
|
let source: string;
|
|
175
265
|
try {
|
|
@@ -197,6 +287,7 @@ function parseSource(sourcePath: string, sourceRoot: string): ResolvedClawclawPe
|
|
|
197
287
|
const execution = literalProperty(manifestBlock, 'execution')?.trim();
|
|
198
288
|
const boundaryBaseline = literalProperty(manifestBlock, 'boundaryBaseline')?.trim();
|
|
199
289
|
const localizations = literalLocalizations(manifestBlock);
|
|
290
|
+
const eventMetadata = literalEventMetadata(manifestBlock);
|
|
200
291
|
const parallelClass = classMatch[1] === 'ClawclawParallelPerception';
|
|
201
292
|
if (
|
|
202
293
|
!id
|
|
@@ -215,6 +306,14 @@ function parseSource(sourcePath: string, sourceRoot: string): ResolvedClawclawPe
|
|
|
215
306
|
)
|
|
216
307
|
|| parallelClass !== (execution === 'parallel_analysis')
|
|
217
308
|
|| localizations === null
|
|
309
|
+
|| eventMetadata === null
|
|
310
|
+
|| (
|
|
311
|
+
eventMetadata !== undefined
|
|
312
|
+
&& (
|
|
313
|
+
Object.keys(eventMetadata).length !== produces.length
|
|
314
|
+
|| Object.keys(eventMetadata).some((type) => !produces.includes(type))
|
|
315
|
+
)
|
|
316
|
+
)
|
|
218
317
|
) return null;
|
|
219
318
|
|
|
220
319
|
const manifest: ClawclawPerceptionManifest = {
|
|
@@ -224,6 +323,7 @@ function parseSource(sourcePath: string, sourceRoot: string): ResolvedClawclawPe
|
|
|
224
323
|
...(localizations === undefined ? {} : { localizations }),
|
|
225
324
|
subjectScope: subjectScope as ClawclawPerceptionManifest['subjectScope'],
|
|
226
325
|
produces,
|
|
326
|
+
...(eventMetadata === undefined ? {} : { eventMetadata }),
|
|
227
327
|
...(execution ? { execution: 'parallel_analysis' as const } : {}),
|
|
228
328
|
...(boundaryBaseline ? { boundaryBaseline: 'current_only' as const } : {}),
|
|
229
329
|
};
|
|
@@ -1253,6 +1253,180 @@ describe('ClawclawSourcePlugin lifecycle', () => {
|
|
|
1253
1253
|
}
|
|
1254
1254
|
});
|
|
1255
1255
|
|
|
1256
|
+
it('rejects one malformed GameSync Event without stopping Source or later frames', async () => {
|
|
1257
|
+
const { runtime, source, games, client } = await fixture([{
|
|
1258
|
+
status: 'allocated',
|
|
1259
|
+
mode: 'match',
|
|
1260
|
+
game_id: 'G-malformed-event',
|
|
1261
|
+
ws_url: 'wss://game.test/ws',
|
|
1262
|
+
}]);
|
|
1263
|
+
await waitUntil(() => games.length === 1, 'Game transport was not started.');
|
|
1264
|
+
const diagnostic = vi.spyOn(runtime, 'diagnostic');
|
|
1265
|
+
|
|
1266
|
+
const roster = (victimAlive: boolean) => ([
|
|
1267
|
+
{ seat: 1, name: 'me', known_status: { is_alive: true } },
|
|
1268
|
+
{ seat: 2, name: 'Killer', known_status: { is_alive: true } },
|
|
1269
|
+
{
|
|
1270
|
+
seat: 4,
|
|
1271
|
+
name: 'Victim',
|
|
1272
|
+
known_status: victimAlive
|
|
1273
|
+
? { is_alive: true }
|
|
1274
|
+
: { is_alive: false, death_cause: 'killed' },
|
|
1275
|
+
},
|
|
1276
|
+
]);
|
|
1277
|
+
await games[0].emit(gameStateFrame('game-1', 1, {
|
|
1278
|
+
tick: 1,
|
|
1279
|
+
phase: 'wandering',
|
|
1280
|
+
you: { player: { seat: 1, name: 'me' }, is_alive: true },
|
|
1281
|
+
players: roster(true),
|
|
1282
|
+
}));
|
|
1283
|
+
await games[0].emit(gameStateFrame('game-1', 2, {
|
|
1284
|
+
tick: 2,
|
|
1285
|
+
phase: 'wandering',
|
|
1286
|
+
you: { player: { seat: 1, name: 'me' }, is_alive: true },
|
|
1287
|
+
players: roster(false),
|
|
1288
|
+
}, [{
|
|
1289
|
+
event_id: 'role-valid-before',
|
|
1290
|
+
type: 'role_assigned',
|
|
1291
|
+
role: 'shrimp',
|
|
1292
|
+
}, {
|
|
1293
|
+
event_id: 'killed-malformed',
|
|
1294
|
+
tick: 2,
|
|
1295
|
+
type: 'killed',
|
|
1296
|
+
actor: { seat: 4, name: 'Victim' },
|
|
1297
|
+
killer: { kind: 'player', player: { seat: 2 } },
|
|
1298
|
+
}, {
|
|
1299
|
+
event_id: 'task-valid-after',
|
|
1300
|
+
type: 'task_assigned',
|
|
1301
|
+
task_name: 'wires',
|
|
1302
|
+
}]));
|
|
1303
|
+
await games[0].emit(gameStateFrame('game-1', 3, {
|
|
1304
|
+
tick: 3,
|
|
1305
|
+
phase: 'wandering',
|
|
1306
|
+
you: { player: { seat: 1, name: 'me' }, is_alive: true },
|
|
1307
|
+
players: roster(false),
|
|
1308
|
+
}));
|
|
1309
|
+
await runtime.flush();
|
|
1310
|
+
|
|
1311
|
+
const secondEpoch = runtime.inputJournal.list(runtime.scope(), 0, 100)
|
|
1312
|
+
.find((epoch) => epoch.domainTick === 2);
|
|
1313
|
+
expect(secondEpoch?.events).toEqual(expect.arrayContaining([
|
|
1314
|
+
expect.objectContaining({
|
|
1315
|
+
type: 'role_assigned', sourceEventId: 'role-valid-before', sourceSeq: '2:0',
|
|
1316
|
+
}),
|
|
1317
|
+
expect.objectContaining({
|
|
1318
|
+
type: 'task_assigned', sourceEventId: 'task-valid-after', sourceSeq: '2:2',
|
|
1319
|
+
}),
|
|
1320
|
+
expect.objectContaining({
|
|
1321
|
+
type: 'players_changed',
|
|
1322
|
+
payload: expect.objectContaining({
|
|
1323
|
+
changes: expect.arrayContaining([expect.objectContaining({
|
|
1324
|
+
change: 'died', name: 'Victim', cause: 'killed', killer: null,
|
|
1325
|
+
})]),
|
|
1326
|
+
}),
|
|
1327
|
+
}),
|
|
1328
|
+
]));
|
|
1329
|
+
expect(secondEpoch?.events.some((event) => event.type === 'killed')).toBe(false);
|
|
1330
|
+
expect(diagnostic).toHaveBeenCalledWith(
|
|
1331
|
+
'runtime.source.event_rejected',
|
|
1332
|
+
expect.objectContaining({
|
|
1333
|
+
source_id: CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1334
|
+
source_seq: 2,
|
|
1335
|
+
event_index: 1,
|
|
1336
|
+
event_type: 'killed',
|
|
1337
|
+
source_event_id: 'killed-malformed',
|
|
1338
|
+
error_code: 'GAME_SYNC_EVENT_NORMALIZATION_FAILED',
|
|
1339
|
+
error_name: 'TypeError',
|
|
1340
|
+
}),
|
|
1341
|
+
'error',
|
|
1342
|
+
);
|
|
1343
|
+
expect(client.acknowledgeGameSync).toHaveBeenLastCalledWith('ack:game-1:3', false);
|
|
1344
|
+
expect(games[0].stoppedWith).toBeUndefined();
|
|
1345
|
+
expect(runtime.repository.latestEventByType(
|
|
1346
|
+
runtime.scope().sessionId,
|
|
1347
|
+
'error',
|
|
1348
|
+
runtime.scope().segmentId,
|
|
1349
|
+
)?.payload).not.toMatchObject({ code: 'SOURCE_FAILED' });
|
|
1350
|
+
|
|
1351
|
+
await source.stop();
|
|
1352
|
+
await runtime.close();
|
|
1353
|
+
});
|
|
1354
|
+
|
|
1355
|
+
it('logs partial State loss, rejects an invalid tick, and accepts the next frame', async () => {
|
|
1356
|
+
const { runtime, source, games, client } = await fixture([{
|
|
1357
|
+
status: 'allocated',
|
|
1358
|
+
mode: 'match',
|
|
1359
|
+
game_id: 'G-malformed-frame',
|
|
1360
|
+
ws_url: 'wss://game.test/ws',
|
|
1361
|
+
}]);
|
|
1362
|
+
await waitUntil(() => games.length === 1, 'Game transport was not started.');
|
|
1363
|
+
const diagnostic = vi.spyOn(runtime, 'diagnostic');
|
|
1364
|
+
|
|
1365
|
+
await games[0].emit(gameStateFrame('game-1', 1, {
|
|
1366
|
+
tick: 1,
|
|
1367
|
+
phase: 'wandering',
|
|
1368
|
+
you: { player: { seat: 1, name: 'me' } },
|
|
1369
|
+
players: [{ seat: 1, name: 'me', known_status: { is_alive: true } }],
|
|
1370
|
+
}));
|
|
1371
|
+
await games[0].emit(gameStateFrame('game-1', 2, {
|
|
1372
|
+
tick: 2,
|
|
1373
|
+
phase: 'wandering',
|
|
1374
|
+
}));
|
|
1375
|
+
|
|
1376
|
+
const invalidTick = gameStateFrame('game-1', 3, {
|
|
1377
|
+
tick: 99,
|
|
1378
|
+
phase: 'wandering',
|
|
1379
|
+
});
|
|
1380
|
+
invalidTick.meta.domainTick = 3;
|
|
1381
|
+
await expect(games[0].emit(invalidTick)).resolves.toBeUndefined();
|
|
1382
|
+
|
|
1383
|
+
await games[0].emit(gameStateFrame('game-1', 4, {
|
|
1384
|
+
tick: 4,
|
|
1385
|
+
phase: 'wandering',
|
|
1386
|
+
you: { player: { seat: 1, name: 'me' } },
|
|
1387
|
+
players: [{ seat: 1, name: 'me', known_status: { is_alive: true } }],
|
|
1388
|
+
}));
|
|
1389
|
+
await runtime.flush();
|
|
1390
|
+
|
|
1391
|
+
const ticks = runtime.inputJournal.list(runtime.scope(), 0, 100)
|
|
1392
|
+
.map((epoch) => epoch.domainTick);
|
|
1393
|
+
expect(ticks).toEqual(expect.arrayContaining([1, 2, 4]));
|
|
1394
|
+
expect(ticks).not.toContain(3);
|
|
1395
|
+
expect(diagnostic).toHaveBeenCalledWith(
|
|
1396
|
+
'runtime.source.state_degraded',
|
|
1397
|
+
expect.objectContaining({
|
|
1398
|
+
source_id: CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1399
|
+
source_seq: 2,
|
|
1400
|
+
error_code: 'GAME_SYNC_STATE_FIELDS_MISSING',
|
|
1401
|
+
missing_fields: ['players', 'you'],
|
|
1402
|
+
}),
|
|
1403
|
+
'warn',
|
|
1404
|
+
);
|
|
1405
|
+
expect(diagnostic).toHaveBeenCalledWith(
|
|
1406
|
+
'runtime.source.frame_rejected',
|
|
1407
|
+
expect.objectContaining({
|
|
1408
|
+
source_id: CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1409
|
+
source_seq: 3,
|
|
1410
|
+
error_code: 'GAME_SYNC_TICK_INVALID',
|
|
1411
|
+
reason: 'tick_mismatch',
|
|
1412
|
+
frame_tick: 3,
|
|
1413
|
+
state_tick: 99,
|
|
1414
|
+
}),
|
|
1415
|
+
'error',
|
|
1416
|
+
);
|
|
1417
|
+
expect(client.acknowledgeGameSync).not.toHaveBeenCalledWith('ack:game-1:3', false);
|
|
1418
|
+
expect(client.acknowledgeGameSync).toHaveBeenLastCalledWith('ack:game-1:4', false);
|
|
1419
|
+
expect(games[0].stoppedWith).toBeUndefined();
|
|
1420
|
+
expect(runtime.repository.latestEventByType(
|
|
1421
|
+
runtime.scope().sessionId,
|
|
1422
|
+
'error',
|
|
1423
|
+
runtime.scope().segmentId,
|
|
1424
|
+
)?.payload).not.toMatchObject({ code: 'SOURCE_FAILED' });
|
|
1425
|
+
|
|
1426
|
+
await source.stop();
|
|
1427
|
+
await runtime.close();
|
|
1428
|
+
});
|
|
1429
|
+
|
|
1256
1430
|
it('keeps exile pending until a later authoritative State confirms death', async () => {
|
|
1257
1431
|
const { runtime, source, games } = await fixture([{
|
|
1258
1432
|
status: 'allocated',
|
|
@@ -49,7 +49,7 @@ import {
|
|
|
49
49
|
} from '../../../framework/plugins/index.js';
|
|
50
50
|
import type { RuntimeScope } from '../../../framework/state/index.js';
|
|
51
51
|
import type { PlayerProjection } from '../../../../pipeline/player-projection.js';
|
|
52
|
-
import {
|
|
52
|
+
import { normalizeGameSyncEventsSafely } from '../../../../pipeline/game-sync-event-normalization.js';
|
|
53
53
|
import {
|
|
54
54
|
projectClawclawRoomState,
|
|
55
55
|
} from '../../../clawclaw-room-state.js';
|
|
@@ -1127,6 +1127,7 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
|
|
|
1127
1127
|
transport: 'game_ws',
|
|
1128
1128
|
connection_id: info.connectionId,
|
|
1129
1129
|
reason: info.reason,
|
|
1130
|
+
...(info.details ? { details: info.details } : {}),
|
|
1130
1131
|
}, 'warn');
|
|
1131
1132
|
},
|
|
1132
1133
|
onRecoveryStarted: (info) => this.handleGameRecoveryStarted(info),
|
|
@@ -1302,72 +1303,133 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
|
|
|
1302
1303
|
}, 'error');
|
|
1303
1304
|
return 'error';
|
|
1304
1305
|
}
|
|
1305
|
-
const
|
|
1306
|
+
const rawState: unknown = frame.state;
|
|
1307
|
+
if (!rawState || typeof rawState !== 'object' || Array.isArray(rawState)) {
|
|
1308
|
+
this.diagnostic('runtime.source.frame_rejected', {
|
|
1309
|
+
source_id: CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1310
|
+
source_seq: frame.meta.sourceSeq,
|
|
1311
|
+
error_code: 'GAME_SYNC_STATE_INVALID',
|
|
1312
|
+
reason: 'state_not_object',
|
|
1313
|
+
}, 'error');
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
const gameState = rawState as Record<string, any>;
|
|
1306
1317
|
const tick = frame.meta.domainTick;
|
|
1318
|
+
const stateTick = gameState.tick;
|
|
1319
|
+
if (
|
|
1320
|
+
!Number.isInteger(tick)
|
|
1321
|
+
|| tick < 0
|
|
1322
|
+
|| !Number.isInteger(stateTick)
|
|
1323
|
+
|| Number(stateTick) < 0
|
|
1324
|
+
|| Number(stateTick) !== tick
|
|
1325
|
+
) {
|
|
1326
|
+
this.diagnostic('runtime.source.frame_rejected', {
|
|
1327
|
+
source_id: CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1328
|
+
source_seq: frame.meta.sourceSeq,
|
|
1329
|
+
error_code: 'GAME_SYNC_TICK_INVALID',
|
|
1330
|
+
reason: Number.isInteger(tick) && Number.isInteger(stateTick)
|
|
1331
|
+
? 'tick_mismatch'
|
|
1332
|
+
: 'tick_missing_or_invalid',
|
|
1333
|
+
frame_tick: tick,
|
|
1334
|
+
state_tick: stateTick,
|
|
1335
|
+
}, 'error');
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
const previousState = this.latestFreshGameState;
|
|
1339
|
+
const missingFields = new Set<string>();
|
|
1340
|
+
if (!Object.prototype.hasOwnProperty.call(gameState, 'phase')) missingFields.add('phase');
|
|
1341
|
+
if (previousState) {
|
|
1342
|
+
for (const field of Object.keys(previousState)) {
|
|
1343
|
+
if (!Object.prototype.hasOwnProperty.call(gameState, field)) missingFields.add(field);
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
if (missingFields.size > 0) {
|
|
1347
|
+
this.diagnostic('runtime.source.state_degraded', {
|
|
1348
|
+
source_id: CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1349
|
+
source_seq: frame.meta.sourceSeq,
|
|
1350
|
+
error_code: 'GAME_SYNC_STATE_FIELDS_MISSING',
|
|
1351
|
+
missing_fields: [...missingFields].sort(),
|
|
1352
|
+
}, 'warn');
|
|
1353
|
+
}
|
|
1307
1354
|
let acceptedStage: ClawclawRuntimeStage | undefined;
|
|
1308
1355
|
let acceptedPlayerProjection: PlayerProjection[] | undefined;
|
|
1309
1356
|
const acceptance = await this.enqueueInputIf(() => {
|
|
1310
1357
|
if (this.currentStage === 'ended') return null;
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1358
|
+
try {
|
|
1359
|
+
const scope = this.requireConnectionScope(
|
|
1360
|
+
CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1361
|
+
frame.meta.connectionId,
|
|
1362
|
+
);
|
|
1363
|
+
const backendEvents = this.sourceEvents(
|
|
1364
|
+
frame.events,
|
|
1365
|
+
frame.meta,
|
|
1366
|
+
CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1367
|
+
);
|
|
1368
|
+
const recoveryIdentity = `${frame.meta.gameId}:${String(frame.meta.sourceSeq)}:${frame.meta.connectionId}`;
|
|
1369
|
+
const recoveryEvents = frame.meta.origin === 'resume' || frame.meta.snapshotFallback
|
|
1370
|
+
? this.localEvents([{
|
|
1371
|
+
type: 'game_stream_reconnected',
|
|
1372
|
+
recovery: frame.meta.origin,
|
|
1373
|
+
replayed_event_count: frame.events.length,
|
|
1374
|
+
}], `${recoveryIdentity}:recovered`, frame.meta.receivedAt)
|
|
1375
|
+
: [];
|
|
1376
|
+
const playerUpdate = nextPlayerProjection({
|
|
1377
|
+
previous: this.playerProjection,
|
|
1378
|
+
initialized: this.playerProjectionInitialized,
|
|
1379
|
+
summary: gameState,
|
|
1380
|
+
events: frame.events,
|
|
1381
|
+
selfName: this.profile.agentName,
|
|
1382
|
+
});
|
|
1383
|
+
const events = [
|
|
1384
|
+
...backendEvents,
|
|
1385
|
+
...recoveryEvents,
|
|
1386
|
+
...(playerUpdate.payload
|
|
1387
|
+
? [this.playersChangedEvent(
|
|
1388
|
+
playerUpdate.payload,
|
|
1389
|
+
frame.meta.receivedAt,
|
|
1390
|
+
`${frame.meta.gameId}:${String(frame.meta.sourceSeq)}`,
|
|
1391
|
+
)]
|
|
1392
|
+
: []),
|
|
1393
|
+
];
|
|
1394
|
+
acceptedPlayerProjection = playerUpdate.projection;
|
|
1395
|
+
this.currentTransport = 'game_ws';
|
|
1396
|
+
this.currentConnection = 'connected';
|
|
1397
|
+
acceptedStage = clawclawStageFromGameEvents(
|
|
1398
|
+
frame.events,
|
|
1399
|
+
clawclawStageFromGameState(gameState),
|
|
1400
|
+
);
|
|
1401
|
+
return {
|
|
1402
|
+
scope,
|
|
1403
|
+
sourceId: CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1404
|
+
connectionId: frame.meta.connectionId,
|
|
1405
|
+
// A resume barrier has no sequence of its own. In the valid zero-replay
|
|
1406
|
+
// case its to_seq equals the last update accepted before disconnect, so
|
|
1407
|
+
// include the new connection identity to avoid retry-conflicting with
|
|
1408
|
+
// that earlier runtime input while preserving the backend sequence.
|
|
1409
|
+
sourceKey: frame.meta.origin === 'resume'
|
|
1410
|
+
? `game:${frame.meta.gameId}:${String(frame.meta.sourceSeq)}:resume:${frame.meta.connectionId}`
|
|
1411
|
+
: `game:${frame.meta.gameId}:${String(frame.meta.sourceSeq)}`,
|
|
1412
|
+
sourceSeq: `${frame.meta.gameId}:${String(frame.meta.sourceSeq)}`,
|
|
1413
|
+
domainTick: tick,
|
|
1414
|
+
state: this.gameRuntimeState(
|
|
1415
|
+
acceptedStage,
|
|
1416
|
+
acceptedStage === 'ended' ? 'ending' : 'game_snapshot',
|
|
1417
|
+
{ status: 'fresh', state: structuredClone(gameState) },
|
|
1418
|
+
),
|
|
1419
|
+
events,
|
|
1420
|
+
completeness: frame.meta.snapshotFallback ? 'best_effort' : 'complete',
|
|
1421
|
+
receivedAt: frame.meta.receivedAt,
|
|
1422
|
+
};
|
|
1423
|
+
} catch (error) {
|
|
1424
|
+
this.diagnostic('runtime.source.frame_rejected', {
|
|
1425
|
+
source_id: CLAWCLAW_GAME_WS_SOURCE.sourceId,
|
|
1426
|
+
source_seq: frame.meta.sourceSeq,
|
|
1427
|
+
error_code: 'GAME_SYNC_FRAME_PROJECTION_FAILED',
|
|
1428
|
+
error_name: error instanceof Error ? error.name : 'Error',
|
|
1429
|
+
error: serializeError(error),
|
|
1430
|
+
}, 'error');
|
|
1431
|
+
return null;
|
|
1432
|
+
}
|
|
1371
1433
|
}, () => {
|
|
1372
1434
|
this.currentStage = acceptedStage!;
|
|
1373
1435
|
this.currentQueue = undefined;
|
|
@@ -1483,7 +1545,7 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
|
|
|
1483
1545
|
sourceKey: this.physicalSourceKey('room', meta),
|
|
1484
1546
|
sourceSeq: `${meta.connectionId}:${String(meta.sourceSeq)}`,
|
|
1485
1547
|
state: this.runtimeState(this.currentStage === 'ended' ? 'ending' : 'room_update'),
|
|
1486
|
-
events: this.sourceEvents(rawEvents, meta),
|
|
1548
|
+
events: this.sourceEvents(rawEvents, meta, CLAWCLAW_ROOM_WS_SOURCE.sourceId),
|
|
1487
1549
|
completeness: meta.replay || !hadSnapshot ? 'best_effort' : 'complete',
|
|
1488
1550
|
receivedAt: meta.receivedAt,
|
|
1489
1551
|
});
|
|
@@ -1815,10 +1877,28 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
|
|
|
1815
1877
|
private sourceEvents(
|
|
1816
1878
|
values: readonly unknown[],
|
|
1817
1879
|
meta: Pick<WsFrameMeta, 'receivedAt' | 'sourceEventId' | 'sourceSeq'> & { replayed?: boolean },
|
|
1880
|
+
sourceId: string,
|
|
1818
1881
|
): RuntimeSourceEvent<Record<string, unknown>>[] {
|
|
1819
1882
|
const events: RuntimeSourceEvent<Record<string, unknown>>[] = [];
|
|
1820
|
-
const normalizedValues =
|
|
1821
|
-
normalizedValues.forEach((
|
|
1883
|
+
const normalizedValues = normalizeGameSyncEventsSafely(values);
|
|
1884
|
+
normalizedValues.rejected.forEach(({ index, error }) => {
|
|
1885
|
+
const value = values[index];
|
|
1886
|
+
const raw = value && typeof value === 'object' && !Array.isArray(value)
|
|
1887
|
+
? value as Record<string, unknown>
|
|
1888
|
+
: undefined;
|
|
1889
|
+
const sourceEventId = raw ? eventSourceId(raw) : undefined;
|
|
1890
|
+
this.diagnostic('runtime.source.event_rejected', {
|
|
1891
|
+
source_id: sourceId,
|
|
1892
|
+
source_seq: meta.sourceSeq,
|
|
1893
|
+
event_index: index,
|
|
1894
|
+
error_code: 'GAME_SYNC_EVENT_NORMALIZATION_FAILED',
|
|
1895
|
+
error_name: error instanceof Error ? error.name : 'Error',
|
|
1896
|
+
error: serializeError(error),
|
|
1897
|
+
...(typeof raw?.type === 'string' ? { event_type: raw.type } : {}),
|
|
1898
|
+
...(sourceEventId === undefined ? {} : { source_event_id: sourceEventId }),
|
|
1899
|
+
}, 'error');
|
|
1900
|
+
});
|
|
1901
|
+
normalizedValues.events.forEach(({ event: normalized, index }) => {
|
|
1822
1902
|
const value = values[index];
|
|
1823
1903
|
const sourceEventId = eventSourceId(value as Record<string, any>);
|
|
1824
1904
|
const event = (
|
|
@@ -1907,7 +1987,7 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
|
|
|
1907
1987
|
state: this.runtimeState(cause),
|
|
1908
1988
|
events: [
|
|
1909
1989
|
...this.localEvents(eventValues, sourceSeq, receivedAt),
|
|
1910
|
-
...this.sourceEvents(backendEventValues, { receivedAt, sourceSeq }),
|
|
1990
|
+
...this.sourceEvents(backendEventValues, { receivedAt, sourceSeq }, sourceId),
|
|
1911
1991
|
],
|
|
1912
1992
|
completeness: 'complete',
|
|
1913
1993
|
receivedAt,
|
|
@@ -2,9 +2,9 @@ import type { ClawclawActionRequest } from '../action/index.js';
|
|
|
2
2
|
|
|
3
3
|
const ACTION_FIELDS: Readonly<Record<string, readonly string[]>> = Object.freeze({
|
|
4
4
|
move: ['action', 'target', 'target_x', 'target_y', 'stop_on_player', 'thinking_content'],
|
|
5
|
-
kill: ['action', '
|
|
5
|
+
kill: ['action', 'target_seat', 'thinking_content'],
|
|
6
6
|
speech: ['action', 'text', 'thinking_content'],
|
|
7
|
-
vote: ['action', '
|
|
7
|
+
vote: ['action', 'target_seat', 'thinking_content'],
|
|
8
8
|
task: ['action', 'task_name', 'thinking_content'],
|
|
9
9
|
report: ['action', 'thinking_content'],
|
|
10
10
|
think: ['action', 'thinking_content'],
|
|
@@ -52,9 +52,16 @@ export function normalizeClawclawStrategyAction(
|
|
|
52
52
|
break;
|
|
53
53
|
}
|
|
54
54
|
case 'kill':
|
|
55
|
-
case 'vote':
|
|
56
|
-
|
|
55
|
+
case 'vote': {
|
|
56
|
+
const seat = value.target_seat;
|
|
57
|
+
const validSeat = typeof seat === 'number'
|
|
58
|
+
&& Number.isInteger(seat)
|
|
59
|
+
&& seat >= 0
|
|
60
|
+
&& seat <= 10;
|
|
61
|
+
if (!validSeat) throw new Error('invalid_strategy_target');
|
|
62
|
+
if (actionType === 'kill' && seat === 0) throw new Error('invalid_strategy_target');
|
|
57
63
|
break;
|
|
64
|
+
}
|
|
58
65
|
case 'speech':
|
|
59
66
|
if (!nonEmpty(value.text)) throw new Error('invalid_strategy_speech');
|
|
60
67
|
break;
|
|
@@ -74,7 +74,6 @@ export const CLAWCLAW_GAME_STREAM_BACKEND_EVENT_TYPES = [
|
|
|
74
74
|
'game_started',
|
|
75
75
|
'game_over',
|
|
76
76
|
'role_assigned',
|
|
77
|
-
'crab_teammates',
|
|
78
77
|
'emergency_started',
|
|
79
78
|
'emergency_resolved',
|
|
80
79
|
'emergency_interrupted',
|
|
@@ -108,7 +107,6 @@ export const CLAWCLAW_BACKEND_AGENT_EVENT_TYPES = [
|
|
|
108
107
|
'vote_speech_phase_ended',
|
|
109
108
|
'death_speech',
|
|
110
109
|
'wandering_speech',
|
|
111
|
-
'crab_teammates',
|
|
112
110
|
'meeting_briefing',
|
|
113
111
|
'speech',
|
|
114
112
|
'speech_skipped',
|