Repository navigation
feat(desktop): leave the downloads to a running ketch server instead of opening ketch.db too - #448
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| while (true) { | ||
| val engine = read(dir)?.takeIf { isAlive(it.pid) } |
There was a problem hiding this comment.
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 👍 / 👎.
| val existing = entryOf("${config.host}:${config.port}") | ||
| if (existing is RemoteInstance) return existing |
There was a problem hiding this comment.
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 👍 / 👎.
…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.
Why
Since #442,
ketch serverandketch mcp --standalonerefuse to start while the desktop app runs. The other direction was still open (AGENTS.md limitation 9): the app never checkedinstance.lock, so opening it while aketch serverran meant two engines resuming the same tasks into the same files.What changed
The app takes the lock (
DownloadsLock). It acquiresinstance.lockbefore it opensketch.dband holds it until the process exits, not just until the engine closes, becauseKetch.close()only cancels and segments can still save afterwards. When it gets the lock, it deletes anyinstance.jsonleft 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 unreadableconfig.toml/ketch.db), and so a browser extension background launch doesn't pop up a dialog.instance.json, becomes a device named after the command (e.g. "ketch server"). It is listed first, shown at launch and never saved toconfig.toml. This is a newstandInparameter onInstanceManager. If a saved device already has that address, that device is used instead and stays saved.httpon 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.LocalApiServerserves a client of that server, so the browser extension andketch add/list/mcpstill reach the downloads through the app. They always get a token this way; the extension requires one, and a default loopbackketch serverhas none. When the app can't reach the command,LocalApiServerrefuses at once withunavailableinstead of making clients wait ~30 s onnot_ready.ProcessHandle.onExit), a second banner says it stopped. The new strings are in all 10 desktop languages.CLI.
claimDownloadsnow tries the lock before asking the app (otherEngine). Without this, a secondketch serverstarted 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.mdis updated in its server,--standaloneand Database sections.Notes for review
InstanceManagercreates its embedded engine at construction.ketch serveragain 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.ketch mcp --standalonerestarts (new port and token), the app's device is stale until the app is reopened.ketch listthrough the app reportsthe Ketch app refused the request: Connection refused.Testing
./gradlew :app:desktop:test :cli:test :app:shared:jvmTest -PprebuiltWebDir=/nonexistentpasses on top of main.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.ketch serverand the desktop app in a scratchuser.home:ketch.dband connected.ketch listandketch addwent through the app to the server.ketch servernamed the first one.fcntl), removed a planted staleinstance.json, andketch serverwas refused.