Skip to content

Commit fba99cd

Browse files
committed
docs: document gateway session utilities
1 parent d76301e commit fba99cd

9 files changed

Lines changed: 30 additions & 0 deletions

src/gateway/server-session-key.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Gateway run-id to session-key resolver.
2+
// Bridges live agent run context with persisted session stores.
13
import {
24
asDateTimestampMs,
35
resolveExpiresAtMsFromDurationMs,
@@ -53,6 +55,8 @@ function setResolvedSessionKeyCache(
5355
}
5456
let expiresAt: number | null = null;
5557
if (sessionKey === null) {
58+
// Negative caching avoids repeated full-store scans while still allowing
59+
// a just-created run/session pair to appear shortly after the first lookup.
5660
const missExpiresAt = resolveExpiresAtMsFromDurationMs(RUN_LOOKUP_MISS_TTL_MS);
5761
if (missExpiresAt === undefined) {
5862
return;
@@ -122,6 +126,8 @@ export function resolveSessionKeyForRun(runId: string, opts: { agentId?: string
122126
);
123127
const storeKey = resolvePreferredSessionKeyForSessionIdMatches(matches, runId);
124128
if (storeKey) {
129+
// Return caller-facing agent request keys, not raw store keys, because
130+
// HTTP/RPC clients reuse this value in later session operations.
125131
const sessionKey = resolveRunSessionKeyForCaller(storeKey);
126132
setResolvedSessionKeyCache(runId, cacheAgentId, sessionKey);
127133
return sessionKey;

src/gateway/server-startup-log.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Gateway startup logging helpers.
2+
// Produces the compact ready banner with resolved model and safety state.
13
import { normalizeSortedUniqueStringEntries } from "@openclaw/normalization-core/string-normalization";
24
import chalk from "chalk";
35
import { resolveDefaultAgentId, resolveAgentConfig } from "../agents/agent-scope.js";

src/gateway/server-startup-session-migration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Gateway startup session-store migration runner.
2+
// Keeps old orphaned session keys from surviving process upgrades.
13
import type { OpenClawConfig } from "../config/types.openclaw.js";
24
import { migrateOrphanedSessionKeys } from "../infra/state-migrations.js";
35

src/gateway/session-patch-hooks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Session patch hook dispatcher.
2+
// Publishes internal mutation notifications after Gateway session patch calls.
13
import type { SessionsPatchParams } from "../../packages/gateway-protocol/src/index.js";
24
import type { SessionEntry } from "../config/sessions.js";
35
import type { OpenClawConfig } from "../config/types.openclaw.js";

src/gateway/session-reset-service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Gateway session reset/delete service.
2+
// Rotates transcripts and coordinates lifecycle cleanup across runtimes/hooks.
13
import { randomUUID } from "node:crypto";
24
import fs from "node:fs";
35
import path from "node:path";
@@ -80,6 +82,8 @@ function resolveResetSessionFile(params: {
8082
agentId: string;
8183
}): string {
8284
const currentEntry = params.currentEntry;
85+
// Preserve explicit session-file placement across reset while swapping the
86+
// embedded session id, so linked runtimes keep writing beside old transcripts.
8387
const rewrittenSessionFile = currentEntry?.sessionId
8488
? rewriteSessionFileForNewSessionId({
8589
sessionFile: currentEntry.sessionFile,
@@ -108,6 +112,8 @@ function stripRuntimeModelState(entry?: SessionEntry): SessionEntry | undefined
108112
}
109113
return {
110114
...entry,
115+
// Reset should keep user selection preferences but drop per-run resolved
116+
// model state so the next turn rehydrates from current config.
111117
model: undefined,
112118
modelProvider: undefined,
113119
contextTokens: undefined,

src/gateway/session-subagent-reactivation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Subagent session reactivation helper.
2+
// Replaces completed subagent run records when a user steers the child session.
13
import { getLatestSubagentRunByChildSessionKey } from "../agents/subagent-registry-read.js";
24

35
// Completed subagent sessions can be reactivated after a user steer by replacing

src/gateway/session-utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Gateway session listing and projection helpers.
2+
// Normalizes persisted session stores into UI/RPC rows without mutating state.
13
import fs from "node:fs";
24
import path from "node:path";
35
import {
@@ -173,6 +175,8 @@ function resolveIdentityAvatarUrl(
173175
return undefined;
174176
}
175177
try {
178+
// Avatars can be workspace-relative, but projection must keep the file
179+
// read inside the agent workspace and cap bytes before encoding.
176180
const opened = openRootFileSync({
177181
absolutePath: resolvedCandidate,
178182
rootPath: workspaceRoot,
@@ -421,6 +425,8 @@ function shouldKeepStoreOnlyChildLink(entry: SessionEntry, now: number): boolean
421425
if (entry.status === "running" || isFinitePositiveTimestamp(entry.startedAt)) {
422426
return true;
423427
}
428+
// Store-only child links lack a live subagent registry entry. Keep recent
429+
// unknown-state rows visible briefly so reloads do not hide fresh children.
424430
return (
425431
isFinitePositiveTimestamp(entry.updatedAt) &&
426432
now - entry.updatedAt <= STALE_STORE_ONLY_CHILD_LINK_MS

src/gateway/session-utils.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Shared Gateway session projection types.
2+
// Keeps server methods and Control UI payloads aligned.
13
import type { ChatType } from "../channels/chat-type.js";
24
import type {
35
SessionCompactionCheckpoint,

src/gateway/startup-tasks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Ordered Gateway startup task runner.
2+
// Used for best-effort side effects that should not abort the server.
13
import { formatErrorMessage } from "../infra/errors.js";
24

35
// Startup tasks run sequentially so logs and side effects stay ordered during

0 commit comments

Comments
 (0)