feat(cli): pick the app of the project the launcher belongs to

Discovery is per user, so every project's launcher sees every debug app
running, and with two of them up every command refused to guess and
asked for -i, though the agent in one project meant its own app. Two
copies of the example, both named basic, could only be told apart by
id.

The launcher now exports CRAFTER_DEBUG_PROJECT, its project's directory
(flatpak-spawn gets it spelled out), and the client counts an app as
the project's when its executable lies under it, as `-r` runs it from
<project>/bin/<variant>/. With other projects' apps running, commands
pick this project's; two copies of it still need -i. Another project's
app is never picked by default: with this project's app not up yet, or
crashed, the agent would otherwise drive, or quit, someone else's. It
is listed instead, for -i.

`ls` lists this project's apps first, marked, `--json` says which are,
and `--wait` waits for one of them rather than any app; the exit code
says whether one runs. A client started without the launcher has no
project and picks the one app running, as before, and the command the
protocol check prints for an older app's own client now names it with
-i.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-11-01 18:33:59 +01:00
commit 2217037d72
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
5 changed files with 126 additions and 16 deletions

View file

@ -80,17 +80,24 @@ namespace Crafter::Debug {
// against; from another app's sandbox it goes through flatpak-spawn.
// `--die-with-parent` ends the client when whatever started the launcher
// is killed, a timed-out `wait` or a Monitor-stopped `log --follow`.
std::string LauncherText(const fs::path& client, const Sandbox& sandbox, std::string_view flag) {
//
// It tells the client which project it belongs to: with apps of other
// projects running too, the client picks this project's and leaves the
// others to -i. flatpak run passes the environment on; flatpak-spawn
// needs it spelled out.
std::string LauncherText(const fs::path& client, const Sandbox& sandbox, std::string_view flag, const fs::path& project) {
std::string text = std::format(
"#!/bin/sh\n"
"# Written by Crafter.Debug (Debug::Use) each time crafter-build loads this\n"
"# project with {0}: runs the crafter-debug client that build compiled.\n"
"client={1}\n"
"CRAFTER_DEBUG_PROJECT={2}\n"
"export CRAFTER_DEBUG_PROJECT\n"
"if [ ! -x \"$client\" ]; then\n"
" echo \"crafter-debug: the client is not built yet; let crafter-build {0} finish first, which builds it with the app\" >&2\n"
" exit 2\n"
"fi\n",
flag, ShellQuote(client.string()));
flag, ShellQuote(client.string()), ShellQuote(project.string()));
if (sandbox.app.empty()) {
text += "exec \"$client\" \"$@\"\n";
return text;
@ -100,7 +107,7 @@ namespace Crafter::Debug {
text += std::format(
"if grep -qsx {0} /.flatpak-info; then exec \"$client\" \"$@\"; fi\n"
"if command -v flatpak >/dev/null 2>&1; then exec {1}; fi\n"
"if command -v flatpak-spawn >/dev/null 2>&1; then exec flatpak-spawn --host {1}; fi\n"
"if command -v flatpak-spawn >/dev/null 2>&1; then exec flatpak-spawn --host --env=CRAFTER_DEBUG_PROJECT=\"$CRAFTER_DEBUG_PROJECT\" {1}; fi\n"
"echo \"crafter-debug: needs flatpak to start $client\" >&2\n"
"exit 1\n",
ShellQuote(std::format("name={}", sandbox.app)), run);
@ -189,7 +196,7 @@ export namespace Crafter::Debug {
if (client == library->dependencies.end()) {
throw std::runtime_error(std::format("Crafter.Debug: {} declares no crafter.debug-cli client", (Checkout() / "project.cpp").string()));
}
WriteIfChanged(launcher, LauncherText((*client)->BinDir() / (*client)->outputName, CurrentSandbox(), options.flag));
WriteIfChanged(launcher, LauncherText((*client)->BinDir() / (*client)->outputName, CurrentSandbox(), options.flag, launcher.parent_path().parent_path()));
return true;
}
}