Files
openclaw/extensions/codex/src/app-server/thread-archive-guard.ts
Peter Steinberger f94a7dc183 feat(codex): supervise native Codex sessions (#104045)
* feat(codex): add native session supervision

* fix(codex): harden supervision integration

* fix(codex): preserve locked harness ownership

* fix(codex): fence native session archive

* fix(codex): revalidate archive binding ownership

* feat(codex): integrate supervision runtime

* feat(sessions): preserve harness-owned execution

* feat(sessions): persist harness ownership invariants

* feat(gateway): enforce harness-owned sessions

* feat(setup): enable detected Codex supervision

* feat(mac): expose supervised Codex sessions

* feat(ui): make Codex sessions actionable

* docs(codex): document session supervision

* test(codex): cover integration ownership

* chore(i18n): refresh supervision inventories

* fix(setup): finalize Codex activation atomically

* test(codex): narrow binding store update

* fix(sessions): preserve legacy model locks

* test(macos): serialize Codex catalog fixtures

* fix(sessions): preserve legacy lock admission

* chore(i18n): reconcile supervision metadata

* test(sessions): mark legacy lock fixture

* fix(macos): drain final Codex catalog frame

* docs: leave supervision note to release

* style(macos): satisfy Codex catalog type length

* chore: record session accessor seam owners

* fix(macos): honor configured Codex supervision

* fix(codex): preserve harness-owned model locks

* fix(codex): satisfy supervision lint gates

* chore(i18n): refresh native supervision inventory

* fix(codex): align supervision validation contracts

* fix(codex): close supervision boundary gaps

* fix(codex): preserve supervision activation contracts

* fix(codex): dispose standalone supervision runtime

* fix(codex): pin supervised source connection

* fix(plugins): bind delegated runs to exact session target

* fix(codex): scope supervised sessions to configured agents

* fix(codex): fingerprint effective supervision home

* fix(codex): normalize supervision plugin policy

* fix(codex): keep supervised bindings stable across upgrades

* fix(codex): guard all supervised binding connections

* fix(codex): preserve catalog filters and pending CAS identity

* fix(codex): preserve supervision identity for diagnostics

* fix(codex): bind uncertain commits to supervision connection

* fix(codex): satisfy supervision type boundaries

* fix(macos): reconcile current main validation

* fix(codex): handle absent runtime config in supervision

* fix(doctor): own local audio acceleration check

* fix(codex): satisfy integration lint gates

* fix(codex): satisfy lifecycle safety guards
2026-07-11 00:12:08 -07:00

96 lines
3.4 KiB
TypeScript

import { isJsonObject, type CodexThreadListParams } from "./protocol.js";
import type { CodexAppServerBindingStore } from "./session-binding.js";
const DESCENDANT_PAGE_LIMIT = 100;
const MAX_DESCENDANT_PAGES = 100;
const MAX_THREAD_ID_LENGTH = 256;
const MAX_CURSOR_LENGTH = 4096;
function readBoundedId(value: unknown): string | undefined {
if (typeof value !== "string") {
return undefined;
}
const normalized = value.trim();
return normalized && normalized.length <= MAX_THREAD_ID_LENGTH ? normalized : undefined;
}
function readNextCursor(value: unknown): string | undefined {
if (value === undefined || value === null) {
return undefined;
}
if (typeof value !== "string" || !value.trim() || value.length > MAX_CURSOR_LENGTH) {
throw new Error("Codex app-server returned an invalid descendant-list cursor");
}
return value;
}
/**
* Native archive includes the spawned subtree. Enumerate that same subtree first so an
* OpenClaw-owned descendant cannot be stopped as an undocumented side effect.
*/
export async function assertCodexArchiveDescendantsUnowned(params: {
bindingStore: CodexAppServerBindingStore;
threadId: string;
listPage: (request: CodexThreadListParams) => Promise<unknown>;
assertDescendantIdle: (threadId: string) => Promise<void>;
}): Promise<void> {
const ancestorThreadId = readBoundedId(params.threadId);
if (!ancestorThreadId) {
throw new Error("cannot verify Codex archive descendants for an invalid thread id");
}
const seenCursors = new Set<string>();
const seenThreadIds = new Set<string>([ancestorThreadId]);
let cursor: string | undefined;
for (let pageIndex = 0; pageIndex < MAX_DESCENDANT_PAGES; pageIndex += 1) {
const response = await params.listPage({
ancestorThreadId,
archived: false,
limit: DESCENDANT_PAGE_LIMIT,
sortKey: "created_at",
sortDirection: "desc",
useStateDbOnly: true,
...(cursor ? { cursor } : {}),
});
if (!isJsonObject(response) || !Array.isArray(response.data)) {
throw new Error("Codex app-server returned an invalid descendant-list response");
}
if (response.data.length > DESCENDANT_PAGE_LIMIT) {
throw new Error("Codex app-server exceeded the descendant-list page limit");
}
for (const value of response.data) {
if (!isJsonObject(value)) {
throw new Error("Codex app-server returned an invalid descendant thread");
}
const descendantThreadId = readBoundedId(value.id);
if (!descendantThreadId) {
throw new Error("Codex app-server returned a descendant without a valid thread id");
}
if (seenThreadIds.has(descendantThreadId)) {
throw new Error("Codex app-server returned a cyclic descendant thread list");
}
seenThreadIds.add(descendantThreadId);
await params.assertDescendantIdle(descendantThreadId);
if (await params.bindingStore.hasOtherThreadOwner(descendantThreadId)) {
throw new Error(
"cannot archive a Codex thread while a spawned descendant is owned by an OpenClaw session",
);
}
}
const nextCursor = readNextCursor(response.nextCursor);
if (!nextCursor) {
return;
}
if (seenCursors.has(nextCursor)) {
throw new Error("Codex app-server returned a repeated descendant-list cursor");
}
seenCursors.add(nextCursor);
cursor = nextCursor;
}
throw new Error("Codex descendant enumeration exceeded its safety limit");
}