@myclaw163/clawclaw-cli 0.7.15 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (335) hide show
  1. package/QUICKSTART_HERMES.md +1 -1
  2. package/QUICKSTART_OPENCLAW.md +3 -11
  3. package/QUICKSTART_OTHER.md +8 -8
  4. package/README.md +18 -9
  5. package/bin/clawclaw-channel.mjs +13 -0
  6. package/docs/configuration.md +86 -33
  7. package/docs/custom-modules/README.md +148 -0
  8. package/docs/custom-modules/api-reference.md +767 -0
  9. package/docs/custom-modules/behavior.md +318 -0
  10. package/docs/custom-modules/perception.md +305 -0
  11. package/docs/custom-modules/trigger.md +292 -0
  12. package/docs/desktop-event-push.md +6 -4
  13. package/docs/runtime/README.md +28 -10
  14. package/docs/runtime/agent-notifications.md +48 -29
  15. package/docs/runtime/behavior-runtime.md +139 -0
  16. package/docs/runtime/components-and-boundaries.md +89 -173
  17. package/docs/runtime/events-and-state.md +55 -66
  18. package/docs/runtime/official-perception-modules.md +17 -9
  19. package/docs/runtime/owner-lifecycle-and-observability.md +62 -39
  20. package/docs/runtime/perception-replay.md +6 -6
  21. package/docs/runtime/short-commands-and-capabilities.md +35 -37
  22. package/package.json +7 -4
  23. package/scripts/check-skill-command-surface.mjs +54 -10
  24. package/scripts/compare-perception-replays.test.ts +10 -10
  25. package/scripts/compare-perception-replays.ts +14 -10
  26. package/scripts/replay-perception.ts +3 -3
  27. package/scripts/watch-perception-events.test.ts +1 -1
  28. package/skills/clawclaw/SKILL.md +97 -59
  29. package/skills/clawclaw/references/BEHAVIORS.md +154 -0
  30. package/skills/clawclaw/references/CHANNELS.md +115 -0
  31. package/skills/clawclaw/references/CHATTERBOX.md +3 -2
  32. package/skills/clawclaw/references/COMMANDS.md +108 -58
  33. package/skills/clawclaw/references/GAME-MECHANICS.md +4 -3
  34. package/skills/clawclaw/references/HUB.md +27 -23
  35. package/skills/clawclaw/references/STREAM.md +18 -14
  36. package/skills/clawclaw/references/TACTICS.md +2 -2
  37. package/src/behaviors/official-finite/do-task.ts +141 -0
  38. package/src/behaviors/official-finite/evade.ts +78 -0
  39. package/src/behaviors/official-finite/find-player.ts +50 -0
  40. package/src/behaviors/official-finite/follow-player.ts +70 -0
  41. package/src/behaviors/official-finite/go-to.ts +69 -0
  42. package/src/behaviors/official-finite/hide-temporarily.ts +77 -0
  43. package/src/behaviors/official-finite/hold-position.ts +48 -0
  44. package/src/behaviors/official-finite/hunt-player.ts +47 -0
  45. package/src/behaviors/official-finite/index.test.ts +202 -0
  46. package/src/behaviors/official-finite/index.ts +47 -0
  47. package/src/behaviors/official-finite/inspect-corpse.ts +64 -0
  48. package/src/behaviors/official-finite/patrol-area.ts +80 -0
  49. package/src/behaviors/official-finite/report-corpse.ts +73 -0
  50. package/src/behaviors/official-finite/seek-kill-opportunity.ts +152 -0
  51. package/src/behaviors/official-finite/shared.ts +27 -0
  52. package/src/behaviors/official-finite/trigger-alarm.ts +53 -0
  53. package/src/channel/claude-channel-ipc-server.ts +295 -0
  54. package/src/channel/claude-channel-ipc.ts +152 -0
  55. package/src/channel/claude-channel-server.test.ts +202 -0
  56. package/src/channel/claude-channel-server.ts +221 -0
  57. package/src/cli.ts +4 -2
  58. package/src/commands/account.test.ts +17 -17
  59. package/src/commands/behavior.test.ts +158 -0
  60. package/src/commands/behavior.ts +443 -0
  61. package/src/commands/communication.test.ts +12 -0
  62. package/src/commands/communication.ts +6 -3
  63. package/src/commands/data.test.ts +1 -1
  64. package/src/commands/data.ts +1 -1
  65. package/src/commands/events-canonical-state.test.ts +221 -2
  66. package/src/commands/events.ts +205 -12
  67. package/src/commands/game/index.test.ts +172 -3
  68. package/src/commands/game/index.ts +69 -32
  69. package/src/commands/hub.test.ts +25 -14
  70. package/src/commands/hub.ts +15 -19
  71. package/src/commands/load.test.ts +228 -6
  72. package/src/commands/load.ts +102 -18
  73. package/src/commands/memory.ts +1 -1
  74. package/src/commands/peek.ts +1 -1
  75. package/src/commands/perception.test.ts +30 -1
  76. package/src/commands/perception.ts +15 -17
  77. package/src/commands/trigger.test.ts +140 -0
  78. package/src/commands/trigger.ts +314 -0
  79. package/src/lib/account-guidance.test.ts +4 -0
  80. package/src/lib/account-guidance.ts +1 -1
  81. package/src/lib/behavior-export.test.ts +282 -0
  82. package/src/lib/behavior-export.ts +514 -0
  83. package/src/lib/game-client.test.ts +4 -17
  84. package/src/lib/game-client.ts +3 -0
  85. package/src/lib/gameplay-config.test.ts +258 -37
  86. package/src/lib/gameplay-config.ts +175 -35
  87. package/src/lib/hub-install.test.ts +19 -41
  88. package/src/lib/hub-install.ts +10 -38
  89. package/src/lib/hub-reminder.ts +7 -9
  90. package/src/lib/init-command.ts +1 -1
  91. package/src/lib/official-resource-sync.test.ts +199 -0
  92. package/src/lib/official-resource-sync.ts +221 -0
  93. package/src/lib/perception-config.test.ts +9 -4
  94. package/src/lib/perception-config.ts +10 -13
  95. package/src/lib/perception-export.test.ts +145 -0
  96. package/src/lib/perception-export.ts +178 -0
  97. package/src/lib/trigger-config.test.ts +102 -0
  98. package/src/lib/trigger-config.ts +217 -0
  99. package/src/lib/user-data.test.ts +23 -6
  100. package/src/lib/user-data.ts +15 -123
  101. package/src/pipeline/canonical-event-presentation.test.ts +5 -5
  102. package/src/pipeline/canonical-event-presentation.ts +13 -5
  103. package/src/pipeline/event-format.test.ts +106 -3
  104. package/src/pipeline/event-format.ts +172 -4
  105. package/src/pipeline/event-hints.test.ts +57 -4
  106. package/src/pipeline/event-hints.ts +34 -8
  107. package/src/runtime/canonical-monitor.test.ts +2 -0
  108. package/src/runtime/canonical-monitor.ts +6 -2
  109. package/src/runtime/clawclaw/agent-event-policy.test.ts +23 -2
  110. package/src/runtime/clawclaw/agent-event-policy.ts +12 -5
  111. package/src/runtime/clawclaw/behavior-decision.test.ts +29 -0
  112. package/src/runtime/clawclaw/behavior-decision.ts +82 -0
  113. package/src/runtime/clawclaw/behavior-input-fields.test.ts +175 -0
  114. package/src/runtime/clawclaw/behavior-input-fields.ts +270 -0
  115. package/src/runtime/clawclaw/behavior-input.test.ts +151 -0
  116. package/src/runtime/clawclaw/behavior-input.ts +70 -0
  117. package/src/runtime/clawclaw/behavior-observation-memory.test.ts +59 -0
  118. package/src/runtime/clawclaw/behavior-observation-memory.ts +142 -0
  119. package/src/runtime/clawclaw/behavior-read-api.test.ts +123 -0
  120. package/src/runtime/clawclaw/behavior-read-api.ts +158 -0
  121. package/src/runtime/clawclaw/{plugins/perception/visual-state-projector.ts → observation-frame-projector.ts} +44 -25
  122. package/src/runtime/clawclaw/owner.test.ts +187 -0
  123. package/src/runtime/clawclaw/owner.ts +431 -69
  124. package/src/runtime/clawclaw/plugins/action/action-plugin.test.ts +304 -0
  125. package/src/runtime/clawclaw/plugins/action/action-plugin.ts +286 -41
  126. package/src/runtime/clawclaw/plugins/action/automated-action-normalization.ts +82 -0
  127. package/src/runtime/clawclaw/plugins/assembly.test.ts +135 -29
  128. package/src/runtime/clawclaw/plugins/assembly.ts +54 -28
  129. package/src/runtime/clawclaw/plugins/behavior/activation-manager.test.ts +180 -0
  130. package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +468 -0
  131. package/src/runtime/clawclaw/plugins/behavior/behavior-action.test.ts +40 -0
  132. package/src/runtime/clawclaw/plugins/behavior/behavior-action.ts +30 -0
  133. package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +152 -0
  134. package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.test.ts +156 -0
  135. package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +104 -0
  136. package/src/runtime/clawclaw/plugins/behavior/behavior-manifest.ts +173 -0
  137. package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.test.ts +182 -0
  138. package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.ts +223 -0
  139. package/src/runtime/clawclaw/plugins/behavior/behavior-normalization.ts +157 -0
  140. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +848 -0
  141. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +1347 -0
  142. package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +212 -0
  143. package/src/runtime/clawclaw/plugins/behavior/behavior-source.test.ts +174 -0
  144. package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +621 -0
  145. package/src/runtime/clawclaw/plugins/behavior/control-driver.test.ts +195 -0
  146. package/src/runtime/clawclaw/plugins/behavior/control-driver.ts +180 -0
  147. package/src/runtime/clawclaw/plugins/behavior/index.ts +11 -0
  148. package/src/runtime/clawclaw/plugins/behavior/official-goal-behavior.ts +766 -0
  149. package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.test.ts +283 -0
  150. package/src/runtime/clawclaw/plugins/{strategy/thread-strategy-executor.ts → behavior/thread-behavior-executor.ts} +109 -121
  151. package/src/runtime/clawclaw/plugins/behavior-input-source.test.ts +80 -0
  152. package/src/runtime/clawclaw/plugins/behavior-input-source.ts +247 -0
  153. package/src/runtime/clawclaw/plugins/{strategy → game-query}/context-capabilities.ts +1 -1
  154. package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +13 -10
  155. package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +26 -14
  156. package/src/runtime/clawclaw/plugins/game-query/index.ts +1 -0
  157. package/src/runtime/clawclaw/plugins/index.ts +3 -1
  158. package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.test.ts +239 -0
  159. package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +100 -0
  160. package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-socket.ts +346 -0
  161. package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +230 -12
  162. package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +118 -90
  163. package/src/runtime/clawclaw/plugins/mailbox/consumer-ids.ts +5 -0
  164. package/src/runtime/clawclaw/plugins/mailbox/index.ts +3 -0
  165. package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +1 -1
  166. package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +4 -0
  167. package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.test.ts +87 -0
  168. package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +70 -12
  169. package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +94 -11
  170. package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +1229 -345
  171. package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +479 -35
  172. package/src/runtime/clawclaw/plugins/perception/contracts.ts +4 -3
  173. package/src/runtime/clawclaw/plugins/perception/index.ts +1 -1
  174. package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +23 -13
  175. package/src/runtime/clawclaw/plugins/perception/module-runner.ts +11 -9
  176. package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +39 -38
  177. package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.test.ts +92 -0
  178. package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.ts +248 -0
  179. package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +39 -37
  180. package/src/runtime/clawclaw/plugins/perception/modules/index.ts +1 -0
  181. package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +22 -21
  182. package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +15 -9
  183. package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +3 -3
  184. package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +33 -18
  185. package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +6 -7
  186. package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +22 -16
  187. package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +6 -7
  188. package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +18 -12
  189. package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +14 -10
  190. package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +20 -14
  191. package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +5 -6
  192. package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +5 -6
  193. package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +49 -34
  194. package/src/runtime/clawclaw/plugins/perception/official-module-ids.ts +1 -0
  195. package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +126 -20
  196. package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.ts +24 -3
  197. package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +59 -46
  198. package/src/runtime/clawclaw/plugins/perception/perception-module-loader.ts +27 -5
  199. package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +381 -49
  200. package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +267 -245
  201. package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +98 -0
  202. package/src/runtime/clawclaw/plugins/perception/perception-source.ts +89 -16
  203. package/src/runtime/clawclaw/plugins/perception/perception-test-fixtures.ts +34 -0
  204. package/src/runtime/clawclaw/plugins/perception/replay.test.ts +27 -15
  205. package/src/runtime/clawclaw/plugins/perception/replay.ts +10 -5
  206. package/src/runtime/clawclaw/plugins/perception/shared/math.ts +1 -107
  207. package/src/runtime/clawclaw/plugins/perception/spatial-facts.ts +7 -10
  208. package/src/runtime/clawclaw/plugins/plugin-api-version.ts +112 -0
  209. package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +389 -559
  210. package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +56 -5
  211. package/src/runtime/clawclaw/plugins/source/player-change-tracker.ts +135 -26
  212. package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +249 -16
  213. package/src/runtime/clawclaw/plugins/source/source-plugin.ts +99 -2
  214. package/src/runtime/clawclaw/plugins/source/task-change-tracker.test.ts +212 -0
  215. package/src/runtime/clawclaw/plugins/source/task-change-tracker.ts +259 -0
  216. package/src/runtime/clawclaw/plugins/trigger/contracts.ts +97 -0
  217. package/src/runtime/clawclaw/plugins/trigger/index.ts +4 -0
  218. package/src/runtime/clawclaw/plugins/trigger/trigger-module-loader.ts +118 -0
  219. package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.test.ts +548 -0
  220. package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +655 -0
  221. package/src/runtime/clawclaw/plugins/trigger/trigger-source.test.ts +169 -0
  222. package/src/runtime/clawclaw/plugins/trigger/trigger-source.ts +457 -0
  223. package/src/runtime/clawclaw/runtime.ts +8 -0
  224. package/src/runtime/clawclaw/spatial-api.ts +76 -0
  225. package/src/runtime/clawclaw-event-catalog.test.ts +46 -1
  226. package/src/runtime/clawclaw-event-catalog.ts +40 -3
  227. package/src/runtime/framework/capability-registry.test.ts +196 -55
  228. package/src/runtime/framework/capability-registry.ts +99 -17
  229. package/src/runtime/framework/contracts.ts +30 -4
  230. package/src/runtime/framework/control/control-scheduler.ts +13 -5
  231. package/src/runtime/framework/control/types.ts +11 -4
  232. package/src/runtime/framework/control-coordinator.test.ts +95 -80
  233. package/src/runtime/framework/events/event-delivery-service.ts +4 -2
  234. package/src/runtime/framework/events/event-ingress.test.ts +25 -0
  235. package/src/runtime/framework/events/event-ingress.ts +2 -1
  236. package/src/runtime/framework/events/event-repository.ts +24 -2
  237. package/src/runtime/framework/events/mailbox-event-repositories.test.ts +124 -8
  238. package/src/runtime/framework/events/memory-event-repository.ts +92 -28
  239. package/src/runtime/framework/events/sqlite-event-repository.test.ts +604 -2
  240. package/src/runtime/framework/events/sqlite-event-repository.ts +531 -21
  241. package/src/runtime/framework/events/types.ts +24 -8
  242. package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +38 -1
  243. package/src/runtime/framework/modules/runtime-module-loader.test.ts +233 -7
  244. package/src/runtime/framework/modules/runtime-module-loader.ts +69 -4
  245. package/src/runtime/framework/plugins/contracts.ts +12 -18
  246. package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +81 -71
  247. package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +38 -20
  248. package/src/runtime/framework/runtime.ts +20 -23
  249. package/src/runtime/host-monitor-rate-gate.test.ts +73 -0
  250. package/src/runtime/host-monitor-rate-gate.ts +123 -0
  251. package/src/runtime/mailbox-host-relay.test.ts +45 -0
  252. package/src/runtime/mailbox-host-relay.ts +25 -0
  253. package/src/runtime/observability/factory.ts +4 -4
  254. package/src/runtime/observability/observability.test.ts +25 -3
  255. package/src/runtime/observability/reporter.ts +1 -1
  256. package/src/runtime/observability/types.ts +2 -2
  257. package/src/runtime/owner-control.ts +10 -2
  258. package/src/runtime/runtime-logger.test.ts +1 -1
  259. package/src/runtime/runtime-logger.ts +4 -6
  260. package/src/runtime/runtime.test.ts +88 -22
  261. package/src/sdk/action.ts +2 -2
  262. package/src/sdk/behavior-plugin/index.test.ts +74 -0
  263. package/src/sdk/behavior-plugin/index.ts +335 -0
  264. package/src/sdk/behavior-toolkit/corpse-search.ts +87 -0
  265. package/src/sdk/behavior-toolkit/escape.ts +40 -0
  266. package/src/sdk/behavior-toolkit/index.ts +7 -0
  267. package/src/sdk/behavior-toolkit/kill-flow.ts +299 -0
  268. package/src/sdk/behavior-toolkit/model.ts +263 -0
  269. package/src/sdk/behavior-toolkit/move-gate.ts +52 -0
  270. package/src/sdk/behavior-toolkit/patrol.ts +53 -0
  271. package/src/sdk/behavior-toolkit/search.ts +102 -0
  272. package/src/sdk/index.test.ts +45 -0
  273. package/src/sdk/index.ts +67 -73
  274. package/src/sdk/module-input/index.test.ts +49 -0
  275. package/src/sdk/module-input/index.ts +229 -0
  276. package/src/sdk/perception-plugin/index.test.ts +2 -2
  277. package/src/sdk/perception-plugin/index.ts +51 -142
  278. package/src/sdk/perception-plugin/math.ts +107 -0
  279. package/src/sdk/trigger-plugin/index.test.ts +41 -0
  280. package/src/sdk/trigger-plugin/index.ts +128 -0
  281. package/src/strategies/avoid-lone.ts +21 -10
  282. package/src/strategies/avoid-players.ts +26 -16
  283. package/src/strategies/corpse-patrol.ts +25 -14
  284. package/src/strategies/crab-sabotage.ts +20 -9
  285. package/src/strategies/find-player.ts +33 -21
  286. package/src/strategies/game-utils.ts +1 -1
  287. package/src/strategies/goals/crab-sabotage-top.test.ts +145 -0
  288. package/src/strategies/goals/crab-sabotage-top.ts +32 -69
  289. package/src/strategies/hide.ts +25 -15
  290. package/src/strategies/kill-frenzy.ts +21 -10
  291. package/src/strategies/kill-lone.ts +27 -17
  292. package/src/strategies/kill-target.ts +33 -22
  293. package/src/strategies/lone-kill-task.ts +20 -9
  294. package/src/strategies/move-room.ts +29 -18
  295. package/src/strategies/official-long-behavior.test.ts +89 -0
  296. package/src/strategies/paradise-fish.ts +29 -18
  297. package/src/strategies/pathfind/astar-path.test.ts +127 -2
  298. package/src/strategies/pathfind/astar-path.ts +136 -26
  299. package/src/strategies/patrol.ts +21 -10
  300. package/src/strategies/player-targets.test.ts +10 -0
  301. package/src/strategies/player-targets.ts +13 -0
  302. package/src/strategies/report-patrol.ts +21 -10
  303. package/src/strategies/shrimp-memory.ts +30 -19
  304. package/src/strategies/social-task.ts +34 -23
  305. package/src/strategies/task-kill-report.ts +33 -22
  306. package/src/strategies/task-only.test.ts +99 -85
  307. package/src/strategies/task-only.ts +23 -12
  308. package/src/strategies/task-report.ts +25 -14
  309. package/src/strategies/types.ts +8 -8
  310. package/src/strategies/warrior-memory.ts +29 -19
  311. package/docs/runtime/strategy-runtime.md +0 -111
  312. package/docs//350/207/252/345/256/232/344/271/211/346/204/237/347/237/245.md +0 -502
  313. package/docs//350/207/252/345/256/232/344/271/211/347/255/226/347/225/245.md +0 -395
  314. package/skills/clawclaw/references/STRATEGIES.md +0 -76
  315. package/src/commands/strategy.test.ts +0 -209
  316. package/src/commands/strategy.ts +0 -242
  317. package/src/lib/strategy-export.test.ts +0 -406
  318. package/src/lib/strategy-export.ts +0 -381
  319. package/src/runtime/clawclaw/plugins/perception/official-modules.ts +0 -31
  320. package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.test.ts +0 -177
  321. package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.ts +0 -191
  322. package/src/runtime/clawclaw/plugins/strategy/index.ts +0 -8
  323. package/src/runtime/clawclaw/plugins/strategy/local-strategy-executor.ts +0 -412
  324. package/src/runtime/clawclaw/plugins/strategy/strategy-action.ts +0 -80
  325. package/src/runtime/clawclaw/plugins/strategy/strategy-analysis-thread-host.ts +0 -183
  326. package/src/runtime/clawclaw/plugins/strategy/strategy-executor-types.ts +0 -29
  327. package/src/runtime/clawclaw/plugins/strategy/strategy-executor.test.ts +0 -693
  328. package/src/runtime/clawclaw/plugins/strategy/strategy-game-api.ts +0 -57
  329. package/src/runtime/clawclaw/plugins/strategy/strategy-module-loader.ts +0 -168
  330. package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.test.ts +0 -1994
  331. package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +0 -1579
  332. package/src/runtime/clawclaw/plugins/strategy/strategy-source.test.ts +0 -153
  333. package/src/runtime/clawclaw/plugins/strategy/strategy-source.ts +0 -443
  334. package/src/sdk/strategy-plugin/index.test.ts +0 -958
  335. package/src/sdk/strategy-plugin/index.ts +0 -1350
@@ -1,502 +0,0 @@
1
- # ClawClaw 自定义感知开发指南
2
-
3
- 自定义感知是由 CCL 直接加载的受信任 TypeScript/JavaScript 类。短小、有状态的算法可以在主线程按顺序接收视觉帧;耗时算法可以把无状态分析与有序状态提交拆开,让不同帧在自适应 Worker Thread 池中并行计算。两种写法都把“进入视野、持续移动、停留、跟随”等判断输出为独立的 `visual.*` Event。感知代码不应连接后端、读取 Runtime/SQLite 私有对象或直接给 Agent 拼整段文字;Runtime 仍只调度一个 Perception Plugin,Mailbox 负责决定 Event 是否进入 Monitor 或 `ccl events`。
4
-
5
- ```mermaid
6
- flowchart LR
7
- Runtime["Runtime epoch"] --> Perception["ClawclawPerceptionPlugin"]
8
- Perception --> Projection["当前 Visual State 投影"]
9
- Projection --> Serial["ordered update:主线程持久实例"]
10
- Projection --> Batch["parallel analysis:本帧全部模块一个 batch"]
11
- Batch --> Threads["预热 2、按压力扩到 10 的 Worker Thread 池"]
12
- Threads --> Reducers["主线程持久 reducer,按 frameSeq 提交"]
13
- Serial --> Merge["按 module ID 稳定汇总"]
14
- Reducers --> Merge
15
- Merge --> Result["一份 Perception Plugin 结果"]
16
- ```
17
-
18
- ## 放置和启用
19
-
20
- 把 `.ts` 或 `.js` 入口文件放入用户感知目录:
21
-
22
- | 系统 | 默认目录 |
23
- |---|---|
24
- | macOS/Linux | `~/.clawclaw/user-data/perceptions/` |
25
- | Windows | `%APPDATA%\clawclaw\user-data\perceptions\` |
26
-
27
- 也可以用 `CLAWCLAW_WORKSPACE_DIR` 或全局 `--workspace-dir` 改变 workspace。CCL 只把目录顶层、默认导出 `ClawclawPerception` 或 `ClawclawParallelPerception` 子类的文件识别为入口;入口可以相对导入辅助文件。CCL 先静态读取 manifest,再在 Runtime 启动时直接 import 所有已启用的普通模块;并行模块由每个 Worker 在自身启动时 import。逐帧调用不会重新 import 或打包源码,但队列压力触发扩容时会创建新的 Worker。
28
-
29
- 入口只要在目录中且没有被禁用就会运行。模块开关统一放在 `<workspace>/user-data/gameplay.json`:
30
-
31
- ```json
32
- {
33
- "disabled_perception_modules": ["visual.custom_linger"]
34
- }
35
- ```
36
-
37
- 并行感知的 Worker Thread 使用进程内 TypeScript transform,不启动 `tsx` / `esbuild` 子进程。`.ts` 源码必须把纯类型依赖写成 `import type`,且不能使用 JSX;每个线程在启动时(初始预热或压力扩容)按普通 ESM `import()` 一次模块。
38
-
39
- 七个官方模块和目录中的自定义模块遵循同一规则。disable 表里保留一个已经不存在的旧 ID 会被忽略,不报错;不需要维护一份容易漏掉新模块的 enabled 列表。Event 投递方式和追帧窗口仍属于根 `<workspace>/config.json`:
40
-
41
- ```json
42
- {
43
- "perception": { "maxBackfillVisualFrames": 10 },
44
- "events": {
45
- "delivery": {
46
- "visual.player_entered": "monitor_and_events",
47
- "visual.custom_linger": "events_only"
48
- }
49
- }
50
- }
51
- ```
52
-
53
- 旧的 `config.json perception.modules` 已被拒绝,不能再用它选择模块。`events.delivery` 不控制模块是否计算,只控制已生成 Event 的投递:`monitor_and_events` 同时进入 Monitor 和 `ccl events`,`events_only` 只进入 `ccl events`,`internal_only` 只保存在 Canonical Event Ledger。未配置的 `visual.*` Event 默认为 `events_only`。这些配置在 CCL 长进程启动时读取并冻结。
54
-
55
- ## 作者只写一个类
56
-
57
- 普通入口默认导出一个继承 `ClawclawPerception` 的类。CCL 在主线程直接 import 并创建一个持久实例,上一帧 `update()` 完成后才进入下一帧。作者不需要写循环、创建 Worker、连接 WebSocket 或打开数据库。
58
-
59
- ```js
60
- import { ClawclawPerception } from '@myclaw163/clawclaw-cli/perception-plugin';
61
-
62
- export default class EnterCounter extends ClawclawPerception {
63
- static manifest = {
64
- id: 'visual.enter_counter',
65
- name: 'Enter counter',
66
- description: 'Counts other players entering the field of view.',
67
- subjectScope: 'others',
68
- produces: ['visual.enter_count_changed'],
69
- eventMetadata: {
70
- 'visual.enter_count_changed': {
71
- name: 'Player enter count changed',
72
- description: 'Reports a newly observed player entering view.',
73
- localizations: {
74
- 'zh-CN': {
75
- name: '玩家进入视野',
76
- description: '发现一名此前不可见的玩家进入当前视野。',
77
- },
78
- },
79
- },
80
- },
81
- };
82
-
83
- seen = new Set();
84
-
85
- update(frame) {
86
- const visibleOthers = frame.players.filter((player) => !player.observer);
87
- const entered = visibleOthers.filter((player) => !this.seen.has(player.id));
88
- this.seen = new Set(visibleOthers.map((player) => player.id));
89
- if (entered.length === 0) return;
90
-
91
- return entered.map((player) => ({
92
- type: 'visual.enter_count_changed',
93
- dedupeKey: `entered:${player.id}:${frame.frameSeq}`,
94
- payload: {
95
- from_tick: frame.tick,
96
- to_tick: frame.tick,
97
- actors: [player.seat ?? player.id],
98
- text: `${player.seat ?? player.name ?? player.id}进入了你的视野。`,
99
- },
100
- }));
101
- }
102
-
103
- reset() {
104
- this.seen.clear();
105
- }
106
- }
107
- ```
108
-
109
- `static manifest` 必须使用可静态读取的字面量;CCL 在不执行用户代码的前提下读取它。类可以包含普通私有字段,用来保存轨迹、计数器、候选关系和“已经报告到哪里”等短期状态。
110
-
111
- `eventMetadata` 为兼容已有自定义模块仍是可选字段;一旦提供,就必须完整覆盖 `produces`,不能声明额外 Event type。Desktop IPC 只会为当前激活模块、本批实际出现的 Event type 传递这里的本地化名称和描述。新模块建议始终提供该字段;文案属于模块,不应由 Mailbox 或 Desktop 建立中央映射。
112
-
113
- | 类成员 | 是否必需 | 作用 |
114
- |---|---:|---|
115
- | `static manifest` | 是 | 声明模块 ID、说明、事件主体范围、所有可能输出的 Event type,以及可选的 Event 显示元数据 |
116
- | `update(frame, api)` | 是 | 消费一份新的视觉帧,更新私有状态,并可返回零到多条 Event |
117
- | 普通实例字段 | 否 | 保存这个模块自己的跨帧状态;不同模块互不共享 |
118
- | `flush(boundary, api)` | 否 | 在查询或硬边界处报告尚未自然结束的长片段 |
119
- | `reset(reason)` | 否 | 硬边界后清除已失效的轨迹和候选状态 |
120
- | `debugSnapshot()` | 否 | 向诊断和离线复放提供可序列化摘要,不会发给 Agent |
121
-
122
- ### 耗时模块使用并行分析契约
123
-
124
- 如果一次识别可能接近或超过 100ms,不要把耗时工作和轨迹状态都塞进 `update()`。改为继承 `ClawclawParallelPerception`,并在 manifest 明确写 `execution: 'parallel_analysis'`:
125
-
126
- ```ts
127
- import {
128
- ClawclawParallelPerception,
129
- type PerceptionAnalysisInput,
130
- type PerceptionApi,
131
- type PerceptionEventProposal,
132
- type VisualState,
133
- } from '@myclaw163/clawclaw-cli/perception-plugin';
134
-
135
- type Evidence = { playerIds: string[] };
136
-
137
- export default class ExpensiveDetector extends ClawclawParallelPerception<Evidence> {
138
- static readonly manifest = {
139
- id: 'visual.expensive_detector',
140
- name: 'Expensive detector',
141
- description: 'Runs an expensive per-frame detector.',
142
- subjectScope: 'others',
143
- produces: ['visual.expensive_detector'],
144
- execution: 'parallel_analysis',
145
- } as const;
146
-
147
- private committedFrames = 0;
148
-
149
- async analyze(input: PerceptionAnalysisInput, api: PerceptionApi): Promise<Evidence> {
150
- // 只读本次 input/api;不要读取或修改实例历史。
151
- return { playerIds: input.frame.players.filter((player) => !player.observer).map((player) => player.id) };
152
- }
153
-
154
- commit(frame: VisualState, evidence: Evidence): PerceptionEventProposal[] {
155
- // CCL 按 frameSeq 顺序调用;只有这里维护长期状态和产生 Event。
156
- this.committedFrames += 1;
157
- if (evidence.playerIds.length === 0) return [];
158
- return [{
159
- type: 'visual.expensive_detector',
160
- dedupeKey: `expensive:${frame.frameSeq}`,
161
- payload: { from_tick: frame.tick, to_tick: frame.tick, player_ids: evidence.playerIds },
162
- }];
163
- }
164
- }
165
- ```
166
-
167
- `analyze()` 的每次调用使用一个全新实例,不会与持久 reducer 实例共享 `this`。CCL 把同一帧中全部已启用 `parallel_analysis` 模块合成一个 batch,交给一个已预热 Worker Thread;不同帧可由不同线程同时分析。每个线程在报告 ready 前已 import 并缓存全部并行模块的类,但每份 module/frame job 仍创建新 analyzer 实例。`input.frame` 是当前帧,`input.history` 是绑定同一调用 epoch、按时间升序排列的最近视觉帧,总窗口不超过 10 帧;`input.gapBefore` 表示已有前帧但当前帧无法证明连续。这样 N+1 的分析不需要等待 N 的 reducer 提交。返回值必须能被 `structuredClone`,不能包含函数、类实例、原生句柄或循环引用。`commit()`、`flush()`、`reset()` 和 `debugSnapshot()` 只运行在主线程的持久 reducer 实例上,其中 `commit()` 严格按帧顺序调用,必须保持短小,不能做网络请求、长循环或等待外部任务。
168
-
169
- 默认情况下,如果有序提交发现前面若干帧已被追帧窗口淘汰,CCL 会先重置 reducer,并丢弃此前基于旧 history 计算的 analysis。只有 analysis 同时包含一个“仅由当前帧建立状态”的安全基线,而且 `commit()` 在空 reducer 上保证不会保留任何 gap 前关系时,模块才可以在 manifest 声明 `boundaryBaseline: 'current_only'`。CCL 只允许它跨越恰好一次边界 generation 建立 reducer,并丢弃这次 `commit()` 返回的全部 Event。不要仅因为 analysis 里含有当前帧就声明此项;如果未来 Event 仍可能引用旧 history,就必须省略。
170
-
171
- 最大线程数与 `perception.maxBackfillVisualFrames` 及 Runtime 在途窗口对齐,取值 1–10,默认和硬上限都是 10。CCL 在 Perception Plugin 启动完成前只预热 `min(2, 最大线程数)` 个 Worker;当现有线程都忙、更多帧进入在途窗口时按压力逐步扩容,最多到 10。某个多余 Worker 连续空闲 30 秒后即可回收,但不会低于初始数量或当前在途需求。热路径每来一帧仍只提交一个包含全部并行模块的 batch,不按模块另建队列。在途窗口独立保持最大 10,当前物理线程较少不会缩小可接受窗口。一份正在分析的旧帧被 Runtime 取消时,只退役它占用的线程槽;仅在当前需求仍有容量缺口时补位。已经完成但尚未提交的旧 analysis 可以直接丢弃;其他帧和所有已提交 reducer 状态不受影响。
172
-
173
- 在途窗口已满时,Runtime 不会启动第 11 份帧计算。持续过载使中间帧被追帧窗口淘汰时,这些帧的 analysis 和 Event 都不得提交;Perception 把下一份保留帧视为轨迹断点,使旧 generation 失效、重置持久 reducer,并先建立不产生 Event 的新基线。因此模块必须能接受跳帧,不能假设所有 backend tick 都会到达或提交。
174
-
175
- 没有 `execution` 的 `ClawclawPerception.update()` 模块使用主线程持久实例,并保证同一实例不并发调用。CCL 不会猜一个模块是否“其实是纯函数”;如果 `update()` 可能超过一个游戏 tick,应明确迁移到 `parallel_analysis`。
176
-
177
- 自定义模块应把开发边界保持简单:
178
-
179
- - 普通 `update()` 只做能快速结束的有序计算;一旦可能阻塞下一帧,就改用 `parallel_analysis`。
180
- - `analyze()` 只依赖本次 `input/api`,不要依赖构造函数副作用、模块级可变变量或前一次 analyzer 实例;帧可能跳过,完成顺序也不确定。
181
- - `commit()`、`flush()` 和 `reset()` 是唯一可以维护跨帧轨迹状态的位置,必须短小,不能发网络请求或等待外部任务。
182
- - analysis 和 Event proposal 都必须是普通、可结构化克隆的数据;不要返回类实例、函数、原生句柄或循环引用。
183
- - 任何跨帧判断都要检查连续性,并把 `gapBefore`、`continuousFromPrevious: false` 或 reset 当作硬边界。
184
-
185
- ## Manifest 说明
186
-
187
- ```ts
188
- static readonly manifest = {
189
- id: 'visual.custom_linger',
190
- name: 'Custom linger',
191
- description: 'Reports another player remaining nearby.',
192
- subjectScope: 'others',
193
- produces: ['visual.custom_linger'],
194
- } as const;
195
- ```
196
-
197
- `id` 只能包含小写字母、数字、点、下划线和连字符,并且不能与另一个已安装模块重复。`produces` 必须列全这个类可能返回的 `visual.*` Event type;未声明的类型会被拒绝。`name` 和 `description` 是开发者可读信息,不参与运行时判断。
198
-
199
- `subjectScope` 说明 Event 的主体,不是输入过滤器:
200
-
201
- | 值 | 模块可以描述的主体 |
202
- |---|---|
203
- | `others` | 只描述视野内的其他玩家 |
204
- | `self_and_others` | 可以描述自己,也可以描述视野内其他玩家 |
205
- | `visible_pairs` | 描述当前帧中的任意两个玩家,包括“自己与别人”或“两个别人” |
206
- | `corpses` | 描述当前可见尸体 |
207
-
208
- 无论选择哪种 scope,模块拿到的 `frame.players` 都使用同一结构,并尽量包含自己和当前可见的其他玩家。自己通过 `observer: true` 识别;其他玩家是 `observer: false`。模块不能假设数组第一项永远存在,也不能把没有 `observer` 标记的对象猜成自己。
209
-
210
- ## 模块可以用什么信息
211
-
212
- `update(frame, api)` 的 `frame` 与 `analyze(input, api)` 的 `input.frame` 都是只读 `VisualState`,对应一份已经持久化并投影成功的视觉 State。普通模块的 `update()` 和并行模块的 `commit()` 始终按 `frameSeq` 递增;并行模块的 `analyze()` 可以乱序完成。同一游戏 tick 的多份 State revision 仍是不同 `frameSeq`,此时 `tickDelta` 可以为 0。
213
-
214
- | 字段 | 含义 |
215
- |---|---|
216
- | `frameKey` | Source 给这份 State 的稳定标识 |
217
- | `frameSeq` | CCL State History 的递增序号;不是游戏 tick |
218
- | `tick` | 后端游戏 tick;同一个 tick 可能对应不同 Source 输入,不能当作唯一 ID |
219
- | `timeMs` | 该帧的观测时间,毫秒时间戳 |
220
- | `phase` | 当前游戏阶段,例如 `wandering`、`meeting`、`dead` |
221
- | `tickDelta` | 与上一份视觉帧的游戏 tick 差;第一帧为 `null` |
222
- | `continuousFromPrevious` | CCL 是否确认它与上一帧属于同一条连续轨迹 |
223
- | `provenance` | 帧来源;缺省或 `raw` 表示 Source 真实观测 |
224
- | `players` | 自己与当前可见其他玩家的坐标、房间、名称、座位和生死信息 |
225
- | `corpses` | 当前可见尸体的坐标、房间和可用身份信息 |
226
- | `visibleSetComplete` | `true` 才能把“上一帧有、这一帧没有”解释为离开视野 |
227
-
228
- `VisualPlayer` 的稳定字段是 `id/x/y/room/observer`;`name/seat/alive` 只有 Source 已知时才存在。`VisualCorpse` 的稳定字段是 `id/x/y/room`,其余身份字段同样可能缺失。坐标采用屏幕坐标:x 增大表示向右,y 增大表示向下。
229
-
230
- 兼容层的插值帧可以给 motion、stop 等单主体轨迹模块补齐位置基线,但它不是新的现场观测。官方 co-movement/follow 模块不会用这些 synthetic tick 累加关系建立或结束证据;最终的 raw endpoint 才能提交一次观测。
231
-
232
- 感知不区分“self 模块”和“other 模块”。例如移动模块可以遍历所有 `players`,同时为自己和别人产生 Event;进入视野模块则应过滤 `observer`,只维护其他玩家。尸体也不是玩家数组中的特殊玩家,应读取 `corpses`。
233
-
234
- ### 历史由模块自己维护
235
-
236
- Perception Plugin 不向模块暴露可修改的共享 History repository。普通模块可把上一帧、轨迹和已报告水位保存在 `update()` 所属实例中;并行模块的 `analyze()` 只能读取输入自带的不可变最近帧切片,长期状态只能在持久 reducer 的 `commit()` 中保存,`analyze()` 实例每次都是新的:
237
-
238
- ```ts
239
- private previous: VisualState | null = null;
240
- private readonly points = new Map<string, Array<{ frameSeq: number; tick: number; x: number; y: number }>>();
241
-
242
- update(frame: VisualState) {
243
- const previous = frame.continuousFromPrevious ? this.previous : null;
244
- this.previous = frame;
245
- // 只保存本模块算法真正需要的样本。
246
- }
247
-
248
- reset() {
249
- this.previous = null;
250
- this.points.clear();
251
- }
252
- ```
253
-
254
- 这样移动模块可以保留轨迹点,进出视野模块只保留上一帧的可见集合,瞬时信号模块甚至可以不留历史。Runtime 恢复时,Perception Plugin 会按 `frameSeq` 重放已持久化 State,通过同样的有序 `update()` 或 `commit()` 重建私有历史。
255
-
256
- ### Runtime 并行计算、顺序提交与追帧
257
-
258
- Perception Plugin 的计算窗口默认允许最多 10 个 epoch 在途。新帧不等待旧帧的 `analyze()` 完成;计算结果可以乱序返回,但 Runtime 只按 epoch 顺序调用 Perception `commit()`,因此 Canonical Event、checkpoint 和 Mailbox 水位不会乱序。七个官方模块都把每帧几何、方位、边和玩家对计算放入并行 `analyze()`;轨迹水位、迟滞和 Event 生成留在短小的有序 `commit()`。未声明并行模式的自定义 `update()` 仍完整串行。
259
-
260
- `perception.maxBackfillVisualFrames` 现在同时决定 Perception 的最大在途窗口,默认 10。最旧计算落后超过窗口时,Runtime 明确淘汰它;后续有序提交看到 `frameSeq` 缺口,会先以 `trajectory_gap` 关闭并重置旧轨迹,再把当前帧作为新基线。它不会把缺口两端拼成连续移动。`ccl events` 查询边界禁止合并,只有边界前仍保留的结果完成或被明确淘汰后,才会执行 `flush(events_requested)`。
261
-
262
- 模块因此只需遵守两个规则:每次 `update()`/`commit()` 只提交当前这一帧;`continuousFromPrevious=false` 或随后的 `reset('trajectory_gap')` 之后,不把新样本接到旧轨迹。最大报告延迟仍由模块自己的计数器决定,与追帧窗口无关。
263
-
264
- ### `api.spatial`
265
-
266
- `api.spatial` 是 CCL 从当前前端地图同步出的只读空间能力:
267
-
268
- | 字段或方法 | 含义 |
269
- |---|---|
270
- | `xPositive` / `yPositive` | 固定为 `right` / `down` |
271
- | `playerBodyLengthPx` | 当前地图尺度下一个玩家身位的像素长度 |
272
- | `rooms` | 房间多边形和包围盒 |
273
- | `roomAt(x, y)` | 按随包地图 polygon 解析房间;未落入命名房间时返回 `null` |
274
- | `sideInRoom(position)` | 已确认在房间内时返回上、下、左、右或组合方位 |
275
- | `placeLabel(position)` | 生成保守的位置标签,例如“能源舱左侧” |
276
- | `transitionIsMapped(from, to)` | 两个房间是否存在已登记的直接转换关系 |
277
-
278
- 这些接口只提供已知几何事实,不负责猜目的地。玩家向右移动而右边可能通往多个房间时,应输出“向右移动”,不能擅自写成“前往某个房间”。
279
-
280
- ## `update()` / `commit()` 需要输出什么
281
-
282
- 普通模块的 `update()`、并行模块的 `commit()` 以及两者的 `flush()` 可以不返回,也可以返回 `PerceptionEventProposal[]`。`analyze()` 只返回内部 evidence,不能直接产生 Event。每条 Event proposal 的最小结构是:
283
-
284
- ```ts
285
- return [{
286
- type: 'visual.custom_linger',
287
- dedupeKey: `linger:${player.id}:${fromFrameSeq}:${toFrameSeq}`,
288
- payload: {
289
- from_tick: fromTick,
290
- to_tick: toTick,
291
- actors: [player.seat ?? player.id],
292
- status: 'ongoing',
293
- text: '7号持续停留在能源舱左侧。',
294
- data: { player_id: player.id, frame_count: 100 },
295
- },
296
- }];
297
- ```
298
-
299
- | 字段 | 要求 |
300
- |---|---|
301
- | `type` | 必须出现在本类 `manifest.produces` 中 |
302
- | `dedupeKey` | 必填;同一语义片段和同一报告区间必须稳定,重放时不能随机生成 |
303
- | `payload` | 必填的可序列化对象;不要放类实例、函数或循环引用 |
304
- | `occurredAt` | 可选;省略时 CCL 使用视觉帧时间 |
305
- | `dedupeScope` | 可选的 `segment` 或 `session`;通常使用默认 segment 范围即可 |
306
-
307
- 为了让 Mailbox 和 Agent 直接消费,视觉 Event 建议统一提供 `from_tick`、`to_tick`、`actors`、`text`,并把算法明细放进 `data`;尚未结束的片段加 `status: "ongoing"`。Event 只描述已经由输入支持的事实,不要输出“可能在做任务”“应该前往某房间”等策略判断。
308
-
309
- Runtime 会为成功返回的整批 Event 补上当前 `epoch_seq`、`producer_plugin_id` 和稳定的生产序号,经 Event Catalog 校验后统一写入 Ledger。模块不应自己伪造这些归属字段,也不能直接投递给 Monitor 或 `ccl events`。
310
-
311
- ## 长片段怎样切分
312
-
313
- 瞬时事实通常在 `update()` 中立即返回,例如“进入视野”“转向”“从静止恢复”。持续事实需要模块自己保存生命周期,例如“从哪移动到哪”“连续停留”“跟随关系”。一个模块应至少记录:片段起点、最近样本、是否已经达到成立阈值,以及已经报告到哪个 frame/tick。
314
-
315
- **最大报告延迟也是模块自己的逻辑,不是 Runtime 的统一配置。** 例如模块可以规定“未报告样本达到 100 帧时,输出一次 `status: ongoing` 的 Event”,然后把 `reportedThroughFrameSeq` 推进到当前帧并继续保留轨迹。也可以按 10 个游戏 tick 或 1 秒切片,取决于该信号的语义。官方移动、共同移动和跟随模块当前按自己的 tick 阈值周期报告;其他瞬时模块不需要这个计数器。
316
-
317
- Runtime 只提供不能由模块自然预测的边界:
318
-
319
- | `boundary.kind` | `mode` | 模块应做什么 |
320
- |---|---|---|
321
- | `events_requested` | `snapshot` | `ccl events` 查询点;报告截至 `through` 尚未报告的尾段,标记 ongoing,推进已报告水位,但保留轨迹 |
322
- | `phase_changed` | `close` | 在新阶段帧进入本模块历史前结束旧阶段片段,随后 `reset()` |
323
- | `trajectory_gap` | `close` | 连接或 tick 连续性无法确认;结束可确认的旧片段,随后 `reset()`,禁止跨缺口连轨迹 |
324
- | `segment_ending` | `close` | 房间或对局 segment 正常结束时收尾,随后 `reset()` |
325
- | `shutdown` | `close` | CCL 退出时释放生命周期;正常退出会先通过 Source 终止边界排空可提交结果 |
326
-
327
- `boundary.through` 是本次允许使用的最后一份视觉帧;还没有视觉帧时为 `null`。连续调用两次 `ccl events` 且中间没有新视觉帧时,第二次 `flush()` 必须不重复返回上一段,所以模块要维护自己的“已报告到哪里”。
328
-
329
- 一个合理的长片段流程是:
330
-
331
- ```text
332
- 连续 update() 累积样本
333
- -> 达到自然成立/最大未报告帧数:返回 Event,推进 reported-through
334
- -> ccl events:flush(snapshot),只返回尚未报告尾段,轨迹继续
335
- -> 离开视野/停止:update() 返回结束 Event,删除轨迹
336
- -> 阶段变化/断线:flush(close),随后 reset()
337
- ```
338
-
339
- ## 完整的周期报告示例
340
-
341
- 下面的模块只观察其他玩家。某人连续出现在视野中时,它最多积累 100 次 `update()` 就报告一次;`ccl events` 提前到来时立即截断尚未报告部分,但不会结束这条轨迹。
342
-
343
- ```ts
344
- import {
345
- ClawclawPerception,
346
- type PerceptionApi,
347
- type PerceptionBoundary,
348
- type PerceptionEventProposal,
349
- type VisualPlayer,
350
- type VisualState,
351
- } from '@myclaw163/clawclaw-cli/perception-plugin';
352
-
353
- interface Track {
354
- player: VisualPlayer;
355
- fromTick: number;
356
- lastTick: number;
357
- fromFrameSeq: number;
358
- lastFrameSeq: number;
359
- unreportedFrames: number;
360
- }
361
-
362
- export default class CustomLinger extends ClawclawPerception {
363
- static readonly manifest = {
364
- id: 'visual.custom_linger',
365
- name: 'Custom linger',
366
- description: 'Reports how long another player remains visible.',
367
- subjectScope: 'others',
368
- produces: ['visual.custom_linger'],
369
- } as const;
370
-
371
- private static readonly MAX_UNREPORTED_FRAMES = 100;
372
- private readonly tracks = new Map<string, Track>();
373
-
374
- update(frame: VisualState, api: PerceptionApi): PerceptionEventProposal[] {
375
- if (frame.phase !== 'wandering') return [];
376
- const output: PerceptionEventProposal[] = [];
377
- const visible = frame.players.filter((player) => !player.observer);
378
- const visibleIds = new Set(visible.map((player) => player.id));
379
-
380
- for (const player of visible) {
381
- const track = this.tracks.get(player.id) ?? {
382
- player,
383
- fromTick: frame.tick,
384
- lastTick: frame.tick,
385
- fromFrameSeq: frame.frameSeq,
386
- lastFrameSeq: frame.frameSeq,
387
- unreportedFrames: 0,
388
- };
389
- track.player = player;
390
- if (track.unreportedFrames === 0) {
391
- track.fromTick = frame.tick;
392
- track.fromFrameSeq = frame.frameSeq;
393
- }
394
- track.lastTick = frame.tick;
395
- track.lastFrameSeq = frame.frameSeq;
396
- track.unreportedFrames += 1;
397
- this.tracks.set(player.id, track);
398
- if (track.unreportedFrames >= CustomLinger.MAX_UNREPORTED_FRAMES) {
399
- output.push(this.report(track, api, true));
400
- }
401
- }
402
-
403
- if (frame.visibleSetComplete) {
404
- for (const [playerId, track] of this.tracks) {
405
- if (visibleIds.has(playerId)) continue;
406
- if (track.unreportedFrames > 0) output.push(this.report(track, api, false));
407
- this.tracks.delete(playerId);
408
- }
409
- }
410
- return output;
411
- }
412
-
413
- flush(boundary: PerceptionBoundary, api: PerceptionApi): PerceptionEventProposal[] {
414
- return [...this.tracks.values()]
415
- .filter((track) => track.unreportedFrames > 0)
416
- .map((track) => this.report(track, api, boundary.mode === 'snapshot'));
417
- }
418
-
419
- reset(): void {
420
- this.tracks.clear();
421
- }
422
-
423
- debugSnapshot(): unknown {
424
- return [...this.tracks.values()].map((track) => ({
425
- player_id: track.player.id,
426
- from_frame_seq: track.fromFrameSeq,
427
- last_frame_seq: track.lastFrameSeq,
428
- unreported_frames: track.unreportedFrames,
429
- }));
430
- }
431
-
432
- private report(track: Track, api: PerceptionApi, ongoing: boolean): PerceptionEventProposal {
433
- const frames = track.unreportedFrames;
434
- const fromTick = track.fromTick;
435
- const fromFrameSeq = track.fromFrameSeq;
436
- track.unreportedFrames = 0;
437
- return {
438
- type: 'visual.custom_linger',
439
- dedupeKey: `linger:${track.player.id}:${fromFrameSeq}:${track.lastFrameSeq}`,
440
- payload: {
441
- from_tick: fromTick,
442
- to_tick: track.lastTick,
443
- actors: [track.player.seat ?? track.player.id],
444
- ...(ongoing ? { status: 'ongoing' } : {}),
445
- text: `${track.player.seat ?? track.player.name ?? track.player.id}在${api.spatial.placeLabel(track.player)}持续可见。`,
446
- data: {
447
- player_id: track.player.id,
448
- frame_count: frames,
449
- from_frame_seq: fromFrameSeq,
450
- to_frame_seq: track.lastFrameSeq,
451
- },
452
- },
453
- };
454
- }
455
- }
456
- ```
457
-
458
- 示例在每次报告后把 `unreportedFrames` 清零,下一份视觉帧会成为新片段起点,因此连续 Event 的 frame 区间不会重叠。更复杂的模块也可以直接保存 `reportedThroughTick/reportedThroughFrameSeq`,但仍必须保证查询截断、自然结束和周期报告不会重复同一段轨迹。
459
-
460
- ## 官方模块和主体范围
461
-
462
- 七个官方模块也由一个 `ClawclawPerceptionPlugin` 管理,各自保留独立的 reducer 实例和跨帧状态,并全部声明 `parallel_analysis`。不同帧的纯分析可以重叠,结果仍按 `frameSeq` 进入各自 reducer:
463
-
464
- | 模块 ID | 主体 | 主要 Event |
465
- |---|---|---|
466
- | `visual.visibility` | 仅其他玩家 | 进入、离开、重新出现 |
467
- | `visual.motion` | 自己和其他玩家 | 持续移动、移动方向变化 |
468
- | `visual.stop` | 自己和其他玩家 | 持续至少 1 秒的停留、经位移确认后的恢复移动 |
469
- | `visual.spatial` | 自己和其他玩家 | 房间变化、房间内方位变化 |
470
- | `visual.route` | 自己和其他玩家 | 转向、折返、徘徊 |
471
- | `visual.co_movement` | 可见玩家对 | 同方向共同移动及结束 |
472
- | `visual.follow` | 可见玩家对 | 前后跟随关系及结束 |
473
-
474
- 模块之间没有共享可变状态。每个模块自己记录需要的历史样本、候选轨迹、最大报告延迟和已报告水位。当前官方模块统一遵守以下时间语义:
475
-
476
- - 同一 backend tick 的多份 State 是对一个画面的修订,不是新的时间证据;motion、route 和 stop 对该 tick 的坐标采用 last-write-wins,并等下一 distinct tick 后才确认。
477
- - motion 用稳定八方向航向判断明显转向,并把连续轨迹按最长约 2 秒的语义片段报告。
478
- - stop 不再输出 `visual.no_significant_stop` 这类“什么都没发生”的负事实;短于 1 秒的停顿不会升级成 Event。
479
- - co-movement 和 following 需要持续证据才建立,短暂阈值抖动不会立即结束;玩家离开完整视野时,由 visibility 报告离开,关系模块只清理私有状态。
480
- - query/snapshot 边界不提交仍可被同 tick 修订的 provisional 样本;它只可投影达到报告间隔的已确认 open span,不把查询本身当成新的时间证据。
481
-
482
- Perception Plugin 负责当前帧投影、边界调用和输出汇总。Canonical 层保留 moved、motion_changed、following、co-movement 等独立 type 和各自完整区间;不要在这里为了缩短 stdout 而破坏 type 订阅契约。将来如需把同窗事实折叠成更短叙事,应在有完整性元数据的 Agent-facing projection 中完成。Runtime 仍只记录 `clawclaw.perception` 的一份完成结果,模块 Event 通过各自 manifest 的 type 区分。
483
-
484
- ## 信任边界和运行限制
485
-
486
- 自定义感知是与 CCL 同权限运行的受信任本地代码,不是安全沙箱。CCL 启动时先静态读取 manifest,再直接 import 已启用模块:普通模块及并行模块的持久 reducer 在 Runtime 主线程执行;`parallel_analysis` 的 `analyze()` 在自适应 Worker Thread 池执行。Worker Thread 与主线程共享同一进程权限和环境,因此只应安装、启用自己信任的源码。
487
-
488
- CCL 不承诺拦截 Node builtin、第三方包、动态 `import()`、`require()`、文件、网络、子进程或原生扩展。模块约定仍要求感知算法只使用 SDK 输入,不连接后端、不读取 Runtime/SQLite 私有对象,也不自行启动线程或进程;这是可维护性和职责边界,不是安全隔离能力。模块也不得写 `console.log()`、`process.stdout.write()` 或其他 stdout 输出:Owner 主线程上的普通模块/reducer 输出会直接污染 Monitor NDJSON;并行 Worker Thread 的 stdout 虽由宿主截获而不会混入 Relay,也只会形成诊断噪声。
489
-
490
- 主线程调用自定义模块前会结构化克隆并深度冻结输入,返回的 Event proposal 也会被克隆和校验;并行 analysis 通过 Worker Thread 消息传递,返回值必须可结构化克隆。这些措施用于保护帧引用和输出契约,不能阻止恶意本地代码访问进程能力。
491
-
492
- 自适应线程池在 Perception Plugin 启动时只预热初始容量,每个线程受内存上限约束;Runtime 的取消或超时命中正在工作的 batch 时,只终止对应线程槽,并按当前在途需求决定是否补位。排队但尚未开始的 batch 可直接取消。空闲回收只终止 idle Worker,不会打断分析;CCL 长进程停止时会关闭整个池。正常模块只能通过 manifest 已声明的 Event proposal 进入 Canonical Event Ledger,但这同样不是针对恶意代码的安全保证。
493
-
494
- ## 调试和复放
495
-
496
- 实现 `debugSnapshot()` 后,离线复放报告会显示每个模块当前的候选轨迹、计数器和已报告水位。它只应返回简短、可序列化的诊断数据,不能把全部历史再复制一遍。
497
-
498
- ```powershell
499
- npm run replay:perception -- --tick 580 --modules visual.custom_linger --output ..\tmp\perception-580.json
500
- ```
501
-
502
- 复放使用和在线 Runtime 相同的 Visual State 投影与模块生命周期,不消费 Agent 的 Event cursor,也不修改 Canonical Event Ledger。详细参数见 [Perception 离线复放](./runtime/perception-replay.md)。