Skip to content

fix(agents): exempt native installs from the staleness sweep, add revive script - #1733

Merged
lilyshen0722 merged 2 commits into
mainfrom
wren/task-041-native-install-staleness
Sep 18, 2026
Merged

lilyshen0722 merged 2 commits into
mainfrom
wren/task-041-native-install-staleness

Conversation

@lilyshen0722

@lilyshen0722 lilyshen0722 commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

What

TASK-041 (Connectors v2, Sam 69271/69274). agentInstallationCleanupService.markStaleInstallations marks an install stale when the owning agent user has no unexpired runtime token, no token used since the cutoff, and no AgentEvent since the cutoff. A native seat (config.runtime.runtimeType === 'native') runs in-process and holds no runtime token, and it gets an AgentEvent only when a person addresses it, so a pod that is quiet for a week reads as a dead agent. Pod authorization requires status: 'active', so every @-mention in such a pod 403s. Measured 2026-09-18: 21 of 30 Scout installs were stale.

  • backend/services/agentInstallationCleanupService.ts: STALENESS_EXEMPT_RUNTIME_TYPES = ['native'] and isStalenessExempt(install). The mark step projects config.runtime, filters exempt installs out before the pair evaluation (an all-native pair is never looked up), and the updateMany filter carries 'config.runtime.runtimeType': { $nin: [...] } so a pair installed both natively and remotely never writes onto the native row. internal is deliberately NOT exempt: it is the legacy in-process runtime type and stays swept exactly as before.
  • backend/scripts/revive-native-installations.ts (+ npm run revive:native-installations): restores rows the cron already marked. Dry run by default, prints the candidate count and a per-agent breakdown; --apply sets status: 'active' and unsets staleSince. Idempotent. It cannot restore rows the prune step already deleted (14 d after staleSince); those are reinstalls.

Proof

  • backend/__tests__/unit/services/agentInstallationCleanup.nativeExempt.test.js (5): exact exemption set; a native install past the window with no token and no events stays active and is never looked up; an internal install in the same state is still marked; config.runtime is projected (without it the exemption silently reads nothing); a mixed native+remote pair carries the $nin guard on the write.
  • backend/__tests__/unit/scripts/revive-native-installations.test.js (3, in-memory Mongo): dry run counts 2 stale native rows and changes nothing; apply revives them, clears staleSince, leaves the internal and moltbot stale rows alone; a second apply finds nothing.
  • Existing agentInstallationCleanup.staleness + failureCount suites and unguardedScriptImporters pass unchanged (20/20 across the five suites). tsc:check clean; lint:ts clean on the two TypeScript files.

Live run (after merge + deploy, per Sam 69274)

From the admin side, in the running backend pod: npm run revive:native-installations first (Vera reads the count), then -- --apply. Nothing this PR touches is on the C4 path.

Pre-runtime rows (b7cdcea, after Vera's dev count)

Vera's read-only count on dev: 19 stale Scout rows carry runtimeType: 'native', 2 carry no config.runtime at all (early first-teammate shape, 2026-09-01/02). Reviving those two alone would last one day — the sweep's $nin treats a missing runtimeType as sweepable. Ruling (69346): backfill, then revive, same script, same --apply.

  • Stale rows with no config.runtime.runtimeType are resolved against their AgentRegistry row; only a declared exempt runtime backfills, and the revive update re-checks the field so an un-backfilled row is never flipped.
  • The registry's ManifestRuntimeSchema is strict and drops manifest.runtime.runtimeType on write — measured with the seed's own upsert shape; only runtime.type: 'native' survives. The script reads either field and refuses deployment-shape values (standalone / commonly-hosted / hybrid). A test pins the stripping.
  • Dry run reports backfill: { candidates, eligible, byAgent, skipped, applied } beside the total; the log line names the pre-runtime subset. Expected on dev: 21 would be revived, 2 of them backfilled.

Follow-up, not in this PR: backend/routes/registry/install.ts:345 falls back to agent.manifest.runtime.runtimeType for native installs from the v2 UI. That field is stripped by the same strict subschema, so the fallback is dead on seeded rows and the bug it describes (runtimeType null → external queue → agent never replies) is plausibly still live. Worth its own row.

🤖 Generated with Claude Code

lilyshen0722 and others added 2 commits September 18, 2026 00:57
…ive script

markStaleInstallations reads two liveness signals, an unexpired runtime token
or one used since the cutoff, with AgentEvent recency as the fallback. Both are
runtime-token signals in disguise: a native seat runs in-process, holds no
token, and gets an AgentEvent only when a person addresses it, so a quiet pod
reads as a dead agent. Measured 2026-09-18: 21 of 30 Scout installs were
'stale', and pod authorization requires 'active', so every @-mention in those
pods 403'd.

Native installs (config.runtime.runtimeType 'native') no longer enter the
evaluation, and the updateMany filter carries the same exemption so a pair
installed both natively and remotely never writes onto the native row. The
legacy 'internal' runtime type is deliberately not exempt and stays swept.

scripts/revive-native-installations.ts restores the rows the cron already
marked: dry run by default (prints the count and a per-agent breakdown),
--apply sets status back to active and clears staleSince. Idempotent; it
cannot restore rows the prune step already deleted.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…e reviving them

Vera's read-only count on dev found 19 stale Scout rows carrying
runtimeType 'native' and 2 with no config.runtime at all, written under
the early first-teammate shape on 2026-09-01/02. Reviving those two
alone is a one-day fix: the sweep's `$nin` treats a missing runtimeType
as sweepable, so they would go stale again at the next 04:00.

The script now finds stale rows with no `config.runtime.runtimeType`,
resolves each agent against its AgentRegistry row, and — only when that
row declares an exempt runtime — sets the runtimeType in the same
`--apply` before the revive update runs. Rows whose agent has no
registry declaration are reported under `backfill.skipped` and left
untouched. Dry run reports the pre-runtime subset as its own count.

The registry's ManifestRuntimeSchema is strict and drops
`runtime.runtimeType` on write (measured with the seed's own upsert
shape; only `runtime.type: 'native'` survives), so the declaration is
read from either field and only an exempt runtime name counts — a
deployment-shape value never backfills. A test pins the stripping so
the script cannot silently start depending on the dropped field.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@lilyshen0722
lilyshen0722 merged commit 3058d7c into main Sep 18, 2026
19 checks passed
samxu01 pushed a commit that referenced this pull request Sep 18, 2026
…back asserted

#1733's suite pins the seeder shape it was written against — "keeps
manifest.runtime.type and drops runtimeType" — and that assertion is exactly what
TASK-043 inverts, so it fails against this branch (1 of 44 suites, on the merged
base).

The guard's intent survives the fix and stays asserted: every row the OLD seeder
wrote still carries enum-invalid type:'native' (findOneAndUpdate does not run
validators), so declaredExemptRuntime's `type` fallback is still load-bearing —
and the read-order test above it already pins runtimeType-first, which is what
makes the two PRs order-independent as Wren described.
lilyshen0722 added a commit that referenced this pull request Sep 18, 2026
)

* fix(registry): make a manifest's declared driver identity persist

install.ts reads manifest.runtime.runtimeType to fill a runtimeType the caller
omitted — the path a native first-party app installs through, since the Hub
sends no runtimeType of its own. No schema path existed for that field and
ManifestRuntimeSchema is strict, so every write dropped it: the fallback could
never fire and a hand-written test fixture kept it green.

- declare runtimeType on ManifestRuntimeSchema (driver identity;  stays
  the deployment shape, whose enum kept refusing 'native' — the value the
  seeder was writing into it)
- seed-native-agents: one buildRegistryManifest, identity only, no enum-invalid
  ; refreshed on every boot like the rest of the manifest
- agentManifestRegistry: carry runtimeType through publish normalization, and
  refuse a deployment shape in that field
- tests: model round-trips (constructor + upsert), the route fixture now comes
  from the real schema, seeder and validator coverage

Measured before the fix: new AgentRegistry({manifest:{runtime:{type:'native',
runtimeType:'native'}}}).toObject() == {type:'native',connection:'mcp'}.
Removing the schema path again fails 4 tests, the route fallback included.

* test(registry): pin the cloud gate against a manifest-declared runtimeType

Wren's shape read (69353) calls the entitlement consequence out as the thing to
get right: publish is plain `auth`, so with runtimeType now persisting any
owner can declare one, and install copies the manifest value BEFORE the gate
reads it. These two cases pin that shape:

- a manifest-declared 'native' install by a non-admin, non-entitled installer
  is refused with cloud_agents_not_entitled (the manifest is not a bypass)
- an explicit caller runtimeType wins over the manifest, so a manifest edit
  cannot re-shape an install the caller already typed

Both fixtures come from the real schema, so they fail if the schema path goes
away again — verified: dropping runtimeType fails the first, letting the
manifest overwrite the caller fails the second.

Open, for Vera's 69355 and Sam's ruling: whether a first-party BUILTIN native
app (scout, pod-welcomer, …) should stay entitlement-gated like every other
in-process runtime, or be exempt as the onboarding wedge. That is policy, not
code shape — this commit pins the mechanism either way.

* test(revive): the seeder writes runtimeType now; keep the `type` fallback asserted

#1733's suite pins the seeder shape it was written against — "keeps
manifest.runtime.type and drops runtimeType" — and that assertion is exactly what
TASK-043 inverts, so it fails against this branch (1 of 44 suites, on the merged
base).

The guard's intent survives the fix and stays asserted: every row the OLD seeder
wrote still carries enum-invalid type:'native' (findOneAndUpdate does not run
validators), so declaredExemptRuntime's `type` fallback is still load-bearing —
and the read-order test above it already pins runtimeType-first, which is what
makes the two PRs order-independent as Wren described.

* feat(registry): exempt first-party native installs from the cloud gate

Ruling (Wren 69359/69361, Sam 69364): first-party native apps are
entitlement-free, so the 403 the restored manifest runtimeType produced is not
intended. Scout reaches every signup ungated — authController upserts it at
workspace creation, capped by dailyRunCap — so gating the Hub copy of the same
app would gate a thing we hand out at signup. cloudAgents exists for the
operator-run tiers; the in-process native runtime is the capped one.

Exemption = effectiveRuntimeType native AND the row itself declares native AND
registry 'commonly-official' AND verified. The row, never manifest content:
publish is plain auth, so an exemption earnable by declaring 'native' would hand
any publisher in-process compute for free. Requiring the row to declare native
stops an official row's provenance being borrowed by an explicit caller
runtimeType to skip another tier's gate.

Tests (non-admin, no entitlement): official+verified native installs and
persists runtimeType; community-declared native 403s; official row that does not
declare native 403s; unverified official 403s. Mutations: dropping the exemption
fails the first, dropping the declares-native half fails the third.

* Revert "feat(registry): exempt first-party native installs from the cloud gate"

Decision card 69363 ruled option 1 — keep the gate (Sam 69366). The first-party
native exemption this reverts was implemented on Wren's 69359/69361 key and Sam's
69364 confirmation; Sam then overrode both, and the reason is checkable in the
repo: the first-teammate path never meets this gate. Scout is installed at
registration by the server (authController.ts:215, buildInstallationConfig) and
the hire path writes the model directly (personaHireService.ts:79-93), neither of
which goes through routes/registry/install.ts — the gate is route-level only. Its
actual subject is a non-admin adding further native apps from the Hub, and #529
deliberately routes that 403 to the BYO flow; exempting it would widen who can
consume platform compute without a pricing sentence behind it.

Content is byte-identical to 4b58841 (git diff 4b58841 HEAD is empty), so
re-gating is a zero-diff re-clear. The exemption itself is preserved in history
at ef8d8ff should a product row for it land. Non-admin tests kept: a community
row declaring native 403s, and an explicit caller runtimeType beats the manifest.

This reverts commit ef8d8ff.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant