Skip to content

Commit 299b35b

Browse files
committed
refactor(agents): remove workspace plugin generation projections
1 parent d1a194b commit 299b35b

19 files changed

Lines changed: 108 additions & 299 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,6 @@ src/agents/plugin-model-catalog.ts 4
19911991
src/agents/plugin-text-transforms.ts 5
19921992
src/agents/prepared-model-catalog.ts 1
19931993
src/agents/prepared-model-catalog.worker.ts 1
1994-
src/agents/prepared-model-runtime-lease.ts 1
19951994
src/agents/prepared-model-runtime.configured.ts 2
19961995
src/agents/prepared-model-runtime.facts.ts 1
19971996
src/agents/prepared-model-runtime.plugin-context.ts 2

‎src/agents/agent-command.compaction-rotation.test.ts‎

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type RunAgentAttempt = typeof runAgentAttempt;
2828
type CaptureSessionDiffBaseline =
2929
(typeof import("../sessions/session-diff.js"))["captureSessionDiffBaseline"];
3030
type CliCompactionParams = {
31+
pluginGeneration?: unknown;
3132
sessionId: string;
3233
sessionEntry?: SessionEntry;
3334
sessionKey: string;
@@ -224,9 +225,7 @@ let agentCommand: typeof import("./agent-command.js").agentCommand;
224225
let agentCommandFromGatewayIngress: typeof import("./agent-command.js").agentCommandFromGatewayIngress;
225226

226227
beforeAll(async () => {
227-
const command = await import("./agent-command.js");
228-
agentCommand = command.agentCommand;
229-
agentCommandFromGatewayIngress = command.agentCommandFromGatewayIngress;
228+
({ agentCommand, agentCommandFromGatewayIngress } = await import("./agent-command.js"));
230229
});
231230

232231
beforeEach(async () => {
@@ -345,6 +344,7 @@ function readLifecyclePhases(): Array<string | undefined> {
345344

346345
const COMPACTION_ERROR =
347346
"CLI transcript compaction failed for openai/gpt-5.5: Summarization failed: Connection error.";
347+
const GATEWAY_INGRESS_ARGS = [defaultRuntime, undefined, {}] as const;
348348

349349
describe("agentCommand compaction transcript rotation", () => {
350350
it.each([
@@ -583,9 +583,7 @@ describe("agentCommand compaction transcript rotation", () => {
583583
cwd: state.workspaceDir,
584584
allowModelOverride: false,
585585
},
586-
defaultRuntime,
587-
undefined,
588-
{},
586+
...GATEWAY_INGRESS_ARGS,
589587
);
590588

591589
const events = (await loadTranscriptEvents({
@@ -614,41 +612,37 @@ describe("agentCommand compaction transcript rotation", () => {
614612
);
615613
});
616614

617-
it("persists the pending final before CLI compaction failure and still delivers", async () => {
615+
it("carries Gateway plugin generation through failed post-turn compaction and still delivers", async () => {
618616
const sessionId = "cli-compaction-failure";
619617
const sessionKey = `agent:main:explicit:${sessionId}`;
620618
const text = "cli reply generated before compaction";
621-
let compactionSessionEntry: SessionEntry | undefined;
619+
const pluginGeneration = {
620+
pluginMetadataSnapshot: { workspaceDir: state.workspaceDir },
621+
} as never;
622622
let storedEntryBeforeCompaction: SessionEntry | undefined;
623623
state.runAgentAttemptMock.mockResolvedValueOnce(makeResult({ sessionId, text, runner: "cli" }));
624624
state.runCliTurnCompactionLifecycleMock.mockImplementationOnce(async (params) => {
625-
compactionSessionEntry = params.sessionEntry;
625+
expect(params.pluginGeneration).toBe(pluginGeneration);
626626
storedEntryBeforeCompaction = findStoredSessionEntry(sessionKey);
627627
throw new Error(COMPACTION_ERROR);
628628
});
629629

630-
const result = await agentCommand({
631-
message: "room message",
632-
sessionId,
633-
sessionKey,
634-
cwd: state.workspaceDir,
635-
channel: "discord",
636-
to: "discord:dm:123",
637-
accountId: "main",
638-
deliver: true,
639-
});
640-
641-
expect(compactionSessionEntry).toMatchObject({
642-
pendingFinalDelivery: {
643-
kind: "replayable",
644-
text,
645-
context: {
646-
channel: "discord",
647-
to: "discord:dm:123",
648-
accountId: "main",
649-
},
630+
const result = await agentCommandFromGatewayIngress(
631+
{
632+
message: "room message",
633+
sessionId,
634+
sessionKey,
635+
cwd: state.workspaceDir,
636+
channel: "discord",
637+
to: "discord:dm:123",
638+
accountId: "main",
639+
deliver: true,
640+
allowModelOverride: false,
650641
},
651-
});
642+
...GATEWAY_INGRESS_ARGS,
643+
{ config: state.cfg ?? {}, pluginGeneration },
644+
);
645+
652646
expect(storedEntryBeforeCompaction).toMatchObject({
653647
pendingFinalDelivery: { kind: "replayable", text },
654648
});

‎src/agents/command/cli-compaction.test.ts‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,18 @@ describe("runCliTurnCompactionLifecycle", () => {
610610
agentDir: tmpDir,
611611
workspaceDir: tmpDir,
612612
});
613-
const acquirePreparedModelRuntime = vi.fn(async () => preparedRuntimeLease);
613+
const pluginGeneration = {
614+
configuredCatalogEntries: [],
615+
inlineProviderModels: [],
616+
pluginMetadataSnapshot: preparedRuntimeLease.snapshot.metadataSnapshot,
617+
pluginRegistry: preparedRuntimeLease.snapshot.pluginRegistry,
618+
} as never;
619+
const acquirePreparedModelRuntime = vi.fn<
620+
NonNullable<CliCompactionTestDeps["acquirePreparedModelRuntime"]>
621+
>(async (_input, options) => {
622+
expect(options?.pluginGeneration).toBe(pluginGeneration);
623+
return preparedRuntimeLease;
624+
});
614625
const ensureSelectedAgentHarnessPlugin = vi.fn(async () => undefined);
615626
const compactAgentHarnessSession = vi.fn(async () => ({
616627
ok: true,
@@ -647,7 +658,7 @@ describe("runCliTurnCompactionLifecycle", () => {
647658
},
648659
});
649660
const { sessionId, sessionKey } = scenario;
650-
const updatedEntry = await scenario.run();
661+
const updatedEntry = await scenario.run({ pluginGeneration });
651662

652663
expect(resolveContextEngine).toHaveBeenCalledTimes(1);
653664
expect(applyAgentAutoCompactionGuard).toHaveBeenCalledWith(
@@ -664,16 +675,7 @@ describe("runCliTurnCompactionLifecycle", () => {
664675
pluginRegistry: preparedRuntimeLease.snapshot.pluginRegistry,
665676
}),
666677
);
667-
expect(acquirePreparedModelRuntime).toHaveBeenCalledWith({
668-
config: {},
669-
agentId: "main",
670-
agentDir: tmpDir,
671-
workspaceDir: tmpDir,
672-
allowGatewaySubagentBinding: true,
673-
runtimePluginSelections: [
674-
{ agentId: "main", modelId: "gpt-5.5", provider: "openai", runtime: "codex" },
675-
],
676-
});
678+
expect(acquirePreparedModelRuntime).toHaveBeenCalledOnce();
677679
expect(applyAgentAutoCompactionGuard.mock.invocationCallOrder[0] ?? 0).toBeLessThan(
678680
compactAgentHarnessSession.mock.invocationCallOrder[0] ?? 0,
679681
);

‎src/agents/command/cli-compaction.ts‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { isRecoverableNativeHarnessBindingFailure } from "../harness/compaction-
4242
import { maybeCompactAgentHarnessSession as maybeCompactAgentHarnessSessionImpl } from "../harness/compaction.js";
4343
import { ensureSelectedAgentHarnessPlugin as ensureSelectedAgentHarnessPluginImpl } from "../harness/runtime-plugin.js";
4444
import { acquireAgentRunPreparedModelRuntime } from "../prepared-model-runtime.js";
45+
import type { PreparedModelRuntimePluginGeneration } from "../prepared-model-runtime.types.js";
4546
import { SessionManager } from "../sessions/session-manager.js";
4647
import {
4748
clearCliSessionInStore as clearCliSessionInStoreImpl,
@@ -420,28 +421,32 @@ async function compactNativeHarnessCliTranscript(params: {
420421
senderIsOwner?: boolean;
421422
thinkLevel?: Parameters<typeof buildEmbeddedCompactionRuntimeContext>[0]["thinkLevel"];
422423
extraSystemPrompt?: string;
424+
pluginGeneration?: PreparedModelRuntimePluginGeneration;
423425
}): Promise<NativeHarnessCliCompactionOutcome> {
424426
let result: EmbeddedAgentCompactResult | undefined;
425427
try {
426428
const sessionAgentId = readAgentIdFromSessionKey(params.sessionKey);
427429
const nativeHarnessId = params.sessionEntry.agentHarnessId?.trim();
428430
const modelSelectionLocked = params.sessionEntry.modelSelectionLocked === true;
429431
const authProfileId = params.sessionEntry.authProfileOverride?.trim() || undefined;
430-
const preparedRuntimeLease = await cliCompactionDeps.acquirePreparedModelRuntime({
431-
config: params.cfg,
432-
...(sessionAgentId ? { agentId: sessionAgentId } : {}),
433-
agentDir: params.agentDir,
434-
workspaceDir: params.workspaceDir,
435-
allowGatewaySubagentBinding: true,
436-
runtimePluginSelections: [
437-
{
438-
provider: params.provider,
439-
modelId: params.model,
440-
...(sessionAgentId ? { agentId: sessionAgentId } : {}),
441-
...(nativeHarnessId ? { runtime: nativeHarnessId } : {}),
442-
},
443-
],
444-
});
432+
const preparedRuntimeLease = await cliCompactionDeps.acquirePreparedModelRuntime(
433+
{
434+
config: params.cfg,
435+
...(sessionAgentId ? { agentId: sessionAgentId } : {}),
436+
agentDir: params.agentDir,
437+
workspaceDir: params.workspaceDir,
438+
allowGatewaySubagentBinding: true,
439+
runtimePluginSelections: [
440+
{
441+
provider: params.provider,
442+
modelId: params.model,
443+
...(sessionAgentId ? { agentId: sessionAgentId } : {}),
444+
...(nativeHarnessId ? { runtime: nativeHarnessId } : {}),
445+
},
446+
],
447+
},
448+
params.pluginGeneration ? { pluginGeneration: params.pluginGeneration } : {},
449+
);
445450
try {
446451
const preparedModelRuntime = preparedRuntimeLease.snapshot;
447452
result = await withPluginRuntimeGenerationScope(preparedModelRuntime, async () => {
@@ -586,6 +591,7 @@ export async function runCliTurnCompactionLifecycle(params: {
586591
senderIsOwner?: boolean;
587592
thinkLevel?: Parameters<typeof buildEmbeddedCompactionRuntimeContext>[0]["thinkLevel"];
588593
extraSystemPrompt?: string;
594+
pluginGeneration?: PreparedModelRuntimePluginGeneration;
589595
}): Promise<SessionEntry | undefined> {
590596
const contextTokenBudget = resolvePositiveInteger(params.sessionEntry?.contextTokens);
591597
if (!params.storePath || !contextTokenBudget) {
@@ -691,6 +697,7 @@ export async function runCliTurnCompactionLifecycle(params: {
691697
senderIsOwner: params.senderIsOwner,
692698
thinkLevel: params.thinkLevel,
693699
extraSystemPrompt: params.extraSystemPrompt,
700+
pluginGeneration: params.pluginGeneration,
694701
});
695702
if (nativeOutcome.compacted) {
696703
compactionKind = "native-harness";

‎src/agents/command/post-run.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ export async function finalizeEmbeddedAgentCommand(params: {
355355
senderIsOwner: params.opts.senderIsOwner,
356356
thinkLevel: effectiveTurnThinkLevel,
357357
extraSystemPrompt: params.opts.extraSystemPrompt,
358+
pluginGeneration: params.prepared.commandRuntimeContext?.pluginGeneration,
358359
});
359360
throwAgentRunRestartAbortReason(params.opts.abortSignal?.reason);
360361
assertAgentRunLifecycleGenerationCurrent(lifecycleGeneration);

‎src/agents/prepared-model-runtime-lease.ts‎

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/** Agent-run lease admission for lifecycle-owned prepared model runtimes. */
2-
import fsp from "node:fs/promises";
3-
import path from "node:path";
42
import { isReservedSystemAgentId } from "../system-agent/agent-id.js";
53
import {
64
PreparedModelRuntimeOwnerNotPublishedError,
@@ -10,7 +8,6 @@ import {
108
normalizePreparedModelRuntimeInput,
119
publishModelRuntimeSnapshot,
1210
rebindInputToCommittedConfiguredOwner,
13-
resolveCommittedConfiguredOwner,
1411
type PreparedModelRuntimeInput,
1512
type PreparedModelRuntimeLease,
1613
type PreparedModelRuntimeOwner,
@@ -23,7 +20,6 @@ import type { PreparedModelRuntimeCatalogMode } from "./prepared-model-runtime.t
2320
type PreparedModelRuntimeLeaseContext = {
2421
owners: Map<string, PreparedModelRuntimeOwner>;
2522
agentBuildCompletions: Map<string, Promise<void>>;
26-
workspacePluginRootPresenceResolutions: Map<string, Promise<boolean | undefined>>;
2723
retainedDirectRunOwners: PreparedModelRuntimeOwnerRetention;
2824
retainedGatewayRunOwners: PreparedModelRuntimeOwnerRetention;
2925
getBuildTimeoutMs(): number;
@@ -32,66 +28,6 @@ type PreparedModelRuntimeLeaseContext = {
3228
prepareSnapshot(input: PreparedModelRuntimeInput): Promise<PreparedModelRuntimeSnapshot>;
3329
};
3430

35-
function resolveReusableConfiguredPluginGeneration(
36-
input: PreparedModelRuntimeInput,
37-
workspacePluginRootPresent: boolean | undefined,
38-
context: PreparedModelRuntimeLeaseContext,
39-
) {
40-
if (workspacePluginRootPresent !== false) {
41-
return undefined;
42-
}
43-
const owner = resolveCommittedConfiguredOwner(context.owners, input);
44-
const generation = owner?.pluginGeneration;
45-
if (
46-
!owner ||
47-
!generation ||
48-
JSON.stringify(owner.input.runtimePluginSelections) !==
49-
JSON.stringify(input.runtimePluginSelections) ||
50-
generation.pluginMetadataSnapshot.index.plugins.some((plugin) => plugin.origin === "workspace")
51-
) {
52-
return undefined;
53-
}
54-
return generation;
55-
}
56-
57-
async function resolveCoalescedWorkspacePluginRootPresence(
58-
input: PreparedModelRuntimeInput,
59-
context: PreparedModelRuntimeLeaseContext,
60-
): Promise<boolean | undefined> {
61-
const key = ownerKey(input);
62-
const existing = context.workspacePluginRootPresenceResolutions.get(key);
63-
if (existing) {
64-
return await existing;
65-
}
66-
const pending = resolveWorkspacePluginRootPresence(input);
67-
context.workspacePluginRootPresenceResolutions.set(key, pending);
68-
try {
69-
return await pending;
70-
} finally {
71-
if (context.workspacePluginRootPresenceResolutions.get(key) === pending) {
72-
context.workspacePluginRootPresenceResolutions.delete(key);
73-
}
74-
}
75-
}
76-
77-
export async function resolveWorkspacePluginRootPresence(
78-
input: PreparedModelRuntimeInput,
79-
): Promise<boolean | undefined> {
80-
if (input.workspacePluginRootPresent !== undefined || !input.workspaceDir) {
81-
return input.workspacePluginRootPresent;
82-
}
83-
return await fsp
84-
.stat(path.join(input.workspaceDir, ".openclaw", "extensions"))
85-
.then(() => true)
86-
.catch((error: unknown) => {
87-
const code = (error as NodeJS.ErrnoException).code;
88-
if (code === "ENOENT" || code === "ENOTDIR") {
89-
return false;
90-
}
91-
throw error;
92-
});
93-
}
94-
9531
export async function acquirePreparedModelRuntimeLeaseFromOwners(
9632
rawInput: PreparedModelRuntimeInput,
9733
provenance: "run" | "ephemeral",
@@ -121,18 +57,7 @@ export async function acquirePreparedModelRuntimeLeaseFromOwners(
12157
}
12258
}
12359
}
124-
const retainedWorkspacePluginRootPresent = context.owners.get(ownerKey(normalizedInput))?.input
125-
.workspacePluginRootPresent;
126-
const workspacePluginRootPresent =
127-
rawInput.workspacePluginRootPresent ??
128-
retainedWorkspacePluginRootPresent ??
129-
(provenance === "run"
130-
? await resolveCoalescedWorkspacePluginRootPresence(normalizedInput, context)
131-
: undefined);
132-
let input = normalizePreparedModelRuntimeInput({
133-
...normalizedInput,
134-
...(workspacePluginRootPresent === undefined ? {} : { workspacePluginRootPresent }),
135-
});
60+
let input = normalizedInput;
13661
let key = ownerKey(input);
13762
let owner: PreparedModelRuntimeOwner;
13863
let snapshot: PreparedModelRuntimeSnapshot;
@@ -187,9 +112,6 @@ export async function acquirePreparedModelRuntimeLeaseFromOwners(
187112
}
188113
}
189114
try {
190-
const reusablePluginGeneration =
191-
options.pluginGeneration ??
192-
resolveReusableConfiguredPluginGeneration(input, workspacePluginRootPresent, context);
193115
if (existing && !staleDynamicOwner) {
194116
snapshot = await context.prepareSnapshot(input);
195117
} else {
@@ -204,7 +126,7 @@ export async function acquirePreparedModelRuntimeLeaseFromOwners(
204126
undefined,
205127
provenance,
206128
options.catalogMode,
207-
reusablePluginGeneration,
129+
options.pluginGeneration,
208130
);
209131
}
210132
} catch (error) {

‎src/agents/prepared-model-runtime.lifecycle.test.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
getPreparedModelRuntimeTestApi,
66
resetPreparedModelRuntimeHarness,
77
} from "./prepared-model-runtime.test-harness.js";
8-
import fsp from "node:fs/promises";
9-
import path from "node:path";
108
import { beforeEach, describe, expect, it, vi } from "vitest";
119
import { retainLegacyDefaultAgentId } from "../config/legacy.default-agent-owner.js";
1210
import {
@@ -243,9 +241,6 @@ describe("prepared model runtime snapshots", () => {
243241
gatewayLifecycle: true,
244242
defaultWorkspaceDir: "/tmp/gateway-launch-workspace",
245243
});
246-
const workspacePluginRoot = path.join(workspaceDir, ".openclaw", "extensions");
247-
const statSpy = vi.spyOn(fsp, "stat");
248-
249244
const acquireDynamicLease = () =>
250245
acquireAgentRunPreparedModelRuntime({
251246
agentId: "default",
@@ -273,10 +268,6 @@ describe("prepared model runtime snapshots", () => {
273268
expect(retainedLease.snapshot).toBe(firstLease.snapshot);
274269
retainedLease.release();
275270
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
276-
expect(
277-
statSpy.mock.calls.filter(([target]) => String(target) === workspacePluginRoot),
278-
).toHaveLength(1);
279-
statSpy.mockRestore();
280271
});
281272

282273
it("joins an in-flight dynamic owner publication", async () => {

0 commit comments

Comments
 (0)