Skip to content

fix(memory): enforce auto-inject budget and correct search schema - #1487

Draft
longlonggo wants to merge 1 commit into
nextlevelbuilder:devfrom
longlonggo:agent/fix-memory-tool-contracts
Draft

fix(memory): enforce auto-inject budget and correct search schema#1487
longlonggo wants to merge 1 commit into
nextlevelbuilder:devfrom
longlonggo:agent/fix-memory-tool-contracts

Conversation

@longlonggo

Copy link
Copy Markdown

Summary

  • enforce InjectParams.MaxTokens across the complete auto-injected memory section
  • apply the documented 200-token default when callers leave MaxTokens unset
  • remove the unsupported memory_search.depth schema parameter
  • direct callers to memory_expand when they need full episodic content

Root cause

pgAutoInjector.Inject limited only the number of L0 entries. It never read
MaxTokens, so long abstracts could exceed the documented prompt budget.

memory_search advertised depth values (l0, l1, and l2), but its
execution path never consumed that argument. Episodic search already returns L0
previews and exposes memory_expand as the explicit full-content operation.

Behavior after this change

  • auto-injection counts the header and every selected L0 entry with the existing
    conservative fallback token counter
  • entries stop being added before the configured/default budget would be exceeded
  • older callers that still send depth remain tolerated because tool execution
    ignores unknown arguments; the unsupported option is simply no longer advertised

Validation

  • go test ./internal/memory ./internal/tools ./internal/agent
  • go build ./...
  • go build -tags sqliteonly ./...
  • go vet ./...
  • go test -race -tags integration ./tests/integration/

The full go test ./... run reached one unrelated environment-specific failure:
TestProvidersHandlerCreateAllows1536EmbeddingDimensions resolved
generativelanguage.googleapis.com to reserved address 198.18.28.90, so the
provider URL guard returned HTTP 400 instead of the test's expected 201. The
memory, tools, and agent packages passed.

Surface parity

  • Gateway/runtime: auto-inject budget is now enforced.
  • Tool contract: unsupported depth schema removed; memory_expand guidance added.
  • PostgreSQL/SQLite: N/A because no store behavior or schema changes.
  • Web UI: N/A because the tool schema is consumed by the agent runtime, not a UI form.
  • CLI/runtime package: N/A because no CLI command or response shape changes.

@clark-cant clark-cant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — clean, focused fix with proper test coverage. The budget enforcement logic is sound, and removing the unsupported depth parameter aligns the schema with actual behavior. Safe to merge.

@clark-cant clark-cant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maintainer review — github-maintain

Summary: Well-scoped fix enforcing the auto-inject memory budget (MaxTokens) and removing the unsupported depth parameter from memory_search schema. Two concrete bugs addressed: (1) auto-injector ignored MaxTokens allowing unbounded prompt growth, (2) schema advertised a depth param that was never consumed.

Risk level: Low — changes are isolated to memory auto-injection and tool schema, no store/schema migrations.

Mandatory gates:

  • Duplicate/prior implementation: clear — no overlapping PRs found
  • Project standards: consistent with existing patterns (tokencount usage, table-driven tests)
  • Strategic necessity: clear value — prevents prompt budget blowout and removes misleading tool schema

Findings:

  • Suggestion: auto_injector_impl.go calls counter.Count("", sb.String()+line) on each loop iteration, rebuilding the full string each time. With small budgets (200 tokens default) this is fine, but a running total (currentTokens += counter.Count("", line)) would be O(n) instead of O(n²). Minor — not blocking.

Verdict: Comment — clean fix, safe to merge once CI checks run and pass. Awaiting CI on the dev base branch.

Posted by github-maintain at 2026-07-31T18:28:30Z

@clark-cant clark-cant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review — github-maintain

Summary: Enforces auto-inject MaxTokens budget (previously only entry-count limited) and removes the unsupported depth schema parameter from memory_search.

Risk level: Low — targeted, well-scoped change (+103/-10, 4 files) with regression tests.

Mandatory gates:

  • Duplicate / prior implementation: clear — no overlapping PRs or issues found.
  • Project standards: aligned with existing patterns (uses tokencount.NewFallbackCounter(), follows existing auto-injector structure).
  • Strategic necessity: clear value — prevents unbounded prompt budget consumption and removes misleading tool schema.

Findings: None at Critical or Important level.

Verdict: Approve — clean implementation, good test coverage, no security or breaking-change concerns.

Posted by /ck:review-pr at 2026-08-01T00:00:00Z

@clark-cant clark-cant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review: fix(memory): enforce auto-inject budget and correct search schema

Summary: Fixes two memory tool contract issues: (1) auto-injector now respects MaxTokens budget across the full injected section (was only limiting entry count), applying the documented 200-token default; (2) removes the unsupported depth parameter from memory_search schema and directs callers to memory_expand for full episodic content.

Risk level: Low — focused bugfix in two packages with clean test coverage.

Mandatory gates:

  • Duplicate / prior implementation: clear — no overlapping PR or prior fix found
  • Project standards: docs found — aligns with existing memory tool contract
  • Strategic necessity: clear value — prevents unbounded prompt token growth from memory injection and removes misleading schema

Findings:

  • No Critical or Important issues
  • Suggestion: counter.Count("", sb.String()+line) re-counts the entire accumulated section per entry; for large budgets this is O(n²) in token counting calls. Acceptable for the 200-token default ceiling, but worth noting if the budget is ever raised significantly.

Verdict: Approve

Local tests noted in PR body (go test ./internal/memory ./internal/tools ./internal/agent) cover both changes. Merge deferred: no GitHub checks reported and mergeStateStatus is UNSTABLE.

Posted by github-maintain at 2026-08-08T18:00Z

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.

2 participants