@myclaw163/clawclaw-cli 0.8.4 → 0.8.5
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 +3 -2
- package/docs/configuration.md +44 -26
- package/docs/custom-modules/README.md +7 -10
- package/docs/custom-modules/behavior.md +3 -1
- package/docs/custom-modules/perception.md +1 -1
- package/docs/custom-modules/trigger.md +1 -1
- package/docs/runtime/README.md +4 -4
- package/docs/runtime/official-perception-modules.md +2 -2
- package/docs/runtime/owner-lifecycle-and-observability.md +1 -1
- package/package.json +9 -2
- package/scripts/build-official-hub-behaviors.mjs +136 -0
- package/scripts/build-official-hub-behaviors.test.mjs +178 -0
- package/scripts/enable_ccl_permissions.cjs +4 -2
- package/scripts/enable_ccl_permissions.test.mjs +8 -4
- package/scripts/postinstall.mjs +1 -4
- package/scripts/sync-bundled-skill.mjs +12 -159
- package/scripts/sync-bundled-skill.test.mjs +9 -74
- package/skills/clawclaw/SKILL.md +14 -9
- package/skills/clawclaw/references/BEHAVIORS.md +4 -4
- package/skills/clawclaw/references/COMMANDS.md +32 -16
- package/skills/clawclaw/references/HUB.md +25 -38
- package/src/commands/account.ts +58 -14
- package/src/commands/behavior.ts +116 -38
- package/src/commands/hub.ts +110 -153
- package/src/commands/load.ts +138 -33
- package/src/commands/perception.ts +55 -24
- package/src/commands/persona.ts +66 -20
- package/src/commands/skill.ts +12 -48
- package/src/commands/trigger.ts +68 -40
- package/src/hub-official-behaviors/kill-lone.ts +138 -0
- package/src/hub-official-behaviors/patrol.ts +30 -0
- package/src/hub-official-behaviors/shared.ts +125 -0
- package/src/hub-official-behaviors/task-only.ts +158 -0
- package/src/lib/account-guidance.ts +54 -9
- package/src/lib/agent-workspace-client.ts +135 -0
- package/src/lib/agent-workspace-runtime.ts +154 -0
- package/src/lib/agent-workspace-skills.ts +177 -0
- package/src/lib/agent-workspace-sync.ts +852 -0
- package/src/lib/gameplay-config.ts +29 -15
- package/src/lib/hub-client.ts +252 -34
- package/src/lib/hub-reminder.ts +22 -7
- package/src/lib/init-command.ts +0 -6
- package/src/lib/load-context.ts +12 -6
- package/src/lib/persona.ts +141 -69
- package/src/lib/server-status.ts +78 -0
- package/src/lib/skill-host.ts +160 -0
- package/src/lib/user-data.ts +197 -4
- package/src/runtime/clawclaw/owner.ts +63 -19
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +8 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +2 -2
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +4 -1
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +11 -5
- package/src/runtime/clawclaw/plugins/perception/replay.ts +0 -2
- package/personas//345/217/243/346/230/257/345/277/203/351/235/236.md +0 -50
- package/personas//345/277/240/345/277/203/350/200/201/344/273/206.md +0 -51
- package/personas//346/227/240/344/272/272/350/256/276.md +0 -1
- package/personas//350/264/253/345/230/264/346/215/237/345/217/213.md +0 -48
- package/personas//350/275/257/350/220/214/350/205/271/351/273/221.md +0 -51
- package/src/behaviors/official-finite/index.test.ts +0 -249
- package/src/channel/claude-channel-server.test.ts +0 -202
- package/src/commands/account.test.ts +0 -570
- package/src/commands/behavior.test.ts +0 -158
- package/src/commands/communication.test.ts +0 -282
- package/src/commands/competition.test.ts +0 -116
- package/src/commands/data.test.ts +0 -89
- package/src/commands/events-canonical-state.test.ts +0 -406
- package/src/commands/events.test.ts +0 -58
- package/src/commands/game/index.test.ts +0 -1251
- package/src/commands/game-map.test.ts +0 -69
- package/src/commands/hub.test.ts +0 -124
- package/src/commands/load.test.ts +0 -440
- package/src/commands/perception.test.ts +0 -75
- package/src/commands/setup/hermes.test.ts +0 -96
- package/src/commands/setup/openclaw.test.ts +0 -124
- package/src/commands/trigger.test.ts +0 -140
- package/src/commands/upgrade.test.ts +0 -82
- package/src/lib/account-guidance.test.ts +0 -36
- package/src/lib/auth.test.ts +0 -151
- package/src/lib/behavior-export.test.ts +0 -326
- package/src/lib/behavior-export.ts +0 -537
- package/src/lib/clawclaw-skill-spectate-guidance.test.ts +0 -21
- package/src/lib/custom-tts.test.ts +0 -387
- package/src/lib/diagnostics-config.test.ts +0 -59
- package/src/lib/game-client-tts.test.ts +0 -151
- package/src/lib/game-client.test.ts +0 -250
- package/src/lib/gameplay-config.test.ts +0 -301
- package/src/lib/host-config-patcher.test.ts +0 -130
- package/src/lib/http-transport.test.ts +0 -124
- package/src/lib/hub-client.test.ts +0 -57
- package/src/lib/hub-install.test.ts +0 -135
- package/src/lib/hub-install.ts +0 -185
- package/src/lib/hub-unzip.test.ts +0 -69
- package/src/lib/init-command.test.ts +0 -103
- package/src/lib/load-context.test.ts +0 -62
- package/src/lib/match-state.test.ts +0 -134
- package/src/lib/official-resource-sync.test.ts +0 -199
- package/src/lib/official-resource-sync.ts +0 -221
- package/src/lib/perception-config.test.ts +0 -143
- package/src/lib/perception-export.test.ts +0 -145
- package/src/lib/perception-export.ts +0 -178
- package/src/lib/persona.test.ts +0 -84
- package/src/lib/room.test.ts +0 -88
- package/src/lib/server-registry.test.ts +0 -64
- package/src/lib/service-status.test.ts +0 -81
- package/src/lib/skill-version.test.ts +0 -48
- package/src/lib/trigger-config.test.ts +0 -102
- package/src/lib/trigger-export.test.ts +0 -80
- package/src/lib/trigger-export.ts +0 -210
- package/src/lib/user-data.test.ts +0 -121
- package/src/lib/workspace-argv.test.ts +0 -49
- package/src/pipeline/canonical-event-presentation.test.ts +0 -257
- package/src/pipeline/event-format.test.ts +0 -879
- package/src/pipeline/event-hints.test.ts +0 -337
- package/src/pipeline/game-sync-event-normalization.test.ts +0 -419
- package/src/pipeline/player-projection.test.ts +0 -247
- package/src/runtime/auto-upgrade.test.ts +0 -66
- package/src/runtime/canonical-event-emitter.test.ts +0 -70
- package/src/runtime/canonical-monitor.test.ts +0 -577
- package/src/runtime/clawclaw/agent-event-policy.test.ts +0 -60
- package/src/runtime/clawclaw/behavior-decision.test.ts +0 -43
- package/src/runtime/clawclaw/behavior-input-fields.test.ts +0 -175
- package/src/runtime/clawclaw/behavior-input.test.ts +0 -173
- package/src/runtime/clawclaw/behavior-observation-memory.test.ts +0 -59
- package/src/runtime/clawclaw/behavior-read-api.test.ts +0 -123
- package/src/runtime/clawclaw/notifications.test.ts +0 -47
- package/src/runtime/clawclaw/owner-lock.test.ts +0 -221
- package/src/runtime/clawclaw/owner.test.ts +0 -203
- package/src/runtime/clawclaw/plugins/action/action-plugin.test.ts +0 -328
- package/src/runtime/clawclaw/plugins/assembly.test.ts +0 -200
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.test.ts +0 -180
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.test.ts +0 -43
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.test.ts +0 -156
- package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.test.ts +0 -182
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +0 -1094
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.test.ts +0 -174
- package/src/runtime/clawclaw/plugins/behavior/control-driver.test.ts +0 -195
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.test.ts +0 -283
- package/src/runtime/clawclaw/plugins/behavior-input-source.test.ts +0 -80
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.test.ts +0 -267
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +0 -226
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.test.ts +0 -239
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +0 -407
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-push.integration.test.ts +0 -397
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.test.ts +0 -270
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +0 -166
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.test.ts +0 -194
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.test.ts +0 -87
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +0 -2012
- package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +0 -214
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +0 -347
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.test.ts +0 -92
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.ts +0 -248
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +0 -389
- package/src/runtime/clawclaw/plugins/perception/modules/index.ts +0 -8
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +0 -342
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +0 -475
- package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +0 -30
- package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +0 -304
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +0 -179
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +0 -364
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +0 -81
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +0 -200
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +0 -216
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +0 -284
- package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +0 -120
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +0 -105
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +0 -269
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +0 -865
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +0 -1878
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +0 -278
- package/src/runtime/clawclaw/plugins/perception/replay.test.ts +0 -77
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +0 -853
- package/src/runtime/clawclaw/plugins/source/game-transport.test.ts +0 -287
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +0 -321
- package/src/runtime/clawclaw/plugins/source/room-transport.test.ts +0 -307
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +0 -2646
- package/src/runtime/clawclaw/plugins/source/task-change-tracker.test.ts +0 -212
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.test.ts +0 -548
- package/src/runtime/clawclaw/plugins/trigger/trigger-source.test.ts +0 -169
- package/src/runtime/clawclaw/runtime-data-export.test.ts +0 -249
- package/src/runtime/clawclaw-event-catalog.test.ts +0 -128
- package/src/runtime/framework/action-diagnostics.test.ts +0 -81
- package/src/runtime/framework/capability-registry.test.ts +0 -423
- package/src/runtime/framework/control-coordinator.test.ts +0 -675
- package/src/runtime/framework/events/event-delivery-service.test.ts +0 -84
- package/src/runtime/framework/events/event-ingress.test.ts +0 -203
- package/src/runtime/framework/events/mailbox-event-repositories.test.ts +0 -710
- package/src/runtime/framework/events/sqlite-event-repository.test.ts +0 -802
- package/src/runtime/framework/mailbox/coalescer.test.ts +0 -55
- package/src/runtime/framework/mailbox/identity.test.ts +0 -38
- package/src/runtime/framework/mailbox/mailbox-dispatcher.test.ts +0 -513
- package/src/runtime/framework/mailbox/notification-projector.test.ts +0 -77
- package/src/runtime/framework/modules/runtime-module-loader.test.ts +0 -435
- package/src/runtime/framework/participation-manager.test.ts +0 -108
- package/src/runtime/framework/plugins/runtime-input-journal.test.ts +0 -179
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +0 -2040
- package/src/runtime/framework/state/durable-agent-state-snapshot.test.ts +0 -144
- package/src/runtime/framework/state/history/sqlite-state-history-repository.test.ts +0 -157
- package/src/runtime/framework/state/history/state-history.test.ts +0 -251
- package/src/runtime/framework/state/state-core.test.ts +0 -197
- package/src/runtime/game-sync-protocol.test.ts +0 -123
- package/src/runtime/host-monitor-rate-gate.test.ts +0 -73
- package/src/runtime/mailbox-host-relay.test.ts +0 -293
- package/src/runtime/observability/observability.test.ts +0 -583
- package/src/runtime/owner-control.test.ts +0 -153
- package/src/runtime/parent-process-watch.test.ts +0 -27
- package/src/runtime/pending-event-handoff.test.ts +0 -29
- package/src/runtime/raw-ws-log.test.ts +0 -102
- package/src/runtime/runtime-logger.test.ts +0 -58
- package/src/runtime/runtime-persistence.test.ts +0 -26
- package/src/runtime/runtime.test.ts +0 -1770
- package/src/runtime/ws-client.test.ts +0 -562
- package/src/sdk/action.test.ts +0 -8
- package/src/sdk/behavior-plugin/index.test.ts +0 -74
- package/src/sdk/index.test.ts +0 -45
- package/src/sdk/module-input/index.test.ts +0 -49
- package/src/sdk/perception-plugin/index.test.ts +0 -22
- package/src/sdk/resource-metadata.test.ts +0 -40
- package/src/sdk/trigger-plugin/index.test.ts +0 -41
- package/src/short-command/classifier.test.ts +0 -107
- package/src/short-command/presenter.test.ts +0 -119
- package/src/short-command/runner.test.ts +0 -78
- package/src/short-command/sensitive-data-filter.test.ts +0 -56
- package/src/short-command/transport-response.test.ts +0 -39
- package/src/strategies/custom-module.test.ts +0 -267
- package/src/strategies/game-utils.test.ts +0 -305
- package/src/strategies/goals/avoid-players-top.test.ts +0 -95
- package/src/strategies/goals/crab-sabotage-top.test.ts +0 -131
- package/src/strategies/goals/kill-lone-top.test.ts +0 -96
- package/src/strategies/goals/kill-target-top.test.ts +0 -65
- package/src/strategies/goals/lone-kill-task-top.test.ts +0 -84
- package/src/strategies/goals/normal-shrimp-top.test.ts +0 -112
- package/src/strategies/goals/paradise-fish-top.test.ts +0 -203
- package/src/strategies/goals/task-only-top.test.ts +0 -88
- package/src/strategies/goals/warrior-shrimp-top.test.ts +0 -138
- package/src/strategies/meeting-gate.test.ts +0 -59
- package/src/strategies/official-long-behavior.test.ts +0 -89
- package/src/strategies/pathfind/astar-path.test.ts +0 -198
- package/src/strategies/pathfind/escape-planner.test.ts +0 -223
- package/src/strategies/player-targets.test.ts +0 -10
- package/src/strategies/task-only.test.ts +0 -138
|
@@ -1,397 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import { createServer, type Server, type Socket } from 'node:net';
|
|
3
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
-
import { presentCanonicalEventForDesktop } from '../../../../pipeline/canonical-event-presentation.js';
|
|
5
|
-
import { compactStateForEvents } from '../../../../pipeline/event-format.js';
|
|
6
|
-
import { createClawclawEventCatalog } from '../../../clawclaw-event-catalog.js';
|
|
7
|
-
import {
|
|
8
|
-
EventIngress,
|
|
9
|
-
MemoryEventRepository,
|
|
10
|
-
type CanonicalEvent,
|
|
11
|
-
} from '../../../framework/events/index.js';
|
|
12
|
-
import type { RuntimeReadyView } from '../../../framework/runtime.js';
|
|
13
|
-
import {
|
|
14
|
-
AGENT_MAILBOX_COALESCE_MS,
|
|
15
|
-
AgentMailboxCoordinator,
|
|
16
|
-
} from './agent-mailbox-coordinator.js';
|
|
17
|
-
import {
|
|
18
|
-
DESKTOP_EVENT_PROTOCOL_SCHEMA,
|
|
19
|
-
DesktopEventSocketSink,
|
|
20
|
-
} from './desktop-event-socket.js';
|
|
21
|
-
import {
|
|
22
|
-
ClawclawMailboxPlugin,
|
|
23
|
-
CompositeClawclawMailboxDriver,
|
|
24
|
-
} from './mailbox-plugin.js';
|
|
25
|
-
import { MailboxEventBatchPump } from './mailbox-event-batch-delivery.js';
|
|
26
|
-
|
|
27
|
-
interface ReceivedFrame {
|
|
28
|
-
atMs: number;
|
|
29
|
-
frame: Record<string, any>;
|
|
30
|
-
socket: Socket;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const servers: Server[] = [];
|
|
34
|
-
const serverSockets: Socket[] = [];
|
|
35
|
-
|
|
36
|
-
async function closeServer(server: Server): Promise<void> {
|
|
37
|
-
if (!server.listening) return;
|
|
38
|
-
await new Promise<void>((resolve, reject) => {
|
|
39
|
-
server.close((error) => error ? reject(error) : resolve());
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
afterEach(async () => {
|
|
44
|
-
for (const socket of serverSockets.splice(0)) socket.destroy();
|
|
45
|
-
await Promise.all(servers.splice(0).map((server) => closeServer(server)));
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
function delay(milliseconds: number): Promise<void> {
|
|
49
|
-
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function readyView(epochSeq: number, eventWatermark: number): RuntimeReadyView {
|
|
53
|
-
const scope = {
|
|
54
|
-
adapterId: 'clawclaw',
|
|
55
|
-
accountId: 'account-1',
|
|
56
|
-
sessionId: 'session-1',
|
|
57
|
-
segmentId: 'game-1',
|
|
58
|
-
segmentSeq: 1,
|
|
59
|
-
};
|
|
60
|
-
return {
|
|
61
|
-
scope,
|
|
62
|
-
state: {
|
|
63
|
-
state: {
|
|
64
|
-
phase: 'wandering',
|
|
65
|
-
tick: epochSeq,
|
|
66
|
-
you: { name: 'self', seat: 1, alive: true },
|
|
67
|
-
},
|
|
68
|
-
stateStatus: {
|
|
69
|
-
status: 'fresh',
|
|
70
|
-
semantics: 'latest_received',
|
|
71
|
-
source: 'owner_memory',
|
|
72
|
-
scope,
|
|
73
|
-
connectionId: 'connection-1',
|
|
74
|
-
connectionState: 'connected',
|
|
75
|
-
receivedAtMs: Date.now(),
|
|
76
|
-
ageMs: 0,
|
|
77
|
-
backendTick: epochSeq,
|
|
78
|
-
reason: null,
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
pluginStates: {},
|
|
82
|
-
pluginOutcomes: {},
|
|
83
|
-
events: [],
|
|
84
|
-
throughCommitSeq: eventWatermark,
|
|
85
|
-
frameSeq: epochSeq,
|
|
86
|
-
eventWatermark,
|
|
87
|
-
epochSeq,
|
|
88
|
-
hasMore: false,
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async function publish(
|
|
93
|
-
ingress: EventIngress,
|
|
94
|
-
input: {
|
|
95
|
-
eventId: string;
|
|
96
|
-
type: string;
|
|
97
|
-
epochSeq: number;
|
|
98
|
-
producer?: 'backend_push' | 'local_synth';
|
|
99
|
-
payload?: Record<string, unknown>;
|
|
100
|
-
},
|
|
101
|
-
): Promise<CanonicalEvent> {
|
|
102
|
-
return (await ingress.publish({
|
|
103
|
-
epochSeq: input.epochSeq,
|
|
104
|
-
eventId: input.eventId,
|
|
105
|
-
type: input.type,
|
|
106
|
-
producer: input.producer ?? 'backend_push',
|
|
107
|
-
sessionId: 'session-1',
|
|
108
|
-
segmentId: 'game-1',
|
|
109
|
-
payload: input.payload ?? {},
|
|
110
|
-
source: { sourceId: 'test', eventId: `remote:${input.eventId}` },
|
|
111
|
-
})).event;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function sendAck(
|
|
115
|
-
received: ReceivedFrame,
|
|
116
|
-
overrides: Partial<Record<'connection_id' | 'seq' | 'delivery_id', unknown>> = {},
|
|
117
|
-
): void {
|
|
118
|
-
const frame = received.frame;
|
|
119
|
-
received.socket.write(`${JSON.stringify({
|
|
120
|
-
schema: DESKTOP_EVENT_PROTOCOL_SCHEMA,
|
|
121
|
-
type: 'ack',
|
|
122
|
-
connection_id: frame.connection_id,
|
|
123
|
-
seq: frame.seq,
|
|
124
|
-
delivery_id: frame.delivery_id,
|
|
125
|
-
receipt_id: `wake-${frame.seq}`,
|
|
126
|
-
...overrides,
|
|
127
|
-
})}\n`);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
describe('Desktop Agent event push integration', () => {
|
|
131
|
-
it('pushes sanitized ready visible Events and retries one durable batch until exact ACK', async () => {
|
|
132
|
-
const pipeName = `ccl-desktop-integration-${randomUUID()}`;
|
|
133
|
-
const socketPath = process.platform === 'win32'
|
|
134
|
-
? `\\\\.\\pipe\\${pipeName}`
|
|
135
|
-
: `/tmp/${pipeName}.sock`;
|
|
136
|
-
const received: ReceivedFrame[] = [];
|
|
137
|
-
let disconnectNextBatch = false;
|
|
138
|
-
const server = createServer((socket) => {
|
|
139
|
-
serverSockets.push(socket);
|
|
140
|
-
socket.setEncoding('utf8');
|
|
141
|
-
let buffer = '';
|
|
142
|
-
socket.on('data', (chunk: string) => {
|
|
143
|
-
buffer += chunk;
|
|
144
|
-
for (;;) {
|
|
145
|
-
const newline = buffer.indexOf('\n');
|
|
146
|
-
if (newline < 0) break;
|
|
147
|
-
const line = buffer.slice(0, newline);
|
|
148
|
-
buffer = buffer.slice(newline + 1);
|
|
149
|
-
if (!line.trim()) continue;
|
|
150
|
-
const frame = JSON.parse(line) as Record<string, any>;
|
|
151
|
-
received.push({ atMs: Date.now(), frame, socket });
|
|
152
|
-
if (frame.type === 'mailbox_batch' && disconnectNextBatch) {
|
|
153
|
-
disconnectNextBatch = false;
|
|
154
|
-
socket.destroy();
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
servers.push(server);
|
|
160
|
-
await new Promise<void>((resolve, reject) => {
|
|
161
|
-
server.once('error', reject);
|
|
162
|
-
server.listen(socketPath, resolve);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const repository = new MemoryEventRepository();
|
|
166
|
-
repository.saveSession({
|
|
167
|
-
sessionId: 'session-1',
|
|
168
|
-
adapterId: 'clawclaw',
|
|
169
|
-
accountId: 'account-1',
|
|
170
|
-
status: 'open',
|
|
171
|
-
isCurrent: true,
|
|
172
|
-
openedAt: '2026-08-04T00:00:00.000Z',
|
|
173
|
-
});
|
|
174
|
-
repository.saveSegment({
|
|
175
|
-
segmentId: 'game-1',
|
|
176
|
-
sessionId: 'session-1',
|
|
177
|
-
sequence: 1,
|
|
178
|
-
kind: 'game',
|
|
179
|
-
status: 'open',
|
|
180
|
-
isCurrent: true,
|
|
181
|
-
openedAt: '2026-08-04T00:00:00.000Z',
|
|
182
|
-
});
|
|
183
|
-
const ingress = new EventIngress(repository, createClawclawEventCatalog());
|
|
184
|
-
let ready = readyView(1, 0);
|
|
185
|
-
const coordinator = new AgentMailboxCoordinator({
|
|
186
|
-
repository,
|
|
187
|
-
readyView: () => structuredClone(ready),
|
|
188
|
-
presentEvent: presentCanonicalEventForDesktop,
|
|
189
|
-
compactState: compactStateForEvents,
|
|
190
|
-
});
|
|
191
|
-
let pump: MailboxEventBatchPump | undefined;
|
|
192
|
-
const sink = new DesktopEventSocketSink({
|
|
193
|
-
socketPath,
|
|
194
|
-
token: '0123456789abcdef0123456789abcdef',
|
|
195
|
-
ackTimeoutMs: 2_000,
|
|
196
|
-
reconnectBaseMs: 10,
|
|
197
|
-
reconnectMaxMs: 20,
|
|
198
|
-
onConnected: () => pump?.retryNow(),
|
|
199
|
-
activeResources: () => ({
|
|
200
|
-
locale: 'zh-CN',
|
|
201
|
-
strategy: {
|
|
202
|
-
id: 'normal-shrimp',
|
|
203
|
-
name: '普通虾',
|
|
204
|
-
description: '完成任务并保持警觉。',
|
|
205
|
-
},
|
|
206
|
-
perceptions: [{
|
|
207
|
-
id: 'visual.motion',
|
|
208
|
-
name: '玩家移动',
|
|
209
|
-
description: '感知自己和可见玩家的连续移动片段及显著方向变化。',
|
|
210
|
-
}],
|
|
211
|
-
}),
|
|
212
|
-
eventDescriptors: () => [{
|
|
213
|
-
type: 'visual.player_moved',
|
|
214
|
-
perception_id: 'visual.motion',
|
|
215
|
-
name: '玩家移动',
|
|
216
|
-
description: '玩家持续移动形成一段可观察的移动轨迹。',
|
|
217
|
-
}],
|
|
218
|
-
});
|
|
219
|
-
pump = new MailboxEventBatchPump({
|
|
220
|
-
source: {
|
|
221
|
-
prepare: () => coordinator.prepareEvents({
|
|
222
|
-
deliveryVia: 'mailbox_full',
|
|
223
|
-
ownerIdPrefix: 'desktop_ipc_test',
|
|
224
|
-
}),
|
|
225
|
-
renew: (deliveryToken) => coordinator.renew(deliveryToken),
|
|
226
|
-
ack: (deliveryToken) => coordinator.ack(deliveryToken),
|
|
227
|
-
release: (deliveryToken) => coordinator.release(deliveryToken),
|
|
228
|
-
},
|
|
229
|
-
sink,
|
|
230
|
-
});
|
|
231
|
-
const mailbox = new ClawclawMailboxPlugin();
|
|
232
|
-
mailbox.attachDriver(new CompositeClawclawMailboxDriver([coordinator, pump]));
|
|
233
|
-
sink.start();
|
|
234
|
-
|
|
235
|
-
try {
|
|
236
|
-
await vi.waitFor(() => {
|
|
237
|
-
expect(received.filter(({ frame }) => frame.type === 'hello')).toHaveLength(1);
|
|
238
|
-
});
|
|
239
|
-
expect(received.find(({ frame }) => frame.type === 'hello')?.frame.active_resources)
|
|
240
|
-
.toMatchObject({
|
|
241
|
-
locale: 'zh-CN',
|
|
242
|
-
strategy: { id: 'normal-shrimp', name: '普通虾' },
|
|
243
|
-
perceptions: [{ id: 'visual.motion', name: '玩家移动' }],
|
|
244
|
-
});
|
|
245
|
-
expect(await mailbox.update({ pendingEvents: [] } as any)).toMatchObject({
|
|
246
|
-
outcome: 'noop',
|
|
247
|
-
reason: 'no_pending_events',
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
const internal = await publish(ingress, {
|
|
251
|
-
eventId: 'task-assigned-1',
|
|
252
|
-
type: 'task_assigned',
|
|
253
|
-
epochSeq: 1,
|
|
254
|
-
payload: { task_name: 'repair' },
|
|
255
|
-
});
|
|
256
|
-
ready = readyView(1, internal.commitSeq);
|
|
257
|
-
await mailbox.update({ pendingEvents: [internal] } as any);
|
|
258
|
-
await delay(AGENT_MAILBOX_COALESCE_MS + 60);
|
|
259
|
-
expect(received.filter(({ frame }) => frame.type === 'mailbox_batch')).toHaveLength(0);
|
|
260
|
-
|
|
261
|
-
const joined = await publish(ingress, {
|
|
262
|
-
eventId: 'joined-1',
|
|
263
|
-
type: 'joined',
|
|
264
|
-
epochSeq: 2,
|
|
265
|
-
producer: 'local_synth',
|
|
266
|
-
payload: {
|
|
267
|
-
status: 'matched',
|
|
268
|
-
url: 'https://example.test/lobby?token=desktop-secret',
|
|
269
|
-
queue: {
|
|
270
|
-
status: 'matched',
|
|
271
|
-
spectate_url: 'https://example.test/lobby?token=nested-desktop-secret',
|
|
272
|
-
},
|
|
273
|
-
},
|
|
274
|
-
});
|
|
275
|
-
const longText = `BEGIN-${'x'.repeat(70 * 1024)}-END`;
|
|
276
|
-
const speech = await publish(ingress, {
|
|
277
|
-
eventId: 'speech-1',
|
|
278
|
-
type: 'speech',
|
|
279
|
-
epochSeq: 2,
|
|
280
|
-
payload: { actor_name: 'player-2', text: longText },
|
|
281
|
-
});
|
|
282
|
-
expect(speech.deliveryClass).toBe('monitor_and_events');
|
|
283
|
-
ready = readyView(2, speech.commitSeq);
|
|
284
|
-
const firstObservedAt = Date.now();
|
|
285
|
-
await mailbox.update({ pendingEvents: [joined, speech] } as any);
|
|
286
|
-
await delay(100);
|
|
287
|
-
|
|
288
|
-
const visual = await publish(ingress, {
|
|
289
|
-
eventId: 'visual-1',
|
|
290
|
-
type: 'visual.player_moved',
|
|
291
|
-
epochSeq: 3,
|
|
292
|
-
producer: 'local_synth',
|
|
293
|
-
payload: { text: 'player-2 moved through engine' },
|
|
294
|
-
});
|
|
295
|
-
ready = readyView(3, visual.commitSeq);
|
|
296
|
-
await mailbox.update({ pendingEvents: [visual] } as any);
|
|
297
|
-
expect(received.filter(({ frame }) => frame.type === 'mailbox_batch')).toHaveLength(0);
|
|
298
|
-
|
|
299
|
-
await vi.waitFor(() => {
|
|
300
|
-
expect(received.filter(({ frame }) => frame.type === 'mailbox_batch')).toHaveLength(1);
|
|
301
|
-
}, { timeout: 2_000 });
|
|
302
|
-
const firstBatch = received.find(({ frame }) => frame.type === 'mailbox_batch')!;
|
|
303
|
-
expect(firstBatch.atMs - firstObservedAt).toBeGreaterThanOrEqual(
|
|
304
|
-
AGENT_MAILBOX_COALESCE_MS - 20,
|
|
305
|
-
);
|
|
306
|
-
expect(firstBatch.frame.payload).toMatchObject({
|
|
307
|
-
schema: 'clawclaw.channel-events.v1',
|
|
308
|
-
state: { phase: 'wandering', tick: 3 },
|
|
309
|
-
});
|
|
310
|
-
expect(firstBatch.frame.active_resources).toMatchObject({
|
|
311
|
-
locale: 'zh-CN',
|
|
312
|
-
strategy: { id: 'normal-shrimp', name: '普通虾' },
|
|
313
|
-
perceptions: [{ id: 'visual.motion', name: '玩家移动' }],
|
|
314
|
-
});
|
|
315
|
-
expect(firstBatch.frame.event_descriptors).toEqual([{
|
|
316
|
-
type: 'visual.player_moved',
|
|
317
|
-
perception_id: 'visual.motion',
|
|
318
|
-
name: '玩家移动',
|
|
319
|
-
description: '玩家持续移动形成一段可观察的移动轨迹。',
|
|
320
|
-
}]);
|
|
321
|
-
expect(firstBatch.frame.payload.events).toEqual([
|
|
322
|
-
'visual.player_moved | player-2 moved through engine',
|
|
323
|
-
expect.objectContaining({ type: 'speech', text: longText }),
|
|
324
|
-
expect.objectContaining({
|
|
325
|
-
type: 'joined',
|
|
326
|
-
status: 'matched',
|
|
327
|
-
hint: 'The game runtime is attached; do not start a duplicate runtime.',
|
|
328
|
-
}),
|
|
329
|
-
]);
|
|
330
|
-
expect(JSON.stringify(firstBatch.frame.payload)).not.toContain('desktop-secret');
|
|
331
|
-
expect(JSON.stringify(firstBatch.frame.payload)).not.toMatch(/spectate_url/iu);
|
|
332
|
-
expect(JSON.stringify(firstBatch.frame.payload)).not.toMatch(/Share this Event/iu);
|
|
333
|
-
expect(Buffer.byteLength(JSON.stringify(firstBatch.frame.payload), 'utf8'))
|
|
334
|
-
.toBeGreaterThan(64 * 1024);
|
|
335
|
-
expect(JSON.stringify(firstBatch.frame.payload)).not.toContain('task_assigned');
|
|
336
|
-
|
|
337
|
-
sendAck(firstBatch, { seq: firstBatch.frame.seq + 1 });
|
|
338
|
-
await delay(40);
|
|
339
|
-
expect(repository.delivery(speech.eventId, 'local-agent')?.detailState).toBe('pending');
|
|
340
|
-
sendAck(firstBatch);
|
|
341
|
-
await vi.waitFor(() => {
|
|
342
|
-
expect(repository.delivery(speech.eventId, 'local-agent')).toMatchObject({
|
|
343
|
-
detailState: 'delivered',
|
|
344
|
-
detailDeliveryVia: 'mailbox_full',
|
|
345
|
-
});
|
|
346
|
-
});
|
|
347
|
-
await delay(300);
|
|
348
|
-
expect(received.filter(({ frame }) => frame.type === 'mailbox_batch')).toHaveLength(1);
|
|
349
|
-
|
|
350
|
-
const boundaryEvent = await publish(ingress, {
|
|
351
|
-
eventId: 'speech-boundary',
|
|
352
|
-
type: 'speech',
|
|
353
|
-
epochSeq: 4,
|
|
354
|
-
payload: { actor_name: 'player-3', text: 'held behind ReadyView' },
|
|
355
|
-
});
|
|
356
|
-
const previousReady = ready;
|
|
357
|
-
disconnectNextBatch = true;
|
|
358
|
-
await mailbox.update({ pendingEvents: [boundaryEvent] } as any);
|
|
359
|
-
await delay(AGENT_MAILBOX_COALESCE_MS + 80);
|
|
360
|
-
expect(ready).toBe(previousReady);
|
|
361
|
-
expect(received.filter(({ frame }) => frame.type === 'mailbox_batch')).toHaveLength(1);
|
|
362
|
-
|
|
363
|
-
ready = readyView(4, boundaryEvent.commitSeq);
|
|
364
|
-
await vi.waitFor(() => {
|
|
365
|
-
expect(received.filter(({ frame }) => frame.type === 'mailbox_batch').length)
|
|
366
|
-
.toBeGreaterThanOrEqual(2);
|
|
367
|
-
}, { timeout: 2_000 });
|
|
368
|
-
await vi.waitFor(() => {
|
|
369
|
-
expect(received.filter(({ frame }) => frame.type === 'hello')).toHaveLength(2);
|
|
370
|
-
}, { timeout: 2_000 });
|
|
371
|
-
await vi.waitFor(() => {
|
|
372
|
-
expect(received.filter(({ frame }) => frame.type === 'mailbox_batch')).toHaveLength(3);
|
|
373
|
-
}, { timeout: 2_000 });
|
|
374
|
-
|
|
375
|
-
const retries = received
|
|
376
|
-
.filter(({ frame }) => frame.type === 'mailbox_batch')
|
|
377
|
-
.slice(1);
|
|
378
|
-
expect(retries).toHaveLength(2);
|
|
379
|
-
expect(retries[0].frame.delivery_id).toBe(retries[1].frame.delivery_id);
|
|
380
|
-
expect(retries[0].frame.connection_id).not.toBe(retries[1].frame.connection_id);
|
|
381
|
-
expect(repository.delivery(boundaryEvent.eventId, 'local-agent')?.detailState)
|
|
382
|
-
.toBe('pending');
|
|
383
|
-
|
|
384
|
-
sendAck(retries[1]);
|
|
385
|
-
await vi.waitFor(() => {
|
|
386
|
-
expect(repository.delivery(boundaryEvent.eventId, 'local-agent')).toMatchObject({
|
|
387
|
-
detailState: 'delivered',
|
|
388
|
-
detailDeliveryVia: 'mailbox_full',
|
|
389
|
-
});
|
|
390
|
-
});
|
|
391
|
-
await delay(300);
|
|
392
|
-
expect(received.filter(({ frame }) => frame.type === 'mailbox_batch')).toHaveLength(3);
|
|
393
|
-
} finally {
|
|
394
|
-
await mailbox.stop('shutdown');
|
|
395
|
-
}
|
|
396
|
-
}, 10_000);
|
|
397
|
-
});
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import { createServer, type Server, type Socket } from 'node:net';
|
|
3
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
-
import {
|
|
5
|
-
DESKTOP_EVENT_PROTOCOL_SCHEMA,
|
|
6
|
-
DesktopEventSocketSink,
|
|
7
|
-
normalizeDesktopEventSocketPath,
|
|
8
|
-
validateDesktopEventToken,
|
|
9
|
-
} from './desktop-event-socket.js';
|
|
10
|
-
|
|
11
|
-
const servers: Server[] = [];
|
|
12
|
-
|
|
13
|
-
async function closeServer(server: Server): Promise<void> {
|
|
14
|
-
if (!server.listening) return;
|
|
15
|
-
await new Promise<void>((resolve, reject) => {
|
|
16
|
-
server.close((error) => error ? reject(error) : resolve());
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
afterEach(async () => {
|
|
21
|
-
await Promise.all(servers.splice(0).map((server) => closeServer(server)));
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
describe('DesktopEventSocketSink', () => {
|
|
25
|
-
it('normalizes only local IPC endpoints and validates the nonce', () => {
|
|
26
|
-
expect(normalizeDesktopEventSocketPath('clawclaw-desktop', 'win32'))
|
|
27
|
-
.toBe('\\\\.\\pipe\\clawclaw-desktop');
|
|
28
|
-
expect(normalizeDesktopEventSocketPath('\\\\.\\pipe\\clawclaw-desktop', 'win32'))
|
|
29
|
-
.toBe('\\\\.\\pipe\\clawclaw-desktop');
|
|
30
|
-
expect(() => normalizeDesktopEventSocketPath('\\\\remote\\pipe\\event', 'win32'))
|
|
31
|
-
.toThrow('local named pipe');
|
|
32
|
-
expect(normalizeDesktopEventSocketPath('/tmp/clawclaw.sock', 'linux'))
|
|
33
|
-
.toBe('/tmp/clawclaw.sock');
|
|
34
|
-
expect(() => normalizeDesktopEventSocketPath('relative.sock', 'linux'))
|
|
35
|
-
.toThrow('absolute Unix socket');
|
|
36
|
-
expect(validateDesktopEventToken('0123456789abcdef')).toBe('0123456789abcdef');
|
|
37
|
-
expect(() => validateDesktopEventToken('short')).toThrow('at least 16');
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('reuses one connection and resolves only a matching application ACK', async () => {
|
|
41
|
-
const pipeName = `ccl-desktop-test-${randomUUID()}`;
|
|
42
|
-
const socketPath = process.platform === 'win32'
|
|
43
|
-
? `\\\\.\\pipe\\${pipeName}`
|
|
44
|
-
: `/tmp/${pipeName}.sock`;
|
|
45
|
-
const frames: Array<Record<string, any>> = [];
|
|
46
|
-
const connections: Socket[] = [];
|
|
47
|
-
const server = createServer((socket) => {
|
|
48
|
-
connections.push(socket);
|
|
49
|
-
socket.setEncoding('utf8');
|
|
50
|
-
let buffer = '';
|
|
51
|
-
socket.on('data', (chunk: string) => {
|
|
52
|
-
buffer += chunk;
|
|
53
|
-
for (;;) {
|
|
54
|
-
const newline = buffer.indexOf('\n');
|
|
55
|
-
if (newline < 0) break;
|
|
56
|
-
const line = buffer.slice(0, newline);
|
|
57
|
-
buffer = buffer.slice(newline + 1);
|
|
58
|
-
if (!line.trim()) continue;
|
|
59
|
-
const frame = JSON.parse(line) as Record<string, any>;
|
|
60
|
-
frames.push(frame);
|
|
61
|
-
if (frame.type !== 'mailbox_batch') continue;
|
|
62
|
-
socket.write(`${JSON.stringify({
|
|
63
|
-
schema: DESKTOP_EVENT_PROTOCOL_SCHEMA,
|
|
64
|
-
type: 'ack',
|
|
65
|
-
connection_id: frame.connection_id,
|
|
66
|
-
seq: frame.seq + 1,
|
|
67
|
-
delivery_id: frame.delivery_id,
|
|
68
|
-
})}\n`);
|
|
69
|
-
const ack = `${JSON.stringify({
|
|
70
|
-
schema: DESKTOP_EVENT_PROTOCOL_SCHEMA,
|
|
71
|
-
type: 'ack',
|
|
72
|
-
connection_id: frame.connection_id,
|
|
73
|
-
seq: frame.seq,
|
|
74
|
-
delivery_id: frame.delivery_id,
|
|
75
|
-
receipt_id: `wake-${frame.seq}`,
|
|
76
|
-
})}\n`;
|
|
77
|
-
socket.write(ack.slice(0, 7));
|
|
78
|
-
socket.write(ack.slice(7));
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
servers.push(server);
|
|
83
|
-
await new Promise<void>((resolve, reject) => {
|
|
84
|
-
server.once('error', reject);
|
|
85
|
-
server.listen(socketPath, resolve);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
const activeResources = {
|
|
89
|
-
locale: 'zh-CN',
|
|
90
|
-
strategy: null,
|
|
91
|
-
perceptions: [{
|
|
92
|
-
id: 'visual.motion',
|
|
93
|
-
name: '玩家移动',
|
|
94
|
-
description: '感知自己和可见玩家的连续移动片段及显著方向变化。',
|
|
95
|
-
}],
|
|
96
|
-
} as const;
|
|
97
|
-
const eventDescriptors = [{
|
|
98
|
-
type: 'visual.player_moved',
|
|
99
|
-
perception_id: 'visual.motion',
|
|
100
|
-
name: '玩家移动',
|
|
101
|
-
description: '玩家持续移动形成一段可观察的移动轨迹。',
|
|
102
|
-
}] as const;
|
|
103
|
-
const sink = new DesktopEventSocketSink({
|
|
104
|
-
socketPath,
|
|
105
|
-
token: '0123456789abcdef0123456789abcdef',
|
|
106
|
-
reconnectBaseMs: 10,
|
|
107
|
-
reconnectMaxMs: 20,
|
|
108
|
-
activeResources: () => activeResources,
|
|
109
|
-
eventDescriptors: () => eventDescriptors,
|
|
110
|
-
});
|
|
111
|
-
sink.start();
|
|
112
|
-
await vi.waitFor(() => expect(frames[0]).toMatchObject({
|
|
113
|
-
schema: DESKTOP_EVENT_PROTOCOL_SCHEMA,
|
|
114
|
-
type: 'hello',
|
|
115
|
-
token: '0123456789abcdef0123456789abcdef',
|
|
116
|
-
pid: process.pid,
|
|
117
|
-
active_resources: activeResources,
|
|
118
|
-
}));
|
|
119
|
-
|
|
120
|
-
const largeText = 'z'.repeat(70 * 1024);
|
|
121
|
-
const first = await sink.deliver({
|
|
122
|
-
deliveryId: 'delivery-1',
|
|
123
|
-
batch: {
|
|
124
|
-
state: { phase: 'meeting' },
|
|
125
|
-
events: [{ type: 'meeting_briefing', text: largeText }],
|
|
126
|
-
},
|
|
127
|
-
}, new AbortController().signal);
|
|
128
|
-
const second = await sink.deliver({
|
|
129
|
-
deliveryId: 'delivery-2',
|
|
130
|
-
batch: {
|
|
131
|
-
state: { phase: 'wandering' },
|
|
132
|
-
events: [
|
|
133
|
-
'visual.player_moved | 1:2,你向右移动。',
|
|
134
|
-
'visual.following | 1:2,玩家2跟随玩家3。',
|
|
135
|
-
{ type: 'speech_your_turn' },
|
|
136
|
-
],
|
|
137
|
-
},
|
|
138
|
-
}, new AbortController().signal);
|
|
139
|
-
|
|
140
|
-
expect(first.receiptId).toBe('wake-1');
|
|
141
|
-
expect(second.receiptId).toBe('wake-2');
|
|
142
|
-
expect(connections).toHaveLength(1);
|
|
143
|
-
expect(frames.filter((frame) => frame.type === 'mailbox_batch')).toEqual([
|
|
144
|
-
expect.objectContaining({
|
|
145
|
-
seq: 1,
|
|
146
|
-
delivery_id: 'delivery-1',
|
|
147
|
-
active_resources: activeResources,
|
|
148
|
-
payload: expect.objectContaining({
|
|
149
|
-
schema: 'clawclaw.channel-events.v1',
|
|
150
|
-
events: [expect.objectContaining({ text: largeText })],
|
|
151
|
-
}),
|
|
152
|
-
}),
|
|
153
|
-
expect.objectContaining({
|
|
154
|
-
seq: 2,
|
|
155
|
-
delivery_id: 'delivery-2',
|
|
156
|
-
event_descriptors: eventDescriptors,
|
|
157
|
-
payload: expect.objectContaining({
|
|
158
|
-
events: [
|
|
159
|
-
'visual.player_moved | 1:2,你向右移动。',
|
|
160
|
-
'visual.following | 1:2,玩家2跟随玩家3。',
|
|
161
|
-
{ type: 'speech_your_turn' },
|
|
162
|
-
],
|
|
163
|
-
}),
|
|
164
|
-
}),
|
|
165
|
-
]);
|
|
166
|
-
expect(frames.find((frame) => frame.delivery_id === 'delivery-1'))
|
|
167
|
-
.not.toHaveProperty('event_descriptors');
|
|
168
|
-
|
|
169
|
-
await sink.close();
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it('rejects an unacknowledged batch, reconnects, and permits the durable retry', async () => {
|
|
173
|
-
const pipeName = `ccl-desktop-retry-${randomUUID()}`;
|
|
174
|
-
const socketPath = process.platform === 'win32'
|
|
175
|
-
? `\\\\.\\pipe\\${pipeName}`
|
|
176
|
-
: `/tmp/${pipeName}.sock`;
|
|
177
|
-
const hellos: Array<Record<string, any>> = [];
|
|
178
|
-
const batches: Array<Record<string, any>> = [];
|
|
179
|
-
let connectionNumber = 0;
|
|
180
|
-
const server = createServer((socket) => {
|
|
181
|
-
connectionNumber += 1;
|
|
182
|
-
const currentConnection = connectionNumber;
|
|
183
|
-
socket.setEncoding('utf8');
|
|
184
|
-
let buffer = '';
|
|
185
|
-
socket.on('data', (chunk: string) => {
|
|
186
|
-
buffer += chunk;
|
|
187
|
-
for (;;) {
|
|
188
|
-
const newline = buffer.indexOf('\n');
|
|
189
|
-
if (newline < 0) break;
|
|
190
|
-
const line = buffer.slice(0, newline);
|
|
191
|
-
buffer = buffer.slice(newline + 1);
|
|
192
|
-
if (!line.trim()) continue;
|
|
193
|
-
const frame = JSON.parse(line) as Record<string, any>;
|
|
194
|
-
if (frame.type === 'hello') hellos.push(frame);
|
|
195
|
-
if (frame.type !== 'mailbox_batch') continue;
|
|
196
|
-
batches.push(frame);
|
|
197
|
-
if (currentConnection === 1) {
|
|
198
|
-
socket.destroy();
|
|
199
|
-
continue;
|
|
200
|
-
}
|
|
201
|
-
socket.write(`${JSON.stringify({
|
|
202
|
-
schema: DESKTOP_EVENT_PROTOCOL_SCHEMA,
|
|
203
|
-
type: 'ack',
|
|
204
|
-
connection_id: frame.connection_id,
|
|
205
|
-
seq: frame.seq,
|
|
206
|
-
delivery_id: frame.delivery_id,
|
|
207
|
-
})}\n`);
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
servers.push(server);
|
|
212
|
-
await new Promise<void>((resolve, reject) => {
|
|
213
|
-
server.once('error', reject);
|
|
214
|
-
server.listen(socketPath, resolve);
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
let strategy = {
|
|
218
|
-
id: 'strategy-a',
|
|
219
|
-
name: '策略甲',
|
|
220
|
-
description: '策略甲描述。',
|
|
221
|
-
};
|
|
222
|
-
let eventName = '旧玩家移动';
|
|
223
|
-
const sink = new DesktopEventSocketSink({
|
|
224
|
-
socketPath,
|
|
225
|
-
token: '0123456789abcdef0123456789abcdef',
|
|
226
|
-
reconnectBaseMs: 10,
|
|
227
|
-
reconnectMaxMs: 20,
|
|
228
|
-
activeResources: () => ({ locale: 'zh-CN', strategy, perceptions: [] }),
|
|
229
|
-
eventDescriptors: () => [{
|
|
230
|
-
type: 'visual.player_moved',
|
|
231
|
-
perception_id: 'visual.motion',
|
|
232
|
-
name: eventName,
|
|
233
|
-
description: '重试快照测试。',
|
|
234
|
-
}],
|
|
235
|
-
});
|
|
236
|
-
sink.start();
|
|
237
|
-
await vi.waitFor(() => expect(hellos).toHaveLength(1));
|
|
238
|
-
const envelope = {
|
|
239
|
-
deliveryId: 'durable-delivery',
|
|
240
|
-
batch: {
|
|
241
|
-
state: null,
|
|
242
|
-
events: ['visual.player_moved | 测试移动。'],
|
|
243
|
-
},
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
await expect(sink.deliver(envelope, new AbortController().signal)).rejects.toMatchObject({
|
|
247
|
-
code: 'desktop_event_socket_disconnected',
|
|
248
|
-
retryable: true,
|
|
249
|
-
});
|
|
250
|
-
strategy = {
|
|
251
|
-
id: 'strategy-b',
|
|
252
|
-
name: '策略乙',
|
|
253
|
-
description: '策略乙描述。',
|
|
254
|
-
};
|
|
255
|
-
eventName = '新事件名称';
|
|
256
|
-
await vi.waitFor(() => expect(hellos).toHaveLength(2));
|
|
257
|
-
await expect(sink.deliver(envelope, new AbortController().signal)).resolves.toMatchObject({
|
|
258
|
-
acceptedAt: expect.any(String),
|
|
259
|
-
});
|
|
260
|
-
expect(hellos[1].connection_id).not.toBe(hellos[0].connection_id);
|
|
261
|
-
expect(hellos[1].active_resources.strategy.id).toBe('strategy-b');
|
|
262
|
-
expect(batches).toHaveLength(2);
|
|
263
|
-
expect(batches[0].active_resources.strategy.id).toBe('strategy-a');
|
|
264
|
-
expect(batches[1].active_resources.strategy.id).toBe('strategy-a');
|
|
265
|
-
expect(batches[0].event_descriptors[0].name).toBe('旧玩家移动');
|
|
266
|
-
expect(batches[1].event_descriptors[0].name).toBe('旧玩家移动');
|
|
267
|
-
|
|
268
|
-
await sink.close();
|
|
269
|
-
});
|
|
270
|
-
});
|