diff --git a/apps/server/src/provider/Layers/HermesGatewayBroker.test.ts b/apps/server/src/provider/Layers/HermesGatewayBroker.test.ts index 836431a364..8c789200e5 100644 --- a/apps/server/src/provider/Layers/HermesGatewayBroker.test.ts +++ b/apps/server/src/provider/Layers/HermesGatewayBroker.test.ts @@ -18,6 +18,7 @@ import * as Context from "effect/Context"; import * as Deferred from "effect/Deferred"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import * as ServerSecretStore from "../../auth/ServerSecretStore.ts"; @@ -36,6 +37,7 @@ const otherInstanceId = ProviderInstanceId.make("hermes_other"); const defaultHermesInstanceId = ProviderInstanceId.make("hermes"); const metadataSecretNameForTest = (id: ProviderInstanceId) => `hermes-gateway-metadata-${Buffer.from(id, "utf8").toString("base64url")}`; +const encodeUnknownJson = Schema.encodeUnknownSync(Schema.UnknownFromJsonString); class HermesTestInstance extends Context.Service()( "t3/provider/Layers/HermesGatewayBroker.test/HermesTestInstance", ) {} @@ -554,6 +556,49 @@ it.effect("checks nickname uniqueness from persisted metadata after restart", () }), ); +it.effect("keeps previous-protocol metadata manageable after restart", () => + Effect.gen(function* () { + const secrets = makeSecretStore(); + const previousProtocolVersion = HERMES_GATEWAY_PROTOCOL_VERSION - 1; + const legacyMetadata = (nickname: string) => ({ + nickname, + connectorUrl: "https://t3.example.test", + revoked: false, + lastSeen: { + pluginVersion: "0.1.0", + hermesVersion: "0.19.0", + capabilities: { + ...capabilities, + protocolVersion: previousProtocolVersion, + }, + connectedAt: "2026-07-24T00:00:00.000Z", + activeSessionCount: 0, + }, + }); + yield* secrets.set( + metadataSecretNameForTest(instanceId), + new TextEncoder().encode(encodeUnknownJson(legacyMetadata("Legacy Remote"))), + ); + yield* secrets.set( + metadataSecretNameForTest(otherInstanceId), + new TextEncoder().encode(encodeUnknownJson(legacyMetadata("Legacy Other"))), + ); + + const broker = yield* makeBroker(secrets); + const legacyStatus = yield* broker.getInstanceStatus(instanceId); + assert.equal(legacyStatus.protocolVersion, previousProtocolVersion); + assert.isNull(legacyStatus.capabilities); + assert.equal((yield* broker.revokeInstance(instanceId)).status, "revoked"); + + const enrollment = yield* broker.createEnrollment({ + instanceId: otherInstanceId, + nickname: "Legacy Other", + connectorUrl: "https://t3.example.test", + }); + assert.equal(enrollment.instanceId, otherInstanceId); + }), +); + it.effect( "renames the display label while preserving instance identity and normalized uniqueness", () => diff --git a/apps/server/src/provider/Layers/HermesGatewayBroker.ts b/apps/server/src/provider/Layers/HermesGatewayBroker.ts index 53902e237a..8be5c69863 100644 --- a/apps/server/src/provider/Layers/HermesGatewayBroker.ts +++ b/apps/server/src/provider/Layers/HermesGatewayBroker.ts @@ -6,6 +6,7 @@ import { HermesGatewayCredential, HermesGatewayEnrollmentToken, HermesGatewayCapabilities, + HermesGatewayHelloCapabilities, HermesGatewayManagementError, type HermesGatewayConnectionHello, type HermesGatewayCreateEnrollmentInput, @@ -65,7 +66,7 @@ const InstanceMetadata = Schema.Struct({ Schema.Struct({ pluginVersion: Schema.String, hermesVersion: Schema.String, - capabilities: HermesGatewayCapabilities, + capabilities: HermesGatewayHelloCapabilities, connectedAt: Schema.String, activeSessionCount: Schema.Number, }), @@ -155,7 +156,10 @@ const statusFromState = ( activeSessionCount: state.connection?.activeSessionCount ?? 0, protocolVersion: observed?.capabilities.protocolVersion ?? state.upgradeRequired?.protocolVersion ?? null, - capabilities: observed?.capabilities ?? null, + capabilities: + observed?.capabilities !== undefined && isStrictCapabilities(observed.capabilities) + ? observed.capabilities + : null, }; })(), instanceId, diff --git a/packages/client-runtime/src/state/threads-sync.test.ts b/packages/client-runtime/src/state/threads-sync.test.ts index 8d0e4d0a35..18be3fd74e 100644 --- a/packages/client-runtime/src/state/threads-sync.test.ts +++ b/packages/client-runtime/src/state/threads-sync.test.ts @@ -559,6 +559,7 @@ describe("EnvironmentThreads", () => { expect(Option.isNone(recovered.error)).toBe(true); expect(yield* Ref.get(harness.subscriptionCount)).toBe(2); + expect(yield* Ref.get(harness.loaderCalls)).toBe(1); expect(yield* Ref.get(harness.retryCount)).toBe(0); }), ); diff --git a/packages/client-runtime/src/state/threads.ts b/packages/client-runtime/src/state/threads.ts index 196229cc8b..df09aae607 100644 --- a/packages/client-runtime/src/state/threads.ts +++ b/packages/client-runtime/src/state/threads.ts @@ -21,6 +21,7 @@ import { EnvironmentSupervisor } from "../connection/supervisor.ts"; import * as ConnectionWakeups from "../connection/wakeups.ts"; import { EnvironmentCacheStore } from "../platform/persistence.ts"; import { subscribeDynamic } from "../rpc/client.ts"; +import type { RpcSession } from "../rpc/session.ts"; import { ThreadSnapshotLoader } from "./threadSnapshotHttp.ts"; import { parseThreadKey, threadKey } from "./entities.ts"; import { applyThreadDetailEvent } from "./threadReducer.ts"; @@ -79,6 +80,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make const lastSequence = yield* SubscriptionRef.make( Option.match(cached, { onNone: () => 0, onSome: (snapshot) => snapshot.snapshotSequence }), ); + const snapshotSession = yield* Ref.make(undefined); const awaitingCompletion = yield* Ref.make(false); const persistence = yield* Queue.sliding(1); @@ -253,24 +255,30 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make let current = yield* SubscriptionRef.get(state); if (Option.isNone(current.data) && current.status !== "deleted") { - const prepared = yield* SubscriptionRef.get(supervisor.prepared).pipe( - Effect.flatMap( - Option.match({ - onSome: Effect.succeed, - onNone: () => - SubscriptionRef.changes(supervisor.prepared).pipe( - Stream.filter(Option.isSome), - Stream.map((value) => value.value), - Stream.runHead, - Effect.map(Option.getOrThrow), - ), - }), - ), - ); - const httpSnapshot = yield* snapshotLoader.load(prepared, threadId); - if (Option.isSome(httpSnapshot)) { - yield* applyItem({ kind: "snapshot", snapshot: httpSnapshot.value }); - current = yield* SubscriptionRef.get(state); + const shouldLoadSnapshot = yield* Ref.modify(snapshotSession, (previous) => [ + previous !== session, + session, + ]); + if (shouldLoadSnapshot) { + const prepared = yield* SubscriptionRef.get(supervisor.prepared).pipe( + Effect.flatMap( + Option.match({ + onSome: Effect.succeed, + onNone: () => + SubscriptionRef.changes(supervisor.prepared).pipe( + Stream.filter(Option.isSome), + Stream.map((value) => value.value), + Stream.runHead, + Effect.map(Option.getOrThrow), + ), + }), + ), + ); + const httpSnapshot = yield* snapshotLoader.load(prepared, threadId); + if (Option.isSome(httpSnapshot)) { + yield* applyItem({ kind: "snapshot", snapshot: httpSnapshot.value }); + current = yield* SubscriptionRef.get(state); + } } }