Skip to content

fix(callbacks): classify HTTP errors and sanitize CLI diagnostics - #966

Merged
mergify[bot] merged 3 commits into
matrixorigin:mainfrom
loveRhythm1990:fix/cli-callback-http-errors
Oct 10, 2026
Merged

mergify[bot] merged 3 commits into
matrixorigin:mainfrom
loveRhythm1990:fix/cli-callback-http-errors

Conversation

@loveRhythm1990

@loveRhythm1990 loveRhythm1990 commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A tool-result or approval callback rejected by a gateway could be classified as a lifecycle contract violation and print a full HTML block page. Classify callback HTTP errors by status and require the producer-authored edge_callback_payload_conflict code for HTTP 409 contract conflicts. JSON shape and English detail text are insufficient. The Server now emits stable codes for divergent callback payloads, lost execution authority, and superseded interactions. Recorded interactions that did not resume execution and unknown HTTP 409s remain ordinary conflicts (InvalidRequest), preserving turn evaluation and the existing approval acknowledgement gate. Reuse the CLI API formatter for live stderr, returned errors, and final detach diagnostics: retain status and bounded diagnostic IDs, omit HTML/markup pages, and bound other response previews. HTML responses retain the canonical HTTP status hints used by session-authentication recovery. Callback 401 handling remains owned by the existing authentication handlers; the unreachable classifier branch is removed.

Related issue

Related #919. The WAF configuration has been corrected and an end-to-end callback succeeded; this PR addresses the independent CLI diagnostics defect.

Change type

  • Bug fix
  • Documentation
  • Test

User and compatibility impact

Gateway HTML rejections no longer imply durable lifecycle rejection or flood the terminal. Authentication refresh, coded payload conflicts, transport retry bounds, and existing interactive/headless settlement behavior remain intact. Callback conflict responses gain additive error_code values; request bodies and configuration are unchanged. Older uncoded 409 responses remain ordinary HTTP conflicts and never establish approval acknowledgement.

Architecture and complexity delta

  • Canonical owner changed or extended: existing CLI API error formatter, Edge callback failure projection, Server callback response producers, and shared thin-client protocol constants.
  • Existing implementations and callers searched: tool-result and approval callbacks, authentication retry, and interactive/headless recovery consumers.
  • Superseded code, states, tables, shims, or self-only tests removed: raw callback API-body rendering and HTTP/JSON-shape-based contract classification.
  • Parallel implementations: none added; callback diagnostics use the shared formatter.

Verification

  • Local make format-check, git diff --check, and cargo test -p astra-runtime --lib edge_callback_insert_tests -- --test-threads=2 passed (40 tests).
  • Local CLI/HTTP test builds were stopped because they were consuming excessive workstation resources; those local checks did not finish. No local compilation remains running.
  • CI on current head 3b46a12deabc5b267712283a0393bd68df7d935f passed workspace Clippy and static checks. The remaining test jobs are still running; they are not claimed as passed.
  • The preceding formatter follow-up passed workspace Clippy and relevant CLI tests locally; its complete CLI CI passed 4,710 tests (10 skipped). This is prior-head evidence only.

New regressions cover both durable and Edge-ledger approval receipt replay after reconstructing AppState; authority loss and supersession remain HTTP 409 without authorizing local execution. Tool-result coverage rejects divergent replay after consumption. CLI loopback tests exercise both callback endpoints with HTML 405, generic JSON 409, and all three producer-authored conflict codes, including acknowledgement and detachment gates.

Database verification: not applicable; response projection changes only, with no persistence or approval-decision changes.

Final checklist

  • Tests cover the owning layer and unhappy paths.
  • Updated the session observability design contract.
  • Checked the diff for credentials, private URLs, customer data, and generated files.
  • PR title follows Conventional Commit format.

@loveRhythm1990

Copy link
Copy Markdown
Collaborator Author

Review (cf1eb48)

Important

  1. crates/astra-cli/src/cli/cli_config/cli_utils.rs:845-860 — 新的 HTML/markup 分支直接 return,完全丢弃了 status_hint_for(status, None)。非 HTML 的 body 走 format_error_with_context(cli_utils.rs:711)或 JSON 分支时都会带上状态提示,只有 HTML 分支没有。

    具体后果:认证代理 / SSO 网关对 401、403、429 返回 HTML 登录页或拦截页时,用户拿到的只有 request failed (401): HTML/markup response body omitted + WAF 提示,丢掉了 Authentication required — try /login / Permission denied — check your access rights。受影响的调用点包括 admin_cli/http_helpers.rs:18、tui/event_loop.rs:1223、cli/slash/slash_memory.rs(多处)。

    次级后果:is_astra_session_auth_error()(cli_utils.rs:763-773)是对格式化后的文本做匹配的,其中两条模式正是 hint: authentication required — try /login。该函数经 auth_flow::is_auth_error 喂给 turn/turn_auth_retry.rs:16 和 command_router.rs:1325 的重试/重登决策。本 PR 把 read_api_error 的输出接进了 edge_callback_detach_message → accum.error_message → TurnFailure.error,于是这条文本匹配路径的输入形态被改变了。

    建议:HTML 分支里把 status_hint_for(status, None) 和 WAF 提示一起输出,状态相关的指引不要因为 body 是 markup 就消失。

    顺带说一句好的方向:把页面正文从消息里摘掉,同时也消除了"WAF 页面正文里恰好含 invalid token 之类字样 → is_auth_error 误命中 → 触发无意义的 token 刷新"这个旧风险。不过根本问题仍是用自然语言文本驱动认证恢复的控制流;edge_callback_error_kind 这次改成按 status + 结构化 JSON 判定是对的,is_astra_session_auth_error 这条链建议后续单独用 ErrorKind / status 收口。

  2. crates/astra-cli/src/cli/stream/stream_render.rs:1573-1578 — 新增的 UNAUTHORIZED => ErrorKind::Auth 分支在生产路径上不可达。edge_callback_error_kind 的唯一生产调用点是 mark_edge_callback_failure(stream_render.rs:1859),而它的两个调用方 handle_post_tool_result_error / handle_post_approval_error(stream_render.rs:1884、1904)都先用 is_edge_auth_failure(e) 把 401 拦走走 mark_edge_auth_failure;刷新失败后再次回到 Err(e) 分支时同样还是走 401 拦截。目前只有单测直接调用该函数才会命中这条分支。建议删掉,或者在注释里写明它为什么要保留。

次要

  • api_error_preview(cli_utils.rs:899-906)按 is_control() && !is_whitespace() 过滤,ESC 字节确实被去掉了,但序列剩余部分(如 [31m)会作为普通文本留在输出里。新测试只断言 !error.contains('\u{1b}'),所以这点没被覆盖。注入风险已经消除,只是显示上会留下可见垃圾。
  • HTML 判定只 trim_start_matches(['\u{feff}', ' ', '\t', '\r', '\n']),前面有其它空白(如 \u{000b})时不会命中;此时会落到 fallback 分支,仍被 512 字符上限兜住,影响有限。

已核对、未发现问题

  • edge_callback_error_kind 的分支顺序(CONFLICT 结构化 → 5xx → 401 → 429 → 其它 4xx)没有遮蔽问题;409 结构化判定与 recorded_approval_decision(stream_render.rs:1521)的 approval_decision_already_recorded 通路互不干扰。
  • 409 纯文本 / HTML 从 ContractViolation 降级为 InvalidRequest 会让 server_admission_host.rs:1517 不再丢弃 turn_evaluation。这是本 PR 想要的方向(网关层 HTTP 拒绝不是协议契约违规),行为变化本身合理。
  • truncate_line 是按 char 计数的,多字节边界安全;api_error_preview 先折叠空白,lines().next() 不会再截断。

测试

  • 新增的三个 read_api_error 单测 + edge_callback_http_failures_require_structured_evidence_for_contract_conflicts + Wiremock 回环的 edge_callback_html_rejection_stops_physical_stream_without_reposting(断言两个 endpoint 各仅 1 次请求、physical token 取消、parent token 未取消)覆盖是扎实的。
  • 缺口:没有任何测试覆盖"HTML body + 需要状态提示的 status"(上面第 1 条),所以该回归不会被 CI 发现。

Review provenance

  • 审阅 commit cf1eb487b3faca43d01b1a40ec48c94c2018b112,全量 diff(3 文件,+145/-12)。
  • 由 Claude Opus 5 直接审阅;未按 review_changes 的默认要求委派独立的 GPT-6 high 评审——此处如实披露,这份结论不等同于独立复核。
  • 未运行 Rust 测试(本地未跑 make lint / cargo test);CI 全绿为唯一执行证据。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed current head 4e76564a120a9e3976f053050661667a807a3e4b, including the follow-up commit. Requesting changes for the remaining P2 classification issue attached inline. It is a residual design defect in the area this PR changes, not a newly introduced regression.

The follow-up fixes the earlier HTML status-hint regression, removes the unreachable 401 classifier arm, and reuses the existing ANSI stripper. Those findings are withdrawn. The HTML 405 incident is addressed, and sharing the existing formatter across callback diagnostics is the right ownership choice.

The remaining issue is the inference that a JSON-shaped HTTP 409 proves an internal lifecycle violation. Existing server outcomes for superseded approval or lost execution authority show why that inference is too broad. Keep the current acknowledgement and detachment safety gates while making the error projection follow the producer's typed outcome.

Existing comment disposition: status-hint loss and the dead 401 arm are now fixed. I do not adopt the claimed turn-auth-refresh regression: current turn_auth_retry uses typed error fields. The admin CLI formatter is also separate from the formatter changed here.

Verification: read the current CLI CI log: 4,710 tests passed, 10 skipped, including both new formatter follow-up tests. Current Clippy and formatting jobs passed. Inspected real callback callers, the server's structured conflict outcomes, auth refresh, approval acknowledgement, and interactive/headless detach consumers. Implementation +90/-23 (net +67); tests +253/-4 (net +249); documentation +11.

Limits: the selected executor failed to become usable, so no local Rust tests, live WAF/provider harness, or performance benchmarks were run by this reviewer. CI execution evidence is distinguished from source inspection. No subagents were used.

@loveRhythm1990
loveRhythm1990 force-pushed the fix/cli-callback-http-errors branch from 4e76564 to 3b46a12 Compare October 10, 2026 06:42
@loveRhythm1990 loveRhythm1990 changed the title fix(cli): classify callback HTTP errors and suppress HTML pages fix(callbacks): classify HTTP errors and sanitize CLI diagnostics Oct 10, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed eb34cf99ae617a09d35b3a59e40bfc05ac34cbe8 against 1b200734e8fa5c573dbe1e401b9014c130d9c108. No remaining material correctness or design findings. This approval supersedes my earlier Request Changes review on 4e76564a; its P2 has been fixed in code, and the corresponding thread is resolved.

The earlier P2 is addressed at the correct ownership boundary: Server callback handlers project their existing typed outcomes into shared thin-client error-code constants. CLI classification requires HTTP 409 plus the exact edge_callback_payload_conflict code for a contract violation. Authority loss, supersession, and unknown/uncoded JSON or HTML conflicts remain ordinary rejected HTTP requests. They cannot establish approval acknowledgement. The existing approval_decision_already_recorded decoder and durable_allow_was_acknowledged gate remain separate and unchanged.

Design assessment: the patch extends existing response producers, the existing error classifier and the shared API formatter. It removes raw callback-body rendering and broad HTTP-409 inference without adding a lifecycle, storage, authorization, or retry owner. Generic unknown-409 rejection is a fail-closed outcome, not a version-migration path. There is no model-specific or English-message-based classification in the changed callback path. Existing transport/authentication retry and interactive/headless settlement owners remain in place.

Other prior comments are addressed: markup responses retain canonical status hints, the unreachable callback-401 classifier arm is absent, Unicode leading whitespace is handled, and the existing ANSI stripper removes complete CSI/OSC sequences. The actual callback 401 path still uses HTTP status and its existing token-refresh handlers. Live stderr, returned callback errors and detach diagnostics now share the formatter.

Tests and behavior checked:

  • Loopback HTTP cases cover both callback endpoints with HTML 405, generic JSON 409, and the three producer codes. Assertions cover denied acknowledgement, preserved failure kind/run identity, child stream cancellation without parent cancellation, bounded diagnostics, exactly one request per endpoint, and no Server cancellation request.
  • Server tests cover divergent tool-result replay after consumption, plus authority-lost/superseded approval receipts through both durable and Edge-ledger handler branches.
  • Existing acknowledgement, replay, authentication and interactive/headless detach behavior remains covered. The production resolver's existing ordering preserves authority-lost/superseded outcomes ahead of divergent-payload handling, so a nonresuming receipt cannot be upgraded into an execution acknowledgement through that replay path.

Current-head execution evidence is now complete, rather than relying on the prior-head checks noted in the PR description. Inspected logs identify merge commit cdbecc230da22124d823452f2eb65d55fe91cb57 containing this exact head:

  • CLI: 4,710 passed, 10 skipped; new formatter, producer-code and loopback callback regressions explicitly passed.
  • Runtime: 5,911 passed, 173 skipped; all 40 callback-handler tests, including the extended nonresuming-outcome coverage, passed.
  • Online services: 441 passed, 1,819 skipped.
  • Static checks: Clippy and formatting passed.

Limits: no local executor is available, so I did not independently rerun Rust tests, native git diff --check, a real WAF/provider harness, or performance benchmarks. Added diff lines contain no observed trailing whitespace/conflict markers. The restart-named regression reconstructs AppState over a shared in-memory lifecycle fixture; it proves response projection without local ledger state, not process-crash/database reload recovery. Output previews are bounded, but response-body reads and formatting scans are not a new memory bound. No additional database/network work is introduced by the production change. No subagents were used.

Change accounting: implementation +121/-38 (net +83); tests +411/-63 (net +348); documentation +19/-0.

@mergify mergify Bot added the queued label Oct 10, 2026
@mergify

mergify Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-10 08:58 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ❌ Checks failed · in-place
  • 🚫 Left the queue — 2026-10-10 09:29 UTC · at f303c4e497449f4cbd1fc36151ee9e760f1e2134

This pull request spent 31 minutes 13 seconds in the queue, with no time running CI.

Reason

The merge conditions cannot be satisfied due to failing checks

  • Check PR title
  • check
  • Test: core crates + bridge hooks
  • Test: astra-runtime
  • Test: astra-cli (edge-fs-tools)
  • Test: astra-cli (edge-git-gix)
  • Test: astra-cli (edge-rest)
  • Test: astra-cli (edge-shell)
  • Test: astra-cli (non-edge)
  • Test: online (core)
  • Test: online (integration)
  • Test: turn-core + services + plan
  • web (typecheck, test, build)
  • Astra SDK (typecheck, test+coverage, build)

Hint

You may have to fix your CI before adding the pull request to the queue again.
If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Test: online (core)
    • check-skipped = Test: online (core)
    • check-success = Test: online (core)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Test: online (integration)
    • check-skipped = Test: online (integration)
    • check-success = Test: online (integration)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Test: macOS workspace coordination
    • check-skipped = Test: macOS workspace coordination
    • check-success = Test: macOS workspace coordination
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Test: online (core)
    • check-skipped = Test: online (core)
    • check-success = Test: online (core)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Test: online (integration)
    • check-skipped = Test: online (integration)
    • check-success = Test: online (integration)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Test: macOS workspace coordination
    • check-skipped = Test: macOS workspace coordination
    • check-success = Test: macOS workspace coordination
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Check PR title
    • check-neutral = Check PR title
    • check-skipped = Check PR title
  • any of [🛡 GitHub branch protection]:
    • check-success = check
    • check-neutral = check
    • check-skipped = check
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: core crates + bridge hooks
    • check-neutral = Test: core crates + bridge hooks
    • check-skipped = Test: core crates + bridge hooks
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-runtime
    • check-neutral = Test: astra-runtime
    • check-skipped = Test: astra-runtime
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-fs-tools)
    • check-neutral = Test: astra-cli (edge-fs-tools)
    • check-skipped = Test: astra-cli (edge-fs-tools)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-git-gix)
    • check-neutral = Test: astra-cli (edge-git-gix)
    • check-skipped = Test: astra-cli (edge-git-gix)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-rest)
    • check-neutral = Test: astra-cli (edge-rest)
    • check-skipped = Test: astra-cli (edge-rest)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-shell)
    • check-neutral = Test: astra-cli (edge-shell)
    • check-skipped = Test: astra-cli (edge-shell)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (non-edge)
    • check-neutral = Test: astra-cli (non-edge)
    • check-skipped = Test: astra-cli (non-edge)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: turn-core + services + plan
    • check-neutral = Test: turn-core + services + plan
    • check-skipped = Test: turn-core + services + plan
  • any of [🛡 GitHub branch protection]:
    • check-skipped = web (typecheck, test, build)
    • check-neutral = web (typecheck, test, build)
    • check-success = web (typecheck, test, build)
  • any of [🛡 GitHub branch protection]:
    • check-skipped = Astra SDK (typecheck, test+coverage, build)
    • check-neutral = Astra SDK (typecheck, test+coverage, build)
    • check-success = Astra SDK (typecheck, test+coverage, build)

Requeued — the merge queue status continues in this comment ↓.

@mergify
mergify Bot force-pushed the fix/cli-callback-http-errors branch from eb34cf9 to f303c4e Compare October 10, 2026 09:07
@mergify

mergify Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-10 09:43 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-10-10 09:44 UTC · at 7ffef40138884a6db5da267fa685af2820bb794c · squash

This pull request spent 14 seconds in the queue, including 1 second running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Check PR title
    • check-neutral = Check PR title
    • check-skipped = Check PR title
  • any of [🛡 GitHub branch protection]:
    • check-success = check
    • check-neutral = check
    • check-skipped = check
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: core crates + bridge hooks
    • check-neutral = Test: core crates + bridge hooks
    • check-skipped = Test: core crates + bridge hooks
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-runtime
    • check-neutral = Test: astra-runtime
    • check-skipped = Test: astra-runtime
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-fs-tools)
    • check-neutral = Test: astra-cli (edge-fs-tools)
    • check-skipped = Test: astra-cli (edge-fs-tools)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-git-gix)
    • check-neutral = Test: astra-cli (edge-git-gix)
    • check-skipped = Test: astra-cli (edge-git-gix)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-rest)
    • check-neutral = Test: astra-cli (edge-rest)
    • check-skipped = Test: astra-cli (edge-rest)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-shell)
    • check-neutral = Test: astra-cli (edge-shell)
    • check-skipped = Test: astra-cli (edge-shell)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (non-edge)
    • check-neutral = Test: astra-cli (non-edge)
    • check-skipped = Test: astra-cli (non-edge)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: online (core)
    • check-neutral = Test: online (core)
    • check-skipped = Test: online (core)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: online (integration)
    • check-neutral = Test: online (integration)
    • check-skipped = Test: online (integration)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: turn-core + services + plan
    • check-neutral = Test: turn-core + services + plan
    • check-skipped = Test: turn-core + services + plan
  • any of [🛡 GitHub branch protection]:
    • check-skipped = web (typecheck, test, build)
    • check-neutral = web (typecheck, test, build)
    • check-success = web (typecheck, test, build)
  • any of [🛡 GitHub branch protection]:
    • check-skipped = Astra SDK (typecheck, test+coverage, build)
    • check-neutral = Astra SDK (typecheck, test+coverage, build)
    • check-success = Astra SDK (typecheck, test+coverage, build)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: macOS workspace coordination
    • check-neutral = Test: macOS workspace coordination
    • check-skipped = Test: macOS workspace coordination

@mergify
mergify Bot merged commit 7ffef40 into matrixorigin:main Oct 10, 2026
@mergify mergify Bot removed the queued label Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants