.maestro/playbooks/2026-02-25-CM-Issues-PRs/2026-02-25-Branch-Memory/BRANCH-MEMORY-03.md
This phase connects the ancestor resolution utility to the context injection pipeline. When a new session starts, the ContextBuilder will filter observations to only those whose commit SHA is an ancestor of the current HEAD. Observations from merged branches appear naturally, while sibling branch work stays invisible — matching how git history works. Pre-migration observations (with null commit_sha) remain always visible for backward compatibility.
Update ContextBuilder to resolve branch visibility at context generation time:
src/services/context/ContextBuilder.ts to understand the full generateContext() flow — note the ContextInput type (which already has cwd), the queryObservations call, and the queryObservationsMulti call for worktreesgenerateContext(), after the database is initialized and project is resolved, call getUniqueCommitShasForProject(db, project) from src/services/sqlite/observations/get.ts to get all candidate SHAs, then call resolveVisibleCommitShas(candidates, cwd) from src/services/integrations/git-ancestry.tsvisibleCommitShas: string[] | null to the observation query functionsgetUniqueCommitShasForProject and resolveVisibleCommitShas into ContextBuilder. Collects unique SHAs across all projects, resolves visibility once per cwd. Fails open (null = show all) on errors.Update the observation query functions to accept and apply commit SHA filtering:
queryObservations and queryObservationsMulti (likely defined in ContextBuilder.ts or a separate query module) — read the code to understand the current SQLvisibleCommitShas?: string[] | null parameter to these functionsvisibleCommitShas is null → no filtering (not a git repo, backward compatible)visibleCommitShas is an empty array → only show observations where commit_sha IS NULL (pre-migration observations)visibleCommitShas is populated → add SQL clause: AND (commit_sha IS NULL OR commit_sha IN (?, ?, ...)) with parameterized placeholderscommit_sha IS NULL clause is critical — it ensures pre-migration observations (created before branch memory existed) are always visible regardless of branchbuildCommitShaFilter() helper to ObservationCompiler.ts. Both queryObservations and queryObservationsMulti accept optional visibleCommitShas parameter. 7 unit tests pass.Verify session-init flow passes cwd to ContextBuilder:
src/cli/handlers/session-init.ts and trace how the context is generatedContextInput interface already has a cwd?: string field — verify that session-init is passing the cwd from the hook input to the ContextBuilderinput.cwd available from Claude Code's JSON payload/api/context/inject. The hook handler (context.ts) now passes &cwd= query param. The worker endpoint (SearchRoutes.ts) extracts it and passes it to generateContext(). Previously cwd was a synthetic /context/{project} path — now the real cwd is used for git ancestry resolution.Build and manually verify context injection respects branch boundaries:
npm run build-and-syncnpm run build succeeds cleanly. All 7 branch filtering tests pass (20 assertions). All 17 existing related tests (git-ancestry + observation-compiler) continue to pass.