Skip to content

feat(cli): work on the running app or server instead of a second engine - #442

Merged
linroid merged 2 commits into
mainfrom
feat/cli-running-instances
Oct 10, 2026
Merged

linroid merged 2 commits into
mainfrom
feat/cli-running-instances

Conversation

@linroid

@linroid linroid commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Implements the Command line for running devices roadmap item (AGENTS.md roadmap 15).

What changed

New attaching commands (cli): ketch add, list, pause, resume and watch work on the downloads of the Ketch that is already running, through RemoteKetch, instead of opening ketch.db with an engine of their own. InstanceLocator picks, in order:

  1. --server <url> / KETCH_SERVER (with --token / KETCH_API_TOKEN)
  2. the desktop app, asked over its SingleInstance endpoint (cli/connect) for the loopback API server it already runs for the browser extension (BrowserExtensionServer, renamed LocalApiServer)
  3. the ketch server or ketch mcp --standalone that instance.json names, if its pid is alive
  • add prints the task ID (--json for the task), resolves relative destinations against the cwd for a local instance, sends a requestId and retries connection failures/5xx with it. --idempotency-key makes retries safe across runs (a UUID as-is, other text → name-based UUID).
  • watch prints NDJSON (snapshot, added, state, progress, removed); with IDs it exits once they finish (0 all completed, 1 otherwise), and exits 1 when the connection is lost.
  • Task IDs accept unique prefixes. Results go to stdout, everything else to stderr; exit codes 0/1/2.

ketch mcp now attaches the same way (InstanceConnection): it finds the instance when a tool first needs it and again once the connection is lost (e.g. the app restarts on a new port). KetchToolSet/KetchMcpServer take a connect: suspend () -> KetchApi provider, asked per call. startDownload gains an optional requestId. --standalone keeps the old own-engine behaviour, now with a loopback API the attaching commands use.

One engine per database: ketch server and ketch mcp --standalone refuse to start while the desktop app runs or another holds instance.lock, and publish an owner-only instance.json (command, pid, loopback URL, token).

Fixes found while testing the native binary

  • Idempotency vs. save folders: on a token-less server, a request re-sent with its ID was refused as a different request once its file existed (name (1)). DestinationGuard now keeps the first destination when it names the same place (DestinationPathPolicy.confinesTo).
  • kotlin-reflect in the native image: it was excluded from runtimeClasspath but not nativeImageClasspath, so Ktor StatusPages crashed (Built-in class kotlin.Any is not found) and every 4xx from the native server became a 500. Now excluded from both; binary 92 MB → 79 MB.
  • Native shutdown: added --install-exit-handlers so SIGINT/SIGTERM run shutdown hooks (the server now pauses downloads and removes instance.json).

Reviewer notes

  • Breaking: ketch mcp no longer runs its own engine by default; --dir/--config now require --standalone, so MCP client configs passing --dir fail to start until updated.
  • The CLI implements the client side of SingleInstance's framing itself (DesktopApp); both sides note to stay in step.
  • Known gap (AGENTS.md limitation 9): the desktop app still doesn't check for a CLI engine already running the same downloads.
  • Docs: cli/README.md (new "Work on a running Ketch" section, MCP, server), AGENTS.md, and all six READMEs (roadmap entry removed, feature line added).

Testing

  • ./gradlew :cli:test :library:mcp:test :library:server:test :library:core:jvmTest :library:remote:jvmTest :app:desktop:test -PprebuiltWebDir=/nonexistent passes, including an end-to-end AttachedCommandsTest against a real loopback KetchServer.
  • Smoke-tested installDist and :cli:nativeCompile binaries against an isolated ketch server (add/re-add with key, list, pause/resume, watch, mcp attach, --standalone refusal, second-server refusal, SIGTERM cleanup).
  • Against a real, older desktop app: ketch list reports it must be updated; ketch server refuses to start.

Add `ketch add`, `list`, `pause`, `resume` and `watch`, which attach through
RemoteKetch to the Ketch that runs the downloads: `--server`/KETCH_SERVER, else
the desktop app (asked over its SingleInstance endpoint for its loopback API
server, `cli/connect`), else the `ketch server` this machine runs. `add` sends a
request ID and retries with it, and `--idempotency-key` keeps retried
submissions from adding a download twice. `watch` prints task changes as JSON
lines.

`ketch mcp` attaches the same way, finding the instance when a tool first needs
it and again after losing it; `--standalone` keeps the old engine of its own.
`ketch server` and `ketch mcp --standalone` refuse to open ketch.db while the
desktop app or another one runs it (instance.lock), and describe themselves in
an owner-only instance.json for the attaching commands.

Also fixes, found while testing the native binary:
- a request sent again with its ID to a token-less server kept failing as a
  different request once its file existed; the guard now keeps the first
  destination (DestinationPathPolicy.confinesTo)
- kotlin-reflect was still compiled into the native image, whose StatusPages
  then turned every 4xx into a 500
- the native binary skipped shutdown hooks on SIGINT and SIGTERM

BrowserExtensionServer is now LocalApiServer, as it serves the CLI too.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T00:28:52.432142Z 334fe98 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 334fe98366

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

`ketch server` and `ketch mcp --standalone` published their endpoint before
restoring tasks, so a client that found them right away could see an empty
list. Worse, Ketch.start replaced the task list with the stored snapshot, so
a download added while the records loaded, through any server that already
listens, vanished from the list while it still ran. Start now keeps such
downloads, and the endpoint is published after restoration.

The MCP startDownload tool refuses a requestId when the instance lacks
KetchFeatures.REQUEST_ID, which an older server would drop and so add the
download again on a retry.
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 419 files  +11   1 446 suites  +11   9m 30s ⏱️ +32s
 9 397 tests +66   9 168 ✅ +66  229 💤 ±0  0 ❌ ±0 
10 796 runs  +66  10 567 ✅ +66  229 💤 ±0  0 ❌ ±0 

Results for commit 7ecf25d. ± Comparison against base commit bbd62d6.

This pull request removes 8 and adds 74 tests. Note that renamed tests count towards both.
com.linroid.ketch.app.desktop.BrowserExtensionServerTest ‑ close_stopsTheServer
com.linroid.ketch.app.desktop.BrowserExtensionServerTest ‑ connect_afterTheServerFailedToStart_triesAgain
com.linroid.ketch.app.desktop.BrowserExtensionServerTest ‑ connect_beforeTheAppIsReady_waitsForIt
com.linroid.ketch.app.desktop.BrowserExtensionServerTest ‑ connect_eachTimeTheExtensionConnects_reportsIt
com.linroid.ketch.app.desktop.BrowserExtensionServerTest ‑ connect_reportingTheConnectionFails_stillReplies
com.linroid.ketch.app.desktop.BrowserExtensionServerTest ‑ connect_startsTheServerOnceAndRepliesWithItsAddressAndToken
com.linroid.ketch.app.desktop.BrowserExtensionServerTest ‑ connect_whenTheAppNeverGetsReady_repliesWithAnError
com.linroid.ketch.app.desktop.BrowserExtensionServerTest ‑ connect_whenTheServerFails_reportsNoConnection
com.linroid.ketch.app.desktop.LocalApiServerTest ‑ close_stopsTheServer
com.linroid.ketch.app.desktop.LocalApiServerTest ‑ connect_afterTheServerFailedToStart_triesAgain
com.linroid.ketch.app.desktop.LocalApiServerTest ‑ connect_beforeTheAppIsReady_waitsForIt
com.linroid.ketch.app.desktop.LocalApiServerTest ‑ connect_eachTimeTheExtensionConnects_reportsIt
com.linroid.ketch.app.desktop.LocalApiServerTest ‑ connect_fromTheCommandLine_repliesWithoutReportingAnExtension
com.linroid.ketch.app.desktop.LocalApiServerTest ‑ connect_reportingTheConnectionFails_stillReplies
com.linroid.ketch.app.desktop.LocalApiServerTest ‑ connect_startsTheServerOnceAndRepliesWithItsAddressAndToken
com.linroid.ketch.app.desktop.LocalApiServerTest ‑ connect_whenTheAppNeverGetsReady_repliesWithAnError
com.linroid.ketch.app.desktop.LocalApiServerTest ‑ connect_whenTheServerFails_reportsNoConnection
com.linroid.ketch.cli.AttachedCommandsTest ‑ a wrong token is reported as such
…

@linroid
linroid merged commit 183658f into main Oct 10, 2026
13 checks passed
@linroid
linroid deleted the feat/cli-running-instances branch October 10, 2026 01:38
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