Skip to content

feat(desktop): leave the downloads to a running ketch server instead of opening ketch.db too - #448

Merged
linroid merged 1 commit into
mainfrom
feat/desktop-instance-lock
Oct 10, 2026
Merged

linroid merged 1 commit into
mainfrom
feat/desktop-instance-lock

Conversation

@linroid

@linroid linroid commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Why

Since #442, ketch server and ketch mcp --standalone refuse to start while the desktop app runs. The other direction was still open (AGENTS.md limitation 9): the app never checked instance.lock, so opening it while a ketch server ran meant two engines resuming the same tasks into the same files.

What changed

The app takes the lock (DownloadsLock). It acquires instance.lock before it opens ketch.db and holds it until the process exits, not just until the engine closes, because Ketch.close() only cancels and segments can still save afterwards. When it gets the lock, it deletes any instance.json left by a command that stopped.

When a command already holds the lock, the app starts without an engine of its own and never opens ketch.db. I chose this over a blocking dialog or quitting, so a start is never blocked (the same approach as unreadable config.toml/ketch.db), and so a browser extension background launch doesn't pop up a dialog.

  • Device: the command's API, read from instance.json, becomes a device named after the command (e.g. "ketch server"). It is listed first, shown at launch and never saved to config.toml. This is a new standIn parameter on InstanceManager. If a saved device already has that address, that device is used instead and stays saved.
  • Token safety: the app only connects, and only sends the token, when the URL is http on a loopback address or one of this machine's interfaces, and the pid is alive. If the command has just taken the lock, the app waits up to 3 s for it to publish its address.
  • Local API: LocalApiServer serves a client of that server, so the browser extension and ketch add/list/mcp still reach the downloads through the app. They always get a token this way; the extension requires one, and a default loopback ketch server has none. When the app can't reach the command, LocalApiServer refuses at once with unavailable instead of making clients wait ~30 s on not_ready.
  • Banners: an info banner names the command and its pid and says to stop it, then quit and reopen Ketch. When that process exits (ProcessHandle.onExit), a second banner says it stopped. The new strings are in all 10 desktop languages.

CLI. claimDownloads now tries the lock before asking the app (otherEngine). Without this, a second ketch server started while the app shows a running server's downloads would wrongly blame the app; now it names the server holding the lock. An app that is running still blocks the CLI even when the CLI gets the lock, since an app from before this change runs its engine without the lock.

Docs. AGENTS.md drops limitation 9 and describes both directions. cli/README.md is updated in its server, --standalone and Database sections.

Notes for review

  • In stand-in mode the app only runs downloads itself once it is reopened. It doesn't take over in-process: InstanceManager creates its embedded engine at construction.
  • Starting ketch server again while a stand-in app is still open is refused ("the Ketch app is running"). The CLI can't tell that app from one that runs an engine without the lock.
  • If ketch mcp --standalone restarts (new port and token), the app's device is stale until the app is reopened.
  • Keep-awake follows only the embedded engine, so it stays off in stand-in mode.
  • With the server gone, ketch list through the app reports the Ketch app refused the request: Connection refused.

Testing

  • ./gradlew :app:desktop:test :cli:test :app:shared:jvmTest -PprebuiltWebDir=/nonexistent passes on top of main.
  • New tests:
    • DownloadsLockTest: claiming the lock, clearing a stale info file, waiting for a starting command, a holder that stopped, address validation.
    • LocalApiServerTest: refuse, and closing only the client it owns.
    • InstanceManagerStandInTest: listing and selection, never saved, a saved device at the same address, removal.
    • CliInstanceTest: otherEngine.
  • End to end, with a real ketch server and the desktop app in a scratch user.home:
    • The app started without opening ketch.db and connected.
    • ketch list and ketch add went through the app to the server.
    • A second ketch server named the first one.
    • The app logged the server's exit.
    • Restarted normally, the app held the lock (checked from a separate process with fcntl), removed a planted stale instance.json, and ketch server was refused.
  • I haven't seen the banners rendered on screen.

…of opening ketch.db too

The desktop app now takes instance.lock before it opens ketch.db and keeps it
until its process exits, so `ketch server` and `ketch mcp --standalone` cannot
run a second engine on the same downloads. It removes an instance.json a stopped
command left behind (DownloadsLock).

When one of those commands already holds the lock, the app starts without an
engine of its own and never opens ketch.db:
- the command's API, read from instance.json and used only at an http address
  of this machine, is a device named after the command, listed first, shown at
  launch and never saved (InstanceManager.standIn)
- LocalApiServer serves a client of it, so the browser extension and the
  `ketch` commands still reach the downloads through the app, with a token even
  when a loopback `ketch server` needs none; it refuses clients at once when
  the app cannot reach the command
- banners name the command and its pid, and say when it stops that the app runs
  the downloads once it is opened again

`ketch server` and `ketch mcp --standalone` now take the lock before they ask
for the app, so a second one started while the app shows a running server's
downloads names that server instead of the app (otherEngine).

Removes limitation 9 from AGENTS.md.
@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-10T01:52:19.261635Z 99e22cd 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: 99e22cd438

ℹ️ 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".

Comment on lines +84 to +85
while (true) {
val engine = read(dir)?.takeIf { isAlive(it.pid) }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Retry the lock before declaring an unknown owner

When the desktop app and a standalone command start concurrently, the command can acquire this lock while the app has already published its SingleInstance endpoint. The app's single tryLock() then fails, the command sees the endpoint and exits while releasing the lock, but this loop only polls instance.json and never retries the now-free lock. After three seconds the app therefore returns Taken(null), leaving neither process running the downloads until the user quits and reopens the app.

Useful? React with 👍 / 👎.

Comment on lines +407 to +408
val existing = entryOf("${config.host}:${config.port}")
if (existing is RemoteInstance) return existing

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Use live stand-in credentials for duplicate addresses

When a saved remote already has the same host and port as the command described by instance.json, this early return discards the live stand-in configuration. If the command was started with a different token, or the saved entry uses different TLS settings, the UI constructs its client with the stale saved credentials and cannot show the downloads even though the app discovered the correct connection details; preserve the saved entry for persistence while using the stand-in's current transport credentials.

Useful? React with 👍 / 👎.

@linroid
linroid enabled auto-merge (squash) October 10, 2026 01:53
@linroid
linroid merged commit 3e2cdd0 into main Oct 10, 2026
9 of 10 checks passed
@linroid
linroid deleted the feat/desktop-instance-lock branch October 10, 2026 01:53
linroid added a commit that referenced this pull request Oct 14, 2026
…g server's own token (#555)

Follow-up to #448, from its review:
- DownloadsLock.claim tries the lock again while it waits for the holder to
  describe itself. A `ketch server` started together with the app can take the
  lock, then quit on finding the app running; the app used to give up after
  three seconds with an unknown holder, leaving no engine on the downloads until
  it was reopened.
- A saved device at the address of the `ketch server` that stands in for the
  app's engine connects with the token and scheme from instance.json, which it
  then saves, instead of the ones saved before, which the server may no longer
  accept.
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