feat: add machine-readable session error reasons #258
If the caller cancels but the runtime error wraps an independent context.DeadlineExceeded, this retains both context errors and admissionInterruptedCode returns DEADLINE_EXCEEDED instead of CANCELED. Carry the caller's context outcome in a distinct marker and select the public code from it, while retaining the backend cause for Go callers. Same issue in session/errors.go:107–108 for Suspend/Fork.
If a later child fails with ErrComputeUnavailable while the caller is live, but the caller expires while Fork rolls back earlier children, this late context check replaces UNAVAILABLE/COMPUTE_UNAVAILABLE with DEADLINE_EXCEEDED and no reason. Classify caller interruption at the failing runtime operation before rollback, rather than after the whole fan-out returns, so cleanup cannot change the primary failure's status.
The new cause-preserving statuses let observability/grpc.go's context-first grpcCode checks override the selected RPC status. A live caller's Describe timeout returns UNAVAILABLE/HARNESS_UNAVAILABLE but is logged as grpc_code=DeadlineExceeded and error_kind=deadline_exceeded, misclassifying a harness outage as a caller timeout. Make that helper prefer the selected gRPC status too, using context classification only for errors without a status.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
Context-error handling can select incorrect RPC codes, let rollback timeouts overwrite primary failures, and cause observability to misclassify backend outages as caller timeouts.
<details open>
<summary><strong>3 open issues</strong></summary>
</details>
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>api/genpb/session_grpc.pb.go</code></strong>: Generated file</li>
</ul>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
Adds machine-readable session error reasons so clients can choose recovery actions without parsing messages. This establishes backend-neutral error categories for #250; Substrate classification remains separate.
Changes:
ErrorInfo details to recognized failures while preserving causes for direct Go callers.| File | Description |
| ---- | ----------- |
| spec.md | Defines error reasons, precedence, and recovery contracts. |
| session/service.go | Applies shared status mapping to lifecycle operations. |
| session/errors.go | Centralizes reason details and cause-preserving statuses. |
| session/error_start_test.go | Tests compute-start failures and unchanged journal heads. |
| session/error_reasons_test.go | Tests mappings, causes, sanitization, and cleanup precedence. |
| session/delete.go | Reuses the shared status helper. |
| session/authorization.go | Preserves sanitization despite newly exposed cause chains. |
| placement/cleanup.go | Marks caller interruption during allocation. |
| placement/cleanup_context_test.go | Tests allocation and confirmation context boundaries. |
| docs/api-reference.md | Documents recognized reasons in the API reference. |
| client/client_test.go | Checks error details survive Exec’s first frame. |
| changelog/unreleased/error-reasons.md | Announces structured reasons and retained Go causes. |
| changelog/unreleased/error-reasons-compat.md | Records status and diagnostic-visibility changes. |
| api/session.proto | Adds service comments describing error details. |
| api/runtime.go | Adds backend-neutral compute failure sentinels. |
| api/genpb/session_grpc.pb.go | Reflects service documentation in generated comments. |
</details>
<sub>Review effort: Balanced</sub>
I reproduced this on bd00da0: the call fails with DEADLINE_EXCEEDED, retains both causes, and appends no turn. I'm keeping the CANCELED versus DEADLINE_EXCEEDED distinction as a follow-up; it doesn't turn the failed operation into a successful one.
I verified the late classification change on bd00da0. The detached rollback still runs and the original failure remains in the Go cause chain, but its RPC code and reason can change when the caller ends during cleanup. I'm keeping that classification correction as a follow-up rather than changing the rollback boundary in this PR.
I reproduced the logging mismatch on bd00da0: the selected RPC status is UNAVAILABLE, while the logging helper reports DeadlineExceeded. The response code and ErrorInfo remain unchanged, so I'm leaving the logging-only correction to a follow-up.
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Error handling can misclassify caller cancellations, replace primary Fork failures after rollback, and produce request diagnostics that disagree with the actual RPC status.
No open issues 🎉
<details>
<summary><strong>1 previously missed issue</strong></summary>
In code that hasn't changed since last review
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> Classify Suspend/Fork failures using caller context</summary>
session/errors.go:108
A canceled Suspend/Fork caller can be reported as DEADLINE_EXCEEDED when the backend error wraps an independent context.DeadlineExceeded: this wrapper retains both sentinels, and admissionInterruptedCode selects the backend's deadline before the caller's cancellation. Capture the caller context error as explicit classification data and make the mapper use it without discarding the original backend cause.
</details>
</details>
<details>
<summary><strong>3 resolved since last review</strong></summary>
</details>
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>api/genpb/session_grpc.pb.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
Fixed in 94b88ba. The caller's outcome now chooses the context code independently of a backend timeout, while both Go causes remain available. The regression covers Exec and the Suspend/Fork mapping.
Fixed in 94b88ba. Fork now classifies the failure before rollback, so cancellation during cleanup cannot replace its code or reason. The regression cancels during an earlier child's rollback after the next child fails.
Fixed in 94b88ba. Request diagnostics now prefer the selected RPC status and classify context errors only when no status exists. The regression covers wrapped statuses and bare context errors.
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Cancellation and deadline responses can bypass INTERNAL-only sanitization and expose raw backend diagnostics, potentially leaking credential-bearing URLs to clients.
No open issues 🎉
<details>
<summary><strong>1 previously missed issue</strong></summary>
In code that hasn't changed since last review
<details>
<summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture> Prevent leaking backend diagnostics in canceled and deadline responses</summary>
placement/cleanup.go:58
This exposes raw backend diagnostics in CANCELED/DEADLINE_EXCEEDED responses, bypassing authorization's INTERNAL-only sanitization without the runtime opting into safe diagnostics. For example, an Exec deadline_unix can expire during Create while the RPC remains live, allowing a backend error containing a credential-bearing URL to reach the client. Omit Cause from this message while retaining it through Unwrap for direct Go callers.
</details>
</details>
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>api/genpb/session_grpc.pb.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
Fixed in 32d881e. The caller's outcome chooses the status, and backend diagnostics stay out of context-response messages while Go callers retain the cause.
Fixed at 32d881e. The two-child regression verifies that cancellation during rollback cannot replace the original code or reason.
Fixed at 32d881e. Request logs now honor the selected response status, including wrapped statuses, rather than a nested timeout.
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Human assessment is needed because cancellation can erase journal recovery classifications, while resource-exhaustion failures remain misclassified as internal faults in diagnostics.
No open issues 🎉
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>api/genpb/session_grpc.pb.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
Fixed in cd66a75. The caller's outcome controls cancellation/deadline classification, backend diagnostics stay out of the response, and both Go causes survive.
Fixed in cd66a75. Fork classifies a failing runtime child before rollback; cancellation during cleanup cannot replace that result, and journal conflicts keep their recovery reasons.
Fixed in cd66a75. Logs prefer the selected RPC status, use context classification only without a status, and label capacity failures as resource_exhausted rather than internal.
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Human assessment is needed because caller-interruption handling can mask established lifecycle recovery reasons across Exec, Resume, Fork, and Suspend.
No open issues 🎉
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>api/genpb/session_grpc.pb.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Human assessment is needed to evaluate a subtle allocation/cancellation interaction that can cause sessions to report an internal error instead of cancellation or deadline expiry.
No open issues 🎉
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>api/genpb/session_grpc.pb.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
<!-- ccr-overview-v2 -->
🟢 Approval recommended
No active review issues remain for the machine-readable error-reason changes.
No open issues 🎉
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>api/genpb/session_grpc.pb.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
Fixed in 4b419a1. Context codes now use the recorded caller outcome, not a nested backend timeout; both Go causes remain available and established lifecycle reasons keep priority.
Fixed in 4b419a1. Runtime failures are classified before rollback, so cancellation during cleanup cannot replace the primary code or reason. Journal refusals keep their own recovery reasons.
Fixed in 4b419a1. Logs use the selected RPC status, falling back to context only when no status exists. Capacity failures also have their own error kind.
Looks good to me. Two non-blocking nits inline.
nit: once the caller's context is done, this treats every Suspend failure as a caller interruption. That includes a SUSPEND journal append that failed on its own after a successful Snapshot, which was INTERNAL on main and now logs and counts as Canceled. Could the classification be limited to the Snapshot error, like allocationStartError does, or to errors.Is(err, ctx.Err())?
nit: withStatusCause now attaches ErrorInfo for every caller, so Approve/CloseApproval (SESSION_DELETED), RegisterHarness (HARNESS_UNAVAILABLE) and authorized mutations (SESSION_BUSY) return reasons, while session.proto, spec.md and api-reference.md say only Exec, Resume, Suspend, Fork and DeleteSession do. Should withStatusCause stay reasonless, with statusError used only in the operation mappers, or should the docs list these RPCs (and the busy case in approval.go use statusError too)?
I’ll track “Preserve Suspend journal failures after caller cancellation” as a separate follow-up issue. The call at 4b419a1 still fails and keeps its Go cause; cancellation changes the reported code and diagnostics.
I’ll track “Clarify ErrorInfo method and path coverage” as a separate follow-up issue, to settle the method list and approval-path consistency together. At 4b419a1, the extra host-owned details leave the refusal codes unchanged; I’ll keep that coverage decision separate from the Substrate classifier.
Approved these changes.
aramase
gambtho
Adds machine-readable reasons to known session failures, so clients can choose a recovery path without parsing messages.
Existing lifecycle codes stay the same. Compute-start cancellations no longer default to INTERNAL, and direct Go callers retain the original cause.
This adds the runtime error categories; Substrate classification will follow separately. Unknown backend failures still return INTERNAL without a reason.
Refs #250.