Skip to content

feat(probes): instrument inference seam + prompt assembly (#206, #195) - #1545

Merged
joelteply merged 1 commit into
canaryfrom
probes/inference-and-prompt-assembly
Jun 7, 2026
Merged

joelteply merged 1 commit into
canaryfrom
probes/inference-and-prompt-assembly

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

Iteration 1 of the inference-latency campaign (#195). With #196's on_close fix already merged, time_sync! / time_probe! spans persist to the JSONL sink end-to-end — so the timing data this commit produces is now captureable for the first time.

What's instrumented

inference/llamacpp_adapter.rs::generate_text

The dominant cost on LCD tier (95%+ wall-clock per 2026-06-06 baseline). Until this PR the function was a black box from the operator's POV — tok_per_sec lived in a private RwLock<f64> (last-throughput-only).

  • inference.generate.enter — request fingerprint at entry (model, persona_id, msg_count, max_tokens, has_system_prompt, parts_image, parts_audio)
  • time_sync!("inference.render_chat", ...) — chat-template rendering (sub-ms typically, but cumulative)
  • time_probe!("inference.forward.text", ...) — pure-text scheduler-managed forward pass
  • time_probe!("inference.forward.multimodal", ...) — mtmd single-flight path (image / audio)
  • inference.generate.exit — carries the headline metric tok_per_sec plus duration_ms, tokens_out, text_len, model. A jq filter on this class IS the latency dashboard

persona/prompt_assembly.rs::assemble

The leading indicator for "why is prefill slow." Carries system_message_len, message_count, estimated_tokens, matched_angle_present, engrams_count, social_signals_present, voice_mode, multi_party_strategy.

Manual update

docs/architecture/RTOS-DEBUGGER-PROBES.md:

  • Added the new classes to the taxonomy (with full field lists)
  • Marked the prompt-assembly + llamacpp-adapter checklist items DONE per the doc's "When you add a probe, update this manual" rule

Doctrine alignment

  • [[jtag-probes-are-rtos-debugger]] — every site names surrounding vars the way a breakpoint inspector would. One-liners; macros do the plumbing.
  • [[no-rust-gates-around-cognition]] — probes observe, they DO NOT decide. Zero control-flow change. The existing runtime::logger + last_throughput_tok_s paths are untouched.
  • [[init-once-handle-then-lease-zero-copy-refs]] — macros expand to tracing::event! / info_span! which inherit release_max_level_* compile-time gates. Zero cost when off.

Validation

  • cargo check --features metal,accelerate — clean
  • cargo test --lib persona::prompt_assembly — 12/12 pass
  • cargo test --lib inference::llamacpp — 12/12 pass
  • 24/24 green across the two modules this PR touches

Next iteration (separate slice)

Run a real continuum boot with CONTINUUM_PROBE_FILE=/tmp/continuum-probes.jsonl + CONTINUUM_PROBE_CLASSES=inference,persona.prompt,timing. Exercise the persona service loop. jq the JSONL to identify the dominant bottleneck. Optimize that. Iterate.

Reference: substrate task #206 (this PR), task #195 (the campaign).

🤖 Generated with Claude Code

Iteration 1 of the inference-latency campaign (#195). Sprinkles
probes at the load-bearing seams the substrate's RTOS debugger
manual marked unchecked. With #196's on_close fix already on
canary, `time_sync!` / `time_probe!` spans now persist to the
JSONL sink — the timing data this commit produces is captureable
end-to-end for the first time.

## What's instrumented

**`inference/llamacpp_adapter.rs::generate_text`** — the dominant
cost on LCD tier (95%+ wall-clock per 2026-06-06 baseline). The
function was effectively a black box from the operator's POV: the
existing `runtime::logger("llamacpp")` lines describe shape but
not duration, and `tok_per_sec` was kept in a private
`RwLock<f64>` (last-throughput-only). Probes added:

- `inference.generate.enter` — request fingerprint at entry
  (model, persona_id, msg_count, max_tokens, has_system_prompt,
  parts_image, parts_audio). Pairs with `.exit` via span ancestry.
- `time_sync!("inference.render_chat", ...)` — chat-template
  rendering. Synchronous + small, but cumulative across many
  turns. Bracketing it lets the operator subtract it from
  `forward.*` cleanly.
- `time_probe!("inference.forward.text", ...)` — pure-text
  scheduler-managed path. The actual LLM decode.
- `time_probe!("inference.forward.multimodal", ...)` — mtmd path
  (image / audio). Distinct seam because it bypasses the
  scheduler and runs single-flight.
- `inference.generate.exit` — pairs with `.enter`. Carries the
  campaign's headline metric `tok_per_sec` plus duration_ms,
  tokens_out, text_len, model. A `jq` filter on
  `class == "inference.generate.exit"` is the latency dashboard
  in JSONL form.

**`persona/prompt_assembly.rs::assemble`** — the leading
indicator for "why is prefill slow." When engrams / social
signals / matched-angle grow unbounded, `system_message_len`
shadows tok/s in the timing breakdown. Probe at the function
tail carries the composition shape: system_message_len,
message_count, estimated_tokens, matched_angle_present,
engrams_count, social_signals_present, voice_mode,
multi_party_strategy.

## Doctrine alignment

Per [[jtag-probes-are-rtos-debugger]] (Joel 2026-06-06): every
probe site names the surrounding vars the way a breakpoint
inspector would show locals. Easy one-liners; the macros do the
plumbing. `class` strings follow the canonical taxonomy in
`docs/architecture/RTOS-DEBUGGER-PROBES.md` (updated in this
commit per the "When you add a probe, update this manual" rule).

Per [[no-rust-gates-around-cognition]]: probes observe, they
DO NOT decide. None of these emit changes control flow. The
existing `runtime::logger` and `last_throughput_tok_s` paths
remain untouched — probes are additive.

Per [[init-once-handle-then-lease-zero-copy-refs]]: the macros
expand to `tracing::event!` / `tracing::info_span!` calls that
inherit `tracing`'s `release_max_level_*` compile-time gates.
Zero cost when off; auditable per task #198 if a hot loop
later needs the visitor allocation reviewed.

## Manual update

`docs/architecture/RTOS-DEBUGGER-PROBES.md`:
- Added the new classes to the taxonomy (`persona.prompt.assemble`
  with full field list; `inference.generate.{enter,exit}`; the
  three new `timing` seams).
- Marked the prompt-assembly checklist item DONE.
- Marked the llamacpp-adapter checklist item DONE with the
  specific call-site list and the campaign cross-reference.

## Validation

- `cargo check --features metal,accelerate` — clean
- `cargo test --lib persona::prompt_assembly` — 12/12 pass
- `cargo test --lib inference::llamacpp` — 12/12 pass
- 24/24 green across the two affected modules

## Next iteration

Iteration 2 (separate slice): run a real continuum boot with
CONTINUUM_PROBE_FILE set, exercise the persona service loop
against the multi-persona stress fixture (#1518's baseline),
`jq` the JSONL to identify the dominant bottleneck. Optimize
THAT. Iterate. Until tok_per_sec on the LCD tier hits the
M5-class target.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@joelteply
joelteply merged commit 29d901a into canary Jun 7, 2026
3 checks passed
@joelteply
joelteply deleted the probes/inference-and-prompt-assembly branch June 7, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant