@myclaw163/clawclaw-cli 0.9.1 → 0.10.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 (216) hide show
  1. package/QUICKSTART_OTHER.md +5 -5
  2. package/README.md +7 -8
  3. package/bin/clawclaw-channel.mjs +2 -2
  4. package/bin/clawclaw-cli.mjs +2 -2
  5. package/docs/configuration.md +25 -99
  6. package/docs/custom-modules/README.md +27 -40
  7. package/docs/custom-modules/api-reference.md +132 -273
  8. package/docs/custom-modules/behavior.md +185 -75
  9. package/docs/custom-modules/examples/auto-escape.ts +41 -0
  10. package/docs/custom-modules/examples/go-to-point.ts +11 -11
  11. package/docs/custom-modules/examples/nearby-reflex.ts +22 -0
  12. package/docs/custom-modules/examples/task-reminder.ts +4 -5
  13. package/docs/custom-modules/perception.md +46 -806
  14. package/docs/custom-modules/skill.md +1 -1
  15. package/docs/desktop-event-push.md +3 -3
  16. package/docs/runtime/README.md +18 -19
  17. package/docs/runtime/agent-notifications.md +31 -12
  18. package/docs/runtime/behavior-runtime.md +54 -48
  19. package/docs/runtime/components-and-boundaries.md +87 -137
  20. package/docs/runtime/events-and-state.md +52 -17
  21. package/docs/runtime/framework-package.md +73 -0
  22. package/docs/runtime/official-perception-modules.md +5 -5
  23. package/docs/runtime/owner-lifecycle-and-observability.md +6 -6
  24. package/docs/runtime/short-commands-and-capabilities.md +8 -9
  25. package/docs/user-guide/README.md +12 -9
  26. package/docs/user-guide/api-reference.md +132 -180
  27. package/docs/user-guide/behavior.md +175 -65
  28. package/docs/user-guide/installation.md +7 -7
  29. package/docs/user-guide/perception.md +46 -789
  30. package/docs/user-guide/skill.md +1 -1
  31. package/package.json +24 -9
  32. package/scripts/check-framework-boundaries.mjs +27 -0
  33. package/scripts/check-skill-command-surface.mjs +9 -14
  34. package/scripts/install-local-runtime.mjs +35 -0
  35. package/scripts/qa-install.mjs +105 -0
  36. package/skills/clawclaw/SKILL.md +50 -48
  37. package/skills/clawclaw/references/BEHAVIORS.md +45 -65
  38. package/skills/clawclaw/references/CHANNELS.md +2 -2
  39. package/skills/clawclaw/references/COMMANDS.md +65 -87
  40. package/skills/clawclaw/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +185 -75
  41. package/skills/clawclaw/references/CUSTOM-MODULE-API.md +145 -198
  42. package/skills/clawclaw/references/CUSTOM-MODULES.md +28 -29
  43. package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +46 -811
  44. package/skills/clawclaw/references/CUSTOM-SKILL-DEVELOPMENT.md +1 -1
  45. package/skills/clawclaw/references/HUB.md +1 -1
  46. package/skills/clawclaw/references/STREAM.md +8 -8
  47. package/skills/clawclaw/references/TACTICS.md +1 -1
  48. package/src/channel/claude-channel-ipc-server.ts +1 -0
  49. package/src/cli.ts +4 -2
  50. package/src/commands/behavior.ts +49 -104
  51. package/src/commands/capability.ts +3 -3
  52. package/src/commands/load.ts +18 -25
  53. package/src/commands/module-runtime-control.ts +113 -0
  54. package/src/commands/perception.ts +23 -4
  55. package/src/commands/strategy.ts +2 -0
  56. package/src/commands/trigger.ts +1 -1
  57. package/src/lib/capability-load-diagnostics.ts +3 -3
  58. package/src/lib/perception-config.ts +18 -6
  59. package/src/lib/runtime-cadence-config.ts +16 -0
  60. package/src/lib/workspace-runtime-lock.ts +3 -46
  61. package/src/pipeline/canonical-event-presentation.ts +23 -124
  62. package/src/pipeline/event-format.ts +12 -21
  63. package/src/pipeline/event-hints.ts +6 -6
  64. package/src/pipeline/game-sync-event-normalization.ts +57 -63
  65. package/src/runtime/clawclaw/behavior-decision.ts +7 -3
  66. package/src/runtime/clawclaw/behavior-input.ts +8 -4
  67. package/src/runtime/clawclaw/behavior-observation-memory.ts +3 -2
  68. package/src/runtime/clawclaw/behavior-read-api.ts +4 -0
  69. package/src/runtime/clawclaw/behavior-reflex-control.ts +74 -0
  70. package/src/runtime/clawclaw/notification-policy.ts +42 -0
  71. package/src/runtime/clawclaw/notification-turn.ts +19 -0
  72. package/src/runtime/clawclaw/observation-frame-projector.ts +3 -3
  73. package/src/runtime/clawclaw/owner.ts +67 -8
  74. package/src/runtime/clawclaw/perception-module-manifest.ts +22 -8
  75. package/src/runtime/clawclaw/perception-strategy-names.ts +17 -0
  76. package/src/runtime/clawclaw/persistence-compatibility.ts +36 -0
  77. package/src/runtime/clawclaw/plugins/action/action-plugin.ts +3 -3
  78. package/src/runtime/clawclaw/plugins/assembly.ts +29 -41
  79. package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +1 -482
  80. package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +17 -186
  81. package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +2 -0
  82. package/src/runtime/clawclaw/plugins/behavior/behavior-instance-snapshot.ts +1 -123
  83. package/src/runtime/clawclaw/plugins/behavior/behavior-normalization.ts +3 -0
  84. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +126 -1707
  85. package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +13 -271
  86. package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +2 -2
  87. package/src/runtime/clawclaw/plugins/behavior/behavior-worker-bridge.ts +8 -0
  88. package/src/runtime/clawclaw/plugins/behavior/control-driver.ts +3 -180
  89. package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +13 -960
  90. package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.ts +9 -107
  91. package/src/runtime/clawclaw/plugins/derived-event/derived-event-rules.ts +5 -4
  92. package/src/runtime/clawclaw/plugins/derived-event/reminder-plugin.ts +27 -17
  93. package/src/runtime/clawclaw/plugins/derived-event/reminder-runner.ts +1 -0
  94. package/src/runtime/clawclaw/plugins/derived-event/state-change-rules.ts +84 -0
  95. package/src/runtime/clawclaw/plugins/derived-event/task-change-tracker.ts +1 -0
  96. package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +4 -4
  97. package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +9 -97
  98. package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-socket.ts +8 -343
  99. package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +60 -572
  100. package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +6 -434
  101. package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +1 -266
  102. package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +6 -161
  103. package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +4 -23
  104. package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +49 -752
  105. package/src/runtime/clawclaw/plugins/perception/map-concurrent.ts +1 -0
  106. package/src/runtime/clawclaw/plugins/perception/notification-content.ts +60 -0
  107. package/src/runtime/clawclaw/plugins/perception/perception-module-plugin.ts +136 -0
  108. package/src/runtime/clawclaw/plugins/perception/perception-module-runner.ts +13 -0
  109. package/src/runtime/clawclaw/plugins/perception/perception-module-worker.ts +13 -24
  110. package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +19 -8
  111. package/src/runtime/clawclaw/plugins/perception/perception-source.ts +23 -15
  112. package/src/runtime/clawclaw/plugins/perception/task-baseline.ts +29 -0
  113. package/src/runtime/clawclaw/plugins/source/source-plugin.ts +8 -103
  114. package/src/runtime/clawclaw/plugins/system-perception/rule-runner.ts +107 -0
  115. package/src/runtime/clawclaw/plugins/system-perception/system-perceptions.ts +107 -0
  116. package/src/runtime/clawclaw/plugins/trigger/contracts.ts +3 -1
  117. package/src/runtime/clawclaw/plugins/trigger/trigger-module-loader.ts +5 -2
  118. package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +69 -13
  119. package/src/runtime/clawclaw/plugins/trigger/trigger-thread-pool.ts +13 -1
  120. package/src/runtime/clawclaw/plugins/trigger/trigger-thread-worker.ts +4 -0
  121. package/src/runtime/clawclaw/reaction-behavior-resolution.ts +1 -23
  122. package/src/runtime/clawclaw/reaction-manifest.ts +18 -12
  123. package/src/runtime/clawclaw/reminder-manifest.ts +5 -2
  124. package/src/runtime/clawclaw/runtime-cadence.ts +24 -0
  125. package/src/runtime/clawclaw/runtime-state.ts +12 -1
  126. package/src/runtime/clawclaw/runtime.ts +11 -3
  127. package/src/runtime/clawclaw/strategy-memory-rpc.ts +1 -0
  128. package/src/runtime/clawclaw/strategy-memory.ts +1 -0
  129. package/src/runtime/clawclaw-event-catalog.ts +17 -17
  130. package/src/runtime/framework/action-diagnostics.ts +3 -83
  131. package/src/runtime/framework/capability-registry.ts +2 -395
  132. package/src/runtime/framework/contracts.ts +2 -144
  133. package/src/runtime/framework/control/control-scheduler.ts +2 -1268
  134. package/src/runtime/framework/control/domain-set.ts +2 -81
  135. package/src/runtime/framework/control/index.ts +1 -3
  136. package/src/runtime/framework/control/types.ts +2 -191
  137. package/src/runtime/framework/control-coordinator.ts +2 -11
  138. package/src/runtime/framework/events/event-catalog.ts +2 -74
  139. package/src/runtime/framework/events/event-delivery-service.ts +2 -200
  140. package/src/runtime/framework/events/event-ingress.ts +2 -99
  141. package/src/runtime/framework/events/event-repository.ts +2 -82
  142. package/src/runtime/framework/events/index.ts +2 -7
  143. package/src/runtime/framework/events/memory-event-repository.ts +2 -1100
  144. package/src/runtime/framework/events/runtime-resume.ts +2 -20
  145. package/src/runtime/framework/events/sqlite-event-repository.ts +5 -2280
  146. package/src/runtime/framework/events/test-helpers.ts +1 -1
  147. package/src/runtime/framework/events/types.ts +2 -312
  148. package/src/runtime/framework/index.ts +3 -7
  149. package/src/runtime/framework/load-node-sqlite.ts +2 -17
  150. package/src/runtime/framework/mailbox/coalescer.ts +2 -83
  151. package/src/runtime/framework/mailbox/host-notification-sink.ts +2 -44
  152. package/src/runtime/framework/mailbox/identity.ts +2 -38
  153. package/src/runtime/framework/mailbox/index.ts +1 -7
  154. package/src/runtime/framework/mailbox/mailbox-dispatcher.ts +2 -282
  155. package/src/runtime/framework/mailbox/mailbox-repository.ts +2 -44
  156. package/src/runtime/framework/mailbox/notification-projector.ts +2 -65
  157. package/src/runtime/framework/mailbox/types.ts +2 -145
  158. package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +2 -154
  159. package/src/runtime/framework/modules/runtime-module-loader.ts +7 -51
  160. package/src/runtime/framework/modules/runtime-module-output.ts +2 -24
  161. package/src/runtime/framework/modules/runtime-module-worker-bootstrap.mjs +3 -23
  162. package/src/runtime/framework/participation-manager.ts +2 -253
  163. package/src/runtime/framework/persistence/async-sqlite-persistence-worker.ts +2 -77
  164. package/src/runtime/framework/persistence/memory-first-persistence.ts +4 -489
  165. package/src/runtime/framework/plugins/contracts.ts +2 -388
  166. package/src/runtime/framework/plugins/index.ts +1 -4
  167. package/src/runtime/framework/plugins/runtime-event-identity.ts +2 -17
  168. package/src/runtime/framework/plugins/runtime-input-journal.ts +2 -866
  169. package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +2 -2060
  170. package/src/runtime/framework/runtime.ts +5 -1843
  171. package/src/runtime/framework/serial-task-queue.ts +2 -14
  172. package/src/runtime/framework/state/contracts.ts +2 -49
  173. package/src/runtime/framework/state/current-state-store.ts +2 -184
  174. package/src/runtime/framework/state/durable-agent-state-snapshot.ts +2 -209
  175. package/src/runtime/framework/state/freshness.ts +2 -72
  176. package/src/runtime/framework/state/history/contracts.ts +2 -158
  177. package/src/runtime/framework/state/history/history-writer.ts +2 -194
  178. package/src/runtime/framework/state/history/memory-state-history-repository.ts +2 -230
  179. package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +2 -648
  180. package/src/runtime/framework/state/history/trajectory-projector.ts +2 -209
  181. package/src/runtime/framework/state/index.ts +1 -13
  182. package/src/runtime/framework/state/state-ingress.ts +2 -95
  183. package/src/runtime/framework/state/state-provider.ts +2 -62
  184. package/src/runtime/framework/state/types.ts +2 -155
  185. package/src/runtime/host-monitor-rate-gate.ts +1 -123
  186. package/src/runtime/mailbox-host-relay.ts +1 -326
  187. package/src/runtime/observability/detail-capture.ts +4 -108
  188. package/src/runtime/observability/process-timing.ts +2 -59
  189. package/src/runtime/observability/recorder.ts +2 -238
  190. package/src/runtime/observability/redaction.ts +2 -181
  191. package/src/runtime/observability/reporter.ts +2 -154
  192. package/src/runtime/observability/sinks.ts +2 -135
  193. package/src/runtime/observability/types.ts +2 -92
  194. package/src/runtime/owner-control.ts +45 -315
  195. package/src/runtime/parent-process-watch.ts +1 -51
  196. package/src/runtime/process-identity.ts +1 -72
  197. package/src/sdk/behavior-plugin/index.ts +13 -4
  198. package/src/sdk/index.ts +44 -3
  199. package/src/sdk/module-input/index.ts +5 -0
  200. package/src/sdk/perception-plugin/index.ts +9 -5
  201. package/src/sdk/perception-plugin/module.ts +25 -6
  202. package/src/sdk/perception-plugin/reaction.ts +1 -1
  203. package/src/sdk/perception-plugin/reminder-rules.ts +8 -42
  204. package/src/sdk/perception-plugin/reminder.ts +2 -1
  205. package/src/{runtime/clawclaw/plugins/source → sdk/perception-plugin}/task-change-tracker.ts +1 -2
  206. package/src/sdk/strategy-memory.ts +1 -0
  207. package/src/sdk/strategy-plugin/index.ts +36 -0
  208. package/src/sdk/trigger-plugin/index.ts +4 -3
  209. package/docs/custom-modules/examples/nearby-escape.ts +0 -56
  210. package/docs/custom-modules/examples/nearby-notice.ts +0 -36
  211. package/docs/custom-modules/trigger.md +0 -295
  212. package/src/runtime/clawclaw/behavior-activation-capability.ts +0 -21
  213. package/src/runtime/clawclaw/plugins/perception/reaction-plugin.ts +0 -233
  214. package/src/runtime/clawclaw/plugins/perception/reaction-runner.ts +0 -49
  215. package/src/runtime/clawclaw/plugins/perception/reaction-worker.ts +0 -26
  216. /package/src/runtime/clawclaw/plugins/{source → derived-event}/player-change-tracker.ts +0 -0
@@ -1,8 +1,9 @@
1
1
  import type { GameClient } from '../../../lib/game-client.js';
2
+ import { ClawclawStrategyMemory } from '../strategy-memory.js';
2
3
  import { ClawclawReminderPlugin, CLAWCLAW_REMINDER_PLUGIN_ID } from './derived-event/reminder-plugin.js';
3
- import { listClawclawPerceptionSources } from './perception/perception-source.js';
4
+ import { listClawclawPerceptionSources, isSupportedPerceptionSource } from './perception/perception-source.js';
4
5
  import { DEFAULT_PERCEPTION_MODULES } from './perception/official-module-ids.js';
5
- import { ClawclawReactionPlugin, CLAWCLAW_REACTION_PLUGIN_ID, CLAWCLAW_PERCEPTION_MODULE_PLUGIN_ID } from './perception/reaction-plugin.js';
6
+ import { ClawclawPerceptionModulePlugin, CLAWCLAW_PERCEPTION_MODULE_PLUGIN_ID } from './perception/perception-module-plugin.js';
6
7
  import type { RuntimeEpochPlugin } from '../../framework/plugins/index.js';
7
8
  import {
8
9
  CLAWCLAW_ACTION_PLUGIN_ID,
@@ -14,10 +15,8 @@ import {
14
15
  ClawclawBehaviorPlugin,
15
16
  type ClawclawRoleFallbackBehaviors,
16
17
  } from './behavior/index.js';
17
- import {
18
- CLAWCLAW_DERIVED_EVENT_PLUGIN_ID,
19
- ClawclawDerivedEventPlugin,
20
- } from './derived-event/index.js';
18
+ import { createSystemPerceptions } from './system-perception/system-perceptions.js';
19
+ import { BUILTIN_PERCEPTION_PREFIX } from '../notification-policy.js';
21
20
  import {
22
21
  CLAWCLAW_GAME_QUERY_PLUGIN_ID,
23
22
  ClawclawGameQueryPlugin,
@@ -50,8 +49,7 @@ export interface ClawclawPluginAssemblyOptions {
50
49
  }
51
50
 
52
51
  export interface ClawclawPluginAssembly {
53
- modules?: ClawclawReactionPlugin;
54
- reactions?: ClawclawReactionPlugin;
52
+ modules?: ClawclawPerceptionModulePlugin;
55
53
  plugins: RuntimeEpochPlugin<Record<string, any>, Record<string, unknown>>[];
56
54
  action: ClawclawActionPlugin;
57
55
  gameQuery: ClawclawGameQueryPlugin;
@@ -69,6 +67,9 @@ export function assembleClawclawPlugins(
69
67
  ): ClawclawPluginAssembly {
70
68
  const plugins = [...(options.epochPlugins ?? [])];
71
69
  for (const plugin of plugins) {
70
+ if (plugin.id.startsWith(BUILTIN_PERCEPTION_PREFIX) || plugin.id === 'clawclaw.derived-event') {
71
+ throw new Error(`Builtin perceptions are mandatory and cannot be replaced: ${plugin.id}`);
72
+ }
72
73
  const failureMode = typeof plugin.failurePolicy === 'string'
73
74
  ? plugin.failurePolicy
74
75
  : plugin.failurePolicy?.mode;
@@ -87,25 +88,21 @@ export function assembleClawclawPlugins(
87
88
  ?? new ClawclawActionPlugin(client);
88
89
  if (!configuredAction) plugins.push(action);
89
90
 
90
- if (!plugins.some((plugin) => plugin.id === CLAWCLAW_DERIVED_EVENT_PLUGIN_ID)) {
91
- plugins.push(new ClawclawDerivedEventPlugin());
92
- }
91
+ plugins.push(...createSystemPerceptions(options.behaviorEventTypes));
93
92
  const configuredReminders = plugins.find(plugin => plugin.id === CLAWCLAW_REMINDER_PLUGIN_ID);
94
93
  if (configuredReminders && !(configuredReminders instanceof ClawclawReminderPlugin)) {
95
94
  throw new Error(`Runtime Plugin ${CLAWCLAW_REMINDER_PLUGIN_ID} is not a ClawclawReminderPlugin.`);
96
95
  }
97
96
  const selected = new Set(options.perception?.modules ?? DEFAULT_PERCEPTION_MODULES);
98
- const reactionSources = (options.perception?.moduleSources ?? listClawclawPerceptionSources(options.perception?.workspaceDir))
99
- .filter(source => source.reaction && source.apiVersion === 2 && selected.has(source.id));
100
- const moduleSources = (options.perception?.moduleSources ?? listClawclawPerceptionSources(options.perception?.workspaceDir))
101
- .filter(source => source.module && source.apiVersion === 3 && selected.has(source.id));
102
- const moduleOutputTypes = new Set(moduleSources.flatMap(source => source.module?.produces ?? []));
103
- const legacyReadsUnifiedOutput = reactionSources.some(source => source.reaction?.consumes.some(type => moduleOutputTypes.has(type)));
97
+ const availableSources = options.perception?.moduleSources ?? listClawclawPerceptionSources(options.perception?.workspaceDir);
98
+ const explicitlyRequired = (id: string) => plugins.some(plugin => plugin.after?.includes(id));
99
+ const moduleSources = availableSources
100
+ .filter(source => source.module && isSupportedPerceptionSource(source) && selected.has(source.id));
101
+ const reminderSources = availableSources.filter(source => source.reminder && selected.has(source.id));
104
102
  const reminders = configuredReminders as ClawclawReminderPlugin | undefined ?? new ClawclawReminderPlugin({
105
- sources: (options.perception?.moduleSources ?? listClawclawPerceptionSources(options.perception?.workspaceDir))
106
- .filter(source => source.reminder && selected.has(source.id)),
103
+ sources: reminderSources,
107
104
  });
108
- if (!configuredReminders) plugins.push(reminders);
105
+ if (!configuredReminders && (reminderSources.length > 0 || explicitlyRequired(reminders.id))) plugins.push(reminders);
109
106
 
110
107
  const configuredGameQuery = plugins.find((plugin) => plugin.id === CLAWCLAW_GAME_QUERY_PLUGIN_ID);
111
108
  if (configuredGameQuery && !(configuredGameQuery instanceof ClawclawGameQueryPlugin)) {
@@ -120,8 +117,8 @@ export function assembleClawclawPlugins(
120
117
  throw new Error(`Runtime Plugin ${CLAWCLAW_PERCEPTION_PLUGIN_ID} is not a ClawclawPerceptionPlugin.`);
121
118
  }
122
119
  const perception = configuredPerception as ClawclawPerceptionPlugin | undefined
123
- ?? new ClawclawPerceptionPlugin(options.perception);
124
- if (!configuredPerception) plugins.push(perception);
120
+ ?? new ClawclawPerceptionPlugin({ ...options.perception, moduleSources: availableSources });
121
+ if (!configuredPerception && (perception.produces.events.length > 0 || explicitlyRequired(perception.id))) plugins.push(perception);
125
122
 
126
123
  const configuredTrigger = plugins.find((plugin) => plugin.id === CLAWCLAW_TRIGGER_PLUGIN_ID);
127
124
  if (configuredTrigger && !(configuredTrigger instanceof ClawclawTriggerPlugin)) {
@@ -130,24 +127,13 @@ export function assembleClawclawPlugins(
130
127
  const trigger = configuredTrigger as ClawclawTriggerPlugin | undefined
131
128
  ?? new ClawclawTriggerPlugin({
132
129
  after: [],
133
- // A migrated, selected perception replaces the same declared legacy Trigger.
134
- // Its old file/config can remain for rollback without evaluating the rule twice.
135
- triggers: options.triggers?.map(entry => [...reactionSources, ...moduleSources].some(source =>
136
- (source.declaredId ?? source.id) === entry.source.id) ? { ...entry, enabled: false } : entry),
130
+ triggers: options.triggers,
137
131
  diagnosticsEnabled: options.triggerDiagnosticsEnabled,
138
132
  });
139
133
  if (!configuredTrigger) plugins.push(trigger);
140
134
 
141
- const reactions = reactionSources.length ? new ClawclawReactionPlugin({
142
- sources: reactionSources,
143
- after: [CLAWCLAW_DERIVED_EVENT_PLUGIN_ID, CLAWCLAW_REMINDER_PLUGIN_ID,
144
- CLAWCLAW_GAME_QUERY_PLUGIN_ID, CLAWCLAW_PERCEPTION_PLUGIN_ID, CLAWCLAW_TRIGGER_PLUGIN_ID,
145
- ...(legacyReadsUnifiedOutput ? [CLAWCLAW_PERCEPTION_MODULE_PLUGIN_ID] : [])],
146
- behaviors: () => options.behaviorCatalog?.list?.() ?? [],
147
- }) : undefined;
148
- if (reactions) plugins.push(reactions);
149
- const modules = moduleSources.length ? new ClawclawReactionPlugin({ unified: true, sources: moduleSources,
150
- after: [CLAWCLAW_DERIVED_EVENT_PLUGIN_ID],
135
+ const modules = moduleSources.length ? new ClawclawPerceptionModulePlugin({ sources: moduleSources,
136
+ after: [],
151
137
  behaviors: () => options.behaviorCatalog?.list?.() ?? [],
152
138
  }) : undefined;
153
139
  if (modules) plugins.push(modules);
@@ -158,24 +144,26 @@ export function assembleClawclawPlugins(
158
144
  }
159
145
  const behavior = configuredBehavior as ClawclawBehaviorPlugin | undefined
160
146
  ?? new ClawclawBehaviorPlugin({
161
- perceptionActivations: !!reactions || !!modules,
162
147
  catalog: options.behaviorCatalog ?? new ClawclawBehaviorCatalog([]),
163
148
  fallbackBehaviors: options.fallbackBehaviors,
164
149
  after: [
165
150
  CLAWCLAW_ACTION_PLUGIN_ID,
166
- CLAWCLAW_DERIVED_EVENT_PLUGIN_ID,
167
151
  CLAWCLAW_REMINDER_PLUGIN_ID,
168
152
  CLAWCLAW_GAME_QUERY_PLUGIN_ID,
169
153
  CLAWCLAW_PERCEPTION_PLUGIN_ID,
170
154
  CLAWCLAW_TRIGGER_PLUGIN_ID,
171
- ...(reactions ? [CLAWCLAW_REACTION_PLUGIN_ID] : []),
172
155
  ...(modules ? [CLAWCLAW_PERCEPTION_MODULE_PLUGIN_ID] : []),
173
- ],
156
+ ].filter(id => plugins.some(plugin => plugin.id === id)),
174
157
  eventTypes: options.behaviorEventTypes,
175
158
  diagnosticsEnabled: options.behaviorDiagnosticsEnabled,
176
159
  });
177
160
  if (!configuredBehavior) plugins.push(behavior);
178
161
 
162
+ const strategyMemory = new ClawclawStrategyMemory();
163
+ trigger.attachMemory(strategyMemory);
164
+ behavior.attachMemory(strategyMemory);
165
+ modules?.attachMemory(strategyMemory);
166
+ modules?.attachDriver(behavior);
179
167
  trigger.attachDriver(behavior);
180
168
  // Direct Agent/user commands remain ActionPlugin calls this round. Preserve
181
169
  // their original activity timing: mark activity at Action admission, before
@@ -196,5 +184,5 @@ export function assembleClawclawPlugins(
196
184
  ?? new ClawclawMailboxPlugin({ after: eventProducers });
197
185
  if (!configuredMailbox) plugins.push(mailbox);
198
186
 
199
- return { plugins, action, gameQuery, perception, reminders, trigger, behavior, mailbox, reactions, modules };
187
+ return { plugins, action, gameQuery, perception, reminders, trigger, behavior, mailbox, modules };
200
188
  }
@@ -1,482 +1 @@
1
- import type { BehaviorActivationLayer } from '../../../../sdk/behavior-plugin/index.js';
2
-
3
- export type BehaviorActivationSource = 'user' | 'agent' | 'idle' | 'trigger' | 'behavior' | 'system' | 'perception';
4
- export type BehaviorActivationState = 'running' | 'suspended';
5
-
6
- export interface BehaviorActivationSpec {
7
- readonly activationId: string;
8
- readonly behaviorId: string;
9
- readonly layer: BehaviorActivationLayer;
10
- readonly source: BehaviorActivationSource;
11
- readonly input: Readonly<Record<string, unknown>>;
12
- readonly requestedAt: number;
13
- readonly maxRuntimeMs?: number;
14
- readonly deadlineOutcome?: 'completed' | 'failed';
15
- readonly triggerId?: string;
16
- readonly reflexPriority?: number;
17
- }
18
-
19
- export interface BehaviorFallbackBinding {
20
- readonly behaviorId: string;
21
- readonly input: Readonly<Record<string, unknown>>;
22
- }
23
-
24
- export interface BehaviorActivationSnapshot extends BehaviorActivationSpec {
25
- readonly state: BehaviorActivationState;
26
- readonly instanceGeneration: number;
27
- readonly startedAt?: number;
28
- readonly deadlineAt?: number;
29
- }
30
-
31
- export type BehaviorActivationTransitionType =
32
- | 'started'
33
- | 'suspended'
34
- | 'restarted'
35
- | 'completed'
36
- | 'failed'
37
- | 'cancelled'
38
- | 'replaced'
39
- | 'ignored';
40
-
41
- export interface BehaviorActivationTransition {
42
- readonly type: BehaviorActivationTransitionType;
43
- readonly activationId: string;
44
- readonly behaviorId: string;
45
- readonly layer: BehaviorActivationLayer;
46
- readonly at: number;
47
- readonly reason: string;
48
- readonly instanceGeneration: number;
49
- readonly replacedByActivationId?: string;
50
- }
51
-
52
- export interface BehaviorActivationManagerOptions {
53
- readonly idleMs?: number;
54
- readonly createActivationId?: () => string;
55
- }
56
-
57
- interface MutableActivation {
58
- spec: BehaviorActivationSpec;
59
- state: BehaviorActivationState;
60
- instanceGeneration: number;
61
- startedAt?: number;
62
- deadlineAt?: number;
63
- }
64
-
65
- const DEFAULT_IDLE_MS = 500;
66
-
67
- function cloneInput(input: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>> {
68
- return Object.freeze(structuredClone(input));
69
- }
70
-
71
- function validateIdleMs(value: number): number {
72
- if (!Number.isSafeInteger(value) || value < 0 || value > 60_000) {
73
- throw new Error('behavior_idle_ms_invalid');
74
- }
75
- return value;
76
- }
77
-
78
- function validateSpec(spec: BehaviorActivationSpec): BehaviorActivationSpec {
79
- if (!spec.activationId.trim()) throw new Error('behavior_activation_id_invalid');
80
- if (!spec.behaviorId.trim()) throw new Error('behavior_id_invalid');
81
- if (!['long', 'short', 'reflex'].includes(spec.layer)) {
82
- throw new Error('behavior_activation_layer_invalid');
83
- }
84
- if (!Number.isFinite(spec.requestedAt)) throw new Error('behavior_activation_time_invalid');
85
- if (
86
- spec.maxRuntimeMs !== undefined
87
- && (!Number.isSafeInteger(spec.maxRuntimeMs) || spec.maxRuntimeMs <= 0)
88
- ) throw new Error('behavior_activation_runtime_invalid');
89
- if (
90
- spec.deadlineOutcome !== undefined
91
- && spec.deadlineOutcome !== 'completed'
92
- && spec.deadlineOutcome !== 'failed'
93
- ) throw new Error('behavior_activation_deadline_outcome_invalid');
94
- if (spec.deadlineOutcome !== undefined && spec.maxRuntimeMs === undefined) {
95
- throw new Error('behavior_activation_deadline_outcome_without_runtime');
96
- }
97
- if (spec.layer === 'reflex') {
98
- if (!spec.triggerId?.trim()) throw new Error('behavior_reflex_trigger_id_invalid');
99
- if (!Number.isSafeInteger(spec.reflexPriority)) {
100
- throw new Error('behavior_reflex_priority_invalid');
101
- }
102
- } else if (spec.triggerId !== undefined || spec.reflexPriority !== undefined) {
103
- throw new Error('behavior_reflex_fields_not_allowed');
104
- }
105
- return Object.freeze({
106
- ...spec,
107
- input: cloneInput(spec.input),
108
- });
109
- }
110
-
111
- /**
112
- * Owns the bounded physical-control stack: one long, one short, one reflex.
113
- * It never imports modules or submits Actions; Behavior Host applies transitions.
114
- */
115
- export class BehaviorActivationManager {
116
- private idleMs: number;
117
- private readonly createActivationId: () => string;
118
- private fallback?: BehaviorFallbackBinding;
119
- private long?: MutableActivation;
120
- private short?: MutableActivation;
121
- private reflex?: MutableActivation;
122
- private longEligibleAt?: number;
123
- private boundaryActive = false;
124
- private idleGeneration = 0;
125
- private fallbackStartedGeneration = -1;
126
- private sequence = 0;
127
-
128
- constructor(options: BehaviorActivationManagerOptions = {}) {
129
- this.idleMs = validateIdleMs(options.idleMs ?? DEFAULT_IDLE_MS);
130
- this.createActivationId = options.createActivationId
131
- ?? (() => `behavior-${++this.sequence}`);
132
- }
133
-
134
- setIdleMs(idleMs: number, now: number): readonly BehaviorActivationTransition[] {
135
- this.idleMs = validateIdleMs(idleMs);
136
- if (!this.short && !this.reflex && (!this.long || this.long.state === 'suspended')) {
137
- this.longEligibleAt = now + this.idleMs;
138
- }
139
- return this.advance(now);
140
- }
141
-
142
- idleDelayMs(): number {
143
- return this.idleMs;
144
- }
145
-
146
- bindFallback(
147
- binding: BehaviorFallbackBinding | undefined,
148
- now: number,
149
- startImmediately = false,
150
- ): readonly BehaviorActivationTransition[] {
151
- this.fallback = binding
152
- ? Object.freeze({ behaviorId: binding.behaviorId, input: cloneInput(binding.input) })
153
- : undefined;
154
- this.idleGeneration += 1;
155
- this.fallbackStartedGeneration = -1;
156
- if (!this.short && !this.reflex && !this.long) {
157
- this.longEligibleAt = startImmediately ? now : now + this.idleMs;
158
- }
159
- return this.advance(now);
160
- }
161
-
162
- noteAgentActivity(now: number): readonly BehaviorActivationTransition[] {
163
- this.idleGeneration += 1;
164
- this.fallbackStartedGeneration = -1;
165
- this.longEligibleAt = now + this.idleMs;
166
- if (this.long?.spec.source !== 'idle') return [];
167
- const transitions = [this.transition(this.long, 'cancelled', now, 'agent_activity')];
168
- this.long = undefined;
169
- return transitions;
170
- }
171
-
172
- /**
173
- * Enter a game boundary where wandering Behaviors cannot keep authority.
174
- * Finite foreground work is cancelled; an explicit/fallback long Activation
175
- * is retained as configuration but must later restart from a fresh runner.
176
- */
177
- enterBoundary(reason: string, now: number): readonly BehaviorActivationTransition[] {
178
- this.boundaryActive = true;
179
- const transitions: BehaviorActivationTransition[] = [];
180
- if (this.reflex) {
181
- transitions.push(this.transition(this.reflex, 'cancelled', now, reason));
182
- this.reflex = undefined;
183
- }
184
- if (this.short) {
185
- transitions.push(this.transition(this.short, 'cancelled', now, reason));
186
- this.short = undefined;
187
- }
188
- if (this.long?.state === 'running') {
189
- this.long.state = 'suspended';
190
- transitions.push(this.transition(this.long, 'suspended', now, reason));
191
- }
192
- this.longEligibleAt = undefined;
193
- return transitions;
194
- }
195
-
196
- /** Starts the one shared hand-back delay after a meeting/loading boundary. */
197
- leaveBoundary(now: number): readonly BehaviorActivationTransition[] {
198
- this.boundaryActive = false;
199
- if (this.reflex || this.short) return [];
200
- if (this.long?.state === 'suspended' || (!this.long && this.fallback)) {
201
- this.longEligibleAt = now + this.idleMs;
202
- }
203
- return this.advance(now);
204
- }
205
-
206
- /** Ends every Activation in the current game scope while retaining policy bindings. */
207
- clearScope(reason: string, now: number): readonly BehaviorActivationTransition[] {
208
- this.boundaryActive = true;
209
- const transitions: BehaviorActivationTransition[] = [];
210
- for (const layer of ['reflex', 'short', 'long'] as const) {
211
- const activation = this[layer];
212
- if (!activation) continue;
213
- transitions.push(this.transition(activation, 'cancelled', now, reason));
214
- this[layer] = undefined;
215
- }
216
- this.idleGeneration += 1;
217
- this.fallbackStartedGeneration = -1;
218
- this.longEligibleAt = undefined;
219
- return transitions;
220
- }
221
-
222
- startLong(
223
- spec: Omit<BehaviorActivationSpec, 'layer'> & { readonly layer?: 'long' },
224
- now: number,
225
- ): readonly BehaviorActivationTransition[] {
226
- const next = this.activation({ ...spec, layer: 'long' }, now);
227
- const transitions: BehaviorActivationTransition[] = [];
228
- if (this.long) {
229
- transitions.push(this.transition(this.long, 'replaced', now, 'long_replaced', next.spec.activationId));
230
- }
231
- this.long = next;
232
- this.longEligibleAt = undefined;
233
- if (this.boundaryActive || this.short || this.reflex) {
234
- next.state = 'suspended';
235
- transitions.push(this.transition(next, 'suspended', now,
236
- this.boundaryActive ? 'phase_boundary' : 'higher_layer_active'));
237
- } else {
238
- transitions.push(this.transition(next, 'started', now, 'explicit_long_started'));
239
- }
240
- return transitions;
241
- }
242
-
243
- startShort(
244
- spec: Omit<BehaviorActivationSpec, 'layer'> & { readonly layer?: 'short' },
245
- now: number,
246
- ): readonly BehaviorActivationTransition[] {
247
- const next = this.activation({ ...spec, layer: 'short' }, now);
248
- const transitions: BehaviorActivationTransition[] = [];
249
- if (this.short) {
250
- transitions.push(this.transition(this.short, 'replaced', now, 'short_replaced', next.spec.activationId));
251
- }
252
- this.short = next;
253
- if (this.long?.state === 'running') {
254
- this.long.state = 'suspended';
255
- transitions.push(this.transition(this.long, 'suspended', now, 'short_started'));
256
- }
257
- if (this.reflex) {
258
- next.state = 'suspended';
259
- transitions.push(this.transition(next, 'suspended', now, 'reflex_active'));
260
- } else {
261
- transitions.push(this.transition(next, 'started', now, 'short_started'));
262
- }
263
- return transitions;
264
- }
265
-
266
- requestReflex(
267
- spec: Omit<BehaviorActivationSpec, 'layer' | 'source'> & {
268
- readonly layer?: 'reflex';
269
- readonly source?: 'trigger';
270
- },
271
- now: number,
272
- ): readonly BehaviorActivationTransition[] {
273
- const candidate = this.activation({ ...spec, layer: 'reflex', source: 'trigger' }, now);
274
- const active = this.reflex;
275
- if (active) {
276
- const sameTrigger = active.spec.triggerId === candidate.spec.triggerId;
277
- const higher = (candidate.spec.reflexPriority as number) > (active.spec.reflexPriority as number);
278
- if (sameTrigger || !higher) {
279
- return [this.transition(
280
- candidate,
281
- 'ignored',
282
- now,
283
- sameTrigger ? 'reflex_trigger_already_active' : 'reflex_priority_not_higher',
284
- )];
285
- }
286
- }
287
-
288
- const transitions: BehaviorActivationTransition[] = [];
289
- if (active) {
290
- transitions.push(this.transition(active, 'replaced', now, 'higher_priority_reflex', candidate.spec.activationId));
291
- }
292
- this.reflex = candidate;
293
- if (this.short?.state === 'running') {
294
- this.short.state = 'suspended';
295
- transitions.push(this.transition(this.short, 'suspended', now, 'reflex_started'));
296
- }
297
- if (this.long?.state === 'running') {
298
- this.long.state = 'suspended';
299
- transitions.push(this.transition(this.long, 'suspended', now, 'reflex_started'));
300
- }
301
- transitions.push(this.transition(candidate, 'started', now, 'reflex_started'));
302
- return transitions;
303
- }
304
-
305
- finish(
306
- activationId: string,
307
- outcome: 'completed' | 'failed',
308
- reason: string,
309
- now: number,
310
- ): readonly BehaviorActivationTransition[] {
311
- return this.remove(activationId, outcome, reason, now);
312
- }
313
-
314
- cancel(activationId: string, reason: string, now: number): readonly BehaviorActivationTransition[] {
315
- return this.remove(activationId, 'cancelled', reason, now);
316
- }
317
-
318
- advance(now: number): readonly BehaviorActivationTransition[] {
319
- const transitions: BehaviorActivationTransition[] = [];
320
- for (const activation of [this.reflex, this.short, this.long]) {
321
- if (activation?.deadlineAt !== undefined && now >= activation.deadlineAt) {
322
- const outcome = activation.spec.deadlineOutcome ?? 'failed';
323
- transitions.push(...this.remove(
324
- activation.spec.activationId,
325
- outcome,
326
- outcome === 'completed' ? 'duration_elapsed' : 'deadline_elapsed',
327
- now,
328
- ));
329
- }
330
- }
331
- if (this.reflex || this.short) return transitions;
332
- // Direct Agent Actions may rearm the idle delay during a meeting. Keep the
333
- // deadline checks above, but never hand long control back inside a boundary.
334
- if (this.boundaryActive) return transitions;
335
-
336
- if (this.long?.state === 'suspended') {
337
- if (this.longEligibleAt !== undefined && now >= this.longEligibleAt) {
338
- this.longEligibleAt = undefined;
339
- transitions.push(this.restart(this.long, now, 'idle_delay_elapsed'));
340
- }
341
- return transitions;
342
- }
343
-
344
- if (!this.long && this.fallback && this.longEligibleAt !== undefined && now >= this.longEligibleAt) {
345
- if (this.fallbackStartedGeneration === this.idleGeneration) return transitions;
346
- this.fallbackStartedGeneration = this.idleGeneration;
347
- this.longEligibleAt = undefined;
348
- const activation = this.activation({
349
- activationId: this.createActivationId(),
350
- behaviorId: this.fallback.behaviorId,
351
- input: this.fallback.input,
352
- layer: 'long',
353
- source: 'idle',
354
- requestedAt: now,
355
- }, now);
356
- this.long = activation;
357
- transitions.push(this.transition(activation, 'started', now, 'idle_fallback_started'));
358
- }
359
- return transitions;
360
- }
361
-
362
- current(): BehaviorActivationSnapshot | undefined {
363
- return this.snapshotActivation(
364
- this.reflex ?? this.short ?? (this.long?.state === 'running' ? this.long : undefined),
365
- );
366
- }
367
-
368
- snapshot(): Readonly<{
369
- long?: BehaviorActivationSnapshot;
370
- short?: BehaviorActivationSnapshot;
371
- reflex?: BehaviorActivationSnapshot;
372
- fallback?: BehaviorFallbackBinding;
373
- idleMs: number;
374
- longEligibleAt?: number;
375
- }> {
376
- return Object.freeze({
377
- ...(this.long ? { long: this.snapshotActivation(this.long) } : {}),
378
- ...(this.short ? { short: this.snapshotActivation(this.short) } : {}),
379
- ...(this.reflex ? { reflex: this.snapshotActivation(this.reflex) } : {}),
380
- ...(this.fallback ? { fallback: structuredClone(this.fallback) } : {}),
381
- idleMs: this.idleMs,
382
- ...(this.longEligibleAt !== undefined ? { longEligibleAt: this.longEligibleAt } : {}),
383
- });
384
- }
385
-
386
- isCurrent(activationId: string, instanceGeneration: number): boolean {
387
- const current = this.current();
388
- return current?.activationId === activationId
389
- && current.instanceGeneration === instanceGeneration
390
- && current.state === 'running';
391
- }
392
-
393
- private remove(
394
- activationId: string,
395
- outcome: 'completed' | 'failed' | 'cancelled',
396
- reason: string,
397
- now: number,
398
- ): BehaviorActivationTransition[] {
399
- const layer = this.findLayer(activationId);
400
- if (!layer) return [];
401
- const activation = this[layer] as MutableActivation;
402
- const transitions: BehaviorActivationTransition[] = [
403
- this.transition(activation, outcome, now, reason),
404
- ];
405
- this[layer] = undefined;
406
-
407
- if (layer === 'reflex') {
408
- if (this.short) transitions.push(this.restart(this.short, now, 'reflex_ended'));
409
- else this.longEligibleAt = now + this.idleMs;
410
- } else if (layer === 'short') {
411
- if (!this.reflex) this.longEligibleAt = now + this.idleMs;
412
- } else if (!this.short && !this.reflex) {
413
- this.longEligibleAt = now + this.idleMs;
414
- }
415
- return transitions;
416
- }
417
-
418
- private findLayer(activationId: string): BehaviorActivationLayer | undefined {
419
- if (this.reflex?.spec.activationId === activationId) return 'reflex';
420
- if (this.short?.spec.activationId === activationId) return 'short';
421
- if (this.long?.spec.activationId === activationId) return 'long';
422
- return undefined;
423
- }
424
-
425
- private activation(spec: BehaviorActivationSpec, now: number): MutableActivation {
426
- const validated = validateSpec(spec);
427
- return {
428
- spec: validated,
429
- state: 'running',
430
- instanceGeneration: 1,
431
- startedAt: now,
432
- ...(validated.maxRuntimeMs !== undefined
433
- ? { deadlineAt: now + validated.maxRuntimeMs }
434
- : {}),
435
- };
436
- }
437
-
438
- private restart(activation: MutableActivation, now: number, reason: string): BehaviorActivationTransition {
439
- activation.state = 'running';
440
- activation.instanceGeneration += 1;
441
- activation.startedAt = now;
442
- if (activation.spec.maxRuntimeMs !== undefined) {
443
- // A suspended activation keeps its original deadline. Fresh restart is
444
- // about module state and input, not extending its authority lease.
445
- activation.deadlineAt ??= activation.spec.requestedAt + activation.spec.maxRuntimeMs;
446
- }
447
- return this.transition(activation, 'restarted', now, reason);
448
- }
449
-
450
- private transition(
451
- activation: MutableActivation,
452
- type: BehaviorActivationTransitionType,
453
- at: number,
454
- reason: string,
455
- replacedByActivationId?: string,
456
- ): BehaviorActivationTransition {
457
- return Object.freeze({
458
- type,
459
- activationId: activation.spec.activationId,
460
- behaviorId: activation.spec.behaviorId,
461
- layer: activation.spec.layer,
462
- at,
463
- reason,
464
- instanceGeneration: activation.instanceGeneration,
465
- ...(replacedByActivationId ? { replacedByActivationId } : {}),
466
- });
467
- }
468
-
469
- private snapshotActivation(
470
- activation: MutableActivation | undefined,
471
- ): BehaviorActivationSnapshot | undefined {
472
- if (!activation) return undefined;
473
- return Object.freeze({
474
- ...activation.spec,
475
- input: cloneInput(activation.spec.input),
476
- state: activation.state,
477
- instanceGeneration: activation.instanceGeneration,
478
- ...(activation.startedAt !== undefined ? { startedAt: activation.startedAt } : {}),
479
- ...(activation.deadlineAt !== undefined ? { deadlineAt: activation.deadlineAt } : {}),
480
- });
481
- }
482
- }
1
+ export * from '@myclaw163/game-agent-runtime/strategy/activation-manager.js';