Files
multica/server/pkg/db/generated/chat_pinned_agent.sql.go
Multica Eve b06af2ae17 feat(runtime): unbind agents on runtime delete instead of destroying them (#6220)
* feat(runtime): unbind agents on runtime delete instead of destroying them

Deleting a runtime archived its agents and then hard-deleted the rows, so the
agents and every conversation with them disappeared — while the confirmation
dialog said "archive", which a user reasonably reads as recoverable. Retiring a
laptop is an ordinary action; losing the agents configured on it is not an
ordinary consequence.

An agent is now a persistent business object and a runtime is replaceable
execution capacity: deleting a runtime unbinds its agents. `runtime_id IS NULL`
means unbound — orthogonal to archived — and the agent keeps its instructions,
skills, chats, labels, channel installations, autopilots and task history.
service.AgentReadiness already refused an agent with no runtime, so the
scheduling safety gate needed no change.

Two columns become nullable, not one. Without `agent_task_queue.runtime_id`,
deleting the runtime still cascades the task history away (and task_message /
task_usage / task_token with it), so the agents would survive with no record of
anything they did — the same class of loss. A NOT VALID CHECK keeps NULL confined
to history: an active task must always have a runtime, so claim / dispatch /
delivery-CAS paths can never observe one without. It is written against
completed_at rather than a status list so a future non-terminal status fails
closed instead of slipping through.

Two prerequisites this depends on:

- 'deferred' (migration 128) was missing from CancelAgentTasksByRuntimeOrAgent.
  It went unnoticed because the delete used to cascade those rows away; with the
  new CHECK it would abort the delete and make the runtime undeletable.
- The channel-installation / label / chat-pin / invocation-target / draft-restore
  cleanups were scoped to "archived agents on this runtime". Archived user agents
  now survive, so that scope is narrowed to kind='system' — otherwise the fix
  would produce a subtler loss: agent alive, configuration wiped.

Also removes the squad guard that refused (409) when an active squad's leader was
an archived agent on the runtime, plus the archived-squad delete that existed
only to get past squad.leader_id's RESTRICT FK. The leader is no longer deleted,
so nothing needs to be given up to retire a machine. Autopilots are no longer
paused either: their assignee survives, and a rebind restores them without the
owner having to remember to re-enable.

Reason codes: an unbound agent reports agent_runtime_required, not
runtime_offline. The copy for runtime_offline tells users to reconnect a machine;
an unbound agent has no machine to reconnect, and the fix is to bind a runtime.
Chat's bare 409 string gains the same code so the composer can offer that action.

API: agents gain runtime_bound. runtime_id stays a string (empty when unbound) so
installed clients keep parsing and no gated two-release rollout is needed. The
confirmed-delete endpoint is /unbind-agents-and-delete; /archive-agents-and-delete
still routes to it, and the compared expected_active_agent_ids set is unchanged —
widening it would 409 every older client forever.

Co-authored-by: multica-agent <github@multica.ai>

* fix: make runtime unbinding recoverable

Co-authored-by: multica-agent <github@multica.ai>

* fix: address runtime unbind review nits

Co-authored-by: multica-agent <github@multica.ai>

* fix: resolve runtime unbind review blockers

Co-authored-by: multica-agent <github@multica.ai>

* fix(migrations): renumber runtime unbind after main merge

Co-authored-by: multica-agent <github@multica.ai>

* test(daemon): avoid late-request lease flake

Co-authored-by: multica-agent <github@multica.ai>

* test(autopilots): bind validation fixture runtime

Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: Eve <eve@multica-ai.local>
Co-authored-by: multica-agent <github@multica.ai>
2026-08-03 12:39:27 +08:00

145 lines
4.3 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: chat_pinned_agent.sql
package db
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createChatPinnedAgent = `-- name: CreateChatPinnedAgent :one
INSERT INTO chat_pinned_agent (workspace_id, user_id, agent_id, position)
VALUES ($1, $2, $3, $4)
ON CONFLICT (workspace_id, user_id, agent_id)
DO UPDATE SET position = chat_pinned_agent.position
RETURNING id, workspace_id, user_id, agent_id, position, created_at
`
type CreateChatPinnedAgentParams struct {
WorkspaceID pgtype.UUID `json:"workspace_id"`
UserID pgtype.UUID `json:"user_id"`
AgentID pgtype.UUID `json:"agent_id"`
Position float64 `json:"position"`
}
// Idempotent: pinning an already-pinned agent is a no-op that returns the
// existing row (DO UPDATE keeps `:one` from hitting a no-rows error).
func (q *Queries) CreateChatPinnedAgent(ctx context.Context, arg CreateChatPinnedAgentParams) (ChatPinnedAgent, error) {
row := q.db.QueryRow(ctx, createChatPinnedAgent,
arg.WorkspaceID,
arg.UserID,
arg.AgentID,
arg.Position,
)
var i ChatPinnedAgent
err := row.Scan(
&i.ID,
&i.WorkspaceID,
&i.UserID,
&i.AgentID,
&i.Position,
&i.CreatedAt,
)
return i, err
}
const deleteChatPinnedAgent = `-- name: DeleteChatPinnedAgent :exec
DELETE FROM chat_pinned_agent
WHERE workspace_id = $1 AND user_id = $2 AND agent_id = $3
`
type DeleteChatPinnedAgentParams struct {
WorkspaceID pgtype.UUID `json:"workspace_id"`
UserID pgtype.UUID `json:"user_id"`
AgentID pgtype.UUID `json:"agent_id"`
}
func (q *Queries) DeleteChatPinnedAgent(ctx context.Context, arg DeleteChatPinnedAgentParams) error {
_, err := q.db.Exec(ctx, deleteChatPinnedAgent, arg.WorkspaceID, arg.UserID, arg.AgentID)
return err
}
const deleteChatPinnedAgentsBySystemRuntimeAgents = `-- name: DeleteChatPinnedAgentsBySystemRuntimeAgents :exec
DELETE FROM chat_pinned_agent
WHERE agent_id IN (
SELECT id FROM agent WHERE runtime_id = $1 AND kind = 'system'
)
`
// Scoped to the system agents a runtime delete hard-deletes. User agents keep
// their pins: since MUL-5559 they survive their runtime as unbound agents.
func (q *Queries) DeleteChatPinnedAgentsBySystemRuntimeAgents(ctx context.Context, runtimeID pgtype.UUID) error {
_, err := q.db.Exec(ctx, deleteChatPinnedAgentsBySystemRuntimeAgents, runtimeID)
return err
}
const deleteChatPinnedAgentsByWorkspace = `-- name: DeleteChatPinnedAgentsByWorkspace :exec
DELETE FROM chat_pinned_agent
WHERE workspace_id = $1
`
func (q *Queries) DeleteChatPinnedAgentsByWorkspace(ctx context.Context, workspaceID pgtype.UUID) error {
_, err := q.db.Exec(ctx, deleteChatPinnedAgentsByWorkspace, workspaceID)
return err
}
const getMaxChatPinnedAgentPosition = `-- name: GetMaxChatPinnedAgentPosition :one
SELECT COALESCE(MAX(position), 0)::float8 AS max_position
FROM chat_pinned_agent
WHERE workspace_id = $1 AND user_id = $2
`
type GetMaxChatPinnedAgentPositionParams struct {
WorkspaceID pgtype.UUID `json:"workspace_id"`
UserID pgtype.UUID `json:"user_id"`
}
func (q *Queries) GetMaxChatPinnedAgentPosition(ctx context.Context, arg GetMaxChatPinnedAgentPositionParams) (float64, error) {
row := q.db.QueryRow(ctx, getMaxChatPinnedAgentPosition, arg.WorkspaceID, arg.UserID)
var max_position float64
err := row.Scan(&max_position)
return max_position, err
}
const listChatPinnedAgents = `-- name: ListChatPinnedAgents :many
SELECT id, workspace_id, user_id, agent_id, position, created_at FROM chat_pinned_agent
WHERE workspace_id = $1 AND user_id = $2
ORDER BY position ASC, created_at ASC
`
type ListChatPinnedAgentsParams struct {
WorkspaceID pgtype.UUID `json:"workspace_id"`
UserID pgtype.UUID `json:"user_id"`
}
func (q *Queries) ListChatPinnedAgents(ctx context.Context, arg ListChatPinnedAgentsParams) ([]ChatPinnedAgent, error) {
rows, err := q.db.Query(ctx, listChatPinnedAgents, arg.WorkspaceID, arg.UserID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []ChatPinnedAgent{}
for rows.Next() {
var i ChatPinnedAgent
if err := rows.Scan(
&i.ID,
&i.WorkspaceID,
&i.UserID,
&i.AgentID,
&i.Position,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}