diff --git a/repl.lua b/repl.lua index 23508f8..311965b 100644 --- a/repl.lua +++ b/repl.lua @@ -1286,6 +1286,11 @@ function M.run(config) and secrets.streaming_rehydrator(secrets_session) or nil end, + -- Phase 7: hand the central usage chokepoint to safety.lua. + -- safety.norris_step routes the Norris main broker's usage + -- here under category="norris"; safety.is_destructive's LLM + -- probe routes via opts.on_usage under category="probe". + on_usage = _record_usage, } local step_n = 1 @@ -1726,15 +1731,17 @@ function M.run(config) -- :safety check --no-llm if added in v2. -- Issue #52: thread secrets scrub/rehydrate so the probe -- model sees placeholders for any secrets in `cmd`. - local probe_opts + -- Phase 7: also thread on_usage so the probe's cost + -- lands in the accumulator under category="probe". + local probe_opts = { on_usage = _record_usage } if secrets_session then - probe_opts = { - scrub_msgs = function(msgs, mode_cfg) - return scrub_messages(msgs, - secrets_mode_for(mode_cfg or active_cfg)) - end, - rehydrate = function(t) return secrets_session:rehydrate(t) end, - } + probe_opts.scrub_msgs = function(msgs, mode_cfg) + return scrub_messages(msgs, + secrets_mode_for(mode_cfg or active_cfg)) + end + probe_opts.rehydrate = function(t) + return secrets_session:rehydrate(t) + end end local hit, reason = safety.is_destructive(cmd, config, probe_opts) if hit then diff --git a/safety.lua b/safety.lua index 4a5f253..4076c25 100644 --- a/safety.lua +++ b/safety.lua @@ -188,11 +188,21 @@ local function llm_probe(model_cfg, system, cmd, opts) if opts and opts.scrub_msgs then msgs = opts.scrub_msgs(msgs, model_cfg) end - local reply, err = broker.chat(model_cfg, msgs, - { max_tokens = 4, timeout_ms = PROBE_TIMEOUT_MS }) + -- Phase 7: opts.category = "probe" tags the usage in the + -- accumulator so :cost detail surfaces probe spend separately. + -- broker.chat returns (text, usage) on success; capture as + -- (reply, second) and branch on reply nil-ness. + local reply, second = broker.chat(model_cfg, msgs, + { max_tokens = 4, timeout_ms = PROBE_TIMEOUT_MS, category = "probe" }) if not reply then -- Broker failure → safe default: treat as YES (destructive) - return "YES_FAILSAFE", err + return "YES_FAILSAFE", second + end + -- Phase 7 (N4): route the usage payload through opts.on_usage if + -- the caller wired one (repl.lua's _record_usage when secrets/ + -- cost are configured). + if second and opts and opts.on_usage then + opts.on_usage(second.model, second.category, second) end if opts and opts.rehydrate then reply = opts.rehydrate(reply) end local upper = reply:upper() @@ -344,9 +354,17 @@ function M.norris_step(ctx, model_cfg, helpers, opts) local msgs = ctx:to_messages() if helpers.scrub_msgs then msgs = helpers.scrub_msgs(msgs, model_cfg) end local rehydrator = helpers.streaming_rehydrator and helpers.streaming_rehydrator() or nil + -- Phase 7: thread on_usage callback into the LLM probe via + -- probe_opts so destructive-check costs land in the accumulator + -- under the "probe" category. helpers.on_usage is repl.lua's + -- _record_usage (the central chokepoint with warn-threshold check). local probe_opts = nil - if helpers.scrub_msgs or helpers.rehydrate then - probe_opts = { scrub_msgs = helpers.scrub_msgs, rehydrate = helpers.rehydrate } + if helpers.scrub_msgs or helpers.rehydrate or helpers.on_usage then + probe_opts = { + scrub_msgs = helpers.scrub_msgs, + rehydrate = helpers.rehydrate, + on_usage = helpers.on_usage, + } end local text_parts = {} @@ -361,9 +379,16 @@ function M.norris_step(ctx, model_cfg, helpers, opts) end elseif kind == "tool_call" then tool_calls_seen[#tool_calls_seen + 1] = payload + elseif kind == "usage" then + -- Phase 7: route Norris's own broker usage to the + -- accumulator via helpers.on_usage. R5 chokepoint + -- (_record_usage) is what's wired in. + if helpers.on_usage then + helpers.on_usage(payload.model, payload.category, payload) + end end end, - { tools = helpers.tools_schema() }) + { tools = helpers.tools_schema(), category = "norris" }) if rehydrator then local tail = rehydrator:flush() if tail ~= "" then