Skip to content

fix(grep): grep context blowup from trajectory files#22

Open
Dong1017 wants to merge 1 commit intovigo999:refactor-arch-4from
Dong1017:refactor-arch-4-fix_grep_issue
Open

fix(grep): grep context blowup from trajectory files#22
Dong1017 wants to merge 1 commit intovigo999:refactor-arch-4from
Dong1017:refactor-arch-4-fix_grep_issue

Conversation

@Dong1017
Copy link
Copy Markdown
Collaborator

Summary

Fix a grep failure mode where broad searches could pull large .cache/*.trajectory.jsonl contents into tool output and blow up the agent context window.

This change keeps the fix minimal and localized to tools/fs/grep.go.

Root Cause

grep recursively searched the workspace without guarding against runtime trajectory files under .cache/.
Those files can contain large conversation traces, and grep previously returned unbounded matching output, which could flood model context.

Changes

  • Skip .cache/*.trajectory.jsonl during directory walk
  • Keep explicit file-path search working for trajectory files
    • if the user directly targets a specific trajectory file, grep still allows it
  • Add head_limit to cap returned match count
    • default: 20
    • max: 100
  • Truncate overly long matched lines before returning them
  • Add focused tests for:
    • skipping trajectory files during recursive search
    • allowing explicit trajectory file search
    • head_limit truncation
    • long-line truncation
    • no-match behavior

Scope

Intentionally not changed in this PR:

  • no global tool-result truncation
  • no context-manager changes
  • no UI truncation changes
  • no trace writer changes
  • no broader .cache exclusion beyond *.trajectory.jsonl

Why this approach

This is the smallest fix that addresses the current incident while preserving existing grep behavior as much as possible.

It blocks the known self-referential trace-file case and adds minimal output guards so grep cannot return effectively unbounded content.

Validation

Ran:

  • gofmt -w tools/fs/grep.go tools/fs/grep_test.go
  • go test ./tools/fs
  • git diff --check

Test result:

  • ok github.com/vigo999/ms-cli/tools/fs

Risk

Low.

The behavior change is limited to:

  • recursive search skipping .cache/*.trajectory.jsonl
  • bounded grep output
  • truncation of unusually long matched lines

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