fix: re-ensure Hermes sessions when the connection is replaced
Restarting the gateway killed every existing thread: the next message came back "Call session.ensure before starting a turn" with no way to recover from the UI. The reconnect trigger watched connectedness, but a plugin restart does not produce a disconnected gap. The broker accepts the new socket and retires the displaced one in the same step, then publishes a single `connected` status — so the edge never fired, no session.ensure was re-issued, and T3 kept a session id the new plugin process had never heard of. The plugin's session map is per-process, so its next turn.start was rejected outright. Track the connection's monotonic generation instead, which the registry already assigned but did not expose. A replacement changes it even though connectedness never wavers, while the session-count and ping republishes of one live connection leave it alone — re-ensuring on those would cancel pending approvals under a running turn. The existing reconnect tests all drove offline→connected, which is why they passed while the replacement path was broken. The harness can now model a replacement, and the regression test establishes a prior connection first: without that the adapter has no generation latched, the first status looks new under either implementation, and the test passes against the bug it is meant to catch. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
422cf087ee
commit
6d341a14be
6 files changed
+151
-5
No files matched your search
@@ -35,9 +35,19 @@ import {
|
||||
* The reconnect trigger is a broker status transition, so these tests drive the
|
||||
* broker's two streams directly and never touch a real socket or a clock.
|
||||
*/
|
||||
let generationCounter = 0;
|
||||
const nextGeneration = () => {
|
||||
generationCounter += 1;
|
||||
return generationCounter;
|
||||
};
|
||||
const statusFor = (
|
||||
instanceId: ProviderInstanceId,
|
||||
status: HermesGatewayInstanceStatus["status"],
|
||||
// Defaults to a fresh generation per connected status so a test that just
|
||||
// wants "it reconnected" gets one; pass an explicit value to model a
|
||||
// republish of the SAME connection (session counts, pings), which must not
|
||||
// re-trigger the resume.
|
||||
connectionGeneration: number | null = status === "connected" ? nextGeneration() : null,
|
||||
): HermesGatewayInstanceStatus => ({
|
||||
instanceId,
|
||||
nickname: "Hermes",
|
||||
@@ -50,6 +60,7 @@ const statusFor = (
|
||||
activeSessionCount: 0,
|
||||
protocolVersion: HERMES_GATEWAY_PROTOCOL_VERSION,
|
||||
capabilities: null,
|
||||
connectionGeneration,
|
||||
});
|
||||
|
||||
const collectEvents = (adapter: { readonly streamEvents: Stream.Stream<ProviderRuntimeEvent> }) =>
|
||||
@@ -509,8 +520,36 @@ const makeReconnectHarness = (options: {
|
||||
yield* PubSub.publish(brokerStatuses, statusFor(options.instanceId, "connected"));
|
||||
yield* drain;
|
||||
});
|
||||
// A plugin restart as it actually happens: the broker accepts the new
|
||||
// socket and retires the old one in the same step, so the instance never
|
||||
// observably goes offline and only one `connected` status is published.
|
||||
const replaceConnection = Effect.gen(function* () {
|
||||
state.connected = true;
|
||||
yield* PubSub.publish(brokerStatuses, statusFor(options.instanceId, "connected"));
|
||||
yield* drain;
|
||||
});
|
||||
// The same live connection republishing (session counts, pings). Must NOT
|
||||
// look like a new connection.
|
||||
const republishSameConnection = (generation: number) =>
|
||||
Effect.gen(function* () {
|
||||
state.connected = true;
|
||||
yield* PubSub.publish(
|
||||
brokerStatuses,
|
||||
statusFor(options.instanceId, "connected", generation),
|
||||
);
|
||||
yield* drain;
|
||||
});
|
||||
|
||||
return { adapter, sent, brokerEvents, state, disconnect, reconnect } as const;
|
||||
return {
|
||||
adapter,
|
||||
sent,
|
||||
brokerEvents,
|
||||
state,
|
||||
disconnect,
|
||||
reconnect,
|
||||
replaceConnection,
|
||||
republishSameConnection,
|
||||
} as const;
|
||||
});
|
||||
|
||||
it.effect("re-issues session.ensure on reconnect and keeps streaming a turn that survived", () =>
|
||||
@@ -572,6 +611,82 @@ it.effect("re-issues session.ensure on reconnect and keeps streaming a turn that
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
// Regression: a plugin restart REPLACES the socket rather than leaving a gap.
|
||||
// The broker accepts the new connection and retires the old one together, so
|
||||
// the instance never observably goes offline and exactly one `connected`
|
||||
// status is published. An edge detector watching connectedness therefore never
|
||||
// fires, T3 keeps a session the new plugin process has never heard of, and its
|
||||
// next turn.start comes back "Call session.ensure before starting a turn" —
|
||||
// the thread is dead with no way to recover from the UI.
|
||||
it.effect("re-issues session.ensure when the connection is replaced without going offline", () =>
|
||||
Effect.gen(function* () {
|
||||
const instanceId = ProviderInstanceId.make("hermes_replaced");
|
||||
const threadId = ThreadId.make("thread-replaced");
|
||||
const initialSessionId = HermesGatewaySessionId.make("session-before-restart");
|
||||
const harness = yield* makeReconnectHarness({ instanceId, threadId, initialSessionId });
|
||||
|
||||
// Production ordering: the plugin is already connected and the adapter has
|
||||
// observed that connection before any thread exists.
|
||||
yield* harness.republishSameConnection(7);
|
||||
|
||||
yield* harness.adapter.startSession({
|
||||
threadId,
|
||||
providerInstanceId: instanceId,
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
// No disconnect: the plugin restarted, so the broker retired the old socket
|
||||
// and accepted the new one together. The instance never reports offline.
|
||||
yield* harness.replaceConnection;
|
||||
|
||||
const ensures = harness.sent.filter((message) => message.type === "session.ensure");
|
||||
assert.equal(
|
||||
ensures.length,
|
||||
2,
|
||||
"a replaced connection must re-ensure, or the next turn is rejected",
|
||||
);
|
||||
const resume = ensures[1];
|
||||
assert.equal(
|
||||
resume?.type === "session.ensure" ? resume.resumeSessionId : undefined,
|
||||
initialSessionId,
|
||||
);
|
||||
|
||||
// A later turn now targets a session the new plugin knows about.
|
||||
yield* harness.adapter.sendTurn({ threadId, input: "after the restart" });
|
||||
const starts = harness.sent.filter((message) => message.type === "turn.start");
|
||||
assert.equal(starts.length, 1);
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("does not re-ensure when the same connection republishes its status", () =>
|
||||
Effect.gen(function* () {
|
||||
const instanceId = ProviderInstanceId.make("hermes_same_connection");
|
||||
const threadId = ThreadId.make("thread-same-connection");
|
||||
const harness = yield* makeReconnectHarness({
|
||||
instanceId,
|
||||
threadId,
|
||||
initialSessionId: HermesGatewaySessionId.make("session-stable"),
|
||||
});
|
||||
|
||||
// Production ordering: the plugin connects first, so its generation is
|
||||
// already known by the time a thread creates a session.
|
||||
yield* harness.republishSameConnection(4242);
|
||||
|
||||
yield* harness.adapter.startSession({
|
||||
threadId,
|
||||
providerInstanceId: instanceId,
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
// Session-count reports and ping results republish the SAME connection.
|
||||
// Re-ensuring on each would cancel pending approvals under a running turn.
|
||||
yield* harness.republishSameConnection(4242);
|
||||
yield* harness.republishSameConnection(4242);
|
||||
|
||||
assert.equal(harness.sent.filter((message) => message.type === "session.ensure").length, 1);
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("settles the thread when a turn does not survive the reconnect", () =>
|
||||
Effect.gen(function* () {
|
||||
const instanceId = ProviderInstanceId.make("hermes_dead_turn");
|
||||
|
||||
@@ -573,14 +573,28 @@ export const makeHermesAdapter = Effect.fn("makeHermesAdapter")(function* (input
|
||||
* `turn.completed` arrived for a turn orchestration had written off, was
|
||||
* rejected by its conflict gate, and the thread never settled.
|
||||
*/
|
||||
let connected = yield* broker.isConnected(input.instanceId);
|
||||
// Track the connection identity, not merely whether we are connected. A
|
||||
// plugin restart usually REPLACES the socket: the old one dies as the new
|
||||
// one is accepted, so exactly one `connected` status is published and a
|
||||
// connectedness edge never fires. The plugin's session map is per-process,
|
||||
// so skipping the re-ensure there leaves T3 holding a session the new
|
||||
// process has never heard of, and its next turn.start is rejected with
|
||||
// "Call session.ensure before starting a turn" — a dead thread.
|
||||
//
|
||||
// Starts null rather than reading the current generation: sessions are only
|
||||
// created after this point, so the first status we observe has nothing to
|
||||
// resume, and treating it as new is harmless.
|
||||
let seenGeneration: number | null = null;
|
||||
yield* broker.streamStatuses.pipe(
|
||||
Stream.filter((status) => status.instanceId === input.instanceId),
|
||||
Stream.runForEach((status) =>
|
||||
Effect.gen(function* () {
|
||||
const nowConnected = status.status === "connected";
|
||||
const reconnected = nowConnected && !connected;
|
||||
connected = nowConnected;
|
||||
const generation = status.connectionGeneration;
|
||||
const reconnected =
|
||||
status.status === "connected" && generation !== null && generation !== seenGeneration;
|
||||
// Only advance on a live connection: holding the last generation
|
||||
// through an offline gap means the reconnect still registers as new.
|
||||
if (generation !== null) seenGeneration = generation;
|
||||
if (!reconnected) return;
|
||||
yield* Effect.forEach(
|
||||
Array.from(sessions.values()),
|
||||
|
||||
@@ -46,6 +46,10 @@ export const livenessStatusFields = (
|
||||
protocolVersion:
|
||||
observed?.capabilities.protocolVersion ?? liveness?.upgradeRequired?.protocolVersion ?? null,
|
||||
capabilities: observed?.capabilities ?? null,
|
||||
// Exposed so consumers can tell a replacement (old socket dies as a new
|
||||
// one is accepted, publishing a single `connected` status) from a
|
||||
// continuous connection. Watching connectedness alone cannot.
|
||||
connectionGeneration: liveness?.connection?.generation ?? null,
|
||||
connected: liveness?.connection !== undefined,
|
||||
upgradeRequired: liveness?.upgradeRequired !== undefined,
|
||||
};
|
||||
|
||||
@@ -160,6 +160,7 @@ export type HermesLivenessStatusFields = Pick<
|
||||
| "activeSessionCount"
|
||||
| "protocolVersion"
|
||||
| "capabilities"
|
||||
| "connectionGeneration"
|
||||
> & {
|
||||
readonly connected: boolean;
|
||||
readonly upgradeRequired: boolean;
|
||||
|
||||
@@ -70,6 +70,7 @@ describe("Hermes gateway management contracts", () => {
|
||||
pluginVersion: "0.2.0",
|
||||
hermesVersion: "1.2.3",
|
||||
model: "gpt-5.6-terra",
|
||||
connectionGeneration: 3,
|
||||
activeSessionCount: 2,
|
||||
protocolVersion: 2,
|
||||
capabilities: {
|
||||
|
||||
@@ -130,6 +130,17 @@ export const HermesGatewayInstanceStatus = Schema.Struct({
|
||||
* plugin predates the `model` field on `connection.hello`.
|
||||
*/
|
||||
model: Schema.NullOr(TrimmedNonEmptyString),
|
||||
/**
|
||||
* Monotonic id of the underlying connection, or null while offline.
|
||||
*
|
||||
* Consumers must key "this is a different plugin process now" off this
|
||||
* rather than off `status` transitioning through `offline`. A replacement —
|
||||
* the old socket dying as a new one is accepted — publishes a single
|
||||
* `connected` status, so a connectedness edge detector never fires and
|
||||
* anything that must be re-established per connection (notably
|
||||
* `session.ensure`) is silently skipped.
|
||||
*/
|
||||
connectionGeneration: Schema.NullOr(NonNegativeInt),
|
||||
activeSessionCount: NonNegativeInt,
|
||||
protocolVersion: Schema.NullOr(PositiveInt),
|
||||
capabilities: Schema.NullOr(HermesGatewayCapabilities),
|
||||
|
||||
Reference in new issue
Block a user