@myclaw163/clawclaw-cli 0.10.17 → 0.10.19

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 (34) hide show
  1. package/cli-runtime/commands/behavior.js +7 -0
  2. package/cli-runtime/commands/prepare.js +17 -15
  3. package/cli-runtime/lib/agent-workspace-runtime.js +37 -15
  4. package/cli-runtime/lib/user-data.js +3 -1
  5. package/cli-runtime/runtime/clawclaw/behavior-start-control.js +4 -2
  6. package/cli-runtime/runtime/clawclaw/behavior-trigger.js +17 -1
  7. package/cli-runtime/runtime/clawclaw/owner.js +12 -4
  8. package/cli-runtime/runtime/clawclaw/plugins/behavior/behavior-catalog.js +6 -0
  9. package/cli-runtime/runtime/clawclaw/plugins/behavior/behavior-plugin.js +49 -4
  10. package/cli-runtime/runtime/clawclaw/plugins/perception/perception-module-plugin.js +27 -2
  11. package/dist/strategy-worker/runtime/clawclaw/plugins/behavior/sdk-sdk.js +1 -0
  12. package/docs/custom-modules/README.md +1 -1
  13. package/docs/custom-modules/api-reference.md +2 -2
  14. package/docs/custom-modules/behavior.md +6 -4
  15. package/docs/user-guide/README.md +1 -1
  16. package/docs/user-guide/api-reference.md +2 -2
  17. package/docs/user-guide/behavior.md +6 -4
  18. package/package.json +1 -1
  19. package/skills/clawclaw/SKILL.md +2 -2
  20. package/skills/clawclaw/references/BEHAVIORS.md +2 -0
  21. package/skills/clawclaw-creator/SKILL.md +1 -1
  22. package/skills/clawclaw-creator/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +6 -4
  23. package/skills/clawclaw-creator/references/CUSTOM-MODULE-API.md +2 -2
  24. package/skills/clawclaw-creator/references/CUSTOM-MODULES.md +1 -1
  25. package/src/commands/behavior.ts +7 -0
  26. package/src/commands/prepare.ts +16 -17
  27. package/src/lib/agent-workspace-runtime.ts +41 -25
  28. package/src/lib/user-data.ts +4 -1
  29. package/src/runtime/clawclaw/behavior-start-control.ts +3 -1
  30. package/src/runtime/clawclaw/behavior-trigger.ts +21 -1
  31. package/src/runtime/clawclaw/owner.ts +9 -4
  32. package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +8 -0
  33. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +60 -7
  34. package/src/runtime/clawclaw/plugins/perception/perception-module-plugin.ts +31 -4
@@ -1,7 +1,7 @@
1
1
  import { isNotificationTurn } from '../../notification-turn.js';
2
2
  import { ClawclawStrategyMemory } from '../../strategy-memory.js';
3
3
  import { resolveDetailCapture } from '../../../observability/detail-capture.js';
4
- import type { BehaviorContinuation, BehaviorInput, ClawclawResolvedResourceMetadata, GameAction } from '../../../../sdk/behavior-plugin/index.js';
4
+ import type { BehaviorContinuation, BehaviorDecision, BehaviorInput, ClawclawResolvedResourceMetadata, GameAction } from '../../../../sdk/behavior-plugin/index.js';
5
5
  import { resolveClawclawResourceMetadata } from '../../../../sdk/behavior-plugin/index.js';
6
6
  import type { RuntimeEpochCatchUp } from '../../../framework/plugins/contracts.js';
7
7
  import { createBehaviorInput } from '../../behavior-input.js';
@@ -18,7 +18,7 @@ import type { PreparedClawclawBehavior } from './behavior-normalization.js';
18
18
  import { ClawclawBehaviorRunner } from './behavior-runner.js';
19
19
  import type { ClawclawBehaviorControlDriver } from './control-driver.js';
20
20
  import { StrategyHost } from '@myclaw163/game-agent-runtime/strategy/strategy-host.js';
21
- import type { PreparedStrategy, StrategyAdapter, StrategyFrame } from '@myclaw163/game-agent-runtime/strategy/contracts.js';
21
+ import type { PreparedStrategy, ReflexRequest, ReflexResult, StrategyAdapter, StrategyFrame } from '@myclaw163/game-agent-runtime/strategy/contracts.js';
22
22
  import type { BehaviorDecisionFrame } from '../../../../sdk/module-input/index.js';
23
23
  import type { ClawclawBehaviorObservationSnapshot } from '../../behavior-observation-memory.js';
24
24
  export const CLAWCLAW_BEHAVIOR_PLUGIN_ID = 'clawclaw.behavior';
@@ -59,7 +59,7 @@ function isSuccessfulBackendStrategyReport(value: unknown): boolean {
59
59
  }
60
60
  export interface ClawclawBehaviorPluginOptions {
61
61
  readonly memory?: ClawclawStrategyMemory;
62
- readonly catalog: Pick<ClawclawBehaviorCatalog, 'has' | 'prepare'> & Partial<Pick<ClawclawBehaviorCatalog, 'prepareForActivation'>>;
62
+ readonly catalog: Pick<ClawclawBehaviorCatalog, 'has' | 'prepare'> & Partial<Pick<ClawclawBehaviorCatalog, 'prepareForActivation' | 'activationError'>>;
63
63
  readonly fallbackBehaviors?: ClawclawRoleFallbackBehaviors;
64
64
  readonly manager?: BehaviorActivationManager;
65
65
  readonly driver?: ClawclawBehaviorControlDriver;
@@ -87,7 +87,16 @@ function sdkFrame(frame: StrategyFrame): Readonly<BehaviorInput> { return frame.
87
87
  function preparedValue(prepared: PreparedStrategy): PreparedClawclawBehavior {
88
88
  return { ...(prepared.value as PreparedClawclawBehavior), ...(prepared.module ? { module: prepared.module as PreparedClawclawBehavior['module'] } : {}) };
89
89
  }
90
- function createAdapter(deferredDisplay: () => ClawclawResolvedResourceMetadata | undefined, useAdaptiveWorkers: boolean): StrategyAdapter {
90
+ function createAdapter(deferredDisplay: () => ClawclawResolvedResourceMetadata | undefined, useAdaptiveWorkers: boolean,
91
+ assertStartAllowed: (id: string) => void): StrategyAdapter {
92
+ // The CCL adapter receives normalized decisions from both execution modes.
93
+ const checkDelegation = (decision: BehaviorDecision) => {
94
+ if (decision.delegate) {
95
+ const target = decision.delegate;
96
+ assertStartAllowed('strategy' in target ? target.strategy : target.behavior);
97
+ }
98
+ return decision;
99
+ };
91
100
  const observations = new ClawclawBehaviorObservationMemory();
92
101
  return {
93
102
  input: (input, parameters, continuation, history) => {
@@ -121,8 +130,8 @@ function createAdapter(deferredDisplay: () => ClawclawResolvedResourceMetadata |
121
130
  nowMs: input.nowMs, tick: Number(input.gameState.tick ?? input.epochId), cycle: input.cycle, events: input.events }, active)) });
122
131
  return { execution: runner.execution, everyTicks: runner.everyTicks, prepare: () => runner.prepare(),
123
132
  stop: reason => runner.stop(reason), diagnosticSnapshot: () => runner.diagnosticSnapshot(),
124
- decideSerial: (frame, signal, observation) => runner.decideSerial(sdkFrame(frame), signal, observation as ClawclawBehaviorObservationSnapshot),
125
- decideParallel: (frame, signal, observation) => runner.decideParallel(sdkFrame(frame), signal, observation as ClawclawBehaviorObservationSnapshot) };
133
+ decideSerial: (frame, signal, observation) => runner.decideSerial(sdkFrame(frame), signal, observation as ClawclawBehaviorObservationSnapshot).then(checkDelegation),
134
+ decideParallel: (frame, signal, observation) => runner.decideParallel(sdkFrame(frame), signal, observation as ClawclawBehaviorObservationSnapshot).then(checkDelegation) };
126
135
  },
127
136
  action: (action, epoch, safety, generation, tick) => ({ ...normalizeClawclawBehaviorAction(action as GameAction, epoch, safety, generation, tick) }),
128
137
  display: prepared => deferredDisplay() ?? (prepared
@@ -156,11 +165,20 @@ interface DeferredShortControl {
156
165
 
157
166
  export class ClawclawBehaviorPlugin extends StrategyHost {
158
167
  private readonly deferredShort: { control?: DeferredShortControl };
168
+ private readonly catalogAccess: ClawclawBehaviorPluginOptions['catalog'];
169
+ private readonly assertStartAllowed: (id: string) => void;
159
170
 
160
171
  constructor(options: ClawclawBehaviorPluginOptions) {
172
+ const assertStartAllowed = (id: string) => {
173
+ const error = options.catalog.activationError?.(id);
174
+ if (error) throw new Error(error);
175
+ };
176
+ for (const binding of Object.values(options.fallbackBehaviors ?? {})) {
177
+ if (binding) assertStartAllowed(binding.behaviorId);
178
+ }
161
179
  const deferredShort: { control?: DeferredShortControl } = {};
162
180
  super({ ...options, id: CLAWCLAW_BEHAVIOR_PLUGIN_ID, stateTopic: CLAWCLAW_BEHAVIOR_STATE_TOPIC,
163
- actionCapability: CLAWCLAW_ACTION_CAPABILITY, adapter: createAdapter(() => deferredShort.control?.display(), !!options.catalog.prepareForActivation), captureActions: resolveDetailCapture().actions,
181
+ actionCapability: CLAWCLAW_ACTION_CAPABILITY, adapter: createAdapter(() => deferredShort.control?.display(), !!options.catalog.prepareForActivation, assertStartAllowed), captureActions: resolveDetailCapture().actions,
164
182
  catalog: { has: id => options.catalog.has(id), prepare: async (id, input) => {
165
183
  const prepared = await options.catalog.prepare(id, input);
166
184
  return { ...prepared, value: prepared };
@@ -169,6 +187,41 @@ export class ClawclawBehaviorPlugin extends StrategyHost {
169
187
  return { ...prepared, value: prepared, discardWorker: () => prepared.worker?.dispose() };
170
188
  } } : {}) }, after: options.after ?? [CLAWCLAW_ACTION_PLUGIN_ID, CLAWCLAW_GAME_QUERY_PLUGIN_ID, CLAWCLAW_PERCEPTION_PLUGIN_ID, CLAWCLAW_TRIGGER_PLUGIN_ID] });
171
189
  this.deferredShort = deferredShort;
190
+ this.catalogAccess = options.catalog;
191
+ this.assertStartAllowed = assertStartAllowed;
192
+ }
193
+
194
+ startError(id: string): string | undefined {
195
+ return this.catalogAccess.activationError?.(id);
196
+ }
197
+
198
+ override async startLong(id: string, input: Readonly<Record<string, unknown>>, options: StartBehaviorOptions) {
199
+ this.assertStartAllowed(id);
200
+ return super.startLong(id, input, options);
201
+ }
202
+
203
+ override async startShort(id: string, input: Readonly<Record<string, unknown>>, options: StartBehaviorOptions) {
204
+ this.assertStartAllowed(id);
205
+ return super.startShort(id, input, options);
206
+ }
207
+
208
+ override async bindFallback(id: string, input: Readonly<Record<string, unknown>>) {
209
+ this.assertStartAllowed(id);
210
+ return super.bindFallback(id, input);
211
+ }
212
+
213
+ override async configureFallbackBehaviors(bindings: ClawclawRoleFallbackBehaviors, currentRole?: string) {
214
+ for (const binding of Object.values(bindings)) {
215
+ if (binding) this.assertStartAllowed(binding.behaviorId);
216
+ }
217
+ return super.configureFallbackBehaviors(bindings, currentRole);
218
+ }
219
+
220
+ override async requestReflex(request: Readonly<ReflexRequest>, guard?: Readonly<{ isCurrent(): boolean }>): Promise<ReflexResult> {
221
+ if (this.catalogAccess.activationError?.(request.behavior, request)) {
222
+ return { accepted: false, reason: 'activation_ineligible' };
223
+ }
224
+ return super.requestReflex(request, guard);
172
225
  }
173
226
 
174
227
  attachDeferredShortControl(control: DeferredShortControl): void {
@@ -4,7 +4,7 @@ import { preparePerceptionNotification } from './report-content.js';
4
4
  import { isNotificationTurn } from '../../notification-turn.js';
5
5
  import type { RuntimeEpochPluginInput, RuntimeEpochPluginContext } from '../../../framework/plugins/index.js';
6
6
  import { normalizeBehaviorInput } from '../../behavior-input-fields.js';
7
- import { type ReactionBehaviorCandidate } from '../../reaction-behavior-resolution.js';
7
+ import { resolveReactionBehavior, type ReactionBehaviorCandidate } from '../../reaction-behavior-resolution.js';
8
8
  import { clawclawComputationGameState, isClawclawRuntimeState } from '../../runtime-state.js';
9
9
  import { projectTaskChoiceEventPayload } from '../../task-choice-projection.js';
10
10
  import type { ResolvedClawclawPerceptionSource } from './perception-source.js';
@@ -22,7 +22,7 @@ export interface PerceptionModulePluginOptions {
22
22
  /** Unified execution, including verified v2 Reaction consumers, at the source/system observation boundary. */
23
23
  sources: readonly ResolvedClawclawPerceptionSource[];
24
24
  after: readonly string[];
25
- behaviors?: () => readonly ReactionBehaviorCandidate[];
25
+ behaviors?: () => readonly (ReactionBehaviorCandidate & { readonly activationMode?: 'standard' | 'automatic_only' })[];
26
26
  runnerFactory?: (source: ResolvedClawclawPerceptionSource) => Pick<PerceptionModuleRunner, 'start' | 'evaluate' | 'stop'>;
27
27
  }
28
28
  interface GameCheckpoint {
@@ -41,7 +41,7 @@ interface GameCheckpoint {
41
41
  }>;
42
42
  }
43
43
  function sourceValue(source: PerceptionSource): ResolvedClawclawPerceptionSource { return source.value as ResolvedClawclawPerceptionSource; }
44
- function createAdapter(options: PerceptionModulePluginOptions): PerceptionAdapter {
44
+ function createAdapter(options: PerceptionModulePluginOptions, blockedBindings: ReadonlyMap<string, ReadonlySet<string>>): PerceptionAdapter {
45
45
  function prepare(input: RuntimeEpochPluginInput<Record<string, any>>, previous: unknown, context?: RuntimeEpochPluginContext<Record<string, any>>) {
46
46
  const checkpoint = structuredClone(previous ?? { modules: {} }) as GameCheckpoint;
47
47
  // The shipped host checkpoint already has this baseline under flat names.
@@ -103,6 +103,11 @@ function createAdapter(options: PerceptionModulePluginOptions): PerceptionAdapte
103
103
  const { input, game, frame, facts, eventStates, boundary, common } = prepared.value as Prepared;
104
104
  const events = facts.filter(event => source.module.consumes?.includes(event.type));
105
105
  const comparison = comparePerceptionTasks(baseline as PerceptionTaskBaseline | undefined, input, boundary);
106
+ const blocked = blockedBindings.get(source.id);
107
+ if (blocked) {
108
+ bindings = { ...bindings, availableBindings: bindings.availableBindings.filter(key => !blocked.has(key)),
109
+ enabledBindings: bindings.enabledBindings.filter(key => !blocked.has(key)) };
110
+ }
106
111
  const embedded = sourceValue(source).embedded;
107
112
  const declaredTargets = embedded && bindings.availableBindings.length
108
113
  ? Object.values(embedded.declaredManifest.behaviors ?? {}).map(binding => binding.name) : [];
@@ -123,10 +128,32 @@ function createAdapter(options: PerceptionModulePluginOptions): PerceptionAdapte
123
128
  };
124
129
  }
125
130
  export class ClawclawPerceptionModulePlugin extends PerceptionHost {
131
+ private readonly blockedBindings: ReadonlyMap<string, ReadonlySet<string>>;
132
+
126
133
  constructor(options: PerceptionModulePluginOptions) {
127
- super({ id: CLAWCLAW_PERCEPTION_MODULE_PLUGIN_ID, after: options.after, behaviors: options.behaviors, adapter: createAdapter(options),
134
+ // The Owner pins this catalog for the session. Keep external observers and
135
+ // their memory bindings alive even when only the embedded perception may start the strategy.
136
+ const blockedBindings = new Map<string, Set<string>>();
137
+ const candidates = options.behaviors?.() ?? [];
138
+ const automatic = new Set(candidates.filter(entry => entry.activationMode === 'automatic_only').map(entry => entry.id));
139
+ for (const source of options.sources) {
140
+ if (source.embedded) continue;
141
+ const blocked = Object.entries(source.module?.behaviors ?? {}).filter(([, binding]) => {
142
+ const id = resolveReactionBehavior(binding, candidates);
143
+ return id !== undefined && automatic.has(id);
144
+ }).map(([key]) => key);
145
+ if (blocked.length) blockedBindings.set(source.id, new Set(blocked));
146
+ }
147
+ super({ id: CLAWCLAW_PERCEPTION_MODULE_PLUGIN_ID, after: options.after, behaviors: options.behaviors, adapter: createAdapter(options, blockedBindings),
128
148
  sources: options.sources.filter(source => source.module).map(source => ({ id: source.id, name: source.name, module: source.module!, value: source })) });
149
+ this.blockedBindings = blockedBindings;
129
150
  }
151
+
152
+ override bindings(id?: string) {
153
+ return super.bindings(id).map(binding => this.blockedBindings.get(binding.perception_id)?.has(binding.binding)
154
+ ? { ...binding, enabled: false, available: false, reason: 'strategy_automatic_only' } : binding);
155
+ }
156
+
130
157
  override localizedModuleDescriptors(locale: string): ClawclawResolvedResourceMetadata[] {
131
158
  const descriptors = super.localizedModuleDescriptors(locale) as ClawclawResolvedResourceMetadata[];
132
159
  return descriptors.filter(item => !item.id.startsWith('strategy:'));