@kodelyth/codex 2026.5.40 → 2026.5.42
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/dist/client-ChMX13_o.js +642 -0
- package/dist/client-factory-D3dIsp4Y.js +9 -0
- package/dist/command-formatters-BRW7_Nu7.js +519 -0
- package/dist/command-handlers-P2IqtXaZ.js +1462 -0
- package/dist/compact-baos5flR.js +329 -0
- package/dist/computer-use-VfLvTMaa.js +367 -0
- package/dist/config-CezENx_E.js +510 -0
- package/dist/doctor-contract-api.js +53 -0
- package/dist/harness.js +51 -0
- package/dist/index.js +1133 -0
- package/dist/media-understanding-provider.js +335 -0
- package/dist/models-B9DhrIwD.js +110 -0
- package/dist/node-cli-sessions-De4_DuFw.js +1216 -0
- package/dist/plugin-activation-BlMuJeXz.js +452 -0
- package/dist/prompt-overlay.js +12 -0
- package/dist/protocol-C9UWI98H.js +9 -0
- package/dist/protocol-validators-BGBspNmF.js +5988 -0
- package/dist/provider-catalog.js +84 -0
- package/dist/provider-discovery.js +33 -0
- package/dist/provider.js +150 -0
- package/dist/rate-limit-cache-CHuacE27.js +24 -0
- package/dist/request-CTQKUxaa.js +89 -0
- package/dist/rolldown-runtime-DUslC3ob.js +14 -0
- package/dist/run-attempt-DqV2OU1R.js +5366 -0
- package/dist/session-binding-3PzU7ZTW.js +222 -0
- package/dist/shared-client-Cnyr9dyT.js +631 -0
- package/dist/side-question-CP5XlA0U.js +667 -0
- package/dist/test-api.js +45 -0
- package/dist/thread-lifecycle-DBJetBuV.js +1561 -0
- package/dist/vision-tools-Cl_5a93K.js +1379 -0
- package/doctor-contract-api.test.ts +44 -0
- package/doctor-contract-api.ts +68 -0
- package/harness.ts +72 -0
- package/index.test.ts +230 -0
- package/index.ts +66 -0
- package/klaw.plugin.json +24 -85
- package/media-understanding-provider.test.ts +486 -0
- package/media-understanding-provider.ts +521 -0
- package/package.json +3 -3
- package/prompt-overlay-runtime-contract.test.ts +48 -0
- package/prompt-overlay.ts +21 -0
- package/provider-catalog.ts +83 -0
- package/provider-discovery.ts +45 -0
- package/provider.test.ts +384 -0
- package/provider.ts +243 -0
- package/src/app-server/app-inventory-cache.test.ts +176 -0
- package/src/app-server/app-inventory-cache.ts +324 -0
- package/src/app-server/approval-bridge.test.ts +1471 -0
- package/src/app-server/approval-bridge.ts +1211 -0
- package/src/app-server/auth-bridge.test.ts +1449 -0
- package/src/app-server/auth-bridge.ts +614 -0
- package/src/app-server/auth-profile-runtime-contract.test.ts +239 -0
- package/src/app-server/capabilities.ts +27 -0
- package/src/app-server/client-factory.ts +24 -0
- package/src/app-server/client.test.ts +563 -0
- package/src/app-server/client.ts +715 -0
- package/src/app-server/compact.test.ts +710 -0
- package/src/app-server/compact.ts +500 -0
- package/src/app-server/computer-use.test.ts +788 -0
- package/src/app-server/computer-use.ts +683 -0
- package/src/app-server/config.test.ts +879 -0
- package/src/app-server/config.ts +1038 -0
- package/src/app-server/context-engine-projection.test.ts +252 -0
- package/src/app-server/context-engine-projection.ts +403 -0
- package/src/app-server/delivery-no-reply-runtime-contract.test.ts +80 -0
- package/src/app-server/dynamic-tool-diagnostics.ts +73 -0
- package/src/app-server/dynamic-tool-profile.ts +69 -0
- package/src/app-server/dynamic-tools.test.ts +1302 -0
- package/src/app-server/dynamic-tools.ts +623 -0
- package/src/app-server/elicitation-bridge.test.ts +1056 -0
- package/src/app-server/elicitation-bridge.ts +783 -0
- package/src/app-server/event-projector.test.ts +2668 -0
- package/src/app-server/event-projector.ts +2057 -0
- package/src/app-server/image-payload-sanitizer.test.ts +49 -0
- package/src/app-server/image-payload-sanitizer.ts +167 -0
- package/src/app-server/klaw-owned-tool-runtime-contract.test.ts +456 -0
- package/src/app-server/local-runtime-attribution.ts +39 -0
- package/src/app-server/managed-binary.test.ts +139 -0
- package/src/app-server/managed-binary.ts +193 -0
- package/src/app-server/models.test.ts +246 -0
- package/src/app-server/models.ts +172 -0
- package/src/app-server/native-hook-relay.test.ts +271 -0
- package/src/app-server/native-hook-relay.ts +150 -0
- package/src/app-server/native-subagent-task-mirror.test.ts +573 -0
- package/src/app-server/native-subagent-task-mirror.ts +497 -0
- package/src/app-server/outcome-fallback-runtime-contract.test.ts +404 -0
- package/src/app-server/plugin-activation.test.ts +336 -0
- package/src/app-server/plugin-activation.ts +283 -0
- package/src/app-server/plugin-app-cache-key.ts +74 -0
- package/src/app-server/plugin-approval-roundtrip.ts +122 -0
- package/src/app-server/plugin-inventory.test.ts +355 -0
- package/src/app-server/plugin-inventory.ts +357 -0
- package/src/app-server/plugin-thread-config.test.ts +865 -0
- package/src/app-server/plugin-thread-config.ts +455 -0
- package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +33 -0
- package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +199 -0
- package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +102 -0
- package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +227 -0
- package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +2630 -0
- package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +2630 -0
- package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +1659 -0
- package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +1655 -0
- package/src/app-server/protocol-validators.test.ts +75 -0
- package/src/app-server/protocol-validators.ts +203 -0
- package/src/app-server/protocol.ts +520 -0
- package/src/app-server/rate-limit-cache.ts +48 -0
- package/src/app-server/rate-limits.test.ts +202 -0
- package/src/app-server/rate-limits.ts +583 -0
- package/src/app-server/request.ts +73 -0
- package/src/app-server/run-attempt.context-engine.test.ts +1004 -0
- package/src/app-server/run-attempt.test.ts +9477 -0
- package/src/app-server/run-attempt.ts +4683 -0
- package/src/app-server/run-attempt.vision-tools.test.ts +35 -0
- package/src/app-server/schema-normalization-runtime-contract.test.ts +206 -0
- package/src/app-server/session-binding.test.ts +303 -0
- package/src/app-server/session-binding.ts +398 -0
- package/src/app-server/session-history.ts +44 -0
- package/src/app-server/shared-client.test.ts +589 -0
- package/src/app-server/shared-client.ts +289 -0
- package/src/app-server/side-question.test.ts +1175 -0
- package/src/app-server/side-question.ts +1007 -0
- package/src/app-server/test-support.ts +48 -0
- package/src/app-server/thread-lifecycle.test.ts +447 -0
- package/src/app-server/thread-lifecycle.ts +939 -0
- package/src/app-server/thread-lifecycle.user-mcp-servers.test.ts +442 -0
- package/src/app-server/timeout.ts +9 -0
- package/src/app-server/tool-progress-normalization.ts +77 -0
- package/src/app-server/trajectory.test.ts +205 -0
- package/src/app-server/trajectory.ts +365 -0
- package/src/app-server/transcript-mirror.test.ts +524 -0
- package/src/app-server/transcript-mirror.ts +208 -0
- package/src/app-server/transcript-repair-runtime-contract.test.ts +44 -0
- package/src/app-server/transport-stdio.test.ts +171 -0
- package/src/app-server/transport-stdio.ts +107 -0
- package/src/app-server/transport-websocket.test.ts +69 -0
- package/src/app-server/transport-websocket.ts +90 -0
- package/src/app-server/transport.ts +117 -0
- package/src/app-server/user-input-bridge.test.ts +249 -0
- package/src/app-server/user-input-bridge.ts +316 -0
- package/src/app-server/version.ts +4 -0
- package/src/app-server/vision-tools.ts +12 -0
- package/src/command-account.ts +544 -0
- package/src/command-formatters.ts +425 -0
- package/src/command-handlers.ts +2004 -0
- package/src/command-rpc.test.ts +16 -0
- package/src/command-rpc.ts +142 -0
- package/src/commands.test.ts +3312 -0
- package/src/commands.ts +65 -0
- package/src/conversation-binding-data.ts +124 -0
- package/src/conversation-binding.test.ts +599 -0
- package/src/conversation-binding.ts +561 -0
- package/src/conversation-control.test.ts +126 -0
- package/src/conversation-control.ts +303 -0
- package/src/conversation-turn-collector.test.ts +191 -0
- package/src/conversation-turn-collector.ts +186 -0
- package/src/conversation-turn-input.test.ts +141 -0
- package/src/conversation-turn-input.ts +106 -0
- package/src/manifest.test.ts +20 -0
- package/src/migration/apply.ts +501 -0
- package/src/migration/helpers.ts +55 -0
- package/src/migration/plan.ts +461 -0
- package/src/migration/provider.test.ts +1741 -0
- package/src/migration/provider.ts +41 -0
- package/src/migration/source.ts +643 -0
- package/src/migration/targets.ts +25 -0
- package/src/node-cli-sessions.test.ts +180 -0
- package/src/node-cli-sessions.ts +711 -0
- package/test-api.ts +82 -0
- package/tsconfig.json +16 -0
- package/doctor-contract-api.js +0 -7
- package/harness.js +0 -7
- package/index.js +0 -7
- package/media-understanding-provider.js +0 -7
- package/prompt-overlay.js +0 -7
- package/provider-catalog.js +0 -7
- package/provider-discovery.js +0 -7
- package/provider.js +0 -7
- package/test-api.js +0 -7
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
CodexAppInventoryCache,
|
|
4
|
+
buildCodexAppInventoryCacheKey,
|
|
5
|
+
serializeCodexAppInventoryError,
|
|
6
|
+
} from "./app-inventory-cache.js";
|
|
7
|
+
import type { v2 } from "./protocol.js";
|
|
8
|
+
|
|
9
|
+
describe("Codex app inventory cache", () => {
|
|
10
|
+
it("returns missing while scheduling one coalesced app/list refresh", async () => {
|
|
11
|
+
const cache = new CodexAppInventoryCache({ ttlMs: 100 });
|
|
12
|
+
const request = vi.fn(async (_method: "app/list", params: v2.AppsListParams) => {
|
|
13
|
+
return {
|
|
14
|
+
data: [app(params.cursor ? "app-2" : "app-1")],
|
|
15
|
+
nextCursor: params.cursor ? null : "next",
|
|
16
|
+
} satisfies v2.AppsListResponse;
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const key = buildCodexAppInventoryCacheKey({ codexHome: "/codex", authProfileId: "work" });
|
|
20
|
+
const read = cache.read({ key, request, nowMs: 0 });
|
|
21
|
+
expect(read.state).toBe("missing");
|
|
22
|
+
expect(read.refreshScheduled).toBe(true);
|
|
23
|
+
|
|
24
|
+
const snapshot = await cache.refreshNow({ key, request, nowMs: 0 });
|
|
25
|
+
expect(snapshot.apps.map((item) => item.id)).toEqual(["app-1", "app-2"]);
|
|
26
|
+
expect(request).toHaveBeenCalledTimes(2);
|
|
27
|
+
|
|
28
|
+
const fresh = cache.read({ key, request, nowMs: 50 });
|
|
29
|
+
expect(fresh.state).toBe("fresh");
|
|
30
|
+
expect(fresh.refreshScheduled).toBe(false);
|
|
31
|
+
expect(fresh.snapshot?.apps.map((item) => item.id)).toEqual(["app-1", "app-2"]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("can read missing inventory without scheduling app/list", async () => {
|
|
35
|
+
const cache = new CodexAppInventoryCache({ ttlMs: 100 });
|
|
36
|
+
const request = vi.fn(async () => {
|
|
37
|
+
return {
|
|
38
|
+
data: [app("app-1")],
|
|
39
|
+
nextCursor: null,
|
|
40
|
+
} satisfies v2.AppsListResponse;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const read = cache.read({
|
|
44
|
+
key: "runtime",
|
|
45
|
+
request,
|
|
46
|
+
suppressRefresh: true,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
expect(read.state).toBe("missing");
|
|
50
|
+
expect(read.refreshScheduled).toBe(false);
|
|
51
|
+
expect(request).not.toHaveBeenCalled();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("uses stale inventory for the current read while still refreshing asynchronously", async () => {
|
|
55
|
+
const cache = new CodexAppInventoryCache({ ttlMs: 10 });
|
|
56
|
+
const request = vi.fn(async () => {
|
|
57
|
+
return {
|
|
58
|
+
data: [app(`app-${request.mock.calls.length}`)],
|
|
59
|
+
nextCursor: null,
|
|
60
|
+
} satisfies v2.AppsListResponse;
|
|
61
|
+
});
|
|
62
|
+
const key = "runtime";
|
|
63
|
+
await cache.refreshNow({ key, request, nowMs: 0 });
|
|
64
|
+
|
|
65
|
+
const stale = cache.read({ key, request, nowMs: 11, suppressRefresh: true });
|
|
66
|
+
expect(stale.state).toBe("stale");
|
|
67
|
+
expect(stale.snapshot?.apps.map((item) => item.id)).toEqual(["app-1"]);
|
|
68
|
+
expect(stale.refreshScheduled).toBe(true);
|
|
69
|
+
|
|
70
|
+
const refreshed = await cache.refreshNow({ key, request, nowMs: 11 });
|
|
71
|
+
expect(refreshed.apps.map((item) => item.id)).toEqual(["app-2"]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("records refresh errors without discarding the last successful snapshot", async () => {
|
|
75
|
+
const cache = new CodexAppInventoryCache({ ttlMs: 1 });
|
|
76
|
+
const key = "runtime";
|
|
77
|
+
await cache.refreshNow({
|
|
78
|
+
key,
|
|
79
|
+
nowMs: 0,
|
|
80
|
+
request: async () => ({ data: [app("app-1")], nextCursor: null }),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
await expect(
|
|
84
|
+
cache.refreshNow({
|
|
85
|
+
key,
|
|
86
|
+
nowMs: 2,
|
|
87
|
+
request: async () => {
|
|
88
|
+
throw new Error("app list failed");
|
|
89
|
+
},
|
|
90
|
+
}),
|
|
91
|
+
).rejects.toThrow("app list failed");
|
|
92
|
+
|
|
93
|
+
const read = cache.read({
|
|
94
|
+
key,
|
|
95
|
+
nowMs: 2,
|
|
96
|
+
request: async () => ({ data: [app("app-2")], nextCursor: null }),
|
|
97
|
+
});
|
|
98
|
+
expect(read.snapshot?.apps.map((item) => item.id)).toEqual(["app-1"]);
|
|
99
|
+
expect(read.diagnostic?.message).toBe("app list failed");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("omits challenge HTML when serializing app/list errors", () => {
|
|
103
|
+
const error = new Error(
|
|
104
|
+
'failed to list apps: Request failed with status 403 Forbidden: <html><script src="/backend-api/connectors/directory/list?__cf_chl_tk=secret-token"></script></html>',
|
|
105
|
+
);
|
|
106
|
+
const serialized = serializeCodexAppInventoryError(error);
|
|
107
|
+
|
|
108
|
+
expect(serialized.message).toBe(
|
|
109
|
+
"failed to list apps: Request failed with status 403 Forbidden: [HTML response body omitted]",
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("forces a post-install refresh past an older in-flight app/list", async () => {
|
|
114
|
+
const cache = new CodexAppInventoryCache({ ttlMs: 1_000 });
|
|
115
|
+
const key = "runtime";
|
|
116
|
+
let resolveStale: ((response: v2.AppsListResponse) => void) | undefined;
|
|
117
|
+
let resolveFresh: ((response: v2.AppsListResponse) => void) | undefined;
|
|
118
|
+
const request = vi.fn(
|
|
119
|
+
async (_method: "app/list", params: v2.AppsListParams): Promise<v2.AppsListResponse> => {
|
|
120
|
+
expect(params.forceRefetch).toBe(request.mock.calls.length === 2);
|
|
121
|
+
return await new Promise((resolve) => {
|
|
122
|
+
if (request.mock.calls.length === 1) {
|
|
123
|
+
resolveStale = resolve;
|
|
124
|
+
} else {
|
|
125
|
+
resolveFresh = resolve;
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const staleRead = cache.read({ key, request, nowMs: 0 });
|
|
132
|
+
expect(staleRead.state).toBe("missing");
|
|
133
|
+
expect(staleRead.refreshScheduled).toBe(true);
|
|
134
|
+
|
|
135
|
+
cache.invalidate(key, "plugin installed", 1);
|
|
136
|
+
const forcedRead = cache.read({ key, request, nowMs: 1, forceRefetch: true });
|
|
137
|
+
expect(forcedRead.state).toBe("missing");
|
|
138
|
+
expect(forcedRead.refreshScheduled).toBe(true);
|
|
139
|
+
expect(request).toHaveBeenCalledTimes(2);
|
|
140
|
+
|
|
141
|
+
const forced = cache.refreshNow({ key, request, nowMs: 1 });
|
|
142
|
+
resolveFresh?.({ data: [app("fresh-app")], nextCursor: null });
|
|
143
|
+
await expect(forced).resolves.toStrictEqual({
|
|
144
|
+
key,
|
|
145
|
+
apps: [app("fresh-app")],
|
|
146
|
+
fetchedAtMs: 1,
|
|
147
|
+
expiresAtMs: 1_001,
|
|
148
|
+
revision: 2,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
resolveStale?.({ data: [app("stale-app")], nextCursor: null });
|
|
152
|
+
await Promise.resolve();
|
|
153
|
+
|
|
154
|
+
const freshRead = cache.read({ key, request, nowMs: 2 });
|
|
155
|
+
expect(freshRead.state).toBe("fresh");
|
|
156
|
+
expect(freshRead.snapshot?.apps.map((item) => item.id)).toEqual(["fresh-app"]);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
function app(id: string): v2.AppInfo {
|
|
161
|
+
return {
|
|
162
|
+
id,
|
|
163
|
+
name: id,
|
|
164
|
+
description: null,
|
|
165
|
+
logoUrl: null,
|
|
166
|
+
logoUrlDark: null,
|
|
167
|
+
distributionChannel: null,
|
|
168
|
+
branding: null,
|
|
169
|
+
appMetadata: null,
|
|
170
|
+
labels: null,
|
|
171
|
+
installUrl: null,
|
|
172
|
+
isAccessible: true,
|
|
173
|
+
isEnabled: true,
|
|
174
|
+
pluginDisplayNames: [],
|
|
175
|
+
};
|
|
176
|
+
}
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import { embeddedAgentLog } from "klaw/plugin-sdk/agent-harness-runtime";
|
|
2
|
+
import type { JsonValue, v2 } from "./protocol.js";
|
|
3
|
+
|
|
4
|
+
export const CODEX_APP_INVENTORY_CACHE_TTL_MS = 60 * 60 * 1_000;
|
|
5
|
+
const MAX_SERIALIZED_ERROR_MESSAGE_LENGTH = 500;
|
|
6
|
+
|
|
7
|
+
export type CodexAppInventoryRequest = (
|
|
8
|
+
method: "app/list",
|
|
9
|
+
params: v2.AppsListParams,
|
|
10
|
+
) => Promise<v2.AppsListResponse>;
|
|
11
|
+
|
|
12
|
+
export type CodexAppInventoryCacheKeyInput = {
|
|
13
|
+
codexHome?: string;
|
|
14
|
+
endpoint?: string;
|
|
15
|
+
authProfileId?: string;
|
|
16
|
+
accountId?: string;
|
|
17
|
+
envApiKeyFingerprint?: string;
|
|
18
|
+
appServerVersion?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type CodexAppInventoryCacheDiagnostic = {
|
|
22
|
+
message: string;
|
|
23
|
+
atMs: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type CodexAppInventorySnapshot = {
|
|
27
|
+
key: string;
|
|
28
|
+
apps: v2.AppInfo[];
|
|
29
|
+
fetchedAtMs: number;
|
|
30
|
+
expiresAtMs: number;
|
|
31
|
+
revision: number;
|
|
32
|
+
lastError?: CodexAppInventoryCacheDiagnostic;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type CodexAppInventoryReadState = "fresh" | "stale" | "missing";
|
|
36
|
+
|
|
37
|
+
export type CodexAppInventoryCacheRead = {
|
|
38
|
+
state: CodexAppInventoryReadState;
|
|
39
|
+
key: string;
|
|
40
|
+
revision: number;
|
|
41
|
+
snapshot?: CodexAppInventorySnapshot;
|
|
42
|
+
refreshScheduled: boolean;
|
|
43
|
+
diagnostic?: CodexAppInventoryCacheDiagnostic;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type CacheEntry = CodexAppInventorySnapshot & {
|
|
47
|
+
invalidated: boolean;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
type RefreshParams = {
|
|
51
|
+
key: string;
|
|
52
|
+
request: CodexAppInventoryRequest;
|
|
53
|
+
nowMs?: number;
|
|
54
|
+
forceRefetch?: boolean;
|
|
55
|
+
suppressRefresh?: boolean;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export class CodexAppInventoryCache {
|
|
59
|
+
private readonly ttlMs: number;
|
|
60
|
+
private readonly entries = new Map<string, CacheEntry>();
|
|
61
|
+
private readonly inFlight = new Map<string, Promise<CodexAppInventorySnapshot>>();
|
|
62
|
+
// Per-key refresh generation. Each refresh attempt claims the next token so
|
|
63
|
+
// an older request that finishes late cannot overwrite a newer snapshot.
|
|
64
|
+
private readonly refreshTokens = new Map<string, number>();
|
|
65
|
+
private readonly diagnostics = new Map<string, CodexAppInventoryCacheDiagnostic>();
|
|
66
|
+
private revision = 0;
|
|
67
|
+
|
|
68
|
+
constructor(options: { ttlMs?: number } = {}) {
|
|
69
|
+
this.ttlMs = options.ttlMs ?? CODEX_APP_INVENTORY_CACHE_TTL_MS;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
read(params: RefreshParams): CodexAppInventoryCacheRead {
|
|
73
|
+
const nowMs = params.nowMs ?? Date.now();
|
|
74
|
+
const entry = this.entries.get(params.key);
|
|
75
|
+
if (!entry) {
|
|
76
|
+
const refreshScheduled = params.suppressRefresh ? false : this.scheduleRefresh(params);
|
|
77
|
+
return {
|
|
78
|
+
state: "missing",
|
|
79
|
+
key: params.key,
|
|
80
|
+
revision: this.revision,
|
|
81
|
+
refreshScheduled,
|
|
82
|
+
...(this.diagnostics.get(params.key)
|
|
83
|
+
? { diagnostic: this.diagnostics.get(params.key) }
|
|
84
|
+
: {}),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const state: CodexAppInventoryReadState =
|
|
89
|
+
entry.invalidated || entry.expiresAtMs <= nowMs ? "stale" : "fresh";
|
|
90
|
+
const refreshScheduled =
|
|
91
|
+
state === "fresh" && !params.forceRefetch ? false : this.scheduleRefresh(params);
|
|
92
|
+
return {
|
|
93
|
+
state,
|
|
94
|
+
key: params.key,
|
|
95
|
+
revision: entry.revision,
|
|
96
|
+
snapshot: stripEntryState(entry),
|
|
97
|
+
refreshScheduled,
|
|
98
|
+
...(entry.lastError ? { diagnostic: entry.lastError } : {}),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
refreshNow(params: RefreshParams): Promise<CodexAppInventorySnapshot> {
|
|
103
|
+
return this.refresh(params);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
invalidate(key: string, reason: string, nowMs = Date.now()): number {
|
|
107
|
+
this.revision += 1;
|
|
108
|
+
const diagnostic = { message: reason, atMs: nowMs };
|
|
109
|
+
const entry = this.entries.get(key);
|
|
110
|
+
if (entry) {
|
|
111
|
+
entry.invalidated = true;
|
|
112
|
+
entry.lastError = diagnostic;
|
|
113
|
+
entry.revision = this.revision;
|
|
114
|
+
} else {
|
|
115
|
+
this.diagnostics.set(key, diagnostic);
|
|
116
|
+
}
|
|
117
|
+
return this.revision;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
clear(): void {
|
|
121
|
+
this.entries.clear();
|
|
122
|
+
this.inFlight.clear();
|
|
123
|
+
this.refreshTokens.clear();
|
|
124
|
+
this.diagnostics.clear();
|
|
125
|
+
this.revision = 0;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
getRevision(): number {
|
|
129
|
+
return this.revision;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private scheduleRefresh(params: RefreshParams): boolean {
|
|
133
|
+
if (this.inFlight.has(params.key) && !params.forceRefetch) {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
const promise = this.refresh(params);
|
|
137
|
+
this.inFlight.set(params.key, promise);
|
|
138
|
+
promise.catch(() => undefined);
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private async refresh(params: RefreshParams): Promise<CodexAppInventorySnapshot> {
|
|
143
|
+
const existing = this.inFlight.get(params.key);
|
|
144
|
+
if (existing && !params.forceRefetch) {
|
|
145
|
+
return existing;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const refreshToken = (this.refreshTokens.get(params.key) ?? 0) + 1;
|
|
149
|
+
this.refreshTokens.set(params.key, refreshToken);
|
|
150
|
+
const promise = this.refreshUncoalesced(params, refreshToken);
|
|
151
|
+
this.inFlight.set(params.key, promise);
|
|
152
|
+
try {
|
|
153
|
+
return await promise;
|
|
154
|
+
} finally {
|
|
155
|
+
if (this.inFlight.get(params.key) === promise) {
|
|
156
|
+
this.inFlight.delete(params.key);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private async refreshUncoalesced(
|
|
162
|
+
params: RefreshParams,
|
|
163
|
+
refreshToken: number,
|
|
164
|
+
): Promise<CodexAppInventorySnapshot> {
|
|
165
|
+
const nowMs = params.nowMs ?? Date.now();
|
|
166
|
+
try {
|
|
167
|
+
const apps = await listAllApps(params.request, params.forceRefetch ?? false);
|
|
168
|
+
this.revision += 1;
|
|
169
|
+
const snapshot: CodexAppInventorySnapshot = {
|
|
170
|
+
key: params.key,
|
|
171
|
+
apps,
|
|
172
|
+
fetchedAtMs: nowMs,
|
|
173
|
+
expiresAtMs: nowMs + this.ttlMs,
|
|
174
|
+
revision: this.revision,
|
|
175
|
+
};
|
|
176
|
+
// Only publish this snapshot if no newer refresh started for the same key
|
|
177
|
+
// while this request was in flight.
|
|
178
|
+
if (this.refreshTokens.get(params.key) === refreshToken) {
|
|
179
|
+
this.entries.set(params.key, { ...snapshot, invalidated: false });
|
|
180
|
+
this.diagnostics.delete(params.key);
|
|
181
|
+
}
|
|
182
|
+
return snapshot;
|
|
183
|
+
} catch (error) {
|
|
184
|
+
const diagnostic = {
|
|
185
|
+
message: sanitizeErrorMessage(error instanceof Error ? error.message : String(error)),
|
|
186
|
+
atMs: nowMs,
|
|
187
|
+
};
|
|
188
|
+
this.diagnostics.set(params.key, diagnostic);
|
|
189
|
+
const entry = this.entries.get(params.key);
|
|
190
|
+
if (entry) {
|
|
191
|
+
entry.lastError = diagnostic;
|
|
192
|
+
}
|
|
193
|
+
embeddedAgentLog.warn("codex app inventory refresh failed", {
|
|
194
|
+
forceRefetch: params.forceRefetch === true,
|
|
195
|
+
keyFingerprint: fingerprintInventoryCacheKey(params.key),
|
|
196
|
+
error: serializeCodexAppInventoryError(error),
|
|
197
|
+
});
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function serializeCodexAppInventoryError(error: unknown): Record<string, unknown> {
|
|
204
|
+
const record = isRecord(error) ? error : undefined;
|
|
205
|
+
const data = record && "data" in record ? redactErrorData(record.data) : undefined;
|
|
206
|
+
return {
|
|
207
|
+
name:
|
|
208
|
+
error instanceof Error
|
|
209
|
+
? error.name
|
|
210
|
+
: typeof record?.name === "string"
|
|
211
|
+
? record.name
|
|
212
|
+
: undefined,
|
|
213
|
+
message: sanitizeErrorMessage(error instanceof Error ? error.message : String(error)),
|
|
214
|
+
...(typeof record?.code === "number" ? { code: record.code } : {}),
|
|
215
|
+
...(data !== undefined ? { data } : {}),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export const defaultCodexAppInventoryCache = new CodexAppInventoryCache();
|
|
220
|
+
|
|
221
|
+
export function buildCodexAppInventoryCacheKey(input: CodexAppInventoryCacheKeyInput): string {
|
|
222
|
+
return JSON.stringify({
|
|
223
|
+
codexHome: input.codexHome ?? null,
|
|
224
|
+
endpoint: input.endpoint ?? null,
|
|
225
|
+
authProfileId: input.authProfileId ?? null,
|
|
226
|
+
accountId: input.accountId ?? null,
|
|
227
|
+
envApiKeyFingerprint: input.envApiKeyFingerprint ?? null,
|
|
228
|
+
appServerVersion: input.appServerVersion ?? null,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async function listAllApps(
|
|
233
|
+
request: CodexAppInventoryRequest,
|
|
234
|
+
forceRefetch: boolean,
|
|
235
|
+
): Promise<v2.AppInfo[]> {
|
|
236
|
+
const apps: v2.AppInfo[] = [];
|
|
237
|
+
let cursor: string | null | undefined;
|
|
238
|
+
do {
|
|
239
|
+
const response = await request("app/list", {
|
|
240
|
+
cursor,
|
|
241
|
+
limit: 100,
|
|
242
|
+
forceRefetch,
|
|
243
|
+
});
|
|
244
|
+
apps.push(...response.data);
|
|
245
|
+
cursor = response.nextCursor;
|
|
246
|
+
} while (cursor);
|
|
247
|
+
return apps;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function stripEntryState(entry: CacheEntry): CodexAppInventorySnapshot {
|
|
251
|
+
const { invalidated: _invalidated, ...snapshot } = entry;
|
|
252
|
+
return snapshot;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function fingerprintInventoryCacheKey(key: string): string {
|
|
256
|
+
let hash = 0;
|
|
257
|
+
for (let index = 0; index < key.length; index += 1) {
|
|
258
|
+
hash = (hash * 31 + key.charCodeAt(index)) >>> 0;
|
|
259
|
+
}
|
|
260
|
+
return hash.toString(16).padStart(8, "0");
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
264
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function redactErrorData(value: unknown, depth = 0): JsonValue | undefined {
|
|
268
|
+
if (value === undefined) {
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
if (value === null || typeof value === "boolean" || typeof value === "number") {
|
|
272
|
+
return value;
|
|
273
|
+
}
|
|
274
|
+
if (depth > 6) {
|
|
275
|
+
return "[truncated]";
|
|
276
|
+
}
|
|
277
|
+
if (Array.isArray(value)) {
|
|
278
|
+
return value.map((entry) => redactErrorData(entry, depth + 1) ?? null);
|
|
279
|
+
}
|
|
280
|
+
if (isRecord(value)) {
|
|
281
|
+
const redacted: Record<string, JsonValue> = {};
|
|
282
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
283
|
+
redacted[key] = isSensitiveErrorDataKey(key)
|
|
284
|
+
? "<redacted>"
|
|
285
|
+
: (redactErrorData(entry, depth + 1) ?? null);
|
|
286
|
+
}
|
|
287
|
+
return redacted;
|
|
288
|
+
}
|
|
289
|
+
if (typeof value === "string" && value.length > 500) {
|
|
290
|
+
return `${value.slice(0, 500)}...`;
|
|
291
|
+
}
|
|
292
|
+
if (typeof value === "string") {
|
|
293
|
+
return value;
|
|
294
|
+
}
|
|
295
|
+
if (typeof value === "bigint") {
|
|
296
|
+
return value.toString();
|
|
297
|
+
}
|
|
298
|
+
if (typeof value === "symbol") {
|
|
299
|
+
return value.description ? `Symbol(${value.description})` : "Symbol()";
|
|
300
|
+
}
|
|
301
|
+
if (typeof value === "function") {
|
|
302
|
+
return value.name ? `[function ${value.name}]` : "[function]";
|
|
303
|
+
}
|
|
304
|
+
return "[unserializable]";
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function sanitizeErrorMessage(message: string): string {
|
|
308
|
+
const htmlStart = message.search(/<html[\s>]/i);
|
|
309
|
+
const withoutHtml =
|
|
310
|
+
htmlStart >= 0
|
|
311
|
+
? `${message.slice(0, htmlStart).trimEnd()} [HTML response body omitted]`
|
|
312
|
+
: message;
|
|
313
|
+
const redacted = withoutHtml.replace(
|
|
314
|
+
/([?&][^=\s"'<>]*(?:api[_-]?key|authorization|cookie|credential|password|secret|token|tk)[^=\s"'<>]*=)[^&\s"'<>]+/gi,
|
|
315
|
+
"$1<redacted>",
|
|
316
|
+
);
|
|
317
|
+
return redacted.length > MAX_SERIALIZED_ERROR_MESSAGE_LENGTH
|
|
318
|
+
? `${redacted.slice(0, MAX_SERIALIZED_ERROR_MESSAGE_LENGTH)}...`
|
|
319
|
+
: redacted;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function isSensitiveErrorDataKey(key: string): boolean {
|
|
323
|
+
return /api[_-]?key|authorization|cookie|credential|password|secret|token/i.test(key);
|
|
324
|
+
}
|