Repository navigation
Context breakdown in the web UI; provider errors are not replies; MCP tools no longer hidden; rebuild same-second edits - #636
Conversation
…piled FPC decides a unit is up to date by comparing its source's time with what the .ppu recorded, to the second. A source rewritten in the same second its unit was compiled -- an edit straight after a build, a checkout, a tool that edits and rebuilds in one go -- looks unchanged, and the build links the old code without a word. It bit the revert checks for #635: a restored provider unit stayed compiled from the reverted version, and later builds kept failing on a change no longer in the source. scripts/sweep-stale-units.sh runs once per make invocation, before any recipe: it deletes the .ppu/.o of every source under src changed since the previous run started (find -newer a marker file compares to the nanosecond, GNU and BSD find alike), then moves the marker to now. A unit FPC would have rebuilt anyway costs nothing extra; one it would have kept is the bug. The Makefile already gives $(BIN) every source as a prerequisite for the same reason (a stale binary once reached a benchmark run); this closes FPC's own gap. test-stale-units (in make test) pins it with fixed timestamps, the same on every run: a unit compiled from a source stamped 00:00:00.5, the source rewritten at 00:00:00.9 after the run's marker at 00:00:00.2. FPC alone keeps the old code; after the sweep the edit is built; a source older than the marker keeps its unit. With a sweep that does nothing the test fails. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 85dafaf5c1
ℹ️ 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".
| done | ||
| done | ||
| fi | ||
| touch "$mark" |
There was a problem hiding this comment.
Capture the next marker timestamp before scanning sources
If a source is edited after find has examined it but before this final touch, the edit is neither swept now nor eligible for the next sweep: its timestamp is already older than the new marker. For a same-second edit that FPC also misses, this permanently preserves the stale compiled unit—the failure this change is intended to prevent. I reproduced the missed invalidation across two successive sweeps by editing a source immediately after the scan. Create a separate marker before scanning, then publish that marker after the sweep so edits made during traversal remain eligible on the next invocation.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8dbda16. The next marker is now touched before the scan, under a per-process name (.sweep-mark.$$), and renamed into place after it. An edit made while find is walking is therefore newer than the published marker, and the next run sweeps it. Two concurrent makes never publish each other's marker. A later publish can only move the marker back, which sweeps more.
test-stale-units now reproduces your case on every run. A find shim runs the real find and then edits the source, and the next sweep must remove the unit. The test fails on the old script and passes now.
Generated by Claude Code
The MCP bridge builds its TTool on the stack and registers it through RegisterDeferred, the one Register* that did not reset Hidden. Hidden is the back-compat-alias flag: ToProviderDefs skips the tool for good. So leftover stack bytes decided whether an MCP tool reached the model. With progressive disclosure off it was simply absent; with it on, tool_search listed and revealed it and the schema still never went out. RegisterDeferred now clears Hidden like every other path, and the bridge zeroes its record first (Category too, which now reliably means "treat as mutating", the documented default). Found by the context breakdown's e2e test: a stub server's one tool was in neither the provider tool list nor the deferred list. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
What the next chat turn puts in front of the model, part by part, like Claude Code's /context. - POST /v1/context takes the /v1/chat/completions body and assembles it with the chat route's own code: ChatRequestMessages and ComposeChatSystem are now shared by both routes, plus the stored-turn merge, the orphaned-call repair, the registry's tool list and the turn clock. Nothing is sent to a model. - BuildSystemPrompt is now BuildSystemPromptSections joined, so the report names each section of the real prompt (byte-identical output in every mode, checked against the old code). - PasClaw.Agent.ContextUsage turns that into categories (system prompt, memory, project rules, skills, built-in tools, MCP tools, messages), per-section and per-tool sizes, the largest messages, and the compaction state. counted_tokens comes from CompactMeasure, now the function NeedsCompact itself uses. - New config key context_window (display only); without it the bar is drawn against the compaction threshold. - Web UI: a toolbar button with the running total, /context, and a panel with a segmented bar, a legend of tokens and shares, and expandable sections, tools and messages. Palette validated for the dark surface and for the inverted light theme. Tests: context_usage_tests (unit) and context_usage_e2e.py, which runs a real gateway with a relay model and a stub MCP server and checks the report against the request the model actually receives. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
The marker was touched after the scan. A source edited while find was walking -- after it had looked at that file -- was then older than the new marker, so no later sweep would ever take its unit: for a same-second edit FPC also misses, the stale unit stayed for good (Codex on #636). The next marker is now touched before the scan under a per-process name and renamed into place after it, so an edit made during the walk is newer than the published marker and the next run sweeps it. Two concurrent makes never publish each other's half-done marker; the later publish can only move the marker back, which sweeps more, not less. test-stale-units gains the case: a find shim runs the real find and then edits the source, every run. The next sweep must take the unit. It fails on the old script and passes now (5 runs). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
When the provider call failed, /v1/chat/completions handed the provider's
error text back as the answer: HTTP 200, finish_reason "stop", the text
as content ("anthropic error: status=-1 msg=Connection Closed
Gracefully."). PersistGatewaySession saved it as an assistant message --
its guard assumed a hard failure has no text, and it has. So the web UI
kept it in its history, a session_context client got it back from the
store, and on the next turn the model was told it had said it.
- Gateway, /v1/chat/completions: a refusal (ProviderRefused) is HTTP 502
{"error": {message, type: provider_error, upstream_status}}, or with
stream:true an OpenAI error frame then [DONE].
- PersistGatewaySession (every route that saves a named session): the
turn's history is saved, the error is not appended as a reply.
- Web UI: an error frame or a non-2xx is recorded as an "e" turn --
shown, with a retry action, never sent to the model and never saved.
Tool activity that streamed before the failure stays as the partial
reply it was; retry drops back to the user's message.
Test: provider_error_e2e.py runs the gateway against a fake provider
that refuses with 401 and then recovers: 502 / error frame with the
provider's message, no error text as content, nothing saved as a reply,
and the next session_context turn sends the question without the error.
Each of the three gateway changes, reverted alone, fails it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
This PR now carries four changes. Parts 1, 2 and 4 were added here on request, while this PR was still open.
1. Context breakdown for the web UI (
/v1/context+ the Context panel)This is the equivalent of Claude Code's
/context: what the next chat turn puts in front of the model, part by part.Web UI
6.5k · 8.1%./context, opens a panel with:Gateway:
POST /v1/context/v1/chat/completionsbody (model,mode,messages,system,no_tools,session_context+X-PasClaw-Session) and assembles it with the chat route's own code. Nothing is sent to a model.ChatRequestMessagesandComposeChatSystemare now shared by both routes.ToProviderDefsand turn clock (HistWithTurnClock).BuildSystemPromptis nowBuildSystemPromptSectionsjoined, so the report names each section of the real prompt. The output is byte-identical to the old code in every mode, with and without tools, memory, AGENTS.md and caller additions; I checked this against a build of the previous unit.compaction.counted_tokenscomes fromCompactMeasure, whichNeedsCompactnow uses itself, so it is the trigger's own number.context_window(display only, default 0).text: trueadds each section's text.The web UI's chat request body is now built by one function (
chatRequestBody) used by both the chat and the panel.2. A provider refusal is an error, not the assistant's reply
When the provider call failed,
/v1/chat/completionshanded the provider's error text back as the answer: HTTP 200,finish_reason: "stop", the text as content (anthropic error: status=-1 msg=Connection Closed Gracefully.).PersistGatewaySessionalso saved it as an assistant message: its guard assumed a hard failure has no text, and it has. The web UI kept it in its history, and asession_contextclient got it back from the store. Either way, on the next turn the model was told it had said it./v1/chat/completions: a refusal (ProviderRefused) now gets HTTP 502{"error": {message, type: "provider_error", upstream_status}}, or, withstream: true, an OpenAI error frame then[DONE].PersistGatewaySession(every route that saves a named session): the turn's history is saved, but the error is not appended as a reply.eturn. It is shown with a retry action, and is never sent to the model and never saved. Tool activity that streamed before the failure stays as the partial reply it was; retry drops back to the user's message.3. MCP tools could be invisible to the model
The MCP bridge built its
TToolon the stack and registered it throughRegisterDeferred, the oneRegister*that did not resetHidden.Hiddenis the back-compat-alias flag, andToProviderDefsskips such a tool for good. So leftover stack bytes decided whether an MCP tool reached the model:tool_searchlisted and revealed it, and the schema still never went out.RegisterDeferrednow clearsHiddenlike every other path, and the bridge zeroes its record first. That also makesCategoryreliably "treat as mutating", the documented default. I found this through the context e2e test: a stub MCP server's one tool was in neither the provider list nor the deferred list.4. Rebuild units whose source changed in the second they were compiled
This was the original content of this PR.
FPC decides a unit is up to date by comparing its source's timestamp with what the
.ppurecorded, to the second. A source rewritten in the same second its unit was compiled looks unchanged, and the build links the old code without any warning.scripts/sweep-stale-units.shruns once permakeinvocation:.ppu/.oof every source undersrc/that is newer than the marker (find -newer, which compares to the nanosecond);findis walking is then newer than the published marker, so the next run sweeps it (Codex review). Two concurrent makes never publish each other's marker.Tests
make test-context-usage(unit):CompactMeasureis exactly whereNeedsCompactfires.make test-context-usage-e2e: a real gateway with a relay model and a stub MCP server, MEMORY.md and AGENTS.md. For each request shape it compares the report with the request the model actually receives:no_tools;session_contextstored turns;make test-provider-error-e2e: the gateway against a fake provider that refuses with 401 and then recovers. It checks:session_contextturn sends the provider the question without the error;make test-mcp-disclosure:RegisterDeferredwith a garbageHidden, both deferred and not.make test-stale-units: the same-second edit with fixed timestamps, plus an edit during the scan, reproduced every run by afindshim that edits the source after the realfindhas looked at it.no_tools, skipping the session merge, dropping the mode directive, filing MCP tools as built-in, filing memory as system, counting a caller system message as a message;Hidden;/contextcommand, the window box (persisted, with the compaction tick at the right place) and the hover tooltips were checked too. Error turns render with a working retry.read_file,write_fileandshell_execcalls. The system prompt the model received was byte-identical to the panel's sections joined, and the tool lists matched. The panel recounted after each turn: 6,059 → 6,242 → 6,507 tokens.make testpasses on FPC 3.2.2. Delphi (dcc64) is not available here, so the Delphi build is untested.🤖 Generated with Claude Code
https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U