Skip to content

Commit d9aedc3

Browse files
authored
fix(gateway): keep provider-owned CLI sessions across the daily default reset (#97931)
The gateway agent.run freshness decision called evaluateSessionFreshness directly at both of its decision sites with no provider-owned guard, so a provider-owned CLI session (claude-cli, codex, gemini-cli) under the default reset config was rotated after the daily boundary when a turn ran through the gateway path (webchat, openclaw agent, ACP, control UI, cron, heartbeat). The rotation cleared the CLI session binding and split the transcript, violating the documented exemption that the inbound auto-reply path and the canonical session helper already honor. Route both gateway freshness decisions through the same resetPolicy.configured !== true && hasProviderOwnedSession(entry) skip the inbound path uses, and export hasProviderOwnedSession so the predicate has one shared definition instead of a third copy. Explicit session.reset and /reset still cut these sessions.
1 parent bc8fa13 commit d9aedc3

3 files changed

Lines changed: 78 additions & 13 deletions

File tree

‎src/config/sessions/entry-freshness.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export type ResolvedSessionEntryResetFreshness =
4545
resetType: SessionResetType;
4646
};
4747

48-
function hasProviderOwnedSession(entry: SessionEntry | undefined): boolean {
48+
export function hasProviderOwnedSession(entry: SessionEntry | undefined): boolean {
4949
const provider = normalizeOptionalString(entry?.providerOverride ?? entry?.modelProvider);
5050
return Boolean(provider && getCliSessionBinding(entry, provider));
5151
}

‎src/gateway/server-methods/agent.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4060,6 +4060,61 @@ describe("gateway agent handler", () => {
40604060
}
40614061
});
40624062

4063+
it("keeps a provider-owned CLI session across the daily default boundary on the gateway path", async () => {
4064+
const now = Date.parse("2026-04-25T12:00:00.000Z");
4065+
vi.useFakeTimers();
4066+
vi.setSystemTime(now);
4067+
try {
4068+
mocks.resolveExplicitAgentSessionKey.mockReturnValue("agent:main:main");
4069+
mockMainSessionEntry({
4070+
sessionId: "provider-owned-session-id",
4071+
updatedAt: now,
4072+
sessionStartedAt: now - 25 * 60 * 60_000,
4073+
lastInteractionAt: now - 25 * 60 * 60_000,
4074+
modelProvider: "claude-cli",
4075+
cliSessionBindings: { "claude-cli": { sessionId: "claude-cli-conversation-123" } },
4076+
});
4077+
const loaded = mocks.loadSessionEntry();
4078+
let capturedEntry: Record<string, unknown> | undefined;
4079+
mocks.updateSessionStore.mockImplementation(async (_path, updater) => {
4080+
const store: Record<string, unknown> = {
4081+
[loaded.canonicalKey]: structuredClone(loaded.entry),
4082+
};
4083+
const result = await updater(store);
4084+
capturedEntry = result as Record<string, unknown>;
4085+
return result;
4086+
});
4087+
mocks.agentCommand.mockResolvedValue({
4088+
payloads: [{ text: "ok" }],
4089+
meta: { durationMs: 100 },
4090+
});
4091+
4092+
await invokeAgent(
4093+
{
4094+
message: "provider-owned daily boundary",
4095+
agentId: "main",
4096+
sessionKey: "agent:main:main",
4097+
idempotencyKey: "provider-owned-daily-boundary",
4098+
},
4099+
{ reqId: "provider-owned-daily-boundary" },
4100+
);
4101+
4102+
const call = await waitForAgentCommandCall<{
4103+
sessionId?: string;
4104+
sessionKey?: string;
4105+
}>();
4106+
expect(call.sessionKey).toBe("agent:main:main");
4107+
expect(call.sessionId).toBe("provider-owned-session-id");
4108+
expect(capturedEntry?.sessionStartedAt).toBe(now - 25 * 60 * 60_000);
4109+
expect(capturedEntry?.cliSessionBindings).toMatchObject({
4110+
"claude-cli": { sessionId: "claude-cli-conversation-123" },
4111+
});
4112+
expect(mocks.emitGatewaySessionEndPluginHook).not.toHaveBeenCalled();
4113+
} finally {
4114+
vi.useRealTimers();
4115+
}
4116+
});
4117+
40634118
it("emits idle lifecycle reason when inactivity rotates a gateway agent session", async () => {
40644119
const now = Date.parse("2026-04-25T12:00:00.000Z");
40654120
vi.useFakeTimers();

‎src/gateway/server-methods/agent.ts‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ import {
7171
resolveSessionResetPolicy,
7272
resolveSessionResetType,
7373
type SessionEntry,
74+
type SessionFreshness,
7475
updateSessionStore,
7576
} from "../../config/sessions.js";
77+
import { hasProviderOwnedSession } from "../../config/sessions/entry-freshness.js";
7678
import { resolveMaintenanceConfigFromInput } from "../../config/sessions/store-maintenance.js";
7779
import type { OpenClawConfig } from "../../config/types.openclaw.js";
7880
import {
@@ -1882,13 +1884,17 @@ export const agentHandlers: GatewayRequestHandlers = {
18821884
agentId: canonicalSessionAgentId,
18831885
})
18841886
: undefined;
1887+
const skipImplicitExpiry =
1888+
resetPolicy.configured !== true && hasProviderOwnedSession(entry);
18851889
let freshness = entry
1886-
? evaluateSessionFreshness({
1887-
updatedAt: entry.updatedAt,
1888-
...lifecycleTimestamps,
1889-
now,
1890-
policy: resetPolicy,
1891-
})
1890+
? skipImplicitExpiry
1891+
? ({ fresh: true } satisfies SessionFreshness)
1892+
: evaluateSessionFreshness({
1893+
updatedAt: entry.updatedAt,
1894+
...lifecycleTimestamps,
1895+
now,
1896+
policy: resetPolicy,
1897+
})
18921898
: undefined;
18931899
const visibleRequest =
18941900
request.bootstrapContextRunKind !== "cron" &&
@@ -2073,13 +2079,17 @@ export const agentHandlers: GatewayRequestHandlers = {
20732079
agentId: sessionAgent,
20742080
})
20752081
: undefined;
2082+
const freshSkipImplicitExpiry =
2083+
resetPolicy.configured !== true && hasProviderOwnedSession(freshEntry);
20762084
const freshFreshness = freshEntry
2077-
? evaluateSessionFreshness({
2078-
updatedAt: freshEntry.updatedAt,
2079-
...freshLifecycleTimestamps,
2080-
now,
2081-
policy: resetPolicy,
2082-
})
2085+
? freshSkipImplicitExpiry
2086+
? ({ fresh: true } satisfies SessionFreshness)
2087+
: evaluateSessionFreshness({
2088+
updatedAt: freshEntry.updatedAt,
2089+
...freshLifecycleTimestamps,
2090+
now,
2091+
policy: resetPolicy,
2092+
})
20832093
: undefined;
20842094
const freshRequestedSessionMatchesEntry = Boolean(
20852095
requestedSessionId && freshEntry?.sessionId?.trim() === requestedSessionId,

0 commit comments

Comments
 (0)