gitagent icon indicating copy to clipboard operation
gitagent copied to clipboard

feat: token optimization layer — doc-converter, CCR, and compression pipeline

Open stealthwhizz opened this issue 5 months ago • 1 comments

Problem

gitagent currently cannot read PDF, DOCX, XLSX, or PPTX files. Clients whose users upload documents get a [Binary file] response — the agent is blind to those files entirely.

Additionally, when agents work with large documents across multiple turns, the full document content accumulates in context on every turn, compounding API costs with each message.

Proposed Solution

A multi-layer token optimization pipeline built into the read tool:

1. Doc-converter — binary → readable

Convert PDF, DOCX, XLSX, PPTX to clean markdown automatically when read() is called. No manual pre-processing needed by the client.

2. CCR (Chunked Reversible Retrieval) — large doc efficiency

For documents exceeding a token threshold, return a lightweight outline on first read. The agent fetches only the sections it needs via read_doc_section. Prevents large documents from flooding context on every turn in multi-turn sessions.

3. SmartCrusher — JSON compression

Strip nulls, deduplicate arrays, truncate padding from JSON tool outputs.

4. CodeCompressor — source file compression

Remove comments and blank lines from source files before returning to agent.

5. CacheAligner — system prompt KV cache

Reorder system prompt so static content comes first, maximising provider-side KV cache hits across sessions.

Scope

  • src/tools/read.ts — pipeline wired in
  • src/tools/doc-converter.ts — new
  • src/tools/doc-store.ts — new (CCR + read_doc_section tool)
  • src/compression/ — new (SmartCrusher, CodeCompressor, CacheAligner)

Benchmark methodology (proposed)

Compare two successful agent runs on the same document:

  • Baseline — doc-converter converts PDF, full markdown injected into prompt
  • With CCR — agent receives outline, fetches only relevant sections on demand

This gives a fair apples-to-apples token comparison where both runs complete the task.

stealthwhizz avatar Jul 01 '26 10:07 stealthwhizz

Closed as New Issue is issued

stealthwhizz avatar Jul 01 '26 10:07 stealthwhizz