Skip to content

hooks: fix PR #110 review — steering via SystemPrompt + wire OnError - #111

Merged
FMXExpress merged 3 commits into
claude/hooks-and-steeringfrom
claude/pr110-review-fixes
Jun 1, 2026
Merged

FMXExpress merged 3 commits into
claude/hooks-and-steeringfrom
claude/pr110-review-fixes

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Stacks on PR #110. Targets claude/hooks-and-steering so the diff is just the review fixes; once #110 merges, GitHub will auto-retarget to main.

Summary

Two Codex findings on PR #110.

P1 — Steering messages invisible under the default component config

PR #110 appended each batch's SteeringMessages as an mrSystem entry in Hist before the next LLM round. But the OpenAI / Anthropic / Gemini provider builders explicitly drop in-history mrSystem entries whenever ChatOptions.SystemPrompt is non-empty — they ship one consolidated system prompt via that slot, not via the messages array. TPasClawAgent.ChatHistory always populates Cfg.Options.SystemPrompt via BuildSystemPrompt, so the default component path always hit this case. Steering was silently dropped before reaching the model.

Fix: fold BatchSteering into LiveOptions.SystemPrompt (which does get shipped to the model on the next Provider.Chat) instead of appending an mrSystem to Hist. Two newlines as a separator preserves the original BuildSystemPrompt content's formatting.

Side effect: steering accumulates across iterations through end of loop — the picoclaw semantic (each new tool result builds on prior steering). Embedders wanting ephemeral per-batch steering can reset SystemPrompt in BeforeTurn.

P2 — HooksOnError dispatcher declared but never invoked

The helper existed in PasClaw.Agent.Hooks; nothing in RunToolLoop called it. None of the documented stages could reach an embedder's audit hook.

Fix: wired HooksOnError into two failure paths:

Stage Trigger
hsProviderCall After the fallback walk, if the final response carries non-2xx (every fallback failed, or a non-retryable 4xx came back).
hsDuringToolCall Per batch slot in phase 3, if Dispatches[j].Err is non-empty after dispatch. Covers both PreflightToolCall rejections and worker-caught handler exceptions.

The other stages (hsBeforeTurn, hsBeforeToolCall, hsAfterToolResult) currently have no failure modes RunToolLoop catches — hook methods that raise propagate up since hooks run on the main thread. Those stages stay in the API for future use.

Verification

Three-part end-to-end test (built + run + removed):

=== Test 1: steering folds into SystemPrompt ===
  PASS — steering in SystemPrompt, no stray mrSystem
=== Test 2a: hsDuringToolCall fires ===
  PASS — fired 1 time(s), last msg: tool "fail_tool": simulated tool failure
=== Test 2b: hsProviderCall fires ===
  PASS — fired 1 time(s), last msg: provider returned status=502

all PR #110 review-fix tests passed.

Test 1 asserts the steering text appears in Loop.FinalSystemPrompt and that no mrSystem message with the steering text leaked into Loop.FinalMessages. Tests 2a/2b register an audit hook + a deliberately-failing tool / provider and check the audit hook recorded the right stage + message.

make smoke 11/11 green.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

Two Codex P-level findings on PR #110, both legitimate.

P1 — Steering messages invisible under the default component
configuration. PR #110 appended each batch's accumulated
SteeringMessages as an mrSystem entry in Hist before the next
LLM round. But the OpenAI / Anthropic / Gemini provider builders
explicitly DROP in-history mrSystem entries whenever the
ChatOptions.SystemPrompt slot is non-empty — they ship one
consolidated system prompt via that slot, not via the messages
array. TPasClawAgent.ChatHistory always populates
Cfg.Options.SystemPrompt via BuildSystemPrompt, so the default
component path always hits this case. Steering was silently
dropped before reaching the model.

Fix: fold BatchSteering into LiveOptions.SystemPrompt (which
DOES get shipped to the model on the next provider.Chat call)
instead of appending an mrSystem to Hist. Two newlines as a
separator preserves the original BuildSystemPrompt content's
formatting. Side effect: steering accumulates across iterations
through to end of loop — the picoclaw semantic (each new tool
result builds on prior steering). Embedders that want
ephemeral per-batch steering can reset SystemPrompt in
BeforeTurn.

P2 — HooksOnError dispatcher declared but never invoked. The
helper existed in PasClaw.Agent.Hooks; nothing in RunToolLoop
called it. None of the documented stages (hsBeforeTurn /
hsBeforeToolCall / hsDuringToolCall / hsAfterToolResult /
hsProviderCall) could reach an embedder's audit hook.

Fix: wired HooksOnError into two failure paths in
RunToolLoop:

  hsProviderCall   After the fallback walk completes, if the
                   final response carries a non-2xx status code
                   (every fallback also failed, or a primary 4xx
                   we don't retry on like 400/401/403/404 came
                   back), fire OnError with the status. Loop
                   continues so OnText / the legacy event path
                   still get a chance.

  hsDuringToolCall Per batch slot in phase 3, if
                   Dispatches[j].Err is non-empty after dispatch,
                   fire OnError with "tool '<name>': <err>". Covers
                   both PreflightToolCall rejections and
                   worker-caught handler exceptions.

The other stages (hsBeforeTurn, hsBeforeToolCall,
hsAfterToolResult) currently have no failure modes RunToolLoop
catches — hook methods that raise propagate up since hooks run
on the main thread. Those stages stay in the API for future
use without requiring callers to enum-match against a smaller
set today.

Verified end-to-end with a focused three-part test (built + run
+ removed, not committed):

  === Test 1: steering folds into SystemPrompt ===
    PASS — steering in SystemPrompt, no stray mrSystem
  === Test 2a: hsDuringToolCall fires ===
    PASS — fired 1 time(s), last msg: tool "fail_tool": simulated tool failure
  === Test 2b: hsProviderCall fires ===
    PASS — fired 1 time(s), last msg: provider returned status=502

  all PR #110 review-fix tests passed.

make smoke 11/11 green.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 438201924e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +533 to +534
if (Length(Cfg.Hooks) > 0) and
((Resp.StatusCode > 0) and ((Resp.StatusCode < 200) or (Resp.StatusCode >= 300))) then

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Report non-HTTP provider failures to hooks

When the primary provider and every fallback fail before receiving an HTTP response, the final status is negative and this guard skips HooksOnError. The shipped HTTP helper explicitly returns StatusCode := -1 for failures such as unavailable TLS support (PasClaw.Providers.HTTP.pas:204), and the loop already treats every StatusCode <= 0 as a retryable provider failure in IsRetryableStatus. As a result, an embedder's audit or alerting hook remains silent for DNS/TLS/socket-style outages after the fallback walk is exhausted. Include negative statuses in the hsProviderCall reporting path while preserving the intended handling for legacy unset statuses if needed.

Useful? React with 👍 / 👎.

Codex P2 on PR #111: the OnError(hsProviderCall) dispatcher
gated on `Resp.StatusCode > 0`, so DNS / TLS / socket /
no-OpenSSL failures (the HTTP helper flags those as StatusCode
:= -1) silently slipped past the audit-hook path. Exactly the
outage modes an embedder running unattended would want to alert
on.

Fix: drop the StatusCode > 0 guard. The condition is now
"anything that isn't 2xx" — covers -1 (network/TLS), 0 (pre-HTTP
unclassified failure), 4xx (auth/bad-request), and 5xx (server)
in one check.

  before:
    if (Resp.StatusCode > 0) and
       ((Resp.StatusCode < 200) or (Resp.StatusCode >= 300)) then

  after:
    if (Resp.StatusCode < 200) or (Resp.StatusCode >= 300) then

Verified with a focused probe test (built + run + removed,
not committed) — fires OnError on the right stages for status
codes {-1, 0, 429, 502, 401}:

  --- probe status=-1  ---  PASS — provider returned status=-1
  --- probe status=0   ---  PASS — provider returned status=0
  --- probe status=429 ---  PASS — provider returned status=429
  --- probe status=502 ---  PASS — provider returned status=502
  --- probe status=401 ---  PASS — provider returned status=401

  PASS — all non-2xx statuses now reach OnError hooks.

make clean && make smoke 11/11 green.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
hooks: fix PR #111 review — OnError fires for status ≤ 0 too
@FMXExpress
FMXExpress merged commit a795363 into claude/hooks-and-steering Jun 1, 2026
FMXExpress added a commit that referenced this pull request Jun 1, 2026
hooks: recover PR #111 + #112 review fixes onto main
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