File size: 16,333 Bytes
e249c6d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | /**
* Updates persisted session metadata after agent command runs.
*/
import { asNonNegativeFiniteNumber } from "@openclaw/normalization-core/number-coercion";
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import {
SESSION_TOTAL_TOKENS_VERSION,
setSessionRuntimeModel,
type CliSessionBinding,
type SessionEntry,
} from "../../config/sessions.js";
import { patchSessionEntryCore } from "../../config/sessions/session-accessor.js";
import { COMPACTION_RUN_USAGE_CLEAR_PATCH } from "../../config/sessions/session-entry-projection.js";
import { projectSessionSnapshotChanges } from "../../config/sessions/session-snapshot-merge.js";
import { resolveMaintenanceConfigFromInput } from "../../config/sessions/store-maintenance.js";
import type { InternalSessionEntry } from "../../config/sessions/types.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { createLazyPromise } from "../../shared/lazy-promise.js";
import { clearAllCliSessions, setCliSessionBinding } from "../cli-session.js";
import { DEFAULT_CONTEXT_TOKENS } from "../defaults.js";
import type { CompactionAccountingFact } from "../embedded-agent-runner/run/internal-params.js";
import type { EmbeddedAgentCompactResult } from "../embedded-agent-runner/types.js";
import { clearMainSessionRecoveryAfterAgentRun } from "../main-session-recovery/main-session-recovery-clear.js";
import { deriveSessionTotalTokens, hasBillableUsage, hasNonzeroUsage } from "../usage.js";
type RunResult = Awaited<ReturnType<(typeof import("../embedded-agent.js"))["runEmbeddedAgent"]>>;
const getUsageFormatModule = createLazyPromise(() => import("../../utils/usage-format.js"));
const getContextModule = createLazyPromise(() => import("../context.js"));
export function normalizeSessionTokenCount(value: number | undefined): number | undefined {
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
return undefined;
}
return Math.floor(value);
}
/** Applies run result metadata and usage to a session entry. */
export async function updateSessionStoreAfterAgentRun(params: {
cfg: OpenClawConfig;
agentDir: string;
sessionId: string;
sessionKey: string;
storePath: string;
sessionStore: Record<string, SessionEntry>;
defaultProvider: string;
defaultModel: string;
fallbackProvider?: string;
fallbackModel?: string;
result: RunResult;
/** Private committed owner and ordered context; an unknown snapshot invalidates older usage. */
compactionAccounting?: Extract<CompactionAccountingFact, { kind: "durable" }>;
touchInteraction?: boolean;
/**
* When false, skip the lastActivityAt bump so heartbeat/internal-event runs
* do not re-flag sessions unread; cron and user-facing runs count as activity.
*/
touchActivity?: boolean;
/**
* When true, preserve the pre-existing runtime model fields (model,
* modelProvider, contextTokens) on the session entry instead of overwriting
* them with the model used by this run. Used for turn-local fallback and
* heartbeat runs so their model does not bleed into the session selection.
*/
preserveRuntimeModel?: boolean;
preserveUserFacingSessionModelState?: boolean;
/** Clear the durable replay-safe recovery guard after this recovery run terminates. */
clearRestartRecoveryForceSafeTools?: boolean;
}) {
const {
cfg,
sessionId,
sessionKey,
storePath,
sessionStore,
defaultProvider,
defaultModel,
fallbackProvider,
fallbackModel,
result,
} = params;
const now = Date.now();
const touchInteraction = params.touchInteraction !== false;
const touchActivity = params.touchActivity !== false;
const usage = result.meta.agentMeta?.usage;
const promptTokens = result.meta.agentMeta?.promptTokens;
const lastCallUsage = result.meta.agentMeta?.lastCallUsage;
const modelUsed = result.meta.agentMeta?.model ?? fallbackModel ?? defaultModel;
const providerUsed = result.meta.agentMeta?.provider ?? fallbackProvider ?? defaultProvider;
const agentHarnessId = normalizeOptionalString(result.meta.agentMeta?.agentHarnessId);
const runtimeContextTokens = normalizeSessionTokenCount(result.meta.agentMeta?.contextTokens);
const contextBudgetStatus = result.meta.agentMeta?.contextBudgetStatus;
const contextTokens =
runtimeContextTokens !== undefined
? runtimeContextTokens
: ((await getContextModule()).resolveContextTokensForModel({
cfg,
provider: providerUsed,
model: modelUsed,
fallbackContextTokens: DEFAULT_CONTEXT_TOKENS,
allowAsyncLoad: false,
}) ?? DEFAULT_CONTEXT_TOKENS);
const contextTokensSource = result.meta.agentMeta?.contextTokensSource ?? "resolved";
const preserveUserFacingRunState = params.preserveUserFacingSessionModelState === true;
const preserveRuntimeModel = params.preserveRuntimeModel === true || preserveUserFacingRunState;
const hadPreExistingEntry = sessionStore[sessionKey] !== undefined;
const entry: InternalSessionEntry = sessionStore[sessionKey] ?? {
sessionId,
updatedAt: now,
sessionStartedAt: now,
};
const expectedSession = params.compactionAccounting?.target ?? entry;
if (!preserveUserFacingRunState && expectedSession.sessionId !== sessionId) {
return;
}
const next: SessionEntry = {
...entry,
updatedAt: now,
sessionStartedAt: entry.sessionStartedAt ?? now,
lastInteractionAt: touchInteraction ? now : entry.lastInteractionAt,
lastActivityAt: touchActivity ? now : entry.lastActivityAt,
...(preserveRuntimeModel
? {}
: {
contextTokens,
contextTokensSource,
}),
};
if (preserveRuntimeModel) {
// Keep the pre-existing runtime model and context window so a turn-local
// model does not bleed into the session's perceived selection.
if (entry.model) {
// Prior runtime model exists: preserve its contextTokens. When missing,
// leave contextTokens unset rather than falling back to the heartbeat
// run's context window; status derives it from the preserved model.
next.contextTokens = entry.contextTokens;
if (entry.modelProvider) {
setSessionRuntimeModel(next, {
provider: entry.modelProvider,
model: entry.model,
});
} else {
// Retain the model-only entry without borrowing the heartbeat provider
// to avoid invalid cross-provider pairs (e.g. ollama/claude-opus-4-6).
next.model = entry.model;
}
}
// When there is no prior runtime model, do nothing: a heartbeat turn
// should not establish initial model state on an empty session.
} else {
setSessionRuntimeModel(next, {
provider: providerUsed,
model: modelUsed,
});
}
if (!preserveUserFacingRunState) {
if (!preserveRuntimeModel) {
next.agentHarnessId = agentHarnessId;
}
next.abortedLastRun = result.meta.aborted ?? false;
clearMainSessionRecoveryAfterAgentRun(next, params.clearRestartRecoveryForceSafeTools);
if (result.meta.systemPromptReport) {
next.systemPromptReport = result.meta.systemPromptReport;
}
if (!preserveRuntimeModel) {
next.contextBudgetStatus = contextBudgetStatus;
}
}
const hasUsage = hasNonzeroUsage(usage);
if (hasBillableUsage(usage) && !preserveUserFacingRunState) {
const { estimateAggregateUsageCost } = await getUsageFormatModule();
const runEstimatedCostUsd = asNonNegativeFiniteNumber(
estimateAggregateUsageCost({
usage,
provider: providerUsed,
model: modelUsed,
config: cfg,
agentDir: params.agentDir,
}),
);
if (hasUsage) {
next.inputTokens = usage.input ?? 0;
next.outputTokens = usage.output ?? 0;
next.cacheRead = usage.cacheRead ?? 0;
next.cacheWrite = usage.cacheWrite ?? 0;
}
// Snapshot cumulative run cost once, independently of current context.
// Unknown current cost must clear the previous run's snapshot too.
next.estimatedCostUsd = runEstimatedCostUsd;
}
if (!preserveUserFacingRunState) {
const currentContextSnapshot = params.compactionAccounting?.currentContextSnapshot;
if (currentContextSnapshot || hasUsage) {
const totalTokens = currentContextSnapshot
? currentContextSnapshot.tokens
: deriveSessionTotalTokens({ lastCallUsage, contextTokens, promptTokens });
next.totalTokens = totalTokens;
next.totalTokensFresh = totalTokens !== undefined;
next.totalTokensVersion =
totalTokens !== undefined ? SESSION_TOTAL_TOKENS_VERSION : undefined;
} else {
// Empty-session zero is no longer current after a turn without usage.
next.totalTokensFresh = false;
next.totalTokensVersion = undefined;
}
}
const metadataPatch = preserveUserFacingRunState
? {
// Preserved-state runs must not alter perceived session state, so the
// unread-driving lastActivityAt stays untouched here.
updatedAt: next.updatedAt,
...(touchInteraction ? { lastInteractionAt: next.lastInteractionAt } : {}),
}
: next;
const maintenanceConfig = resolveMaintenanceConfigFromInput(cfg.session?.maintenance);
await patchSessionEntryCore(
{
storePath,
sessionKey,
},
(currentEntry, context) => {
if (
(!context.existingEntry && hadPreExistingEntry) ||
(!preserveUserFacingRunState &&
context.existingEntry &&
(context.existingEntry.sessionId !== expectedSession.sessionId ||
context.existingEntry.lifecycleRevision !== expectedSession.lifecycleRevision ||
context.existingEntry.activeWriterRunId !== expectedSession.activeWriterRunId))
) {
// Successor acceptance owns identity changes. Finalizers may update only
// their exact still-current row and cannot recreate a deleted owner.
return null;
}
return preserveUserFacingRunState
? metadataPatch
: projectSessionSnapshotChanges({
initial: entry,
next,
current: currentEntry,
reassertAbortedLastRun: result.meta.aborted === true,
});
},
{
...(preserveUserFacingRunState || params.compactionAccounting
? {}
: { fallbackEntry: entry }),
maintenanceConfig,
onCommitted: (committed) => {
// Maintenance may yield to a newer writer before the patch promise returns.
sessionStore[sessionKey] = committed;
},
},
);
}
type CliSessionForkStoreParams = {
provider: string;
sessionKey: string;
sessionStore: Record<string, SessionEntry>;
storePath: string;
expectedCliSessionId: string;
assertCommitAllowed?: () => void;
};
function isSameSessionLifecycleOwner(
current: InternalSessionEntry,
expected: InternalSessionEntry,
): boolean {
return (
current.sessionId === expected.sessionId &&
current.lifecycleRevision === expected.lifecycleRevision &&
current.activeWriterRunId === expected.activeWriterRunId
);
}
async function patchCliSessionForkBinding(
params: CliSessionForkStoreParams,
updateBinding: (binding: CliSessionBinding) => CliSessionBinding | undefined,
): Promise<SessionEntry | undefined> {
const { provider, sessionKey, sessionStore, storePath, expectedCliSessionId } = params;
const entry = sessionStore[sessionKey];
if (!entry || entry.cliSessionBindings?.[provider]?.sessionId !== expectedCliSessionId) {
return undefined;
}
let committed: SessionEntry | undefined;
await patchSessionEntryCore(
{ storePath, sessionKey },
(currentEntry) => {
const currentBinding = currentEntry.cliSessionBindings?.[provider];
// A binding id can survive session rollover. Fork authority belongs to the exact lifecycle.
if (
!isSameSessionLifecycleOwner(currentEntry, entry) ||
currentBinding?.sessionId !== expectedCliSessionId
) {
return null;
}
const nextBinding = updateBinding(currentBinding);
if (!nextBinding) {
return null;
}
const next = { ...currentEntry };
setCliSessionBinding(next, provider, nextBinding);
return next;
},
{
assertCommitAllowed: params.assertCommitAllowed,
onCommitted: (current) => {
// Only the commit edge proves this transition and owns cache publication.
committed = current;
sessionStore[sessionKey] = current;
},
},
);
return committed;
}
/** Clears the one-shot fork marker before the resumed CLI process starts. */
export async function consumeCliSessionForkInStore(
params: CliSessionForkStoreParams,
): Promise<SessionEntry | undefined> {
return await patchCliSessionForkBinding(params, (binding) => {
if (binding.forkNextResume !== true) {
return undefined;
}
const { forkNextResume: _forkNextResume, ...consumedBinding } = binding;
return consumedBinding;
});
}
/** Arms a fork marker for recovery, or re-arms one after a failed CLI turn. */
export async function restoreCliSessionForkInStore(
params: CliSessionForkStoreParams,
): Promise<SessionEntry | undefined> {
return await patchCliSessionForkBinding(params, (binding) =>
binding.forkNextResume === true ? undefined : { ...binding, forkNextResume: true },
);
}
/** Rebinds a claimed fork to its successor before the rest of the CLI turn can fail. */
export async function persistCliSessionForkSuccessorInStore(
params: CliSessionForkStoreParams & {
successorCliSessionId: string;
},
): Promise<SessionEntry | undefined> {
if (params.successorCliSessionId === params.expectedCliSessionId) {
return undefined;
}
return await patchCliSessionForkBinding(params, (binding) =>
binding.forkNextResume === true
? undefined
: { ...binding, sessionId: params.successorCliSessionId, forceReuse: true },
);
}
/** Records CLI compaction metadata on the persisted session entry. */
export async function recordCliCompactionInStore(params: {
compactionKind: NonNullable<EmbeddedAgentCompactResult["compactionKind"]>;
sessionKey: string;
sessionStore: Record<string, SessionEntry>;
storePath: string;
tokensAfter?: number;
expectedSession: Pick<
InternalSessionEntry,
"sessionId" | "lifecycleRevision" | "activeWriterRunId"
>;
}): Promise<SessionEntry | undefined> {
const { compactionKind, sessionKey, sessionStore, storePath, expectedSession } = params;
const entry = sessionStore[sessionKey];
if (!entry) {
return undefined;
}
const next = { ...entry };
// A shared-history rewrite invalidates every binding; native compaction preserves its session.
if (compactionKind === "context-engine") {
clearAllCliSessions(next);
}
next.compactionCount = (entry.compactionCount ?? 0) + 1;
next.updatedAt = Date.now();
const tokensAfterCompaction = asNonNegativeFiniteNumber(params.tokensAfter);
next.contextBudgetStatus = undefined;
Object.assign(next, COMPACTION_RUN_USAGE_CLEAR_PATCH);
if (tokensAfterCompaction !== undefined) {
next.totalTokens = Math.floor(tokensAfterCompaction);
next.totalTokensFresh = true;
next.totalTokensVersion = SESSION_TOTAL_TOKENS_VERSION;
} else {
next.totalTokensFresh = false;
next.totalTokensVersion = undefined;
}
let committedEntry: SessionEntry | undefined;
await patchSessionEntryCore(
{
storePath,
sessionKey,
},
(currentEntry, context) => {
if (
!context.existingEntry ||
currentEntry.sessionId !== expectedSession.sessionId ||
currentEntry.lifecycleRevision !== expectedSession.lifecycleRevision ||
currentEntry.activeWriterRunId !== expectedSession.activeWriterRunId
) {
return null;
}
return {
...currentEntry,
...projectSessionSnapshotChanges({ initial: entry, next, current: currentEntry }),
compactionCount: (currentEntry.compactionCount ?? 0) + 1,
};
},
{
onCommitted: (committed) => {
// Retain the committed fact without overwriting a later writer's cache on return.
committedEntry = committed;
sessionStore[sessionKey] = committed;
},
},
);
return committedEntry;
}
|