Skip to content

feat: context pipeline for structured AI prompt injection#71

Merged
Atharv777 merged 5 commits intokentuckyfriedcode:mainfrom
Krishna41357:issue61
Mar 31, 2026
Merged

feat: context pipeline for structured AI prompt injection#71
Atharv777 merged 5 commits intokentuckyfriedcode:mainfrom
Krishna41357:issue61

Conversation

@Krishna41357
Copy link
Copy Markdown
Contributor

fixes #61

What changed

Added a structured pipeline that selects and injects only relevant
project context into AI prompts instead of sending raw full-project data.

New files

server/utils/context_builder.py
Scores and selects files to include based on import references,
directory proximity, language match, and recency. Enforces hard
size budgets: 8k chars for the active file, 4k per related file,
32k total. Skips heavy dirs (node_modules, .git, target).
TTL-based cache with a 200-entry cap and automatic eviction.

server/utils/prompt_builder.py
Assembles the final prompt string from selected context. Three-tier
fallback: truncate related files, truncate current file to 300 chars,
metadata only. Suppresses the explore nudge when file context exists.

test_context_pipeline.py
74 tests covering context selection, budget enforcement, cache TTL
and size cap, prompt assembly, fallback strategies, route endpoints,
and the frontend constant contract.

Updated files

server/utils/init.py
Registers context_builder and prompt_builder as package exports.

server/agent.py
Imports context_builder and prompt_builder. Adds project_context
field to AgentRequestContext. Adds _parse_context_payload() to read
context from POST body. Route now accepts GET and POST. System prompt
built via build_prompt(), task prompt via build_task_prompt(). Adds
PROJECT CONTEXT section to get_prompt() so the model knows to use
injected context instead of re-exploring.

server/project_routes.py
Two new endpoints:
POST /api/projects//context - pre-fetches and returns context
payload for the frontend to attach to agent requests.
POST /api/projects//context/invalidate - clears the cache on
file save. Path traversal guard added.

pages/app/index.jsx
Tracks active file and recently modified files. Fetches context on
file switch (debounced 800ms). Invalidates cache on save. Sends
context payload in POST body instead of query string. Live context
status indicator in toolbar.

System design

image

Tests

74/74 passing

image

What this enables

  • Agent skips file exploration when context is already injected
  • Related files selected by relevance score, not random inclusion
  • Large projects handled without timeout or token blowout
  • Context payload kept out of server logs and browser history
  • Single source of truth for recently-modified boost limit across
    Python and JS (RECENTLY_MODIFIED_BOOST_LIMIT = 5)

System choices

  • Character budgets over token counts: simpler, no tokenizer needed,
    conservative enough (~4 chars per token for code)
  • Scoring on 2k preview read only, full read only for selected files
  • Cache keyed by project_path::file_path, evicts oldest half when full
  • POST body for context payload instead of query param (security)
  • Debounce on frontend prevents flooding the context endpoint on rapid
    file switching

@Atharv777
Copy link
Copy Markdown
Collaborator

Great PR @Krishna41357
will have to inspect and review it properly before merging. But great work

@Krishna41357
Copy link
Copy Markdown
Contributor Author

sure! happy to do any changes if required .

@Atharv777
Copy link
Copy Markdown
Collaborator

@Krishna41357 Hey sorry for the trouble, I've merged a few PRs that have changed a few things in the app/index.jsx. Can you please look into it and resolve the merge conflicts here?

@Krishna41357
Copy link
Copy Markdown
Contributor Author

yeah sure !

@Krishna41357
Copy link
Copy Markdown
Contributor Author

@Atharv777 here you go !! all cleanly merged

@Atharv777 Atharv777 merged commit d52ca93 into kentuckyfriedcode:main Mar 31, 2026
4 checks passed
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.

[AI] Implement structured project context pipeline for AI requests

2 participants