Skip to content

fix(agent): apply toModelOutput to late background tool results - #174

Open
alexb-openrouter wants to merge 1 commit into
mainfrom
devin/1796054467-late-background-to-model-output
Open

alexb-openrouter wants to merge 1 commit into
mainfrom
devin/1796054467-late-background-to-model-output

Conversation

@alexb-openrouter

@alexb-openrouter alexb-openrouter commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A lifecycle: 'background' tool whose work outlives graceMs is delivered to the model as a tool_task_result user message. That envelope embedded the raw execute result, so a toModelOutput mapper that hides fields was bypassed on this path (both in-run delivery in flushAsyncToolDeliveries and the envelope persisted after the run in dropSettledTasks).

Both paths now go through a new lateTaskForModel(task) before building the envelope:

completed task + tool has toModelOutput:
  input = safeParse(inputSchema, task.input) ? parsed : raw   // same as computeToolOutputForModel
  mapped = await toModelOutput({ output: task.result, input })
  mapped.type in {'content','json'} -> envelope.result = mapped.value
  otherwise / mapper throws          -> envelope.status = 'failed', envelope.error = message (raw result is not sent)

The persisted pendingAsyncTools terminal status follows the delivered status. tool.result events and PostToolUse hooks still receive the raw result, as on the in-round path.

The json branch is checked at runtime, so this works on main (where ToModelOutputResult only declares content) and with #173, which adds json to the type and handles it on the other result paths. This PR covers the late-background path that #173 leaves out of scope. It merges cleanly with #171, #172 and #173; with #173 merged in, both test files pass.

Not covered here: deferred tasks completed through resumeToolResults build their envelope in buildResumeEnvelope and also do not call toModelOutput.

Tests

tests/unit/to-model-output-late-background.test.ts (fails on main, passes here): content mapper with schema-parsed input, json mapper, unknown mapper type, throwing mapper, raw-argument fallback on parse failure, and the envelope persisted after an onRunEnd: 'cancel' run. Full unit suite, typecheck and lint pass.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

devin-ai-integration Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring

@alexb-openrouter
alexb-openrouter marked this pull request as ready for review November 30, 2026 16:03
@devin-ai-integration

Copy link
Copy Markdown
Contributor

@strix-security

This branch has not been deployed

No deployments
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.

1 participant