Skip to content

fix(session): rebuild index on boot to find missing sessions - #1390

Merged
sailist merged 3 commits into
MoonshotAI:mainfrom
sailist:feat/session-index-reindex-on-boot
Jul 5, 2026
Merged

fix(session): rebuild index on boot to find missing sessions#1390
sailist merged 3 commits into
MoonshotAI:mainfrom
sailist:feat/session-index-reindex-on-boot

Conversation

@sailist

@sailist sailist commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue; the problem is described below.

Problem

Some sessions that still exist on disk (and that the TUI can see and resume) are missing from the web session list, and opening them directly by session id returns 404. Forking such a session in the TUI makes the new id accessible again.

Root cause: the server's session list/detail path reads only the global session index and does not fall back to scanning directories. If a session's index line is missing or invalid (e.g. never written, home-dir relocation, or a torn line from concurrent appends), the server cannot find it — while the TUI, which scans the per-workdir directory directly, still can. Forking rewrites a fresh, valid index line, which is why it masks the bug.

What changed

  • On server startup, scan the session directories once and rebuild the index entries that are missing or stale, so the scan-free request path can still find every session.
  • Make each session directory self-describing by persisting its workDir into state.json, and read summaries from there. This is required because the bucket directory name is a one-way hash of the workDir, so a disk-only rebuild cannot recover it otherwise; it also removes a class of index invalidation caused by home-dir relocation.
  • Harden the index against future invalidation: stop dropping entries over a non-authoritative workDir, and serialize in-process index appends to avoid torn lines.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

sailist added 2 commits July 5, 2026 13:41
- persist workDir into state.json so session dirs are self-describing and
  summaries do not depend on the index's one-way-hashed workDir
- relax readSessionIndex so a stale or non-absolute index workDir no longer
  drops an otherwise valid entry
- serialize in-process index appends to avoid torn jsonl lines
- add SessionStore.reindex() and run it once at server boot so the
  scan-free request path can find sessions whose index line is missing
  or stale
@changeset-bot

changeset-bot Bot commented Jul 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f6f9d3f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 5, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@f6f9d3f
npx https://pkg.pr.new/@moonshot-ai/kimi-code@f6f9d3f

commit: f6f9d3f

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6bb3712a2f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

if (resolve(sessionDir) !== resolve(expectedDir)) continue;

const existing = index.get(id);
if (existing !== undefined && resolve(existing.sessionDir) === resolve(sessionDir)) continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Repair entries even when only workDir is stale

When an existing index line already points at the correct sessionDir but has a stale or bogus workDir (the kind of entry readSessionIndex now intentionally keeps), this early continue prevents boot reindex from appending the recovered workDir. That leaves legacy sessions whose state only has custom.cwd without a top-level workDir still using entry.workDir in summaryFromDir, so list/resume/fork can continue to use the wrong cwd even after the rebuild; the skip should also compare the recovered workDir before treating the entry as healthy.

Useful? React with 👍 / 👎.

@sailist
sailist merged commit 083d0ca into MoonshotAI:main Jul 5, 2026
10 checks passed
This was referenced Jul 5, 2026
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