@ctliz/agent-intercom-codex 0.11.0-connect.2

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.
Files changed (52) hide show
  1. package/.codex-plugin/plugin.json +39 -0
  2. package/.mcp.json +10 -0
  3. package/COPYRIGHT +6 -0
  4. package/LICENSE +661 -0
  5. package/LICENSE_TRANSITION.md +25 -0
  6. package/README.md +572 -0
  7. package/THIRD_PARTY_NOTICES.md +17 -0
  8. package/assets/logo-generated.png +0 -0
  9. package/assets/logo.svg +22 -0
  10. package/boss-control-outbox.ts +195 -0
  11. package/broker/access-credential.ts +76 -0
  12. package/broker/access-registry.ts +464 -0
  13. package/broker/audit.ts +60 -0
  14. package/broker/authorization.ts +54 -0
  15. package/broker/boss-adapter.ts +531 -0
  16. package/broker/boss-control-ledger.ts +306 -0
  17. package/broker/broker.ts +2327 -0
  18. package/broker/client.ts +1111 -0
  19. package/broker/framing.ts +87 -0
  20. package/broker/ownership.ts +61 -0
  21. package/broker/paths.ts +161 -0
  22. package/broker/spawn.ts +453 -0
  23. package/codex/app-server-client.ts +562 -0
  24. package/codex/boss-client.ts +34 -0
  25. package/codex/bridge-config.ts +282 -0
  26. package/codex/bridge-daemon.ts +913 -0
  27. package/codex/clipboard.ts +142 -0
  28. package/codex/coi.ts +802 -0
  29. package/codex/contact.ts +54 -0
  30. package/codex/mcp-protocol.ts +248 -0
  31. package/codex/runtime.ts +488 -0
  32. package/codex/server.ts +60 -0
  33. package/codex/team.ts +249 -0
  34. package/codex/tui-input.ts +174 -0
  35. package/config.ts +195 -0
  36. package/dist/bridge-daemon.mjs +3781 -0
  37. package/dist/broker.mjs +3012 -0
  38. package/dist/build-info.json +12 -0
  39. package/dist/codex-server.mjs +2890 -0
  40. package/dist/coi.mjs +4686 -0
  41. package/durable-json.ts +25 -0
  42. package/licenses/MIT-pi-intercom.txt +21 -0
  43. package/outbound-outbox.ts +116 -0
  44. package/package.json +89 -0
  45. package/protocol-v4/contract.ts +27 -0
  46. package/provider/protected-service.ts +162 -0
  47. package/provider/provider.mjs +34 -0
  48. package/scripts/build-info.mjs +58 -0
  49. package/scripts/build.mjs +55 -0
  50. package/skills/codex-intercom/SKILL.md +104 -0
  51. package/tsconfig.json +16 -0
  52. package/types.ts +225 -0
@@ -0,0 +1,306 @@
1
+ import { existsSync, readFileSync, renameSync } from "node:fs";
2
+ import { canonicalJson } from "@ctliz/agent-intercom-core/canonical";
3
+ import { writeDurableJson } from "../durable-json.ts";
4
+ import { assertBossCanonicalData } from "./boss-adapter.ts";
5
+ import { restrictIntercomRuntimeFile } from "./paths.ts";
6
+ import type { BrokerMessage } from "../types.ts";
7
+
8
+ const BOSS_CONTROL_LEDGER_VERSION = 3;
9
+ const EXPIRING_BOSS_CONTROL_LEDGER_VERSION = 2;
10
+ const MAX_BOSS_CONTROL_RESULTS = 2048;
11
+ const BOSS_CONTROL_FAILURE_CODES = new Set([
12
+ "INVALID_CONTROL",
13
+ "IDEMPOTENCY_CONFLICT",
14
+ "SESSION_NOT_FOUND",
15
+ "POLICY_DENIED",
16
+ "RECIPIENT_DISCONNECTED",
17
+ "DELIVERY_TIMEOUT",
18
+ ]);
19
+
20
+ export type BossControlResult = Extract<BrokerMessage, { type: "boss_control_result" }>;
21
+ export type BossControlAck = Extract<BrokerMessage, { type: "boss_control_ack" }>;
22
+
23
+ interface BossControlLedgerEntryBase {
24
+ scope: string;
25
+ fingerprint: string;
26
+ }
27
+
28
+ interface AcceptedBossControlLedgerEntry extends BossControlLedgerEntryBase {
29
+ state: "accepted";
30
+ deliveryId: string;
31
+ }
32
+
33
+ interface TerminalBossControlLedgerEntry extends BossControlLedgerEntryBase {
34
+ state: "terminal";
35
+ result: BossControlResult;
36
+ }
37
+
38
+ type BossControlLedgerEntry = AcceptedBossControlLedgerEntry | TerminalBossControlLedgerEntry;
39
+
40
+ interface BossControlLedgerState {
41
+ version: typeof BOSS_CONTROL_LEDGER_VERSION;
42
+ entries: BossControlLedgerEntry[];
43
+ }
44
+
45
+ function exactStringKeys(value: Record<string, unknown>, required: readonly string[], optional: readonly string[] = []): boolean {
46
+ const keys = Reflect.ownKeys(value);
47
+ const permitted = new Set([...required, ...optional]);
48
+ return required.every((key) => Object.hasOwn(value, key))
49
+ && keys.every((key) => typeof key === "string" && permitted.has(key));
50
+ }
51
+
52
+ export function parseBossControlResult(value: unknown): BossControlResult {
53
+ assertBossCanonicalData(value, "$.bossControlResult");
54
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
55
+ throw new Error("Boss control result must be an exact plain object");
56
+ }
57
+ const result = value as Record<string, unknown>;
58
+ const base = typeof result.requestId === "string"
59
+ && result.requestId.length > 0
60
+ && result.messageId === result.requestId
61
+ && typeof result.idempotencyKey === "string"
62
+ && result.idempotencyKey.length > 0;
63
+ if (!base || result.type !== "boss_control_result") throw new Error("Invalid Boss control result binding");
64
+ if (
65
+ result.status === "delivered"
66
+ && result.delivered === true
67
+ && typeof result.deliveryId === "string"
68
+ && result.deliveryId.length > 0
69
+ && exactStringKeys(result, ["type", "requestId", "messageId", "idempotencyKey", "status", "delivered", "deliveryId"])
70
+ ) return result as unknown as BossControlResult;
71
+ if (
72
+ result.status === "rejected"
73
+ && result.delivered === false
74
+ && typeof result.code === "string"
75
+ && BOSS_CONTROL_FAILURE_CODES.has(result.code)
76
+ && typeof result.reason === "string"
77
+ && result.reason.length > 0
78
+ && (!Object.hasOwn(result, "deliveryId") || (typeof result.deliveryId === "string" && result.deliveryId.length > 0))
79
+ && exactStringKeys(
80
+ result,
81
+ ["type", "requestId", "messageId", "idempotencyKey", "status", "delivered", "code", "reason"],
82
+ ["deliveryId"],
83
+ )
84
+ ) return result as unknown as BossControlResult;
85
+ throw new Error("Invalid Boss control result discriminant");
86
+ }
87
+
88
+ export function parseBossControlAck(value: unknown): BossControlAck {
89
+ assertBossCanonicalData(value, "$.bossControlAck");
90
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
91
+ throw new Error("Boss control acknowledgement must be an exact plain object");
92
+ }
93
+ const ack = value as Record<string, unknown>;
94
+ if (
95
+ !exactStringKeys(ack, ["type", "requestId", "messageId", "idempotencyKey", "status", "deliveryId"])
96
+ || ack.type !== "boss_control_ack"
97
+ || typeof ack.requestId !== "string"
98
+ || ack.requestId.length === 0
99
+ || ack.messageId !== ack.requestId
100
+ || typeof ack.idempotencyKey !== "string"
101
+ || ack.idempotencyKey.length === 0
102
+ || ack.status !== "accepted"
103
+ || typeof ack.deliveryId !== "string"
104
+ || ack.deliveryId.length === 0
105
+ ) throw new Error("Invalid Boss control acknowledgement discriminant");
106
+ return ack as unknown as BossControlAck;
107
+ }
108
+
109
+ export function rebindBossControlResult(resultValue: unknown, messageId: string): BossControlResult {
110
+ if (typeof messageId !== "string" || messageId.length === 0) throw new Error("Replay messageId is required");
111
+ const result = parseBossControlResult(resultValue);
112
+ return parseBossControlResult({ ...result, requestId: messageId, messageId });
113
+ }
114
+
115
+ export function bossControlReplayFrames(resultValue: unknown, messageId: string): [BossControlResult] | [BossControlAck, BossControlResult] {
116
+ const result = rebindBossControlResult(resultValue, messageId);
117
+ if (result.deliveryId === undefined) return [result];
118
+ return [{
119
+ type: "boss_control_ack",
120
+ requestId: messageId,
121
+ messageId,
122
+ idempotencyKey: result.idempotencyKey,
123
+ status: "accepted",
124
+ deliveryId: result.deliveryId,
125
+ }, result];
126
+ }
127
+
128
+ /**
129
+ * A caller recovering a durable accepted entry is newly attached to the
130
+ * stable delivery. It must observe that accepted transition before any
131
+ * terminal denial produced while the broker revalidates the target.
132
+ */
133
+ export function bossControlAcceptedRecoveryFrames(
134
+ resultValue: unknown,
135
+ ): [BossControlAck, BossControlResult] {
136
+ const result = parseBossControlResult(resultValue);
137
+ if (result.status !== "rejected" || result.deliveryId === undefined) {
138
+ throw new Error("Accepted Boss recovery requires a delivery-bound rejected result");
139
+ }
140
+ return [{
141
+ type: "boss_control_ack",
142
+ requestId: result.requestId,
143
+ messageId: result.messageId,
144
+ idempotencyKey: result.idempotencyKey,
145
+ status: "accepted",
146
+ deliveryId: result.deliveryId,
147
+ }, result];
148
+ }
149
+
150
+ function parseHash(value: unknown, field: string): string {
151
+ if (typeof value !== "string" || !/^[a-f0-9]{64}$/.test(value)) throw new Error(`Invalid Boss ledger ${field}`);
152
+ return value;
153
+ }
154
+
155
+ function parseEntry(value: unknown, version: number): BossControlLedgerEntry {
156
+ assertBossCanonicalData(value, "$.entries[]");
157
+ if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error("Invalid Boss ledger entry");
158
+ const entry = value as Record<string, unknown>;
159
+ const legacyExpiry = version === EXPIRING_BOSS_CONTROL_LEDGER_VERSION;
160
+ if (legacyExpiry && (typeof entry.expiresAt !== "number" || !Number.isSafeInteger(entry.expiresAt))) {
161
+ throw new Error("Invalid Boss ledger expiry");
162
+ }
163
+ const base: BossControlLedgerEntryBase = {
164
+ scope: parseHash(entry.scope, "scope"),
165
+ fingerprint: parseHash(entry.fingerprint, "fingerprint"),
166
+ };
167
+ const baseKeys = legacyExpiry
168
+ ? ["scope", "fingerprint", "expiresAt", "state"]
169
+ : ["scope", "fingerprint", "state"];
170
+ if (
171
+ entry.state === "accepted"
172
+ && exactStringKeys(entry, [...baseKeys, "deliveryId"])
173
+ && typeof entry.deliveryId === "string"
174
+ && entry.deliveryId.length > 0
175
+ ) return { ...base, state: "accepted", deliveryId: entry.deliveryId };
176
+ if (entry.state === "terminal" && exactStringKeys(entry, [...baseKeys, "result"])) {
177
+ return { ...base, state: "terminal", result: parseBossControlResult(entry.result) };
178
+ }
179
+ throw new Error("Invalid Boss ledger state discriminant");
180
+ }
181
+
182
+ export type BossControlLedgerLookup =
183
+ | { status: "miss" }
184
+ | { status: "conflict" }
185
+ | { status: "accepted"; deliveryId: string }
186
+ | { status: "replay"; result: BossControlResult };
187
+
188
+ /**
189
+ * Boss bindings outlive transport reconnects and the ordinary recent-delivery
190
+ * cache. Until the broker has an authoritative Boss run/binding teardown
191
+ * signal, retaining every accepted binding and terminal tombstone is the only
192
+ * fail-closed policy. Capacity exhaustion therefore rejects new work instead
193
+ * of silently forgetting an idempotency scope.
194
+ */
195
+ export class BossControlResultLedger {
196
+ private state: BossControlLedgerState;
197
+
198
+ constructor(
199
+ private readonly path: string,
200
+ private readonly now: () => number = Date.now,
201
+ ) {
202
+ const loaded = this.load();
203
+ this.state = loaded.state;
204
+ if (loaded.migrated) this.persist();
205
+ }
206
+
207
+ lookup(scope: string, fingerprint: string): BossControlLedgerLookup {
208
+ const entry = this.state.entries.find((candidate) => candidate.scope === scope);
209
+ if (!entry) return { status: "miss" };
210
+ if (entry.fingerprint !== fingerprint) return { status: "conflict" };
211
+ return entry.state === "accepted"
212
+ ? { status: "accepted", deliveryId: entry.deliveryId }
213
+ : { status: "replay", result: structuredClone(entry.result) };
214
+ }
215
+
216
+ recordAccepted(scope: string, fingerprint: string, deliveryId: string): void {
217
+ if (!/^[a-f0-9]{64}$/.test(scope) || !/^[a-f0-9]{64}$/.test(fingerprint) || !deliveryId) {
218
+ throw new Error("Invalid Boss accepted-state binding");
219
+ }
220
+ const existing = this.state.entries.find((entry) => entry.scope === scope);
221
+ if (existing) {
222
+ if (existing.fingerprint !== fingerprint || existing.state !== "accepted" || existing.deliveryId !== deliveryId) {
223
+ throw new Error("Boss idempotency scope is already bound to a different canonical state");
224
+ }
225
+ return;
226
+ }
227
+ this.reserveCapacity();
228
+ this.state.entries.push({ scope, fingerprint, state: "accepted", deliveryId });
229
+ this.persist();
230
+ }
231
+
232
+ recordTerminal(scope: string, fingerprint: string, resultValue: unknown): void {
233
+ const result = parseBossControlResult(resultValue);
234
+ canonicalJson(result);
235
+ const existing = this.state.entries.find((entry) => entry.scope === scope);
236
+ if (existing?.fingerprint !== undefined && existing.fingerprint !== fingerprint) {
237
+ throw new Error("Boss idempotency scope is already bound to a different canonical request");
238
+ }
239
+ if (existing?.state === "terminal") {
240
+ const existingStable = { ...existing.result, requestId: "<caller>", messageId: "<caller>" };
241
+ const resultStable = { ...result, requestId: "<caller>", messageId: "<caller>" };
242
+ if (canonicalJson(existingStable) !== canonicalJson(resultStable)) {
243
+ throw new Error("Boss idempotency scope is already bound to a different canonical result");
244
+ }
245
+ return;
246
+ }
247
+ if (result.status === "delivered" || result.deliveryId !== undefined) {
248
+ if (existing?.state !== "accepted" || existing.deliveryId !== result.deliveryId) {
249
+ throw new Error("Boss terminal delivery requires the matching durable accepted state");
250
+ }
251
+ } else if (existing?.state === "accepted") {
252
+ throw new Error("A terminal result after acceptance must carry the accepted deliveryId");
253
+ }
254
+ const terminal: TerminalBossControlLedgerEntry = {
255
+ scope,
256
+ fingerprint,
257
+ state: "terminal",
258
+ result,
259
+ };
260
+ if (existing) this.state.entries[this.state.entries.indexOf(existing)] = terminal;
261
+ else {
262
+ this.reserveCapacity();
263
+ this.state.entries.push(terminal);
264
+ }
265
+ this.persist();
266
+ }
267
+
268
+ private reserveCapacity(): void {
269
+ if (this.state.entries.length >= MAX_BOSS_CONTROL_RESULTS) {
270
+ throw new Error("Durable Boss control ledger is full");
271
+ }
272
+ }
273
+
274
+ private load(): { state: BossControlLedgerState; migrated: boolean } {
275
+ if (!existsSync(this.path)) {
276
+ return { state: { version: BOSS_CONTROL_LEDGER_VERSION, entries: [] }, migrated: false };
277
+ }
278
+ try {
279
+ const parsed: unknown = JSON.parse(readFileSync(this.path, "utf8"));
280
+ assertBossCanonicalData(parsed, "$.bossControlLedger");
281
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error("expected object");
282
+ const state = parsed as Record<string, unknown>;
283
+ if (
284
+ !exactStringKeys(state, ["version", "entries"])
285
+ || (state.version !== BOSS_CONTROL_LEDGER_VERSION && state.version !== EXPIRING_BOSS_CONTROL_LEDGER_VERSION)
286
+ || !Array.isArray(state.entries)
287
+ ) throw new Error("invalid ledger state");
288
+ return {
289
+ state: {
290
+ version: BOSS_CONTROL_LEDGER_VERSION,
291
+ entries: state.entries.map((entry) => parseEntry(entry, state.version as number)),
292
+ },
293
+ migrated: state.version === EXPIRING_BOSS_CONTROL_LEDGER_VERSION,
294
+ };
295
+ } catch (error) {
296
+ const corruptPath = `${this.path}.corrupt-${this.now()}`;
297
+ renameSync(this.path, corruptPath);
298
+ restrictIntercomRuntimeFile(corruptPath);
299
+ throw new Error(`Boss control ledger was corrupt and quarantined at ${corruptPath}`, { cause: error });
300
+ }
301
+ }
302
+
303
+ private persist(): void {
304
+ writeDurableJson(this.path, this.state);
305
+ }
306
+ }