Skip to content

fix(user-profile-analysis): cap context sent to profile LLM - #186

Merged
tickernelz merged 1 commit into
tickernelz:mainfrom
4nibhal:fix/profile-context-cap
Jul 23, 2026
Merged

tickernelz merged 1 commit into
tickernelz:mainfrom
4nibhal:fix/profile-context-cap

Conversation

@4nibhal

@4nibhal 4nibhal commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #176 (partial)

What this addresses

Item #3 of the suggested fixes in #176: cap the total assembled prompt size that buildUserAnalysisContext ships to the profile-analysis LLM. Today the function concatenates the template + all N user prompts with no size guard, so a single oversized prompt or a large backlog can push the payload past the model window.

This PR adds the total-size cap (default 32 KB). It does not add the per-item cap or skip-analysis-prompt logic from items #1 and #2 — those are a separate, deeper surgery on the prompts.map((p, i) => \${i + 1}. ${p.content}`).join("\n\n")` line and will come as a follow-up if there is interest.

Changes

  • src/services/user-memory-learning.ts (+9/-2): introduce a local truncate(s) helper and apply it to both branches (base alone, and base + validationPrompt).
  • src/config.ts (+4): new optional knob userProfileMaxContextBytes wired through OpenCodeMemConfig, DEFAULTS, and buildConfig in the same shape as userProfileAnalysisInterval.
  • README.md (+1): added the new field to the Configuration Essentials example block.

Verification

  • bun run typecheck — clean
  • bun run build — clean
  • bunx prettier --check — clean

Manual check

Set userProfileMaxContextBytes: 1024 in ~/.config/opencode/opencode-mem.jsonc and trigger a profile analysis with enough prompts to exceed 1 KB. The LLM payload should be truncated and include the marker [... context truncated to userProfileMaxContextBytes ...].

Background

See #issuecomment-5033470016 for the full analysis this is based on.

buildUserAnalysisContext now truncates the prompt payload
at CONFIG.userProfileMaxContextBytes (default 32768). This
keeps retry loops affordable when the profile LLM call
fails repeatedly with oversized context.

Adds a new config knob userProfileMaxContextBytes, wired
through OpenCodeMemConfig, DEFAULTS, and the buildConfig
resolver following the same pattern as userProfileAnalysisInterval.

README updated with the new field in Configuration Essentials.
@tickernelz
tickernelz merged commit d59e08b into tickernelz:main Jul 23, 2026
1 check passed
@tickernelz

Copy link
Copy Markdown
Owner

Merged into main and included in the v2.20.1 release tag. Verified on the fully integrated batch tree with bun install --frozen-lockfile, bun run typecheck, bun run build, and bun test (230 pass / 0 fail). Thanks for the contribution!

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.

Profile learning recursively re-ingests its own analysis prompt

2 participants