@myclaw163/clawclaw-cli 0.6.111 → 0.7.4
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/QUICKSTART.md +46 -0
- package/QUICKSTART_HERMES.md +134 -0
- package/QUICKSTART_OPENCLAW.md +134 -0
- package/QUICKSTART_OTHER.md +134 -0
- package/README.md +23 -398
- package/bin/clawclaw-cli.mjs +9 -1
- package/docs/configuration.md +197 -0
- package/docs/runtime/README.md +52 -0
- package/docs/runtime/agent-notifications.md +55 -0
- package/docs/runtime/components-and-boundaries.md +213 -0
- package/docs/runtime/events-and-state.md +105 -0
- package/docs/runtime/owner-lifecycle-and-observability.md +69 -0
- package/docs/runtime/perception-replay.md +51 -0
- package/docs/runtime/short-commands-and-capabilities.md +55 -0
- package/docs/runtime/strategy-runtime.md +111 -0
- package/docs//350/207/252/345/256/232/344/271/211/346/204/237/347/237/245.md +488 -0
- package/docs//350/207/252/345/256/232/344/271/211/347/255/226/347/225/245.md +395 -0
- package/package.json +24 -7
- package/scripts/check-skill-command-surface.mjs +16 -7
- package/scripts/compare-perception-replays.test.ts +188 -0
- package/scripts/compare-perception-replays.ts +589 -0
- package/scripts/generate-spatial-facts.py +246 -0
- package/scripts/package-readme.mjs +48 -0
- package/scripts/replay-perception.ts +347 -0
- package/scripts/sync-frontend-map.mjs +195 -0
- package/scripts/watch-perception-events.test.ts +43 -0
- package/scripts/watch-perception-events.ts +162 -0
- package/skills/clawclaw/SKILL.md +120 -64
- package/skills/clawclaw/references/CHATTERBOX.md +2 -4
- package/skills/clawclaw/references/CLAUDE-CODE-MONITOR.md +1 -1
- package/skills/clawclaw/references/COMMANDS.md +95 -61
- package/skills/clawclaw/references/GAME-MECHANICS.md +18 -8
- package/skills/clawclaw/references/HUB.md +5 -5
- package/skills/clawclaw/references/STRATEGIES.md +34 -17
- package/skills/clawclaw/references/STREAM.md +82 -46
- package/src/assets/clawclaw-map.json +1692 -0
- package/src/assets/clawclaw-spatial-facts.json +1291 -0
- package/src/cli.ts +21 -47
- package/src/commands/account.test.ts +229 -50
- package/src/commands/account.ts +456 -223
- package/src/commands/communication.test.ts +259 -0
- package/src/commands/communication.ts +391 -0
- package/src/commands/competition.test.ts +116 -0
- package/src/commands/competition.ts +50 -28
- package/src/commands/data.test.ts +57 -1
- package/src/commands/data.ts +79 -4
- package/src/commands/events-canonical-state.test.ts +187 -0
- package/src/commands/events.test.ts +50 -136
- package/src/commands/events.ts +175 -241
- package/src/commands/game/index.test.ts +510 -0
- package/src/commands/game/index.ts +556 -0
- package/src/commands/game-map.test.ts +40 -3
- package/src/commands/hub.ts +2 -9
- package/src/commands/load.test.ts +12 -2
- package/src/commands/load.ts +15 -2
- package/src/commands/peek.ts +2 -3
- package/src/commands/perception.test.ts +46 -0
- package/src/commands/perception.ts +92 -0
- package/src/commands/setup/codex.ts +0 -1
- package/src/commands/strategy.test.ts +139 -83
- package/src/commands/strategy.ts +175 -125
- package/src/commands/upgrade.ts +0 -21
- package/src/lib/auth.test.ts +35 -0
- package/src/lib/auth.ts +45 -6
- package/src/lib/diagnostics-config.test.ts +59 -0
- package/src/lib/diagnostics-config.ts +76 -0
- package/src/lib/game-client.test.ts +166 -0
- package/src/lib/game-client.ts +316 -87
- package/src/lib/game-context.ts +29 -0
- package/src/lib/gameplay-config.test.ts +66 -0
- package/src/lib/gameplay-config.ts +151 -0
- package/src/lib/http-transport.test.ts +63 -0
- package/src/lib/http-transport.ts +76 -10
- package/src/lib/init-command.test.ts +25 -1
- package/src/lib/init-command.ts +66 -1
- package/src/lib/participation-leave.ts +78 -0
- package/src/lib/perception-config.test.ts +138 -0
- package/src/lib/perception-config.ts +101 -0
- package/src/lib/room.test.ts +14 -0
- package/src/lib/room.ts +28 -2
- package/src/lib/server-registry.test.ts +15 -5
- package/src/lib/server-registry.ts +9 -10
- package/src/lib/strategy-export.test.ts +123 -44
- package/src/lib/strategy-export.ts +111 -101
- package/src/lib/user-data.test.ts +2 -0
- package/src/lib/user-data.ts +17 -16
- package/src/pipeline/canonical-event-presentation.test.ts +195 -0
- package/src/pipeline/canonical-event-presentation.ts +71 -0
- package/src/pipeline/event-format.test.ts +357 -59
- package/src/pipeline/event-format.ts +365 -80
- package/src/pipeline/event-hints.test.ts +241 -0
- package/src/pipeline/event-hints.ts +202 -51
- package/src/pipeline/game-sync-event-normalization.test.ts +239 -0
- package/src/pipeline/game-sync-event-normalization.ts +215 -0
- package/src/pipeline/player-projection.test.ts +164 -102
- package/src/pipeline/player-projection.ts +194 -235
- package/src/runtime/canonical-event-emitter.test.ts +70 -0
- package/src/runtime/canonical-event-emitter.ts +78 -0
- package/src/runtime/canonical-monitor.test.ts +575 -0
- package/src/runtime/canonical-monitor.ts +388 -0
- package/src/runtime/clawclaw/agent-event-policy.test.ts +27 -0
- package/src/runtime/clawclaw/agent-event-policy.ts +35 -0
- package/src/runtime/clawclaw/index.ts +4 -0
- package/src/runtime/clawclaw/notifications.test.ts +47 -0
- package/src/runtime/clawclaw/notifications.ts +65 -0
- package/src/runtime/clawclaw/owner-lock.test.ts +221 -0
- package/src/runtime/clawclaw/owner.ts +945 -0
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +299 -0
- package/src/runtime/clawclaw/plugins/action/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/assembly.test.ts +81 -0
- package/src/runtime/clawclaw/plugins/assembly.ts +116 -0
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.test.ts +150 -0
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.ts +100 -0
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-rules.ts +162 -0
- package/src/runtime/clawclaw/plugins/derived-event/index.ts +2 -0
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +223 -0
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +236 -0
- package/src/runtime/clawclaw/plugins/game-query/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/index.ts +7 -0
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +3 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +77 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +91 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +1008 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +475 -0
- package/src/runtime/clawclaw/plugins/perception/contracts.ts +32 -0
- package/src/runtime/clawclaw/plugins/perception/index.ts +8 -0
- package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +204 -0
- package/src/runtime/clawclaw/plugins/perception/module-runner.ts +290 -0
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +324 -0
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +365 -0
- package/src/runtime/clawclaw/plugins/perception/modules/index.ts +7 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +341 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +447 -0
- package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +30 -0
- package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +289 -0
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +180 -0
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +329 -0
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +82 -0
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +172 -0
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +212 -0
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +256 -0
- package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +121 -0
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +106 -0
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +225 -0
- package/src/runtime/clawclaw/plugins/perception/official-module-ids.ts +11 -0
- package/src/runtime/clawclaw/plugins/perception/official-modules.ts +31 -0
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +759 -0
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.ts +707 -0
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +215 -0
- package/src/runtime/clawclaw/plugins/perception/perception-module-loader.ts +61 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +1516 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +1529 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +119 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +244 -0
- package/src/runtime/clawclaw/plugins/perception/replay.test.ts +65 -0
- package/src/runtime/clawclaw/plugins/perception/replay.ts +86 -0
- package/src/runtime/clawclaw/plugins/perception/shared/math.ts +107 -0
- package/src/runtime/clawclaw/plugins/perception/spatial-facts.ts +114 -0
- package/src/runtime/clawclaw/plugins/perception/visual-state-projector.ts +98 -0
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +1019 -0
- package/src/runtime/clawclaw/plugins/source/game-transport.test.ts +287 -0
- package/src/runtime/clawclaw/plugins/source/game-transport.ts +281 -0
- package/src/runtime/clawclaw/plugins/source/index.ts +5 -0
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +270 -0
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.ts +151 -0
- package/src/runtime/clawclaw/plugins/source/room-transport.test.ts +307 -0
- package/src/runtime/clawclaw/plugins/source/room-transport.ts +480 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +1909 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +2055 -0
- package/src/runtime/clawclaw/plugins/source/source-transports.ts +30 -0
- package/src/runtime/clawclaw/plugins/source/types.ts +79 -0
- package/src/runtime/clawclaw/plugins/strategy/context-capabilities.ts +5 -0
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.test.ts +177 -0
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.ts +185 -0
- package/src/runtime/clawclaw/plugins/strategy/index.ts +8 -0
- package/src/runtime/clawclaw/plugins/strategy/local-strategy-executor.ts +412 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-action.ts +73 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-analysis-thread-host.ts +183 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor-types.ts +25 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor.test.ts +693 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-game-api.ts +57 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-module-loader.ts +164 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.test.ts +1936 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +1557 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.test.ts +131 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.ts +413 -0
- package/src/runtime/clawclaw/plugins/strategy/thread-strategy-executor.ts +792 -0
- package/src/runtime/clawclaw/runtime-data-export.test.ts +249 -0
- package/src/runtime/clawclaw/runtime-data-export.ts +402 -0
- package/src/runtime/clawclaw/runtime-state.ts +98 -0
- package/src/runtime/clawclaw/runtime.ts +133 -0
- package/src/runtime/clawclaw-event-catalog.test.ts +74 -0
- package/src/runtime/clawclaw-event-catalog.ts +266 -0
- package/src/runtime/clawclaw-position.ts +4 -0
- package/src/runtime/clawclaw-room-state.ts +51 -0
- package/src/runtime/framework/action-diagnostics.test.ts +81 -0
- package/src/runtime/framework/action-diagnostics.ts +146 -0
- package/src/runtime/framework/capability-registry.test.ts +282 -0
- package/src/runtime/framework/capability-registry.ts +283 -0
- package/src/runtime/framework/contracts.ts +118 -0
- package/src/runtime/framework/control/control-scheduler.ts +1260 -0
- package/src/runtime/framework/control/domain-set.ts +81 -0
- package/src/runtime/framework/control/index.ts +3 -0
- package/src/runtime/framework/control/types.ts +184 -0
- package/src/runtime/framework/control-coordinator.test.ts +660 -0
- package/src/runtime/framework/control-coordinator.ts +11 -0
- package/src/runtime/framework/events/event-catalog.ts +74 -0
- package/src/runtime/framework/events/event-delivery-service.test.ts +84 -0
- package/src/runtime/framework/events/event-delivery-service.ts +198 -0
- package/src/runtime/framework/events/event-ingress.test.ts +178 -0
- package/src/runtime/framework/events/event-ingress.ts +97 -0
- package/src/runtime/framework/events/event-repository.ts +59 -0
- package/src/runtime/framework/events/index.ts +7 -0
- package/src/runtime/framework/events/mailbox-event-repositories.test.ts +594 -0
- package/src/runtime/framework/events/memory-event-repository.ts +928 -0
- package/src/runtime/framework/events/sqlite-event-repository.test.ts +200 -0
- package/src/runtime/framework/events/sqlite-event-repository.ts +1681 -0
- package/src/runtime/framework/events/test-helpers.ts +78 -0
- package/src/runtime/framework/events/types.ts +286 -0
- package/src/runtime/framework/index.ts +7 -0
- package/src/runtime/framework/load-node-sqlite.ts +17 -0
- package/src/runtime/framework/mailbox/coalescer.test.ts +55 -0
- package/src/runtime/framework/mailbox/coalescer.ts +83 -0
- package/src/runtime/framework/mailbox/host-notification-sink.ts +44 -0
- package/src/runtime/framework/mailbox/identity.test.ts +38 -0
- package/src/runtime/framework/mailbox/identity.ts +38 -0
- package/src/runtime/framework/mailbox/index.ts +7 -0
- package/src/runtime/framework/mailbox/mailbox-dispatcher.test.ts +513 -0
- package/src/runtime/framework/mailbox/mailbox-dispatcher.ts +282 -0
- package/src/runtime/framework/mailbox/mailbox-repository.ts +44 -0
- package/src/runtime/framework/mailbox/notification-projector.test.ts +77 -0
- package/src/runtime/framework/mailbox/notification-projector.ts +65 -0
- package/src/runtime/framework/mailbox/types.ts +139 -0
- package/src/runtime/framework/modules/runtime-module-loader-hooks.d.mts +19 -0
- package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +117 -0
- package/src/runtime/framework/modules/runtime-module-loader.test.ts +209 -0
- package/src/runtime/framework/modules/runtime-module-loader.ts +71 -0
- package/src/runtime/framework/modules/runtime-module-worker-bootstrap.mjs +23 -0
- package/src/runtime/framework/participation-manager.test.ts +108 -0
- package/src/runtime/framework/participation-manager.ts +244 -0
- package/src/runtime/framework/plugins/contracts.ts +382 -0
- package/src/runtime/framework/plugins/index.ts +4 -0
- package/src/runtime/framework/plugins/runtime-event-identity.ts +17 -0
- package/src/runtime/framework/plugins/runtime-input-journal.test.ts +179 -0
- package/src/runtime/framework/plugins/runtime-input-journal.ts +693 -0
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +1994 -0
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +1860 -0
- package/src/runtime/framework/runtime.ts +1756 -0
- package/src/runtime/framework/serial-task-queue.ts +14 -0
- package/src/runtime/framework/state/contracts.ts +49 -0
- package/src/runtime/framework/state/current-state-store.ts +184 -0
- package/src/runtime/framework/state/durable-agent-state-snapshot.test.ts +144 -0
- package/src/runtime/framework/state/durable-agent-state-snapshot.ts +209 -0
- package/src/runtime/framework/state/freshness.ts +72 -0
- package/src/runtime/framework/state/history/contracts.ts +150 -0
- package/src/runtime/framework/state/history/history-writer.ts +194 -0
- package/src/runtime/framework/state/history/memory-state-history-repository.ts +178 -0
- package/src/runtime/framework/state/history/sqlite-state-history-repository.test.ts +157 -0
- package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +629 -0
- package/src/runtime/framework/state/history/state-history.test.ts +251 -0
- package/src/runtime/framework/state/history/trajectory-projector.ts +209 -0
- package/src/runtime/framework/state/index.ts +13 -0
- package/src/runtime/framework/state/state-core.test.ts +197 -0
- package/src/runtime/framework/state/state-ingress.ts +95 -0
- package/src/runtime/framework/state/state-provider.ts +62 -0
- package/src/runtime/framework/state/types.ts +155 -0
- package/src/runtime/game-sync-protocol.test.ts +123 -0
- package/src/runtime/game-sync-protocol.ts +261 -0
- package/src/runtime/mailbox-host-relay.test.ts +248 -0
- package/src/runtime/mailbox-host-relay.ts +300 -0
- package/src/runtime/observability/factory.ts +188 -0
- package/src/runtime/observability/index.ts +6 -0
- package/src/runtime/observability/observability.test.ts +561 -0
- package/src/runtime/observability/recorder.ts +209 -0
- package/src/runtime/observability/redaction.ts +154 -0
- package/src/runtime/observability/reporter.ts +154 -0
- package/src/runtime/observability/sinks.ts +135 -0
- package/src/runtime/observability/types.ts +92 -0
- package/src/runtime/owner-control.test.ts +153 -0
- package/src/runtime/owner-control.ts +206 -42
- package/src/runtime/parent-process-watch.test.ts +27 -0
- package/src/runtime/parent-process-watch.ts +51 -0
- package/src/runtime/pending-event-handoff.test.ts +29 -0
- package/src/runtime/pending-event-handoff.ts +61 -0
- package/src/runtime/raw-ws-log.test.ts +86 -17
- package/src/runtime/raw-ws-log.ts +230 -21
- package/src/runtime/runtime-logger.test.ts +58 -0
- package/src/runtime/runtime-logger.ts +10 -13
- package/src/runtime/runtime-persistence.test.ts +26 -0
- package/src/runtime/runtime.test.ts +1704 -0
- package/src/runtime/ws-client.test.ts +496 -103
- package/src/runtime/ws-client.ts +546 -166
- package/src/sdk/action.ts +3 -1
- package/src/sdk/index.ts +4 -19
- package/src/sdk/perception-plugin/index.test.ts +22 -0
- package/src/sdk/perception-plugin/index.ts +285 -0
- package/src/sdk/strategy-plugin/index.test.ts +952 -0
- package/src/sdk/strategy-plugin/index.ts +1324 -0
- package/src/sdk/types.ts +3 -1
- package/src/short-command/classifier.test.ts +107 -0
- package/src/short-command/classifier.ts +169 -0
- package/src/short-command/index.ts +7 -0
- package/src/short-command/presenter.test.ts +119 -0
- package/src/short-command/presenter.ts +308 -0
- package/src/short-command/process-io.ts +13 -0
- package/src/short-command/response-contract.ts +58 -0
- package/src/short-command/runner.test.ts +78 -0
- package/src/short-command/runner.ts +164 -0
- package/src/short-command/sensitive-data-filter.test.ts +56 -0
- package/src/short-command/sensitive-data-filter.ts +123 -0
- package/src/short-command/transport-response.test.ts +39 -0
- package/src/short-command/transport-response.ts +89 -0
- package/src/short-command/types.ts +92 -0
- package/src/strategies/avoid-lone.ts +18 -11
- package/src/strategies/avoid-players.ts +29 -13
- package/src/strategies/corpse-patrol.ts +29 -11
- package/src/strategies/crab-sabotage.ts +31 -18
- package/src/strategies/custom-module.test.ts +3 -5
- package/src/strategies/find-player.ts +34 -15
- package/src/strategies/game-utils.test.ts +42 -0
- package/src/strategies/game-utils.ts +143 -53
- package/src/strategies/goals/avoid-lone-top.ts +22 -1
- package/src/strategies/goals/avoid-players-top.test.ts +13 -0
- package/src/strategies/goals/avoid-players-top.ts +6 -5
- package/src/strategies/goals/corpse-patrol-top.ts +9 -2
- package/src/strategies/goals/crab-octopus-reflexes.ts +6 -2
- package/src/strategies/goals/crab-sabotage-top.ts +7 -1
- package/src/strategies/goals/emergency-hunt-goal.ts +15 -3
- package/src/strategies/goals/find-player-top.ts +47 -3
- package/src/strategies/goals/flee-players-goal.ts +2 -0
- package/src/strategies/goals/follow-companion-goal.ts +27 -3
- package/src/strategies/goals/hide-top.ts +47 -5
- package/src/strategies/goals/keep-away-goal.ts +24 -2
- package/src/strategies/goals/kill-frenzy-top.ts +12 -2
- package/src/strategies/goals/kill-lone-top.test.ts +72 -0
- package/src/strategies/goals/kill-lone-top.ts +8 -6
- package/src/strategies/goals/kill-target-goal.ts +16 -7
- package/src/strategies/goals/kill-target-top.ts +16 -2
- package/src/strategies/goals/linger-corpse-goal.ts +11 -3
- package/src/strategies/goals/lone-kill-core.ts +4 -2
- package/src/strategies/goals/move-room-goal.ts +46 -4
- package/src/strategies/goals/normal-shrimp-top.test.ts +16 -0
- package/src/strategies/goals/normal-shrimp-top.ts +18 -18
- package/src/strategies/goals/paradise-fish-top.test.ts +78 -0
- package/src/strategies/goals/paradise-fish-top.ts +16 -6
- package/src/strategies/goals/patrol-top.ts +14 -3
- package/src/strategies/goals/report-patrol-top.ts +14 -3
- package/src/strategies/goals/safe-task-goal.ts +32 -4
- package/src/strategies/goals/social-task-top.ts +39 -8
- package/src/strategies/goals/task-kill-report-top.ts +33 -10
- package/src/strategies/goals/task-only-top.test.ts +89 -0
- package/src/strategies/goals/task-only-top.ts +9 -2
- package/src/strategies/goals/task-or-patrol-goal.ts +21 -3
- package/src/strategies/goals/warrior-shrimp-top.test.ts +75 -23
- package/src/strategies/goals/warrior-shrimp-top.ts +34 -13
- package/src/strategies/greeting.ts +10 -3
- package/src/strategies/hide-spots.ts +4 -0
- package/src/strategies/hide.ts +35 -20
- package/src/strategies/kill-frenzy.ts +18 -12
- package/src/strategies/kill-lone.ts +43 -12
- package/src/strategies/kill-target.ts +34 -17
- package/src/strategies/lone-kill-task.ts +31 -18
- package/src/strategies/move-room.ts +32 -15
- package/src/strategies/off-route-points.ts +12 -4
- package/src/strategies/paradise-fish.ts +49 -14
- package/src/strategies/pathfind/astar-path.test.ts +73 -0
- package/src/strategies/pathfind/astar-path.ts +196 -0
- package/src/strategies/pathfind/escape-planner.test.ts +27 -1
- package/src/strategies/pathfind/escape-planner.ts +108 -0
- package/src/strategies/patrol.ts +18 -11
- package/src/strategies/report-patrol.ts +18 -11
- package/src/strategies/shrimp-memory.ts +66 -14
- package/src/strategies/social-task.ts +49 -45
- package/src/strategies/task-kill-report.ts +34 -16
- package/src/strategies/task-only.test.ts +124 -0
- package/src/strategies/task-only.ts +20 -11
- package/src/strategies/task-report.ts +29 -11
- package/src/strategies/types.ts +29 -62
- package/src/strategies/warrior-memory.ts +44 -14
- package/scripts/check_monitor.test.mjs +0 -212
- package/skills/clawclaw/references/KNOWLEDGE.md +0 -42
- package/src/commands/do.test.ts +0 -117
- package/src/commands/do.ts +0 -195
- package/src/commands/game-monitor-status.test.ts +0 -58
- package/src/commands/game-start-plan.test.ts +0 -356
- package/src/commands/game.ts +0 -2046
- package/src/commands/history-player.test.ts +0 -105
- package/src/commands/history.ts +0 -573
- package/src/commands/knowledge.test.ts +0 -13
- package/src/commands/knowledge.ts +0 -176
- package/src/commands/meeting-history.test.ts +0 -106
- package/src/commands/state.ts +0 -46
- package/src/commands/watch.test.ts +0 -1313
- package/src/commands/watch.ts +0 -824
- package/src/lib/knowledge-store.test.ts +0 -170
- package/src/lib/knowledge-store.ts +0 -369
- package/src/perception/player-history-store.test.ts +0 -177
- package/src/perception/player-history-store.ts +0 -262
- package/src/pipeline/event-store.test.ts +0 -28
- package/src/pipeline/event-store.ts +0 -193
- package/src/pipeline/pipeline.ts +0 -35
- package/src/runtime/event-daemon.test.ts +0 -268
- package/src/runtime/event-daemon.ts +0 -531
- package/src/runtime/room-lobby-runtime.test.ts +0 -95
- package/src/runtime/room-lobby-runtime.ts +0 -367
- package/src/strategies/avoid-players.knowledge.md +0 -19
- package/src/strategies/kill-lone.knowledge.md +0 -17
- package/src/strategies/knowledge-contract.ts +0 -85
- package/src/strategies/loader.test.ts +0 -678
- package/src/strategies/loader.ts +0 -212
- package/src/strategies/new-events-backfill.ts +0 -98
- package/src/strategies/paradise-fish.knowledge.md +0 -19
- package/src/strategies/shrimp-memory.knowledge.md +0 -19
- package/src/strategies/social-task.test.ts +0 -28
- package/src/strategies/spawn.ts +0 -82
- package/src/strategies/strategy-loop.test.ts +0 -15
- package/src/strategies/strategy-loop.ts +0 -748
- package/src/strategies/warrior-memory.knowledge.md +0 -21
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
|
|
3
|
-
import { tmpdir } from 'os';
|
|
4
|
-
import { join } from 'path';
|
|
5
|
-
import {
|
|
6
|
-
buildKnowledgeView,
|
|
7
|
-
KnowledgeStore,
|
|
8
|
-
readKnowledgeFile,
|
|
9
|
-
readKnowledgeFileResult,
|
|
10
|
-
} from './knowledge-store.js';
|
|
11
|
-
|
|
12
|
-
describe('KnowledgeStore', () => {
|
|
13
|
-
let dir: string;
|
|
14
|
-
let path: string;
|
|
15
|
-
let now: Date;
|
|
16
|
-
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
dir = mkdtempSync(join(tmpdir(), 'clawclaw-knowledge-'));
|
|
19
|
-
path = join(dir, 'knowledge.json');
|
|
20
|
-
now = new Date('2026-06-05T00:00:00.000Z');
|
|
21
|
-
vi.useFakeTimers();
|
|
22
|
-
vi.setSystemTime(now);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
vi.useRealTimers();
|
|
27
|
-
rmSync(dir, { recursive: true, force: true });
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
function tick(ms = 1000): void {
|
|
31
|
-
now = new Date(now.getTime() + ms);
|
|
32
|
-
vi.setSystemTime(now);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function store(gameId = 'game-1'): KnowledgeStore {
|
|
36
|
-
return new KnowledgeStore(path, gameId);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function view(playerNamesBySeat: Record<string, string> = {}) {
|
|
40
|
-
return buildKnowledgeView(readKnowledgeFile(path), { playerNamesBySeat });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
it('writes facts and tags under the current game id', () => {
|
|
44
|
-
store().set({
|
|
45
|
-
type: 'player',
|
|
46
|
-
selector: '5',
|
|
47
|
-
key: 'role',
|
|
48
|
-
value: 'impostor',
|
|
49
|
-
tags: ['kill_if_armed'],
|
|
50
|
-
note: 'vent read',
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const file = readKnowledgeFile(path);
|
|
54
|
-
|
|
55
|
-
expect(file?.gameId).toBe('game-1');
|
|
56
|
-
expect(file?.subjects['player:5']?.facts?.role.value).toBe('impostor');
|
|
57
|
-
expect(file?.subjects['player:5']?.tags?.kill_if_armed.note).toBe('vent read');
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('preserves explicit null fact values', () => {
|
|
61
|
-
store().set({
|
|
62
|
-
type: 'player',
|
|
63
|
-
selector: '5',
|
|
64
|
-
key: 'role',
|
|
65
|
-
value: null,
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
expect(readKnowledgeFile(path)?.subjects['player:5']?.facts?.role.value).toBeNull();
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('distinguishes invalid files from missing files', () => {
|
|
72
|
-
expect(readKnowledgeFileResult(path)).toEqual({ status: 'missing' });
|
|
73
|
-
|
|
74
|
-
writeFileSync(path, '{bad json');
|
|
75
|
-
|
|
76
|
-
expect(readKnowledgeFileResult(path).status).toBe('invalid');
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('returns an empty file when loading knowledge from another game', () => {
|
|
80
|
-
store('old-game').set({ type: 'player', selector: '5', tags: ['hostile'] });
|
|
81
|
-
|
|
82
|
-
expect(store('new-game').list()).toEqual({
|
|
83
|
-
version: 1,
|
|
84
|
-
gameId: 'new-game',
|
|
85
|
-
subjects: {},
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it('treats unmarked players as suspected by default', () => {
|
|
90
|
-
const s = store();
|
|
91
|
-
s.set({ type: 'player', selector: '5', tags: ['hostile'] });
|
|
92
|
-
|
|
93
|
-
const v = view({ '5': 'Bob', '9': 'Zed' });
|
|
94
|
-
|
|
95
|
-
// 显式标记的两档
|
|
96
|
-
expect(v.isHostile({ seat: 5 })).toBe(true);
|
|
97
|
-
expect(v.isSuspect({ seat: 5 })).toBe(false);
|
|
98
|
-
// 完全没标记的人 = 被怀疑(markOf 返回 undefined)
|
|
99
|
-
expect(v.markOf({ seat: 9 })).toBeUndefined();
|
|
100
|
-
expect(v.isSuspect({ name: 'Zed' })).toBe(true);
|
|
101
|
-
expect(v.isHostile({ seat: 9 })).toBe(false);
|
|
102
|
-
expect(v.isTrusted({ seat: 9 })).toBe(false);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('aggregates player marks written by seat and by name', () => {
|
|
106
|
-
const s = store();
|
|
107
|
-
s.set({ type: 'player', selector: '5', tags: ['hostile'] });
|
|
108
|
-
|
|
109
|
-
const v = view({ '5': 'Bob' });
|
|
110
|
-
|
|
111
|
-
expect(v.isHostile({ name: 'Bob' })).toBe(true);
|
|
112
|
-
expect(v.markOf({ seat: 5 })).toBe('hostile');
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('uses the latest mark across seat and name selectors', () => {
|
|
116
|
-
const s = store();
|
|
117
|
-
s.set({ type: 'player', selector: '5', tags: ['hostile'] });
|
|
118
|
-
tick();
|
|
119
|
-
s.set({ type: 'player', selector: 'Bob', tags: ['trusted'] });
|
|
120
|
-
|
|
121
|
-
const v = view({ '5': 'Bob' });
|
|
122
|
-
|
|
123
|
-
expect(v.markOf({ name: 'Bob' })).toBe('trusted');
|
|
124
|
-
expect(v.isHostile({ name: 'Bob' })).toBe(false);
|
|
125
|
-
expect(v.isTrusted({ name: 'Bob' })).toBe(true);
|
|
126
|
-
expect(v.hasTag({ seat: 5 }, 'hostile')).toBe(false);
|
|
127
|
-
expect(v.hasTag({ seat: 5 }, 'trusted')).toBe(true);
|
|
128
|
-
expect(v.selectorsWithTag('player', 'hostile')).toEqual([]);
|
|
129
|
-
expect(v.selectorsWithTag('player', 'trusted')).toEqual(['Bob']);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it('keeps role facts independent from strategy marks', () => {
|
|
133
|
-
const s = store();
|
|
134
|
-
s.set({ type: 'player', selector: '5', key: 'role', value: 'impostor' });
|
|
135
|
-
s.set({ type: 'player', selector: '5', tags: ['trusted'] });
|
|
136
|
-
|
|
137
|
-
const v = view();
|
|
138
|
-
|
|
139
|
-
expect(v.roleOf({ seat: 5 })).toBe('impostor');
|
|
140
|
-
expect(v.markOf({ seat: 5 })).toBe('trusted');
|
|
141
|
-
expect(v.isHostile({ seat: 5 })).toBe(false);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('can flip player marks', () => {
|
|
145
|
-
const s = store();
|
|
146
|
-
s.set({ type: 'player', selector: '5', tags: ['trusted'] });
|
|
147
|
-
tick();
|
|
148
|
-
s.set({ type: 'player', selector: 'Bob', tags: ['hostile'] });
|
|
149
|
-
|
|
150
|
-
const v = view({ '5': 'Bob' });
|
|
151
|
-
|
|
152
|
-
expect(v.isHostile({ seat: 5 })).toBe(true);
|
|
153
|
-
expect(v.isTrusted({ seat: 5 })).toBe(false);
|
|
154
|
-
expect(v.selectorsWithTag('player', 'trusted')).toEqual([]);
|
|
155
|
-
expect(v.selectorsWithTag('player', 'hostile')).toEqual(['Bob']);
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
it('maps legacy tags onto the two canonical marks', () => {
|
|
159
|
-
const s = store();
|
|
160
|
-
s.set({ type: 'player', selector: '6', tags: ['kill_if_armed'] });
|
|
161
|
-
s.set({ type: 'player', selector: '7', tags: ['protected'] });
|
|
162
|
-
|
|
163
|
-
const v = view();
|
|
164
|
-
|
|
165
|
-
expect(v.markOf({ seat: 6 })).toBe('hostile');
|
|
166
|
-
expect(v.markOf({ seat: 7 })).toBe('trusted');
|
|
167
|
-
expect(v.hasTag({ seat: 6 }, 'hostile')).toBe(true);
|
|
168
|
-
expect(v.hasTag({ seat: 7 }, 'trusted')).toBe(true);
|
|
169
|
-
});
|
|
170
|
-
});
|
|
@@ -1,369 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 策略知识系统 — 当前对局的「推断快照」存储。
|
|
3
|
-
*
|
|
4
|
-
* 设计要点(见 docs/策略知识系统-by-claude.md):
|
|
5
|
-
* - 跨进程:Agent(短进程 CLI)写,strategy(长进程 loop)按 mtime 轮询读,免重启。
|
|
6
|
-
* - 快照而非日志:单个 JSON 对象,del/clear 直接重写;写入用临时文件 + rename 原子替换。
|
|
7
|
-
* - gameId 作用域:每份知识标记所属对局(= 最新 session 文件名),换局自动失效。
|
|
8
|
-
* - 价值中立:只存推断 + source,对 value 不做价值判断;
|
|
9
|
-
* 确定事实(role/队友/尸体…)走 state/ctx.teammates,不入库。
|
|
10
|
-
* - 玩家两档标记:只显式标 hostile(坏人)/ trusted(好人),未标记的一律默认「被怀疑」,不再有 suspect 标记,也不存置信度。
|
|
11
|
-
*/
|
|
12
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'fs';
|
|
13
|
-
import { basename, dirname, join } from 'path';
|
|
14
|
-
import { AuthStore } from './auth.js';
|
|
15
|
-
import { getProfileStateDir } from './init-command.js';
|
|
16
|
-
import { EventStore } from '../pipeline/event-store.js';
|
|
17
|
-
|
|
18
|
-
export type KnowledgeSource = 'agent' | 'strategy' | 'server';
|
|
19
|
-
/** v1:仅 'game' 生效(整库按 gameId 失效)。'session'/'persistent' 为预留,暂不跨局保留——跨局画像走 memory.md,CLI 也不暴露 --scope。 */
|
|
20
|
-
export type KnowledgeScope = 'game' | 'session' | 'persistent';
|
|
21
|
-
|
|
22
|
-
export interface FactMeta {
|
|
23
|
-
value: unknown;
|
|
24
|
-
note?: string;
|
|
25
|
-
source?: KnowledgeSource;
|
|
26
|
-
ts: number;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface TagMeta {
|
|
30
|
-
note?: string;
|
|
31
|
-
source?: KnowledgeSource;
|
|
32
|
-
ts: number;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** 一个 subject(玩家/房间/任务/全局…)上聚合的事实 + 标签。 */
|
|
36
|
-
export interface SubjectEntry {
|
|
37
|
-
type: string; // 'player' | 'room' | 'task' | 'global' | <自定义>
|
|
38
|
-
selector: string; // player→座位号或名字;room→房名;global→''
|
|
39
|
-
facts?: Record<string, FactMeta>; // 键值事实,如 role: { value: 'impostor' }
|
|
40
|
-
tags?: Record<string, TagMeta>; // 玩家策略主标记:hostile / trusted(未标记默认被怀疑);也支持自定义标签
|
|
41
|
-
scope?: KnowledgeScope;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface KnowledgeFile {
|
|
45
|
-
version: 1;
|
|
46
|
-
gameId?: string;
|
|
47
|
-
subjects: Record<string, SubjectEntry>; // key = `${type}:${selector}`
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export type KnowledgeFileReadResult =
|
|
51
|
-
| { status: 'ok'; file: KnowledgeFile }
|
|
52
|
-
| { status: 'missing' }
|
|
53
|
-
| { status: 'invalid'; error: string };
|
|
54
|
-
|
|
55
|
-
export type PlayerRef = { name?: string; seat?: number };
|
|
56
|
-
export type SubjectRef = { type: string; selector: string };
|
|
57
|
-
export type KnowledgeRef = PlayerRef | SubjectRef;
|
|
58
|
-
/** 玩家显式标记只有两档:hostile(坏人)/ trusted(好人)。未标记 = 被怀疑(markOf 返回 undefined)。 */
|
|
59
|
-
export type PlayerMark = 'hostile' | 'trusted';
|
|
60
|
-
|
|
61
|
-
export interface KnowledgeView {
|
|
62
|
-
/** 当前没有任何知识时为 true。 */
|
|
63
|
-
readonly empty: boolean;
|
|
64
|
-
/** 取一个 subject(精确 type + selector)。 */
|
|
65
|
-
subject(type: string, selector: string): SubjectEntry | undefined;
|
|
66
|
-
/** 取某 subject 的键值事实。player ref 会按座位↔名字归一化解析。 */
|
|
67
|
-
getFact(ref: KnowledgeRef, key: string): FactMeta | undefined;
|
|
68
|
-
/** 该 subject 是否带某标签。玩家两档 mark 及旧别名按最新结论解析。 */
|
|
69
|
-
hasTag(ref: KnowledgeRef, tag: string): boolean;
|
|
70
|
-
/** 按 type / tag / factKey 筛选 subject。玩家 mark 标签同样遵守最新结论。 */
|
|
71
|
-
query(q: { type?: string; tag?: string; factKey?: string }): SubjectEntry[];
|
|
72
|
-
/** 某 type 下带某标签的全部 selector(喂给 matchesAnyTarget 用)。玩家 mark 标签同样过滤旧结论。 */
|
|
73
|
-
selectorsWithTag(type: string, tag: string): string[];
|
|
74
|
-
// —— player 便捷语法糖 ——
|
|
75
|
-
/** 最近一次 role 事实。身份事实与策略 mark 完全解耦。 */
|
|
76
|
-
roleOf(p: PlayerRef): string | undefined;
|
|
77
|
-
/** 最新显式标记;未标记返回 undefined(= 被怀疑)。 */
|
|
78
|
-
markOf(p: PlayerRef): PlayerMark | undefined;
|
|
79
|
-
/** 被怀疑 = 既非 hostile 也非 trusted(含完全未标记者)。 */
|
|
80
|
-
isSuspect(p: PlayerRef): boolean;
|
|
81
|
-
isHostile(p: PlayerRef): boolean;
|
|
82
|
-
isTrusted(p: PlayerRef): boolean;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const PLAYER_MARK_ALIASES: Record<string, PlayerMark> = {
|
|
86
|
-
hostile: 'hostile',
|
|
87
|
-
kill_if_armed: 'hostile',
|
|
88
|
-
trusted: 'trusted',
|
|
89
|
-
cleared: 'trusted',
|
|
90
|
-
protected: 'trusted',
|
|
91
|
-
do_not_kill: 'trusted',
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 两档化后被废弃的旧玩家标记:一律视同未标记(= 被怀疑),不映射到任何 canonical mark
|
|
96
|
-
*(「被怀疑」就是默认态,没有对应标签)。读取侧与写入侧共用这一份清单,避免漂移:
|
|
97
|
-
* - 读取侧(buildKnowledgeView.tagActive):旧数据里的 tags.suspect / tags.avoid 不再被 hasTag / query / selectorsWithTag 命中。
|
|
98
|
-
* - 写入侧(knowledge mark 的 removeTags):改判玩家时顺手删掉残留的 raw tag,否则 knowledge get / subject() 仍会暴露 tags.suspect/avoid 误导人。
|
|
99
|
-
*/
|
|
100
|
-
export const DROPPED_PLAYER_MARK_TAGS = new Set(['suspect', 'avoid']);
|
|
101
|
-
|
|
102
|
-
function nowTs(): number {
|
|
103
|
-
return Date.now();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function subjectKey(type: string, selector: string): string {
|
|
107
|
-
return `${type}:${selector}`;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** 座位↔名字双向解析(比 game-utils.matchesTarget 多了「selector 是名字、ref 只有座位」一向),不依赖 StrategyContext(避免循环)。 */
|
|
111
|
-
function selectorMatchesPlayer(
|
|
112
|
-
selector: string,
|
|
113
|
-
player: PlayerRef,
|
|
114
|
-
playerNamesBySeat: Record<string, string>,
|
|
115
|
-
): boolean {
|
|
116
|
-
const seatStr = selector.trim();
|
|
117
|
-
const normalized = seatStr.toLowerCase();
|
|
118
|
-
if (!normalized) return false;
|
|
119
|
-
if (player.name && player.name.toLowerCase() === normalized) return true;
|
|
120
|
-
if (/^\d+$/.test(seatStr)) {
|
|
121
|
-
// 选择器是座位号
|
|
122
|
-
if (player.seat != null && String(player.seat) === seatStr) return true;
|
|
123
|
-
const nameFromSeat = playerNamesBySeat[seatStr];
|
|
124
|
-
if (nameFromSeat && player.name && player.name.toLowerCase() === nameFromSeat.toLowerCase()) return true;
|
|
125
|
-
} else if (player.seat != null) {
|
|
126
|
-
// 选择器是名字、引用只有座位 → 用座位映射出名字再比
|
|
127
|
-
const nameFromSeat = playerNamesBySeat[String(player.seat)];
|
|
128
|
-
if (nameFromSeat && nameFromSeat.toLowerCase() === normalized) return true;
|
|
129
|
-
}
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function isSubjectRef(ref: KnowledgeRef): ref is SubjectRef {
|
|
134
|
-
return typeof (ref as SubjectRef).type === 'string' && typeof (ref as SubjectRef).selector === 'string';
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/** 当前对局 id = 最新 session 文件名(去扩展名);无对局时 undefined。 */
|
|
138
|
-
export function currentGameId(): string | undefined {
|
|
139
|
-
try {
|
|
140
|
-
const path = EventStore.latestSessionPath();
|
|
141
|
-
return path ? basename(path, '.jsonl') : undefined;
|
|
142
|
-
} catch {
|
|
143
|
-
return undefined;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export function knowledgeFilePathForActiveAccount(): string {
|
|
148
|
-
const profile = new AuthStore().getActive();
|
|
149
|
-
if (!profile) throw new Error('Not logged in.');
|
|
150
|
-
return join(getProfileStateDir(profile), 'knowledge.json');
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export function readKnowledgeFileResult(path: string): KnowledgeFileReadResult {
|
|
154
|
-
if (!existsSync(path)) return { status: 'missing' };
|
|
155
|
-
try {
|
|
156
|
-
const parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
157
|
-
if (parsed && parsed.version === 1 && parsed.subjects && typeof parsed.subjects === 'object') {
|
|
158
|
-
return { status: 'ok', file: parsed as KnowledgeFile };
|
|
159
|
-
}
|
|
160
|
-
return { status: 'invalid', error: 'invalid knowledge file shape' };
|
|
161
|
-
} catch (err) {
|
|
162
|
-
return { status: 'invalid', error: err instanceof Error ? err.message : String(err) };
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/** 读 knowledge.json;不存在 / 解析失败 / 结构不对都返回 null。需要区分错误原因时用 readKnowledgeFileResult。 */
|
|
167
|
-
export function readKnowledgeFile(path: string): KnowledgeFile | null {
|
|
168
|
-
const result = readKnowledgeFileResult(path);
|
|
169
|
-
return result.status === 'ok' ? result.file : null;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** 临时文件 + rename 原子替换,避免读端读到写一半的 JSON。 */
|
|
173
|
-
export function writeKnowledgeFileAtomic(path: string, file: KnowledgeFile): void {
|
|
174
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
175
|
-
const tmp = `${path}.tmp.${process.pid}`;
|
|
176
|
-
writeFileSync(tmp, JSON.stringify(file, null, 2));
|
|
177
|
-
renameSync(tmp, path);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export interface KnowledgeSetInput {
|
|
181
|
-
type: string;
|
|
182
|
-
selector: string;
|
|
183
|
-
key?: string;
|
|
184
|
-
value?: unknown;
|
|
185
|
-
tags?: string[];
|
|
186
|
-
removeTags?: string[];
|
|
187
|
-
note?: string;
|
|
188
|
-
source?: KnowledgeSource;
|
|
189
|
-
scope?: KnowledgeScope;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/** Agent 侧读-改-写句柄;CLI 命令使用。strategy 侧只读,用 buildKnowledgeView。 */
|
|
193
|
-
export class KnowledgeStore {
|
|
194
|
-
constructor(
|
|
195
|
-
private readonly path: string,
|
|
196
|
-
readonly gameId?: string,
|
|
197
|
-
) {}
|
|
198
|
-
|
|
199
|
-
static forActiveAccount(): KnowledgeStore {
|
|
200
|
-
return new KnowledgeStore(knowledgeFilePathForActiveAccount(), currentGameId());
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/** 读当前文件;若不存在或属于别的对局,返回该对局的空文件(旧局知识自动失效)。 */
|
|
204
|
-
load(): KnowledgeFile {
|
|
205
|
-
const file = readKnowledgeFile(this.path);
|
|
206
|
-
if (file && file.gameId === this.gameId) return file;
|
|
207
|
-
return { version: 1, gameId: this.gameId, subjects: {} };
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
private save(file: KnowledgeFile): void {
|
|
211
|
-
writeKnowledgeFileAtomic(this.path, file);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
set(input: KnowledgeSetInput): SubjectEntry {
|
|
215
|
-
if (!input.type || !input.selector) throw new Error('knowledge set requires type and selector');
|
|
216
|
-
const file = this.load();
|
|
217
|
-
const key = subjectKey(input.type, input.selector);
|
|
218
|
-
const entry: SubjectEntry = file.subjects[key] ?? { type: input.type, selector: input.selector };
|
|
219
|
-
const ts = nowTs();
|
|
220
|
-
const source = input.source ?? 'agent';
|
|
221
|
-
|
|
222
|
-
if (input.key != null) {
|
|
223
|
-
entry.facts = entry.facts ?? {};
|
|
224
|
-
entry.facts[input.key] = {
|
|
225
|
-
value: input.value === undefined ? true : input.value,
|
|
226
|
-
note: input.note,
|
|
227
|
-
source,
|
|
228
|
-
ts,
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
if (input.tags && input.tags.length > 0) {
|
|
232
|
-
entry.tags = entry.tags ?? {};
|
|
233
|
-
for (const tag of input.tags) {
|
|
234
|
-
if (!tag) continue;
|
|
235
|
-
entry.tags[tag] = { note: input.note, source, ts };
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
if (input.removeTags && entry.tags) {
|
|
239
|
-
for (const tag of input.removeTags) delete entry.tags[tag];
|
|
240
|
-
}
|
|
241
|
-
if (input.scope) entry.scope = input.scope;
|
|
242
|
-
|
|
243
|
-
file.subjects[key] = entry;
|
|
244
|
-
this.save(file);
|
|
245
|
-
return entry;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/** 删整个 subject,或仅删某个 fact / tag;删空后自动清除该 subject。 */
|
|
249
|
-
del(type: string, selector: string, opts?: { key?: string; tag?: string }): boolean {
|
|
250
|
-
const file = this.load();
|
|
251
|
-
const key = subjectKey(type, selector);
|
|
252
|
-
const entry = file.subjects[key];
|
|
253
|
-
if (!entry) return false;
|
|
254
|
-
|
|
255
|
-
if (!opts?.key && !opts?.tag) {
|
|
256
|
-
delete file.subjects[key];
|
|
257
|
-
this.save(file);
|
|
258
|
-
return true;
|
|
259
|
-
}
|
|
260
|
-
if (opts.key && entry.facts) delete entry.facts[opts.key];
|
|
261
|
-
if (opts.tag && entry.tags) delete entry.tags[opts.tag];
|
|
262
|
-
const noFacts = !entry.facts || Object.keys(entry.facts).length === 0;
|
|
263
|
-
const noTags = !entry.tags || Object.keys(entry.tags).length === 0;
|
|
264
|
-
if (noFacts && noTags) delete file.subjects[key];
|
|
265
|
-
this.save(file);
|
|
266
|
-
return true;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
clear(): void {
|
|
270
|
-
this.save({ version: 1, gameId: this.gameId, subjects: {} });
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
list(): KnowledgeFile {
|
|
274
|
-
return this.load();
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/** 由 strategy-loop 每次重载时构造。playerNamesBySeat 传入活引用即可,座位解析始终最新。 */
|
|
279
|
-
export function buildKnowledgeView(
|
|
280
|
-
file: KnowledgeFile | null,
|
|
281
|
-
opts: { playerNamesBySeat?: Record<string, string> } = {},
|
|
282
|
-
): KnowledgeView {
|
|
283
|
-
const subjects = file?.subjects ?? {};
|
|
284
|
-
const namesBySeat = opts.playerNamesBySeat ?? {};
|
|
285
|
-
|
|
286
|
-
const resolveAll = (ref: KnowledgeRef): SubjectEntry[] => {
|
|
287
|
-
if (isSubjectRef(ref)) {
|
|
288
|
-
const s = subjects[subjectKey(ref.type, ref.selector)];
|
|
289
|
-
return s ? [s] : [];
|
|
290
|
-
}
|
|
291
|
-
return Object.values(subjects).filter((s) => s.type === 'player' && selectorMatchesPlayer(s.selector, ref, namesBySeat));
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
const latestFact = (ref: KnowledgeRef, key: string): FactMeta | undefined => {
|
|
295
|
-
let best: FactMeta | undefined;
|
|
296
|
-
for (const s of resolveAll(ref)) {
|
|
297
|
-
const f = s.facts?.[key];
|
|
298
|
-
if (f && (!best || f.ts > best.ts)) best = f;
|
|
299
|
-
}
|
|
300
|
-
return best;
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
type MarkVerdict = { kind: PlayerMark; ts: number };
|
|
304
|
-
// 同一时刻打了冲突标记时,trusted 优先级高于 hostile(好人判断更慎重,不轻易被覆盖)。
|
|
305
|
-
const markRank = (kind: PlayerMark): number => kind === 'trusted' ? 2 : 1;
|
|
306
|
-
const latestMark = (ref: KnowledgeRef): MarkVerdict | undefined => {
|
|
307
|
-
let best: MarkVerdict | undefined;
|
|
308
|
-
const take = (v: MarkVerdict) => {
|
|
309
|
-
if (!best
|
|
310
|
-
|| v.ts > best.ts
|
|
311
|
-
|| (v.ts === best.ts && markRank(v.kind) > markRank(best.kind))) {
|
|
312
|
-
best = v;
|
|
313
|
-
}
|
|
314
|
-
};
|
|
315
|
-
for (const s of resolveAll(ref)) {
|
|
316
|
-
for (const [tag, meta] of Object.entries(s.tags ?? {})) {
|
|
317
|
-
const kind = PLAYER_MARK_ALIASES[tag];
|
|
318
|
-
if (kind) take({ kind, ts: meta.ts });
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
return best;
|
|
322
|
-
};
|
|
323
|
-
|
|
324
|
-
const markKindForTag = (tag: string): PlayerMark | undefined => PLAYER_MARK_ALIASES[tag];
|
|
325
|
-
const tagActive = (s: SubjectEntry, tag: string): boolean => {
|
|
326
|
-
if (s.type === 'player') {
|
|
327
|
-
const requestedMark = markKindForTag(tag);
|
|
328
|
-
if (requestedMark) {
|
|
329
|
-
const carriesRequestedMark = Object.keys(s.tags ?? {}).some(candidate => markKindForTag(candidate) === requestedMark);
|
|
330
|
-
if (!carriesRequestedMark) return false;
|
|
331
|
-
const ref: PlayerRef = /^\d+$/.test(s.selector.trim()) ? { seat: Number(s.selector) } : { name: s.selector };
|
|
332
|
-
const mark = latestMark(ref);
|
|
333
|
-
return !!mark && mark.kind === requestedMark;
|
|
334
|
-
}
|
|
335
|
-
// 废弃的旧玩家标记(suspect/avoid)= 未标记 = 被怀疑,绝不命中;其余自定义标签(如 paradise_fish)照常按原值判定。
|
|
336
|
-
if (DROPPED_PLAYER_MARK_TAGS.has(tag)) return false;
|
|
337
|
-
}
|
|
338
|
-
return !!s.tags?.[tag];
|
|
339
|
-
};
|
|
340
|
-
|
|
341
|
-
return {
|
|
342
|
-
empty: Object.keys(subjects).length === 0,
|
|
343
|
-
subject: (type, selector) => subjects[subjectKey(type, selector)],
|
|
344
|
-
getFact: (ref, key) => latestFact(ref, key),
|
|
345
|
-
hasTag: (ref, tag) => resolveAll(ref).some((s) => tagActive(s, tag)),
|
|
346
|
-
query: (q) => Object.values(subjects).filter((s) => {
|
|
347
|
-
if (q.type && s.type !== q.type) return false;
|
|
348
|
-
if (q.factKey && !s.facts?.[q.factKey]) return false;
|
|
349
|
-
if (q.tag && !tagActive(s, q.tag)) return false;
|
|
350
|
-
return true;
|
|
351
|
-
}),
|
|
352
|
-
selectorsWithTag: (type, tag) => Object.values(subjects)
|
|
353
|
-
.filter((s) => s.type === type && tagActive(s, tag))
|
|
354
|
-
.map((s) => s.selector),
|
|
355
|
-
roleOf: (p) => {
|
|
356
|
-
const fact = latestFact(p, 'role');
|
|
357
|
-
return typeof fact?.value === 'string' ? fact.value : undefined;
|
|
358
|
-
},
|
|
359
|
-
markOf: (p) => latestMark(p)?.kind,
|
|
360
|
-
// 未标记 hostile/trusted 的玩家一律「被怀疑」——这就是「除好人坏人外剩下全是被怀疑」的落点。
|
|
361
|
-
isSuspect: (p) => !latestMark(p),
|
|
362
|
-
isHostile: (p) => latestMark(p)?.kind === 'hostile',
|
|
363
|
-
isTrusted: (p) => latestMark(p)?.kind === 'trusted',
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
export function emptyKnowledgeView(): KnowledgeView {
|
|
368
|
-
return buildKnowledgeView(null);
|
|
369
|
-
}
|