Repository navigation
Description
What happens
When a Workflow resumes a paused run by passing a RunState to TemporalOpenAIRunner.run, tools that run after the resume write a copy of the run context, not the context object the run was started with. The caller's object, and the next turn started with it, never see those writes. Nothing fails or warns.
The upstream context guide says tools can read from or modify the object you pass to run(), and the human-in-the-loop guide resumes an approval from the same RunState. With the upstream Runner, an approved tool writes the caller's object. A process that resumes from a string can inject its own context with RunState.fromStringWithContext; through the plugin, that injected object is lost too.
The README says TemporalOpenAIRunner mirrors the upstream Runner and accepts a RunState, and lists its differences; this is not one of them. Passing { context: ctx } again with the state does not help, because the runner ignores context for a RunState input.
Cause
For a RunState input, TemporalOpenAIRunner.run (src/workflow/runner.ts) rebuilds the state with RunState.fromString(converted, input.toString()), so that the state points at the converted agent graph. toString() writes the context as JSON, and fromString builds a new RunContext from that JSON. The resumed run then uses the copy. The upstream Runner uses the passed RunState, and its context, as is.
Reproduction
One file, repro.cjs. It runs the two resume forms twice: with the upstream Runner (control) and through TemporalOpenAIRunner in a Workflow on a local test server, with a fake model. The agent's one tool needs approval and writes runContext.context.after. The first form approves and resumes the same RunState; the second rebuilds the state from its string with an injected context, then approves and resumes. Exit 1 means the bug is present.
npm i @temporalio/openai-agents @temporalio/testing @temporalio/worker @temporalio/client @openai/agents-core
node repro.cjsrepro.cjs
// After an approval resume, TemporalOpenAIRunner's tools write a copy of the run context, not the
// caller's object. One file: the Workflow is bundled from this file. Exits 1 while the bug is present.
// npm i @temporalio/openai-agents @temporalio/testing @temporalio/worker @temporalio/client @openai/agents-core
// node repro.cjs
const { Agent, RunContext, RunState, tool } = require('@openai/agents-core');
const { TemporalOpenAIRunner } = require('@temporalio/openai-agents/workflow');
// A tool that needs approval and writes the run context when it runs.
function makeAgent() {
const setAfter = tool({
name: 'set_after',
description: 'Set a value after approval.',
parameters: { type: 'object', properties: { value: { type: 'string' } }, required: ['value'], additionalProperties: false },
needsApproval: true,
execute: async ({ value }, runContext) => {
runContext.context.after = value;
return 'ok';
},
});
return new Agent({ name: 'ResumeAgent', instructions: 'Set the value.', model: 'fake', tools: [setAfter] });
}
// Both resume forms from the upstream human-in-the-loop guide: the same RunState, and a state
// rebuilt from its string with an injected context.
async function resume(runner) {
const agent = makeAgent();
const ctx = { original: 'orig' };
const first = await runner.run(agent, 'go', { context: ctx });
first.state.approve(first.interruptions[0]);
const resumed = await runner.run(agent, first.state);
const injected = { original: 'injected' };
const second = await runner.run(agent, 'go', { context: { original: 'other' } });
const rebuilt = await RunState.fromStringWithContext(agent, second.state.toString(), new RunContext(injected));
rebuilt.approve(rebuilt.getInterruptions()[0]);
const resumedRebuilt = await runner.run(agent, rebuilt);
return {
sameState: { callerAfter: ctx.after ?? null, usesCallerObject: resumed.runContext.context === ctx },
rebuiltState: { injectedAfter: injected.after ?? null, usesInjectedObject: resumedRebuilt.runContext.context === injected },
};
}
async function resumeWorkflow() {
return resume(new TemporalOpenAIRunner());
}
module.exports = { resumeWorkflow };
async function main() {
// Node-only modules, kept out of the Workflow bundle.
const nodeRequire = require('module').createRequire(__filename);
const { Runner, Usage, setTracingDisabled } = nodeRequire('@openai/agents-core');
setTracingDisabled(true);
const { TestWorkflowEnvironment } = nodeRequire('@temporalio/testing');
const { Worker } = nodeRequire('@temporalio/worker');
const { OpenAIAgentsPlugin } = nodeRequire('@temporalio/openai-agents');
// Calls set_after on "go", then answers "done" after the tool result.
function fakeModel() {
const usage = () => new Usage({ requests: 1, inputTokens: 1, outputTokens: 1, totalTokens: 2 });
return {
async getResponse(request) {
const items = typeof request.input === 'string' ? [{ role: 'user', content: request.input }] : request.input;
if (items.some((i) => i.type === 'function_call_result')) {
return {
usage: usage(),
output: [{ type: 'message', id: 'm', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: 'done' }] }],
};
}
return {
usage: usage(),
output: [{ type: 'function_call', callId: 'call1', name: 'set_after', arguments: '{"value":"S-after"}', status: 'completed' }],
};
},
async *getStreamedResponse() {
throw new Error('not used');
},
};
}
// Control: the upstream Runner, no Temporal.
const model = fakeModel();
const native = await resume(new Runner({ modelProvider: { getModel: () => model }, tracingDisabled: true }));
const env = await TestWorkflowEnvironment.createLocal();
try {
const plugin = new OpenAIAgentsPlugin({ modelProvider: { getModel: () => fakeModel() } });
const worker = await Worker.create({
connection: env.nativeConnection,
taskQueue: 'resume',
workflowsPath: __filename,
bundlerOptions: { ignoreModules: ['module'] },
plugins: [plugin],
});
const temporal = await worker.runUntil(
env.client.workflow.execute(resumeWorkflow, { taskQueue: 'resume', workflowId: `resume-${Date.now()}` })
);
console.log('upstream Runner: ', JSON.stringify(native));
console.log('TemporalOpenAIRunner: ', JSON.stringify(temporal));
const same = JSON.stringify(native) === JSON.stringify(temporal);
console.log(same ? 'OK: resumed tools wrote the caller\'s context object' : 'BUG: resumed tools wrote a copy of the context');
process.exitCode = same ? 0 : 1;
} finally {
await env.teardown();
}
}
if (typeof process === 'object' && require.main === module) {
main().catch((e) => {
console.error(e);
process.exit(2);
});
}Output with @temporalio/openai-agents 1.24.0 (the latest release), @openai/agents-core 0.14.3, Node 24.22.0:
Temporal CLI 1.9.1 (Server 1.32.0, UI 2.54.1)
Temporal Server: localhost:41867
Temporal Persistence: in-memory
Temporal Metrics: http://localhost:35813/metrics
upstream Runner: {"sameState":{"callerAfter":"S-after","usesCallerObject":true},"rebuiltState":{"injectedAfter":"S-after","usesInjectedObject":true}}
TemporalOpenAIRunner: {"sameState":{"callerAfter":null,"usesCallerObject":false},"rebuiltState":{"injectedAfter":null,"usesInjectedObject":false}}
BUG: resumed tools wrote a copy of the context
The same happens on main at c664a34:
Temporal CLI 1.9.1 (Server 1.32.0, UI 2.54.1)
Temporal Server: localhost:37431
Temporal Persistence: in-memory
Temporal Metrics: http://localhost:41685/metrics
upstream Runner: {"sameState":{"callerAfter":"S-after","usesCallerObject":true},"rebuiltState":{"injectedAfter":"S-after","usesInjectedObject":true}}
TemporalOpenAIRunner: {"sameState":{"callerAfter":null,"usesCallerObject":false},"rebuiltState":{"injectedAfter":null,"usesInjectedObject":false}}
BUG: resumed tools wrote a copy of the context
- Expected: as with the upstream
Runner, the resumed run uses the caller's (or the injected) object, and the tool's write is in it. - Observed: the resumed run uses a copy; the caller's object and the injected object stay without
after. - Control: the upstream
Runnerkeeps the object in both forms.
Impact
An approval flow whose approved tool writes the run context, and that reads the caller's object afterwards (for example to start the next turn with it), silently continues with stale state. Context members that JSON can't carry, such as functions, Maps or class instances, are also missing in the resumed run. The README's own Continue-As-New approval recipe passes no context of its own, so it doesn't show the difference.
Proposed fix
Rebuild the state with RunState.fromStringWithContext(converted, input.toString(), input._context), so the resumed run keeps the state's own context, as the upstream Runner does. Resumed tools then write the caller's object in Workflow code, and a Workflow that reads it afterwards can take other commands than it did before, so runs that resumed before the fix must keep the copy on replay: the change needs a patched() gate.
A PR follows.
Activity
fix(openai-agents): refuse asTool() tracing keys, clear nested settin…