Skip to content

Handle routing failures - #591

Merged
josephjclark merged 12 commits into
release/nextfrom
router-recovery
Jul 30, 2026
Merged

Handle routing failures#591
josephjclark merged 12 commits into
release/nextfrom
router-recovery

Conversation

@hanna-paasivirta

@hanna-paasivirta hanna-paasivirta commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Short Description

Gives global chat's subagents a way to recover when the router sends a request to the wrong place, instead of telling the user "I can't see your job code". Misrouted requests are now handled invisibly: the subagent either pulls the context it's missing or hands the request back for rerouting to the planner.

Fixes #506

Implementation Details

Global chat presents as a single assistant, but the router will inevitably misroute some requests. Today a misrouted subagent dead-ends: job_chat can't see the workflow, workflow_chat declines code questions and tells the user to navigate to the Inspector. Both replies leak the internal architecture and strand the user. This PR adds two recovery mechanisms, following the principle that the subagent should either get what it needs or get out of the way.

Pull missing context. In subagent mode, job_chat receives the workflow structure (bodies redacted, ids stripped to save tokens) and an inspect_job_code tool to read other steps' code on demand; this is the same tool the planner uses, moved to a shared module. This keeps the fast direct route viable for requests that reference other steps, rather than paying for a planner run.

Example: The job code subagent can examine other steps and has better knowledge of when this might be relevant because it's given the workflow outline:
Fn Assistant Global (experimentan)

Hand back when the request lands elsewhere. Changes beyond the focused step can't be done from job_chat, so it gets an inspect_workflow tool that immediately returns the request to the router, which re-runs it through the planner as if routed there first. The tool is deliberately framed as a capability rather than an escalation: models narrate before acting, and a slipped sentence should read "I'll take a look at your workflow", which stays truthful once the planner takes over. workflow_chat hands back through a handover field in its structured output schema instead of a tool, because mixing tools into forced structured output has produced garbled output before; the field is generated first, so streaming can suppress all output before anything reaches the user. A shared stream manager across router, subagents and planner keeps a handover to a single message lifecycle, so the user never sees the aborted attempt.

Example:
image

Logs for the example, showing the initial misrouting to the job code agent that can only edit one step, and how it hands over back to the planner agent that can orchestrate several step edits in one go:

INFO:global_chat.global_chat:Global agent called with content: like all waves...
INFO:global_chat.router:RouterAgent initialized with model: claude-haiku-4-5-20251001
INFO:global_chat.router:Router.route_and_execute() called
INFO:global_chat.router:Router decision: job_code_agent (confidence: 4, job_key: create-demo-in-sf)
**INFO:global_chat.router:Routing to job_chat**
INFO:search_adaptor_docs:Checking/loading adaptor docs for @openfn/language-salesforce@4.8.6
INFO:load_adaptor_docs:Checking if docs already exist in database
INFO:load_adaptor_docs:✓ Docs already exist for @openfn/language-salesforce@4.8.6 (44 functions)
INFO:search_adaptor_docs:Adaptor docs for @openfn/language-salesforce@4.8.6 already exist (checked in 0.008s)
INFO:job_chat:Making streaming API call
**INFO:job_chat:job_chat round 0: model called inspect_workflow**
INFO:job_chat:job_chat handing over: Remove all wave emojis from the console.log banners across all Salesforce steps
**WARNING:global_chat.router:job_code_agent handed over: Remove all wave emojis from the console.log banners across all Salesforce steps. Rerouting to planner**
**INFO:global_chat.router:Routing to planner**
INFO:global_chat.planner:PlannerAgent initialized with model: claude-opus-4-8
INFO:global_chat.planner:Planner.run() called
INFO:global_chat.planner:Claude API call 1: stop_reason=tool_use
INFO:global_chat.planner:Executing 3 tool(s): ['call_job_code_agent', 'call_job_code_agent', 'call_job_code_agent']
INFO:global_chat.subagent_caller:Calling job_agent (job_key=create-demo-in-sf): Remove all wave emojis (🌊) from the console.log banner at the top of the step. Leave the rest of the banner and code unc
INFO:global_chat.subagent_caller:Calling job_agent (job_key=create-in-SF): Remove all wave emojis (🌊) from the console.log banner at the top of the step. Leave the rest of the banner and code unc
INFO:global_chat.subagent_caller:Calling job_agent (job_key=update-in-SF): Remove all wave emojis (🌊) from the console.log banner at the top of the step. Leave the rest of the banner and code unc

There is a new Langfuse tag for conversations that have been handed over to the planner not via the router, so that we can keep an eye on how many requests were misrouted.

Prompt updates Both subagents' prompts drop the outdated "send the user to another page" instructions in subagent mode rather than contradicting them: what the model never reads, it can't say. In job_chat this is done by composing the system role from parts; in workflow_chat by a guarded string replace, since its YAML prompt templating makes placeholder slots risky. Production behaviour is unchanged: neither the subagent flag nor workflow_yaml is sent by direct callers, and unit tests pin that the production prompts and response shapes are untouched.

The router also now reroutes low-confidence direct routes (confidence < 3) to the planner, using the confidence score it already produces. It rarely outputs low-confidence scores however, so this will have little impact without further adjustments.

Adds unit tests for the routing, handover parsing and prompt gating, and live integration tests (marked integration, manual/nightly) covering curated misroutes, including oblique phrasings and a wrong-focused-step case.

AI Usage

Please disclose whether you've used AI in this work (it's cool, we just want to
know!):

  • Yes, I have not used AI
  • No, I have not used AI

You can read more details in our
Responsible AI Policy

@hanna-paasivirta
hanna-paasivirta marked this pull request as ready for review July 22, 2026 19:11

@josephjclark josephjclark left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't test this super well, despite a few efforts, but it looks great and certainly doesn't appear to do any harm! Let's merge it and track in prod. Thanks @hanna-paasivirta

@josephjclark
josephjclark changed the base branch from main to release/next July 29, 2026 10:31
@josephjclark

Copy link
Copy Markdown
Collaborator

@hanna-paasivirta I've rebased this against the release and it caused some tests to fail.I've fixed the tests, I don't think it's a big deal, but before I fold this down I'd apprecaite if you could give the branch a once-over? Check I've not missed something?

@hanna-paasivirta

Copy link
Copy Markdown
Contributor Author

@josephjclark This looks ok to me. Thank you!

@josephjclark
josephjclark merged commit 6c322ae into release/next Jul 30, 2026
2 checks passed
@josephjclark
josephjclark deleted the router-recovery branch July 30, 2026 16:52
josephjclark added a commit that referenced this pull request Jul 30, 2026
* Global chat: Add answer streaming to planner (#573)

* add segments

* adjust events

* add event type

* adjust streaming

* update readme

* changeset

---------

Co-authored-by: Joe Clark <jclark@openfn.org>

* Bump the python-minor-patch group across 1 directory with 8 updates (#601)

Bumps the python-minor-patch group with 8 updates in the / directory:

| Package | From | To |
| --- | --- | --- |
| [openai](https://github.com/openai/openai-python) | `2.45.0` | `2.48.0` |
| [anthropic](https://github.com/anthropics/anthropic-sdk-python) | `0.116.0` | `0.119.0` |
| [langchain-core](https://github.com/langchain-ai/langchain) | `1.4.9` | `1.5.1` |
| [langchain-openai](https://github.com/langchain-ai/langchain) | `1.3.4` | `1.4.1` |
| [sentry-sdk](https://github.com/getsentry/sentry-python) | `2.64.0` | `2.66.1` |
| [langfuse](https://github.com/langfuse/langfuse) | `4.14.0` | `4.14.1` |
| [opentelemetry-instrumentation-threading](https://github.com/open-telemetry/opentelemetry-python-contrib) | `0.64b0` | `0.65b0` |
| [ruff](https://github.com/astral-sh/ruff) | `0.15.21` | `0.16.0` |



Updates `openai` from 2.45.0 to 2.48.0
- [Release notes](https://github.com/openai/openai-python/releases)
- [Changelog](https://github.com/openai/openai-python/blob/main/CHANGELOG.md)
- [Commits](openai/openai-python@v2.45.0...v2.48.0)

Updates `anthropic` from 0.116.0 to 0.119.0
- [Release notes](https://github.com/anthropics/anthropic-sdk-python/releases)
- [Changelog](https://github.com/anthropics/anthropic-sdk-python/blob/main/CHANGELOG.md)
- [Commits](anthropics/anthropic-sdk-python@v0.116.0...v0.119.0)

Updates `langchain-core` from 1.4.9 to 1.5.1
- [Release notes](https://github.com/langchain-ai/langchain/releases)
- [Commits](langchain-ai/langchain@langchain-core==1.4.9...langchain-core==1.5.1)

Updates `langchain-openai` from 1.3.4 to 1.4.1
- [Release notes](https://github.com/langchain-ai/langchain/releases)
- [Commits](langchain-ai/langchain@langchain-openai==1.3.4...langchain-openai==1.4.1)

Updates `sentry-sdk` from 2.64.0 to 2.66.1
- [Release notes](https://github.com/getsentry/sentry-python/releases)
- [Changelog](https://github.com/getsentry/sentry-python/blob/master/CHANGELOG.md)
- [Commits](getsentry/sentry-python@2.64.0...2.66.1)

Updates `langfuse` from 4.14.0 to 4.14.1
- [Release notes](https://github.com/langfuse/langfuse/releases)
- [Commits](https://github.com/langfuse/langfuse/commits)

Updates `opentelemetry-instrumentation-threading` from 0.64b0 to 0.65b0
- [Release notes](https://github.com/open-telemetry/opentelemetry-python-contrib/releases)
- [Changelog](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/CHANGELOG.md)
- [Commits](https://github.com/open-telemetry/opentelemetry-python-contrib/commits)

Updates `ruff` from 0.15.21 to 0.16.0
- [Release notes](https://github.com/astral-sh/ruff/releases)
- [Changelog](https://github.com/astral-sh/ruff/blob/main/CHANGELOG.md)
- [Commits](astral-sh/ruff@0.15.21...0.16.0)

---
updated-dependencies:
- dependency-name: openai
  dependency-version: 2.48.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: python-minor-patch
- dependency-name: anthropic
  dependency-version: 0.119.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: python-minor-patch
- dependency-name: langchain-core
  dependency-version: 1.5.1
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: python-minor-patch
- dependency-name: langchain-openai
  dependency-version: 1.4.1
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: python-minor-patch
- dependency-name: sentry-sdk
  dependency-version: 2.66.1
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: python-minor-patch
- dependency-name: langfuse
  dependency-version: 4.14.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: python-minor-patch
- dependency-name: opentelemetry-instrumentation-threading
  dependency-version: 0.65b0
  dependency-type: direct:production
  dependency-group: python-minor-patch
- dependency-name: ruff
  dependency-version: 0.16.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: python-minor-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump actions/setup-python from 6 to 7 (#600)

Bumps [actions/setup-python](https://github.com/actions/setup-python) from 6 to 7.
- [Release notes](https://github.com/actions/setup-python/releases)
- [Commits](actions/setup-python@v6...v7)

---
updated-dependencies:
- dependency-name: actions/setup-python
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Handle routing failures (#591)

* add subagent mode and inspect tool

* edits and tests

* udpate subagent prompt

* add navigation info

* add langfuse tracing

* rename inspect_workflow tool to edit_workflow

* changeset

* resolve conflict

* update test fixtures

* add coherence instruction

* fix prompt for existing workflows

---------

Co-authored-by: Joe Clark <jclark@openfn.org>

* version: 3.1.0

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Hanna Paasivirta <hanna@openfn.org>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global chat: Handling routing failures

2 participants