Skip to content

Context breakdown in the web UI; provider errors are not replies; MCP tools no longer hidden; rebuild same-second edits - #636

Merged
FMXExpress merged 5 commits into
mainfrom
claude/pasclaw-desktop-plan-uapzcp
Nov 13, 2026
Merged

FMXExpress merged 5 commits into
mainfrom
claude/pasclaw-desktop-plan-uapzcp

Conversation

@FMXExpress

@FMXExpress FMXExpress commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

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

  • A ◔ button in the chat toolbar always shows the running total, for example 6.5k · 8.1%.
  • Clicking it, or typing /context, opens a panel with:
    • a segmented bar drawn against the model's window;
    • a legend with each part's tokens and share: system prompt, memory, project rules, skills, built-in tools, MCP tools, messages, free space;
    • a line on how far the conversation is from compaction;
    • expandable lists:
      • system prompt sections: open one to read its text;
      • tools: built-in and per MCP server, biggest first, plus the deferred ones;
      • messages: by role, and the largest.
  • It recounts after every turn, on a session switch, and when the mode, model or system prompt changes.
  • A "window" box stores the model's context window per model. Without one, the bar is drawn against the compaction threshold, the limit PasClaw itself acts on.
  • The palette is validated for the dark surface and, with counter-inverted marks, for the inverted light theme. Every part's figure is listed beside the bar, so no value is colour-only.

Gateway: POST /v1/context

  • It takes the /v1/chat/completions body (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.
    • ChatRequestMessages and ComposeChatSystem are now shared by both routes.
    • It also uses the same stored-turn merge, orphaned-call repair, ToProviderDefs and turn clock (HistWithTurnClock).
  • BuildSystemPrompt is now BuildSystemPromptSections joined, 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_tokens comes from CompactMeasure, which NeedsCompact now uses itself, so it is the trigger's own number.
  • New config key context_window (display only, default 0).
  • text: true adds each section's text.
  • Every number is a chars/4 estimate, and the report says so.

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/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 also 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 a session_context client 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, with stream: 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.
  • Web UI: an error frame or a non-2xx is recorded as an e turn. 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 TTool on the stack and registered it through RegisterDeferred, the one Register* that did not reset Hidden. Hidden is the back-compat-alias flag, and ToProviderDefs skips such a tool for good. So leftover stack bytes decided whether an MCP tool reached the model:

  • with progressive disclosure off, the tool 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. That also makes Category reliably "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 .ppu recorded, 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.sh runs once per make invocation:

  • it deletes the .ppu/.o of every source under src/ that is newer than the marker (find -newer, which compares to the nanosecond);
  • the next marker is stamped before the scan, under a per-process name, and renamed into place after it. A source edited while find is 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):
    • the sections join to the prompt in every mode;
    • categories and MCP grouping, including the longest configured server prefix;
    • per-message and per-tool costs;
    • the report adds up;
    • window sources;
    • a caller's system message is counted as the system prompt;
    • CompactMeasure is exactly where NeedsCompact fires.
  • 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:
    • the tool list;
    • exact message tokens, turn clock included;
    • the exact compaction measure;
    • the sections against the system prompt;
    • a caller system message with plan mode;
    • no_tools;
    • session_context stored turns;
    • the window from the request or config.
  • make test-provider-error-e2e: the gateway against a fake provider that refuses with 401 and then recovers. It checks:
    • the 502 and the error frame carry the provider's message;
    • no error text arrives as content;
    • nothing is saved as a reply;
    • the next session_context turn sends the provider the question without the error;
    • a working reply is still saved.
  • make test-mcp-disclosure: RegisterDeferred with a garbage Hidden, 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 a find shim that edits the source after the real find has looked at it.
  • Revert checks: each of the following was undone in turn and caught by a test:
    • context: dropping the turn clock, measuring the stamped history, ignoring 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;
    • errors: saving the refusal as a reply, streaming it as content, answering 200;
    • MCP: not clearing Hidden;
    • sweep: publishing the marker after the scan.
  • Browser: the panel was checked in Chromium in dark, light and 390px-wide layouts, with no script errors and no horizontal scroll. The /context command, 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.
  • Live runs with Claude as the model (web UI → gateway → Anthropic provider → a by-hand Messages API):
    • Context: two turns with real read_file, write_file and shell_exec calls. 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.
    • Errors: the provider refused turn 2 with a 401. The web UI showed an error turn and saved only the real messages. The retry and the next turn reached the model with clean history: its own earlier answer, the questions, and no trace of the error.
  • Full make test passes 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

…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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T00:04:18.573949Z 85dafaf PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 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".

Comment thread scripts/sweep-stale-units.sh Outdated
done
done
fi
touch "$mark"

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 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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
@FMXExpress FMXExpress changed the title build: rebuild units whose source changed in the second they were compiled Context breakdown in the web UI; MCP tools no longer hidden by stack garbage; rebuild same-second edits Nov 13, 2026
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
@FMXExpress FMXExpress changed the title Context breakdown in the web UI; MCP tools no longer hidden by stack garbage; rebuild same-second edits Context breakdown in the web UI; provider errors are not replies; MCP tools no longer hidden; rebuild same-second edits Nov 13, 2026
@FMXExpress
FMXExpress merged commit f5c17d6 into main Nov 13, 2026
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