@myclaw163/game-agent-runtime 0.1.0 → 0.1.2
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/dist/framework/events/types.d.ts +2 -0
- package/dist/framework/modules/runtime-module-loader-hooks.mjs +13 -0
- package/dist/mailbox/mailbox-plugin.d.ts +2 -0
- package/dist/mailbox/mailbox-plugin.js +18 -1
- package/dist/perception/contracts.d.ts +10 -3
- package/dist/perception/perception-host.d.ts +8 -0
- package/dist/perception/perception-host.js +77 -34
- package/dist/strategy/activation-manager.d.ts +2 -0
- package/dist/strategy/activation-manager.js +11 -0
- package/dist/strategy/contracts.d.ts +2 -0
- package/dist/strategy/strategy-host.d.ts +14 -1
- package/dist/strategy/strategy-host.js +265 -77
- package/dist/strategy/strategy-script-runner.d.ts +3 -1
- package/dist/strategy/strategy-script-runner.js +5 -1
- package/dist/strategy/strategy-worker-host.d.ts +1 -0
- package/dist/strategy/strategy-worker-host.js +7 -1
- package/dist/strategy/thread-strategy-executor.d.ts +19 -1
- package/dist/strategy/thread-strategy-executor.js +78 -18
- package/package.json +1 -1
|
@@ -7,6 +7,8 @@ export interface EventNotificationContent {
|
|
|
7
7
|
event: Record<string, unknown>;
|
|
8
8
|
monitorText: string;
|
|
9
9
|
desktopEvent?: Record<string, unknown>;
|
|
10
|
+
/** Producer-prepared projections for additional output adapters. */
|
|
11
|
+
views?: Record<string, unknown>;
|
|
10
12
|
}
|
|
11
13
|
export type EventDeliveryClass = 'monitor_and_events' | 'internal_only';
|
|
12
14
|
/** Delivery metadata may be resolved before Ready; factual identity/payload never change. */
|
|
@@ -96,6 +96,19 @@ export function resolve(specifier, context, nextResolve) {
|
|
|
96
96
|
url: target,
|
|
97
97
|
});
|
|
98
98
|
}
|
|
99
|
+
// A retained Strategy Worker evaluates validation and execution in separate
|
|
100
|
+
// module graphs. Carry the validation key through the local dependency tree.
|
|
101
|
+
if (context.parentURL?.includes('gar_strategy_prepare=1') && (specifier.startsWith('./') || specifier.startsWith('../'))) {
|
|
102
|
+
const parent = new URL(context.parentURL);
|
|
103
|
+
parent.searchParams.delete('gar_strategy_prepare');
|
|
104
|
+
const result = resolve(specifier, { ...context, parentURL: parent.href }, nextResolve);
|
|
105
|
+
const target = new URL(result.url);
|
|
106
|
+
if ([...esmModuleRoots].some(root => target.href.startsWith(root))) {
|
|
107
|
+
target.searchParams.set('gar_strategy_prepare', '1');
|
|
108
|
+
return { ...result, url: target.href, shortCircuit: true };
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
99
112
|
// The CLI registers tsx before this hook. Letting tsx resolve a queried
|
|
100
113
|
// user .ts/.tsx entry can percent-encode the query into a CommonJS virtual
|
|
101
114
|
// URL, which then turns public SDK imports into unresolved require() calls.
|
|
@@ -16,6 +16,7 @@ export type MailboxAdapterDescriptor = {
|
|
|
16
16
|
export interface MailboxDriver {
|
|
17
17
|
/** Optional for internal components; public transport adapters should declare it. */
|
|
18
18
|
readonly adapter?: MailboxAdapterDescriptor;
|
|
19
|
+
afterReady?(ready: RuntimeEpochReady): void;
|
|
19
20
|
observeEvents?(events: readonly CanonicalEvent[]): void;
|
|
20
21
|
flush(): Promise<void>;
|
|
21
22
|
/** Optional finite handoff opportunity before any sibling releases shared delivery state. */
|
|
@@ -26,6 +27,7 @@ export interface MailboxDriver {
|
|
|
26
27
|
export declare class CompositeMailboxDriver implements MailboxDriver {
|
|
27
28
|
private readonly drivers;
|
|
28
29
|
constructor(drivers: readonly MailboxDriver[]);
|
|
30
|
+
afterReady(ready: RuntimeEpochReady): void;
|
|
29
31
|
observeEvents(events: readonly CanonicalEvent[]): void;
|
|
30
32
|
flush(): Promise<void>;
|
|
31
33
|
stop(): Promise<void>;
|
|
@@ -24,6 +24,20 @@ export class CompositeMailboxDriver {
|
|
|
24
24
|
consumers.set(adapter.consumerId, adapter.id);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
+
afterReady(ready) {
|
|
28
|
+
let firstReliableError;
|
|
29
|
+
for (const driver of this.drivers) {
|
|
30
|
+
try {
|
|
31
|
+
driver.afterReady?.(ready);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (driver.adapter?.acknowledgement !== 'none')
|
|
35
|
+
firstReliableError ??= error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (firstReliableError)
|
|
39
|
+
throw firstReliableError;
|
|
40
|
+
}
|
|
27
41
|
observeEvents(events) {
|
|
28
42
|
let firstReliableError;
|
|
29
43
|
for (const driver of this.drivers) {
|
|
@@ -89,7 +103,10 @@ export class MailboxPlugin {
|
|
|
89
103
|
};
|
|
90
104
|
}
|
|
91
105
|
afterReady(ready) {
|
|
92
|
-
if (
|
|
106
|
+
if (this.stopped)
|
|
107
|
+
return;
|
|
108
|
+
this.driver?.afterReady?.(ready);
|
|
109
|
+
if (ready.events.length)
|
|
93
110
|
this.driver?.observeEvents?.(ready.events);
|
|
94
111
|
}
|
|
95
112
|
async flush(_reason, _context) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RuntimeEpochPluginInput, RuntimeEventProposal } from '../framework/plugins/contracts.js';
|
|
1
|
+
import type { RuntimeEpochPluginInput, RuntimeEpochPluginContext, RuntimeEventProposal } from '../framework/plugins/contracts.js';
|
|
2
2
|
import type { EventNotificationContent } from '../framework/events/types.js';
|
|
3
3
|
import type { StrategyMemory } from '../memory/contracts.js';
|
|
4
4
|
export interface PerceptionBinding {
|
|
@@ -35,6 +35,10 @@ export interface PerceptionResult {
|
|
|
35
35
|
readonly key: string;
|
|
36
36
|
readonly wakeAgent?: boolean;
|
|
37
37
|
readonly payload: Readonly<Record<string, unknown>>;
|
|
38
|
+
readonly occurredAt?: string;
|
|
39
|
+
}[];
|
|
40
|
+
readonly forwards?: readonly {
|
|
41
|
+
readonly eventId: string;
|
|
38
42
|
}[];
|
|
39
43
|
readonly activations?: readonly (PerceptionActivation | {
|
|
40
44
|
readonly strategy: string;
|
|
@@ -58,11 +62,13 @@ export interface PerceptionBoundary {
|
|
|
58
62
|
readonly state: unknown;
|
|
59
63
|
readonly identity?: string;
|
|
60
64
|
readonly resetModules: boolean;
|
|
65
|
+
readonly resetBindings?: boolean;
|
|
61
66
|
readonly resetScope: boolean;
|
|
62
67
|
readonly notificationOnly: boolean;
|
|
63
68
|
readonly nowMs: number;
|
|
64
69
|
readonly reflexAllowed: boolean;
|
|
65
70
|
readonly value: unknown;
|
|
71
|
+
readonly consumeEventsThroughCommitSeq?: number;
|
|
66
72
|
}
|
|
67
73
|
export interface PerceptionBindingInput {
|
|
68
74
|
readonly availableBehaviors: readonly string[];
|
|
@@ -72,7 +78,8 @@ export interface PerceptionBindingInput {
|
|
|
72
78
|
export interface PerceptionAdapter {
|
|
73
79
|
reset(): void;
|
|
74
80
|
recover(checkpoint: unknown): unknown;
|
|
75
|
-
prepare(input: RuntimeEpochPluginInput<Record<string, any>>, previous: unknown): PerceptionBoundary;
|
|
81
|
+
prepare(input: RuntimeEpochPluginInput<Record<string, any>>, previous: unknown, context?: RuntimeEpochPluginContext<Record<string, any>>): PerceptionBoundary;
|
|
82
|
+
forward?(eventId: string, boundary: PerceptionBoundary, source: PerceptionSource): RuntimeEventProposal;
|
|
76
83
|
input(source: PerceptionSource, boundary: PerceptionBoundary, inputState: unknown, state: unknown, config: Readonly<Record<string, unknown>>, bindings: PerceptionBindingInput): {
|
|
77
84
|
invocation: PerceptionInvocation;
|
|
78
85
|
inputState: unknown;
|
|
@@ -81,5 +88,5 @@ export interface PerceptionAdapter {
|
|
|
81
88
|
createRunner(source: PerceptionSource, bindMemory: (input: PerceptionInvocation, active: () => boolean) => StrategyMemory): PerceptionRunner;
|
|
82
89
|
configure(source: PerceptionSource, value: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>>;
|
|
83
90
|
describe(source: PerceptionSource, locale: string): unknown;
|
|
84
|
-
notification(event: RuntimeEventProposal, boundary: PerceptionBoundary): EventNotificationContent;
|
|
91
|
+
notification(event: RuntimeEventProposal, boundary: PerceptionBoundary, source?: PerceptionSource): EventNotificationContent;
|
|
85
92
|
}
|
|
@@ -10,6 +10,12 @@ export interface PerceptionHostOptions {
|
|
|
10
10
|
after: readonly string[];
|
|
11
11
|
behaviors?: () => readonly ReactionBehaviorCandidate[];
|
|
12
12
|
adapter: PerceptionAdapter;
|
|
13
|
+
notificationRelay?: RuntimeEpochPlugin['notificationRelay'];
|
|
14
|
+
/** Registration-owned budgets; script manifests cannot change these. */
|
|
15
|
+
resultLimits?: {
|
|
16
|
+
events?: number;
|
|
17
|
+
bytes?: number;
|
|
18
|
+
};
|
|
13
19
|
}
|
|
14
20
|
/** Observing and notifications remain active independently of fixed Reflex admission. */
|
|
15
21
|
export declare class PerceptionHost implements RuntimeEpochPlugin<Record<string, any>> {
|
|
@@ -27,9 +33,11 @@ export declare class PerceptionHost implements RuntimeEpochPlugin<Record<string,
|
|
|
27
33
|
readonly produces: {
|
|
28
34
|
events: readonly string[];
|
|
29
35
|
};
|
|
36
|
+
readonly notificationRelay?: RuntimeEpochPlugin['notificationRelay'];
|
|
30
37
|
private readonly sources;
|
|
31
38
|
private readonly runners;
|
|
32
39
|
private readonly failed;
|
|
40
|
+
private readonly reflexWarnings;
|
|
33
41
|
private checkpoint;
|
|
34
42
|
constructor(options: PerceptionHostOptions);
|
|
35
43
|
private driver?;
|
|
@@ -16,13 +16,16 @@ export class PerceptionHost {
|
|
|
16
16
|
after;
|
|
17
17
|
consumes;
|
|
18
18
|
produces;
|
|
19
|
+
notificationRelay;
|
|
19
20
|
sources;
|
|
20
21
|
runners = new Map();
|
|
21
22
|
failed = new Set();
|
|
23
|
+
reflexWarnings = [];
|
|
22
24
|
checkpoint = { modules: {}, failures: [] };
|
|
23
25
|
constructor(options) {
|
|
24
26
|
this.options = options;
|
|
25
27
|
this.id = options.id;
|
|
28
|
+
this.notificationRelay = options.notificationRelay;
|
|
26
29
|
this.sources = options.sources.filter(s => s.module).map(source => ({ ...source, module: normalizePerceptionManifestNames(source.module) })).sort((a, b) => a.id.localeCompare(b.id));
|
|
27
30
|
this.after = options.after;
|
|
28
31
|
this.consumes = { events: [...new Set(this.sources.flatMap(source => [...(source.module.consumes ?? [])]))] };
|
|
@@ -116,6 +119,7 @@ export class PerceptionHost {
|
|
|
116
119
|
this.watches.clear();
|
|
117
120
|
this.configs.clear();
|
|
118
121
|
this.checkpoint = { modules: {}, failures: [] };
|
|
122
|
+
this.reflexWarnings.length = 0;
|
|
119
123
|
this.failed.clear();
|
|
120
124
|
this.options.adapter.reset();
|
|
121
125
|
await Promise.all(this.sources.map(async (source, index) => {
|
|
@@ -158,26 +162,29 @@ export class PerceptionHost {
|
|
|
158
162
|
return this.sources.filter(source => !this.failed.has(source.id)).map(source => this.options.adapter.describe(source, locale));
|
|
159
163
|
}
|
|
160
164
|
async update(input, context) {
|
|
161
|
-
const prepared = this.options.adapter.prepare(input, this.checkpoint.adapterState);
|
|
165
|
+
const prepared = this.options.adapter.prepare(input, this.checkpoint.adapterState, context);
|
|
162
166
|
if (prepared.notificationOnly)
|
|
163
167
|
return { outcome: 'noop', reason: 'notification_only' };
|
|
164
168
|
this.scope = { ...input.epoch.scope };
|
|
165
169
|
if (prepared.resetScope)
|
|
166
170
|
this.checkpoint = { modules: {}, failures: [] };
|
|
167
|
-
|
|
168
|
-
|
|
171
|
+
for (const state of Object.values(this.checkpoint.modules)) {
|
|
172
|
+
if (prepared.resetModules)
|
|
169
173
|
state.value = undefined;
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
174
|
+
if (!(prepared.resetBindings ?? prepared.resetModules))
|
|
175
|
+
continue;
|
|
176
|
+
for (const binding of Object.values(state.bindings)) {
|
|
177
|
+
binding.armed = true;
|
|
178
|
+
binding.lastAcceptedAt = undefined;
|
|
179
|
+
binding.failedAttempt = undefined;
|
|
175
180
|
}
|
|
181
|
+
}
|
|
176
182
|
this.checkpoint.adapterState = prepared.state;
|
|
177
183
|
const nowMs = prepared.nowMs;
|
|
178
184
|
this.lastNow = nowMs;
|
|
179
185
|
const candidates = this.options.behaviors?.() ?? [];
|
|
180
|
-
const events =
|
|
186
|
+
const events = this.reflexWarnings.splice(0)
|
|
187
|
+
.filter(warning => warning.current()).map(warning => warning.event);
|
|
181
188
|
const requests = [];
|
|
182
189
|
const results = await mapConcurrent(this.sources, 4, async (source) => {
|
|
183
190
|
const events = [];
|
|
@@ -210,10 +217,16 @@ export class PerceptionHost {
|
|
|
210
217
|
this.validate(result, source);
|
|
211
218
|
state.value = structuredClone(result.state);
|
|
212
219
|
state.inputState = module.inputState;
|
|
213
|
-
for (const event of result.events ?? [])
|
|
214
|
-
|
|
220
|
+
for (const event of result.events ?? []) {
|
|
221
|
+
const proposal = { type: event.type,
|
|
215
222
|
dedupeKey: JSON.stringify([source.id, event.key]), dedupeScope: 'segment', wakeAgent: event.wakeAgent ?? true,
|
|
216
|
-
payload: { ...event.payload }, occurredAt: new Date(nowMs).toISOString() }
|
|
223
|
+
payload: { ...event.payload }, occurredAt: event.occurredAt ?? new Date(nowMs).toISOString() };
|
|
224
|
+
events.push({ ...proposal, notificationContent: this.options.adapter.notification(proposal, prepared, source) });
|
|
225
|
+
}
|
|
226
|
+
for (const forward of result.forwards ?? []) {
|
|
227
|
+
const proposal = this.options.adapter.forward(forward.eventId, prepared, source);
|
|
228
|
+
events.push({ ...proposal, notificationContent: this.options.adapter.notification(proposal, prepared, source) });
|
|
229
|
+
}
|
|
217
230
|
const matched = new Map((result.activations ?? []).map(activation => [activation.behavior, activation]));
|
|
218
231
|
for (const [key, binding] of Object.entries(source.module.behaviors ?? {})) {
|
|
219
232
|
const current = state.bindings[key] ??= { armed: true, warningSequence: 0 };
|
|
@@ -268,6 +281,8 @@ export class PerceptionHost {
|
|
|
268
281
|
const attempt = JSON.stringify([behavior, binding.input ?? {}, activation.input ?? {}]);
|
|
269
282
|
if (state.failedAttempt === attempt)
|
|
270
283
|
continue;
|
|
284
|
+
const current = () => !this.stopped && !context.signal.aborted && generation === this.generation
|
|
285
|
+
&& !!behavior && this.watches.has(behavior) && this.sameScope(input.epoch.scope);
|
|
271
286
|
let result = { accepted: false, reason: 'behavior_not_loaded' };
|
|
272
287
|
if (behavior && this.watches.has(behavior)) {
|
|
273
288
|
try {
|
|
@@ -276,53 +291,81 @@ export class PerceptionHost {
|
|
|
276
291
|
epochId: input.epoch.epochSeq, configGeneration: generation, behavior,
|
|
277
292
|
behaviorInput: { ...binding.input, ...activation.input }, reflexPriority: binding.reflexPriority ?? 0,
|
|
278
293
|
maxRuntimeMs: binding.maxRuntimeMs ?? 10_000,
|
|
279
|
-
reason: activation.reason ?? `perception:${source.id}:${activation.behavior}` }, { isCurrent:
|
|
280
|
-
&& this.watches.has(behavior) && this.sameScope(input.epoch.scope) })
|
|
294
|
+
reason: activation.reason ?? `perception:${source.id}:${activation.behavior}` }, { isCurrent: current })
|
|
281
295
|
?? { accepted: false, reason: 'activation_unavailable' };
|
|
282
296
|
}
|
|
283
297
|
catch {
|
|
284
298
|
result = { accepted: false, reason: 'activation_unavailable' };
|
|
285
299
|
}
|
|
286
300
|
}
|
|
287
|
-
|
|
301
|
+
const accept = (acceptedAt) => {
|
|
288
302
|
state.armed = false;
|
|
289
|
-
state.lastAcceptedAt =
|
|
303
|
+
state.lastAcceptedAt = acceptedAt;
|
|
290
304
|
state.warning = undefined;
|
|
291
305
|
state.failedAttempt = undefined;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const reason = result.reason ?? 'activation_unavailable';
|
|
306
|
+
};
|
|
307
|
+
const reject = (reason) => {
|
|
295
308
|
state.failedAttempt = attempt;
|
|
296
|
-
if (state.warning
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
309
|
+
if (state.warning === reason)
|
|
310
|
+
return undefined;
|
|
311
|
+
state.warning = reason;
|
|
312
|
+
state.warningSequence++;
|
|
313
|
+
return { type: 'perception.behavior_unavailable', wakeAgent: true,
|
|
314
|
+
dedupeKey: JSON.stringify([prepared.identity, source.id, activation.behavior, state.warningSequence]),
|
|
315
|
+
payload: { module_id: source.id, binding: activation.behavior, behavior_name: binding.name,
|
|
316
|
+
...(binding.resourceId ? { preferred_resource_id: binding.resourceId } : {}),
|
|
317
|
+
...(behavior ? { behavior_id: behavior } : {}), reason,
|
|
318
|
+
text: `感知“${source.name}”未能触发策略“${binding.name}”(${reason})。已跳过本次触发,请 Agent 判断处理;其他感知和策略继续运行。` } };
|
|
319
|
+
};
|
|
320
|
+
if (result.accepted && result.ready) {
|
|
321
|
+
void result.ready.then(outcome => {
|
|
322
|
+
if (!current())
|
|
323
|
+
return;
|
|
324
|
+
if (outcome === 'ready')
|
|
325
|
+
accept(this.lastNow ?? nowMs);
|
|
326
|
+
if (outcome === 'invalid') {
|
|
327
|
+
const event = reject('binding_invalid');
|
|
328
|
+
if (event)
|
|
329
|
+
this.reflexWarnings.push({ event, current });
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
else if (result.accepted) {
|
|
334
|
+
accept(nowMs);
|
|
335
|
+
}
|
|
336
|
+
else if (!['same_trigger_active', 'reflex_priority_not_higher', 'stale_request', 'activation_ineligible'].includes(result.reason)) {
|
|
337
|
+
const event = reject(result.reason);
|
|
338
|
+
if (event)
|
|
339
|
+
events.push(event);
|
|
306
340
|
}
|
|
307
341
|
}
|
|
308
342
|
return { outcome: 'processed', events: events.map(event => ({ ...event,
|
|
309
|
-
notificationContent: this.options.adapter.notification(event, prepared) })), checkpoint: structuredClone(this.checkpoint),
|
|
310
|
-
...(
|
|
343
|
+
notificationContent: event.notificationContent ?? this.options.adapter.notification(event, prepared) })), checkpoint: structuredClone(this.checkpoint),
|
|
344
|
+
...(prepared.consumeEventsThroughCommitSeq !== undefined
|
|
345
|
+
? { consumeEventsThroughCommitSeq: prepared.consumeEventsThroughCommitSeq }
|
|
346
|
+
: input.pendingEvents.length ? { consumeEventsThroughCommitSeq: input.pendingEvents.at(-1).commitSeq } : {}) };
|
|
311
347
|
}
|
|
312
348
|
validate(result, source) {
|
|
313
|
-
if (!record(result) || Object.keys(result).some(key => !['state', 'events', 'activations'].includes(key))
|
|
349
|
+
if (!record(result) || Object.keys(result).some(key => !['state', 'events', 'forwards', 'activations'].includes(key))
|
|
314
350
|
|| result.events !== undefined && !Array.isArray(result.events)
|
|
315
351
|
|| result.activations !== undefined && !Array.isArray(result.activations)
|
|
316
|
-
||
|
|
317
|
-
||
|
|
352
|
+
|| result.forwards !== undefined && (!this.notificationRelay || !this.options.adapter.forward || !Array.isArray(result.forwards))
|
|
353
|
+
|| (result.events?.length ?? 0) > (this.options.resultLimits?.events ?? 32) || (result.activations?.length ?? 0) > 16
|
|
354
|
+
|| Buffer.byteLength(JSON.stringify(result)) > (this.options.resultLimits?.bytes ?? 128_000))
|
|
318
355
|
throw new Error('REACTION_OUTPUT_INVALID');
|
|
319
356
|
for (const event of result.events ?? []) {
|
|
320
357
|
if (!record(event) || !source.module.produces?.includes(event.type)
|
|
321
358
|
|| typeof event.key !== 'string' || !event.key.trim() || event.key.length > 512
|
|
322
359
|
|| event.wakeAgent !== undefined && typeof event.wakeAgent !== 'boolean'
|
|
360
|
+
|| event.occurredAt !== undefined && (typeof event.occurredAt !== 'string' || !Number.isFinite(Date.parse(event.occurredAt)))
|
|
323
361
|
|| !record(event.payload))
|
|
324
362
|
throw new Error('REACTION_EVENT_INVALID');
|
|
325
363
|
}
|
|
364
|
+
for (const forward of result.forwards ?? []) {
|
|
365
|
+
if (!record(forward) || typeof forward.eventId !== 'string' || !forward.eventId
|
|
366
|
+
|| Object.keys(forward).some(key => key !== 'eventId'))
|
|
367
|
+
throw Error('PERCEPTION_FORWARD_INVALID');
|
|
368
|
+
}
|
|
326
369
|
const bindings = new Set();
|
|
327
370
|
for (const activation of result.activations ?? []) {
|
|
328
371
|
if (!record(activation) || typeof activation.behavior !== 'string' || !Object.hasOwn(source.module.behaviors ?? {}, activation.behavior)
|
|
@@ -83,6 +83,8 @@ export declare class BehaviorActivationManager {
|
|
|
83
83
|
cancel(activationId: string, reason: string, now: number): readonly BehaviorActivationTransition[];
|
|
84
84
|
advance(now: number): readonly BehaviorActivationTransition[];
|
|
85
85
|
current(): BehaviorActivationSnapshot | undefined;
|
|
86
|
+
/** Install normalized input before an asynchronously prepared activation gets control. */
|
|
87
|
+
setPreparedInput(activationId: string, generation: number, input: Readonly<Record<string, unknown>>, now: number): void;
|
|
86
88
|
/** New immutable input, same activation and deadline; old decision generations expire. */
|
|
87
89
|
updateInput(activationId: string, input: Readonly<Record<string, unknown>>, now: number): readonly BehaviorActivationTransition[];
|
|
88
90
|
updateFallbackInput(behaviorId: string, input: Readonly<Record<string, unknown>>): void;
|
|
@@ -255,6 +255,17 @@ export class BehaviorActivationManager {
|
|
|
255
255
|
current() {
|
|
256
256
|
return this.snapshotActivation(this.reflex ?? this.short ?? (this.long?.state === 'running' ? this.long : undefined));
|
|
257
257
|
}
|
|
258
|
+
/** Install normalized input before an asynchronously prepared activation gets control. */
|
|
259
|
+
setPreparedInput(activationId, generation, input, now) {
|
|
260
|
+
const layer = this.findLayer(activationId);
|
|
261
|
+
const activation = layer ? this[layer] : undefined;
|
|
262
|
+
if (!activation || activation.instanceGeneration !== generation)
|
|
263
|
+
return;
|
|
264
|
+
activation.spec = { ...activation.spec, input: cloneInput(input) };
|
|
265
|
+
activation.startedAt = now;
|
|
266
|
+
if (activation.spec.maxRuntimeMs !== undefined)
|
|
267
|
+
activation.deadlineAt = now + activation.spec.maxRuntimeMs;
|
|
268
|
+
}
|
|
258
269
|
/** New immutable input, same activation and deadline; old decision generations expire. */
|
|
259
270
|
updateInput(activationId, input, now) {
|
|
260
271
|
const active = [this.long, this.short, this.reflex].find(entry => entry?.spec.activationId === activationId);
|
|
@@ -99,6 +99,7 @@ export interface PreparedStrategy {
|
|
|
99
99
|
readonly createModule?: () => object;
|
|
100
100
|
readonly source?: unknown;
|
|
101
101
|
readonly value: unknown;
|
|
102
|
+
readonly discardWorker?: () => void;
|
|
102
103
|
}
|
|
103
104
|
export interface StrategyObservation {
|
|
104
105
|
readonly revision: number;
|
|
@@ -178,6 +179,7 @@ export type ReflexResult = Readonly<{
|
|
|
178
179
|
accepted: true;
|
|
179
180
|
activationId: string;
|
|
180
181
|
replacedActivationId?: string;
|
|
182
|
+
ready?: Promise<'ready' | 'invalid' | 'cancelled'>;
|
|
181
183
|
}> | Readonly<{
|
|
182
184
|
accepted: false;
|
|
183
185
|
reason: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StrategyMemoryStore } from '../memory/strategy-memory.js';
|
|
2
|
-
import type { RuntimeEpochCatchUp, RuntimeEpochPlugin, RuntimeEpochPluginContext, RuntimeEpochPluginInput, RuntimeEpochPluginResult } from '../framework/plugins/contracts.js';
|
|
2
|
+
import type { RuntimeEpochCatchUp, RuntimeEpochFlushReason, RuntimeEpochPlugin, RuntimeEpochPluginContext, RuntimeEpochPluginInput, RuntimeEpochPluginResult } from '../framework/plugins/contracts.js';
|
|
3
3
|
import { BehaviorActivationManager, type BehaviorActivationSnapshot, type BehaviorActivationSource } from './activation-manager.js';
|
|
4
4
|
import type { StrategyControlDriver } from './control-driver.js';
|
|
5
5
|
import type { StrategyAdapter, PreparedStrategy, ReflexDriver, ReflexRequest, ReflexResult } from './contracts.js';
|
|
@@ -19,6 +19,7 @@ export interface StrategyHostOptions {
|
|
|
19
19
|
readonly catalog: {
|
|
20
20
|
has(id: string): boolean;
|
|
21
21
|
prepare(id: string, input: Readonly<Record<string, unknown>>): PreparedStrategy | Promise<PreparedStrategy>;
|
|
22
|
+
prepareForActivation?(id: string, input: Readonly<Record<string, unknown>>): Promise<PreparedStrategy>;
|
|
22
23
|
};
|
|
23
24
|
readonly fallbackBehaviors?: StrategyFallbackBindings;
|
|
24
25
|
readonly manager?: BehaviorActivationManager;
|
|
@@ -85,8 +86,13 @@ export declare class StrategyHost implements RuntimeEpochPlugin<Record<string, a
|
|
|
85
86
|
private readonly pendingLifecycleEvents;
|
|
86
87
|
private readonly decisionControllers;
|
|
87
88
|
private controlTail;
|
|
89
|
+
private readonly startRequests;
|
|
90
|
+
private readonly deferredDelegations;
|
|
91
|
+
private readonly deferredReflexes;
|
|
92
|
+
private pendingPreparations;
|
|
88
93
|
private driver?;
|
|
89
94
|
private scope?;
|
|
95
|
+
private scopeClosing;
|
|
90
96
|
private diagnostic?;
|
|
91
97
|
private boundaryActive;
|
|
92
98
|
private stopped;
|
|
@@ -133,16 +139,22 @@ export declare class StrategyHost implements RuntimeEpochPlugin<Record<string, a
|
|
|
133
139
|
}>): Promise<ReflexResult>;
|
|
134
140
|
update(input: RuntimeEpochPluginInput<Record<string, any>>, context: RuntimeEpochPluginContext<Record<string, any>>): Promise<RuntimeEpochPluginResult>;
|
|
135
141
|
commit(input: RuntimeEpochPluginInput<Record<string, any>>, computation: Readonly<RuntimeEpochPluginResult>, context: RuntimeEpochPluginContext<Record<string, any>>): Promise<RuntimeEpochPluginResult>;
|
|
142
|
+
flush(reason: RuntimeEpochFlushReason): void;
|
|
136
143
|
stop(reason: 'segment_ending' | 'shutdown'): Promise<void>;
|
|
137
144
|
private startLayer;
|
|
145
|
+
private sameScope;
|
|
146
|
+
private activateLayer;
|
|
138
147
|
private commitOrdered;
|
|
139
148
|
private applyDecision;
|
|
149
|
+
private deferDelegation;
|
|
140
150
|
private prepareDelegatedShort;
|
|
141
151
|
private startDelegatedShort;
|
|
142
152
|
private finishCurrent;
|
|
143
153
|
private failCurrent;
|
|
144
154
|
private applyTransitions;
|
|
145
155
|
private captureDelegationResults;
|
|
156
|
+
private deferRunner;
|
|
157
|
+
private acquireControl;
|
|
146
158
|
private ensureRuntime;
|
|
147
159
|
private syncRoleFallback;
|
|
148
160
|
/**
|
|
@@ -161,5 +173,6 @@ export declare class StrategyHost implements RuntimeEpochPlugin<Record<string, a
|
|
|
161
173
|
private output;
|
|
162
174
|
private withDecisionTimeout;
|
|
163
175
|
private interruptDecisions;
|
|
176
|
+
private inBackground;
|
|
164
177
|
private enqueueControl;
|
|
165
178
|
}
|