Skip to content
This repository was archived by the owner on May 26, 2026. It is now read-only.

feat(kora): KR-PLUGIN-EXTRACTIONS-BATCH-2 — audit + caching + short-circuit + state-holders#188

Merged
rafe-walker merged 1 commit into
feature/phase2-upgradesfrom
feat/kora-KR-PLUGIN-EXTRACTIONS-BATCH-2
May 24, 2026
Merged

feat(kora): KR-PLUGIN-EXTRACTIONS-BATCH-2 — audit + caching + short-circuit + state-holders#188
rafe-walker merged 1 commit into
feature/phase2-upgradesfrom
feat/kora-KR-PLUGIN-EXTRACTIONS-BATCH-2

Conversation

@rafe-walker

Copy link
Copy Markdown
Owner

Summary

Four sub-plugin extractions in one PR — Deliverables A/B/C/D of the KR-PLUGIN-EXTRACTIONS-BATCH-2 bucket. Each follows the PR #185 KR-PLUGIN-COST-LADDER template (constants + <logic> + plugin file shape; backward-compat shim with identity-against-canonical drift guard).

After this lands: 5 of 7 plugin extractions complete (cost-ladder + audit + caching + short-circuit + state-holders). Remaining: KR-PLUGIN-IDENTITY (deferred per Lock R3-2) and KR-HAIKU-ROUTER-PLUGIN (waits on KR-HERMES-LOCAL-EXT-REISSUE).

Behavior: unchanged. No hook handler that wasn't firing before fires now; no wire-shape changes.

File-tree diagram (post-batch)

kora_cli/reasoning/kora_hermes_plugin/
├── __init__.py
├── plugin.py                                    orchestrator (3 resident hooks + 5 sub-registers)
├── cost_ladder/                              ← PR #185
│   ├── __init__.py / constants.py / selector.py / plugin.py
├── audit/                                    ← NEW (Deliverable A)
│   ├── __init__.py
│   ├── constants.py                            AUDIT_SEAMS vocabulary
│   ├── writer.py                               _emit_tool_called_audit (moved from anthropic_engine.py)
│   └── plugin.py                               post_tool_call + post_llm_call handlers
├── caching/                                  ← NEW (Deliverable B)
│   ├── __init__.py
│   ├── constants.py                            CACHE_CONTROL_EPHEMERAL
│   ├── markers.py                              _wrap_system/tools_as_cacheable (moved from anthropic_engine.py)
│   └── plugin.py                               standalone caching_hook (not registered; bundled with cost-ladder in v1)
├── short_circuit/                            ← NEW (Deliverable C)
│   ├── __init__.py
│   ├── constants.py                            BUNDLED_PHRASEBOOK_PACKAGE + filename + override path
│   ├── matcher.py                              full phrasebook surface (moved from kora_cli/short_circuit/dm_phrasebook.py)
│   └── plugin.py                               short_circuit_hook stub (not registered; handler owns call path in v1)
└── state_holders/                            ← NEW (Deliverable D)
    ├── __init__.py
    ├── registry.py                             STATE_HOLDER_ACCESSORS + holder_liveness probe
    └── plugin.py                               on_session_start handler

kora_cli/reasoning/anthropic_engine.py        ← shims: re-imports _emit_tool_called_audit + caching markers
kora_cli/short_circuit/dm_phrasebook.py       ← shim (333 → 62 lines) re-exports matcher's public surface
kora_cli/short_circuit/default_slack_dm_phrasebook.yml   ← unchanged location (phrasebook editor #177 compat)

tests/kora_cli/reasoning/kora_hermes_plugin/
├── audit/test_writer.py                         7 tests   (identity + writer end-to-end JSONL + register)
├── caching/test_markers.py                      8 tests   (identity + cross-dep cleanup proof + markers shape + standalone hook)
├── short_circuit/test_matcher.py              moved 1:1   (was tests/kora_cli/short_circuit/test_dm_phrasebook.py)
└── state_holders/test_plugin.py                 7 tests   (identity + handler gate + register + registry/liveness)

tests/kora_cli/short_circuit/test_dm_phrasebook.py    converted to 3-test shim-verification suite
tests/kora_cli/test_phrasebook_endpoints.py           updated to source-grep canonical matcher.py for drift guard

Per-extraction LOC migration

Deliverable Canonical files added (LOC) Source LOC moved Shim LOC at old location
A — Audit 242 (4 files: __init__ 36 + constants 25 + writer 93 + plugin 88) ~60 LOC of _emit_tool_called_audit moved out of anthropic_engine.py 8-line re-import in anthropic_engine.py
B — Caching 248 (4 files: __init__ 48 + constants 30 + markers 69 + plugin 101) ~43 LOC of _wrap_system/tools_as_cacheable moved out of anthropic_engine.py 10-line re-import in anthropic_engine.py
C — Short-circuit 547 (4 files: __init__ 50 + constants 31 + matcher 346 + plugin 120) ~333 LOC moved out of kora_cli/short_circuit/dm_phrasebook.py 62-line re-export shim at dm_phrasebook.py
D — State-holders 174 (3 files: __init__ 33 + registry 75 + plugin 66) ~13 LOC _on_session_start moved out of kora_hermes_plugin/plugin.py Re-export at orchestrator (4 lines)

Total: 30 files changed, +2417 / -927.

Cross-dep cleanup confirmation

Deliverable B's stated value: cost-ladder plugin no longer imports from anthropic_engine.py.

Before this PR, cost_ladder/plugin.py:135-138 had:

from kora_cli.reasoning.anthropic_engine import (
    _wrap_system_as_cacheable,
    _wrap_tools_as_cacheable,
)

After this PR (cost_ladder/plugin.py:135-141):

# KR-PLUGIN-EXTRACTIONS-BATCH-2 (Deliverable B) — markers now
# live at the canonical caching sub-plugin location, NOT in
# anthropic_engine.py. Cleans the v1 cross-dep.
from kora_cli.reasoning.kora_hermes_plugin.caching.markers import (
    _wrap_system_as_cacheable,
    _wrap_tools_as_cacheable,
)

Verified by grep:

$ grep -n "anthropic_engine" kora_cli/reasoning/kora_hermes_plugin/cost_ladder/plugin.py
(no matches)

The cross-dep cleanup is also asserted at runtime by tests/kora_cli/reasoning/kora_hermes_plugin/caching/test_markers.py::test_cost_ladder_plugin_imports_markers_from_canonical_location — it patches the canonical marker location and confirms the cost-ladder hook honors the patched version (would fail if the hook still imported from the engine).

Hook ownership decisions (no-ops by design)

Two of the new sub-plugins have intentional no-op register() functions:

  • caching/plugin.register(): would otherwise double-fire the cache marker wrap that the bundled cost-ladder hook already does. Standalone caching_hook exists at the canonical location for a future split.
  • short_circuit/plugin.register(): would otherwise double-fire the matcher that slack_dm_handler.py already calls directly. Standalone short_circuit_hook stub exists for a future transform_input wiring.

Both no-ops are explicitly tested (test_register_is_intentional_noop for caching) and documented in their module docstrings — the canonical boundary + future-ready API is the value extraction.

STOP-ASK conditions encountered

None — no cross-dependency surprises, no circular-import breaks, no phrasebook-editor regression. The bundled YAML stayed at kora_cli/short_circuit/default_slack_dm_phrasebook.yml so the editor (PR #177) continues reading/writing through the same importlib.resources.files("kora_cli.short_circuit") path.

Test plan

  • Deliverable A: 7 audit tests pass at canonical location (identity-against-engine-shim, orchestrator re-export, discovery shim re-export, end-to-end JSONL emit through canonical sink, dual-write log line preservation, register wires 2 hooks, handler gate)
  • Deliverable B: 8 caching tests pass at canonical location (identity-against-engine-shim, cross-dep cleanup proof via patched canonical, marker shape, last-tool-only, empty input, intentional-no-op register, standalone caching_hook end-to-end)
  • Deliverable C: phrasebook tests moved 1:1 (canonical imports); 3-test shim-verification at old location; phrasebook-endpoints drift guard updated to source-grep canonical matcher
  • Deliverable D: 7 state-holder tests pass (orchestrator + discovery shim identity, handler gate, register wires on_session_start, accessors registry shape, holder_liveness boolean contract)
  • 67 existing test_kora_hermes_plugin{,_st2,_st2b}.py tests stay green — discovery shim re-exports + orchestrator re-exports preserved every name they import
  • test_anthropic_engine_caching.py + test_jsonl_sink.py stay green — shim ensures the engine's existing use sites resolve the same callables
  • test_slack_dm_short_circuit.py + test_phrasebook_editor.py + test_phrasebook_endpoints.py stay green — phrasebook shim + drift-guard source-path update
  • Full xdist regression: 337/337 directly-affected tests passing; remaining failures pre-exist on base branch and grep-confirmed to not reference any module changed here
  • Manual smoke after merge: slack DM through a kora-runtime deployment to confirm short-circuit reply path + cost-ladder + caching wrap all still fire end-to-end

Recommendation for next dispatch

Batched pair: KR-HERMES-LOCAL-EXT-REISSUE + KR-HAIKU-ROUTER-PLUGIN. They're tightly coupled — REISSUE adds the Hermes-side hook surface needed for the haiku-router post-call escalation, and HAIKU-ROUTER-PLUGIN consumes that surface to wire should_escalate_post_call (which exists at cost_ladder/selector.py since PR #185 but has no caller). Once both land, KR-HAIKU-ROUTER-PLUGIN becomes the 6th of 7 extractions.

KR-PLUGIN-IDENTITY remains deferred per Lock R3-2; that's the 7th and final extraction.

🤖 Generated with Claude Code

…ircuit + state-holders

Four sub-plugin extractions in one PR, following the PR #185
KR-PLUGIN-COST-LADDER template (constants + <logic> + plugin
file shape; backward-compat shim with identity-against-canonical
drift guard).

# Canonical locations created

  kora_cli/reasoning/kora_hermes_plugin/
    audit/         — Deliverable A; _emit_tool_called_audit
                      writer (moved from anthropic_engine);
                      post_tool_call + post_llm_call handlers
    caching/       — Deliverable B; _wrap_system/tools_as_cacheable
                      markers (moved from anthropic_engine);
                      standalone caching_hook (not registered;
                      bundled with cost-ladder hook in v1)
    short_circuit/ — Deliverable C; PhrasebookEntry +
                      ShortCircuitMatch + load_phrasebook +
                      match_message + render_reply +
                      try_short_circuit (moved from
                      kora_cli/short_circuit/dm_phrasebook.py)
    state_holders/ — Deliverable D; on_session_start handler +
                      STATE_HOLDER_ACCESSORS registry +
                      holder_liveness probe

# Backward-compat shims

  kora_cli/reasoning/anthropic_engine.py
    Re-imports _emit_tool_called_audit (Deliverable A) +
    _wrap_system/tools_as_cacheable (Deliverable B) from their
    canonical sub-plugin locations. The 3 in-file callers of
    the audit helper + the engine's _make_request_kwargs caching
    wrap keep resolving the symbols.

  kora_cli/short_circuit/dm_phrasebook.py (333 → 62 lines)
    Re-exports the full public surface from the canonical
    matcher module. slack_dm_handler + web_server + phrasebook
    editor (PR #177) keep working unchanged. Bundled YAML
    phrasebook stays at default_slack_dm_phrasebook.yml in this
    package — phrasebook editor reads/writes it through
    importlib.resources.files("kora_cli.short_circuit").

# Cross-dep cleanup (Deliverable B's stated value)

The cost-ladder plugin's hook handler (added in PR #185)
previously imported the caching wrap helpers from
kora_cli.reasoning.anthropic_engine. This PR cuts that
cross-dep — cost_ladder/plugin.py now imports
_wrap_system/tools_as_cacheable from
kora_hermes_plugin.caching.markers (the canonical sub-plugin
location). Verified by grep: zero remaining anthropic_engine
imports in cost_ladder/plugin.py.

# Orchestrator topology (post-batch)

  KoraHermesPlugin.register walks 5 sub-registers in order
  (cost_ladder, audit, caching, short_circuit, state_holders)
  then attaches 3 remaining orchestrator-resident handlers
  (pre_tool_list_finalized + pre_tool_call + the ST2B tool
  bridge). 7 total hooks wired against KORA_ROUTES.

# Tests

  tests/kora_cli/reasoning/kora_hermes_plugin/audit/test_writer.py
    7 tests: identity-against-canonical (engine shim,
    orchestrator re-export, discovery shim re-export) + writer
    end-to-end JSONL emit + dual-write log preservation +
    register-wires-2-hooks + handler-noop-on-non-Kora-routes.

  tests/kora_cli/reasoning/kora_hermes_plugin/caching/test_markers.py
    8 tests: engine shim identity + cost-ladder hook proves it
    imports from canonical location (via patched-canonical
    sentinel) + marker shape + last-tool-only + empty input +
    register-is-noop + standalone caching_hook end-to-end.

  tests/kora_cli/reasoning/kora_hermes_plugin/short_circuit/test_matcher.py
    Moved 1:1 from tests/kora_cli/short_circuit/test_dm_
    phrasebook.py with import path updated to canonical.

  tests/kora_cli/reasoning/kora_hermes_plugin/state_holders/test_plugin.py
    7 tests: orchestrator + discovery shim identity + handler
    no-op gate + register-wires-on-session-start + accessors-
    registry-shape + holder_liveness-returns-bool-per-accessor.

  tests/kora_cli/short_circuit/test_dm_phrasebook.py
    Converted to 3-test shim-verification suite (identity-
    against-canonical for public + private re-exports +
    package-level resolves + canonical-module attribution).

  tests/kora_cli/test_phrasebook_endpoints.py
    test_placeholder_regex_matches_dm_phrasebook_source updated
    to source-grep the canonical matcher.py location (the
    regex moved with the module; the drift-guard intent
    preserved).

# Scoped regression (xdist)

  tests/kora_cli/reasoning/kora_hermes_plugin/   passing
  tests/kora_cli/short_circuit/                  passing
  tests/kora_cli/reasoning/test_anthropic_engine_caching.py  passing
  tests/kora_cli/audit/test_jsonl_sink.py        passing
  tests/plugins/test_kora_hermes_plugin*.py      passing
  tests/kora_cli/handlers/test_slack_dm_short_circuit.py  passing
  tests/kora_cli/test_phrasebook_{editor,endpoints}.py  passing
  tests/kora_cli/reasoning/test_anthropic_engine_router.py  passing
  tests/kora_cli/snapshot/test_state_snapshot.py passing
  Total directly-affected surface: 337/337 passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@rafe-walker rafe-walker merged commit a1ad439 into feature/phase2-upgrades May 24, 2026
@rafe-walker rafe-walker deleted the feat/kora-KR-PLUGIN-EXTRACTIONS-BATCH-2 branch May 24, 2026 05:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant