Skip to content

feat(session): recall saved project conversations across sessions - #1045

Open
俞小姐与茉莉清茶 (Sanikki) wants to merge 6 commits into
synthetic-sciences:mainfrom
Sanikki:feat/project-memory
Open

俞小姐与茉莉清茶 (Sanikki) wants to merge 6 commits into
synthetic-sciences:mainfrom
Sanikki:feat/project-memory

Conversation

@Sanikki

@Sanikki 俞小姐与茉莉清茶 (Sanikki) commented Nov 4, 2026 •

Copy link
Copy Markdown

What does this PR do, and why?

This PR extends the existing recall tool with scope: "project" so the model can look up earlier work when it’s relevant. Calls without a scope continue to search only the current session. When it needs context from an earlier conversation, the model can request the project scope itself; users don’t need to enable a setting.

Project recall searches saved conversations in the same project and directory. It returns matching excerpts as tool results, with their source session and message IDs, rather than adding conversation history to the system prompt. It reuses the existing session store and does not search automatically on every turn.

The search checks the latest 32 messages in up to 8 recent eligible sessions and returns matches from at most 4 sessions within a 16 KiB output budget. Only completed user–assistant exchanges are eligible. Child, archived, and reverted sessions are excluded, as are tool outputs, reasoning, and synthetic text. If matches are omitted because of a result limit, the tool result says so.

Setting OPENSCIENCE_DISABLE_PROJECT_MEMORY=1 disables cross-session search without deleting saved conversations or affecting current-session recall. Markdown note loading is outside the scope of this PR.

Linked issue

Related to #1043.

How did you verify it?

  • From backend/cli: bun test --timeout 200000 ./test/tool/recall.test.ts ./test/tool/recall-project.test.ts ./test/tool/recall-project-prompt.test.ts — 13 passed, 0 failed (74 assertions). Covers current-session compatibility, cross-session persistence, project and directory isolation, exclusions, output limits, and disable switches.
  • A separate-process test verifies that a fact saved in one session is absent from a later session’s first request and reaches the model through the recall tool result. It also checks that the serialized system messages and tool definitions stay identical across sessions and before and after the recall call.
  • From backend/cli: bun run typecheck passed. Changed-file Prettier checks and the staged gitleaks scan also passed.
  • From frontend/docs: bun run check passed in a temporary LF-normalized copy on Windows. It validated 70 pages, 982 links, 16 JSON examples, and 9 TypeScript examples.
  • Manually tested in the local workspace: project recall in a new session returned the original excerpt from an earlier session, with the expected source session and message IDs.

Checklist

  • bun run check is green (format, typecheck, backend + frontend/ui + SDK tests) — not rerun for this revision; see the focused checks above.
  • CHANGELOG.md has an Unreleased entry
  • Matching user documentation updated
  • No version bumps

No server API/SDK, installer or UI source changes.

@vercel

vercel Bot commented Nov 4, 2026

Copy link
Copy Markdown

俞小姐与茉莉清茶 (@Sanikki) is attempting to deploy a commit to the InkVell Team on Vercel.

A member of the Team first needs to authorize it.

@KB-syntheticsciences

Copy link
Copy Markdown
Collaborator

Hi 俞小姐与茉莉清茶 (@Sanikki), thank you for this, and sorry it took us so long to reply. You asked for design agreement on #1043 and we should have answered sooner.

The work is careful: reusing the saved conversations instead of a second store, the size limits, the exclusions (subagent, archived and reverted sessions, unfinished replies), the off switch and the tests are all things we'd want to keep. We'd like to take the feature, but in a different shape. The issue is where the recalled text goes, not how it's selected.

Why not the system prompt

  • Prompt caching. Since perf(session): reuse the provider's prompt cache across a project's sessions #1066 (merged on 15 Nov), the system prompt and tools are byte-identical across a project's sessions, so providers can reuse a cached prompt. A new Claude session now reads about 14,600 cached tokens on its first call instead of none. Recalled exchanges in InstructionPrompt.system(), refreshed every turn, would make each session's system prompt different. They would also change it mid-session whenever another session in the project finishes a reply, and each change makes the provider re-read the whole prompt at full price.
  • Trust level. The system prompt holds rules. Text from earlier sessions, including anything quoted or pasted there, would sit at the highest-trust position in every later session of the project, even with the "not instructions" label. CLAUDE.md describes the split we follow: rules in the system prompt, facts in messages.
  • Relevance. The most recent exchanges are carried on every turn whether or not they relate to the task, at up to about 4K tokens per turn by default.

What we'd suggest instead

  1. Recall on demand. recall (backend/cli/src/tool/recall.ts) currently searches only the current session. Extending it to search the project's other saved sessions would let the model pull in earlier work when it's relevant, with no cost when it isn't and no effect on caching. Your selection rules (same project and directory, no subagent, archived or reverted sessions, completed replies only, size limits) carry over directly.
  2. Keep .openscience/memory/**/*.md notes alongside the other instruction sources. They're stable, user-written files, so they're cache-safe as they are.
  3. Make any automatic recall opt-in through a setting rather than only an environment variable. With on-demand recall it may not be needed at all.

If it helps, the notes part could go in first as its own small PR while the recall part is reworked. Please also rebase on main: #1066 changed SystemPrompt.environment, and SystemPrompt.withSessionContext shows where per-session facts go now.

Let us know if you'd like to take this on. If you'd rather not, or we haven't heard back in about two weeks, we'll close this PR and implement the design ourselves, crediting your work.

@KB-syntheticsciences KB (KB-syntheticsciences) mentioned this pull request Nov 21, 2026
1 task done
@Sanikki

Copy link
Copy Markdown
Author

Thanks for the feedback, and no worries about the wait! I’m happy to keep working on this. Your points about caching and keeping conversation history out of the system prompt make sense.

I’ll rebase on main and extend the existing recall tool to search across project sessions, keeping the filtering rules, size limits, and tests. I’ll drop automatic recall for now and put the Markdown notes in a separate PR.

I’ll update this PR when the changes are ready.

Sessions start empty, so decisions reached in an earlier session in the same
project get restated every time. Read every markdown file under
.openscience/memory/ into the instruction sources, appended last so the
existing byte budget drops memory before AGENTS.md or configured
instructions.

Reading alone leaves the directory empty, so the prompt also names the file a
new note goes in: when the user asks the agent to remember something it
appends an entry there with its file tools. Nothing is captured, summarized
or written in the background.
@Sanikki

Copy link
Copy Markdown
Author

I’ve rebased the branch on main and pushed the changes in 403d775. I’ve also updated the PR description to reflect the switch from automatic history injection to on-demand recall. Following up on my last comment:

  1. On-demand recall: The existing recall tool now accepts scope: "project". Calls without a scope still search only the current session. When the model needs context from an earlier conversation, it can choose the project scope itself; users don’t need to enable a setting. The search stays within the same project and directory, preserves the eligibility rules and size limits, and returns each excerpt with its source session and message IDs.
  2. Markdown notes: I’ve left .openscience/memory/**/*.md loading out of this revision so it can be reviewed in a separate PR.
  3. Automatic recall: I removed the per-turn history lookup rather than adding a setting for it. Earlier conversations are searched only when the model calls recall with the project scope, and the excerpts come back as tool results rather than going into the system prompt. Setting OPENSCIENCE_DISABLE_PROJECT_MEMORY=1 disables cross-session search without deleting saved conversations or affecting current-session recall.

All 13 focused tests and the CLI typecheck pass. One test specifically checks the caching concern you raised: it records a fact in one session and verifies that, in a later session, the fact reaches the model only through the tool result. The serialized system messages and tool definitions stay identical across sessions and before and after the recall call.

Thanks again for the feedback. This is ready for another look whenever you have a chance.

@KB-syntheticsciences

Copy link
Copy Markdown
Collaborator

Thanks 俞小姐与茉莉清茶 (@Sanikki), the redesign is right: on-demand recall with scope: "project", results as tool output, nothing per-session in the system prompt or tools. We built 403d7755 locally and tried it in the app. The simple case works: a new session recalls a fact from an earlier one. We found two bugs that make it miss facts in normal use. Both come from the search window, so one fix covers them.

Bug 1: a long turn hides the whole session

Each agent step is stored as its own assistant message, and harness continuations add user messages. Session.messages({ limit: 32 }) takes the newest 32 raw messages. So one turn with 32+ tool calls fills the window, the prompt that started that turn falls outside it, and the session contributes nothing.

To reproduce:

  1. In session A, say "Note for later: the codename for run R8 is GOLDFINCH-17."
  2. In the same session, ask for a task that makes about 34 tool calls in one turn. (We asked for 34 sequential glob calls.)
  3. In a new session, ask for R8's codename using recall with scope: "project".

Result: "No matches in the searched project history window". The model then told the user the fact was never mentioned.

Bug 2: empty sessions use up the 8 slots

Candidates are the 8 most recently updated sessions, chosen before checking whether they contain any finished exchange. Empty sessions, aborted sessions and forks take those slots, so older sessions with real content are never searched.

To reproduce:

  1. In session A, say "Note for later: the codename for run R7 is BLUEHERON-42." Then confirm a new session finds it with recall (scope: "project", pattern BLUEHERON).
  2. Create 9 empty sessions (for example with POST /session).
  3. Repeat the same recall in a new session.

Result: "No matches", for the search that worked a minute earlier.

Suggested fix

Drop the fixed session and message windows. Search all eligible sessions in the project, newest first, and stop when the result budget (16 KiB / limit) is full or after a short time budget (about 2 s), checking abort between sessions. Keep your existing eligibility rules. Skip sessions with no finished exchange without counting them. When a matching message's prompt isn't loaded yet, fetch it directly with MessageV2.get.

Since the search may stop early, please end every result with what was covered, e.g. "Searched 143 of 143 sessions" or "Stopped after 2 s; searched sessions back to 12 Nov". Then "no match" isn't read as "never happened".

Tests

Please add tests that check behaviour, without asserting the internal limits (8 sessions, 32 messages):

  • a fact stated before a turn of 34+ assistant steps (including a continuation) is found from a new session;
  • a fact in an older session is still found after 9 empty sessions, aborted sessions and a fork are created after it, and the fork doesn't produce duplicate excerpts;
  • subagent, archived and undone sessions are still excluded;
  • when the time budget stops the search, the result reports how far it got;
  • aborting during a search stops it promptly.

A heads-up for later

Users' sessions already live in SQLite, so we may later move the search internals onto a database query, and possibly a full-text index, for very large projects. If we do, the parts you've built would stay: the scope: "project" interface, the eligibility rules, the result format and these tests. Only the scanning underneath would change.

Once the fixes are in, we'll run CI and try it in the app again. Thanks for sticking with this!

@Sanikki

Copy link
Copy Markdown
Author

Thanks for building the previous revision and trying it in the app. I reproduced both issues on 403d775: after a long turn with many tool calls, recall could no longer find a fact it had found before, and creating empty sessions made a previously successful lookup return no matches.

I’ve pushed the fix in bdaa4d8.

Changes and tests based on your feedback

I removed the fixed session and message windows. recall now searches eligible sessions in the same project and directory, newest first, until it reaches the requested match limit, the 16 KiB output budget, or the roughly two-second time budget. For a completed assistant reply, it fetches the parent user message by ID with MessageV2.get. If the parent is an internal continuation, it follows the epoch reference back to the original request. The last line of each result reports what was actually searched, even when nothing matched.

Here’s how I verified the five cases you raised:

  1. Long turns: I recorded GOLDFINCH-17 in a completed exchange and confirmed that a new session could find it by searching for GOLDFINCH. I then added a turn with 34 recorded glob steps and a continuation after step 17, without mentioning the codename again. Repeating the search from another new session returned GOLDFINCH-17 with the original user message ID. A separate test confirmed that the continued reply was paired with the original request, not the internal continuation text.
  2. Empty sessions, aborted sessions, and forks: After recording BLUEHERON-42, I created nine empty sessions and an aborted exchange containing BLUEHERON discarded. Searching for BLUEHERON returned the original fact but not the aborted text. I then forked the source session and repeated the query: it returned exactly one excerpt. I also added a fact only to the fork and verified that it was searchable and cited the fork’s own session and message IDs.
  3. Exclusions and coverage counts: I put distinct content in a normal session, a subagent session, an archived session, and a reverted session. Only the normal session’s exchange was returned. To check the count separately, I searched two sessions with completed exchanges plus one empty session for a term absent from all three. The footer reported Searched 2 of 2 eligible sessions. A fully searched session counts even if nothing matches; an empty session does not.
  4. Timeouts and partial results: I put different matching facts in a newer and an older session, then used a controlled clock to exhaust the budget just as the scan reached the older one. The result kept the newer hit. The footer reported that one session had been fully searched, gave the update date of the older session, and explicitly said that the older session had not been fully scanned.
  5. Cancellation: I held a source read open, waited until the scan reached it, and sent abort without releasing the read. The tool call returned the cancellation error before the read was released. It didn’t wait for the timeout or report cancellation as a normal “no matches” result.

Both of your original repro cases now find the previously missed fact on the first call, without a retry or a cursor.

Two additional changes from follow-up testing

  • Resuming after a timeout: In a project with a longer history, retrying the same query could keep scanning the newest sessions without ever reaching the older fact. A partial result now includes an optional cursor. To test it, I placed a receipt only in the oldest session and searched for its label, not its value. After the first call timed out, I continued with the same regex and the returned cursor. The search found the receipt and cited its original source. For a label that didn’t exist, continuing instead reached the end with zero matches. The tool doesn’t automatically fetch more pages.
  • Matching near the end of a long message: This is separate from the long-turn bug: a search could reach the right message but still miss a code near its end. For a subset of simple regexes with a known maximum match length, recall now scans the full visible text in chunks. I placed three identifiers in one message: one near the start, one straddling the old 65,536 UTF-16 code unit boundary, and one past offset 100,000. Searching for SEAL-\d{2} returned the same three offsets as running the regex over the whole original string, each exactly once. The interface is still regex-only. More complex expressions retain the prefix limit, and the result says when that limit applied.

On a personal note, thank you for taking the time to test this yourselves and point out exactly what needed to change. I’m still learning how to build agent applications, and seeing where my design fell short in real use taught me a lot. Getting hands-on experience with these issues and hearing your feedback has been especially helpful. I really appreciate your suggestions and guidance.

This branch has not been deployed

No deployments
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