Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(teamhub): remove the orphan Computer when Set Up Again renews its holder

xiaoke12345678f04460908e21
diff --git a/apps/desktop/AGENTS.md b/apps/desktop/AGENTS.md
index fdd14e25..37da5403 100644
--- a/apps/desktop/AGENTS.md
+++ b/apps/desktop/AGENTS.md
@@ -233,7 +233,9 @@ Gateway and runs Agents. The daemon exits with the app.
the `daemonId` the active enrollment named, and at start an enrollment that
named none or another one is renewed once the server lists the capability,
so a computer an earlier Set Up Again paired anew gets back the Computer
- that holds its Daemon Identity, with its Agents.
+ that holds its Daemon Identity, with its Agents. TeamHub removes the Computer
+ the renewal named when it never bound a Daemon Identity and has no Agents
+ (the orphan that earlier Set Up Again left), in the same transaction.
Before installing or resuming it moves the
`COMPOSIO_API_KEY`, `SONIOX_API_KEY`, `XAI_API_KEY` and `TZ` of an unpacked
source package's `.env` into `<home>/instances/desktop/user.env` once and
diff --git a/apps/teamhub/internal/agentruntimestate/runner.go b/apps/teamhub/internal/agentruntimestate/runner.go
index f7c8b4ed..96d072eb 100644
--- a/apps/teamhub/internal/agentruntimestate/runner.go
+++ b/apps/teamhub/internal/agentruntimestate/runner.go
@@ -265,7 +265,10 @@ func (s *Service) RemoveLocalComputer(ctx context.Context, input LocalComputerCr
// CreateLocalComputerEnrollment records a pending PKCE enrollment and returns
// the resolved Computer ID. When DaemonID matches a live Computer owned by the
// same owner, that Computer is renewed so existing Agent placements stay intact;
-// claiming another owner's bound DaemonID returns ErrDaemonIdentityInUse.
+// claiming another owner's bound DaemonID returns ErrDaemonIdentityInUse. A
+// renewal that moves from another Computer of the owner that never bound a
+// Daemon Identity and has no Agents removes that orphan in the same
+// transaction.
func (s *Service) CreateLocalComputerEnrollment(ctx context.Context, input CreateLocalComputerEnrollmentInput) (string, error) {
if !validText(input.EnrollmentID, 8, 128) || !validComputerID(input.ComputerID) ||
(input.DaemonID != "" && !daemonIDPattern.MatchString(input.DaemonID)) ||
@@ -286,6 +289,7 @@ func (s *Service) CreateLocalComputerEnrollment(ctx context.Context, input Creat
}
computerID := input.ComputerID
renews := input.RenewsExistingComputer
+ var changed *LocalComputerOwner
if input.DaemonID != "" {
bound, boundErr := queries.LockAgentRuntimeLocalComputerByDaemonID(ctx, input.DaemonID)
if boundErr != nil && !isNoRows(boundErr) {
@@ -297,6 +301,15 @@ func (s *Service) CreateLocalComputerEnrollment(ctx context.Context, input Creat
}
// Renew the owner's Computer already bound to this Daemon Identity
// instead of creating a duplicate Computer that would fail connect.
+ if bound.ComputerID != computerID && renews {
+ removed, err := removeOrphanLocalComputer(ctx, queries, input.Owner, computerID)
+ if err != nil {
+ return "", nil, err
+ }
+ if removed {
+ changed = &input.Owner
+ }
+ }
computerID = bound.ComputerID
renews = true
}
@@ -339,10 +352,44 @@ func (s *Service) CreateLocalComputerEnrollment(ctx context.Context, input Creat
if err != nil {
return "", nil, err
}
- return computerID, nil, nil
+ return computerID, changed, nil
})
}
+// removeOrphanLocalComputer removes the owner's Computer that an earlier Set
+// Up Again paired: it never bound a Daemon Identity, because its daemon's was
+// held by another Computer, and no Agent was placed on it. It reports whether
+// the Computer was removed; any other Computer is left alone.
+func removeOrphanLocalComputer(ctx context.Context, queries *store.Queries, owner LocalComputerOwner, computerID string) (bool, error) {
+ orphan, err := queries.LockAgentRuntimeLocalComputer(ctx, computerID)
+ if isNoRows(err) {
+ return false, nil
+ }
+ if err != nil {
+ return false, err
+ }
+ if orphan.Status == "removed" || orphan.DaemonID.Valid ||
+ orphan.OwnerTenantID != owner.TenantID || orphan.OwnerActorID != owner.OwnerActorID {
+ return false, nil
+ }
+ placedAgents, err := queries.CountLocalAgentRegistrationsByComputer(ctx, store.CountLocalAgentRegistrationsByComputerParams{
+ TenantID: owner.TenantID, ComputerID: computerID,
+ })
+ if err != nil || placedAgents > 0 {
+ return false, err
+ }
+ if err := queries.PurgeAgentRuntimeLocalComputerEnrollmentsByComputer(ctx, computerID); err != nil {
+ return false, err
+ }
+ if err := queries.DeleteLocalComputerModelSnapshot(ctx, computerID); err != nil {
+ return false, err
+ }
+ if _, err := queries.RemoveAgentRuntimeLocalComputer(ctx, computerID); err != nil {
+ return false, err
+ }
+ return true, nil
+}
+
func (s *Service) ConsumeLocalComputerEnrollment(ctx context.Context, input ConsumeLocalComputerEnrollmentInput) (ConsumeLocalComputerEnrollmentResult, error) {
if !validText(input.EnrollmentID, 8, 128) || !validText(input.RedirectURI, 1, 2000) ||
!validText(input.CodeChallenge, 43, 128) || len(input.AuthorizationCodeHash) != sha256.Size {
diff --git a/apps/teamhub/tests/integration/agent_runtime_runner_state_test.go b/apps/teamhub/tests/integration/agent_runtime_runner_state_test.go
index ca6981fa..ab829059 100644
--- a/apps/teamhub/tests/integration/agent_runtime_runner_state_test.go
+++ b/apps/teamhub/tests/integration/agent_runtime_runner_state_test.go
@@ -276,6 +276,82 @@ func TestAgentRuntimeLocalComputerRenewsByDaemonIdentityWithActiveAgents(t *test
}
}
+// An earlier Set Up Again paired a new Computer that never bound a Daemon
+// Identity, because its daemon's identity stayed bound to the original
+// Computer. Renewing that orphan with the Daemon ID moves the enrollment to the
+// original Computer and removes the orphan. A Computer that has Agents or its
+// own Daemon Identity, or a request that does not renew, is not an orphan.
+func TestAgentRuntimeLocalComputerEnrollmentByDaemonIDRemovesOnlyTheOrphan(t *testing.T) {
+ allowParallelIntegrationTest(t)
+ env := newRuntimeStateTestEnv(t)
+ owner := runtimeComputerOwner()
+ enrollRuntimeComputer(t, env, "computer_runtime_held")
+ held := runtimeConnectInput("computer_runtime_held", 0, time.Now())
+ if _, err := env.service.ConnectLocalComputer(t.Context(), held); err != nil {
+ t.Fatalf("connect held Computer: %v", err)
+ }
+ enrollRuntimeComputer(t, env, "computer_runtime_paired")
+ if _, err := env.service.ConnectLocalComputer(t.Context(), runtimeConnectInput("computer_runtime_paired", 0, time.Now())); err != nil {
+ t.Fatalf("connect paired Computer: %v", err)
+ }
+ enrollRuntimeComputer(t, env, "computer_runtime_placed")
+ setRuntimeAgentPlacementFields(t, env, "computer_runtime_placed", "codex")
+ enrollRuntimeComputer(t, env, "computer_runtime_orphan")
+
+ for _, name := range []string{"computer_runtime_paired", "computer_runtime_placed"} {
+ renewal := runtimeEnrollmentInput(name, owner)
+ renewal.EnrollmentID, renewal.RenewsExistingComputer, renewal.DaemonID = "enrollment-keep-"+name, true, held.DaemonID
+ if computerID, err := env.service.CreateLocalComputerEnrollment(t.Context(), renewal); err != nil || computerID != "computer_runtime_held" {
+ t.Fatalf("renew %s with the held Daemon ID: computer=%q err=%v, want computer_runtime_held", name, computerID, err)
+ }
+ assertLocalComputerRow(t, env, name, true)
+ }
+
+ pairing := runtimeEnrollmentInput("computer_runtime_new", owner)
+ pairing.EnrollmentID, pairing.DaemonID = "enrollment-pairing", held.DaemonID
+ if computerID, err := env.service.CreateLocalComputerEnrollment(t.Context(), pairing); err != nil || computerID != "computer_runtime_held" {
+ t.Fatalf("enroll a new Computer with the held Daemon ID: computer=%q err=%v", computerID, err)
+ }
+ assertLocalComputerRow(t, env, "computer_runtime_orphan", true)
+
+ heal := runtimeEnrollmentInput("computer_runtime_orphan", owner)
+ heal.EnrollmentID, heal.RenewsExistingComputer, heal.DaemonID = "enrollment-heal", true, held.DaemonID
+ if computerID, err := env.service.CreateLocalComputerEnrollment(t.Context(), heal); err != nil || computerID != "computer_runtime_held" {
+ t.Fatalf("renew the orphan with the held Daemon ID: computer=%q err=%v, want computer_runtime_held", computerID, err)
+ }
+ assertLocalComputerRow(t, env, "computer_runtime_orphan", false)
+ assertLocalComputerRow(t, env, "computer_runtime_held", true)
+ consumed, err := env.service.ConsumeLocalComputerEnrollment(t.Context(), runtimeEnrollmentExchange(heal))
+ assertConsumedEnrollment(t, consumed, err, "computer_runtime_held", 1)
+ if _, err := env.service.ConnectLocalComputer(t.Context(), runtimeConnectInput("computer_runtime_held", 1, time.Now())); err != nil {
+ t.Fatalf("reconnect the held Computer: %v", err)
+ }
+}
+
+func runtimeEnrollmentInput(computerID string, owner agentruntimestate.LocalComputerOwner) agentruntimestate.CreateLocalComputerEnrollmentInput {
+ codeHash := sha256.Sum256([]byte("authorization-code-" + computerID))
+ return agentruntimestate.CreateLocalComputerEnrollmentInput{
+ EnrollmentID: "enrollment-" + computerID, ComputerID: computerID, DisplayName: computerID,
+ Owner: owner, RedirectURI: "http://127.0.0.1:53100/callback", CodeChallenge: strings.Repeat("a", 43),
+ AuthorizationCodeHash: codeHash[:], ExpiresAt: time.Now().UTC().Truncate(time.Microsecond).Add(time.Hour),
+ }
+}
+
+func runtimeEnrollmentExchange(input agentruntimestate.CreateLocalComputerEnrollmentInput) agentruntimestate.ConsumeLocalComputerEnrollmentInput {
+ return agentruntimestate.ConsumeLocalComputerEnrollmentInput{
+ EnrollmentID: input.EnrollmentID, RedirectURI: input.RedirectURI,
+ CodeChallenge: input.CodeChallenge, AuthorizationCodeHash: input.AuthorizationCodeHash,
+ }
+}
+
+func assertLocalComputerRow(t *testing.T, env runtimeStateTestEnv, computerID string, wantListed bool) {
+ t.Helper()
+ var listed bool
+ if err := env.pool.QueryRow(t.Context(), `SELECT EXISTS(SELECT 1 FROM local_computers WHERE computer_id=$1 AND status <> 'removed')`, computerID).Scan(&listed); err != nil || listed != wantListed {
+ t.Fatalf("Local Computer %q live=%v err=%v, want live=%v", computerID, listed, err, wantListed)
+ }
+}
+
// A move only changes the registration row. The Computer the Agent left no
// longer lists it when it connects, so its Runner there is removed and its
// late results have no placement; the new Computer lists it at the next