Skip to content

fix(bots): keep Memory Quick add from silently dropping a fact in an open code fence - #47437

Merged
AmethystLiang merged 19 commits into
mainfrom
AmethystLiang/sta-17039
Nov 29, 2026
Merged

AmethystLiang merged 19 commits into
mainfrom
AmethystLiang/sta-17039

Conversation

@AmethystLiang

@AmethystLiang AmethystLiang commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

If a bot's MEMORY.md ended inside a code block that was never closed, a fact you typed into Memory's Quick add box went into that code block. Everything in a code block reads as code, not as a fact, so the fact quietly disappeared even though Orca said it was saved. Now the fact goes just above that code block, and an add that can't save anything says so.

Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​181 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​180
Prod 36 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​300 $\color{#cf222e}{\Huge{\mathbf{−}}}$​77 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​223

What Changed

  • Quick add above an open fence. When MEMORY.md has no list line and ends inside an unclosed ``` or ~~~ fence, the new fact goes above the fence's opening line instead of being appended inside it. An add that would still save nothing (the fact reads as code wherever it lands) now fails with "Could not add that to MEMORY.md; nothing was saved." instead of reporting success.
  • Put-backs above an open fence too. The other code paths that write a fact back (a turn end putting back a fact a chat lost, and Undo) used to place it after the line it followed, which could also be inside a fence. They now move it above the fence, using one fence map per rewrite (bot-memory-fences.ts).
  • A code block never pulls a fact into it. A line that opens a code block or sits inside one is never the line a fact followed, so Undo and a turn end's put-back no longer land a fact inside a code block after a copy of its line there. Otherwise a fact goes back after the last copy of the line it followed, as on main. Choosing among several copies of a repeated line (such as two ## Deploy sections) is left to Memory History: Undo can't tell a file change from a record-only change #49287.
  • Duplicate and blank adds are explicit. Adding a fact that's already in MEMORY.md shows "That fact is already in memory." instead of a silent success with an empty Undo, and changes nothing, even when a chat wrote that fact earlier in its still-running turn. Settings and the phone offer Save only for text with a fact's words in it: not blank, and not only a bullet or separator such as -, : or a dash, which would leave a fact with no words that Forget can't find. The host refuses such an add from any other caller, and both apps show that as "Add some words first." in the owner's language.
  • No-op changes leave the file byte for byte. A change that changes no line no longer rewrites MEMORY.md, so a file with mixed line endings doesn't get them normalized.
  • History shows every Undo. An Undo that only writes its History row (a chat had already put the fact back) leaves MEMORY.md byte for byte the same, so an open History list, which reloads when the file changes, kept its old rows. Desktop and phone now read History again after every Undo.

Why

Quick add must never drop a fact silently. Putting the fact above the open fence keeps the owner's code block exactly as written, unlike closing the fence for them, and it keeps the fact in the part of the file a session loads. The same fence rule applies to every path that writes a fact back, so a fact can't come back inside a fence by another route. No comparable: mechanical.

Linked Issue

STA-17039. No GitHub issue; found while reviewing #46394. Follow-up: #49287 (History Undo can't tell a file change from a record-only change; on main today).

Visual Proof

N/A: no layout change. The only visible addition is a standard info toast ("That fact is already in memory.") on a duplicate add.

Testing

  • I manually tested these changes locally (verified with scripts against the real functions, not in the app)
  • Automated tests added/updated, or explained why not below
  • Every added test fails without this change and avoids the test anti-patterns

Tests in src/main/bots/bot-memory-history.test.ts run on the real memory record, history and turn end. The data-integrity invariant: an owner's Quick add is never lost to an open code fence. With the fence placement reverted, the add throws "nothing was saved"; on main it dropped the fact silently. Each of the other added tests fails with its own fix reverted: Undo goes after the last copy of a line outside a code block (main's rule counts a copy inside one), a Quick add that is only a bullet, separator or dash is refused while ones with words (non-Latin, emoji, punctuation) are kept, and an open History list shows an Undo's row when the file reads back the same. Also covered: a Quick add of a fact the file already states leaving the file byte for byte, with no row and the chat's source on its turn end; and a blank add. bot-presentation.test.ts checks the desktop wording of the no-words refusal, and mobile/src/bots/soul-memory/bot-memory-operations.test.ts checks the phone's, through the real reply interpreter.

Local: the src/main/bots/bot-memory* and bot-turn-context* tests (55), the History and bot-presentation renderer tests (12), the phone's memory reply tests (3), oxlint and oxfmt on the changed files. Typecheck runs in CI.

Performance

No measurable change, judged by reading the diff and its callers. No benchmark was run.

  • Where it runs: main-process bots.memory* RPCs in src/main/runtime/rpc/methods/bots-memory.ts, plus the turn-end put-back through bot-memory-lines.ts. They run only when an owner edits, Quick-adds or undoes a fact, or when a turn ends with memory changes. Nothing new runs at startup, on a timer or on a hot render path.
  • Cost per call: string passes over one MEMORY.md, which is usually a few hundred lines. quickAddBotMemoryChange does one add, one compare and one line diff, all O(n). A put-back builds the fence map once (botMemoryFenceOpeners, O(n)) and skips it when the file has no code block. Each restored fact does one scan from the end, one splice and one fence-map shift, so k facts cost O(k·n), which is the same order as main's scan from the end.
  • Disk: a Quick add of a fact the file already has now makes an empty change, so it skips the MEMORY.md rewrite and adds no History row. That saves one disk write. Every other path writes the same number of times as before.
  • Renderer and phone: botMemoryFactHasWords runs on each render of the Quick add field. It parses a one-line string (O(length of the input)), replacing a trim(). The History lists add one state counter, so an Undo refetches once, the same as a version bump.
  • Merging main: this only moved the new error code into bots-lifecycle-params.ts, a constant array with no Zod schema, and adapted to the mobile page's hook-based theme and locale. Neither adds work.
  • Limits: this is reasoning, not measurement. A very large hand-written MEMORY.md (thousands of lines) with many facts restored in one turn is the worst case; it grows linearly per restored fact.

Risk

Level: Low

The riskiest part is where a fact lands. In a file whose end is inside an unclosed fence, new and put-back facts now appear above that fence instead of inside it, where they were invisible. History rows are unchanged. Back out with a plain revert.

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The change adds shared helpers for memory anchors and code fences, and updates fact insertion and change application. The memory edit RPC validates blank additions and uses a quick-add helper that reports whether a fact was already present. Desktop and mobile clients display translated feedback for that result. Tests cover quick add around code fences, history changes during a chat turn, and undo placement for repeated lines.

Merge Risk: Minimal · previous review 71213

Memory quick add and undo now place facts outside unclosed code fences, report duplicates, and reject blank input. No concrete merge-blocking risk was found in the supplied changes.

Pre-merge checks: Passed 3 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Description check Warning The description follows the template's section order (ELI5, What Changed, Why, Linked Issue, Visual Proof, Testing, Performance, Risk, Review, Notes, Checklist) and most sections are substantive. It f… 1. In Linked Issue, either link a real GitHub issue with "Fixes #" or confirm the author is a StablyAI team member and the issue requirement does not apply. Do not rely on STA-17039 alone. 2. Add the AI Disclosure section and state …
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title names the main change: a Memory Quick add fix that stops silently dropping a fact placed inside an open code fence. The changeset adds fence-aware placement (fact goes above an unclosed fenc…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: a Memory Quick add fix that stops silently dropping a fact placed inside an open code fence. The changeset adds fence-aware placement (fact goes above an unclosed fence), a failure path for adds that cannot be saved, and related put-back and Undo placement changes. The title is specific and scoped to the primary fix, though it omits the secondary changes such as duplicate-add toasts and anchor-occurrence tracking, which is acceptable.

Full details: Description check

Explanation

The description follows the template's section order (ELI5, What Changed, Why, Linked Issue, Visual Proof, Testing, Performance, Risk, Review, Notes, Checklist) and most sections are substantive. It fails because two required items are incomplete. First, the Linked Issue section names STA-17039 and says there is no GitHub issue. The template requires an actual linked issue for outside contributors, written as "Fixes #<number>", and the template's Fixes line is left blank. Second, the AI Disclosure section is missing entirely. The template says to fill it in unless the author is a StablyAI team member, and the description does not state whether AI was used. Minor gaps: the Testing checkbox for manual testing is unchecked while the text says the checks were run with scripts, and the pnpm lint/typecheck/test/build checklist item is unchecked. The mobile typecheck was deferred to CI. The 'Every added test fails without this change' box is checked, which the description supports with specific revert claims.

Resolution

  1. In Linked Issue, either link a real GitHub issue with "Fixes #<number>" or confirm the author is a StablyAI team member and the issue requirement does not apply. Do not rely on STA-17039 alone. 2. Add the AI Disclosure section and state which AI model, if any, was used. If the author is a StablyAI team member, state that the section is skipped. 3. Clarify whether the manual test was run in the app. The description says the checks were run with scripts and not in the app, so the manual-testing box is unchecked. Say what was and was not tested. 4. Either check the pnpm lint, typecheck, test, and build item or state which of these ran locally and which CI will cover.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0ba2d61a-6b50-4e8d-95ce-da3eb527bd97
Commits

Reviewing files that changed from the base of the PR and between 55a7e15 and 71213f7.

Files selected for processing: 3

  • src/main/bots/bot-memory-history.test.ts
  • src/shared/bot-memory-anchors.ts
  • src/shared/bot-memory-quick-add.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fb13d0f4-08f1-4125-b65c-75637679b8e0
Commits

Reviewing files that changed from the base of the PR and between 0c72e64 and 1130ef5.

Files selected for processing: 1

  • mobile/src/i18n/locales/en.json

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

Comment thread mobile/src/i18n/locales/en.json Resolved
@AmethystLiang
AmethystLiang force-pushed the AmethystLiang/sta-17039 branch 6 times, most recently from 788ee8d to af07d35 Compare November 25, 2026 21:33
@AmethystLiang
AmethystLiang force-pushed the AmethystLiang/sta-17039 branch 2 times, most recently from 10b8795 to 8eefa05 Compare November 25, 2026 22:37
… an open code fence

A new or restored fact landing inside an unclosed fence read as code, so the
change came back empty and the add reported success with nothing saved. Facts
now go above the fence they would land in, and an add that saves nothing fails.
…g per restored fact

Restoring many facts into a MEMORY.md rescanned the whole file for fences
before each insert. Fact lines never open or close a fence, so the fence map
is now built once and shifted as lines go in, and skipped for a fence-free file.
…s turn end

A Quick add of a fact a chat wrote mid-turn found it on disk and reported it
saved, but Orca's record never took it, so a turn end that dropped it lost it.
Such an add now goes into the record. A memory change that changes no line
leaves the file byte for byte (mixed line endings stay as they are), and an
Undo row without an anchor occurrence goes after the anchor's first copy, never
a later one. The three Memory tests this branch added become one invariant test.
… at the end

When the copy of a repeated line a fact followed is gone (say one of two
'## Deploy' headings was removed), Undo and a turn end now put the fact after
the last copy still there, instead of at the end of MEMORY.md past the lines a
session loads. A blank Quick add is rejected inside the add itself too.

Tests cover the anchor copy (repeated, removed twin, later fenced copy), a
duplicate Quick add leaving mixed line endings byte for byte, a fact only the
file has being recorded, and a blank add.
… add

A Quick add of a fact MEMORY.md already stated (a chat wrote it mid-turn)
changes only Orca's record, but its history row said the owner added it, so
Undo took every copy of that fact out of the file. A row now leaves out a fact
the record took that the file already stated, and that Quick add reports the
fact is already in memory, with nothing to undo. The record still has it, so a
turn end that drops it puts it back.

An older history row without an occurrence goes back after the last copy of
its line again, as before occurrences were recorded. Neither it nor a counted
occurrence can land after a copy inside a code fence, which no fact follows.
Putting facts back counts each line's copies once per rewrite and searches
from the end, so a long unchanged start is no longer read once per fact.
A Quick add of a fact a chat had just written put Orca's own unlabeled line
in the record, so the chat's turn end saw the fact as already recorded and
dropped where it came from. The record now adopts such a fact instead: it
stays out of the record until the turn end records it with its source like
any new fact, is put back if lost before then, and is never kept out.

An Undo whose only effect was on the record wrote an empty row, which history
skips, so the change it undid still offered Undo. An Undo's row now keeps
everything it did. Other rows still leave out what changed nothing for the
record: a fact the file already had, and one the record already had, whose
Undo would make a chat's drop stick.

A Quick add of only a bullet or separator ("-", ":") is rejected. A change
with nothing to do returns at once, and an anchor copy is looked for from both
ends of the file at once.
…fer it

A Quick add of only a bullet or separator ("-", ":") was refused with "Missing
text", which the desktop showed as "Something went wrong" and the phone showed
untranslated. Settings and the phone now offer Save only for text with a
fact's words in it, by the same check the host uses. The host refuses any
other add with a code both show as "Add some words first." in the owner's
language.
…hing

A Quick add of a fact the file already states (a chat wrote it mid-turn) took
it into Orca's record so a turn end that dropped it would put it back. Each way
of keeping that consistent with Undo and with where the fact came from left
another case where Undo deleted the chat's copies, a Forget didn't stick, or a
fact lost its source. Such an add now changes nothing, as before this branch:
it says the fact is already in memory, leaves no history row, and the chat's
turn end records the fact with its own source. The record and turn end are as
on main.

The no-words check now reads the line as Quick add writes it, so an en or em
dash alone (which reads as the date's separator) is refused like "-" or ":".
The phone maps the host's refusal where its reply is read, so it says why in
the owner's language instead of showing the code.
…eading when a copy is added above

An Undo that writes only its History row (a chat had already put the fact
back) leaves MEMORY.md byte for byte the same, so open History lists on the
desktop and the phone, which reload when the file changes, kept their old rows.
Both now read History again after every Undo.

A row now also records how many copies of its anchor line there were. With
as many copies as then, the fact goes back after the same copy; once copies
came or went it goes after the one as far from the end, so a heading added
above no longer moves it to another section, and it never lands worse than an
older row's last copy would.
…he fact

Counting the copy from the end once copies came or went fixed a heading added
above but broke one added below: the fact went back under a later twin, past
the lines a session loads. A row now records the first line after the fact
that stayed, and a put-back goes after the one copy whose part of the file,
up to the next copy, still has that line, whatever came or went above or
below. When that can't tell (no such line, or it is in more than one part or
none) it falls back to the copy at the fact's occurrence, else the last copy.
The row's count of copies is gone.

The line that followed only helps place a fact, so a secret-looking one is
left out of the row rather than making it non-undoable.
…as on main

Choosing which copy of a repeated line (such as a second "## Deploy") a fact
goes back under kept finding a new arrangement of copies that put it in the
wrong section, so this goes back to main's rule: after the last copy of the
line it followed. History rows hold only that line again, as on main. What
stays from this branch: a copy that opens a code fence or sits inside one is
never that line, so a fence can't pull a fact into code, and an add or put-back
whose place is inside a fence goes above it. Picking the right copy is left to
#49287.
@AmethystLiang
AmethystLiang force-pushed the AmethystLiang/sta-17039 branch from 8eefa05 to 5eb7dd5 Compare November 25, 2026 23:22
Conflicts were mechanical: the bot error code list moved to
bots-lifecycle-params.ts (bot_memory_no_fact added there), the mobile
Memory page now reads theme colors and the date locale through hooks,
and the two new Memory toast strings sit next to keys main added.
@AmethystLiang
AmethystLiang merged commit 21307af into main Nov 29, 2026
58 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.

1 participant