Skip to content

Skepsis as a library: host the review page in another server - #124

Merged
david-crespo merged 6 commits into
spr/run-jj-and-git-in-the-diffs-directoryfrom
spr/skepsis-as-a-library-host-the-review-page-in-another-server
Nov 19, 2026
Merged

david-crespo merged 6 commits into
spr/run-jj-and-git-in-the-diffs-directoryfrom
spr/skepsis-as-a-library-host-the-review-page-in-another-server

Conversation

@david-crespo

@david-crespo david-crespo commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Built on #123, this PR lets another Node server mount the review page under a URL prefix, for any number of diffs in one process. @oxide/skepsis now exports createApp, which returns a fetch handler and the token. The README has a new Embedding section. The library API may change in a minor version while the package is 0.x.

Of about 920 changed lines, about 510 are tests, including a 90-line refactor of the Playwright fixture that changes no test. Source is about 260 lines, the README and CLAUDE.md about 95, and build and CI config about 55.

Choosing the diff

A caller tells createApp which diff to show with the same options the CLI has: revisions for -r, or from and to for -f and -t. createApp works out the jj or git command from those, as the CLI does.

const app = await createApp({
  cwd,
  revisions: 'abc',
  send: (payload) => deliver(payload.prompt),
})

createApp rejects a cwd below the jj workspace or git worktree root. Both jj and git print diff paths relative to the root, whereas the server reads working-copy files and writes review comments by joining those paths onto cwd (with cwd set to <repo>/sub, sub/f.txt becomes <repo>/sub/sub/f.txt). The CLI started from a subdirectory has the same mismatch, which this PR doesn't change.

send is a function

The CLI's --send takes a shell command, but createApp's send takes only a function. A shell command would inherit the host's environment, including a SKEPSIS_URL that isn't the page's. So the CLI's server, which knows its own URL, now turns its --send command into a function before building the app (server/main.ts):

const send = command
  ? (payload: SendPayload) => {
      const env = { SKEPSIS_URL: serverUrl, SKEPSIS_ROOT: cwd }
      return runSend(command, payload, { cwd, env })
    }
  : null
const app = await buildApp({ ...opts, send })

The guard

The page's API (the /api/... routes the app serves) reads and writes files in the repository and calls send, so something has to decide which requests to those routes are allowed. The CLI's server does that with a per-run token that only the page it opened has, plus Host and Origin checks on /api/send. This PR calls those checks the guard, and createApp has them on by default.

With the guard on, the host puts the token in the page's URL as #token=${app.token}. Send only works when the host is reached on loopback over plain http, because of the Host and Origin checks. Each createApp call makes a new token, so the host has to keep the app for as long as its page may be open.

A host that already decides who may reach the mounted page, with its own login or signed URLs for example, can pass guard: false to turn the token and the Host and Origin checks off. Any other host should leave the guard on. The README's Embedding section covers both cases.

One small change for CLI users

The page's asset and API URLs are now relative, so it works at any path ending in /. For CLI users, the one change is that a path with no file returns 404 where the server used to return index.html.

Tests

A Playwright test mounts the built package at /some/prefix in a stand-in host with the guard off. It checks that the diff renders, the theme script and highlighter worker load, Send reaches the host's function, viewed state survives a reload, no request leaves the prefix, and an unknown path is a 404. A second case leaves the guard on and checks that the page works from the URL with the token, still works on the bare URL after a reload, can Send, and that the API returns 401 without the token.

The host passes requests on with new Request(url, request), which fails on Node 26.11 or later with @hono/node-server before 2.1.8 (honojs/node-server#408), so the dev dependency's minimum is raised. CI now builds before the type-check so the test's package import resolves.

@david-crespo
david-crespo added this pull request to stack #125 November 19, 2026 17:58
david-crespo added a commit that referenced this pull request Nov 19, 2026
`/api/file-contents` and the two comment routes take a file path from the request and read or write that file under the repository directory. They didn't check the path, so one starting with `../` reached files outside the repository. `GET /api/file-contents?path=../outside.txt` returned `outside.txt`, and the comment routes could rewrite it. These routes are behind the per-run token, so only the page could do this. It matters more for the `createApp` export stacked on this PR (#124), where a host application can turn the token off and check requests itself.

The routes now accept only a path in the latest diff, or a rename's old path, and return 400 otherwise. That also blocks files inside the repository that the diff doesn't show, such as an ignored `.env`. When a path isn't in the last diff, the server runs the diff once more before refusing, so a file that just changed isn't rejected.

The 71 changed lines are 36 for the check and 35 in tests. We checked that `/api/file-contents` returns 400 for a file outside the repository and for an unchanged file inside it, and that both comment routes return 400 for the outside file and leave it unchanged. Both content requests returned 200 before the fix. The git test fixture now runs `git add --intent-to-add`, because two existing tests request untracked files, which `git diff` leaves out.

Reviewers: 

Pull Request: #121
With guard off, the app issues no token and /api/send skips its Host and Origin check. This is for a host application that checks requests before passing them on. The CLI's server always has it on.
buildApp now takes send as a function that delivers a payload. startServer wraps the --send command in one and gives it SKEPSIS_URL and SKEPSIS_ROOT, so the app no longer needs its own address or a shell.
The package now exports createApp from lib.ts, built to dist/lib.js and dist/lib.d.ts. It builds the diff source from the CLI's revision options, takes send as a function with a timeout, and rejects a cwd below the repository root, where root-relative diff paths and cwd-relative file paths differ.
The mounted-page test in a later commit starts its own host process, so it needs the temp repo and the browser without the CLI server.
Fixes "Illegal invocation" from `new Request(url, request)` on Node 26.11 or later (honojs/node-server#408), which is how a host passes a request on to a mounted app.
The page's asset and API URLs are now relative, so it works at any path ending in /. Paths with no file get a 404 in place of index.html, which could not load its assets from there. The stored token is keyed by path, so pages at different paths on one origin each keep their own.

A browser test mounts the built package under a prefix in a stand-in host. CI now builds before the type-check so the package import resolves.
@david-crespo
david-crespo force-pushed the spr/skepsis-as-a-library-host-the-review-page-in-another-server branch from 9fd0fef to 8020445 Compare November 19, 2026 18:20
@david-crespo
david-crespo merged commit 599fd63 into main Nov 19, 2026
2 checks passed
@david-crespo
david-crespo deleted the spr/skepsis-as-a-library-host-the-review-page-in-another-server branch November 19, 2026 18:43
@github-actions

Copy link
Copy Markdown

Canary published: npx @oxide/[email protected]

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.

1 participant