Skip to content

refactor(runtime): extract history-compact checkpoint coordinator from runtime-kernel.ts and dedupe kernel internals #1406

Closed

Description

@Astro-Han

Part of #1084 (second-pass sink-file and boundary governance). Scope re-verified against current main (runtime-kernel.ts, 1991 lines).

Scope

  • Extract the stateful checkpoint coordinator into a sibling next to history-compact-checkpoint.ts (which holds only pure builders/policy today): the historyCompactCheckpoints map (~L233), loadHistoryCompactCheckpoint (~L1388), recordHistoryCompactCheckpoint + CAS via canReplaceHistoryCompactCheckpoint (~L1421), scheduleHistoryCompactCleanup (~L1449), including the mid_turn/child-lineage partitioning added by feat(runtime): migrate agent tools to child sessions #1385 (~L1652–1666).
  • Rename the private AsyncEventQueue family (~L1907–1989) to reflect its distinct contract (per-entry delivery-ack with backpressure, vs the seq-ack single-consumer queue in async-queue.ts), e.g. DeliveryAckQueue, in its own file with the contract difference documented.
  • Deduplicate ensureActive (~L1490) / ensureChildActive (~L1606): ~90 lines of recorder-hook wiring are verbatim copies parameterized only by the active-session resolver; one intentional hook omission on the child side (~L1660) must be preserved explicitly.

Notes

session-manager.test.ts:8491 pins kernel internals by slicing source text; co-migrate that assertion to a behavioral/typed check (see #1403 — do not add new source-regex contracts).

Invariants

Same as #1084: behavior-neutral; stable public exports via re-exports; no reverse imports; flat PR off main.

Verification

Typecheck/build + runtime kernel/session tests.

Activity

  1. added
    enhancement
    on Jul 23, 2026
  2. chinawch007 commented on Jul 27, 2026

    @chinawch007
    Contributor

    I'd like to claim this task. Plan is to ship it as a single PR with four behavior-neutral commits, ordered by dependency:

    1. Rename the private queue → DeliveryAckQueue in its own file (delivery-ack-queue.ts), with a header comment documenting the contract difference vs async-queue.ts (per-entry delivery-ack with backpressure vs fire-and-forget push). Pure rename + move; the two new AsyncEventQueue(...) and isAsyncEventQueueClosed(...) call sites in runAgentTurn / runAgentContinuation update accordingly.

    2. Extract HistoryCompactCheckpointCoordinator into a sibling next to history-compact-checkpoint.ts. The coordinator owns the four maps (checkpoints/loads/writes/cleanups) and the three methods (load / record / scheduleCleanup); record takes the AgentRun as a per-call arg (resolved by the caller from this.active / this.childActive), so the coordinator never reaches into kernel session state. disposeBackendNow's two Map.delete calls become a single coordinator.clear(sessionId). HistoryCompactCleanupRequest moves with it; runtime-kernel.ts re-exports the type so session-manager.ts keeps its import path.

    3. Deduplicate ensureActive / ensureChildActive via a private buildBackendRecorderHooks({ resolveActive, sessionId }) helper. The 8 recorder hooks collapse to one parameterized block (only the active-session resolver differs). The intentional deltas stay explicit at the call sites: allowMidTurnHistoryCompaction (Boolean(runtimeEventStore) vs false), shellRunContextSummary (present on the parent, absent on the child), and the system-prompt/tools provisioning (subagent snapshot vs explicit args).

    4. Migrate session-manager.test.ts source-regex assertion to a behavioral check (per test: replace source-regex extraction contracts with behavioral assertions #1403 — no new source-regex contracts). The existing test slices runAgentTurn's source to assert new AiSdkFlow / absence of mapSessionEventToRuntimeEvent. Replacing it with a behavioral test that asserts the AiSdkFlow ingress authority: a backend that yields a forged queue_update event has it dropped from the turn's event stream (AiSdkFlow drops these at ai-sdk-flow.ts L763; an inline mapper would not).

    Verification: runtime typecheck + build; full @maka/runtime test suite (2554 tests); lint + format:check. Flat off main, behavior-neutral, no reverse imports from the new leaves.

  3. chinawch007 commented on Jul 30, 2026

    @chinawch007
    Contributor

    Completed in #1675 . This splits runtime-kernel.ts via behavior-neutral refactors — extracting DeliveryAckQueue and HistoryCompactCheckpointCoordinator into self-contained sink files, deduplicating the ensureActive/ensureChildActive recorder hooks via a shared buildBackendRecorderHooks helper, and dropping the #1403-forbidden source-regex test (its invariant already covered by the behavioral "a backend-forged queue_update never reaches the ledger or observers"(session-manager.test.ts:17641)).

  4. Astro-Han commented on Aug 2, 2026

    @Astro-Han
    ContributorAuthor

    Landed in #1675: DeliveryAckQueue and the history-compact checkpoint coordinator are extracted out of runtime-kernel.ts, ensureActive is deduplicated, and the source-regex tests are gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions