Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(runtime): wake daemon for local skill requests (#7713)

Janez Štuparbfe32f5e39ea
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.go
index af10ca7bbf..fa39590c2a 100644
--- a/server/internal/daemon/daemon.go
+++ b/server/internal/daemon/daemon.go
@@ -4005,11 +4005,11 @@ func (d *Daemon) handleHeartbeatActions(ctx context.Context, runtimeID string, r
//
// The HTTP heartbeat is used on purpose rather than queueing a WS frame: the
// hint arrives on the read pump, the WS write path may be backed up or tearing
-// down, and this is a human-interactive, low-frequency path (a user opened a
-// model picker) where one extra request is cheaper than a missed wakeup. Note it
-// intentionally bypasses the wsHeartbeatRecentlyAcked suppression that the
-// scheduled HTTP tick honours — that suppression exists to avoid duplicate
-// periodic writes, not to block an explicitly requested pull.
+// down, and this is a human-interactive, low-frequency path where one extra
+// request is cheaper than a missed wakeup. Note it intentionally bypasses the
+// wsHeartbeatRecentlyAcked suppression that the scheduled HTTP tick honours —
+// that suppression exists to avoid duplicate periodic writes, not to block an
+// explicitly requested pull.
func (d *Daemon) handlePendingWorkHint(runtimeID, kind string) {
if runtimeID == "" {
return
@@ -4037,10 +4037,10 @@ func (d *Daemon) handlePendingWorkHint(runtimeID, kind string) {
d.pendingWorkMu.Unlock()
return
}
- // Rate limit per runtime. The hint is caller-triggered (any workspace member
- // hitting the list-models endpoint), so without a floor a request loop would
- // become a heartbeat amplifier. A suppressed hint costs nothing but the
- // pre-existing wait for the scheduled heartbeat.
+ // Rate limit per runtime. The hint is caller-triggered by interactive model
+ // or capability endpoints, so without a floor a request loop would become a
+ // heartbeat amplifier. A suppressed hint costs nothing but the pre-existing
+ // wait for the scheduled heartbeat.
if last, ok := d.pendingWorkLastRun[runtimeID]; ok && time.Since(last) < pendingWorkHintMinInterval {
d.pendingWorkMu.Unlock()
d.logger.Debug("pending work hint throttled", "runtime_id", runtimeID, "kind", kind)
@@ -4055,10 +4055,12 @@ func (d *Daemon) handlePendingWorkHint(runtimeID, kind string) {
d.pendingWorkMu.Unlock()
}()
- // Root context: handleHeartbeatActions hands this ctx to the actual work
- // (model discovery shells out to a CLI, for up to ~40s on the slowest
- // provider — see agent.hermesDiscoveryTimeout), so it must outlive this
- // function. Only the heartbeat request itself is time-bounded.
+ // Root context: handleHeartbeatActions hands this ctx to the actual work, so
+ // it must outlive this function. Capability discovery and local-skill imports
+ // are normally quick filesystem operations, while model discovery may shell
+ // out to a CLI for up to ~40s on the slowest provider (see
+ // agent.hermesDiscoveryTimeout). Only the heartbeat request itself is
+ // time-bounded.
ctx := d.recoveryContext()
if ctx.Err() != nil {
return
diff --git a/server/internal/handler/handler.go b/server/internal/handler/handler.go
index cafc9e289c..72b547bce5 100644
--- a/server/internal/handler/handler.go
+++ b/server/internal/handler/handler.go
@@ -164,8 +164,9 @@ type WorkspaceSetRefreshNotifier interface {
}
// DaemonPendingWorkNotifier pushes a runtime-scoped "heartbeat now" hint to the
-// daemon so a queued heartbeat-carried request (model discovery) is picked up
-// immediately instead of on the daemon's next scheduled tick (MUL-5444).
+// daemon so a queued heartbeat-carried request (model discovery, capability
+// discovery, or local-skill import) is picked up immediately instead of on the
+// daemon's next scheduled tick (MUL-5444).
// Satisfied by both *daemonws.Hub (single-node) and *daemonws.RelayNotifier
// (multi-node, fans out through Redis).
type DaemonPendingWorkNotifier interface {
diff --git a/server/internal/handler/runtime_local_skills.go b/server/internal/handler/runtime_local_skills.go
index a94b963814..b32a90859b 100644
--- a/server/internal/handler/runtime_local_skills.go
+++ b/server/internal/handler/runtime_local_skills.go
@@ -606,6 +606,7 @@ func (h *Handler) InitiateListLocalSkills(w http.ResponseWriter, r *http.Request
writeError(w, http.StatusInternalServerError, "failed to enqueue local skills request: "+err.Error())
return
}
+ h.requestDaemonPendingWork(rt.runtimeID, protocol.PendingWorkKindLocalSkills)
writeJSON(w, http.StatusOK, req)
}
@@ -690,6 +691,7 @@ func (h *Handler) InitiateImportLocalSkill(w http.ResponseWriter, r *http.Reques
writeError(w, http.StatusInternalServerError, "failed to enqueue local skill import: "+err.Error())
return
}
+ h.requestDaemonPendingWork(rt.runtimeID, protocol.PendingWorkKindLocalSkillImport)
writeJSON(w, http.StatusOK, importReq)
}
diff --git a/server/internal/handler/runtime_local_skills_test.go b/server/internal/handler/runtime_local_skills_test.go
index 3499da3ba8..029f013eb3 100644
--- a/server/internal/handler/runtime_local_skills_test.go
+++ b/server/internal/handler/runtime_local_skills_test.go
@@ -9,8 +9,18 @@ import (
"net/http/httptest"
"testing"
"time"
+
+ "github.com/multica-ai/multica/server/pkg/protocol"
)
+type runtimeLocalSkillPendingWorkRecorder struct {
+ hints []string
+}
+
+func (r *runtimeLocalSkillPendingWorkRecorder) NotifyPendingWork(runtimeID, kind string) {
+ r.hints = append(r.hints, runtimeID+":"+kind)
+}
+
func newRequestAsUser(userID, method, path string, body any) *http.Request {
var buf bytes.Buffer
if body != nil {
@@ -265,6 +275,32 @@ func TestListLocalSkills_AllowsNonOwnerForPublicRuntime(t *testing.T) {
}
}
+func TestInitiateListLocalSkills_WakesDaemon(t *testing.T) {
+ if testHandler == nil {
+ t.Skip("database not available")
+ }
+
+ runtimeID := createRuntimeLocalSkillTestRuntime(t, testUserID)
+ recorder := &runtimeLocalSkillPendingWorkRecorder{}
+ h := *testHandler
+ h.DaemonPendingWork = recorder
+
+ w := httptest.NewRecorder()
+ req := withURLParams(
+ newRequestAsUser(testUserID, http.MethodPost, "/api/runtimes/"+runtimeID+"/local-skills", nil),
+ "runtimeId", runtimeID,
+ )
+
+ h.InitiateListLocalSkills(w, req)
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+ want := runtimeID + ":" + protocol.PendingWorkKindLocalSkills
+ if len(recorder.hints) != 1 || recorder.hints[0] != want {
+ t.Fatalf("pending-work hints = %v, want [%s]", recorder.hints, want)
+ }
+}
+
func TestListLocalSkills_RejectsNonMember(t *testing.T) {
if testHandler == nil {
t.Skip("database not available")
@@ -319,6 +355,49 @@ func TestInitiateImportLocalSkill_RequiresRuntimeOwner(t *testing.T) {
}
}
+func TestInitiateImportLocalSkill_WakesDaemonAfterValidEnqueue(t *testing.T) {
+ if testHandler == nil {
+ t.Skip("database not available")
+ }
+
+ runtimeID := createRuntimeLocalSkillTestRuntime(t, testUserID)
+ recorder := &runtimeLocalSkillPendingWorkRecorder{}
+ h := *testHandler
+ h.DaemonPendingWork = recorder
+
+ w := httptest.NewRecorder()
+ req := withURLParams(
+ newRequestAsUser(testUserID, http.MethodPost, "/api/runtimes/"+runtimeID+"/local-skills/import", map[string]any{
+ "skill_key": "review-helper",
+ }),
+ "runtimeId", runtimeID,
+ )
+
+ h.InitiateImportLocalSkill(w, req)
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+ want := runtimeID + ":" + protocol.PendingWorkKindLocalSkillImport
+ if len(recorder.hints) != 1 || recorder.hints[0] != want {
+ t.Fatalf("pending-work hints = %v, want [%s]", recorder.hints, want)
+ }
+
+ w = httptest.NewRecorder()
+ req = withURLParams(
+ newRequestAsUser(testUserID, http.MethodPost, "/api/runtimes/"+runtimeID+"/local-skills/import", map[string]any{
+ "skill_key": "",
+ }),
+ "runtimeId", runtimeID,
+ )
+ h.InitiateImportLocalSkill(w, req)
+ if w.Code != http.StatusBadRequest {
+ t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
+ }
+ if len(recorder.hints) != 1 {
+ t.Fatalf("rejected enqueue must not send another hint, got %v", recorder.hints)
+ }
+}
+
func TestGetLocalSkillImportRequest_RequiresRuntimeOwner(t *testing.T) {
if testHandler == nil {
t.Skip("database not available")
diff --git a/server/pkg/protocol/events.go b/server/pkg/protocol/events.go
index f372f949f1..11f4a6849a 100644
--- a/server/pkg/protocol/events.go
+++ b/server/pkg/protocol/events.go
@@ -150,10 +150,11 @@ const (
EventDaemonRuntimeProfilesChanged = "daemon:runtime_profiles_changed"
EventDaemonWorkspacesChanged = "daemon:workspaces_changed"
// EventDaemonPendingWork is a runtime-scoped hint that a heartbeat-carried
- // request (today: model-list discovery) is queued for that runtime. Without
- // it the daemon only learns about the request on its next scheduled
- // heartbeat, which adds up to one HeartbeatInterval (15s by default) of
- // dead wait to an interactive UI flow (MUL-5444). The hint carries no work
+ // request (model discovery, capability discovery, or local-skill import) is
+ // queued for that runtime. Without it the daemon only learns about the
+ // request on its next scheduled heartbeat, adding up to one HeartbeatInterval
+ // (15s by default) of dead wait to an interactive UI flow (MUL-5444). The
+ // hint carries no work
// itself: the daemon still pulls the request through the normal heartbeat
// claim, so a lost or duplicated hint is harmless.
EventDaemonPendingWork = "daemon:pending_work"
diff --git a/server/pkg/protocol/messages.go b/server/pkg/protocol/messages.go
index df85319e11..3c35ed4482 100644
--- a/server/pkg/protocol/messages.go
+++ b/server/pkg/protocol/messages.go
@@ -117,7 +117,9 @@ type WorkspacesChangedPayload struct{}
// heartbeat, which claims whatever is queued) — so an unknown value from a
// newer server stays safe on an older daemon.
const (
- PendingWorkKindModelList = "model_list"
+ PendingWorkKindModelList = "model_list"
+ PendingWorkKindLocalSkills = "local_skills"
+ PendingWorkKindLocalSkillImport = "local_skill_import"
)
// PendingWorkPayload is sent from server to daemon as a wakeup hint when a