Skip to content

Commit 087fcf4

Browse files
committed
docs: document cli runner history helpers
1 parent 1d7d8a1 commit 087fcf4

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/agents/cli-runner.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Top-level CLI-backed agent runner orchestration.
3+
*/
14
import type { ReplyPayload } from "../auto-reply/reply-payload.js";
25
import { SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js";
36
import { formatErrorMessage } from "../infra/errors.js";
@@ -44,10 +47,12 @@ const cliRunnerDeps = {
4447
},
4548
};
4649

50+
/** Overrides top-level CLI runner dependencies for tests. */
4751
export function setCliRunnerTestDeps(overrides: Partial<typeof cliRunnerDeps>): void {
4852
Object.assign(cliRunnerDeps, overrides);
4953
}
5054

55+
/** Restores default top-level CLI runner dependencies after tests. */
5156
export function restoreCliRunnerTestDeps(): void {
5257
cliRunnerDeps.claudeCliSessionTranscriptHasContent = claudeCliSessionTranscriptHasContentImpl;
5358
cliRunnerDeps.delay = async (delayMs: number) => {
@@ -80,6 +85,7 @@ function shouldRetryFreshCliSessionAfterFailover(params: {
8085
}
8186
}
8287

88+
/** Checks whether a Claude CLI session binding has reached its transcript file. */
8389
export async function isCliBindingFlushed(
8490
sessionId: string | undefined,
8591
provider: string | undefined,
@@ -281,6 +287,7 @@ async function finalizeCliContextEngineTurn(params: {
281287
}
282288
}
283289

290+
/** Prepares and runs one CLI-backed agent turn. */
284291
export async function runCliAgent(params: RunCliAgentParams): Promise<EmbeddedAgentRunResult> {
285292
// Cron gate must fire before prepareCliRunContext — that call allocates
286293
// backend resources released only by runPreparedCliAgent's try…finally.
@@ -351,6 +358,7 @@ export async function runCliAgent(params: RunCliAgentParams): Promise<EmbeddedAg
351358
}
352359
}
353360

361+
/** Runs an already-prepared CLI agent context through hooks and execution. */
354362
export async function runPreparedCliAgent(
355363
context: PreparedCliRunContext,
356364
): Promise<EmbeddedAgentRunResult> {
@@ -850,11 +858,13 @@ export async function runPreparedCliAgent(
850858
}
851859
}
852860

861+
/** Legacy Claude-specific wrapper params for the generic CLI runner. */
853862
export type RunClaudeCliAgentParams = Omit<RunCliAgentParams, "provider" | "cliSessionId"> & {
854863
provider?: string;
855864
claudeSessionId?: string;
856865
};
857866

867+
/** Converts legacy Claude CLI wrapper params into generic CLI runner params. */
858868
export function buildRunClaudeCliAgentParams(params: RunClaudeCliAgentParams): RunCliAgentParams {
859869
return {
860870
sessionId: params.sessionId,
@@ -892,6 +902,7 @@ export function buildRunClaudeCliAgentParams(params: RunClaudeCliAgentParams): R
892902
};
893903
}
894904

905+
/** Runs the legacy Claude CLI wrapper through the generic CLI runner. */
895906
export async function runClaudeCliAgent(
896907
params: RunClaudeCliAgentParams,
897908
): Promise<EmbeddedAgentRunResult> {

src/agents/cli-runner/session-history.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Loads and renders persisted session history for CLI session reseeding and
3+
* context-engine synchronization.
4+
*/
15
import fsp from "node:fs/promises";
26
import path from "node:path";
37
import {
@@ -14,9 +18,13 @@ import {
1418
import type { AgentMessage } from "../runtime/index.js";
1519
import { migrateSessionEntries, parseSessionEntries } from "../sessions/session-manager.js";
1620

21+
/** Maximum transcript size read for CLI session history. */
1722
export const MAX_CLI_SESSION_HISTORY_FILE_BYTES = 5 * 1024 * 1024;
23+
/** Maximum transcript messages exposed to CLI hook history. */
1824
export const MAX_CLI_SESSION_HISTORY_MESSAGES = MAX_AGENT_HOOK_HISTORY_MESSAGES;
25+
/** Minimum reseed-history prompt budget for fresh CLI sessions. */
1926
export const MAX_CLI_SESSION_RESEED_HISTORY_CHARS = 12 * 1024;
27+
/** Maximum automatic reseed-history prompt budget derived from context size. */
2028
export const MAX_AUTO_CLI_SESSION_RESEED_HISTORY_CHARS = 256 * 1024;
2129
const CLI_SESSION_RESEED_HISTORY_CONTEXT_SHARE = 0.08;
2230
const CHARS_PER_TOKEN_ESTIMATE = 4;
@@ -60,6 +68,7 @@ const RAW_TRANSCRIPT_RESEED_ALLOWED_REASONS = new Set<RawTranscriptReseedReason>
6068
"session-expired",
6169
]);
6270

71+
/** Resolves how much prior transcript text may reseed a fresh CLI session. */
6372
export function resolveAutoCliSessionReseedHistoryChars(contextWindowTokens: number): number {
6473
if (!Number.isFinite(contextWindowTokens) || contextWindowTokens <= 0) {
6574
return MAX_CLI_SESSION_RESEED_HISTORY_CHARS;
@@ -157,6 +166,7 @@ function renderHistoryMessage(message: unknown): string | undefined {
157166
return text ? `${role}: ${text}` : undefined;
158167
}
159168

169+
/** Builds a reseed prompt that carries prior OpenClaw transcript context. */
160170
export function buildCliSessionHistoryPrompt(params: {
161171
messages: unknown[];
162172
prompt: string;
@@ -325,6 +335,7 @@ async function loadCliSessionEntries(params: {
325335
}
326336
}
327337

338+
/** Checks whether a safe, bounded transcript file exists for a CLI session. */
328339
export async function hasCliSessionTranscript(params: {
329340
sessionId: string;
330341
sessionFile: string;
@@ -354,6 +365,7 @@ export async function hasCliSessionTranscript(params: {
354365
}
355366
}
356367

368+
/** Loads transcript messages for CLI lifecycle hook context. */
357369
export async function loadCliSessionHistoryMessages(params: {
358370
sessionId: string;
359371
sessionFile: string;
@@ -368,6 +380,7 @@ export async function loadCliSessionHistoryMessages(params: {
368380
return limitAgentHookHistoryMessages(history, MAX_CLI_SESSION_HISTORY_MESSAGES);
369381
}
370382

383+
/** Loads transcript messages formatted for context-engine updates. */
371384
export async function loadCliSessionContextEngineMessages(params: {
372385
sessionId: string;
373386
sessionFile: string;
@@ -411,6 +424,7 @@ export async function loadCliSessionContextEngineMessages(params: {
411424
];
412425
}
413426

427+
/** Loads compacted/raw transcript messages eligible for CLI session reseeding. */
414428
export async function loadCliSessionReseedMessages(params: {
415429
sessionId: string;
416430
sessionFile: string;

src/agents/cli-runner/toml-inline.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
/**
2+
* Minimal TOML inline serializer for CLI config overrides.
3+
*/
14
import { isRecord } from "@openclaw/normalization-core/record-coerce";
25

3-
// Minimal TOML inline serializer for CLI config overrides.
46
function escapeTomlString(value: string): string {
57
return value.replaceAll("\\", "\\\\").replaceAll('"', '\\"');
68
}

0 commit comments

Comments
 (0)