Skip to content

feat: thread cwd into observer/reflector agents for project-scope awareness - #50

Open
jingyulong wants to merge 1 commit into
elpapi42:masterfrom
jingyulong:feat/cwd-scope-awareness
Open

feat: thread cwd into observer/reflector agents for project-scope awareness#50
jingyulong wants to merge 1 commit into
elpapi42:masterfrom
jingyulong:feat/cwd-scope-awareness

Conversation

@jingyulong

Copy link
Copy Markdown

PR: Thread cwd into observer/reflector agents for project-scope awareness

Problem

In a coding session for project A, the agent sometimes reads documentation, notes, or files that belong to a different project B (for example, referencing another codebase's debugging notes or a library writeup stored in a separate directory). Because the observer and reflector agents have no notion of the current working directory, they record these cross-project details as observations and crystallize them into reflections.

These reflections then propagate into compaction summaries and subsequent system prompts, so later turns in project A are oriented by durable "facts" about project B — facts the user never asked to remember for project A. Over multiple sessions this pollutes the reflection store with cross-project trivia that does not help (and sometimes misleads) future work in the current project.

Root cause

The consolidation pipeline already has the session cwd available in ConsolidationCtx, but:

  1. runObserver and runReflector do not receive cwd — neither RunObserverArgs nor RunReflectorArgs has a cwd field.
  2. Neither agent's system prompt (OBSERVER_SYSTEM, REFLECTOR_SYSTEM) mentions the working directory or asks the agent to weigh project relevance before recording/crystallizing.
  3. consolidation-trigger.ts calls runObserver and runReflector without forwarding ctx.cwd.

So the LLM has no way to know which project a piece of content belongs to, and no instruction to filter cross-project material.

Changes

  • RunObserverArgs / RunReflectorArgs: add optional cwd?: string.
  • runObserver / runReflector: when cwd is provided, inject a Working directory: <cwd> line into the agent user text (observer: after the timestamp; reflector: before CURRENT REFLECTIONS). Omitted entirely when cwd is absent — fully backward compatible.
  • Observer & reflector system prompts: add a Project scope section guiding the agents to:
    • treat content clearly about a different project as low relevance or skip it (observer) / not crystallize it (reflector),
    • only keep cross-project content when the user explicitly states it applies to the current project, or it is genuinely project-agnostic (e.g. a global CLI flag),
    • prefer lower relevance / leave-as-observation when project membership is unclear.
  • consolidation-trigger.ts: pass cwd: ctx.cwd to both runObserver and runReflector.
  • Tests:
    • observer.test.ts: cwd injected into user text when provided; omitted when absent.
    • reflector.test.ts: same coverage.
    • consolidation-trigger.test.ts: end-to-end — ctx.cwd is forwarded to both mocked agents.

Design notes

  • The cwd field is optional on both arg types, so existing callers (tests, any other callers) keep working without changes.
  • The prompt guidance is additive — it does not change the existing durability/relevance criteria, only adds a project-scope lens on top.
  • This is intentionally a light-touch fix: it gives the agents the information and the guidance, without hard-blocking cross-project content (the user may legitimately want a cross-project fact recorded if they say so).

Verification

  • npm run typecheck — passes.
  • npm test — 245 tests pass (25 files), including 5 new tests.

Related

…reness

The consolidation pipeline already has the session cwd available in
ConsolidationCtx but never passes it to runObserver or runReflector.
Neither agent's system prompt mentions the working directory or project
relevance, so the LLM has no way to distinguish content that belongs to
the current project from cross-project material that was read into the
session (e.g. notes or files from a different project directory).

This leads to off-topic observations being recorded and then crystallized
into reflections that do not belong to the current project, polluting
subsequent compaction summaries and system prompts with durable 'facts'
about other codebases.

Changes:
- RunObserverArgs / RunReflectorArgs: add optional cwd field
- runObserver / runReflector: inject a 'Working directory' line into the
  agent user text when cwd is provided
- observer/reflector system prompts: add a 'Project scope' section
  guiding the agents to deprioritize or skip cross-project content and
  to prefer lower relevance when project membership is unclear
- consolidation-trigger: pass cwd:ctx.cwd to both agent calls
- tests: cover cwd injection, omission when absent, and end-to-end
  forwarding from the consolidation trigger
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