test(perf): narrow codex session key test

This commit is contained in:
Peter Steinberger
2026-05-06 12:45:32 +01:00
parent 855a7c7be7
commit 5d7878dff1
2 changed files with 27 additions and 57 deletions

View File

@@ -509,64 +509,25 @@ describe("runCodexAppServerAttempt", () => {
expect(__testing.shouldForceMessageTool(params)).toBe(false);
});
it("passes the live run session key to Codex dynamic tools when sandbox policy uses another key", async () => {
it("passes the live run session key to Codex dynamic tools when sandbox policy uses another key", () => {
const workspaceDir = path.join(tempDir, "workspace");
const sessionsPath = path.join(tempDir, "sessions.json");
const params = createParams(path.join(tempDir, "session.jsonl"), workspaceDir);
params.disableTools = false;
params.sessionKey = "agent:main:main";
params.config = {
session: { store: sessionsPath, mainKey: "main", scope: "per-sender" },
tools: { profile: "coding" },
};
params.runtimePlan = buildCodexRuntimePlan(params, workspaceDir);
await fs.writeFile(
sessionsPath,
JSON.stringify({
"agent:main:main": {
sessionId: "s-main",
updatedAt: 10,
status: "running",
},
"agent:main:telegram:default:direct:1234": {
sessionId: "s-telegram-policy",
updatedAt: 5,
status: "done",
},
}),
);
let seenRunSessionKey: string | undefined;
__testing.setOpenClawCodingToolsFactoryForTests((options) => {
seenRunSessionKey = options?.runSessionKey;
return [
{
name: "session_status",
description: "session status test tool",
parameters: { type: "object", properties: {} },
execute: vi.fn(async () => ({ details: { sessionKey: options?.runSessionKey } })),
},
] as never;
expect(
__testing.resolveOpenClawCodingToolsSessionKeys(
params,
"agent:main:telegram:default:direct:1234",
),
).toEqual({
sessionKey: "agent:main:telegram:default:direct:1234",
runSessionKey: "agent:main:main",
});
const dynamicTools = await __testing.buildDynamicTools({
params,
resolvedWorkspace: workspaceDir,
effectiveWorkspace: workspaceDir,
sandboxSessionKey: "agent:main:telegram:default:direct:1234",
sandbox: null,
runAbortController: new AbortController(),
sessionAgentId: "main",
pluginConfig: {},
onYieldDetected: () => undefined,
expect(__testing.resolveOpenClawCodingToolsSessionKeys(params, "agent:main:main")).toEqual({
sessionKey: "agent:main:main",
runSessionKey: undefined,
});
const sessionStatus = dynamicTools.find((tool) => tool.name === "session_status");
expect(sessionStatus).toBeDefined();
const result = await sessionStatus?.execute("call-current", { sessionKey: "current" });
expect(seenRunSessionKey).toBe("agent:main:main");
expect((result?.details as { sessionKey?: string } | undefined)?.sessionKey).toBe(
"agent:main:main",
);
});
it("returns a failed dynamic tool response when an app-server tool call exceeds the deadline", async () => {

View File

@@ -1515,6 +1515,17 @@ type DynamicToolBuildParams = {
onYieldDetected: () => void;
};
function resolveOpenClawCodingToolsSessionKeys(
params: EmbeddedRunAttemptParams,
sandboxSessionKey: string,
): Pick<OpenClawCodingToolsOptions, "sessionKey" | "runSessionKey"> {
return {
sessionKey: sandboxSessionKey,
runSessionKey:
params.sessionKey && params.sessionKey !== sandboxSessionKey ? params.sessionKey : undefined,
};
}
async function buildDynamicTools(input: DynamicToolBuildParams) {
const { params } = input;
if (params.disableTools || !supportsModelTools(params.model)) {
@@ -1525,6 +1536,7 @@ async function buildDynamicTools(input: DynamicToolBuildParams) {
const createOpenClawCodingTools =
openClawCodingToolsFactoryForTests ??
(await import("openclaw/plugin-sdk/agent-harness")).createOpenClawCodingTools;
const sessionKeys = resolveOpenClawCodingToolsSessionKeys(params, input.sandboxSessionKey);
const allTools = createOpenClawCodingTools({
agentId: input.sessionAgentId,
...buildEmbeddedAttemptToolRunContext(params),
@@ -1547,11 +1559,7 @@ async function buildDynamicTools(input: DynamicToolBuildParams) {
senderE164: params.senderE164,
senderIsOwner: params.senderIsOwner,
allowGatewaySubagentBinding: params.allowGatewaySubagentBinding,
sessionKey: input.sandboxSessionKey,
runSessionKey:
params.sessionKey && params.sessionKey !== input.sandboxSessionKey
? params.sessionKey
: undefined,
...sessionKeys,
sessionId: params.sessionId,
runId: params.runId,
agentDir,
@@ -1987,6 +1995,7 @@ export const __testing = {
buildDynamicTools,
filterToolsForVisionInputs,
handleDynamicToolCallWithTimeout,
resolveOpenClawCodingToolsSessionKeys,
shouldForceMessageTool,
setOpenClawCodingToolsFactoryForTests(factory: OpenClawCodingToolsFactory): void {
openClawCodingToolsFactoryForTests = factory;