Skip to content

PR-B3 (ADR 0008 Phase B): Generate server-streaming RPC + greedy session-aware decoding - #46

Merged
FluffyAIcode merged 2 commits into
mainfrom
AgentMemory/v030-pr-b3-generate-streaming-8e7f
Jun 1, 2026
Merged

PR-B3 (ADR 0008 Phase B): Generate server-streaming RPC + greedy session-aware decoding#46
FluffyAIcode merged 2 commits into
mainfrom
AgentMemory/v030-pr-b3-generate-streaming-8e7f

Conversation

@FluffyAIcode

@FluffyAIcode FluffyAIcode commented Jun 1, 2026

Copy link
Copy Markdown
Owner

✅ Mac M4 §9 integration test report — landed (commit 16196be)

First Phase-B PR with mandatory ADR 0008 §9 Mac M4 report; carve-out
NOT applicable.
All 5 artifacts pass.

Artifact Result
pr-b3-mac-generator-tests-1780323650.json 31 / 31 passed, 100.00 % on inference_engine/session/generator.py (63 / 63 lines)
pr-b3-mac-grpc-tests-1780323650.json 39 / 39 passed (PR-B1 + B2 + B3 cumulative), 100.00 % on inference_engine/server/grpc_app.py (88 / 88 lines)
pr-b3-mac-grpc-runtime-smoke-1780323650.json 10 / 10 (PR-B1 contract regression: AppendTokens + Generate stay UNIMPLEMENTED with bare Servicer)
pr-b3-mac-grpc-appender-smoke-1780323650.json 10 / 10 (PR-B2 contract regression: AppendTokens reachable, Generate still UNIMPLEMENTED)
pr-b3-mac-grpc-generator-smoke-1780323650.json 10 / 10 (PR-B3 new)
Host macOS-26.5-arm64 / Python 3.13.12 / grpcio 1.81.0

Generator smoke per-step (PR-B3 wire behavior on Apple Silicon)

# Step Expected Observed Time
1 CreateSession ok ok 0.98 ms
2 AppendTokens (cold prefill) ok ok 0.48 ms
3 Generate (max_tokens=3, no EOS) ok ok 0.63 ms
4 Generate (EOS triggers, max_tokens=10) ok ok 1.01 ms
5 Generate (no AppendTokens prior) INVALID_ARGUMENT INVALID_ARGUMENT 0.57 ms
6 Generate (max_tokens=0) INVALID_ARGUMENT INVALID_ARGUMENT 0.33 ms
7 Generate (temperature=0.7, non-greedy rejected) INVALID_ARGUMENT INVALID_ARGUMENT 0.31 ms
8 Generate (unknown session) NOT_FOUND NOT_FOUND 0.34 ms
9 Generate (truncated state → truncated frame) ok ok 1.50 ms
10 Generate (no coordinator wired) UNIMPLEMENTED UNIMPLEMENTED 0.72 ms

10 / 10 step times in 0.31 – 1.50 ms, similar order to Linux (0.27 – 1.55 ms) — confirms gRPC streaming behavior is platform-equivalent on Apple Silicon.

Design choice: greedy first, speculative later

PR-B3 ships greedy decoding only. Speculative-decoding integration
(DLM proposer + AR verifier rejection sampling — Kakeya's
distinguishing feature) is reserved for a later PR. Two reasons:

  1. Wire contract is algorithm-agnosticGenerateResponse oneof on
    token_id / done / truncated works for any decoding algorithm.
    Switching to speculative is a coordinator-internal refactor, not a
    protocol break.
  2. Scope discipline — PR-B3 is already the first PR triggering §9;
    adding speculative would couple two large changes. Greedy is the
    minimum surface that satisfies §9; speculative integration can land
    independently with its own integration test pass.

temperature / top_p / top_k MUST be in their greedy no-op
defaults. Setting any non-default raises ValueError → INVALID_ARGUMENT
per §2.10 "no graceful degradation" — the runtime refuses to silently
downgrade.

Three deliverables

1. inference_engine/session/generator.py (new, ~240 lines)

Symbol Role
TokenEvent / HistoryTruncatedEvent / DoneEvent Frozen dataclass payloads
STOP_REASON_* String constants matching runtime_pb2.GenerateDone.StopReason
GenerationCoordinator(store, verifier) Sync iterator yielding GenerateEvent per step

Per-step contract:

next_token = int(torch.argmax(verifier.next_token_logits).item())
block_logits = verifier.forward_block([next_token])
verifier.commit_or_truncate(forwarded=1, accepted=1)
verifier.next_token_logits = block_logits[-1].clone()
session.cached_token_sequence = list(verifier.cached_token_sequence)
store.append_tokens(session_id, [next_token])     # extends history + INV-1
store.record_position_advance(session_id, verifier.next_global_position)  # INV-2
yield TokenEvent(token_id=next_token)
if next_token in eos_set: yield DoneEvent(STOP_REASON_EOS, ...); return

2. RuntimeServiceServicer.Generate (new)

Path gRPC status
Servicer constructed without generation_coordinator UNIMPLEMENTED
SessionNotFoundError NOT_FOUND
ValueError INVALID_ARGUMENT
InvariantViolation FAILED_PRECONDITION
context.cancelled() mid-stream Final GenerateDone(STOP_REASON_CANCELLED), then return
Success Stream of token_id (+ optional initial truncated), terminated by done

HistoryTruncated frame: emitted at most once per call, before any
token_id
, when the session is in sink+window-truncated state at
Generate's start. Matches runtime.proto contract exactly.

3. INV-3 byte-exact unit tests via FakeVerifier

TestDeterminism.test_two_runs_with_same_history_produce_same_tokens
drives two parallel sessions with identical history through Generate
and asserts byte-identical token streams under greedy decoding.

The 31-test generator suite covers every code path on generator.py
(63 / 63 lines) including:

  • 9 validation paths (max_tokens, sampling params, no-AppendTokens)
  • 2 invariant paths (INV-1 / INV-2 propagation through Generate)
  • 3 EOS paths
  • 3 HistoryTruncated paths
  • 1 byte-exact determinism path

Files

Status Path
add inference_engine/session/generator.py (~240 lines)
add tests/inference_engine/session/test_generator.py (31 tests)
add scripts/smoke_grpc_generator.py (10-scenario smoke)
add scripts/review_pr_b3_on_mac.sh (5-artifact reviewer)
add results/platform-tests/pr-b3-mac-*-1780323650.* (Mac M4 evidence: 5 JSON + 2 junit.xml + 2 coverage.xml)
mod inference_engine/server/grpc_app.py (+119: Generate RPC + factory keyword + cancellation)
mod inference_engine/session/__init__.py (re-exports)
mod tests/inference_engine/server/test_grpc_app.py (+11 tests including direct cancellation invocation)

Linux verification

PYTHONPATH=. pytest <Linux CI gate set> --cov-fail-under=100
629 passed (was 587 + 31 generator + 11 grpc Generate)
TOTAL  1521 stmts  100.00 % coverage

Reviewer checklist

  • Mac M4 evidence on branch: 5 JSON artifacts pushed (commit 16196be), all expected counts match.
  • Greedy contract enforced: temperature/top_p/top_k non-default → INVALID_ARGUMENT. Verified Mac M4 step Engine E2: OpenAI-compatible HTTP API with SSE streaming #7.
  • Cancellation tested: direct Servicer invocation with FakeContext confirms STOP_REASON_CANCELLED Done frame.
  • HistoryTruncated emitted exactly once per call, before any token_id. Verified Mac M4 step Engine E4: continuous batching scheduler (admission control + fair queuing) #9.
  • INV-3 byte-exact under greedy: TestDeterminism passes.
  • No new MLX runtime paths beyond per-token forward_block + commit_or_truncate (these are NOT new — they were exercised by AppendTokens in PR-B2; Generate uses them in a per-token loop).
  • PooledVerifier / inference_engine.memory/ untouched.

Next PR

PR-B4: ship sdks/python/kakeya.Client, kakeya.Session,
100% unit-test coverage against an in-process gRPC server. First
external-facing API surface; Linux-only path.

Open in Web Open in Cursor 

cursoragent and others added 2 commits June 1, 2026 14:06
…ion-aware decoding

First Phase-B PR with mandatory Mac M4 integration test report
under \u00a79 (Linux CI is necessary but not sufficient).

Design choice: PR-B3 ships **greedy decoding only**. Speculative-
decoding integration (DLM proposer + AR verifier rejection sampling)
is reserved for a later PR. The wire contract is algorithm-agnostic
(GenerateResponse oneof on token_id / done / truncated), so the
upgrade path lands without breaking clients.

Three deliverables:

  1. inference_engine/session/generator.py (new, ~240 lines)
     - VerifierProtocol-driven GenerationCoordinator.
     - Yields TokenEvent / HistoryTruncatedEvent / DoneEvent.
     - Strict v0.3 validation: temperature/top_p/top_k must be in
       greedy no-op defaults; max_tokens must be >= 1; session must
       have prior AppendTokens. Per \u00a72.10 'no graceful degradation',
       any deviation raises ValueError -> INVALID_ARGUMENT.
     - HistoryTruncated emitted at most once per call, BEFORE any
       TokenEvent, when the cache is in sink+window-truncated state
       (matches the runtime.proto contract exactly).
     - INV-1 / INV-2 enforced via SessionStore at every step.
     - INV-3 byte-exact under greedy: same (session_id, history)
       -> bit-identical token stream. Tested with FakeVerifier on
       Linux and asserted in TestDeterminism.

  2. inference_engine/server/grpc_app.py (modified, +119 lines)
     - RuntimeServiceServicer.__init__ takes optional
       generation_coordinator: GenerationCoordinator. None = PR-B2
       UNIMPLEMENTED default preserved (regression-tested).
     - Generate RPC implements server-streaming with the four typed
       error mappings:
         SessionNotFoundError  -> NOT_FOUND
         ValueError            -> INVALID_ARGUMENT
         InvariantViolation    -> FAILED_PRECONDITION
       Plus the success path streams TokenEvent -> token_id,
       HistoryTruncatedEvent -> truncated, DoneEvent -> done.
     - Cancellation: polls context.cancelled() between events; on
       True, emits a final GenerateDone(STOP_REASON_CANCELLED) and
       returns. Cancellation latency is bounded by one generation
       step (the in-flight forward pass finishes before the next
       poll).
     - create_grpc_server factory plumbed with the new keyword.

  3. tests/inference_engine/session/test_generator.py (new, 31 tests)
     - TestGreedyHappyPath: 4 tests on token-then-done emission,
       max_tokens cap, single Done event, total_seconds reporting.
     - TestGreedyAdvancesVerifier: 4 tests confirming verifier
       state mirrored onto session after every step.
     - TestEos: 3 tests on EOS detection + STOP_REASON_EOS.
     - TestHistoryTruncated: 3 tests on at-start emission + at-most-
       once contract.
     - TestValidation: 9 tests on max_tokens, sampling params, and
       no-AppendTokens-prior rejection.
     - TestInvariants: 2 tests on INV-1 / INV-2 propagation.
     - TestDeterminism: 1 test on byte-exact greedy output across
       parallel sessions.
     - TestConstructorAndEventDataclasses: 4 tests on frozen dataclass
       contract and constructor.

  + tests/inference_engine/server/test_grpc_app.py (extended, +11 tests)
     - Generate streams tokens then done.
     - EOS triggers STOP_REASON_EOS.
     - HistoryTruncated frame emitted before tokens.
     - Unknown session -> NOT_FOUND.
     - No AppendTokens prior -> INVALID_ARGUMENT.
     - max_tokens=0 -> INVALID_ARGUMENT.
     - temperature=0.7 -> INVALID_ARGUMENT.
     - seed accepted on the wire.
     - InvariantViolation during generation -> FAILED_PRECONDITION.
     - Cancellation -> STOP_REASON_CANCELLED Done frame (direct
       Servicer invocation with FakeContext; cancellation latency
       bounded as documented).
     - Factory accepts generation_coordinator keyword.

Mac M4 reviewer aids:

  scripts/smoke_grpc_generator.py
    10-scenario smoke walking Generate scenarios with Append +
    Generate coordinators wired against FakeVerifier.

  scripts/review_pr_b3_on_mac.sh
    One-shot Mac M4 reviewer producing 5 JSON artifacts:
      pr-b3-mac-generator-tests-<unix>.json (31 tests, 100% on
      generator.py)
      pr-b3-mac-grpc-tests-<unix>.json (39 tests after PR-B3
      additions, 100% on grpc_app.py)
      pr-b3-mac-grpc-runtime-smoke-<unix>.json (PR-B1 regression)
      pr-b3-mac-grpc-appender-smoke-<unix>.json (PR-B2 regression)
      pr-b3-mac-grpc-generator-smoke-<unix>.json (PR-B3 new)

Local verification (Linux VM, py3.12):
  Linux CI gate: 629 passed (was 587 + 31 generator + 11 grpc
                 generate). Coverage 100.00% on 1521 stmts (was
                 1428 + 63 generator + 30 grpc_app additions
                 - 0 deletions).

Per ADR 0008 \u00a79: this PR introduces a NEW MLX-reachable runtime
path (verifier.forward_block + commit_or_truncate driven by the
coordinator's per-token loop). Even though FakeVerifier covers the
Linux-side dispatch logic exhaustively, the v0.3 GA contract
requires a Mac M4 integration test report on the PR branch before
merge. The carve-out paragraph does NOT apply here. PR is opened as
Draft pending the report.

Mac M4 verification command for the PR branch:
  bash scripts/review_pr_b3_on_mac.sh

The script writes 5 JSON artifacts under results/platform-tests/;
the user commits them back to this branch and the PR description is
updated to quote the smoke summary line.

Co-authored-by: FluffyAIcode <FluffyAIcode@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@cursor
cursor Bot marked this pull request as ready for review June 1, 2026 14:27
@cursor cursor Bot changed the title PR-B3 (ADR 0008 Phase B): Generate server-streaming RPC + greedy session-aware decoding [Draft, awaits Mac M4] PR-B3 (ADR 0008 Phase B): Generate server-streaming RPC + greedy session-aware decoding Jun 1, 2026
@FluffyAIcode
FluffyAIcode merged commit 6dd30dc into main Jun 1, 2026
5 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.

2 participants