Skip to content

feat(http): a refused request's log line names its error code - #1812

Merged
V3RON merged 3 commits into
callstackincubator:mainfrom
bheemreddy-samsara:bheemreddy-samsara/request-log-error-code
Nov 25, 2026
Merged

V3RON merged 3 commits into
callstackincubator:mainfrom
bheemreddy-samsara:bheemreddy-samsara/request-log-error-code

Conversation

@bheemreddy-samsara

@bheemreddy-samsara bheemreddy-samsara commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A gateway's request log line has the HTTP status but not the error code. A 404 could be UNKNOWN_LEASE, UNKNOWN_FILE or UNKNOWN_LEASE_REQUEST, and a refusal that never reaches the dispatcher (validation 400s, auth 401/403, tracker refusals) leaves no code anywhere.

Change

Head: 68d02c8a.

  • errorResponse, which writes the JSON error bodies simlock makes, records the code on the request context (errorCode in the app's Variables).
  • The request line carries it as code. The log step reads no body: no clone, no parse, no wait on a worker's relayed body.
  • A mount's own answer and a worker's relayed answer log their status alone, by construction. simlock's own auth refusal on a mount's path logs its code.
  • The gateway's own 413 FILE_TOO_LARGE (gateway-lease-files.ts) is thrown as a DispatchError, so it goes through errorResponse and logs its code. Same body and status.
  • src/http/AGENTS.md describes the new field.

Not covered, because these answers do not go through errorResponse: the 426 on a plain GET /v1/session (upgradeRequired) and the lease-files handler's refusals such as UNKNOWN_FILE (lease-files/handler.ts).

Verification

  • src/http/app-request-log.test.ts covers four cases:

    • a 404 logs code: "UNKNOWN_LEASE", and the caller still gets the full body;
    • a 200 logs no code;
    • a mount's own JSON 400 logs no code;
    • a 401 on a mount's path logs code: "UNAUTHENTICATED".

    The first and last fail without the errorResponse change.

  • src/daemon/gateway-lease-files.test.ts: an over-limit declared size gets 413 FILE_TOO_LARGE with the same body, relays nothing, and logs code. Fails without the change.

  • pnpm typecheck, oxlint, depcruise, oxfmt --check, comment and test lints, pnpm fallow --ci, and the .repo.test.ts files.

  • src/http and src/gateway unit tests: 1,137 pass.

Merge risk

  • Alignment: the approach V3RON asked for; three refusals outside errorResponse stay uncovered (listed above).
  • Impact: one optional code field on refused request lines, on workers and gateways; no new lines.
  • Verification: four unit tests, all 31 CI checks on 0bbb765d, CodeRabbit with no findings.
  • Rollback: revert one commit; the field is additive and omitted on success.
  • Error detection: a missing or wrong code shows in the gateway's request lines.

Review

Spec review: skipped. Code review: 1 blocking, 1 fixed, 2 notes. Claims review: skipped.
Rounds: 2. Tokens: n/a.

Round 2 (0bbb765d) and the fix-check (68d02c8a): 0 findings; every round 1 finding resolved. Both were coordinator self-checks, no lens agents.

  • Blocking: some JSON refusals bypass errorResponse (listed under Change). Left out of scope: covering the file refusals needs the lease-files interface to return the code.
  • Fixed: the mount test's title claimed the body goes unread, which its assertions cannot show.

Rejected:

  • code: the ETag step and the lease-id rewrite still read some relayed bodies. Both predate this PR.
  • code: typing errorResponse's context breaks five callers, because Hono's Context is invariant in its env.

Written by an agent.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The HTTP environment now has an optional errorCode context variable. errorResponse stores the mapped code in that variable. Request logging adds the value as code when it is defined. The logger does not read the response body. Tests cover application refusals, successful responses, mount-generated refusals, and unauthenticated mount requests. The gateway lease-file handler throws a FILE_TOO_LARGE dispatch error when a named file exceeds the declared size limit.

Suggested reviewers: v3ron

Merge Risk: Minimal · up to 68d02

This change adds an optional error code to refused request log lines and routes the gateway's oversized-file refusal through the coded error path. No concrete merge-blocking risk was identified, and the existing response behavior is reported as preserved.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: a refused request's request log line now includes the error code. It matches the summarized changes to errorResponse, the request log step, and the new tests. The 'fe…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is specific and tied to the changeset. It explains that a gateway's request log line lacks the error code, and that the change records errorCode on the request context in errorResponse…
Full details: Title check

Explanation

The title states the main change: a refused request's request log line now includes the error code. It matches the summarized changes to errorResponse, the request log step, and the new tests. The 'feat(http)' prefix names the affected area.

Full details: Description check

Explanation

The description is specific and tied to the changeset. It explains that a gateway's request log line lacks the error code, and that the change records errorCode on the request context in errorResponse so the request log carries it as code. It lists the touched files: gateway-lease-files.ts (413 FILE_TOO_LARGE as a DispatchError), app-request-log.test.ts, gateway-lease-files.test.ts, and src/http/AGENTS.md. It also names uncovered refusals (426 on GET /v1/session, lease-files handler refusals such as UNKNOWN_FILE), and it lists verification and merge risk. The description does not match the PR objectives on one point: it says the gateway 413 is not covered, while a later commit covers it. That mismatch does not make the description off-topic.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: QUIET
  • Plan: Advanced
  • Run ID: 95304e37-b5c2-4848-a9d2-30a22ac13ce7
Commits

Reviewing files that changed from the base of the PR and between 0bbb765 and 68d02c8.

Files selected for processing: 2

  • src/daemon/gateway-lease-files.test.ts
  • src/daemon/gateway-lease-files.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Finishing Touches
Fix failing CI checks

  • Create a new PR

@V3RON

V3RON commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Better solution

Problem. The request log line has the status but not the error code. Refusals that do not go through the dispatcher (validation 400s, auth 401/403, tracker refusals such as UNKNOWN_LEASE_REQUEST) get no operation line, so the code is lost. Dispatched refusals already log code on their operation line (src/daemon/dispatch.ts:291).

The PR's approach works, but there is a simpler one. Every JSON error body simlock writes is built by one function, errorResponse (src/http/errors.ts:198). app.onError calls it, and so do validationHook (routes.ts:282), auth refusals (requireAuth throws HttpApiError, which reaches onError) and the lease routes (app-lease-routes.ts:130,180). So the code can be recorded where it is made, not read back from the body:

// errors.ts, in errorResponse
c.set("errorCode", mapped.code);

// app.ts, in the request log step
const code = c.get("errorCode");

Add errorCode?: string to the app's Variables. This:

  • reads no body: no clone(), no JSON parse, no await before the log line;
  • needs no mount exception: a mount's own answer never calls errorResponse, so it has no code by construction. The isMounted refactor is not needed;
  • also covers a mount's auth refusal (401/403 on /v1/<mount>/...). That refusal is simlock's own errorResponse, but the PR skips it because of the path check;
  • never touches a body that came from a worker (see Side effects).

answeredErrorCode and the shared isMounted can go. The test can stay as it is, plus one case for a 401 on a mount path.

Side effects

  • Gateway: relayed answers are read before the caller gets them. On a gateway, a module's lease route (e.g. /v1/leases/<id>/input) and the lease file routes return the worker's Response. Its body streams from the uplink relay (src/gateway/relay.ts:353). These are not mounts, so a relayed 4xx/5xx JSON answer is cloned and read to the end inside the log step. The step awaits that read before the response goes back up the chain. So the caller's head now waits for the worker's whole body, bounded only by the relay's silence timer, and the body has no size cap. Error bodies are small in practice, but the gateway manual says a worker must not be able to stall its gateway. Recording the code in errorResponse avoids this. As a side effect, relayed refusals log no code, which is honest: the gateway did not make that answer.
  • Worker vs gateway. Both build the app with createHttpApp, so both change in the same way. Apart from the relay point above, nothing else differs.
  • Log volume and format. No new lines. One more field on refused requests only, and it is omitted on success. Fields are code, durationMs, method, path, requesterId, status, the same name code the operation line uses. Nothing in website/ or EVENTS.md documents the request line's fields, so no public doc changes.
  • Performance. One clone and parse per 4xx/5xx JSON answer, including every 401 in an unauthenticated flood. That is small, but it is work the context approach does not do.
  • Duration. durationMs is now taken before the body read, so it stays comparable to the old value. Good.
  • Not covered by either approach: Hono's own c.notFound() (plain text, no code) and errors sent as SSE events after a stream opened (status 200). Both are fine, but worth knowing.

Review

Ran the repo's review skill (reviewer agent, round 1, commit 8b28e1a). It ran no separate lens agents, so this is a coordinator self-check, not a full independent review.

  • Spec review: skipped (no linked issue). Code review: 0 findings. Claims review: 0 findings.
  • Verified: the log step runs outside the ETag step, so it sees the final body. The body is cloned before it is read, so the caller still gets it (the first test asserts this). The new src/http/AGENTS.md line matches the code. No other doc describes the request log fields.
  • Notes (not blocking):
    • src/http/app-request-log.test.ts:41: mounts: [...] as never bypasses the mount type. A typed stub would be cleaner.
    • src/http/app.ts: every 4xx/5xx JSON answer is cloned and parsed once, including 401 floods. The cost is small.

The review did not consider the gateway relay case above. That case is the main reason I suggest the errorResponse approach.

Written by an agent.

A gateway's `request` log line had the status but not the error code, so
a 404 could be `UNKNOWN_LEASE`, `UNKNOWN_FILE` or `UNKNOWN_LEASE_REQUEST`,
and a refusal that never reaches the dispatcher left no code anywhere.

`errorResponse`, which writes every JSON error body simlock makes, now
records the code on the request context, and the log step adds it as
`code`. The log step reads no body, so a mount's own answer and a worker's
answer relayed by a gateway log their status alone, and a gateway never
waits on a worker's body before answering.

Co-authored-by: Cursor <cursoragent@cursor.com>
@bheemreddy-samsara
bheemreddy-samsara force-pushed the bheemreddy-samsara/request-log-error-code branch from 8b28e1a to 0bbb765 Compare November 24, 2026 16:56
@bheemreddy-samsara

Copy link
Copy Markdown
Contributor Author

@V3RON Addressed your review comments

@bheemreddy-samsara

Copy link
Copy Markdown
Contributor Author

@V3RON Switched to recording the code in errorResponse as suggested (0bbb765d): no body read, and the isMounted refactor is gone. Added a test for a 401 on a mount path.

Three JSON refusals don't go through errorResponse, so they log no code:

  • upgradeRequired's 426 on a plain GET /v1/session (src/http/app-helpers.ts)
  • the gateway's own 413 FILE_TOO_LARGE (src/daemon/gateway-lease-files.ts)
  • the lease-files handler's refusals, such as UNKNOWN_FILE (src/lease-files/handler.ts)

Should these be covered too? The 426 is a one-line c.set in the session route. The file refusals would need HttpLeaseFiles.handle to return the code alongside its response. Happy to do either here or in a follow-up, or leave them as they are.

@V3RON

V3RON commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Second review, at 0bbb765d.

Better solution

Yes, errorResponse is the right place. Every JSON error simlock's HTTP layer writes for itself goes through it: app.onError (thrown errors, auth, dispatch), validationHook and the two stream routes. The log step reads no body, so nothing a worker sends can hold it up.

Does the gap of three refusals matter? Not much. My advice: leave them out and merge.

  • 426 on a plain GET /v1/session. path=/v1/session status=426 already says what happened, and it has only one cause. Adding code tells you nothing new.
  • Lease-file refusals (UNKNOWN_FILE, FILE_BUSY, LEASE_FILES_FULL, …). On a gateway these are the worker's answers. They are relayed, so with this design they log no code, and that is honest. The gateway's own refusal on a file path (UNKNOWN_LEASE from findOwnedLease) throws, goes through onError and logs its code. So a 404 on /v1/leases/<id>/files/<name> without a code is the worker's UNKNOWN_FILE, and one with UNKNOWN_LEASE is the gateway's. The ambiguity the PR set out to fix is gone. Only a standalone worker that answers its own file requests leaves the code out, and there status plus path still points to the cause.
  • Gateway's own 413 FILE_TOO_LARGE. This is the only one that is simlock's own answer and that can be confused with a relayed 413 (LEASE_FILES_FULL, or the worker's FILE_TOO_LARGE). If you want to cover it, the simplest fix is in src/daemon/gateway-lease-files.ts: have tooLarge throw an HttpApiError(413, "FILE_TOO_LARGE", message, { name }) instead of building a Response. serveLeaseFiles awaits handle, so onError writes the same body through errorResponse and records the code. You need to export HttpApiError from src/http/app.ts. No interface change. Fine here or in a follow-up. It should not block the merge.

Changing HttpLeaseFiles.handle to return the code next to its response is more interface than this needs. Don't do it.

Side effects

  • Log volume: no new lines. One optional field, only on answers errorResponse wrote. It is left out on success and on relayed or mount answers.
  • Log format: the field is named code, the same as on the operation line. Field order on the request line moves code first. Nothing parses it by position. The request line's fields are not documented in website/ or EVENTS.md, so no public doc changes. The src/http/AGENTS.md line is accurate.
  • Worker vs gateway: both build the app with createHttpApp, so both change the same way. On a gateway, relayed answers (module lease routes, file routes) now log status only, by construction. Nothing reads a worker's body any more, so the round 1 stall concern is gone.
  • Behaviour: response bodies, statuses and headers are unchanged. errorResponse only adds a c.set. The ETag step and the lease-id rewrite are as before. durationMs is measured the same way as on main.
  • Cost: one context write per refusal. No clone, no parse.
  • Still not covered (fine): Hono's c.notFound() (plain text), a handler's own non-JSON Response, and errors sent as SSE events after a stream opened (status 200).
  • Type change: Env moved from AuthEnv to an interface that adds errorCode. pnpm typecheck is clean. errorResponse still takes an untyped Context, so its other callers did not change.

Review

Ran the repo's review skill (reviewer agent, round 2, commit 0bbb765d). It started no separate lens agents, so this is a coordinator self-check, not a full independent review.

  • Spec review: skipped (no linked issue). Code review: 0 findings. Claims review: 0 findings.
  • Earlier findings:
    • Relayed 4xx/5xx body read in the log step (gateway stall): fixed. The log step reads c.get("errorCode") and no body. answeredErrorCode is gone.
    • simlock's own 401/403 on a mount's path logged no code: fixed. New test "carries the code of simlock's own refusal on a mount's path" checks UNAUTHENTICATED/401.
    • mounts: [...] as never in the test: fixed. It is now a typed jsonMount: HttpMount.
    • Clone and parse on every 4xx/5xx JSON answer: fixed. Nothing is cloned or parsed now.
    • isMounted refactor: removed. The ETag step keeps its own mount check, as on main.
  • Checked: pnpm typecheck clean. src/http/app-request-log.test.ts passes 4 of 4 locally. CI has no failing checks on 0bbb765d.

Looks good to merge as is. The 413 change above is optional.

Written by an agent.

@V3RON
V3RON merged commit a5af671 into callstackincubator:main Nov 25, 2026
55 of 57 checks passed
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