Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-8029 fix(daemon): treat a DSH profile without the runtime as missing (#9287)
2penheimerbbf70d125dae
diff --git a/server/internal/daemon/agents_probe.go b/server/internal/daemon/agents_probe.goindex 6e596d96bb..3393e30a3a 100644--- a/server/internal/daemon/agents_probe.go+++ b/server/internal/daemon/agents_probe.go@@ -389,9 +389,9 @@ type dshProbeVerdict intconst (// dshProbeOK: the profile answered with the protocol version this backend drives.dshProbeOK dshProbeVerdict = iota- // dshProbeMissingProfile: the probe ran and no profile manifest is present,- // which is the same fact DSH itself checks before refusing to boot. This is- // the only verdict that may install a bundle or condemn the runtime.+ // dshProbeMissingProfile: the manifest does not name the Multica runtime.+ // A file that merely exists is not enough — a refused install leaves one.+ // This is the only verdict that may install a bundle or condemn the runtime.dshProbeMissingProfile// dshProbeIncompatible: something answered, with a protocol this daemon does// not drive. The profile is installed, so installing over it would replace a@@ -411,10 +411,10 @@ const (// cost is paid only by a probe that has nothing to say, and it stays well// inside the daemon's own 45s startup window.//-// A missing profile does spend the whole budget here: DSH does not refuse the-// probe, it hangs. That is bounded by construction — the state ends the moment-// someone installs the profile — and the verdict below no longer depends on-// telling that hang apart from a busy machine.+// A manifest that does not name the runtime returns before this budget.+// Probing that state hangs, and the manifest is enough to call it missing.+// The budget is spent only when a manifest that does name the runtime+// produces no frame.//// A var so tests can shrink it instead of waiting out a real timeout.var dshProbeTimeout = 15 * time.Second@@ -440,6 +440,18 @@ func parseDshProbeFrame(output string) (dshProbeFrame, bool) {// whole DSH process, and a round abandoned by a shutting-down daemon should not// go on holding one for the rest of the timeout — once per retry, per provider.func probeDshMulticaProfile(ctx context.Context, executablePath string) dshProbeVerdict {+ // A cancelled round says nothing about the profile, and it must be decided+ // before the manifest: a shutdown is not evidence the profile is missing.+ if ctx.Err() != nil {+ return dshProbeUnavailable+ }+ // A manifest that does not name the runtime is missing. Decide that before+ // starting dsh: on current DSH, --probe against the skeleton a refused+ // install leaves hangs until this timeout, and the file is already the answer.+ if !dshMulticaProfilePresent() {+ return dshProbeMissingProfile+ }+parent := ctxctx, cancel := context.WithTimeout(parent, dshProbeTimeout)defer cancel()diff --git a/server/internal/daemon/agents_probe_dsh_test.go b/server/internal/daemon/agents_probe_dsh_test.goindex e1472cbcdf..298bf466a4 100644--- a/server/internal/daemon/agents_probe_dsh_test.go+++ b/server/internal/daemon/agents_probe_dsh_test.go@@ -32,7 +32,7 @@ func dshProbeFixture(t *testing.T, body string, manifest bool) string {if err := os.MkdirAll(dir, 0o755); err != nil {t.Fatal(err)}- if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte("{}\n"), 0o644); err != nil {+ if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(installedDshProfileManifest()), 0o644); err != nil {t.Fatal(err)}}@@ -143,6 +143,67 @@ func TestProbeDshMulticaProfile(t *testing.T) {}})}++ // A manifest that does not name the runtime is missing. Probing it is what+ // hangs, so the classification must not start dsh at all.+ t.Run("unreferenced manifest is missing without probing", func(t *testing.T) {+ manifests := []string{"{}\n", "{\"dependencies\":{}}\n", dshSkeletonProfileManifest()}+ for _, manifest := range manifests {+ home := t.TempDir()+ t.Setenv("DSH_HOME", home)+ dir := filepath.Join(home, "profiles", dshMulticaProfileName)+ if err := os.MkdirAll(dir, 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(manifest), 0o644); err != nil {+ t.Fatal(err)+ }+ marker := filepath.Join(t.TempDir(), "ran")+ t.Setenv("DSH_PROBE_MARKER", marker)+ path := filepath.Join(t.TempDir(), "dsh")+ script := "#!/bin/sh\ncase \"$*\" in\n *--probe*) touch \"$DSH_PROBE_MARKER\"; exec sleep 30 ;;\n *) printf '%s\\n' '0.1.2-rc.1' ;;\nesac\n"+ if err := os.WriteFile(path, []byte(script), 0o755); err != nil {+ t.Fatal(err)+ }+ start := time.Now()+ if got := probeDshMulticaProfile(context.Background(), path); got != dshProbeMissingProfile {+ t.Fatalf("probeDshMulticaProfile() = %v, want missing", got)+ }+ if time.Since(start) > time.Second {+ t.Fatal("an unreferenced manifest still ran the probe")+ }+ if _, err := os.Stat(marker); err == nil {+ t.Fatal("dsh --probe ran for a manifest that does not name the runtime")+ }+ }+ })++ // A file that cannot be parsed is not evidence the runtime is absent, so a+ // failed probe over it stays transient and the probe does run.+ t.Run("unparseable manifest still probes", func(t *testing.T) {+ home := t.TempDir()+ t.Setenv("DSH_HOME", home)+ dir := filepath.Join(home, "profiles", dshMulticaProfileName)+ if err := os.MkdirAll(dir, 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte("{\n"), 0o644); err != nil {+ t.Fatal(err)+ }+ marker := filepath.Join(t.TempDir(), "ran")+ t.Setenv("DSH_PROBE_MARKER", marker)+ path := filepath.Join(t.TempDir(), "dsh")+ script := "#!/bin/sh\ncase \"$*\" in\n *--probe*) touch \"$DSH_PROBE_MARKER\"; exit 0 ;;\n *) printf '%s\\n' '0.1.2-rc.1' ;;\nesac\n"+ if err := os.WriteFile(path, []byte(script), 0o755); err != nil {+ t.Fatal(err)+ }+ if got := probeDshMulticaProfile(context.Background(), path); got != dshProbeUnavailable {+ t.Fatalf("probeDshMulticaProfile() = %v, want unavailable", got)+ }+ if _, err := os.Stat(marker); err != nil {+ t.Fatal("dsh --probe did not run for an unparseable manifest")+ }+ })}// The four ways a probe can fail while the profile IS installed must notdiff --git a/server/internal/daemon/agents_refresh_test.go b/server/internal/daemon/agents_refresh_test.goindex 469ab515a0..15a7c5363d 100644--- a/server/internal/daemon/agents_refresh_test.go+++ b/server/internal/daemon/agents_refresh_test.go@@ -8,8 +8,6 @@ import ("log/slog""net/http""net/http/httptest"- "os"- "path/filepath""sort""sync""testing"@@ -119,13 +117,7 @@ func TestDshRuntimeProfileMismatch(t *testing.T) {}// Installing the profile makes the two agree.- dir := filepath.Join(home, "profiles", dshMulticaProfileName)- if err := os.MkdirAll(dir, 0o755); err != nil {- t.Fatal(err)- }- if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte("{}\n"), 0o644); err != nil {- t.Fatal(err)- }+ installMulticaProfile(t, home)if d.dshRuntimeProfileMismatch() != dshMismatchNone {t.Fatal("a registered dsh with its profile installed was judged inconsistent")}diff --git a/server/internal/daemon/dsh_profile.go b/server/internal/daemon/dsh_profile.goindex c0afdc7dee..6de0cb66ae 100644--- a/server/internal/daemon/dsh_profile.go+++ b/server/internal/daemon/dsh_profile.go@@ -2,6 +2,7 @@ package daemonimport ("context"+ "encoding/json""fmt""log/slog""os"@@ -54,27 +55,69 @@ const (// dshMulticaProfileName is the profile the backend launches. Kept beside// the install command so the two cannot drift.dshMulticaProfileName = "multica"++ // dshMulticaRuntimePackage is the bridge a multica profile has to name+ // before this daemon will treat the profile as installed. `dsh plugin add`+ // records that name in dependencies and, when the package is a bundle, in+ // dsh.profile.bundles — including installs from git, a directory, or a tarball.+ dshMulticaRuntimePackage = "@multica-ai/dsh-runtime")+// dshProfileManifest is the slice of a profile package.json this daemon reads.+// Dependency values are left raw: a string version and an object spec are both+// a named dependency, and a stricter type would make a real install look absent.+type dshProfileManifest struct {+ Dependencies map[string]json.RawMessage `json:"dependencies"`+ DSH struct {+ Profile struct {+ Bundles []string `json:"bundles"`+ } `json:"profile"`+ } `json:"dsh"`+}+// dshProvisionTimeout bounds the whole install. It is a package manager// talking to a registry, so it is generous next to the CLI probes.const dshProvisionTimeout = 3 * time.Minute// dshMulticaProfilePresent reports whether the `multica` profile is installed.//-// A stat, deliberately. This is the cheap change detector the discovery loop-// runs on every tick, and both things that change the answer — `dsh plugin-// --profile multica add` and a user removing the directory — create or remove-// exactly this file. DSH decides a profile exists the same way: dsh-app-boot's-// loadProfile falls back to a built-in template, or fails, when the manifest is-// missing.+// Installed means the manifest names the Multica runtime, not that the file+// exists. A refused `dsh plugin add` leaves package.json behind with empty+// dependencies and only DSH's template bundle; a stat of that file made every+// later probe look transient and blocked the automatic install.+//+// A file that cannot be read or parsed still counts as installed. The daemon+// cannot tell a broken manifest from one it should not overwrite, and guessing+// "missing" is what starts an install.//// The authoritative question — "does --probe actually succeed?" — costs booting-// a whole DSH process, so it stays in the probe round this signal forces.+// a whole DSH process, so it stays in the probe round this signal forces. This+// read is the cheap check the discovery loop runs on every tick.func dshMulticaProfilePresent() bool {+ path := dshMulticaProfileManifestPath()+ if path == "" {+ return false+ }+ data, err := os.ReadFile(path)+ if err != nil {+ // Missing is the only negative answer. A permission error or a file+ // that is not a regular file stays "installed": this daemon will not+ // treat something it cannot read as absent.+ return !os.IsNotExist(err)+ }+ var manifest dshProfileManifest+ if json.Unmarshal(data, &manifest) != nil {+ return true+ }+ return dshProfileNamesRuntime(manifest)+}++// dshMulticaProfileManifestPath is <DSH_HOME>/profiles/multica/package.json.+// An empty result means the home directory could not be resolved.+func dshMulticaProfileManifestPath() string {home, err := os.UserHomeDir()if err != nil {- return false+ return ""}// DSH_HOME is DSH's own override, and the profile store lives under it// either way. Same resolution local_skills.go uses to find ~/.dsh/skills.@@ -82,8 +125,21 @@ func dshMulticaProfilePresent() bool {if dshHome == "" {dshHome = filepath.Join(home, ".dsh")}- _, err = os.Stat(filepath.Join(dshHome, "profiles", dshMulticaProfileName, "package.json"))- return err == nil+ return filepath.Join(dshHome, "profiles", dshMulticaProfileName, "package.json")+}++// dshProfileNamesRuntime reports whether the manifest lists the Multica runtime+// as a dependency or as a profile bundle.+func dshProfileNamesRuntime(manifest dshProfileManifest) bool {+ if _, ok := manifest.Dependencies[dshMulticaRuntimePackage]; ok {+ return true+ }+ for _, bundle := range manifest.DSH.Profile.Bundles {+ if bundle == dshMulticaRuntimePackage {+ return true+ }+ }+ return false}// dshProfileBundleSpecs returns the configured install candidates, in order,@@ -301,6 +357,10 @@ func provisionDshMulticaProfile(ctx context.Context, dshPath string, logger *slolastErr = fmt.Errorf("dsh plugin --profile %s add (candidate %d of %d): %w: %s",dshMulticaProfileName, i+1, len(specs), err, dshProvisionOutput(output))}+ // Leave the directory. A refused add can write a skeleton, and a failure+ // can also land on a profile the user already had. dshMulticaProfilePresent+ // treats a manifest that does not name the runtime as missing, so the+ // next restart retries over it and --probe is not started.logger.Warn("DSH runtime profile install failed","bundle_candidate", i+1, "candidates", len(specs), "error", lastErr)}diff --git a/server/internal/daemon/dsh_profile_test.go b/server/internal/daemon/dsh_profile_test.goindex bbe545f542..268a10e984 100644--- a/server/internal/daemon/dsh_profile_test.go+++ b/server/internal/daemon/dsh_profile_test.go@@ -24,7 +24,27 @@ import (// developer's ~/.dsh.func fakeDsh(t *testing.T) (path, record string) {t.Helper()- return fakeDshScript(t, `mkdir -p "$DSH_HOME/profiles/multica" && printf '{}' > "$DSH_HOME/profiles/multica/package.json"`)+ return fakeDshScript(t, dshProfileWriteCommand(installedDshProfileManifest())+"\nexit 0")+}++// installedDshProfileManifest is a profile whose dependencies and bundle list+// both name the runtime. Either place is enough for the daemon to call it+// installed; writing both matches what a finished `dsh plugin add` leaves.+func installedDshProfileManifest() string {+ return "{\"dependencies\":{\"@multica-ai/dsh-runtime\":\"1.0.0\"},\"dsh\":{\"profile\":{\"bundles\":[\"@multica-ai/dsh-runtime\"]}}}\n"+}++// dshSkeletonProfileManifest is what a refused `dsh plugin add` restores: empty+// dependencies and only DSH's own template bundle.+func dshSkeletonProfileManifest() string {+ return "{\"name\":\"dsh-profile-multica\",\"private\":true,\"dependencies\":{},\"dsh\":{\"profile\":{\"bundles\":[\"@deepseek-ai/dsh-base\"]}}}\n"+}++// dshProfileWriteCommand is the shell that creates the multica profile manifest.+// The manifest is fed through a quoted heredoc so its JSON quotes need no+// escaping inside the fixture script.+func dshProfileWriteCommand(manifest string) string {+ return "mkdir -p \"$DSH_HOME/profiles/multica\" && cat > \"$DSH_HOME/profiles/multica/package.json\" <<'EOF'\n" + manifest + "EOF"}// fakeDshSilentlySucceeds exits 0 from `add` and writes no profile: a package@@ -72,14 +92,20 @@ func pinnedDshHome(t *testing.T) string {return home}-// installMulticaProfile creates the manifest dshMulticaProfilePresent looks for.+// installMulticaProfile creates the manifest dshMulticaProfilePresent looks for:+// one that names the Multica runtime, not a file that merely exists.func installMulticaProfile(t *testing.T, dshHome string) {+ t.Helper()+ writeDshProfileManifest(t, dshHome, installedDshProfileManifest())+}++func writeDshProfileManifest(t *testing.T, dshHome, manifest string) {t.Helper()dir := filepath.Join(dshHome, "profiles", dshMulticaProfileName)if err := os.MkdirAll(dir, 0o755); err != nil {t.Fatal(err)}- if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte("{}\n"), 0o644); err != nil {+ if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(manifest), 0o644); err != nil {t.Fatal(err)}}@@ -230,6 +256,127 @@ func TestProvisionDshMulticaProfile_ExitZeroWithoutAProfileIsAFailedCandidate(t}}+// A refused install can leave a profile directory whose manifest does not name+// the runtime. That directory stays. The manifest check already classifies it+// as missing, so the next restart retries over it, and deleting it would also+// remove files that were already there.+func TestProvisionDshMulticaProfile_FailedInstallLeavesTheProfileDirectory(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("shell fixture")+ }+ tests := []struct {+ name string+ manifest string+ code int+ }{+ {name: "exit 1 restores the skeleton", manifest: dshSkeletonProfileManifest(), code: 1},+ {name: "exit 0 writes an empty manifest", manifest: "{}\n", code: 0},+ {name: "empty dependencies", manifest: "{\"dependencies\":{}}\n", code: 1},+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ body := dshProfileWriteCommand(tc.manifest) + "\nprintf 'x\\n' > \"$DSH_HOME/profiles/multica/cordis.yml\"\nexit " + strconv.Itoa(tc.code)+ dshPath, _ := fakeDshScript(t, body)+ t.Setenv(dshProfileBundleEnv, "@multica-ai/dsh-runtime")+ t.Setenv(dshPluginPathEnv, "")++ logger := slog.New(slog.NewTextHandler(io.Discard, nil))+ err := provisionDshMulticaProfile(context.Background(), dshPath, logger)+ if err == nil {+ t.Fatal("provision returned nil for a manifest that does not name the runtime")+ }+ dir := filepath.Join(os.Getenv("DSH_HOME"), "profiles", dshMulticaProfileName)+ got, readErr := os.ReadFile(filepath.Join(dir, "package.json"))+ if readErr != nil {+ t.Fatalf("profile manifest was removed: %v", readErr)+ }+ if string(got) != tc.manifest {+ t.Fatalf("manifest = %q, want %q", got, tc.manifest)+ }+ config, readErr := os.ReadFile(filepath.Join(dir, "cordis.yml"))+ if readErr != nil {+ t.Fatalf("profile file written by the failed install was removed: %v", readErr)+ }+ if string(config) != "x\n" {+ t.Fatalf("cordis.yml = %q, want the file the failed install wrote", config)+ }+ })+ }+}++// A failed install that never touches an existing profile has no reason to+// remove it. The directory may already hold the user's own files.+func TestProvisionDshMulticaProfile_PreservesPreexistingProfileOnFailure(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("shell fixture")+ }+ // An installer that fails without touching the profile, like an early network error.+ dshPath, _ := fakeDshScript(t, "printf 'registry unreachable\\n' >&2\nexit 1")+ t.Setenv(dshProfileBundleEnv, "@multica-ai/dsh-runtime")+ t.Setenv(dshPluginPathEnv, "")+ home := os.Getenv("DSH_HOME")+ writeDshProfileManifest(t, home, dshSkeletonProfileManifest())+ configPath := filepath.Join(home, "profiles", dshMulticaProfileName, "cordis.yml")+ want := "# user-maintained profile configuration\nplugins: {}\n"+ if err := os.WriteFile(configPath, []byte(want), 0o600); err != nil {+ t.Fatal(err)+ }+ logger := slog.New(slog.NewTextHandler(io.Discard, nil))+ if err := provisionDshMulticaProfile(context.Background(), dshPath, logger); err == nil {+ t.Fatal("expected failed install")+ }+ got, err := os.ReadFile(configPath)+ if err != nil {+ t.Fatalf("pre-existing user configuration was deleted after a failed install: %v", err)+ }+ if string(got) != want {+ t.Fatalf("pre-existing user configuration changed: %q", got)+ }+ manifestPath := filepath.Join(home, "profiles", dshMulticaProfileName, "package.json")+ manifest, err := os.ReadFile(manifestPath)+ if err != nil {+ t.Fatalf("pre-existing manifest was deleted after a failed install: %v", err)+ }+ if string(manifest) != dshSkeletonProfileManifest() {+ t.Fatalf("pre-existing manifest changed: %q", manifest)+ }+}++// A failed install that left other packages, the runtime itself, or a manifest+// that cannot be read must not delete the directory. The first is someone+// else's install; the second is a profile this daemon must not overwrite; the+// third is a file it cannot safely judge.+func TestProvisionDshMulticaProfile_FailedInstallKeepsNonSkeletonProfiles(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("shell fixture")+ }+ tests := []struct {+ name string+ manifest string+ }{+ {name: "other dependency", manifest: "{\"dependencies\":{\"some-plugin\":\"1.0.0\"}}\n"},+ {name: "runtime already named", manifest: installedDshProfileManifest()},+ {name: "unparseable", manifest: "{\n"},+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ body := dshProfileWriteCommand(tc.manifest) + "\nexit 1"+ dshPath, _ := fakeDshScript(t, body)+ t.Setenv(dshProfileBundleEnv, "@multica-ai/dsh-runtime")+ t.Setenv(dshPluginPathEnv, "")++ logger := slog.New(slog.NewTextHandler(io.Discard, nil))+ if err := provisionDshMulticaProfile(context.Background(), dshPath, logger); err == nil {+ t.Fatal("provision returned nil after dsh exited 1")+ }+ manifestPath := filepath.Join(os.Getenv("DSH_HOME"), "profiles", dshMulticaProfileName, "package.json")+ if _, err := os.Stat(manifestPath); err != nil {+ t.Fatalf("manifest was removed: %v", err)+ }+ })+ }+}+// Once per daemon, not once per discovery round: this is a network install that// writes into the user's DSH home, and the caller uses the return value to tell// the user which of the two situations they are in.@@ -389,9 +536,10 @@ func TestKickAgentDiscovery_NonBlockingAndCollapsing(t *testing.T) {(&Daemon{}).kickAgentDiscovery()}-// The stat is what the discovery loop polls every tick to decide whether to-// force a round, so it has to mean exactly what DSH means by "this profile is-// installed" — a directory alone is not one.+// The discovery loop polls this every tick to decide whether to force a round.+// A file existing is not an installed profile: a refused plugin install leaves+// a manifest that does not name the runtime, and treating that as installed is+// what made the runtime permanently undiagnosable.func TestDshMulticaProfilePresent(t *testing.T) {home := t.TempDir()t.Setenv("DSH_HOME", home)@@ -409,12 +557,43 @@ func TestDshMulticaProfilePresent(t *testing.T) {if dshMulticaProfilePresent() {t.Fatal("a directory without a manifest was reported as an installed profile")}- if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte("{}\n"), 0o644); err != nil {- t.Fatal(err)++ absent := []struct {+ name string+ manifest string+ }{+ {name: "empty object", manifest: "{}\n"},+ {name: "empty dependencies", manifest: "{\"dependencies\":{}}\n"},+ {name: "refused-install skeleton", manifest: dshSkeletonProfileManifest()},+ {name: "similar package name", manifest: "{\"dependencies\":{\"@multica-ai/dsh-runtime-extra\":\"1.0.0\"}}\n"},+ }+ for _, tc := range absent {+ t.Run(tc.name, func(t *testing.T) {+ writeDshProfileManifest(t, home, tc.manifest)+ if dshMulticaProfilePresent() {+ t.Fatal("profile reported installed without the Multica runtime")+ }+ })}- if !dshMulticaProfilePresent() {- t.Fatal("manifest present but the profile was reported missing")++ present := []struct {+ name string+ manifest string+ }{+ {name: "dependency names the runtime", manifest: "{\"dependencies\":{\"@multica-ai/dsh-runtime\":\"1.0.0\"}}\n"},+ {name: "dependency value is an object", manifest: "{\"dependencies\":{\"@multica-ai/dsh-runtime\":{\"version\":\"1.0.0\"}}}\n"},+ {name: "bundle names the runtime", manifest: "{\"dsh\":{\"profile\":{\"bundles\":[\"@multica-ai/dsh-runtime\"]}}}\n"},+ {name: "unparseable manifest", manifest: "{\n"},+ }+ for _, tc := range present {+ t.Run(tc.name, func(t *testing.T) {+ writeDshProfileManifest(t, home, tc.manifest)+ if !dshMulticaProfilePresent() {+ t.Fatal("profile reported missing")+ }+ })}+if err := os.RemoveAll(dir); err != nil {t.Fatal(err)}