mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-12 21:53:00 -06:00
fa03d9b913
* refactor: consolidate coercion helpers * fix: remove duplicate coercion imports * fix: preserve serialized coercion guard * chore: ratchet coercion helper carve-outs * fix(test): keep gauntlet subprocess startup lean * fix: preserve imported session timestamp semantics * fix: preserve catalog timestamp string semantics * chore: align plugin SDK surface ratchet * fix: preserve trajectory and SDK string contracts * fix(test): preserve QA record assertion semantics * fix: complete standalone record guard rename * refactor(cron): use canonical string coercion * fix(acpx): preserve Pi timestamp parsing * test(channels): adapt custody test harnesses * test(telegram): classify media harness as test support * test(acpx): split timestamp contract coverage * test(channels): support generated custody contracts * chore: ban the full coercion helper name set Extends the declaration guard to all eleven consolidated helper names and renames the cron schedule-identity readNumber wrapper to readScheduleInteger so the banned generic name cannot regrow. * fix(scripts): repair release-validation guard drift and lint cause Restores the renamed isJsonRecord guard in assertTrustedWorkflowHarness after main added isRecord call sites in parallel, and attaches the caught YAML error as the thrown error cause (preserve-caught-error was red on main). * fix: preserve Claude timestamp string semantics * fix: preserve persisted timestamp string semantics * fix: preserve date-first timestamp contracts * fix(openai): harden delegation failure formatting * chore: close coercion helper guard gaps * test(openai): model non-error delegation rejection * chore: refresh plugin SDK API contract * fix(tasks): use canonical string field reader * fix(ai): use canonical provider error field coercion * fix(browser): migrate native bootstrap coercion * docs(plugin-sdk): clarify text record export compatibility * fix(gateway): normalize approval execution identity * test(outbound): isolate message action poll harness
284 lines
9.1 KiB
TypeScript
284 lines
9.1 KiB
TypeScript
import {
|
|
asOptionalRecord,
|
|
normalizeLowercaseStringOrEmpty,
|
|
normalizeOptionalString,
|
|
} from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
import {
|
|
extractActiveMemorySearchDebugFromSessionRecord,
|
|
extractToolResultNameFromSessionRecord,
|
|
fileTranscriptSource,
|
|
hasTerminalUnavailableMemoryResultInSessionRecord,
|
|
hasUnavailableMemoryResultInSessionRecord,
|
|
hasUsableMemoryResultInSessionRecord,
|
|
streamActiveMemoryTranscriptRecords,
|
|
} from "./transcript.js";
|
|
import {
|
|
TERMINAL_MEMORY_SEARCH_POLL_INTERVAL_MS,
|
|
type ActiveMemorySearchDebug,
|
|
type ActiveMemoryTranscriptSource,
|
|
type TerminalMemorySearchResult,
|
|
type TerminalMemorySearchWatch,
|
|
type TranscriptReadLimits,
|
|
} from "./types.js";
|
|
|
|
async function readActiveMemoryTranscriptState(
|
|
source: ActiveMemoryTranscriptSource | string,
|
|
limits?: TranscriptReadLimits,
|
|
toolsAllow?: readonly string[],
|
|
): Promise<{
|
|
searchDebug?: ActiveMemorySearchDebug;
|
|
hasUsableMemoryResult: boolean;
|
|
hasUnavailableMemorySearchResult: boolean;
|
|
}> {
|
|
let searchDebug: ActiveMemorySearchDebug | undefined;
|
|
let hasUsableMemoryResult = false;
|
|
let hasUnavailableMemorySearchResult = false;
|
|
await streamActiveMemoryTranscriptRecords({
|
|
source: typeof source === "string" ? fileTranscriptSource(source) : source,
|
|
limits,
|
|
onRecord: (record) => {
|
|
const debug = extractActiveMemorySearchDebugFromSessionRecord(record);
|
|
if (debug) {
|
|
searchDebug = debug;
|
|
}
|
|
hasUnavailableMemorySearchResult ||= hasUnavailableMemoryResultInSessionRecord(
|
|
record,
|
|
toolsAllow,
|
|
);
|
|
hasUsableMemoryResult ||= hasUsableMemoryResultInSessionRecord(record, toolsAllow);
|
|
},
|
|
});
|
|
return { searchDebug, hasUsableMemoryResult, hasUnavailableMemorySearchResult };
|
|
}
|
|
|
|
async function readActiveMemorySearchDebug(
|
|
source: ActiveMemoryTranscriptSource | string,
|
|
limits?: TranscriptReadLimits,
|
|
): Promise<ActiveMemorySearchDebug | undefined> {
|
|
return (await readActiveMemoryTranscriptState(source, limits)).searchDebug;
|
|
}
|
|
|
|
async function readMergedActiveMemoryTranscriptState(params: {
|
|
sources: readonly ActiveMemoryTranscriptSource[];
|
|
toolsAllow: readonly string[];
|
|
}): Promise<{
|
|
searchDebug?: ActiveMemorySearchDebug;
|
|
hasUsableMemoryResult: boolean;
|
|
hasUnavailableMemorySearchResult: boolean;
|
|
}> {
|
|
let searchDebug: ActiveMemorySearchDebug | undefined;
|
|
let hasUsableMemoryResult = false;
|
|
let hasUnavailableMemorySearchResult = false;
|
|
const seen = new Set<string>();
|
|
for (const source of params.sources) {
|
|
const key =
|
|
source.kind === "runtime"
|
|
? `runtime:${source.target.agentId ?? ""}:${source.target.sessionId}:${source.target.sessionKey}:${source.target.storePath ?? ""}:${source.target.threadId ?? ""}`
|
|
: `file:${source.sessionFile}`;
|
|
if (seen.has(key)) {
|
|
continue;
|
|
}
|
|
seen.add(key);
|
|
const state = await readActiveMemoryTranscriptState(source, undefined, params.toolsAllow);
|
|
searchDebug = state.searchDebug ?? searchDebug;
|
|
hasUsableMemoryResult ||= state.hasUsableMemoryResult;
|
|
hasUnavailableMemorySearchResult ||= state.hasUnavailableMemorySearchResult;
|
|
}
|
|
return { searchDebug, hasUsableMemoryResult, hasUnavailableMemorySearchResult };
|
|
}
|
|
|
|
async function readTerminalMemorySearchResult(
|
|
source: ActiveMemoryTranscriptSource,
|
|
limits?: TranscriptReadLimits,
|
|
toolsAllow?: readonly string[],
|
|
): Promise<TerminalMemorySearchResult | undefined> {
|
|
// memory_get consumes a path discovered by another tool; it is not an
|
|
// independent fallback that should delay terminal unavailability.
|
|
const recallPathNames = new Set(
|
|
toolsAllow
|
|
?.map((toolName) => normalizeLowercaseStringOrEmpty(toolName))
|
|
.filter((toolName) => toolName && toolName !== "memory_get"),
|
|
);
|
|
if (recallPathNames.size === 0) {
|
|
return undefined;
|
|
}
|
|
const unavailablePathNames = new Set<string>();
|
|
let hasUsableMemoryResult = false;
|
|
let searchDebug: ActiveMemorySearchDebug | undefined;
|
|
await streamActiveMemoryTranscriptRecords({
|
|
source,
|
|
limits,
|
|
onRecord: (record) => {
|
|
hasUsableMemoryResult ||= hasUsableMemoryResultInSessionRecord(record, toolsAllow);
|
|
searchDebug = extractActiveMemorySearchDebugFromSessionRecord(record) ?? searchDebug;
|
|
const toolName = extractToolResultNameFromSessionRecord(record);
|
|
if (!toolName || !recallPathNames.has(toolName)) {
|
|
return false;
|
|
}
|
|
if (hasTerminalUnavailableMemoryResultInSessionRecord(record, toolsAllow ?? [])) {
|
|
unavailablePathNames.add(toolName);
|
|
} else {
|
|
unavailablePathNames.delete(toolName);
|
|
}
|
|
return false;
|
|
},
|
|
});
|
|
if (unavailablePathNames.size !== recallPathNames.size) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
status: "unavailable",
|
|
hasUsableMemoryResult,
|
|
searchDebug,
|
|
};
|
|
}
|
|
|
|
async function readTerminalMemorySearchResultFromSources(
|
|
sources: readonly ActiveMemoryTranscriptSource[],
|
|
limits: TranscriptReadLimits | undefined,
|
|
toolsAllow: readonly string[],
|
|
): Promise<TerminalMemorySearchResult | undefined> {
|
|
for (const source of sources) {
|
|
const result = await readTerminalMemorySearchResult(source, limits, toolsAllow);
|
|
if (result) {
|
|
return result;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function watchTerminalMemorySearchResult(params: {
|
|
getTranscriptSources: () => readonly ActiveMemoryTranscriptSource[];
|
|
abortSignal: AbortSignal;
|
|
toolsAllow: readonly string[];
|
|
}): TerminalMemorySearchWatch {
|
|
let stopped = false;
|
|
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
|
let inFlight = false;
|
|
let resolveWatch: (result: TerminalMemorySearchResult) => void = () => {};
|
|
const stop = () => {
|
|
if (stopped) {
|
|
return;
|
|
}
|
|
stopped = true;
|
|
if (timeoutId) {
|
|
clearTimeout(timeoutId);
|
|
timeoutId = undefined;
|
|
}
|
|
params.abortSignal.removeEventListener("abort", onAbort);
|
|
};
|
|
const finish = (result: TerminalMemorySearchResult) => {
|
|
stop();
|
|
resolveWatch(result);
|
|
};
|
|
const schedule = () => {
|
|
if (stopped) {
|
|
return;
|
|
}
|
|
timeoutId = setTimeout(() => {
|
|
void tick();
|
|
}, TERMINAL_MEMORY_SEARCH_POLL_INTERVAL_MS);
|
|
timeoutId.unref?.();
|
|
};
|
|
const tick = async () => {
|
|
if (stopped || inFlight) {
|
|
return;
|
|
}
|
|
if (params.abortSignal.aborted) {
|
|
stop();
|
|
return;
|
|
}
|
|
inFlight = true;
|
|
try {
|
|
const result = await readTerminalMemorySearchResultFromSources(
|
|
params.getTranscriptSources(),
|
|
undefined,
|
|
params.toolsAllow,
|
|
);
|
|
if (result) {
|
|
finish(result);
|
|
return;
|
|
}
|
|
} catch {
|
|
// Transcript polling is opportunistic; normal timeout handling remains authoritative.
|
|
} finally {
|
|
inFlight = false;
|
|
}
|
|
schedule();
|
|
};
|
|
function onAbort() {
|
|
stop();
|
|
}
|
|
const promise = new Promise<TerminalMemorySearchResult>((resolve) => {
|
|
resolveWatch = resolve;
|
|
params.abortSignal.addEventListener("abort", onAbort, { once: true });
|
|
void tick();
|
|
});
|
|
return {
|
|
promise,
|
|
stop,
|
|
};
|
|
}
|
|
|
|
function normalizeSearchDebug(value: unknown): ActiveMemorySearchDebug | undefined {
|
|
const debug = asOptionalRecord(value);
|
|
if (!debug) {
|
|
return undefined;
|
|
}
|
|
const normalized: ActiveMemorySearchDebug = {
|
|
backend: normalizeOptionalString(debug.backend),
|
|
configuredMode: normalizeOptionalString(debug.configuredMode),
|
|
effectiveMode: normalizeOptionalString(debug.effectiveMode),
|
|
fallback: normalizeOptionalString(debug.fallback),
|
|
searchMs:
|
|
typeof debug.searchMs === "number" && Number.isFinite(debug.searchMs)
|
|
? debug.searchMs
|
|
: undefined,
|
|
hits: typeof debug.hits === "number" && Number.isFinite(debug.hits) ? debug.hits : undefined,
|
|
warning: normalizeOptionalString(debug.warning) ?? normalizeOptionalString(debug.reason),
|
|
action: normalizeOptionalString(debug.action),
|
|
error: normalizeOptionalString(debug.error),
|
|
};
|
|
return normalized.backend ||
|
|
normalized.configuredMode ||
|
|
normalized.effectiveMode ||
|
|
normalized.fallback ||
|
|
typeof normalized.searchMs === "number" ||
|
|
typeof normalized.hits === "number" ||
|
|
normalized.warning ||
|
|
normalized.action ||
|
|
normalized.error
|
|
? normalized
|
|
: undefined;
|
|
}
|
|
|
|
function readActiveMemorySearchDebugFromRunResult(
|
|
result: unknown,
|
|
): ActiveMemorySearchDebug | undefined {
|
|
const record = asOptionalRecord(result);
|
|
const meta = asOptionalRecord(record?.meta);
|
|
return (
|
|
normalizeSearchDebug(meta?.activeMemorySearchDebug) ??
|
|
normalizeSearchDebug(meta?.memorySearchDebug) ??
|
|
normalizeSearchDebug(record?.activeMemorySearchDebug) ??
|
|
normalizeSearchDebug(record?.memorySearchDebug)
|
|
);
|
|
}
|
|
|
|
function readActiveMemorySessionFileFromRunResult(result: unknown): string | undefined {
|
|
const record = asOptionalRecord(result);
|
|
const meta = asOptionalRecord(record?.meta);
|
|
const agentMeta = asOptionalRecord(meta?.agentMeta);
|
|
return (
|
|
normalizeOptionalString(agentMeta?.sessionFile) ?? normalizeOptionalString(meta?.sessionFile)
|
|
);
|
|
}
|
|
|
|
export {
|
|
readActiveMemorySearchDebug,
|
|
readActiveMemorySearchDebugFromRunResult,
|
|
readActiveMemorySessionFileFromRunResult,
|
|
readMergedActiveMemoryTranscriptState,
|
|
watchTerminalMemorySearchResult,
|
|
};
|