Skip to content

fix: answer 4xx instead of 500 for bad KV paths and unknown-automation tokens - #741

Merged
enyst merged 3 commits into
mainfrom
claude/project-thread-aesue3-kv-500s
Nov 11, 2026
Merged

enyst merged 3 commits into
mainfrom
claude/project-thread-aesue3-kv-500s

Conversation

@enyst

@enyst enyst commented Nov 11, 2026

Copy link
Copy Markdown
Member

Requested by Engel · project thread

I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.

Summary

Three client mistakes on the KV store reached the server as unhandled exceptions (500). A malformed ?path= on GET (a[b) raised an uncaught ValueError; a PATCH path naming no field ("", .) hit parts[-1] in set_nested_value (IndexError); and a validly signed KV JWT whose automation has no row was accepted, read as empty, and failed the automation_kv_meta foreign key on its first write. All three now answer 4xx.

Issue Number

Fixes #736

Changes

  • kv_router.get_value: a ValueError from parse_path maps to 400 invalid_path: ..., the same answer PATCH already gives.
  • kv_helpers.set_nested_value: a path with no segments raises ValueError, so single PATCH answers 400 invalid_path: ... and batch patch answers 400 operation_failed (the batch path had the same IndexError).
  • kv_router.get_kv_auth_context: on the JWT path, check the claim's automation row exists (_require_automation_exists); otherwise 404 Automation not found on every KV route, which is exactly what user auth answers for an unknown ?automation_id=. The check ignores deleted_at, so a soft-deleted automation's token keeps working, as user auth already does for soft-deleted automations. A hard-deleted automation's token is now 404 instead of empty reads and a 500 on write.
  • Regression tests in tests/test_kv_router.py, tests/test_kv_helpers.py, tests/test_kv_batch.py, tests/test_kv_user_auth.py (ghost token across list/get/put/patch/delete/incr/rpush/len/batch answers 404 and stores nothing; soft-deleted automation's token still reads and writes).
  • F12-kv-store.md: dropped the three # bug: markers and "known bug" wording; updated the Gotchas (the token claim is now checked against the database). The F12.jwt-unknown-automation recipe changed beyond the marker: the two ghost-token GETs that ran before the PUT expected 200 total:0 and 404 key_not_found; they now expect 404 Automation not found and run after the PUT (so the same recipe reproduces on baseline and passes on the fix), plus a SQL check that no automation_kv_meta row was created. F12.soft-deleted-automation gained a JWT read of the soft-deleted automation.
  • docs/kv-store-design.md, docs/kv-store-client-guide.md: error tables list the empty-path 400 and the Automation not found 404.

Evidence

  • uv run pytest -q tests/: 2905 passed, 7 skipped.
  • uv run pytest -q tests/test_verify_automation_skill.py: 59 passed.
  • pre-commit run --files <changed files>: ruff format/lint, pycodestyle, pyright pass.
  • Baseline (origin/main 1b571fd, map run --file F12 --only <id> --fresh --repeat 2 --checkout <baseline>): F12.get-path-malformed, F12.patch-empty-path, F12.jwt-unknown-automation each xfail ×2.
  • Fix branch, markers still present: each xpass ×2.
  • Markers dropped, control-automation map run --file F12 --fresh: 31 pass, 0 fail, 0 xfail, 0 xpass.

Notes

  • Behavior change for JWT callers: one extra primary-key lookup per JWT request, mirroring the lookup user auth already does. Reads with a token for a missing automation used to answer 200 with an empty list or 404 key_not_found; they now answer 404 Automation not found.
  • The live map runs used SQLite; the auth check is a plain SELECT automations.id, the same on PostgreSQL.

Generated by Claude Code

…n tokens

- GET /v1/kv/{key}?path= with malformed syntax (unclosed bracket) now
  answers 400 invalid_path, like PATCH, instead of an uncaught ValueError.
- set_nested_value rejects a path that names no field ("", "."), so PATCH
  answers 400 invalid_path and a batch patch 400 operation_failed instead
  of an IndexError 500.
- KV JWT auth checks the token's automation exists: a token whose
  automation has no row gets 404 "Automation not found" on every route,
  matching user auth for an unknown automation_id, instead of empty reads
  and a foreign-key 500 on the first write. Soft-deleted automations keep
  their row, so their tokens keep working as user auth does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013ArwwM9ABBZYP9fcggw4R8
@enyst enyst self-assigned this Nov 11, 2026
@github-actions github-actions Bot added the type: fix A bug fix label Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Warning

Your comment is too long (maximum is 65536 characters), so the coverage report was not added. See the job log for how to reduce it.

@datadog-official

This comment has been minimized.

Mark it as a detail string in the design doc's error-code table, and note
in the client guide that a soft-deleted automation's token keeps working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013ArwwM9ABBZYP9fcggw4R8
@enyst
enyst requested a review from all-hands-bot November 11, 2026 19:43

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This review was posted by an AI agent (OpenHands).

Scope: In-repo change. The KV store API (kv_router.py/kv_helpers.py) is owned by this repository per AGENTS.md, and linked issue #736 (open, bug + ready-for-dev, priority:medium) describes exactly these three client-error 500s. No repository move or maintainer decision needed.

Review summary — no material findings.

All three fixes were verified against this head (0602bd4):

  1. GET ?path=a[b → 400 — get_value now catches the ValueError from parse_path and answers 400 invalid_path: ..., matching PATCH. I reproduced this live through the real ASGI app: 400 invalid_path: Invalid path: unclosed bracket in 'a[b'. The depth-limit ValueError from parse_path also lands on this 400 path, which is correct client-error semantics.
  2. Empty/segmentless PATCH path → 400 — set_nested_value raises ValueError before the parts[-1] IndexError; single PATCH maps it to 400 invalid_path: ... and batch patch maps it through KVOperationError to 400 operation_failed. Verified ValueError: Invalid path: '' names no field on this head; tests/test_kv_helpers.py, tests/test_kv_batch.py, tests/test_kv_router.py cover "", ".", "..", and "a[b".
  3. Ghost KV token → 404 — get_kv_auth_context now verifies the claim's automation row exists (_require_automation_exists), so every KV route answers 404 Automation not found instead of empty reads plus a first-write FK 500. Verified live: ghost-token list and PUT both answer 404 Automation not found. All 15 KV routes depend on get_kv_auth_context, so coverage is complete. The check ignores deleted_at, which I confirmed matches the user-auth path (_verify_automation_access has no deleted_at filter; DELETE /v1/{id} is a soft delete that keeps the row) — and I verified a soft-deleted automation's token still reads 200 on this head.

Auth ordering is sound: signature verification happens before the existence check, so callers without a validly signed token cannot probe automation-id existence (they still get 401). The extra primary-key lookup per JWT request mirrors what user auth already does and is disclosed in the PR notes. Docs (F12-kv-store.md, docs/kv-store-design.md, docs/kv-store-client-guide.md) were updated consistently with the new behavior, including the deliberate reordering of the F12.jwt-unknown-automation recipe so it reproduces on baseline and passes on the fix.

Acceptance evidence: the PR's GitHub Actions checks on this exact head are all green (unit-tests, backend, windows-tests, image builds). Locally, the SQLite-based suites covering the new tests pass: tests/test_kv_helpers.py + tests/test_kv_batch.py + tests/test_kv_user_auth.py → 170 passed (incl. all 9 ghost-token route cases and the soft-delete case); the Postgres-testcontainer router tests cannot run in this sandbox (no Docker daemon), so I verified those behaviors live against the app instead as described above.

One pre-existing, out-of-scope nit (not blocking): docs/kv-store-design.md still lists 403 token_expired while the code answers 401 Invalid or expired KV token — this discrepancy predates the PR and is already flagged by the F12 gotcha.

✅ APPROVED

@all-hands-bot
all-hands-bot requested a review from neubig November 11, 2026 19:51
The error table documented an {error, message} body and codes such as
unauthorized and token_expired that the router never sends. List the
real {detail} strings and structured details from kv_router.py and
kv_helpers.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013ArwwM9ABBZYP9fcggw4R8
@enyst
enyst enabled auto-merge (squash) November 11, 2026 23:14
@enyst
enyst merged commit 87d64d3 into main Nov 11, 2026
11 checks passed
@openhands-release-bot openhands-release-bot Bot added the released: 1.33.0 Shipped in 1.33.0 label Nov 17, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.33.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released: 1.33.0 Shipped in 1.33.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: KV API answers 500 for client errors (malformed GET path, empty PATCH path, token for a missing automation)

3 participants