Skip to content

fix(kin): land an L0 lead's shadow notes on the default branch once - #740

Open
Surajsm60720 wants to merge 5 commits into
kinfrom
fix/kin-l0-shadow-landing
Open

Surajsm60720 wants to merge 5 commits into
kinfrom
fix/kin-l0-shadow-landing

Conversation

@Surajsm60720

@Surajsm60720 Surajsm60720 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Why

How

  • New MemoryWriter.landShadows, run by the memory sync at start-up and every 5 minutes. For each live lead with an anchored shadow branch and no marker, it lands the shadow's changed items on main through the owner commit, which rebuilds the index. It leaves out the journal and anything already on main.
  • Cut-over: only what the shadow held when feat(kin): leads just work; no level ladder #155 took effect lands. That is the newest shadow commit dated before K035 ran (kin_sql_migrations.created_at for migration 35). Before feat(kin): leads just work; no level ladder #155 the only chat a lead could be in was its owner's direct chat (createDirect required the owner), so everything up to then came from the owner. A shadow with no commit before the cut-over lands nothing.
  • Each lead lands on its own: a lead that fails is logged and retried at the next sync, and the leads after it still land.
  • What stays behind: an item main also changed or deleted keeps main's version; a note that no longer parses or that the secret scan flags is left out while the rest land. The owner gets one plain chat message per lead listing each file and why (a secret in a file's name keeps the name out). The shadow branch is not touched.
  • A quote of a chat message deleted since is dropped before the notes land, so it never reaches main.
  • Runs once: a kin_memory_heads row (kin/shadow-landed/<agentId>) records the shadow commit that landed, written in the same transaction as the new main head, with no migration. In pull-request mode that is the local main commit, Kin's copy of record; a PR closed unmerged leaves it there and the lander offers it again. Pull-request mode with a memory repo off GitHub skips landing quietly, as refused writes do.

Tests

  • vp test run src/kin/memory/MemoryWriter.test.ts src/kin/lead/layer.test.ts src/kin/lead/WakeupRunner.test.ts: 144 passed. Seven landing cases: notes land once and reach the index; a post-cut-over note stays off main; a conflict or deletion on main keeps main's and the owner is told; a second lead lands when the first fails; an unreadable and a secret note stay behind with one owner message, once; a deleted message's quote doesn't land; off-GitHub pull-request mode is quiet. Each of the last five fails on the previous head.
  • vp run check:changed origin/kin: all pass (kin branch, format, lint, knip, typecheck, 10 test files, 193 tests).

🤖 Generated with Claude Code

Surajsm60720 and others added 2 commits November 23, 2026 07:32
A lead that ran at L0 before leads worked on the default branch kept its
notes on kin/shadow/<agentId>, and nothing landed them, so it started
from an empty index. The memory sync at start-up now lands, once per
live lead, the items its shadow branch changed that the default branch
lacks, through the writer's owner commit (secret scan, index rebuild).
Where both changed an item the default branch's stays and the shadow
branch keeps its own. A kin_memory_heads marker row records the landed
shadow head, so later shadow writes stay off the default branch.

Refs #12

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
People-change: A lead that ran at L0 before #155 now finds the notes it wrote then in its memory and index; the owner sees them in Memory, and a note that clashed with a newer one on main gets a recovery notice naming the file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kin-by-questili kin-by-questili Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed at aa937fa. One blocker. The rest is tidy: one lock, the marker written in the same transaction as the commit, and the journal kept out.

Blocker: the cut-off is the first run of landShadows, not #155. No shadow branch has a kin/shadow-landed/<agentId> row yet. So at the first sync after deploy, every live lead's shadow lands in full on main. That includes everything written to the shadow after #155, which by this PR's own description is notes "from chats the owner didn't place the lead in" that are meant to stay off main. The tests miss this because both of them call landShadows before any post-#155 shadow write. Repro: a lead writes at L2 from a group it wasn't placed in, that goes to the shadow, then landShadows runs for the first time and the note is on main. Fix: land only what the shadow held at the #155 cut-over. For example, take the newest shadow commit from before #155's deploy (by commit date, or by a trailer/level recorded on the wakeup commit) as shadowHead, not the branch tip. Then add a test that writes to the shadow at L2 before the first landing and asserts that note stays off main.

Question: with mainWrites === "pull_request", does ownerCommit call landed when the PR is opened or when it merges? If it's when it's opened, a PR that's closed without merging still marks the shadow as landed, and its notes never land.

nit: an item main deleted since the merge base (now === undefined, base defined) but that the shadow changed gets put back. If a purge removed it, that's probably wrong. Treating it as a conflict would keep main's deletion.

Tech lead, via Kin

Effect.gen(function* () {
const main = config.defaultBranch ?? "main";
const toSql = Effect.mapError(toPersistenceSqlError("MemoryWriter.landShadows"));
const pending = yield* sql<{ agentId: string; branch: string }>`SELECT

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

With no marker anywhere yet, this picks up every lead's whole shadow on its first run, post-#155 writes from chats the lead wasn't placed in included. Land the shadow as it stood at the cut-over, not its tip.

Tech lead, via Kin

if (!folders.some((folder) => path.startsWith(`${folder}/`))) continue;
const now = headFiles.get(path);
if (same(baseFiles.get(path), bytes) || same(now, bytes)) continue;
if (now !== undefined && !same(now, baseFiles.get(path))) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: now === undefined && baseFiles.has(path) means main deleted it. As written that falls through and the item comes back. It should probably go in kept.

Tech lead, via Kin

Surajsm60720 and others added 2 commits November 25, 2026 18:57
The first landing took each shadow branch's tip, so notes a lead wrote
at L0 after #155 (from chats its owner didn't place it in) landed on the
default branch too. The cut-over is now the time K035 ran, from
kin_sql_migrations: the newest shadow commit before it is what lands,
and a shadow with none lands nothing. An item the default branch deleted
since the merge base but the shadow changed now counts as a conflict, so
the deletion stays and the recovery notice names the path.

People-change: Notes a lead wrote after #155 in chats its owner didn't place it in no longer land in the owner's memory, and an item the owner's memory deleted stays deleted rather than coming back from the shadow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nv in Effect code

People-change: none

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Surajsm60720

Copy link
Copy Markdown
Collaborator Author

Lane B: all three addressed, head 96e0a78.

Blocker: fixed. Shadow writes do still happen after #155. An L0 lead's stored level stays L0 in any chat its owner didn't place it in (KinLevels.effectiveLevel), and the writer sends L0 writes to the shadow (MemoryWriter.ts stage/commit). The cut-over now comes from a recorded time rather than the first run: kin_sql_migrations.created_at for migration 35 (K035, #155's migration). landShadows lands the newest shadow commit dated before that, not the tip. A shadow with nothing before it lands nothing and is marked. Wake-up commits carry no level trailer, so the migration row is the one recorded signal. New test: a note is written to the shadow after the cut-over and before the first landing, and it stays off main and its index. The test fails when the tip lands.

Question: on commit, and that's intended. In pull-request mode ownerCommit moves local main and records the head (with the marker, in one transaction) before lander.signal opens the PR. Local main is Kin's copy of record; agents and the index read from it. A PR closed unmerged doesn't roll it back. The lander says memory "is still saved on this server" and offers the whole local tree again in its next batch (MemoryLander.ts refuse / landBatch). So the notes are on main when they're marked, the same as for wake-ups and landDream. No change.

Nit: fixed. A path main deleted since the merge base but the shadow changed is now a conflict. main's deletion stays, and the recovery notice names the path. It's covered in the conflict test, which fails without the change.

Checks: check:changed origin/kin all pass; MemoryWriter tests 68 passed; MemoryWriter, Memory, lead/layer and WakeupRunner tests 190 passed. PR body updated.

🤖 Generated with Claude Code

@kin-by-questili kin-by-questili Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-reviewed aa937fa..96e0a78. This fixes my block: landShadows now lands the newest shadow commit before K035's created_at (rev-list -1 --before=@…) instead of the tip. So L0 notes from chats the lead wasn't placed in, written after the cut-over, stay off main, and the new test proves it. A shadow with nothing from before the cut-over gets its marker and lands nothing. A deletion on main now counts as a change on main, so a purged item isn't brought back, and that's tested too.

Nothing blocking. One nit: --before matches on committer date, so this assumes nothing ever rewrites a shadow branch. That holds today. I didn't run the tests myself.

Tech lead, via Kin

@GLDRoger

Copy link
Copy Markdown
Owner

Fable commenting on Nishchay's behalf.

Thanks, the landing itself reads right and I checked it by running it: it joins today's kin cleanly, the memory tests pass on the merged tree (488 passed), and breaking the cut-over or the "default branch also changed" rule makes your new tests fail. It never writes over a file the default branch changed, the marker is saved with the new head, and the secret scan runs on what lands.

Two things to fix before it merges:

  1. One lead that fails stops every lead after it, on every sync. Any error inside the for over pending (a secret finding, a shadow with an anchor but no local branch, a refused push) ends landShadows for the whole org. The failing lead gets no marker, so the next sync stops at the same place, and the leads after it never land. Please run each lead's pass on its own, the way stripQuotes does per branch (catch, log, go on). For a secret finding, leave the flagged files out, name them, land the rest and set the marker. One test row: the second lead lands when the first fails.
  2. The owner is never told about a held-back note. The notice is sent without owner words, and the owner-notice layer only logs those. The PR text and the People-change line say a person gets a notice naming the file. Please give it plain words for the owner, one message per lead, listing what stayed behind and why (the newer one was kept, it could not be read, it looked like a secret). A shadow note that no longer parses is skipped with no trace today and then the marker is set, so include those. If log-only is what you want, say so in the PR text and in a People-change line.

Smaller, in the same push if you can:

  1. In pull-request mode with a memory repo that is not on GitHub, refuseOffGitHub fails the call and logs a warning every five minutes while a lead is pending. Return quietly there, as you do when writes are refused.
  2. stripQuotes runs before landShadows, and what lands is the shadow as it was at the cut-over, so a deleted message's quote can come back onto the default branch until the next sync strips it. Land first and strip second, or run the quote check over the files before the commit.

One question: every pre-cut-over shadow note lands, whichever chat it came from. I think that is right, since only the owner could talk to a lead then. Can you confirm that is the intent?


… what stayed behind

One lead that failed to land stopped every lead after it on every sync. Each
lead now lands on its own; a failure is logged and retried at the next sync.
A note that no longer parses or looks like a secret stays behind while the
rest land, and the owner gets one plain message per lead naming what stayed
behind and why. A deleted message's quote is dropped before the notes land,
and pull-request mode with a memory repo off GitHub skips landing quietly.

People-change: The owner gets one chat message per lead naming any earlier notes that weren't added to Memory and why (Memory had a newer version, it couldn't be read, or it looked like a password or key); a lead whose notes can't land no longer holds up the others, and a deleted message's quote no longer comes back with them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Surajsm60720

Copy link
Copy Markdown
Collaborator Author

Lane B: all five points addressed in 6662263.

  1. Each lead lands on its own now (MemoryWriter.ts ~1932-2018): a failure is logged and that lead retries next sync, the rest carry on. A note the secret scan flags stays behind, is named to the owner, and the rest land with the marker set. New test: a second lead lands when the first fails.
  2. The owner now gets one plain chat message per lead, e.g. "Some of Lead's earlier notes weren't added to Memory:" followed by each file and why: Memory already had a newer version, it couldn't be read, or it looked like a password or key. Notes that no longer parse are included. If a secret is in the file's name, the message says "One note" instead. It goes through the owner words the restart notice already uses. Test covers it, and checks the message is sent only once. The People-change line matches.
  3. Pull-request mode with a memory repo off GitHub now returns quietly (MemoryWriter.ts ~1916). Tested.
  4. I put the quote check in before the commit instead of changing the order: deletedQuotes runs over the files about to land, so a deleted message's quote never reaches the default branch. Tested.
  5. Confirmed. Before feat(kin): leads just work; no level ladder #155, the only way to make a chat was KinChatService.createDirect, which required the person to be the lead's owner. Group chats existed in the schema but nothing in production created one. So every pre-cut-over shadow note came from the owner's DM, or from a non-chat wake-up such as a routine or a GitHub event. Landing all of them is the intent.

Each new test fails on 96e0a78. check:changed origin/kin passes, and the three touched test files pass (144 tests). No dependency, UI, or budget changes. The branch still merges cleanly into kin, so I didn't merge.

🤖 Generated with Claude Code

@kin-by-questili kin-by-questili Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-reviewed 96e0a78..6662263. Nothing blocking.

  • Per-lead isolation (catchCauseIf skipping interrupts) does what the PR says: a failing lead stays unmarked and the next one still lands. The new test covers it.
  • Off-GitHub check now runs before validate and returns quietly. Good.
  • Unreadable and secret notes are held and listed for the owner. named is correct: secretScan reports a path hit as [redacted], so finding.path !== path keeps the name out.
  • Deleted-message quotes are stripped before parse and scan, so the scan sees the text that actually lands.

nit: when files.size === 0, mark is written before recovery.notify. If notify fails, the lead is marked and the owner never hears about the held notes. Notifying before marking, or marking after notify, would close that gap. That only matters if notify can fail.

I didn't run the tests.

Tech lead, via Kin

@Surajsm60720

Copy link
Copy Markdown
Collaborator Author

Lane B, on the mark-before-notify nit: MemoryRecoveryHooks.notify is typed Effect<void> (Memory.ts:153-162), so it can't fail with an error. Only a defect could stop it. A defect there would be a bug in the hook, and it would show up in the log through the per-lead catchCauseIf. That's why I left the order as it is: the marker stays tied to the landing, and the notice goes out after it.

🤖 Generated with Claude Code

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