Skip to content

feat(agent-core-v2): enforce minimum wire reader version - #4638

Merged
sailist merged 2 commits into
mainfrom
feat-327-11-02-wire-min-reader-version
Nov 4, 2026
Merged

sailist merged 2 commits into
mainfrom
feat-327-11-02-wire-min-reader-version

Conversation

@sailist

@sailist sailist commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Older readers currently replay newer wire journals even when the writer knows the records are incompatible, which can silently discard new record semantics. This PR lets writers declare the minimum reader protocol required by a journal and makes incompatible readers fail with actionable upgrade guidance. kap-server exposes the same failure as a structured compatibility error instead of a generic internal error.

Bug Reproduction Steps

N/A — feature PR.

Root Cause

N/A — feature PR.

Code Changes

  • Add min_protocol_version to wire metadata and maintain it independently from the journal protocol version, so additive protocol changes can remain readable while incompatible changes can require an upgrade.
  • Validate the declaration before restore, replay, ordinary fork, or sliced fork can migrate, rewrite, or copy records. Preserve the existing tolerant replay path for journals without the declaration, reject malformed declarations as corrupted metadata, and stamp the declaration during journal creation and healing rewrites.
  • Add the public wire.version_too_low domain error with required and current reader versions plus upgrade guidance.
  • Map that error to kap-server 40930 WIRE_VERSION_TOO_LOW through transport, catch-all, and fs error handling, and declare it on status, goal, and existing session-conflict routes.
  • Validate cold transcript reads before branch flattening or transcript reduction, preserve the same code through message-history handlers, and validate fork source records before computing turn slices.
  • Expose wire.version_too_low through the SDK public error contract and propagate incompatible subagent restore failures instead of silently omitting those agents.
  • Declare 40930 on every route that can materialize a cold session, and restate errors from SDK temporary-session cold loads.
  • Validate journal metadata before indexing and remove derived documents and offsets for incompatible or malformed journals without importing the engine runtime into the self-contained search worker.
  • Roll back sessions materialized by a failed cold resume without closing sessions that were already live, determining ownership inside the serialized session queue so concurrent callers cannot close each other's sessions.
  • Treat invalid JSON, null, and other malformed first-line metadata as per-file indexing failures while continuing other sessions.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
Create or heal a wire journal Metadata contains only the protocol version and creation time Metadata also declares the minimum reader protocol New journal writers and migrated journals Older binaries ignore the added field; journals without it retain the old behavior
Read a journal with a supported or absent minimum version Restore/replay/fork proceeds under existing version rules Unchanged Existing sessions and older journal fixtures Not needed; covered by current-version, newer-version, and replay tests
Read a journal requiring a newer reader The journal can be replayed tolerantly, with unknown semantics potentially skipped Restore/replay/fork fails with both versions and an upgrade prompt Users opening a session whose writer explicitly raised the minimum reader version Upgrade Kimi Code; covered by restore, replay, and cold-fork tests
Encounter a malformed minimum-version declaration The unknown metadata field is ignored The header is rejected as corrupted metadata Only journals carrying an invalid value for the new field Correct the header; covered by wireService tests
Resume through kap-server The domain error becomes INTERNAL_ERROR The response is 40930 WIRE_VERSION_TOO_LOW with the upgrade message REST clients cold-loading or forking incompatible sessions Upgrade Kimi Code; covered by real cold-status and cold-fork REST tests and transport tests
Read cold transcripts or message history Raw records can be folded without the compatibility check, and message routes can return INTERNAL_ERROR Cold transcript and both message endpoints reject with 40930 before interpreting records Desktop/web cold transcript and message-history readers Upgrade Kimi Code; covered by cold transcript, user-messages, and messages REST tests
Resume through the SDK Engine errors lack a public code, incompatible subagents can be omitted silently, and failed cold resumes can leave the session live Main and subagent incompatibilities throw public wire.version_too_low, and newly materialized sessions are rolled back on failure using ownership captured inside the serialized resume queue SDK callers resuming sessions with incompatible main or subagent journals Upgrade Kimi Code; covered by main and subagent resume tests plus post-failure liveness checks
Cold-load through other REST routes Some routes cannot serialize or declare the compatibility error Every cold-materialization route declares and returns 40930 Environment, interaction, file-history, media, prompt, skill, fs, and other session clients Upgrade Kimi Code; covered by environment/environments REST tests and OpenAPI tests
Index an incompatible or malformed journal Derived documents and completed offsets can be retained without an upgrade signal, and malformed metadata can abort other sessions Only the affected file fails; its derived documents and metadata are removed while other sessions continue indexing Global-search users and the self-contained search worker Upgrade Kimi Code or correct the header; covered by incompatibility, malformed-header isolation, and full search-worker tests

Checklist

  • I have read the CONTRIBUTING document.
  • No related issue; the requirement is described above.
  • I have added test coverage that proves the feature works without increasing the test count.
  • The behavior-change table above is complete, including compatibility for journals without the new declaration.
  • Ran gen-changesets; the user explicitly decided not to include a changeset.
  • No gen-docs update is needed; the generated wire manifest is updated.

@changeset-bot

changeset-bot Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 8dfdd2b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T11:19:49.108047Z 8dfdd2b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 3b3b721213

ℹ️ 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".

@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: 7ef0db5e48

ℹ️ 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".

@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: e505701290

ℹ️ 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".

@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: e0afe58422

ℹ️ 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".

@sailist
sailist force-pushed the feat-327-11-02-wire-min-reader-version branch from 27d27ec to 18b9bd2 Compare November 3, 2026 09:54
@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026 •

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

commit: 8dfdd2b

@sailist

sailist commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@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: 18b9bd2b5b

ℹ️ 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".

});
});
} catch (error) {
if (!wasLive) await closeSessionById(this.engineAccessor, input.id).catch(() => {});

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 Keep failed-resume cleanup inside the session queue

When a cold resume fails after materializing the session and another lifecycle operation for the same ID is already queued, runSessionAccess releases its tail before this outer catch executes, so the cleanup can race the next resume/reload and close the session that operation is using or has just returned. The fresh evidence is that the liveness check is now inside the serialized callback, but closeSessionById still runs outside it; perform the rollback before the callback rejects and releases the queue.

Useful? React with 👍 / 👎.

Comment on lines +416 to +419
const outputRecord =
rewrittenRecords !== undefined && normalized.type === 'metadata'
? { ...normalized, min_protocol_version: WIRE_MIN_READER_VERSION }
: normalized;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Raise the header minimum when writer requirements increase

When WIRE_MIN_READER_VERSION is raised independently while an existing journal is already at WIRE_PROTOCOL_VERSION but has an absent or lower minimum, rewrittenRecords remains undefined and this condition leaves the old header unchanged. The new writer can then append records requiring the raised minimum, while older readers trust the stale declaration and silently replay an incompatible tail—the exact data-loss mode this guard is meant to prevent; trigger a metadata rewrite whenever the declared minimum is below the writer requirement, not only during protocol migration.

AGENTS.md reference: AGENTS.md:L130-L132

Useful? React with 👍 / 👎.

Comment on lines 900 to 902
if (first.type === 'metadata') {
assertWireRecordsReaderVersion([first]);
this.appendLogStore.append(scope, AGENT_WIRE_RECORD_KEY, forkedRecord(agentId));

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 Validate ordinary forks before starting target writes

When an ordinary fork contains multiple agents and one copied journal is incompatible, this assertion rejects from one appendForkedMarker task only after the target directory has been copied, while sibling tasks in the surrounding Promise.all may already have queued marker appends. Because Promise.all rejects without waiting for those siblings, the outer catch can remove the target while an append-log flush is still running, allowing that flush to recreate a partial orphan target after the failed fork. Validate all source headers before copying or writing the target, or wait for every marker task to settle before cleanup.

Useful? React with 👍 / 👎.

@sailist
sailist force-pushed the feat-327-11-02-wire-min-reader-version branch from 18b9bd2 to 8dfdd2b Compare November 3, 2026 11:17
@sailist
sailist merged commit f72dc7e into main Nov 4, 2026
16 checks passed
@sailist
sailist deleted the feat-327-11-02-wire-min-reader-version branch November 4, 2026 02:21
@sailist sailist mentioned this pull request Nov 4, 2026
5 of 6 tasks
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Core slice of upstream MoonshotAI/kimi-code f72dc7e (MoonshotAI#4638).

A wire journal can now declare `min_protocol_version`, and a reader that
is older than that refuses to open it with `wire.version_too_low`
instead of misreading records it does not understand. New journals are
stamped with `WIRE_MIN_READER_VERSION` on creation, on migration rewrite
and when rewriting a repaired journal.

Not yet ported from the same commit, and the ledger says so: the
kap-server route mappings (each route answers the new code), the search
index changes, and the node-sdk error entry.

The added case seeds a journal requiring reader `99.0` and fails with
the assertion removed.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Server slice of upstream MoonshotAI/kimi-code f72dc7e (MoonshotAI#4638).

A session whose wire needs a newer reader now surfaces as 40940 instead of
an internal error: the handler maps the engine code before its generic
fallback, the transport error table carries the pair, and the routes that
can hit a wire read declare it for the OpenAPI surface.

Fork adaptation: 40930 is `DEFAULT_WORKSPACE_UNAVAILABLE` here, so the
code takes the next free value (40940) instead of upstream's number. The
environment, interactions, sessionRating and tools routes are not
declared yet — their error maps use anchors this fork does not carry.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Search slice of upstream MoonshotAI/kimi-code f72dc7e (MoonshotAI#4638).

A cold session whose wire declares a newer `min_protocol_version` used to
be indexed anyway, so search returned snippets from a journal this build
cannot read and the index kept documents for a session the rest of the
server refuses to open. Indexing now checks the metadata line first,
drops that session's documents and reports the failure.

The added case seeds a wire requiring reader 99.0 and fails when the
guard is removed. The search suite carries five pre-existing failures in
its lifecycle-diagnostics block; they reproduce on the pre-change tree.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Last slice of upstream MoonshotAI/kimi-code f72dc7e (MoonshotAI#4638).

The SDK error table gains the entry so a host renders "Kimi Code upgrade
required" instead of an unknown code, and the wire manifest is
regenerated for the metadata record's new optional field.

node-sdk's `tsc` still reports the pre-existing `#/errors` resolution
errors this branch has carried since before the change; its test suite is
76 pass / 3 skip.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
…target

MoonshotAI#4638 lands in four slices: core enforcement (4d49fcb), the kap-server
mapping and route declarations (8988dea), search skipping an
unreadable wire (1a34ae1), and the SDK entry with a regenerated
manifest (65a6412).

MoonshotAI#4662 stays pending rather than closed: its reworded message has a site
now, but it also lands on the full route-declaration set, and four
routes here (environment, interactions, sessionRating, tools) carry no
`WIRE_VERSION_TOO_LOW` declaration because their error maps use anchors
this fork does not have.

Ledger: closed 40 -> 43, remaining 56.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Port of the mention-provider half of upstream MoonshotAI/kimi-code
df7efe9 (MoonshotAI#4515).

The filesystem fallback scanned one root to exhaustion before the next,
so a project directory holding more than the 2,000-entry cap starved the
source folders entirely: `@target-needle` found nothing even though the
file sat in a registered folder. The scan now interleaves the roots,
one entry at a time, so unused capacity on one root is spent on others.

`~` and `~/…` entries now resolve against the home directory instead of
being treated as relative paths, and a root equal to the working
directory is dropped instead of scanned twice.

The added case uses the existing 2,000-entry fixture and fails when the
scan is narrowed to the primary root. The working-directory case pins the
deduplication.

Also completes the MoonshotAI#4638 SDK half: `node-sdk` keeps its own error-code
table, so `wire.version_too_low` had to be added there as well; the
engine registry alone was not enough and the app typecheck caught it.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Port of upstream MoonshotAI/kimi-code 1b13934 (MoonshotAI#4621).

A model or effort switch no longer errors out while a turn runs. A turn that has
already sent its first request keeps the model it was launched with, and the
switch applies from the next turn; before that first request the switch lands
immediately. Choosing Compact and continue while busy queues the whole action:
the queued item is a FIFO barrier like a queued /compact, so prompts behind it
wait, the compaction runs when the queue drains, and it is skipped when another
compaction completed in the meantime. A child agent's compaction no longer
completes the wait.

Local-form adaptations, because this fork's queue diverged from upstream's:
- The queued item type is a five-variant union here (prompt / bash / skill /
  compact / model-switch); upstream has four. Steering narrows to
  QueuedPromptMessage | QueuedSkillActivation so the fork's slash-skill queueing
  keeps working, and the two dispatcher copies (KimiTUI and
  MessageDispatchController) both handle the new mode instead of sending the
  item's /model text as a prompt.
- New user-facing copy goes through t() with en/zh keys (4 keys) instead of
  upstream's literal strings, and the queue row names the model and effort.
- compactForModelSwitch detects compaction.unable by error code rather than
  isKimiError: the SDK normalizes RPC failures across the boundary, so the
  rejection is not an Error2 instance and the type guard never matched.
- The fork's runModelSwitchAction keeps its extra 'new' action; only the
  'compact' arm gained the busy branch.
- QueuePaneComponent is a Container with its own row budget here rather than
  upstream's DockSection, so the model-switch row renders inline.

Also adds the missing wire.version_too_low error copy (en/zh) that MoonshotAI#4638 left
behind; check:t-coverage failed on it.

Verified: app typecheck clean; kimi-tui-message-flow 408 tests (407 pass, 1
skipped), model-switch-confirm 21, queue-pane 12, session-event-handler
compaction/goal-queue + program-status + create-tui-state 40, editor-keyboard +
staging-leases 73; check:locale-keys, check:locale-placeholders,
check:t-coverage, check:no-hardcoded-prose and check:no-comments green; oxlint
--type-aware 0 errors on the touched files. Reverse checks: dropping
model-switch from the barrier predicate reddens the Ctrl-S and queue-pane cases,
forcing busy=false reddens the queued-compact case, and removing the next-turn
suffix reddens two notice cases.
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