@myclaw163/clawclaw-cli 0.8.2 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/configuration.md +1 -1
- package/docs/custom-modules/README.md +2 -2
- package/docs/custom-modules/api-reference.md +17 -3
- package/docs/custom-modules/behavior.md +7 -3
- package/docs/custom-modules/perception.md +26 -3
- package/docs/custom-modules/trigger.md +1 -1
- package/docs/runtime/README.md +1 -1
- package/docs/runtime/behavior-runtime.md +6 -2
- package/docs/runtime/owner-lifecycle-and-observability.md +1 -1
- package/package.json +1 -1
- package/skills/clawclaw/SKILL.md +27 -34
- package/skills/clawclaw/references/COMMANDS.md +1 -1
- package/skills/clawclaw/references/STREAM.md +20 -3
- package/src/commands/load.ts +8 -3
- package/src/commands/trigger.ts +12 -5
- package/src/lib/clawclaw-skill-spectate-guidance.test.ts +21 -0
- package/src/lib/trigger-export.test.ts +80 -0
- package/src/lib/trigger-export.ts +210 -0
- package/src/lib/user-data.test.ts +4 -0
- package/src/lib/user-data.ts +4 -0
- package/src/pipeline/event-format.test.ts +62 -0
- package/src/pipeline/event-format.ts +31 -1
- package/src/pipeline/event-hints.test.ts +4 -0
- package/src/runtime/clawclaw/agent-ability-state.ts +89 -0
- package/src/runtime/clawclaw/agent-event-policy.test.ts +4 -0
- package/src/runtime/clawclaw/agent-event-policy.ts +4 -0
- package/src/runtime/clawclaw/behavior-decision.test.ts +14 -0
- package/src/runtime/clawclaw/behavior-decision.ts +8 -1
- package/src/runtime/clawclaw/behavior-input.test.ts +22 -0
- package/src/runtime/clawclaw/behavior-input.ts +3 -0
- package/src/runtime/clawclaw/owner.test.ts +16 -0
- package/src/runtime/clawclaw/owner.ts +21 -8
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +1 -1
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +246 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +135 -6
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.test.ts +118 -1
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.ts +13 -2
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-rules.ts +156 -0
- package/src/runtime/clawclaw-event-catalog.test.ts +4 -0
- package/src/runtime/clawclaw-event-catalog.ts +4 -0
- package/src/sdk/behavior-plugin/index.ts +10 -0
- package/src/sdk/module-input/index.ts +16 -0
|
@@ -5,6 +5,10 @@ export const CLAWCLAW_MONITOR_EVENT_PRIORITY: Readonly<Record<string, number>> =
|
|
|
5
5
|
kill: 99,
|
|
6
6
|
killed: 99,
|
|
7
7
|
murder_witnessed: 95,
|
|
8
|
+
emergency_countdown_warning: 95,
|
|
9
|
+
'ability.ready': 80,
|
|
10
|
+
'ability.remaining_uses_changed': 80,
|
|
11
|
+
'ability.exhausted': 90,
|
|
8
12
|
'behavior.completed': 90,
|
|
9
13
|
'behavior.failed': 95,
|
|
10
14
|
'behavior.cancelled': 90,
|
|
@@ -26,4 +26,18 @@ describe('normalizeBehaviorDecision', () => {
|
|
|
26
26
|
status: { summary: 'running', lifecycle: 'completed' },
|
|
27
27
|
}, { finite: false })).toThrow('behavior_status_field_unknown:lifecycle');
|
|
28
28
|
});
|
|
29
|
+
|
|
30
|
+
it('accepts one declared delegation and rejects hidden delegation fields', () => {
|
|
31
|
+
expect(normalizeBehaviorDecision({
|
|
32
|
+
delegate: { behavior: 'report_corpse', input: { seat: 3 } },
|
|
33
|
+
})).toMatchObject({
|
|
34
|
+
delegate: { behavior: 'report_corpse', input: { seat: 3 } },
|
|
35
|
+
});
|
|
36
|
+
expect(() => normalizeBehaviorDecision({
|
|
37
|
+
delegate: { behavior: 'report_corpse', input: {}, layer: 'reflex' },
|
|
38
|
+
})).toThrow('behavior_delegate_field_unknown:layer');
|
|
39
|
+
expect(() => normalizeBehaviorDecision({
|
|
40
|
+
delegate: { behavior: '', input: {} },
|
|
41
|
+
})).toThrow('behavior_delegate_id_invalid');
|
|
42
|
+
});
|
|
29
43
|
});
|
|
@@ -3,9 +3,10 @@ import type {
|
|
|
3
3
|
FiniteBehaviorDecision,
|
|
4
4
|
} from '../../sdk/behavior-plugin/index.js';
|
|
5
5
|
|
|
6
|
-
const DECISION_KEYS = new Set(['actions', 'status', 'events', 'finish']);
|
|
6
|
+
const DECISION_KEYS = new Set(['actions', 'status', 'events', 'delegate', 'finish']);
|
|
7
7
|
const STATUS_KEYS = new Set(['summary', 'activity', 'room', 'task', 'targetPlayer', 'data']);
|
|
8
8
|
const EVENT_KEYS = new Set(['type', 'payload', 'occurredAt']);
|
|
9
|
+
const DELEGATE_KEYS = new Set(['behavior', 'input']);
|
|
9
10
|
const FINISH_KEYS = new Set(['outcome', 'reason', 'summary']);
|
|
10
11
|
|
|
11
12
|
function objectRecord(value: unknown, code: string): Record<string, unknown> {
|
|
@@ -67,6 +68,12 @@ export function normalizeBehaviorDecision(
|
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
}
|
|
71
|
+
if (source.delegate !== undefined) {
|
|
72
|
+
const delegate = objectRecord(source.delegate, 'behavior_delegate_invalid');
|
|
73
|
+
rejectUnknownFields(delegate, DELEGATE_KEYS, 'behavior_delegate_field_unknown');
|
|
74
|
+
nonEmptyString(delegate.behavior, 'behavior_delegate_id_invalid');
|
|
75
|
+
if (delegate.input !== undefined) objectRecord(delegate.input, 'behavior_delegate_input_invalid');
|
|
76
|
+
}
|
|
70
77
|
if (source.finish !== undefined) {
|
|
71
78
|
const finish = objectRecord(source.finish, 'behavior_finish_invalid');
|
|
72
79
|
rejectUnknownFields(finish, FINISH_KEYS, 'behavior_finish_field_unknown');
|
|
@@ -135,6 +135,28 @@ describe('createBehaviorInput', () => {
|
|
|
135
135
|
expect(createBehaviorInput(value, {})).toBeNull();
|
|
136
136
|
});
|
|
137
137
|
|
|
138
|
+
it('deep-freezes an optional delegated Behavior continuation', () => {
|
|
139
|
+
const result = createBehaviorInput(runtimeInput(), {}, {
|
|
140
|
+
delegatedBehavior: {
|
|
141
|
+
activationId: 'short-1',
|
|
142
|
+
behavior: 'report_corpse',
|
|
143
|
+
outcome: 'completed',
|
|
144
|
+
reason: 'corpse_reported',
|
|
145
|
+
summary: 'reported',
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
expect(result?.continuation).toEqual({
|
|
150
|
+
delegatedBehavior: expect.objectContaining({
|
|
151
|
+
activationId: 'short-1',
|
|
152
|
+
behavior: 'report_corpse',
|
|
153
|
+
outcome: 'completed',
|
|
154
|
+
}),
|
|
155
|
+
});
|
|
156
|
+
expect(Object.isFrozen(result?.continuation)).toBe(true);
|
|
157
|
+
expect(Object.isFrozen(result?.continuation?.delegatedBehavior)).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
|
|
138
160
|
it('gives Behavior and Perception the same canonical Game State for one frame', () => {
|
|
139
161
|
const value = runtimeInput();
|
|
140
162
|
const behavior = createBehaviorInput(value, {});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
BehaviorContinuation,
|
|
2
3
|
BehaviorInput,
|
|
3
4
|
GameEvent,
|
|
4
5
|
GameState,
|
|
@@ -47,6 +48,7 @@ export function createBehaviorInput<
|
|
|
47
48
|
>(
|
|
48
49
|
input: RuntimeEpochPluginInput<Record<string, any>>,
|
|
49
50
|
behaviorInput: TInput,
|
|
51
|
+
continuation?: Readonly<BehaviorContinuation>,
|
|
50
52
|
): BehaviorInput<TInput> | null {
|
|
51
53
|
const runtimeState = input.epoch.state;
|
|
52
54
|
if (!isClawclawRuntimeState(runtimeState) || runtimeState.game_snapshot.status !== 'fresh') {
|
|
@@ -66,5 +68,6 @@ export function createBehaviorInput<
|
|
|
66
68
|
gameState,
|
|
67
69
|
events: behaviorEvents(input),
|
|
68
70
|
input: structuredClone(behaviorInput),
|
|
71
|
+
...(continuation === undefined ? {} : { continuation: structuredClone(continuation) }),
|
|
69
72
|
});
|
|
70
73
|
}
|
|
@@ -2,9 +2,14 @@ import { existsSync, mkdtempSync, readFileSync, rmSync, statSync } from 'node:fs
|
|
|
2
2
|
import { tmpdir } from 'node:os';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import {
|
|
6
|
+
presentCanonicalEventForCclEvents,
|
|
7
|
+
presentCanonicalEventForDesktop,
|
|
8
|
+
} from '../../pipeline/canonical-event-presentation.js';
|
|
5
9
|
import { gameStartRuntimePath } from '../owner-control.js';
|
|
6
10
|
import {
|
|
7
11
|
channelOwnerDeliveryPolicy,
|
|
12
|
+
ownerAgentEventPresenter,
|
|
8
13
|
performOwnerAgentAction,
|
|
9
14
|
publishOwnerRuntimePointer,
|
|
10
15
|
roleFallbackBehaviors,
|
|
@@ -56,6 +61,17 @@ describe('Channel owner delivery policy', () => {
|
|
|
56
61
|
});
|
|
57
62
|
});
|
|
58
63
|
|
|
64
|
+
describe('Owner Agent Event presentation', () => {
|
|
65
|
+
it('sanitizes both Desktop IPC delivery and Desktop-host ccl events', () => {
|
|
66
|
+
expect(ownerAgentEventPresenter('desktop_ipc', true))
|
|
67
|
+
.toBe(presentCanonicalEventForDesktop);
|
|
68
|
+
expect(ownerAgentEventPresenter('ccl_events', true))
|
|
69
|
+
.toBe(presentCanonicalEventForDesktop);
|
|
70
|
+
expect(ownerAgentEventPresenter('ccl_events', false))
|
|
71
|
+
.toBe(presentCanonicalEventForCclEvents);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
59
75
|
describe('owner direct Agent Action handoff', () => {
|
|
60
76
|
it('does not disturb Behavior control when a delayed Action belongs to a stale scope', async () => {
|
|
61
77
|
const runtime = {
|
|
@@ -84,6 +84,7 @@ import {
|
|
|
84
84
|
CCL_EVENTS_MAILBOX_CONSUMER_ID,
|
|
85
85
|
DESKTOP_IPC_MAILBOX_CONSUMER_ID,
|
|
86
86
|
MONITOR_MAILBOX_CONSUMER_ID,
|
|
87
|
+
type AgentMailboxCoordinatorOptions,
|
|
87
88
|
type ClawclawMailboxDriver,
|
|
88
89
|
type DesktopEventSocketConfig,
|
|
89
90
|
} from './plugins/mailbox/index.js';
|
|
@@ -94,9 +95,11 @@ import {
|
|
|
94
95
|
syncOfficialBehaviors,
|
|
95
96
|
} from '../../lib/behavior-export.js';
|
|
96
97
|
import { syncOfficialPerceptions } from '../../lib/perception-export.js';
|
|
97
|
-
import { getUserDataTriggersDir } from '../../lib/user-data.js';
|
|
98
98
|
import {
|
|
99
|
-
|
|
99
|
+
listWorkspaceClawclawTriggerSources,
|
|
100
|
+
syncOfficialTriggers,
|
|
101
|
+
} from '../../lib/trigger-export.js';
|
|
102
|
+
import {
|
|
100
103
|
type ClawclawTriggerRegistration,
|
|
101
104
|
type ResolvedClawclawTriggerSource,
|
|
102
105
|
} from './plugins/trigger/index.js';
|
|
@@ -128,6 +131,17 @@ export interface ChannelOwnerDeliveryPolicy {
|
|
|
128
131
|
drainOnNaturalCompletion: boolean;
|
|
129
132
|
}
|
|
130
133
|
|
|
134
|
+
export type OwnerAgentEventDelivery = 'ccl_events' | 'desktop_ipc';
|
|
135
|
+
|
|
136
|
+
export function ownerAgentEventPresenter(
|
|
137
|
+
delivery: OwnerAgentEventDelivery,
|
|
138
|
+
desktopHost: boolean,
|
|
139
|
+
): AgentMailboxCoordinatorOptions['presentEvent'] {
|
|
140
|
+
return delivery === 'desktop_ipc' || desktopHost
|
|
141
|
+
? presentCanonicalEventForDesktop
|
|
142
|
+
: presentCanonicalEventForCclEvents;
|
|
143
|
+
}
|
|
144
|
+
|
|
131
145
|
export function channelOwnerDeliveryPolicy(
|
|
132
146
|
channelDelivery: boolean | undefined,
|
|
133
147
|
localStopRequested = false,
|
|
@@ -698,6 +712,7 @@ export async function runClawclawOwner(
|
|
|
698
712
|
const workspaceDir = getWorkspaceDir();
|
|
699
713
|
await syncOfficialBehaviors({ workspaceDir });
|
|
700
714
|
await syncOfficialPerceptions({ workspaceDir });
|
|
715
|
+
await syncOfficialTriggers({ workspaceDir });
|
|
701
716
|
const perceptionConfig = readWorkspacePerceptionConfig();
|
|
702
717
|
const gameplayConfig = readGameplayConfig(workspaceDir);
|
|
703
718
|
const configurationWarning: ConfigurationWarning = (code, details) => {
|
|
@@ -720,7 +735,7 @@ export async function runClawclawOwner(
|
|
|
720
735
|
behaviorCatalog,
|
|
721
736
|
configurationWarning,
|
|
722
737
|
);
|
|
723
|
-
const triggerSources =
|
|
738
|
+
const triggerSources = listWorkspaceClawclawTriggerSources(workspaceDir);
|
|
724
739
|
const triggers = triggerRegistrations(
|
|
725
740
|
triggerSources,
|
|
726
741
|
gameplayConfig,
|
|
@@ -760,9 +775,7 @@ export async function runClawclawOwner(
|
|
|
760
775
|
cclEventsMailbox = new AgentMailboxCoordinator({
|
|
761
776
|
repository: mailboxRepository,
|
|
762
777
|
readyView: () => runtime!.readReadyView(0, 1),
|
|
763
|
-
presentEvent: options.desktopEvents
|
|
764
|
-
? presentCanonicalEventForDesktop
|
|
765
|
-
: presentCanonicalEventForCclEvents,
|
|
778
|
+
presentEvent: ownerAgentEventPresenter('ccl_events', !!options.desktopEvents),
|
|
766
779
|
compactState: compactStateForEvents,
|
|
767
780
|
eventFilter: clawclawEventRequiresDetails,
|
|
768
781
|
delegatedMonitorConsumerId: MONITOR_MAILBOX_CONSUMER_ID,
|
|
@@ -788,7 +801,7 @@ export async function runClawclawOwner(
|
|
|
788
801
|
desktopMailbox = new AgentMailboxCoordinator({
|
|
789
802
|
repository: mailboxRepository,
|
|
790
803
|
readyView: () => runtime!.readReadyView(0, 1),
|
|
791
|
-
presentEvent:
|
|
804
|
+
presentEvent: ownerAgentEventPresenter('desktop_ipc', true),
|
|
792
805
|
compactState: compactStateForEvents,
|
|
793
806
|
consumerId: DESKTOP_IPC_MAILBOX_CONSUMER_ID,
|
|
794
807
|
diagnostic: (name, attributes) => runtime?.diagnostic(name, attributes),
|
|
@@ -1252,7 +1265,7 @@ export async function runClawclawOwner(
|
|
|
1252
1265
|
return { ok: false, type: 'trigger_configure', error: 'stale_runtime_scope' };
|
|
1253
1266
|
}
|
|
1254
1267
|
const latest = readGameplayConfig(workspaceDir);
|
|
1255
|
-
const currentTriggerSources =
|
|
1268
|
+
const currentTriggerSources = listWorkspaceClawclawTriggerSources(workspaceDir);
|
|
1256
1269
|
if (!currentTriggerSources.some((source) => source.id === request.trigger)) {
|
|
1257
1270
|
return { ok: false, type: 'trigger_configure', error: 'trigger_source_unavailable' };
|
|
1258
1271
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { BehaviorActivationLayer } from '../../../../sdk/behavior-plugin/index.js';
|
|
2
2
|
|
|
3
|
-
export type BehaviorActivationSource = 'user' | 'agent' | 'idle' | 'trigger' | 'system';
|
|
3
|
+
export type BehaviorActivationSource = 'user' | 'agent' | 'idle' | 'trigger' | 'behavior' | 'system';
|
|
4
4
|
export type BehaviorActivationState = 'running' | 'suspended';
|
|
5
5
|
|
|
6
6
|
export interface BehaviorActivationSpec {
|
|
@@ -313,6 +313,81 @@ class FinishingShortBehavior extends ClawclawBehavior {
|
|
|
313
313
|
}
|
|
314
314
|
}
|
|
315
315
|
|
|
316
|
+
class DelegatingLongBehavior extends ClawclawBehavior {
|
|
317
|
+
static override readonly manifest = manifest('delegating-long');
|
|
318
|
+
static frames: BehaviorInput[] = [];
|
|
319
|
+
private continued = false;
|
|
320
|
+
|
|
321
|
+
override decide(input: Readonly<BehaviorInput>): BehaviorDecision {
|
|
322
|
+
DelegatingLongBehavior.frames.push(input as BehaviorInput);
|
|
323
|
+
if (input.continuation) {
|
|
324
|
+
this.continued = true;
|
|
325
|
+
return { status: { summary: `continued:${input.continuation.delegatedBehavior.outcome}` } };
|
|
326
|
+
}
|
|
327
|
+
if (this.continued) return { status: { summary: 'continued' } };
|
|
328
|
+
return { delegate: { behavior: 'delegated-short', input: {} } };
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
class DelegatedShortBehavior extends ClawclawBehavior {
|
|
333
|
+
static override readonly manifest = manifest('delegated-short', {
|
|
334
|
+
runtime: { defaultMs: 10_000, deadlineOutcome: 'failed' },
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
override decide(): BehaviorDecision {
|
|
338
|
+
return {
|
|
339
|
+
finish: { outcome: 'completed', reason: 'delegated_done', summary: 'child summary' },
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
class InvalidDelegatingShortBehavior extends ClawclawBehavior {
|
|
345
|
+
static override readonly manifest = manifest('invalid-delegating-short', {
|
|
346
|
+
runtime: { defaultMs: 10_000, deadlineOutcome: 'failed' },
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
override decide(): BehaviorDecision {
|
|
350
|
+
return { delegate: { behavior: 'delegated-short' } };
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
class OutcomeDelegatingLongBehavior extends ClawclawBehavior {
|
|
355
|
+
static override readonly manifest = manifest('outcome-delegating-long');
|
|
356
|
+
static target = 'delegated-short';
|
|
357
|
+
static results: NonNullable<BehaviorInput['continuation']>['delegatedBehavior'][] = [];
|
|
358
|
+
private continued = false;
|
|
359
|
+
|
|
360
|
+
override decide(input: Readonly<BehaviorInput>): BehaviorDecision {
|
|
361
|
+
if (input.continuation) {
|
|
362
|
+
this.continued = true;
|
|
363
|
+
OutcomeDelegatingLongBehavior.results.push(input.continuation.delegatedBehavior);
|
|
364
|
+
return { status: { summary: `outcome:${input.continuation.delegatedBehavior.outcome}` } };
|
|
365
|
+
}
|
|
366
|
+
if (this.continued) return { status: { summary: 'continued' } };
|
|
367
|
+
return { delegate: { behavior: OutcomeDelegatingLongBehavior.target } };
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
class FailingDelegatedShortBehavior extends ClawclawBehavior {
|
|
372
|
+
static override readonly manifest = manifest('failing-delegated-short', {
|
|
373
|
+
runtime: { defaultMs: 10_000, deadlineOutcome: 'failed' },
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
override decide(): BehaviorDecision {
|
|
377
|
+
return { finish: { outcome: 'failed', reason: 'child_failed', summary: 'failed summary' } };
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
class WaitingDelegatedShortBehavior extends ClawclawBehavior {
|
|
382
|
+
static override readonly manifest = manifest('waiting-delegated-short', {
|
|
383
|
+
runtime: { defaultMs: 10_000, deadlineOutcome: 'failed' },
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
override decide(): BehaviorDecision {
|
|
387
|
+
return { status: { summary: 'waiting' } };
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
316
391
|
class ReportBehavior extends ClawclawBehavior {
|
|
317
392
|
static override readonly manifest = manifest('report', {
|
|
318
393
|
runtime: { defaultMs: 10_000, deadlineOutcome: 'failed' },
|
|
@@ -383,6 +458,16 @@ class ActionFinishBehavior extends ClawclawBehavior {
|
|
|
383
458
|
}
|
|
384
459
|
}
|
|
385
460
|
|
|
461
|
+
class ActionDelegatingBehavior extends ClawclawBehavior {
|
|
462
|
+
static override readonly manifest = manifest('action-delegating');
|
|
463
|
+
override decide(): BehaviorDecision {
|
|
464
|
+
return {
|
|
465
|
+
actions: [{ action: 'move', target_x: 1, target_y: 2 }],
|
|
466
|
+
delegate: { behavior: 'delegated-short' },
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
386
471
|
class SlowBehavior extends ClawclawBehavior {
|
|
387
472
|
static override readonly manifest = manifest('slow');
|
|
388
473
|
static onDecide: (() => void) | undefined;
|
|
@@ -598,6 +683,146 @@ describe('Clawclaw unified Behavior plugin', () => {
|
|
|
598
683
|
]);
|
|
599
684
|
});
|
|
600
685
|
|
|
686
|
+
it('lets a long delegate one short and receives its terminal result once after fresh restart', async () => {
|
|
687
|
+
DelegatingLongBehavior.frames = [];
|
|
688
|
+
let id = 0;
|
|
689
|
+
const plugin = new ClawclawBehaviorPlugin({
|
|
690
|
+
catalog: catalog([DelegatingLongBehavior, DelegatedShortBehavior]),
|
|
691
|
+
driver: driver(),
|
|
692
|
+
createActivationId: () => `delegation-${++id}`,
|
|
693
|
+
now: () => 0,
|
|
694
|
+
});
|
|
695
|
+
const ctx = context();
|
|
696
|
+
await plugin.start(ctx);
|
|
697
|
+
await plugin.startLong('delegating-long', {}, { source: 'agent' });
|
|
698
|
+
|
|
699
|
+
const delegated = await step(plugin, input(1, { nowMs: 100 }), ctx);
|
|
700
|
+
expect(plugin.status().current).toMatchObject({
|
|
701
|
+
activationId: 'delegation-2',
|
|
702
|
+
behaviorId: 'delegated-short',
|
|
703
|
+
layer: 'short',
|
|
704
|
+
source: 'behavior',
|
|
705
|
+
});
|
|
706
|
+
expect(delegated.events).toEqual(expect.arrayContaining([
|
|
707
|
+
expect.objectContaining({ type: 'behavior.suspended' }),
|
|
708
|
+
expect.objectContaining({ type: 'behavior.started' }),
|
|
709
|
+
]));
|
|
710
|
+
|
|
711
|
+
await step(plugin, input(2, { nowMs: 200 }), ctx);
|
|
712
|
+
await step(plugin, input(3, { nowMs: 699 }), ctx);
|
|
713
|
+
await step(plugin, input(4, { nowMs: 700 }), ctx);
|
|
714
|
+
const resumed = await step(plugin, input(5, { nowMs: 701 }), ctx);
|
|
715
|
+
expect(resumed.states).toEqual([
|
|
716
|
+
expect.objectContaining({
|
|
717
|
+
value: expect.objectContaining({ status: { summary: 'continued:completed' } }),
|
|
718
|
+
}),
|
|
719
|
+
]);
|
|
720
|
+
expect(DelegatingLongBehavior.frames.at(-1)?.continuation).toEqual({
|
|
721
|
+
delegatedBehavior: {
|
|
722
|
+
activationId: 'delegation-2',
|
|
723
|
+
behavior: 'delegated-short',
|
|
724
|
+
outcome: 'completed',
|
|
725
|
+
reason: 'delegated_done',
|
|
726
|
+
summary: 'child summary',
|
|
727
|
+
},
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
await step(plugin, input(6, { nowMs: 702 }), ctx);
|
|
731
|
+
expect(DelegatingLongBehavior.frames.at(-1)?.continuation).toBeUndefined();
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
it('fails closed when a short Behavior attempts recursive delegation', async () => {
|
|
735
|
+
let id = 0;
|
|
736
|
+
const plugin = new ClawclawBehaviorPlugin({
|
|
737
|
+
catalog: catalog([InvalidDelegatingShortBehavior, DelegatedShortBehavior]),
|
|
738
|
+
driver: driver(),
|
|
739
|
+
createActivationId: () => `invalid-delegation-${++id}`,
|
|
740
|
+
now: () => 0,
|
|
741
|
+
});
|
|
742
|
+
const ctx = context();
|
|
743
|
+
await plugin.start(ctx);
|
|
744
|
+
await plugin.startShort('invalid-delegating-short', {}, { source: 'agent' });
|
|
745
|
+
|
|
746
|
+
const result = await step(plugin, input(1), ctx);
|
|
747
|
+
expect(result.events).toEqual(expect.arrayContaining([
|
|
748
|
+
expect.objectContaining({
|
|
749
|
+
type: 'behavior.failed',
|
|
750
|
+
payload: expect.objectContaining({ reason: 'behavior_delegate_layer_invalid' }),
|
|
751
|
+
}),
|
|
752
|
+
]));
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
it('returns failed, cancelled, and replaced delegated outcomes to the original long', async () => {
|
|
756
|
+
const cases = [
|
|
757
|
+
{
|
|
758
|
+
outcome: 'failed',
|
|
759
|
+
target: 'failing-delegated-short',
|
|
760
|
+
prepare: async (_plugin: ClawclawBehaviorPlugin, _childId: string, _setNow: (value: number) => void) => {},
|
|
761
|
+
reason: 'child_failed',
|
|
762
|
+
summary: 'failed summary',
|
|
763
|
+
},
|
|
764
|
+
{
|
|
765
|
+
outcome: 'cancelled',
|
|
766
|
+
target: 'waiting-delegated-short',
|
|
767
|
+
prepare: async (plugin: ClawclawBehaviorPlugin, childId: string, setNow: (value: number) => void) => {
|
|
768
|
+
setNow(200);
|
|
769
|
+
await plugin.cancel(childId, 'cancelled_by_test');
|
|
770
|
+
},
|
|
771
|
+
reason: 'cancelled_by_test',
|
|
772
|
+
},
|
|
773
|
+
{
|
|
774
|
+
outcome: 'replaced',
|
|
775
|
+
target: 'waiting-delegated-short',
|
|
776
|
+
prepare: async (plugin: ClawclawBehaviorPlugin, _childId: string, setNow: (value: number) => void) => {
|
|
777
|
+
setNow(200);
|
|
778
|
+
await plugin.startShort('delegated-short', {}, { source: 'agent' });
|
|
779
|
+
},
|
|
780
|
+
reason: 'short_replaced',
|
|
781
|
+
},
|
|
782
|
+
] as const;
|
|
783
|
+
|
|
784
|
+
for (const [index, testCase] of cases.entries()) {
|
|
785
|
+
let id = 0;
|
|
786
|
+
let now = 100;
|
|
787
|
+
OutcomeDelegatingLongBehavior.target = testCase.target;
|
|
788
|
+
OutcomeDelegatingLongBehavior.results = [];
|
|
789
|
+
const plugin = new ClawclawBehaviorPlugin({
|
|
790
|
+
catalog: catalog([
|
|
791
|
+
OutcomeDelegatingLongBehavior,
|
|
792
|
+
DelegatedShortBehavior,
|
|
793
|
+
FailingDelegatedShortBehavior,
|
|
794
|
+
WaitingDelegatedShortBehavior,
|
|
795
|
+
]),
|
|
796
|
+
driver: driver(),
|
|
797
|
+
createActivationId: () => `terminal-${index}-${++id}`,
|
|
798
|
+
now: () => now,
|
|
799
|
+
});
|
|
800
|
+
const ctx = context();
|
|
801
|
+
await plugin.start(ctx);
|
|
802
|
+
await plugin.startLong('outcome-delegating-long', {}, { source: 'agent' });
|
|
803
|
+
await step(plugin, input(1, { nowMs: 100 }), ctx);
|
|
804
|
+
const childId = plugin.status().current!.activationId;
|
|
805
|
+
await testCase.prepare(plugin, childId, (value) => { now = value; });
|
|
806
|
+
if (testCase.outcome !== 'cancelled') {
|
|
807
|
+
await step(plugin, input(2, { nowMs: 200 }), ctx);
|
|
808
|
+
}
|
|
809
|
+
await step(plugin, input(3, { nowMs: 699 }), ctx);
|
|
810
|
+
await step(plugin, input(4, { nowMs: 700 }), ctx);
|
|
811
|
+
await step(plugin, input(5, { nowMs: 701 }), ctx);
|
|
812
|
+
|
|
813
|
+
expect(OutcomeDelegatingLongBehavior.results).toEqual([
|
|
814
|
+
expect.objectContaining({
|
|
815
|
+
activationId: childId,
|
|
816
|
+
behavior: testCase.target,
|
|
817
|
+
outcome: testCase.outcome,
|
|
818
|
+
reason: testCase.reason,
|
|
819
|
+
...('summary' in testCase ? { summary: testCase.summary } : {}),
|
|
820
|
+
}),
|
|
821
|
+
]);
|
|
822
|
+
await plugin.stop('shutdown');
|
|
823
|
+
}
|
|
824
|
+
});
|
|
825
|
+
|
|
601
826
|
it('lets a report short observe meeting authority before cancelling the wandering boundary', async () => {
|
|
602
827
|
const actions: Array<Record<string, any>> = [];
|
|
603
828
|
const plugin = new ClawclawBehaviorPlugin({
|
|
@@ -781,6 +1006,27 @@ describe('Clawclaw unified Behavior plugin', () => {
|
|
|
781
1006
|
]));
|
|
782
1007
|
});
|
|
783
1008
|
|
|
1009
|
+
it('rejects actions and delegation in the same Decision before submitting an Action', async () => {
|
|
1010
|
+
const actions: Array<Record<string, any>> = [];
|
|
1011
|
+
const plugin = new ClawclawBehaviorPlugin({
|
|
1012
|
+
catalog: catalog([ActionDelegatingBehavior, DelegatedShortBehavior]),
|
|
1013
|
+
driver: driver(),
|
|
1014
|
+
createActivationId: () => 'action-delegating-1',
|
|
1015
|
+
now: () => 0,
|
|
1016
|
+
});
|
|
1017
|
+
const ctx = context(actions);
|
|
1018
|
+
await plugin.start(ctx);
|
|
1019
|
+
await plugin.startLong('action-delegating', {}, { source: 'agent' });
|
|
1020
|
+
const result = await step(plugin, input(1), ctx);
|
|
1021
|
+
expect(actions).toEqual([]);
|
|
1022
|
+
expect(result.events).toEqual(expect.arrayContaining([
|
|
1023
|
+
expect.objectContaining({
|
|
1024
|
+
type: 'behavior.failed',
|
|
1025
|
+
payload: expect.objectContaining({ reason: 'behavior_delegate_with_actions_invalid' }),
|
|
1026
|
+
}),
|
|
1027
|
+
]));
|
|
1028
|
+
});
|
|
1029
|
+
|
|
784
1030
|
it('interrupts a slow serial Decision before admitting a higher reflex', async () => {
|
|
785
1031
|
let entered!: () => void;
|
|
786
1032
|
const deciding = new Promise<void>((resolve) => { entered = resolve; });
|