GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add file persistence for large tool outputs by ben-vargas · Pull Request #6234 · anomalyco/opencode

Conversation

@ben-vargas

@ben-vargas ben-vargas commented Dec 27, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Edited 2026-01-07: Refactored to align to centralized truncation changes upstream; retain file persistence.

Summary

Enhances the centralized truncation system with file persistence for large tool outputs.

Background: Upstream recently added centralized truncation (Truncate.output() in tool.ts and registry.ts) which addresses "prompt is too long" errors by capping outputs at 50KB/2000 lines. However, this truncation permanently loses data - the model has no way to access content beyond the cutoff.

This PR retains the benefits of file persistence from the original approach: when outputs exceed the threshold, the full output is saved to disk and the model receives a preview with instructions for exploring the data using Read, Grep, or jq tools. The model can then make targeted requests to examine specific portions of large outputs.

Closes #4560
Related: #4826, #4845, #5360

Changes

truncation.ts - Extends the centralized truncation namespace with:

  • outputWithPersistence() - saves large outputs to ~/.local/share/opencode/storage/tool_results/{sessionID}/
  • Head (67%) + tail (33%) preview respecting caller's maxLines/maxBytes
  • JSON detection (.json extension + jq hint for structured queries)
  • 10MB hard cap on persisted files with proper multibyte handling
  • Path traversal protection (sessionID validation + resolve containment check)
  • cleanupSessionFiles() for session deletion lifecycle

tool.ts - Updates Tool.define() wrapper to use outputWithPersistence

registry.ts - Updates fromPlugin() to use outputWithPersistence

session/index.ts - Calls cleanup on session delete

Benefits over pure truncation

Aspect Upstream Truncation This PR
Prompt size ✅ Controlled ✅ Controlled
Data access ❌ Lost permanently ✅ Preserved on disk
Model exploration ❌ Cannot access ✅ Via Read/Grep/jq

Coverage

All tools automatically get file persistence:

  • ✅ Built-in tools via Tool.define() wrapper
  • ✅ Plugin tools via fromPlugin()
  • ✅ MCP tools (via registry)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

CybotTM, pcholakov, SeongwoongCho, mvelbaum, 381181295, shuv1337, cbrunnkvist, and satoruhiga reacted with thumbs up emoji
All reactions
  • 👍 8 reactions
@ben-vargas ben-vargas mentioned this pull request Dec 27, 2025
prompt is too long #5360
Closed
@codyw912 codyw912 mentioned this pull request Dec 29, 2025
Prompt is too long recovery compaction is not working code-yeongyu/oh-my-openagent#96
Closed
@mgunnin

mgunnin commented Dec 29, 2025

Copy link
Copy Markdown

I'm constantly having the prompt is too long error so this PR would be greatly appreciated.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mgunnin

mgunnin commented Dec 29, 2025

Copy link
Copy Markdown

Can we get this prioritized @fwang

ben-vargas reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@taxilian

taxilian commented Dec 29, 2025

Copy link
Copy Markdown

This seems likely to fix the single biggest usability issue with opencode that I keep hitting over and over and over... literally daily, multiple times per day with normal use of opencode.

ben-vargas reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@codyw912

codyw912 commented Dec 29, 2025

Copy link
Copy Markdown

This seems likely to fix the single biggest usability issue with opencode that I keep hitting over and over and over... literally daily, multiple times per day with normal use of opencode.

It's worked for me over the past few days. Haven't hit the "prompt too long" issue once.

ben-vargas reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dzianisv added a commit to VibeTechnologies/opencode that referenced this pull request Dec 30, 2025
@dzianisv
feat: add file persistence for large tool outputs
ecea1d8
Cherry-picked from PR anomalyco#6234 (ben-vargas/ai-opencode)

When tool outputs exceed 30,000 characters, save to disk instead of passing
to model context. This prevents 'prompt is too long' errors caused by
large WebFetch, Bash, or MCP tool outputs.

Changes:
- Add tool-results.ts: Storage helper for large outputs
- Update bash.ts, webfetch.ts, websearch.ts, codesearch.ts: File persistence
- Update prompt.ts: MCP tools save large outputs to file
- Update session/index.ts: Cleanup tool_results on session delete
- Add OPENCODE_EXPERIMENTAL_MCP_MAX_OUTPUT_LENGTH flag for configurability
@codyw912

codyw912 commented Dec 31, 2025

Copy link
Copy Markdown

This seems likely to fix the single biggest usability issue with opencode that I keep hitting over and over and over... literally daily, multiple times per day with normal use of opencode.

It's worked for me over the past few days. Haven't hit the "prompt too long" issue once.

Just wanted to note, this actually hasn't completely fixed the bug for me. It has significantly lowered the frequency that I run into it, but it still happens a few times a day now. Seems like the team is working on a fix though.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ben-vargas

ben-vargas commented Dec 31, 2025

Copy link
Copy Markdown
Contributor Author

@codyw912 - yeah, this is just addressing one possible cause (tool use and specifically mcp tools) which often caused it for me. But i'm certain there are other edge cases to be addressed, hopefully their team gets all of them if not wanting to merge this PR.

I do hope they retain or implement a feature similar to writing to a file rather than just returning truncated data, the models are much more effective when they can be warned of the truncation and take a couple attempts at reading/exploring the large data, especially useful when using tools in enterprise (think reading data from salesforce or servicenow APIs). Flat out truncation of what the model gets back from tools alone isn't great.

@thdxr - Any thoughts on having this kind of file write rather than pure truncation of tools?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ben-vargas
ben-vargas force-pushed the mcp-output-file-persistence branch from 496ae0e to 0339c06 Compare January 5, 2026 03:04
@anntnzrb

anntnzrb commented Jan 7, 2026

Copy link
Copy Markdown
Contributor

+1

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ben-vargas
feat: integrate file persistence into centralized truncation system
ce28fd3
Refactors the file persistence feature to work with the new centralized
truncation architecture in upstream. Instead of modifying each tool
individually, this integrates file persistence directly into the
Truncate namespace.

When tool output exceeds 50KB/2000 lines, the full output is saved to
disk and the model receives a head+tail preview with instructions for
exploring the data using Read/Grep/jq tools.

Changes:
- truncation.ts: Add outputWithPersistence() function with:
  - Head (67%) + tail (33%) preview respecting caller's maxLines
  - Preview byte cap respecting min(caller's maxBytes, 20KB)
  - JSON detection (.json extension + jq hint)
  - 10MB hard cap on persisted files (using Buffer for both detection
    AND truncation to handle multibyte chars correctly)
  - Tool name sanitization (64 char limit, safe chars only)
  - Path traversal protection (sessionID validation + resolve check)
  - cleanupSessionFiles() for session deletion
- tool.ts: Update Tool.define() wrapper to use outputWithPersistence
- registry.ts: Update fromPlugin() to use outputWithPersistence
- session/index.ts: Call Truncate.cleanupSessionFiles on session delete

Benefits over previous per-tool approach:
- Single point of change - all tools automatically get file persistence
- Aligns with upstream's centralized truncation architecture
- No duplicate truncation logic across multiple files
- Cleaner integration with existing session lifecycle
@ben-vargas
ben-vargas force-pushed the mcp-output-file-persistence branch from 2e5a12e to ce28fd3 Compare January 7, 2026 21:20
ben-vargas added 2 commits January 7, 2026 14:55
@ben-vargas
fix: add file persistence for MCP tools
95d5a56
MCP tools have a separate execution path in prompt.ts that wasn't
covered by the centralized truncation changes. This adds
Truncate.outputWithPersistence() to the MCP tool wrapper so that
large outputs from MCP tools (like firecrawl) are also saved to disk
instead of overflowing the context window.

Also improves the truncation notice to be more explicit that the
preview is INCOMPLETE and the model MUST read the file for accurate
results - prevents models from answering based on partial data.
@ben-vargas
feat(tui): show [truncated] indicator on tool calls
5e0aca1
When a tool output is truncated and saved to file, the TUI now shows
a [truncated] indicator in the tool call line so users can see at a
glance that the output was truncated. Shows in warning color on both
InlineTool and BlockTool displays.
@rekram1-node rekram1-node mentioned this pull request Jan 7, 2026
feat: write truncated tool outputs to files #7239
Merged
@ben-vargas

ben-vargas commented Jan 8, 2026

Copy link
Copy Markdown
Contributor Author

@rekram1-node one thing I found really helpful on the tool truncation persistence was the small little indicator to the user I added in the refactor which was done after your PR merging concepts.

You might consider adding something similar to 5e0aca1 in this branch so the user knows when a tool was truncated... helpful because sometimes I have to know truncation happened and prompt it to look a little deeper at the persisted tool output, and user doesn't know unless there is an indicator of some kind.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ben-vargas

ben-vargas commented Jan 8, 2026

Copy link
Copy Markdown
Contributor Author

Also, thanks for considering and landing the persistence!!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@davidbernat davidbernat mentioned this pull request Jan 8, 2026
[Feature] Add Fetch tool that saves to file (like Claude Code) #7168
Closed
@github-actions github-actions Bot mentioned this pull request Jan 20, 2026
feat(opencode): switch tool truncation to token-based limits #9648
Closed
@thdxr
thdxr force-pushed the dev branch from cbab81f to 2d3c7a0 Compare January 30, 2026 04:50
@opencode-agent
opencode-agent Bot force-pushed the dev branch from 00637c0 to 71e0ba2 Compare January 30, 2026 14:32
@thdxr
thdxr force-pushed the dev branch 3 times, most recently from f1ae801 to 08fa7f7 Compare January 30, 2026 14:37
@ben-vargas

ben-vargas commented Feb 4, 2026

Copy link
Copy Markdown
Contributor Author

mostly addressed by other commits referenced above

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ben-vargas ben-vargas closed this Feb 4, 2026
@ben-vargas
ben-vargas deleted the mcp-output-file-persistence branch February 4, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Context protection for tool calls when response is large

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@ben-vargas @mgunnin @taxilian @codyw912 @anntnzrb
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.