@unblocklabs/unblock-codex-prompt 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +376 -70
- package/dist/adapter.d.ts +23 -25
- package/dist/adapter.js +141 -152
- package/dist/claude-adapter.d.ts +32 -0
- package/dist/claude-adapter.js +91 -0
- package/dist/claude-prompt.d.ts +36 -0
- package/dist/claude-prompt.js +134 -0
- package/dist/compatibility.d.ts +1 -1
- package/dist/compatibility.js +4 -3
- package/dist/compiler.d.ts +103 -13
- package/dist/compiler.js +42 -37
- package/dist/index.js +249 -64
- package/dist/managed-target.d.ts +25 -0
- package/dist/managed-target.js +46 -0
- package/dist/manager.d.ts +72 -18
- package/dist/manager.js +131 -40
- package/dist/patched-files.d.ts +117 -0
- package/dist/patched-files.js +160 -0
- package/dist/refresh-error.js +2 -0
- package/dist/remote.d.ts +27 -2
- package/dist/remote.js +23 -4
- package/dist/runtime.d.ts +23 -1
- package/dist/runtime.js +84 -14
- package/dist/runtimes.d.ts +34 -0
- package/dist/runtimes.js +79 -0
- package/openclaw.plugin.json +4 -4
- package/package.json +8 -8
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { readOptional, replaceFile, sha256 } from "./manager.js";
|
|
2
|
+
async function observe(recorded) {
|
|
3
|
+
return Promise.all(recorded.files.map(async (file) => {
|
|
4
|
+
const current = await readOptional(file.path);
|
|
5
|
+
return { file, current, hash: current === null ? null : sha256(current) };
|
|
6
|
+
}));
|
|
7
|
+
}
|
|
8
|
+
const holdsPatched = ({ file, hash }) => hash !== null && (hash === file.patchedSha256 || hash === file.previousPatchedSha256);
|
|
9
|
+
const holdsOriginal = ({ file, hash }) => hash === file.originalSha256;
|
|
10
|
+
/** An update replaced the recorded install: only receipts that recorded a version can say so. */
|
|
11
|
+
async function versionChanged(set, recorded) {
|
|
12
|
+
return recorded.version !== undefined && await set.installedVersion(recorded.root) !== recorded.version;
|
|
13
|
+
}
|
|
14
|
+
async function ownershipOf(set, recorded, observed) {
|
|
15
|
+
if (observed.some(holdsPatched))
|
|
16
|
+
return "patched";
|
|
17
|
+
if (observed.every(holdsOriginal))
|
|
18
|
+
return "restored";
|
|
19
|
+
// Updates rename hashed bundles (old files missing) or replace a recorded version.
|
|
20
|
+
if (observed.every(entry => holdsOriginal(entry) || entry.current === null) || await versionChanged(set, recorded))
|
|
21
|
+
return "superseded";
|
|
22
|
+
return "changed";
|
|
23
|
+
}
|
|
24
|
+
async function load(set) {
|
|
25
|
+
const source = await readOptional(set.receiptPath);
|
|
26
|
+
return { source, recorded: source === null ? null : set.parseReceipt(source) };
|
|
27
|
+
}
|
|
28
|
+
export async function patchedFilesOwnership(set) {
|
|
29
|
+
const { recorded } = await load(set);
|
|
30
|
+
return recorded ? ownershipOf(set, recorded, await observe(recorded)) : "none";
|
|
31
|
+
}
|
|
32
|
+
/** The previous receipt, its ownership, and the root a new install may default to. */
|
|
33
|
+
export async function openPatchedFiles(set) {
|
|
34
|
+
const { source, recorded } = await load(set);
|
|
35
|
+
const ownership = recorded ? await ownershipOf(set, recorded, await observe(recorded)) : "none";
|
|
36
|
+
return { previousReceipt: source, recorded, ownership };
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Start a patch at `root`. A restored or superseded receipt counts as cleared: any root is
|
|
40
|
+
* allowed and its backups may be replaced. Otherwise every recorded file must still hold this
|
|
41
|
+
* adapter's bytes (an interrupted re-patch is recognized by previousPatchedSha256) unless it
|
|
42
|
+
* is missing or the recorded version was replaced; the adapter then reads originals through
|
|
43
|
+
* `read` (backups for owned files) and hands its patched sources to `finish`.
|
|
44
|
+
*/
|
|
45
|
+
export async function beginPatch(set, opened, root) {
|
|
46
|
+
const { previousReceipt, recorded, ownership } = opened;
|
|
47
|
+
const cleared = ownership === "restored" || ownership === "superseded";
|
|
48
|
+
const kept = cleared ? null : recorded;
|
|
49
|
+
if (kept && kept.root !== root)
|
|
50
|
+
throw new Error(set.messages.installChanged);
|
|
51
|
+
const owned = new Map();
|
|
52
|
+
const superseded = [];
|
|
53
|
+
if (kept) {
|
|
54
|
+
const updated = await versionChanged(set, kept);
|
|
55
|
+
for (const entry of await observe(kept)) {
|
|
56
|
+
if (holdsPatched(entry) || holdsOriginal(entry))
|
|
57
|
+
owned.set(entry.file.path, entry.file);
|
|
58
|
+
else if (entry.current === null || updated)
|
|
59
|
+
superseded.push(entry.file);
|
|
60
|
+
else
|
|
61
|
+
throw new Error(set.messages.changed);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const originals = new Map();
|
|
65
|
+
const read = async (path) => {
|
|
66
|
+
const current = await readOptional(path);
|
|
67
|
+
if (current === null)
|
|
68
|
+
throw new Error(set.messages.missing);
|
|
69
|
+
const file = owned.get(path);
|
|
70
|
+
const original = file ? await readOptional(kept.backupPath(file)) : current;
|
|
71
|
+
if (original === null || (file && sha256(original) !== file.originalSha256))
|
|
72
|
+
throw new Error(set.messages.prepareBackupInvalid);
|
|
73
|
+
originals.set(path, { current, original });
|
|
74
|
+
return original;
|
|
75
|
+
};
|
|
76
|
+
/** `patched` maps each changed path (read through `read`) to its patched source, in receipt order. */
|
|
77
|
+
const finish = (patched) => {
|
|
78
|
+
if ([...owned.keys()].some(path => !patched.has(path)))
|
|
79
|
+
throw new Error(set.messages.moved);
|
|
80
|
+
const files = [...patched].map(([path, contents]) => {
|
|
81
|
+
const source = originals.get(path);
|
|
82
|
+
if (!source)
|
|
83
|
+
throw new Error(set.messages.missing);
|
|
84
|
+
return { path, ...source, patched: contents };
|
|
85
|
+
});
|
|
86
|
+
const entries = files.map(file => ({
|
|
87
|
+
path: file.path, originalSha256: sha256(file.original), patchedSha256: sha256(file.patched),
|
|
88
|
+
// Receipt-first upgrades must remain recoverable if source swapping is interrupted.
|
|
89
|
+
...file.current !== file.original && file.current !== file.patched ? { previousPatchedSha256: sha256(file.current) } : {},
|
|
90
|
+
}));
|
|
91
|
+
// Backups of a cleared receipt, or of files an update superseded, may be replaced; any other is foreign.
|
|
92
|
+
const staleBackups = new Set((cleared ? recorded.files : superseded).map(file => file.originalSha256));
|
|
93
|
+
return { files, entries, previousReceipt, staleBackups };
|
|
94
|
+
};
|
|
95
|
+
return { read, finish };
|
|
96
|
+
}
|
|
97
|
+
/** Validates every target before any change; keeps exact original backups, then the receipt, then the files. */
|
|
98
|
+
export async function installPatch(set, plan, receipt) {
|
|
99
|
+
const backups = await Promise.all(plan.files.map(async (file) => {
|
|
100
|
+
const path = set.backupPath(file.path);
|
|
101
|
+
const backup = await readOptional(path);
|
|
102
|
+
if (backup !== null && backup !== file.original && !plan.staleBackups.has(sha256(backup)))
|
|
103
|
+
throw new Error(set.messages.foreignBackup);
|
|
104
|
+
if (await readOptional(file.path) !== file.current)
|
|
105
|
+
throw new Error(set.messages.concurrent);
|
|
106
|
+
return { path, backup, original: file.original };
|
|
107
|
+
}));
|
|
108
|
+
for (const backup of backups)
|
|
109
|
+
await replaceFile(backup.path, backup.backup, backup.original);
|
|
110
|
+
await replaceFile(set.receiptPath, plan.previousReceipt, receipt);
|
|
111
|
+
let adapterChanged = false;
|
|
112
|
+
for (const file of plan.files)
|
|
113
|
+
adapterChanged = await replaceFile(file.path, file.current, file.patched) || adapterChanged;
|
|
114
|
+
return { adapterChanged, restartRequired: true };
|
|
115
|
+
}
|
|
116
|
+
/** Disk state of every recorded file; distinguishes missing files (an update) from foreign edits. */
|
|
117
|
+
export async function patchedFilesStatus(set) {
|
|
118
|
+
const { recorded } = await load(set);
|
|
119
|
+
if (!recorded)
|
|
120
|
+
return null;
|
|
121
|
+
const states = (await observe(recorded)).map(entry => entry.current === null ? "missing" : entry.hash === entry.file.patchedSha256 ? "patched"
|
|
122
|
+
: holdsPatched(entry) ? "stale" : holdsOriginal(entry) ? "original" : "changed");
|
|
123
|
+
const state = states.every(value => value === "patched") ? "patched" : states.includes("changed") ? "changed"
|
|
124
|
+
: states.includes("missing") ? "missing" : states.some(value => value === "patched" || value === "stale") ? "partial" : "original";
|
|
125
|
+
return { recorded, state, installed: state === "patched", paths: recorded.files.map(file => file.path) };
|
|
126
|
+
}
|
|
127
|
+
export async function planPatchedRestore(set) {
|
|
128
|
+
const { recorded } = await load(set);
|
|
129
|
+
if (!recorded)
|
|
130
|
+
return { ownership: "none", files: [], superseded: [] };
|
|
131
|
+
const observed = await observe(recorded);
|
|
132
|
+
const ownership = await ownershipOf(set, recorded, observed);
|
|
133
|
+
if (ownership !== "patched")
|
|
134
|
+
return { ownership, files: [], superseded: [] };
|
|
135
|
+
const updated = await versionChanged(set, recorded);
|
|
136
|
+
const ours = [];
|
|
137
|
+
const superseded = [];
|
|
138
|
+
for (const entry of observed) {
|
|
139
|
+
if (holdsOriginal(entry))
|
|
140
|
+
continue;
|
|
141
|
+
if (holdsPatched(entry))
|
|
142
|
+
ours.push(entry);
|
|
143
|
+
else if (entry.current === null || updated)
|
|
144
|
+
superseded.push(entry.file.path);
|
|
145
|
+
else
|
|
146
|
+
throw new Error(set.messages.foreignEdit);
|
|
147
|
+
}
|
|
148
|
+
const files = await Promise.all(ours.map(async ({ file, current }) => {
|
|
149
|
+
const original = await readOptional(recorded.backupPath(file));
|
|
150
|
+
if (original === null || sha256(original) !== file.originalSha256)
|
|
151
|
+
throw new Error(set.messages.backupInvalid);
|
|
152
|
+
return { path: file.path, current: current, original };
|
|
153
|
+
}));
|
|
154
|
+
return { ownership, files, superseded };
|
|
155
|
+
}
|
|
156
|
+
export async function applyPatchedRestore(plan) {
|
|
157
|
+
for (const file of plan.files)
|
|
158
|
+
await replaceFile(file.path, file.current, file.original);
|
|
159
|
+
return { ownership: plan.ownership, files: plan.files.map(file => file.path), superseded: plan.superseded, restartRequired: plan.files.length > 0 };
|
|
160
|
+
}
|
package/dist/refresh-error.js
CHANGED
|
@@ -16,6 +16,8 @@ const SAFE_CAUSES = new Set([
|
|
|
16
16
|
"Managed prompt pointer missing or changed; refusing refresh",
|
|
17
17
|
"Valid enrolled remote snapshot required; run codex-prompt sync",
|
|
18
18
|
"Frozen context boundary is ambiguous; run codex-prompt sync",
|
|
19
|
+
"Prompt endpoint returned HTTP 400 for runtime=claude; deploy the fleet-prompt Worker with runtime support first",
|
|
20
|
+
"Prompt endpoint did not confirm runtime=claude; deploy the fleet-prompt Worker with runtime support first",
|
|
19
21
|
"Prompt endpoint must return plain text", "Prompt exceeds 256 KiB limit",
|
|
20
22
|
"Prompt is empty or contains NUL bytes", "Prompt revision does not match response body",
|
|
21
23
|
"The managed codex skills block in config.toml is damaged; restore its BEGIN/END markers or remove it",
|
package/dist/remote.d.ts
CHANGED
|
@@ -1,8 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export declare const WORKER_RUNTIME_HINT = "deploy the fleet-prompt Worker with runtime support first";
|
|
2
|
+
/**
|
|
3
|
+
* Explicit sync and daily shared-prompt refresh validate before changing local state.
|
|
4
|
+
* Without `runtime` the configured URL is fetched exactly as written (the Codex artifact, which
|
|
5
|
+
* every Worker version serves). `runtime: "claude"` adds the parameter with the URL API, keeping
|
|
6
|
+
* `channel` and any other parameters, and requires the Worker to confirm it in X-Prompt-Runtime.
|
|
7
|
+
*/
|
|
8
|
+
export declare function fetchPrompt(url: string, request?: typeof fetch, options?: {
|
|
9
|
+
runtime?: "claude";
|
|
10
|
+
}): Promise<{
|
|
3
11
|
prompt: string;
|
|
4
12
|
source: {
|
|
5
13
|
url: string;
|
|
6
14
|
revision: string;
|
|
7
15
|
};
|
|
8
16
|
}>;
|
|
17
|
+
/** Per-runtime fetchers for one configured promptUrl; Codex never sends `runtime`. */
|
|
18
|
+
export declare function policyFetchers(url: string, request?: typeof fetch): {
|
|
19
|
+
codex: () => Promise<{
|
|
20
|
+
prompt: string;
|
|
21
|
+
source: {
|
|
22
|
+
url: string;
|
|
23
|
+
revision: string;
|
|
24
|
+
};
|
|
25
|
+
}>;
|
|
26
|
+
claude: () => Promise<{
|
|
27
|
+
prompt: string;
|
|
28
|
+
source: {
|
|
29
|
+
url: string;
|
|
30
|
+
revision: string;
|
|
31
|
+
};
|
|
32
|
+
}>;
|
|
33
|
+
};
|
package/dist/remote.js
CHANGED
|
@@ -1,16 +1,31 @@
|
|
|
1
1
|
import { validatePromptUrl } from "./config.js";
|
|
2
2
|
import { sha256 } from "./manager.js";
|
|
3
3
|
const MAX_BYTES = 256 * 1024;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
export const WORKER_RUNTIME_HINT = "deploy the fleet-prompt Worker with runtime support first";
|
|
5
|
+
/**
|
|
6
|
+
* Explicit sync and daily shared-prompt refresh validate before changing local state.
|
|
7
|
+
* Without `runtime` the configured URL is fetched exactly as written (the Codex artifact, which
|
|
8
|
+
* every Worker version serves). `runtime: "claude"` adds the parameter with the URL API, keeping
|
|
9
|
+
* `channel` and any other parameters, and requires the Worker to confirm it in X-Prompt-Runtime.
|
|
10
|
+
*/
|
|
11
|
+
export async function fetchPrompt(url, request = fetch, options = {}) {
|
|
12
|
+
const target = validatePromptUrl(url);
|
|
13
|
+
if (options.runtime)
|
|
14
|
+
target.searchParams.set("runtime", options.runtime);
|
|
15
|
+
const response = await request(target, {
|
|
7
16
|
redirect: "error",
|
|
8
17
|
credentials: "omit",
|
|
9
18
|
signal: AbortSignal.timeout(10_000),
|
|
10
19
|
headers: { Accept: "text/plain", "Cache-Control": "no-cache" },
|
|
11
20
|
});
|
|
21
|
+
// Workers before runtime support reject the unknown parameter with 400.
|
|
22
|
+
if (!response.ok && response.status === 400 && options.runtime)
|
|
23
|
+
throw new Error(`Prompt endpoint returned HTTP 400 for runtime=${options.runtime}; ${WORKER_RUNTIME_HINT}`);
|
|
12
24
|
if (!response.ok)
|
|
13
25
|
throw new Error(`Prompt endpoint returned HTTP ${response.status}; existing snapshot retained`);
|
|
26
|
+
if (options.runtime && response.headers.get("x-prompt-runtime") !== options.runtime) {
|
|
27
|
+
throw new Error(`Prompt endpoint did not confirm runtime=${options.runtime}; ${WORKER_RUNTIME_HINT}`);
|
|
28
|
+
}
|
|
14
29
|
const type = response.headers.get("content-type")?.split(";")[0]?.trim();
|
|
15
30
|
if (type !== "text/plain" || !response.body)
|
|
16
31
|
throw new Error("Prompt endpoint must return plain text");
|
|
@@ -38,5 +53,9 @@ export async function fetchPrompt(url, request = fetch) {
|
|
|
38
53
|
const revision = sha256(prompt);
|
|
39
54
|
if (response.headers.get("x-prompt-revision") !== revision)
|
|
40
55
|
throw new Error("Prompt revision does not match response body");
|
|
41
|
-
return { prompt, source: { url, revision } };
|
|
56
|
+
return { prompt, source: { url: options.runtime ? target.href : url, revision } };
|
|
57
|
+
}
|
|
58
|
+
/** Per-runtime fetchers for one configured promptUrl; Codex never sends `runtime`. */
|
|
59
|
+
export function policyFetchers(url, request) {
|
|
60
|
+
return { codex: () => fetchPrompt(url, request), claude: () => fetchPrompt(url, request, { runtime: "claude" }) };
|
|
42
61
|
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
1
|
export { normalizeOpenClawAdditions, normalizeOpenClawTurnContext } from "./context.js";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Called at the actual adapter assembly boundary; failures abort, not a swallowed hook. Non-null
|
|
4
|
+
* means this Codex run is managed. The value is the bundle's `policy`, which current syncs leave
|
|
5
|
+
* "": the Codex policy is the prompt-file prefix, delivered as base instructions.
|
|
6
|
+
*/
|
|
3
7
|
export declare function readFrozenPolicy(agentDir: string): string | null;
|
|
8
|
+
/**
|
|
9
|
+
* The verified base file, sent as each managed thread's base instructions at start and resume.
|
|
10
|
+
* Codex reads base instructions per session rather than from history, so a refreshed file
|
|
11
|
+
* reaches an existing thread when OpenClaw resumes it; the file itself stays the fallback.
|
|
12
|
+
*/
|
|
13
|
+
export declare function readCodexBaseInstructions(agentDir: string): string | null;
|
|
14
|
+
export type ClaudeManagedContext = {
|
|
15
|
+
policy: string;
|
|
16
|
+
agentContext: string;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The Claude policy and the shared agent context (memory reference), both read from the bundle,
|
|
20
|
+
* for the Claude CLI prompt hook. Null unless this agent has an active enrollment that includes
|
|
21
|
+
* Claude CLI (never enrolled, paused by restore, or Codex only). Throws when that enrolled state
|
|
22
|
+
* is invalid or tampered: the bundle is the trust root, and readVerifiedSnapshot checks the prompt
|
|
23
|
+
* file against its promptSha256. It never touches the Codex policy (the prompt-file prefix).
|
|
24
|
+
*/
|
|
25
|
+
export declare function readClaudeManagedContext(agentDir: string): ClaudeManagedContext | null;
|
package/dist/runtime.js
CHANGED
|
@@ -2,6 +2,7 @@ import { lstatSync, readFileSync } from "node:fs";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
4
|
import { readPromptPointer } from "./toml.js";
|
|
5
|
+
import { parseClaudePolicy, parseEnrollment } from "./manager.js";
|
|
5
6
|
export { normalizeOpenClawAdditions, normalizeOpenClawTurnContext } from "./context.js";
|
|
6
7
|
function readPrivate(path) {
|
|
7
8
|
const stat = lstatSync(path);
|
|
@@ -9,23 +10,92 @@ function readPrivate(path) {
|
|
|
9
10
|
throw new Error("Frozen bridge refuses linked or non-regular state");
|
|
10
11
|
return readFileSync(path, "utf8");
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
function readOptionalPrivate(path) {
|
|
14
|
+
try {
|
|
15
|
+
return readPrivate(path);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT")
|
|
19
|
+
return null;
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function sha256(text) { return createHash("sha256").update(text).digest("hex"); }
|
|
24
|
+
function readEnrollment(home) {
|
|
25
|
+
try {
|
|
26
|
+
return parseEnrollment(readOptionalPrivate(join(home, ".unblock-codex-prompt.json")));
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
throw error instanceof Error && error.message === "Invalid prompt enrollment state" ? new Error("Invalid frozen bridge enrollment") : error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** The compiled snapshot, verified against its sync receipt. */
|
|
33
|
+
function readVerifiedSnapshot(home) {
|
|
34
|
+
let bundle;
|
|
35
|
+
// A parse error would quote the start of the file; never let bundle text reach a log.
|
|
36
|
+
try {
|
|
37
|
+
bundle = JSON.parse(readPrivate(join(home, ".unblock-codex-prompt-bundle.json")));
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof SyntaxError)
|
|
41
|
+
throw new Error("Frozen bundle missing or invalid; run codex-prompt sync");
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
if (!bundle || typeof bundle !== "object" || !("schema" in bundle) || bundle.schema !== 1 ||
|
|
45
|
+
!("policy" in bundle) || typeof bundle.policy !== "string" || !("promptSha256" in bundle))
|
|
46
|
+
throw new Error("Frozen bundle missing or invalid; run codex-prompt sync");
|
|
47
|
+
const prompt = readPrivate(join(home, "unblock-codex-prompt.md"));
|
|
48
|
+
if (sha256(prompt) !== bundle.promptSha256)
|
|
49
|
+
throw new Error("Frozen prompt changed outside sync; refusing context suppression");
|
|
50
|
+
return { bundle, policy: bundle.policy, prompt };
|
|
51
|
+
}
|
|
52
|
+
function readCodexSnapshot(agentDir) {
|
|
14
53
|
const home = join(agentDir, "codex-home");
|
|
15
|
-
const state =
|
|
16
|
-
if (!state
|
|
54
|
+
const state = readEnrollment(home);
|
|
55
|
+
if (!state)
|
|
17
56
|
throw new Error("Invalid frozen bridge enrollment");
|
|
18
57
|
if (!state.active)
|
|
19
58
|
return null;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const path = join(home, "unblock-codex-prompt.md");
|
|
25
|
-
if (readPromptPointer(readPrivate(join(home, "config.toml"))) !== path)
|
|
59
|
+
if (!state.runtimes.includes("codex"))
|
|
60
|
+
throw new Error("Codex bridge installed without Codex enrollment; restore or run codex-prompt sync");
|
|
61
|
+
const snapshot = readVerifiedSnapshot(home);
|
|
62
|
+
if (readPromptPointer(readPrivate(join(home, "config.toml"))) !== join(home, "unblock-codex-prompt.md")) {
|
|
26
63
|
throw new Error("Frozen prompt pointer changed; refusing context suppression");
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
64
|
+
}
|
|
65
|
+
return snapshot;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Called at the actual adapter assembly boundary; failures abort, not a swallowed hook. Non-null
|
|
69
|
+
* means this Codex run is managed. The value is the bundle's `policy`, which current syncs leave
|
|
70
|
+
* "": the Codex policy is the prompt-file prefix, delivered as base instructions.
|
|
71
|
+
*/
|
|
72
|
+
export function readFrozenPolicy(agentDir) {
|
|
73
|
+
return readCodexSnapshot(agentDir)?.policy ?? null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The verified base file, sent as each managed thread's base instructions at start and resume.
|
|
77
|
+
* Codex reads base instructions per session rather than from history, so a refreshed file
|
|
78
|
+
* reaches an existing thread when OpenClaw resumes it; the file itself stays the fallback.
|
|
79
|
+
*/
|
|
80
|
+
export function readCodexBaseInstructions(agentDir) {
|
|
81
|
+
return readCodexSnapshot(agentDir)?.prompt ?? null;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The Claude policy and the shared agent context (memory reference), both read from the bundle,
|
|
85
|
+
* for the Claude CLI prompt hook. Null unless this agent has an active enrollment that includes
|
|
86
|
+
* Claude CLI (never enrolled, paused by restore, or Codex only). Throws when that enrolled state
|
|
87
|
+
* is invalid or tampered: the bundle is the trust root, and readVerifiedSnapshot checks the prompt
|
|
88
|
+
* file against its promptSha256. It never touches the Codex policy (the prompt-file prefix).
|
|
89
|
+
*/
|
|
90
|
+
export function readClaudeManagedContext(agentDir) {
|
|
91
|
+
const home = join(agentDir, "codex-home");
|
|
92
|
+
const state = readEnrollment(home);
|
|
93
|
+
if (!state?.active || !state.runtimes.includes("claude-cli"))
|
|
94
|
+
return null;
|
|
95
|
+
const { bundle } = readVerifiedSnapshot(home);
|
|
96
|
+
const claudePolicy = parseClaudePolicy("claudePolicy" in bundle ? bundle.claudePolicy : undefined);
|
|
97
|
+
const agentContext = "agentContext" in bundle ? bundle.agentContext : undefined;
|
|
98
|
+
if (typeof agentContext !== "string" || !agentContext.trim())
|
|
99
|
+
throw new Error("Claude policy missing or invalid; run codex-prompt sync");
|
|
100
|
+
return { policy: claudePolicy.text.trim(), agentContext: agentContext.trim() };
|
|
31
101
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/plugin-entry";
|
|
2
|
+
type PolicyResolver = (params: {
|
|
3
|
+
config: OpenClawConfig;
|
|
4
|
+
provider?: string;
|
|
5
|
+
modelId?: string;
|
|
6
|
+
agentId?: string;
|
|
7
|
+
}) => {
|
|
8
|
+
policy?: {
|
|
9
|
+
id?: unknown;
|
|
10
|
+
};
|
|
11
|
+
} | undefined;
|
|
12
|
+
type Detection = {
|
|
13
|
+
ref: string;
|
|
14
|
+
runtime: string | null;
|
|
15
|
+
};
|
|
16
|
+
/** OpenClaw's public configured-policy resolver (2026.10.6+), feature-detected for older hosts. */
|
|
17
|
+
export declare function runtimePolicyResolver(api: unknown): PolicyResolver | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Model refs the agent can select by configuration, resolved like OpenClaw: the agent's model
|
|
20
|
+
* replaces the default primary (and default fallbacks unless it lists its own), plus the agent's
|
|
21
|
+
* own allowed-model map, which it can switch to at run time. The Gateway-wide default map
|
|
22
|
+
* (agents.defaults.models) does not enroll a runtime for this agent.
|
|
23
|
+
*/
|
|
24
|
+
export declare function configuredModelRefs(config: OpenClawConfig, agentId: string): string[];
|
|
25
|
+
/**
|
|
26
|
+
* Explicit-sync detection only. This reads authored policy; it does not establish runtime
|
|
27
|
+
* availability. Each turn's actual execution path stays authoritative at the patched seams.
|
|
28
|
+
*/
|
|
29
|
+
export declare function detectConfiguredRuntimes(config: OpenClawConfig, agentId: string, resolve: PolicyResolver | undefined): {
|
|
30
|
+
runtimes: Set<"codex" | "claude-cli">;
|
|
31
|
+
detections: Detection[];
|
|
32
|
+
resolver: "openclaw" | "unavailable";
|
|
33
|
+
};
|
|
34
|
+
export {};
|
package/dist/runtimes.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
function record(value) { return !!value && typeof value === "object" && !Array.isArray(value); }
|
|
2
|
+
/** OpenClaw's public configured-policy resolver (2026.10.6+), feature-detected for older hosts. */
|
|
3
|
+
export function runtimePolicyResolver(api) {
|
|
4
|
+
const runtime = record(api) ? api.runtime : undefined;
|
|
5
|
+
const modelConfig = record(runtime) ? runtime.modelConfig : undefined;
|
|
6
|
+
const resolve = record(modelConfig) ? modelConfig.resolveModelRuntimePolicy : undefined;
|
|
7
|
+
return typeof resolve === "function" ? resolve : undefined;
|
|
8
|
+
}
|
|
9
|
+
function agentEntry(config, agentId) {
|
|
10
|
+
const agents = config.agents;
|
|
11
|
+
if (!record(agents))
|
|
12
|
+
return undefined;
|
|
13
|
+
if (Array.isArray(agents.list))
|
|
14
|
+
return agents.list.find((entry) => record(entry) && entry.id === agentId);
|
|
15
|
+
return record(agents.entries) && record(agents.entries[agentId]) ? agents.entries[agentId] : undefined;
|
|
16
|
+
}
|
|
17
|
+
function primaryOf(model) {
|
|
18
|
+
const value = typeof model === "string" ? model : record(model) ? model.primary : undefined;
|
|
19
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
20
|
+
}
|
|
21
|
+
/** OpenClaw's fallback override: an agent model replaces default fallbacks unless it lists its own. */
|
|
22
|
+
function fallbacksOverride(model) {
|
|
23
|
+
if (!model)
|
|
24
|
+
return undefined;
|
|
25
|
+
if (typeof model === "string")
|
|
26
|
+
return primaryOf(model) ? [] : undefined;
|
|
27
|
+
if (!record(model))
|
|
28
|
+
return undefined;
|
|
29
|
+
if (!Object.hasOwn(model, "fallbacks"))
|
|
30
|
+
return Object.hasOwn(model, "primary") && primaryOf(model) ? [] : undefined;
|
|
31
|
+
return Array.isArray(model.fallbacks) ? model.fallbacks : undefined;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Model refs the agent can select by configuration, resolved like OpenClaw: the agent's model
|
|
35
|
+
* replaces the default primary (and default fallbacks unless it lists its own), plus the agent's
|
|
36
|
+
* own allowed-model map, which it can switch to at run time. The Gateway-wide default map
|
|
37
|
+
* (agents.defaults.models) does not enroll a runtime for this agent.
|
|
38
|
+
*/
|
|
39
|
+
export function configuredModelRefs(config, agentId) {
|
|
40
|
+
const agents = config.agents;
|
|
41
|
+
const defaults = record(agents) && record(agents.defaults) ? agents.defaults : undefined;
|
|
42
|
+
const agent = agentEntry(config, agentId);
|
|
43
|
+
const refs = new Set();
|
|
44
|
+
const add = (value) => { if (typeof value === "string" && value.trim())
|
|
45
|
+
refs.add(value.trim()); };
|
|
46
|
+
add(primaryOf(agent?.model) ?? primaryOf(defaults?.model));
|
|
47
|
+
const fallbacks = fallbacksOverride(agent?.model) ?? (record(defaults?.model) && Array.isArray(defaults.model.fallbacks) ? defaults.model.fallbacks : []);
|
|
48
|
+
fallbacks.forEach(add);
|
|
49
|
+
if (record(agent?.models))
|
|
50
|
+
Object.keys(agent.models).forEach(add);
|
|
51
|
+
return [...refs];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Explicit-sync detection only. This reads authored policy; it does not establish runtime
|
|
55
|
+
* availability. Each turn's actual execution path stays authoritative at the patched seams.
|
|
56
|
+
*/
|
|
57
|
+
export function detectConfiguredRuntimes(config, agentId, resolve) {
|
|
58
|
+
const detections = configuredModelRefs(config, agentId).map(ref => {
|
|
59
|
+
const slash = ref.indexOf("/");
|
|
60
|
+
const provider = slash > 0 ? ref.slice(0, slash) : undefined;
|
|
61
|
+
const modelId = slash > 0 ? ref.slice(slash + 1) : ref;
|
|
62
|
+
// Legacy `claude-cli/<model>` refs name the CLI backend directly (OpenClaw compatibility input).
|
|
63
|
+
if (provider?.toLowerCase() === "claude-cli")
|
|
64
|
+
return { ref, runtime: "claude-cli" };
|
|
65
|
+
let id;
|
|
66
|
+
try {
|
|
67
|
+
id = resolve?.({ config, agentId, ...provider ? { provider } : {}, modelId })?.policy?.id;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
id = undefined;
|
|
71
|
+
}
|
|
72
|
+
return { ref, runtime: typeof id === "string" && id.trim() ? id.trim().toLowerCase() : null };
|
|
73
|
+
});
|
|
74
|
+
const runtimes = new Set();
|
|
75
|
+
for (const { runtime } of detections)
|
|
76
|
+
if (runtime === "codex" || runtime === "claude-cli")
|
|
77
|
+
runtimes.add(runtime);
|
|
78
|
+
return { runtimes, detections, resolver: resolve ? "openclaw" : "unavailable" };
|
|
79
|
+
}
|
package/openclaw.plugin.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "unblock-codex-prompt",
|
|
3
3
|
"name": "Unblock Codex Prompt",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"description": "Maintain an agent-scoped
|
|
4
|
+
"version": "0.5.0",
|
|
5
|
+
"description": "Maintain an agent-scoped fleet prompt for Codex (model_instructions_file) and Claude CLI (before_prompt_build hook).",
|
|
6
6
|
"activation": { "onStartup": true },
|
|
7
7
|
"configSchema": {
|
|
8
8
|
"type": "object",
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"uiHints": {
|
|
21
|
-
"agentId": { "label": "OpenClaw agent ID", "help": "Only this agent's managed
|
|
21
|
+
"agentId": { "label": "OpenClaw agent ID", "help": "Only this agent's prompt is managed (on Claude CLI, its claude-cli runs, subagent sessions included). Defaults to main." },
|
|
22
22
|
"agentName": { "label": "Legacy agent name (unused)", "help": "Accepted for existing configs only. Identity comes from agent documents; shared prompt text comes only from promptUrl." },
|
|
23
|
-
"frozenContext": { "label": "
|
|
23
|
+
"frozenContext": { "label": "Managed prompt bridge", "help": "Required: true. Codex: replaces base instructions and OpenClaw generic policy on reviewed OpenClaw/Codex >=2026.10.5 layouts. Claude CLI: replaces OpenClaw's entire system prompt through the public before_prompt_build hook (policy, memory reference and the workspace AGENTS, SOUL, IDENTITY, USER, TOOLS, HEARTBEAT and BOOTSTRAP files, read every turn); no OpenClaw core patch. Needs hooks.allowConversationAccess: true and allowPromptInjection not false. Runtimes come from the agent's configured model policy at explicit sync. SOUL, IDENTITY, USER and AGENTS stay live." },
|
|
24
24
|
"promptUrl": { "label": "Fleet prompt endpoint", "help": "Required public HTTPS plain-text endpoint with X-Prompt-Revision SHA-256. The only shared-policy source. After initial sync, refreshed daily and when overdue on startup without refreshing agent context. No bundled prompt fallback." },
|
|
25
25
|
"suppressExplicitDelegationPrompt": { "label": "Compact managed Codex instructions", "help": "Manage a model_catalog_json override: suppress explicit-only delegation and Default-mode prose, trim role introductions and compact full-access/never-approval text. Runtime mechanics and permissions are unchanged. Refreshed daily from the configured provider. Requires frozenContext and an available provider env_key. Generated-file edits are replaced on refresh." },
|
|
26
26
|
"disabledCodexSkills": { "label": "Codex skills to remove", "help": "Plugin names (all of their skills) or exact skill names to disable in Codex's native skills list. Defaults to ChatGPT Pages and Pets. Applied through a managed [[skills.config]] block at startup, sync and daily refresh; other skills are untouched. Set [] to stop managing Codex skills." }
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unblocklabs/unblock-codex-prompt",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Manage a shared
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Manage a shared fleet prompt for an OpenClaw agent on Codex and Claude CLI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -21,23 +21,23 @@
|
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@openclaw/plugin-inspector": "^0.3.25",
|
|
23
23
|
"@types/node": "^22.0.0",
|
|
24
|
-
"openclaw": "2026.9
|
|
24
|
+
"openclaw": "2026.10.9",
|
|
25
25
|
"typescript": "^5.9.3"
|
|
26
26
|
},
|
|
27
|
-
"peerDependencies": { "openclaw": ">=2026.
|
|
27
|
+
"peerDependencies": { "openclaw": ">=2026.10.5" },
|
|
28
28
|
"peerDependenciesMeta": { "openclaw": { "optional": true } },
|
|
29
29
|
"openclaw": {
|
|
30
30
|
"extensions": ["./dist/index.js"],
|
|
31
|
-
"install": { "npmSpec": "@unblocklabs/unblock-codex-prompt", "defaultChoice": "npm", "minHostVersion": ">=2026.
|
|
31
|
+
"install": { "npmSpec": "@unblocklabs/unblock-codex-prompt", "defaultChoice": "npm", "minHostVersion": ">=2026.10.5" },
|
|
32
32
|
"release": { "publishToClawHub": false, "publishToNpm": true },
|
|
33
|
-
"compat": { "pluginApi": ">=2026.
|
|
34
|
-
"build": { "openclawVersion": "2026.9
|
|
33
|
+
"compat": { "pluginApi": ">=2026.10.5", "minGatewayVersion": ">=2026.10.5" },
|
|
34
|
+
"build": { "openclawVersion": "2026.10.9", "pluginSdkVersion": "2026.10.9" }
|
|
35
35
|
},
|
|
36
36
|
"engines": { "node": ">=22.22.3" },
|
|
37
37
|
"publishConfig": { "access": "public" },
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"author": "Unblock Labs",
|
|
40
|
-
"keywords": ["openclaw", "openclaw-plugin", "codex", "prompt"],
|
|
40
|
+
"keywords": ["openclaw", "openclaw-plugin", "codex", "claude-cli", "prompt"],
|
|
41
41
|
"repository": { "type": "git", "url": "git+https://github.com/unblocklabs-ai/unblock-codex-prompt.git" },
|
|
42
42
|
"bugs": { "url": "https://github.com/unblocklabs-ai/unblock-codex-prompt/issues" },
|
|
43
43
|
"homepage": "https://github.com/unblocklabs-ai/unblock-codex-prompt#readme"
|