Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

test(sdk): steer from the handler, where a steer cannot lose the race

thinhlpg1701814f5c85
diff --git a/packages/agent-sdk/tests/sdk.test.mjs b/packages/agent-sdk/tests/sdk.test.mjs
index 737f3ab81..2d26bf9d4 100644
--- a/packages/agent-sdk/tests/sdk.test.mjs
+++ b/packages/agent-sdk/tests/sdk.test.mjs
@@ -518,19 +518,30 @@ test("a steer lands in the model's next request", { skip: missingRuntime }, asyn
const { provider, scratch, runtime } = await connect(t, {
replies: [TOOL_CALL('host__camera_observe', { frame: 1 }), PROSE('the left bench is clear')],
})
+ // The steer has to be in before the tool's answer goes back: that answer ends
+ // the window in which the runtime still sees the turn as steerable. The
+ // handler runs inside that window, so the steer cannot lose a race the way a
+ // listener on a dispatch thread can.
const session = await runtime.createSession({
cwd: scratch.projectPath,
model: 'stub-model',
ephemeral: true,
builtins: false,
permissions: 'host',
- tools: [{ name: 'camera_observe', capability: 'read', handler: () => ({ text: 'the bench' }) }],
+ tools: [
+ {
+ name: 'camera_observe',
+ capability: 'read',
+ handler: async () => {
+ await session.steer('also check the left bench')
+ return { text: 'the bench' }
+ },
+ },
+ ],
})
const turn = await session.prompt('look at the bench')
- for await (const event of turn) {
- if (event.type === 'tool_request') await session.steer('also check the left bench')
- }
+ for await (const _event of turn);
const result = await turn.result()
assert.equal(result.stopReason, 'completed')
diff --git a/sdk/python/tests/test_sdk.py b/sdk/python/tests/test_sdk.py
index cc4d47135..b9ed2ecbb 100644
--- a/sdk/python/tests/test_sdk.py
+++ b/sdk/python/tests/test_sdk.py
@@ -150,6 +150,15 @@ class SdkTest(unittest.TestCase):
def test_a_steer_lands_in_the_models_next_request(self) -> None:
+ # The steer has to be in before the tool's answer goes back: that answer
+ # ends the window in which the runtime still sees the turn as
+ # steerable. The handler runs inside that window; a listener does not,
+ # because the SDK dispatches listeners asynchronously - a steer from one
+ # races the answer, which is how this test used to fail on a slow runner.
+ def observe(_args, _call):
+ session.steer("also check the left bench")
+ return {"text": "the bench"}
+
scratch = self.prepare([tool_call("host__camera_observe", {"frame": 1}), prose("the left bench is clear")])
with scratch.start() as runtime:
session = runtime.create_session(
@@ -158,15 +167,11 @@ class SdkTest(unittest.TestCase):
ephemeral=True,
builtins=False,
permissions="host",
- tools=[
- HostTool(name="camera_observe", capability="read", handler=lambda _args, _call: {"text": "the bench"})
- ],
+ tools=[HostTool(name="camera_observe", capability="read", handler=observe)],
)
turn = session.prompt("look at the bench")
- for event in turn:
- if event["type"] == "tool_request":
- session.steer("also check the left bench")
-
+ events = list(turn)
+ self.assertTrue(events, "the turn streamed")
self.assertEqual(turn.result().stop_reason, "completed")
# The steer is delivered at the turn's next safe point, which is the
# request the runtime builds from the tool result.