feat(api): API v1 foundation and read endpoints #125

Merged
jfro merged 6 commits from feat/122-api-read into main 2026-12-03 05:29:22 +00:00
Owner

Closes #122. Builds on Nye's design (#121), committed here as docs/api.md (still a draft: choices A-H are with Jeremy).

What's in it

  • /api/v1 pipeline with Authorization: Bearer rma_... auth (API keys only; the chat's answer tokens get 401), the shared error body, cursor paging (limit, opaque cursor tied to its query) and ETags (If-None-Match → 304).
  • RmAnalyzer.ApiKeys.allowed?/2: the one permission check, now used by MCP's tools/call too. Each endpoint needs the MCP tool it is, so existing keys reach exactly what MCP already lets them.
  • Read endpoints: /me, /sync, /documents, /documents/:id, /documents/:id/pages, /documents/:id/pages/:n (number or page id), /documents/:id/pages/:n/image, /folders, /tags, /search, /stats, /suggestions, /changes.
  • Notes.search_hits/3 + hit_maps/2 and Notes.page_maps/4: structured search and read sharing ranking/selection with the text tools. AgentTools.reviewable/1 is the one suggestion filter for MCP and the API.

Folder-limited keys

Sorting, Tagging, Remarkable.all_tags and the change log don't honour Scope.library, so the API filters per item. test/rm_analyzer_web/api/read_test.exs covers a Work-only key on every endpoint (404 outside, filtered lists) and a tool-limited key (403 naming the permission).

Left for #123

grants, writes, /events, /openapi.json, image width. Differences from the design are listed at the top of docs/api.md.

mix precommit: 1658 tests pass.

Closes #122. Builds on Nye's design (#121), committed here as `docs/api.md` (still a draft: choices A-H are with Jeremy). ## What's in it - `/api/v1` pipeline with `Authorization: Bearer rma_...` auth (API keys only; the chat's answer tokens get 401), the shared error body, cursor paging (`limit`, opaque `cursor` tied to its query) and ETags (`If-None-Match` → 304). - `RmAnalyzer.ApiKeys.allowed?/2`: the one permission check, now used by MCP's `tools/call` too. Each endpoint needs the MCP tool it is, so existing keys reach exactly what MCP already lets them. - Read endpoints: `/me`, `/sync`, `/documents`, `/documents/:id`, `/documents/:id/pages`, `/documents/:id/pages/:n` (number or page id), `/documents/:id/pages/:n/image`, `/folders`, `/tags`, `/search`, `/stats`, `/suggestions`, `/changes`. - `Notes.search_hits/3` + `hit_maps/2` and `Notes.page_maps/4`: structured search and read sharing ranking/selection with the text tools. `AgentTools.reviewable/1` is the one suggestion filter for MCP and the API. ## Folder-limited keys Sorting, Tagging, `Remarkable.all_tags` and the change log don't honour `Scope.library`, so the API filters per item. `test/rm_analyzer_web/api/read_test.exs` covers a Work-only key on every endpoint (404 outside, filtered lists) and a tool-limited key (403 naming the permission). ## Left for #123 grants, writes, `/events`, `/openapi.json`, image `width`. Differences from the design are listed at the top of `docs/api.md`. `mix precommit`: 1658 tests pass.
feat(api): API v1 foundation and read endpoints
All checks were successful
CI / test (pull_request) Successful in 1m57s
fc828f82db
JSON API at /api/v1 for native clients, on the same rma_ API keys and
scope as MCP (#122). ApiKeys.allowed?/2 is now the one permission check
MCP and the API share; every library endpoint needs the MCP tool it is.

Adds the Bearer auth plug (API keys only, not the chat's answer tokens),
the shared error body, cursor paging, ETags, and read endpoints: /me,
/sync, /documents, /documents/:id, its pages and page images, /folders,
/tags, /search, /stats, /suggestions and /changes. Notes gains structured
search and read, sharing ranking with the text tools; AgentTools.reviewable/1
filters suggestions for both MCP and the API.

Sorting, Tagging, Remarkable's tags and the change log don't honour a
key's folder limit, so the API filters per item; tests cover a
folder-limited key (404 or filtered lists) and a tool-limited key (403)
on every endpoint.

docs/api.md is Nye's design draft (#121), noting where #122 differs.

Refs #121
Closes #122
Author
Owner

Posted by Haystack for Nye (its token can't comment).

Review of #125 (API v1 read endpoints) — Nye, at fc828f8

Verdict: changes needed (2), then fine to ship. CI green; the folder/tool limits hold everywhere I tried.

Must change

  1. F1 is not fixed on this branch (D5 said it would land here). MCP resources/list, resources/read and resources/subscribe of rm-analyzer://changes (and the GET event stream) still skip the tools check (lib/rm_analyzer_web/notes_mcp.ex, only tools/call uses ApiKeys.allowed?/2). REST /changes returns 403 for the same key, so the two paths disagree. test/rm_analyzer_web/notes_mcp_tool_limit_nye_test.exs fails 0/2 on fc828f8. Fix: gate those resource paths on ApiKeys.allowed?(key, "list_changes") (hide it from resources/list, error on read/subscribe, 403 on the stream).
  2. F2 (low): list-shaped query params crash with 500. q[]=, tag[]= on /documents, first[]= on /documents/:id/pages, q[]= on /search, since[]=/limit[]= on /changes raise FunctionClauseError (String.trim, String.downcase, Integer.parse, Regex.scan). Should be 400 invalid_params. Test: test/rm_analyzer_web/api/read_params_nye_test.exs. A guard is_binary/1 on each param (or one helper that drops non-strings to an error) covers it.

Checked and fine

  • Folder-limited keys: documents, pages, image, search (incl. document_id), folders (parent_id nil'd when unreached), tags, stats, suggestions, changes all filter. A single-document key sees no folders. 404 for unknown vs unreached is byte-identical (no existence oracle). (C8)
  • Existing keys gain nothing: each controller permits on the MCP tool name via ApiKeys.allowed?/2; /sync and /me need none (no library content). Chat tokens refused.
  • Cursors: bound to query fingerprint, other-query cursor refused; ETag round-trip gives 304.
  • No CSRF exposure: the pipeline is Bearer-only with no session fetch.
  • Design differences at the top of docs/api.md (weak body ETags, no image width, 422 for blank page, /tags counts documents only): all acceptable for v1.

Nits (no need to block)

  • Authorization: bearer rma_... (lowercase scheme) gets 401; RFC 9110 says the scheme is case-insensitive. Easy to accept both.
  • path/folder on documents name ancestor folders a limited key doesn't reach. Same as MCP's list_documents today, so not new; worth a line in docs/api.md.
  • /me falls back to the user's email for user.name; fine for the owner's own keys, but mention it in the docs.
Posted by Haystack for Nye (its token can't comment). ## Review of #125 (API v1 read endpoints) — Nye, at fc828f8 **Verdict: changes needed (2), then fine to ship.** CI green; the folder/tool limits hold everywhere I tried. ### Must change 1. **F1 is not fixed on this branch** (D5 said it would land here). MCP `resources/list`, `resources/read` and `resources/subscribe` of `rm-analyzer://changes` (and the GET event stream) still skip the tools check (`lib/rm_analyzer_web/notes_mcp.ex`, only `tools/call` uses `ApiKeys.allowed?/2`). REST `/changes` returns 403 for the same key, so the two paths disagree. `test/rm_analyzer_web/notes_mcp_tool_limit_nye_test.exs` fails 0/2 on fc828f8. Fix: gate those resource paths on `ApiKeys.allowed?(key, "list_changes")` (hide it from resources/list, error on read/subscribe, 403 on the stream). 2. **F2 (low): list-shaped query params crash with 500.** `q[]=`, `tag[]=` on /documents, `first[]=` on /documents/:id/pages, `q[]=` on /search, `since[]=`/`limit[]=` on /changes raise FunctionClauseError (String.trim, String.downcase, Integer.parse, Regex.scan). Should be 400 `invalid_params`. Test: `test/rm_analyzer_web/api/read_params_nye_test.exs`. A guard `is_binary/1` on each param (or one helper that drops non-strings to an error) covers it. ### Checked and fine - Folder-limited keys: documents, pages, image, search (incl. document_id), folders (parent_id nil'd when unreached), tags, stats, suggestions, changes all filter. A single-document key sees no folders. 404 for unknown vs unreached is byte-identical (no existence oracle). (C8) - Existing keys gain nothing: each controller permits on the MCP tool name via `ApiKeys.allowed?/2`; `/sync` and `/me` need none (no library content). Chat tokens refused. - Cursors: bound to query fingerprint, other-query cursor refused; ETag round-trip gives 304. - No CSRF exposure: the pipeline is Bearer-only with no session fetch. - Design differences at the top of docs/api.md (weak body ETags, no image width, 422 for blank page, /tags counts documents only): all acceptable for v1. ### Nits (no need to block) - `Authorization: bearer rma_...` (lowercase scheme) gets 401; RFC 9110 says the scheme is case-insensitive. Easy to accept both. - `path`/`folder` on documents name ancestor folders a limited key doesn't reach. Same as MCP's list_documents today, so not new; worth a line in docs/api.md. - `/me` falls back to the user's email for `user.name`; fine for the owner's own keys, but mention it in the docs.
resources/list, read, subscribe and the GET event stream now go through
ApiKeys.allowed?/2, so a key without list_changes can't read the change
feed another way (finding F1 on #122).
test(api): attack checklist for the read endpoints
All checks were successful
CI / test (pull_request) Successful in 1m58s
f1a70cff22
No existence oracle, no 304 past a folder limit, replayed cursors stay
limited, bad limits and page numbers are 4xx, no CORS or cookies, no
writes on read paths, access changes count from the next request.
Every API query parameter is a single string; a plug refuses lists and
maps (?q[]=a) up front with invalid_params (finding F2 on #122).
fix(api): accept the Bearer scheme in any case
All checks were successful
CI / test (pull_request) Successful in 2m2s
e42d1bda8a
RFC 9110 makes the auth scheme case-insensitive. Notes in docs/api.md on
ancestor folder names, /me's name fallback and list-shaped params.
docs(api): record choices A-H as decided (Dec 3)
All checks were successful
CI / test (pull_request) Successful in 2m15s
a93579eff7
Author
Owner

Posted by Haystack for Nye (its token can't comment).

Re-review of #125 — Nye, at e42d1bd

Verdict: clean.

  • F1 fixed: the MCP rm-analyzer://changes feed now checks list_changes on resources/list, read, subscribe and the GET stream.
  • F2 fixed: list-shaped query parameters (?q[]=) get a 400, not a 500.
  • Bearer scheme accepted in any case; other schemes still 401.
  • 18 extra edge tests on the fixes (C11): no key gets 401 before any 400; keys allowed the change feed still get it.
  • mix precommit passes (1680 tests); CI green.

Head has since moved to a93579e: one docs-only commit (docs/api.md, choices A–H recorded as decided), CI green. Ship report is up for Jeremy.

Posted by Haystack for Nye (its token can't comment). ## Re-review of #125 — Nye, at e42d1bd **Verdict: clean.** - F1 fixed: the MCP `rm-analyzer://changes` feed now checks `list_changes` on resources/list, read, subscribe and the GET stream. - F2 fixed: list-shaped query parameters (`?q[]=`) get a 400, not a 500. - `Bearer` scheme accepted in any case; other schemes still 401. - 18 extra edge tests on the fixes (C11): no key gets 401 before any 400; keys allowed the change feed still get it. - `mix precommit` passes (1680 tests); CI green. Head has since moved to a93579e: one docs-only commit (`docs/api.md`, choices A–H recorded as decided), CI green. Ship report is up for Jeremy.
jfro merged commit d5880494fa into main 2026-12-03 05:29:22 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/rm-analyzer!125
No description provided.