@myagentroam/agent 0.14.10 → 0.14.12
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/runtime/context/world-state.d.ts +3 -1
- package/dist/runtime/context/world-state.js +29 -5
- package/dist/sdk/execution-model-context.d.ts +6 -3
- package/dist/sdk/execution-model-context.js +4 -6
- package/dist/sdk/execution-round-request.d.ts +2 -2
- package/dist/sdk/execution-round-request.js +9 -8
- package/dist/sdk/session-execution-factory.d.ts +1 -1
- package/dist/sdk/session-execution-factory.js +21 -12
- package/dist/sdk/session-runtime.d.ts +6 -4
- package/dist/sdk/session-runtime.js +51 -13
- package/dist/tools/agent/skill.js +1 -1
- package/dist/tools/runtime-context.d.ts +2 -2
- package/package.json +1 -1
|
@@ -27,7 +27,9 @@ export declare function updatePromptWorldState(state: ContextualWorldState, inde
|
|
|
27
27
|
export declare function assertLoadedSkillCapacity(entries: readonly ContextualWorldState['entries'][number][], pending: ReadonlyMap<string, string>, name: string, content: string): void;
|
|
28
28
|
export declare function updateLoadedSkillWorldState(state: ContextualWorldState, index: number, loads: ReadonlyMap<string, string>): ContextualWorldState;
|
|
29
29
|
export declare function parsePersistedContextualWorldState(payload: unknown): ContextualWorldState | undefined;
|
|
30
|
-
|
|
30
|
+
/** Pending loads have no history anchor and survive a Compact/GC generation change. */
|
|
31
|
+
export declare function parsePendingSkillContext(payload: unknown): Map<string, string>;
|
|
32
|
+
export declare function persistContextualWorldState(store: Pick<import('../../session/store/contracts.js').SessionStore, 'writeContextualWorldState'>, sessionId: string, state: ContextualWorldState, pendingSkills?: ReadonlyMap<string, string>): Promise<void>;
|
|
31
33
|
export declare function updateContextualWorldState(state: ContextualWorldState, index: number, current: {
|
|
32
34
|
instructionSnapshot: string | undefined;
|
|
33
35
|
instructionContent: string | undefined;
|
|
@@ -118,6 +118,12 @@ const MAX_CONTEXTUAL_WORLD_STATE_CONTENT_BYTES = 8 * 1024 * 1024;
|
|
|
118
118
|
const MAX_LOADED_SKILLS = 64;
|
|
119
119
|
const MAX_LOADED_SKILL_CONTENT_BYTES = 1024 * 1024;
|
|
120
120
|
const LOADED_SKILL_ID_PREFIX = 'runtime:loaded-skill:';
|
|
121
|
+
function loadedSkillContentBytes(bodies) {
|
|
122
|
+
let bytes = 0;
|
|
123
|
+
for (const body of bodies)
|
|
124
|
+
bytes += Buffer.byteLength(JSON.stringify(body));
|
|
125
|
+
return bytes;
|
|
126
|
+
}
|
|
121
127
|
/** Check both accepted loads and delivered guidance before admitting a new load. */
|
|
122
128
|
export function assertLoadedSkillCapacity(entries, pending, name, content) {
|
|
123
129
|
const selected = new Map();
|
|
@@ -127,10 +133,8 @@ export function assertLoadedSkillCapacity(entries, pending, name, content) {
|
|
|
127
133
|
for (const [pendingName, body] of pending)
|
|
128
134
|
selected.set(pendingName, body);
|
|
129
135
|
selected.set(name, content);
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
bytes += Buffer.byteLength(JSON.stringify(body));
|
|
133
|
-
if (selected.size > MAX_LOADED_SKILLS || bytes > MAX_LOADED_SKILL_CONTENT_BYTES)
|
|
136
|
+
if (selected.size > MAX_LOADED_SKILLS ||
|
|
137
|
+
loadedSkillContentBytes(selected.values()) > MAX_LOADED_SKILL_CONTENT_BYTES)
|
|
134
138
|
throw new AgentError('MAR_AGENT_HOST_LIMIT_EXCEEDED', 'Loaded Skill context exceeds the Session limit. Compact before loading more Skills.');
|
|
135
139
|
}
|
|
136
140
|
export function updateLoadedSkillWorldState(state, index, loads) {
|
|
@@ -252,11 +256,31 @@ function nullableWorldStateString(value) {
|
|
|
252
256
|
? value
|
|
253
257
|
: null;
|
|
254
258
|
}
|
|
255
|
-
|
|
259
|
+
const pendingSkillsSchema = z
|
|
260
|
+
.array(z.tuple([z.string().min(1).max(128), z.string().min(1)]))
|
|
261
|
+
.max(MAX_LOADED_SKILLS);
|
|
262
|
+
/** Pending loads have no history anchor and survive a Compact/GC generation change. */
|
|
263
|
+
export function parsePendingSkillContext(payload) {
|
|
264
|
+
if (payload === null || typeof payload !== 'object')
|
|
265
|
+
return new Map();
|
|
266
|
+
const record = payload;
|
|
267
|
+
if (record.version !== 2 || record.pendingSkills === undefined)
|
|
268
|
+
return new Map();
|
|
269
|
+
const parsed = pendingSkillsSchema.safeParse(record.pendingSkills);
|
|
270
|
+
if (!parsed.success)
|
|
271
|
+
return new Map();
|
|
272
|
+
const pending = new Map(parsed.data);
|
|
273
|
+
if (pending.size !== parsed.data.length ||
|
|
274
|
+
loadedSkillContentBytes(pending.values()) > MAX_LOADED_SKILL_CONTENT_BYTES)
|
|
275
|
+
return new Map();
|
|
276
|
+
return pending;
|
|
277
|
+
}
|
|
278
|
+
export async function persistContextualWorldState(store, sessionId, state, pendingSkills) {
|
|
256
279
|
if (state.entries.length > MAX_CONTEXTUAL_WORLD_STATE_ENTRIES)
|
|
257
280
|
throw new AgentError('MAR_AGENT_HOST_LIMIT_EXCEEDED', 'Contextual world state exceeds the entry limit.');
|
|
258
281
|
await store.writeContextualWorldState(sessionId, {
|
|
259
282
|
version: 2,
|
|
283
|
+
...(pendingSkills?.size ? { pendingSkills: [...pendingSkills] } : {}),
|
|
260
284
|
...(state.promptSnapshot === undefined ? {} : { promptSnapshot: state.promptSnapshot }),
|
|
261
285
|
generation: state.generation ?? null,
|
|
262
286
|
instructionSnapshot: state.instructionSnapshot ?? null,
|
|
@@ -10,7 +10,7 @@ import type { ResourceInputProjection } from './resource-input.js';
|
|
|
10
10
|
interface ExecutionModelContextPorts {
|
|
11
11
|
readonly restored: Awaited<ReturnType<typeof restoreMessages>>;
|
|
12
12
|
readonly initialContextualWorldState: ContextualWorldState;
|
|
13
|
-
readonly store: Parameters<typeof restoreMessages>[0] & Pick<SessionStore, 'append'
|
|
13
|
+
readonly store: Parameters<typeof restoreMessages>[0] & Pick<SessionStore, 'append'>;
|
|
14
14
|
readonly sessionId: string;
|
|
15
15
|
readonly executionId: string;
|
|
16
16
|
readonly turnId: string;
|
|
@@ -19,7 +19,10 @@ interface ExecutionModelContextPorts {
|
|
|
19
19
|
readonly contextualWorldStateGeneration: string | undefined;
|
|
20
20
|
readonly compactMayContainAgentInstructions: boolean;
|
|
21
21
|
readonly messageSources: WeakMap<ModelMessage, SessionRecord>;
|
|
22
|
-
|
|
22
|
+
persistWorldState(state: ContextualWorldState, options?: {
|
|
23
|
+
readonly deliveredSkills?: ReadonlyMap<string, string>;
|
|
24
|
+
readonly checkpoint?: true;
|
|
25
|
+
}): Promise<void>;
|
|
23
26
|
environment(): EnvironmentContextState;
|
|
24
27
|
instructionSnapshot(): string | undefined;
|
|
25
28
|
renderInstructions(previous: boolean): string | undefined;
|
|
@@ -40,7 +43,7 @@ interface ExecutionModelContextPorts {
|
|
|
40
43
|
export declare function createExecutionModelContext(ports: ExecutionModelContextPorts): {
|
|
41
44
|
messages: ModelMessage[];
|
|
42
45
|
readonly worldState: ContextualWorldState;
|
|
43
|
-
commitWorldState: (next: ContextualWorldState) => Promise<void>;
|
|
46
|
+
commitWorldState: (next: ContextualWorldState, deliveredSkills?: ReadonlyMap<string, string>) => Promise<void>;
|
|
44
47
|
recordEnvironment: () => Promise<void>;
|
|
45
48
|
syncCatalogWorldState: (index?: number) => Promise<void>;
|
|
46
49
|
rememberInput(input: CompactUserInput): void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isDeepStrictEqual } from 'node:util';
|
|
2
2
|
import { restoreMessages } from '../runtime/context/restore.js';
|
|
3
|
-
import { contextualInstructionInsertIndex, latestContextualWorldStateGeneration,
|
|
3
|
+
import { contextualInstructionInsertIndex, latestContextualWorldStateGeneration, resetContextualWorldState, updateContextualWorldState, updatePromptWorldState } from '../runtime/context/world-state.js';
|
|
4
4
|
import { environmentContextUpdate } from '../runtime/context/environment.js';
|
|
5
5
|
import { compactedHistoryMessages } from '../runtime/compaction/history.js';
|
|
6
6
|
import { compactSummaryFromCheckpoint } from '../session/record-projection.js';
|
|
@@ -12,10 +12,9 @@ export function createExecutionModelContext(ports) {
|
|
|
12
12
|
let recordedEnvironment = restored.environmentState;
|
|
13
13
|
let instructionInsertIndex = 0;
|
|
14
14
|
let worldState = initialContextualWorldState;
|
|
15
|
-
const commitWorldState = async (next) => {
|
|
16
|
-
await
|
|
15
|
+
const commitWorldState = async (next, deliveredSkills) => {
|
|
16
|
+
await ports.persistWorldState(next, deliveredSkills ? { deliveredSkills } : undefined);
|
|
17
17
|
worldState = next;
|
|
18
|
-
ports.updateWorldState(next);
|
|
19
18
|
};
|
|
20
19
|
const resetCompactWorldState = async (generation, committed) => {
|
|
21
20
|
instructionInsertIndex = contextualInstructionInsertIndex(messages);
|
|
@@ -29,9 +28,8 @@ export function createExecutionModelContext(ports) {
|
|
|
29
28
|
else {
|
|
30
29
|
// The checkpoint is authoritative; its sidecar is recoverable on restart.
|
|
31
30
|
worldState = next;
|
|
32
|
-
ports.updateWorldState(next);
|
|
33
31
|
try {
|
|
34
|
-
await
|
|
32
|
+
await ports.persistWorldState(next, { checkpoint: true });
|
|
35
33
|
}
|
|
36
34
|
catch {
|
|
37
35
|
ports.reportSidecarFailure();
|
|
@@ -21,12 +21,12 @@ export declare function prepareExecutionRoundRequest<Bindings, Scope>(input: {
|
|
|
21
21
|
};
|
|
22
22
|
bind(scope: Scope): void;
|
|
23
23
|
world(): ContextualWorldState;
|
|
24
|
-
commitWorld(world: ContextualWorldState): Promise<void>;
|
|
24
|
+
commitWorld(world: ContextualWorldState, deliveredSkills?: ReadonlyMap<string, string>): Promise<void>;
|
|
25
25
|
preparing(messages: readonly SessionModelMessage[] | undefined, baseLength: number): void;
|
|
26
26
|
transform(request: SessionModelRequest, bindings: Bindings): Promise<SessionModelRequest>;
|
|
27
27
|
}): Promise<{
|
|
28
28
|
candidateRequest: SessionModelRequest;
|
|
29
29
|
request: SessionModelRequest;
|
|
30
30
|
toolScope: Scope;
|
|
31
|
-
|
|
31
|
+
acknowledgeSkillContext: (() => Promise<void>) | undefined;
|
|
32
32
|
}>;
|
|
@@ -36,13 +36,14 @@ export async function prepareExecutionRoundRequest(input) {
|
|
|
36
36
|
candidateRequest,
|
|
37
37
|
request,
|
|
38
38
|
toolScope,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
await input.commitWorld(next);
|
|
46
|
-
|
|
39
|
+
acknowledgeSkillContext: loads?.size
|
|
40
|
+
? async () => {
|
|
41
|
+
const current = input.world();
|
|
42
|
+
const next = current === promptWorld
|
|
43
|
+
? world
|
|
44
|
+
: updateLoadedSkillWorldState(current, skillAnchor, loads);
|
|
45
|
+
await input.commitWorld(next, loads);
|
|
46
|
+
}
|
|
47
|
+
: undefined
|
|
47
48
|
};
|
|
48
49
|
}
|
|
@@ -26,7 +26,7 @@ export declare function createSessionExecutionFactory(ports: {
|
|
|
26
26
|
sessionWriterOwner: object;
|
|
27
27
|
activeExecutionAdapters: AgentSessionComposition['activeExecutionAdapters'];
|
|
28
28
|
sessionId: string;
|
|
29
|
-
sessionRuntime: Pick<SessionRuntime, '
|
|
29
|
+
sessionRuntime: Pick<SessionRuntime, 'acknowledgeMcpDiscovery' | 'pendingMcpDiscoveryNames' | 'mcpDiscoveryRevision' | 'publishMcpDiscovery' | 'assertCanExecute' | 'bindExecutionContext' | 'commitContextualWorldState' | 'deactivateExecution' | 'joinRecordProjection' | 'pendingSkillContext' | 'registerExecution' | 'publishEvent' | 'publishSkillContext' | 'restoreExecutionState' | 'scheduleTool' | 'sessionBackend' | 'unbindExecutionContext' | 'unregisterExecution' | 'recordRequest' | 'completeRequest'>;
|
|
30
30
|
sessionSource: Awaited<ReturnType<SessionStore['getSession']>>['source'];
|
|
31
31
|
logicalWorkspace: string;
|
|
32
32
|
instructionWorkspace: string;
|
|
@@ -183,7 +183,7 @@ export function createSessionExecutionFactory(ports) {
|
|
|
183
183
|
contextualWorldStateGeneration,
|
|
184
184
|
compactMayContainAgentInstructions,
|
|
185
185
|
messageSources,
|
|
186
|
-
|
|
186
|
+
persistWorldState: (state, options) => sessionRuntime.commitContextualWorldState(store, state, options),
|
|
187
187
|
environment: () => executionEnvironment,
|
|
188
188
|
instructionSnapshot: () => currentInstructionSnapshot,
|
|
189
189
|
renderInstructions: (previous) => renderAgentInstructions(instructionEntries, previous),
|
|
@@ -442,7 +442,7 @@ export function createSessionExecutionFactory(ports) {
|
|
|
442
442
|
todo,
|
|
443
443
|
waitInterrupt,
|
|
444
444
|
skills: instructionContext?.skills ?? new Map(),
|
|
445
|
-
publishSkillContext: (name, content) => sessionRuntime.publishSkillContext(name, content),
|
|
445
|
+
publishSkillContext: (name, content, signal) => sessionRuntime.publishSkillContext(store, name, content, signal),
|
|
446
446
|
withRecordProjection,
|
|
447
447
|
publishMcpDiscovery: async (names, signal) => {
|
|
448
448
|
const message = {
|
|
@@ -702,7 +702,7 @@ export function createSessionExecutionFactory(ports) {
|
|
|
702
702
|
await compactCoordinator.checkpoint(true);
|
|
703
703
|
return;
|
|
704
704
|
}
|
|
705
|
-
const
|
|
705
|
+
const preparedSkillAcknowledgements = new WeakMap();
|
|
706
706
|
const prepareRound = createExecutionRoundPreparer({
|
|
707
707
|
prepareInputs: (includeSessionContext) => contextCoordinator.prepareInputs({
|
|
708
708
|
includeSessionContext,
|
|
@@ -742,14 +742,14 @@ export function createSessionExecutionFactory(ports) {
|
|
|
742
742
|
capture: captureRoundToolScope,
|
|
743
743
|
bind: (scope) => sources.bind(scope),
|
|
744
744
|
world: () => modelContext.worldState,
|
|
745
|
-
commitWorld:
|
|
745
|
+
commitWorld: modelContext.commitWorldState,
|
|
746
746
|
preparing: (next, baseLength) => {
|
|
747
747
|
preparingRequestMessages = next;
|
|
748
748
|
preparingRequestBaseLength = baseLength;
|
|
749
749
|
},
|
|
750
750
|
transform: (candidate, bindings) => transformModelRequest(candidate, bindings)
|
|
751
751
|
});
|
|
752
|
-
return { ...candidate, discoveryRevision: capturedDiscoveryRevision
|
|
752
|
+
return { ...candidate, discoveryRevision: capturedDiscoveryRevision };
|
|
753
753
|
},
|
|
754
754
|
adoptLateInput: async () => {
|
|
755
755
|
const lateCoreChange = inputs.hasBoundaryCompactPending && (await appendMailboxMessages());
|
|
@@ -760,7 +760,7 @@ export function createSessionExecutionFactory(ports) {
|
|
|
760
760
|
const lateAdopted = lateBlocking && (await compactCoordinator.checkpoint(true));
|
|
761
761
|
return Boolean(lateCoreChange || lateAdopted);
|
|
762
762
|
},
|
|
763
|
-
prepareResources: async ({ candidateRequest, request, toolScope, discoveryRevision: capturedDiscoveryRevision,
|
|
763
|
+
prepareResources: async ({ candidateRequest, request, toolScope, discoveryRevision: capturedDiscoveryRevision, acknowledgeSkillContext }) => {
|
|
764
764
|
if (capturedDiscoveryRevision !== sessionRuntime.mcpDiscoveryRevision)
|
|
765
765
|
return { retry: true };
|
|
766
766
|
resourceInput.observe(candidateRequest.messages);
|
|
@@ -774,11 +774,10 @@ export function createSessionExecutionFactory(ports) {
|
|
|
774
774
|
}
|
|
775
775
|
const prepared = await resourceInput.prepare(request, candidateRequest.images, candidateRequest.messages);
|
|
776
776
|
controller.signal.throwIfAborted();
|
|
777
|
-
await commitSkillContext();
|
|
778
777
|
if (capturedDiscoveryRevision !== sessionRuntime.mcpDiscoveryRevision)
|
|
779
778
|
return { retry: true };
|
|
780
|
-
if (
|
|
781
|
-
|
|
779
|
+
if (acknowledgeSkillContext)
|
|
780
|
+
preparedSkillAcknowledgements.set(prepared.messages, acknowledgeSkillContext);
|
|
782
781
|
return { round: { request: prepared, toolScope } };
|
|
783
782
|
}
|
|
784
783
|
});
|
|
@@ -803,10 +802,20 @@ export function createSessionExecutionFactory(ports) {
|
|
|
803
802
|
prepareRound,
|
|
804
803
|
runTools: executeRoundTools,
|
|
805
804
|
startModel: (request) => {
|
|
806
|
-
const skillLoads = preparedSkillLoads.get(request.messages);
|
|
807
805
|
return modelLifecycle.start(request, async () => {
|
|
808
|
-
|
|
809
|
-
|
|
806
|
+
const acknowledgeSkills = preparedSkillAcknowledgements.get(request.messages);
|
|
807
|
+
if (acknowledgeSkills) {
|
|
808
|
+
try {
|
|
809
|
+
await acknowledgeSkills();
|
|
810
|
+
}
|
|
811
|
+
catch {
|
|
812
|
+
// Keep the accepted loads pending; a sidecar failure cannot undo model completion.
|
|
813
|
+
coreExtensions.diagnose({
|
|
814
|
+
code: 'MAR_AGENT_SKILL_CONTEXT_ACK_PERSIST_FAILED',
|
|
815
|
+
sessionId
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
}
|
|
810
819
|
await sessionRuntime.acknowledgeMcpDiscovery(request.messages, (receipts) => store.append(sessionId, {
|
|
811
820
|
type: MCP_DISCOVERY_DELIVERED,
|
|
812
821
|
payload: { receipts },
|
|
@@ -11,7 +11,7 @@ import type { ExecutionResult } from './types.js';
|
|
|
11
11
|
import type { AgentEvent } from '../runtime/events.js';
|
|
12
12
|
import type { ResolvedModelConfiguration } from './model-runtime.js';
|
|
13
13
|
import type { SessionExecution, SessionExecutionContext } from './session-execution.js';
|
|
14
|
-
/** Owns warm Session state
|
|
14
|
+
/** Owns warm Session state and serializes private context writes from Executions and retained cells. */
|
|
15
15
|
export declare class SessionRuntime {
|
|
16
16
|
#private;
|
|
17
17
|
readonly storedSession: Awaited<ReturnType<SessionStore['getSession']>>;
|
|
@@ -29,9 +29,12 @@ export declare class SessionRuntime {
|
|
|
29
29
|
retireInputs(): Promise<unknown> | undefined;
|
|
30
30
|
getOrCreateInputs(create: () => SessionInputCoordinator): SessionInputCoordinator;
|
|
31
31
|
resetAfterClear(generation: number): void;
|
|
32
|
-
publishSkillContext(name: string, content: string): void
|
|
32
|
+
publishSkillContext(store: Pick<SessionStore, 'writeContextualWorldState'>, name: string, content: string, signal: AbortSignal): Promise<void>;
|
|
33
33
|
pendingSkillContext(): ReadonlyMap<string, string>;
|
|
34
|
-
|
|
34
|
+
commitContextualWorldState(store: Pick<SessionStore, 'writeContextualWorldState'>, world: ContextualWorldState, options?: {
|
|
35
|
+
readonly deliveredSkills?: ReadonlyMap<string, string>;
|
|
36
|
+
readonly checkpoint?: true;
|
|
37
|
+
}): Promise<void>;
|
|
35
38
|
publishMcpDiscovery(recordId: string, message: SessionModelMessage): void;
|
|
36
39
|
get mcpDiscoveryRevision(): number;
|
|
37
40
|
pendingMcpDiscoveryNames(message: SessionModelMessage): readonly string[];
|
|
@@ -64,7 +67,6 @@ export declare class SessionRuntime {
|
|
|
64
67
|
bindExecutionContext(context: SessionExecutionContext): void;
|
|
65
68
|
unbindExecutionContext(context: SessionExecutionContext): void;
|
|
66
69
|
get contextualWorldState(): ContextualWorldState | undefined;
|
|
67
|
-
updateContextualWorldState(state: ContextualWorldState): void;
|
|
68
70
|
get activeExecutionId(): string | undefined;
|
|
69
71
|
subscribe(listener: (event: AgentEvent) => void): {
|
|
70
72
|
unsubscribe: () => void;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { AgentError } from '../error.js';
|
|
2
2
|
import { ExtensionRequestSnapshots } from '../extensions/model-request.js';
|
|
3
3
|
import { restorePendingMcpDiscovery } from '../session/mcp-discovery.js';
|
|
4
|
-
import { assertLoadedSkillCapacity, latestContextualWorldStateGeneration, parsePersistedContextualWorldState, recoverLoadedSkillEntries } from '../runtime/context/world-state.js';
|
|
4
|
+
import { assertLoadedSkillCapacity, latestContextualWorldStateGeneration, parsePendingSkillContext, parsePersistedContextualWorldState, persistContextualWorldState, recoverLoadedSkillEntries } from '../runtime/context/world-state.js';
|
|
5
5
|
import { TodoStore } from '../tools/agent/todo.js';
|
|
6
6
|
import { createSessionToolScheduler } from '../tools/scheduler.js';
|
|
7
7
|
import { SessionModelAdapters } from './session-model-adapters.js';
|
|
8
|
-
/** Owns warm Session state
|
|
8
|
+
/** Owns warm Session state and serializes private context writes from Executions and retained cells. */
|
|
9
9
|
export class SessionRuntime {
|
|
10
10
|
storedSession;
|
|
11
11
|
onExecutionRegistered;
|
|
@@ -17,6 +17,7 @@ export class SessionRuntime {
|
|
|
17
17
|
#modelAdapters;
|
|
18
18
|
#contextualWorldState;
|
|
19
19
|
#pendingSkillContext = new Map();
|
|
20
|
+
#contextualWrite;
|
|
20
21
|
#inputs;
|
|
21
22
|
#requestSnapshots;
|
|
22
23
|
#recordProjection;
|
|
@@ -68,17 +69,54 @@ export class SessionRuntime {
|
|
|
68
69
|
this.#inputs = undefined;
|
|
69
70
|
this.#requestSnapshots = undefined;
|
|
70
71
|
}
|
|
71
|
-
publishSkillContext(name, content) {
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
publishSkillContext(store, name, content, signal) {
|
|
73
|
+
return this.#withContextualWrite(async () => {
|
|
74
|
+
signal.throwIfAborted();
|
|
75
|
+
const world = this.#contextualWorldState;
|
|
76
|
+
if (!world)
|
|
77
|
+
throw new AgentError('MAR_AGENT_TOOL_NOT_AVAILABLE', 'Skill context delivery is unavailable.');
|
|
78
|
+
assertLoadedSkillCapacity(world.entries, this.#pendingSkillContext, name, content);
|
|
79
|
+
const pending = new Map(this.#pendingSkillContext).set(name, content);
|
|
80
|
+
try {
|
|
81
|
+
await persistContextualWorldState(store, this.storedSession.id, world, pending);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// An injected Store error need not be safe to show in tool results or history.
|
|
85
|
+
throw new AgentError('MAR_AGENT_SKILL_CONTEXT_PERSIST_FAILED', 'Could not save the loaded Skill context.');
|
|
86
|
+
}
|
|
87
|
+
this.#pendingSkillContext = pending;
|
|
88
|
+
});
|
|
74
89
|
}
|
|
75
90
|
pendingSkillContext() {
|
|
76
91
|
return new Map(this.#pendingSkillContext);
|
|
77
92
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
93
|
+
commitContextualWorldState(store, world, options) {
|
|
94
|
+
return this.#withContextualWrite(async () => {
|
|
95
|
+
let pending = this.#pendingSkillContext;
|
|
96
|
+
if (options?.deliveredSkills) {
|
|
97
|
+
pending = new Map(pending);
|
|
98
|
+
for (const [name, content] of options.deliveredSkills)
|
|
99
|
+
if (pending.get(name) === content)
|
|
100
|
+
pending.delete(name);
|
|
101
|
+
}
|
|
102
|
+
// Once a Compact checkpoint commits, its generation wins even if the sidecar write fails.
|
|
103
|
+
if (options?.checkpoint)
|
|
104
|
+
this.#contextualWorldState = world;
|
|
105
|
+
await persistContextualWorldState(store, this.storedSession.id, world, pending);
|
|
106
|
+
this.#contextualWorldState = world;
|
|
107
|
+
this.#pendingSkillContext = pending;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
async #withContextualWrite(write) {
|
|
111
|
+
const next = (this.#contextualWrite ?? Promise.resolve()).catch(() => undefined).then(write);
|
|
112
|
+
this.#contextualWrite = next;
|
|
113
|
+
try {
|
|
114
|
+
await next;
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
if (this.#contextualWrite === next)
|
|
118
|
+
this.#contextualWrite = undefined;
|
|
119
|
+
}
|
|
82
120
|
}
|
|
83
121
|
publishMcpDiscovery(recordId, message) {
|
|
84
122
|
const receipt = message.mcpDiscovery;
|
|
@@ -157,7 +195,10 @@ export class SessionRuntime {
|
|
|
157
195
|
});
|
|
158
196
|
const todo = this.restoreTodo(state.todo, store.hasRetainedSessionLease(sessionId, writerOwner));
|
|
159
197
|
const generation = latestContextualWorldStateGeneration(state.contextRecords);
|
|
160
|
-
const
|
|
198
|
+
const payload = await store.readContextualWorldState(sessionId);
|
|
199
|
+
const persisted = parsePersistedContextualWorldState(payload);
|
|
200
|
+
if (this.#contextualWorldState === undefined)
|
|
201
|
+
this.#pendingSkillContext = parsePendingSkillContext(payload);
|
|
161
202
|
const world = this.#contextualWorldState?.generation === generation &&
|
|
162
203
|
this.#contextualWorldState !== undefined
|
|
163
204
|
? this.#contextualWorldState
|
|
@@ -192,9 +233,6 @@ export class SessionRuntime {
|
|
|
192
233
|
get contextualWorldState() {
|
|
193
234
|
return this.#contextualWorldState;
|
|
194
235
|
}
|
|
195
|
-
updateContextualWorldState(state) {
|
|
196
|
-
this.#contextualWorldState = state;
|
|
197
|
-
}
|
|
198
236
|
get activeExecutionId() {
|
|
199
237
|
return this.#activeExecutionId;
|
|
200
238
|
}
|
|
@@ -33,7 +33,7 @@ export async function executeSkill(arguments_, context) {
|
|
|
33
33
|
if (!content)
|
|
34
34
|
throw new AgentError('MAR_AGENT_TOOL_INPUT_INVALID', 'Skill could not be loaded.');
|
|
35
35
|
context.signal.throwIfAborted();
|
|
36
|
-
context.publishSkillContext(skill.name, content);
|
|
36
|
+
await context.publishSkillContext(skill.name, content, context.signal);
|
|
37
37
|
const data = { success: true };
|
|
38
38
|
return {
|
|
39
39
|
content: JSON.stringify(data),
|
|
@@ -67,8 +67,8 @@ export interface ToolRuntimeContext {
|
|
|
67
67
|
writeToolResultResource?: import('./result-extensions.js').ToolResultResourceWriter;
|
|
68
68
|
codeMode?: import('./contracts.js').ToolExecutionContext['codeMode'];
|
|
69
69
|
skills: ReadonlyMap<string, AgentSkill>;
|
|
70
|
-
/**
|
|
71
|
-
publishSkillContext?(name: string, content: string): void
|
|
70
|
+
/** Persist trusted Skill guidance separately from tool results and guest output. */
|
|
71
|
+
publishSkillContext?(name: string, content: string, signal: AbortSignal): Promise<void>;
|
|
72
72
|
recentImages?: readonly ToolRuntimeImage[];
|
|
73
73
|
searchMcpTools?: (input: unknown, signal: AbortSignal, caller?: import('./contracts.js').ToolCaller) => Promise<import('./contracts.js').ToolExecutionResult>;
|
|
74
74
|
expectedMcpTool?: import('./contracts.js').McpToolCallContext['expectedTool'];
|