@parall/codex-agent 1.44.0 → 1.46.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/dist/app-server-process.d.ts +9 -0
- package/dist/app-server-process.d.ts.map +1 -0
- package/dist/app-server-process.js +58 -0
- package/dist/app-server-protocol.d.ts +19 -0
- package/dist/app-server-protocol.d.ts.map +1 -0
- package/dist/app-server-protocol.js +42 -0
- package/dist/dispatch.d.ts +88 -5
- package/dist/dispatch.d.ts.map +1 -1
- package/dist/dispatch.js +364 -196
- package/dist/index.js +27 -11
- package/dist/instructions-refresh.d.ts +136 -0
- package/dist/instructions-refresh.d.ts.map +1 -0
- package/dist/instructions-refresh.js +244 -0
- package/dist/jsonrpc-client.d.ts +49 -1
- package/dist/jsonrpc-client.d.ts.map +1 -1
- package/dist/jsonrpc-client.js +77 -5
- package/dist/legacy-workspace-config-migration.d.ts +112 -0
- package/dist/legacy-workspace-config-migration.d.ts.map +1 -0
- package/dist/legacy-workspace-config-migration.js +229 -0
- package/dist/server-requests.d.ts +10 -0
- package/dist/server-requests.d.ts.map +1 -0
- package/dist/server-requests.js +39 -0
- package/dist/session-manager.d.ts +12 -0
- package/dist/session-manager.d.ts.map +1 -1
- package/dist/session-manager.js +53 -3
- package/dist/turn-sink.d.ts +26 -0
- package/dist/turn-sink.d.ts.map +1 -0
- package/dist/turn-sink.js +45 -0
- package/dist/workspace.d.ts +23 -25
- package/dist/workspace.d.ts.map +1 -1
- package/dist/workspace.js +138 -138
- package/package.json +5 -5
- package/src/app-server-process.ts +59 -0
- package/src/app-server-protocol.ts +46 -0
- package/src/dispatch.ts +426 -204
- package/src/index.ts +35 -10
- package/src/instructions-refresh.ts +367 -0
- package/src/jsonrpc-client.ts +109 -7
- package/src/legacy-workspace-config-migration.ts +296 -0
- package/src/server-requests.ts +40 -0
- package/src/session-manager.ts +74 -6
- package/src/turn-sink.ts +54 -0
- package/src/workspace.ts +155 -155
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* One-shot migration for workspaces last bootstrapped by a pre-protocol-delivery
|
|
6
|
+
* bridge.
|
|
7
|
+
*
|
|
8
|
+
* Those generations (every released bridge up to and including v1.44.0) injected
|
|
9
|
+
* the system prompt by writing `<workspace>/.codex/config.toml`
|
|
10
|
+
* (`developer_instructions`) and marking the workspace trusted in the operator's
|
|
11
|
+
* global codex config. Instructions now ride the app-server's per-thread
|
|
12
|
+
* `developerInstructions` param, so a leftover bridge-authored file would DOUBLE
|
|
13
|
+
* the instructions on any workspace codex still considers trusted.
|
|
14
|
+
*
|
|
15
|
+
* Deleting a file in someone's workspace is the destructive direction, so the
|
|
16
|
+
* gate is fail-closed: we remove ONLY what is provably the retired bridge's own
|
|
17
|
+
* artifact, and preserve (with a warning) everything else. A duplicated prompt
|
|
18
|
+
* is a degraded agent; a deleted operator file is lost work.
|
|
19
|
+
*
|
|
20
|
+
* WHY THIS IS ONE-SHOT AND NOT MERELY IDEMPOTENT. The authorship proof is
|
|
21
|
+
* `.parall/system-prompt.md`, and the bridge REWRITES that file on every boot.
|
|
22
|
+
* So it is legacy-era evidence exactly once: on the first protocol-delivery boot,
|
|
23
|
+
* before anything overwrites it. A gate that merely re-ran each boot would, from
|
|
24
|
+
* boot 2 on, be comparing the legacy config against a prompt THIS bridge wrote —
|
|
25
|
+
* evidence it manufactured itself. That is not hypothetical: prompt assembly is
|
|
26
|
+
* deterministic, so a file the first boot explicitly preserved as "not provably
|
|
27
|
+
* ours" can match on the next boot and be silently deleted, contradicting the
|
|
28
|
+
* warning the operator was just given. Fail-closed has to hold across the whole
|
|
29
|
+
* migration lifecycle, not per function call.
|
|
30
|
+
*
|
|
31
|
+
* Hence a versioned, one-way CLAIM (`.parall/legacy-workspace-config-migration.v1`),
|
|
32
|
+
* created atomically (`wx`) BEFORE any prompt write:
|
|
33
|
+
* - exactly one boot ever wins the claim — it alone may delete;
|
|
34
|
+
* - every later boot (and every concurrent loser) skips, silently, forever;
|
|
35
|
+
* - a claim that cannot be persisted FAILS the boot before the proof is
|
|
36
|
+
* overwritten, so a retry still has real evidence;
|
|
37
|
+
* - a boot that dies mid-migration leaves the claim behind, so the file is
|
|
38
|
+
* preserved forever rather than re-judged against fabricated evidence.
|
|
39
|
+
*
|
|
40
|
+
* The whole module is dead code once no workspace can still hold a pre-#1866
|
|
41
|
+
* artifact — sunset conditions in
|
|
42
|
+
* docs/tech-debt/codex-legacy-workspace-config-shim.md.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/** Relative location of the retired bridge's workspace config. */
|
|
46
|
+
const LEGACY_CONFIG_RELPATH = ['.codex', 'config.toml'];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The one-way claim. Versioned: a future migration gets its own sentinel rather
|
|
50
|
+
* than reusing (or re-arming) this one.
|
|
51
|
+
*/
|
|
52
|
+
const MIGRATION_SENTINEL_RELPATH = ['.parall', 'legacy-workspace-config-migration.v1'];
|
|
53
|
+
|
|
54
|
+
export function legacyWorkspaceConfigPath(workspaceDir: string): string {
|
|
55
|
+
return path.join(workspaceDir, ...LEGACY_CONFIG_RELPATH);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function migrationSentinelPath(workspaceDir: string): string {
|
|
59
|
+
return path.join(workspaceDir, ...MIGRATION_SENTINEL_RELPATH);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Byte-exact reconstruction of the retired serializer. Verified identical across
|
|
64
|
+
* every released bridge that wrote this file (v1.37.0 … v1.44.0 — the line is
|
|
65
|
+
* byte-for-byte the same in all of them), so a single reconstruction covers the
|
|
66
|
+
* whole legacy fleet. The deletion gate compares raw bytes against this: it must
|
|
67
|
+
* never drift.
|
|
68
|
+
*/
|
|
69
|
+
export function legacyWorkspaceConfigToml(prompt: string): string {
|
|
70
|
+
return `developer_instructions = """\n${prompt.replace(/\\/g, '\\\\').replace(/"""/g, '\\"""')}\n"""\n`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The PRE-OVERWRITE `.parall/system-prompt.md`. The retired bridge wrote the
|
|
75
|
+
* reference copy and the config from the SAME string in the same call, so a byte
|
|
76
|
+
* match against `legacyWorkspaceConfigToml(prompt)` is what identifies our own
|
|
77
|
+
* artifact.
|
|
78
|
+
*
|
|
79
|
+
* `absent` (ENOENT — the bridge never bootstrapped this workspace) and
|
|
80
|
+
* `unreadable` (it is there, but an I/O error hid it) both mean "cannot prove
|
|
81
|
+
* ownership, so do not delete" — but they are different facts, and reporting the
|
|
82
|
+
* second as the first tells the operator the wrong reason for a preserved file.
|
|
83
|
+
*/
|
|
84
|
+
export type AuthorshipProof =
|
|
85
|
+
| { kind: 'present'; prompt: string }
|
|
86
|
+
| { kind: 'absent' }
|
|
87
|
+
| { kind: 'unreadable' };
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* What the filesystem says about the legacy config. Gathered by the executor;
|
|
91
|
+
* the classifier below sees nothing else — no fs, no clock, no env.
|
|
92
|
+
*/
|
|
93
|
+
export type LegacyConfigFacts = {
|
|
94
|
+
/** lstat of the legacy path — never a stat: a symlink must not be followed. `null` = ENOENT. */
|
|
95
|
+
entry: { isPlainFile: boolean; hardLinks: number } | null;
|
|
96
|
+
/** Raw bytes of the legacy file. `null` = not a plain file, or unreadable. */
|
|
97
|
+
rawContent: string | null;
|
|
98
|
+
proof: AuthorshipProof;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export type LegacyConfigVerdict =
|
|
102
|
+
| { action: 'none' }
|
|
103
|
+
| { action: 'remove' }
|
|
104
|
+
| { action: 'preserve'; reason: PreserveReason };
|
|
105
|
+
|
|
106
|
+
export type PreserveReason =
|
|
107
|
+
/** Symlink (a dotfiles arrangement) or a directory — unlink-by-path would sever the operator's link. */
|
|
108
|
+
| 'not-a-plain-file'
|
|
109
|
+
/** Extra hard links: the same inode is reachable from a path we know nothing about. */
|
|
110
|
+
| 'extra-hard-links'
|
|
111
|
+
/** No `.parall/system-prompt.md` from a previous boot — no proof can exist. */
|
|
112
|
+
| 'no-authorship-proof'
|
|
113
|
+
/** The proof copy exists but could not be read, so ownership cannot be evaluated. */
|
|
114
|
+
| 'authorship-proof-unreadable'
|
|
115
|
+
/** Could not read the legacy config's own bytes, so the proof cannot be applied. */
|
|
116
|
+
| 'config-unreadable'
|
|
117
|
+
/** Bytes differ from what the retired bridge would have written (operator-authored, comments, hand-edited). */
|
|
118
|
+
| 'content-mismatch';
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Pure. Decides the fate of the legacy config from filesystem facts alone.
|
|
122
|
+
*
|
|
123
|
+
* The only path to `remove` is: plain regular file + exactly one hard link + a
|
|
124
|
+
* pre-overwrite reference copy exists + raw bytes are EXACTLY the retired
|
|
125
|
+
* serializer's output for that reference prompt. Every other combination
|
|
126
|
+
* preserves — including value-level near-misses (an operator file carrying the
|
|
127
|
+
* same `developer_instructions` value plus a comment would be destroyed together
|
|
128
|
+
* with the comment by a value-level compare).
|
|
129
|
+
*/
|
|
130
|
+
export function classifyLegacyWorkspaceConfig(facts: LegacyConfigFacts): LegacyConfigVerdict {
|
|
131
|
+
const { entry, rawContent, proof } = facts;
|
|
132
|
+
if (!entry) return { action: 'none' };
|
|
133
|
+
if (!entry.isPlainFile) return { action: 'preserve', reason: 'not-a-plain-file' };
|
|
134
|
+
if (entry.hardLinks !== 1) return { action: 'preserve', reason: 'extra-hard-links' };
|
|
135
|
+
if (proof.kind === 'absent') return { action: 'preserve', reason: 'no-authorship-proof' };
|
|
136
|
+
if (proof.kind === 'unreadable') {
|
|
137
|
+
return { action: 'preserve', reason: 'authorship-proof-unreadable' };
|
|
138
|
+
}
|
|
139
|
+
if (rawContent === null) return { action: 'preserve', reason: 'config-unreadable' };
|
|
140
|
+
if (rawContent !== legacyWorkspaceConfigToml(proof.prompt)) {
|
|
141
|
+
return { action: 'preserve', reason: 'content-mismatch' };
|
|
142
|
+
}
|
|
143
|
+
return { action: 'remove' };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const PRESERVE_DETAIL: Record<PreserveReason, string> = {
|
|
147
|
+
'not-a-plain-file':
|
|
148
|
+
'it is a symlink or directory, not a plain file the bridge could have written',
|
|
149
|
+
'extra-hard-links': 'the file has more than one hard link, so another path shares this inode',
|
|
150
|
+
'no-authorship-proof':
|
|
151
|
+
'this workspace has no .parall/system-prompt.md from a previous boot, so nothing here can be proven ours',
|
|
152
|
+
'authorship-proof-unreadable':
|
|
153
|
+
'the .parall/system-prompt.md authorship proof exists but could not be read, so ownership cannot be evaluated',
|
|
154
|
+
'config-unreadable': 'its own bytes could not be read, so authorship cannot be proven',
|
|
155
|
+
'content-mismatch': 'its bytes are not what the retired bridge would have written',
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const SENTINEL_BODY = `Parall codex bridge — one-shot workspace migration record (v1)
|
|
159
|
+
|
|
160
|
+
The legacy workspace-config cleanup has been CLAIMED for this workspace. Only the
|
|
161
|
+
boot that created this file was allowed to remove a bridge-authored
|
|
162
|
+
.codex/config.toml, and only under a byte-exact authorship proof.
|
|
163
|
+
|
|
164
|
+
While this file exists the bridge will NEVER auto-remove .codex/config.toml again.
|
|
165
|
+
Deleting this file does not safely re-arm the migration: the evidence it relied on
|
|
166
|
+
(.parall/system-prompt.md as written by a pre-protocol-delivery bridge) has since
|
|
167
|
+
been overwritten by this bridge, so a re-run could mistake a prompt it wrote itself
|
|
168
|
+
for legacy evidence and delete a file that is not ours.
|
|
169
|
+
|
|
170
|
+
If a stale .codex/config.toml is still present, remove it by hand.
|
|
171
|
+
`;
|
|
172
|
+
|
|
173
|
+
export type MigrationClaim = 'claimed' | 'already-claimed';
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Atomically take the one-way claim. `wx` makes this a single filesystem
|
|
177
|
+
* operation, so concurrent boots cannot both win.
|
|
178
|
+
*
|
|
179
|
+
* THROWS if the claim cannot be persisted (EACCES, EIO, EROFS …). That is
|
|
180
|
+
* deliberate and load-bearing: the caller runs this BEFORE overwriting
|
|
181
|
+
* `.parall/system-prompt.md`, so a boot that cannot record its claim must die
|
|
182
|
+
* with the legacy evidence still intact rather than proceed to manufacture a
|
|
183
|
+
* prompt that a later boot would mistake for that evidence.
|
|
184
|
+
*/
|
|
185
|
+
export function claimLegacyWorkspaceConfigMigration(workspaceDir: string): MigrationClaim {
|
|
186
|
+
const sentinel = migrationSentinelPath(workspaceDir);
|
|
187
|
+
// The module owns its own sentinel, directory included — a caller that has not
|
|
188
|
+
// created `.parall` yet must still get a real claim, not an ENOENT throw.
|
|
189
|
+
fs.mkdirSync(path.dirname(sentinel), { recursive: true });
|
|
190
|
+
try {
|
|
191
|
+
fs.writeFileSync(sentinel, SENTINEL_BODY, { flag: 'wx' });
|
|
192
|
+
return 'claimed';
|
|
193
|
+
} catch (err) {
|
|
194
|
+
if ((err as NodeJS.ErrnoException).code === 'EEXIST') return 'already-claimed';
|
|
195
|
+
throw err;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The migration lifecycle: claim, then — only if we won the claim — read the
|
|
201
|
+
* proof and clean up.
|
|
202
|
+
*
|
|
203
|
+
* `readProof` is a THUNK, not a value, and that is the whole point. Reading the
|
|
204
|
+
* proof is not free of side effects: an unreadable `.parall/system-prompt.md`
|
|
205
|
+
* warns. Passed as an already-evaluated argument, that warning fires before the
|
|
206
|
+
* claim is even checked — so an already-claimed later boot, or a concurrent
|
|
207
|
+
* loser, would emit a migration warning about a decision it is not making. The
|
|
208
|
+
* thunk makes "only the winner touches the proof" a property of the type rather
|
|
209
|
+
* than of the caller's evaluation order.
|
|
210
|
+
*
|
|
211
|
+
* The caller still owns `.parall/system-prompt.md` and must hand over the
|
|
212
|
+
* PRE-OVERWRITE copy: the winner evaluates the thunk here, before anything
|
|
213
|
+
* writes a new prompt.
|
|
214
|
+
*
|
|
215
|
+
* A boot that does not win the claim returns silently — it must not delete, it
|
|
216
|
+
* must not read, and it must not warn: the winner may be deleting the very file
|
|
217
|
+
* it would warn about, and a stale warning about a file that is already gone is
|
|
218
|
+
* exactly the misleading diagnostic this module exists to avoid.
|
|
219
|
+
*/
|
|
220
|
+
export function runLegacyWorkspaceConfigMigration(
|
|
221
|
+
workspaceDir: string,
|
|
222
|
+
readProof: () => AuthorshipProof,
|
|
223
|
+
log?: { warn: (msg: string) => void },
|
|
224
|
+
): void {
|
|
225
|
+
if (claimLegacyWorkspaceConfigMigration(workspaceDir) === 'already-claimed') return;
|
|
226
|
+
cleanupLegacyWorkspaceConfig(workspaceDir, readProof(), log);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Filesystem execution for the one boot that holds the claim. Reads the facts,
|
|
231
|
+
* asks the classifier, applies the verdict.
|
|
232
|
+
*
|
|
233
|
+
* Never throws: a workspace that cannot be migrated must still boot. Every
|
|
234
|
+
* non-ENOENT problem warns.
|
|
235
|
+
*/
|
|
236
|
+
function cleanupLegacyWorkspaceConfig(
|
|
237
|
+
workspaceDir: string,
|
|
238
|
+
proof: AuthorshipProof,
|
|
239
|
+
log?: { warn: (msg: string) => void },
|
|
240
|
+
): void {
|
|
241
|
+
const configPath = legacyWorkspaceConfigPath(workspaceDir);
|
|
242
|
+
|
|
243
|
+
let entry: fs.Stats;
|
|
244
|
+
try {
|
|
245
|
+
entry = fs.lstatSync(configPath);
|
|
246
|
+
} catch (err) {
|
|
247
|
+
// Only ENOENT means "nothing to clean up". Anything else (EACCES, EIO, …)
|
|
248
|
+
// leaves an unverifiable file behind — surface it rather than silently
|
|
249
|
+
// treating it as absent.
|
|
250
|
+
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') {
|
|
251
|
+
log?.warn(`could not inspect legacy workspace codex config ${configPath}: ${String(err)}`);
|
|
252
|
+
}
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const isPlainFile = entry.isFile();
|
|
257
|
+
let rawContent: string | null = null;
|
|
258
|
+
if (isPlainFile) {
|
|
259
|
+
try {
|
|
260
|
+
rawContent = fs.readFileSync(configPath, 'utf8');
|
|
261
|
+
} catch (err) {
|
|
262
|
+
// Read failure → rawContent stays null → the classifier preserves
|
|
263
|
+
// ('config-unreadable'). Log the cause here; the verdict warning follows.
|
|
264
|
+
log?.warn(`could not read legacy workspace codex config ${configPath}: ${String(err)}`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const verdict = classifyLegacyWorkspaceConfig({
|
|
269
|
+
entry: { isPlainFile, hardLinks: entry.nlink },
|
|
270
|
+
rawContent,
|
|
271
|
+
proof,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
if (verdict.action === 'none') return;
|
|
275
|
+
|
|
276
|
+
if (verdict.action === 'remove') {
|
|
277
|
+
try {
|
|
278
|
+
fs.unlinkSync(configPath);
|
|
279
|
+
} catch (err) {
|
|
280
|
+
// ENOENT here means the file went away between our lstat and this unlink
|
|
281
|
+
// — a second bridge booting the same workspace (rapid daemon respawn) got
|
|
282
|
+
// there first. That is exactly the end state we wanted, so it is not a
|
|
283
|
+
// failure: warning "could not remove" about a file that is already gone
|
|
284
|
+
// would send the operator after nothing. Concurrent migrations converge.
|
|
285
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return;
|
|
286
|
+
log?.warn(`could not remove legacy workspace codex config ${configPath}: ${String(err)}`);
|
|
287
|
+
}
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
log?.warn(
|
|
292
|
+
`leaving ${configPath} in place — ${PRESERVE_DETAIL[verdict.reason]}, so it is not provably ` +
|
|
293
|
+
"the retired bridge's own artifact. Codex loads it for trusted workspaces IN ADDITION to " +
|
|
294
|
+
'the platform instructions; remove it manually if it is a stale artifact.',
|
|
295
|
+
);
|
|
296
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Answers for codex app-server server→client requests.
|
|
3
|
+
*
|
|
4
|
+
* The bridge runs headless with `approvalPolicy: "never"`, so approval-shaped
|
|
5
|
+
* requests should not occur — but the protocol allows the app-server to send
|
|
6
|
+
* them (e.g. a thread that fell back to `on-request`), and an unanswered
|
|
7
|
+
* request parks its turn until the dispatch deadline. Every known
|
|
8
|
+
* approval-shaped method is answered with an explicit denial; unknown methods
|
|
9
|
+
* get a method-not-found error from the JSON-RPC layer (`undefined` here).
|
|
10
|
+
*
|
|
11
|
+
* Wire shapes verified against codex-rs 0.144.1
|
|
12
|
+
* (`app-server-protocol/src/protocol/common.rs` server_request_definitions):
|
|
13
|
+
* - v2 `item/commandExecution/requestApproval` / `item/fileChange/requestApproval`
|
|
14
|
+
* respond `{ decision }` with camelCase variants — `decline` denies but lets
|
|
15
|
+
* the turn continue (vs `cancel`, which also interrupts the turn).
|
|
16
|
+
* - legacy v1 `execCommandApproval` / `applyPatchApproval` (SendUserTurn-era,
|
|
17
|
+
* unused by this bridge) respond `{ decision }` with snake_case
|
|
18
|
+
* ReviewDecision values.
|
|
19
|
+
*/
|
|
20
|
+
const APPROVAL_DENIALS: Record<string, unknown> = {
|
|
21
|
+
'item/commandExecution/requestApproval': { decision: 'decline' },
|
|
22
|
+
'item/fileChange/requestApproval': { decision: 'decline' },
|
|
23
|
+
// NOT listed: `item/permissions/requestApproval` — its response shape is a
|
|
24
|
+
// permission GRANT (no deny variant), so denial is correctly expressed by
|
|
25
|
+
// the -32601 error fallback.
|
|
26
|
+
execCommandApproval: { decision: 'denied' },
|
|
27
|
+
applyPatchApproval: { decision: 'denied' },
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the response payload for a server request, or `undefined` when the
|
|
32
|
+
* method is not one we can meaningfully answer (the JSON-RPC client then
|
|
33
|
+
* replies -32601, which also unblocks the app-server). Own-property lookup
|
|
34
|
+
* only — a method named like an Object.prototype member (`toString`,
|
|
35
|
+
* `constructor`) must fall through to -32601, not return an inherited
|
|
36
|
+
* function that would serialize into a result-less frame.
|
|
37
|
+
*/
|
|
38
|
+
export function answerServerRequest(method: string): unknown | undefined {
|
|
39
|
+
return Object.hasOwn(APPROVAL_DENIALS, method) ? APPROVAL_DENIALS[method] : undefined;
|
|
40
|
+
}
|
package/src/session-manager.ts
CHANGED
|
@@ -5,6 +5,22 @@ import type { ForkSessionHandle } from '@parall/agent-core';
|
|
|
5
5
|
type PersistedThread = {
|
|
6
6
|
runtimeKey: string;
|
|
7
7
|
threadId: string;
|
|
8
|
+
/**
|
|
9
|
+
* sha256 (hex) of the platform instructions last known EFFECTIVE on the
|
|
10
|
+
* main thread — i.e. present in the thread's model-visible initial context:
|
|
11
|
+
* recorded when the thread is started (thread/start bakes them in) or after
|
|
12
|
+
* a compaction rebuilt the context from the resumed configuration. NOT
|
|
13
|
+
* recorded on resume alone — resume updates the canonical configuration
|
|
14
|
+
* but leaves the model-visible context untouched (see
|
|
15
|
+
* instructions-refresh.ts). ONE exception weakens the "proven visible"
|
|
16
|
+
* reading: a legacy state file (no sha) ADOPTS the current prompt as
|
|
17
|
+
* baseline without a compaction — there the sha means "assumed baseline,
|
|
18
|
+
* converges at the next change or organic compaction", not verified
|
|
19
|
+
* delivery. Absent on state files from before this tracking existed.
|
|
20
|
+
* Dropped whenever the thread id changes (an effectiveness fact is
|
|
21
|
+
* per-thread).
|
|
22
|
+
*/
|
|
23
|
+
effectiveInstructionsSha?: string;
|
|
8
24
|
};
|
|
9
25
|
|
|
10
26
|
type Logger = { warn(message: string): void };
|
|
@@ -17,6 +33,7 @@ type Logger = { warn(message: string): void };
|
|
|
17
33
|
*/
|
|
18
34
|
export class CodexSessionManager {
|
|
19
35
|
private readonly threadIds = new Map<string, string>();
|
|
36
|
+
private readonly effectiveInstructionsShas = new Map<string, string>();
|
|
20
37
|
|
|
21
38
|
constructor(
|
|
22
39
|
readonly mainSessionKey: string,
|
|
@@ -35,9 +52,47 @@ export class CodexSessionManager {
|
|
|
35
52
|
}
|
|
36
53
|
|
|
37
54
|
recordThreadId(sessionKey: string, threadId: string) {
|
|
55
|
+
// An effective-instructions sha is a fact about one specific thread.
|
|
56
|
+
// Recording a DIFFERENT thread id under the same session invalidates it;
|
|
57
|
+
// re-recording the same id (the resume path) must preserve it, or every
|
|
58
|
+
// bridge restart would trigger a redundant refresh.
|
|
59
|
+
if (this.threadIds.get(sessionKey) !== threadId) {
|
|
60
|
+
this.effectiveInstructionsShas.delete(sessionKey);
|
|
61
|
+
}
|
|
38
62
|
this.threadIds.set(sessionKey, threadId);
|
|
39
63
|
if (sessionKey === this.mainSessionKey) {
|
|
40
|
-
this.persist(
|
|
64
|
+
this.persist();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Record a freshly-STARTED thread together with the sha of the
|
|
70
|
+
* instructions baked into it — one atomic state-file write. Two separate
|
|
71
|
+
* writes (thread id, then sha) would leave a crash window whose survivor
|
|
72
|
+
* looks exactly like a pre-tracking legacy file, and the reconcile path
|
|
73
|
+
* deliberately does NOT compact legacy state — a stale thread would then
|
|
74
|
+
* be recorded as carrying instructions it never saw.
|
|
75
|
+
*/
|
|
76
|
+
recordStartedThread(sessionKey: string, threadId: string, effectiveSha: string | undefined) {
|
|
77
|
+
this.threadIds.set(sessionKey, threadId);
|
|
78
|
+
if (effectiveSha) {
|
|
79
|
+
this.effectiveInstructionsShas.set(sessionKey, effectiveSha);
|
|
80
|
+
} else {
|
|
81
|
+
this.effectiveInstructionsShas.delete(sessionKey);
|
|
82
|
+
}
|
|
83
|
+
if (sessionKey === this.mainSessionKey) {
|
|
84
|
+
this.persist();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
getEffectiveInstructionsSha(sessionKey: string): string | undefined {
|
|
89
|
+
return this.effectiveInstructionsShas.get(sessionKey);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
recordEffectiveInstructionsSha(sessionKey: string, sha: string) {
|
|
93
|
+
this.effectiveInstructionsShas.set(sessionKey, sha);
|
|
94
|
+
if (sessionKey === this.mainSessionKey) {
|
|
95
|
+
this.persist();
|
|
41
96
|
}
|
|
42
97
|
}
|
|
43
98
|
|
|
@@ -50,11 +105,13 @@ export class CodexSessionManager {
|
|
|
50
105
|
|
|
51
106
|
cleanupFork(sessionKey: string) {
|
|
52
107
|
this.threadIds.delete(sessionKey);
|
|
108
|
+
this.effectiveInstructionsShas.delete(sessionKey);
|
|
53
109
|
}
|
|
54
110
|
|
|
55
111
|
/** Clear the main thread id when app-server rejects it (stale / deleted). */
|
|
56
112
|
clearMainThread() {
|
|
57
113
|
this.threadIds.delete(this.mainSessionKey);
|
|
114
|
+
this.effectiveInstructionsShas.delete(this.mainSessionKey);
|
|
58
115
|
try {
|
|
59
116
|
fs.rmSync(this.stateFilePath, { force: true });
|
|
60
117
|
} catch (error) {
|
|
@@ -74,6 +131,15 @@ export class CodexSessionManager {
|
|
|
74
131
|
parsed.threadId.trim()
|
|
75
132
|
) {
|
|
76
133
|
this.threadIds.set(this.mainSessionKey, parsed.threadId.trim());
|
|
134
|
+
if (
|
|
135
|
+
typeof parsed.effectiveInstructionsSha === 'string' &&
|
|
136
|
+
parsed.effectiveInstructionsSha.trim()
|
|
137
|
+
) {
|
|
138
|
+
this.effectiveInstructionsShas.set(
|
|
139
|
+
this.mainSessionKey,
|
|
140
|
+
parsed.effectiveInstructionsSha.trim(),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
77
143
|
}
|
|
78
144
|
} catch (error) {
|
|
79
145
|
if ((error as NodeJS.ErrnoException)?.code !== 'ENOENT') {
|
|
@@ -84,7 +150,9 @@ export class CodexSessionManager {
|
|
|
84
150
|
}
|
|
85
151
|
}
|
|
86
152
|
|
|
87
|
-
private persist(
|
|
153
|
+
private persist() {
|
|
154
|
+
const threadId = this.threadIds.get(this.mainSessionKey);
|
|
155
|
+
if (!threadId) return;
|
|
88
156
|
// Atomic write: truncate-in-place risks leaving a half-written / zero-byte
|
|
89
157
|
// state file if the process dies between `open(O_TRUNC)` and the final
|
|
90
158
|
// fsync. Since this file is the sole source of cross-restart `resume`
|
|
@@ -94,10 +162,10 @@ export class CodexSessionManager {
|
|
|
94
162
|
try {
|
|
95
163
|
fs.mkdirSync(path.dirname(this.stateFilePath), { recursive: true });
|
|
96
164
|
const tmpPath = `${this.stateFilePath}.tmp`;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
);
|
|
165
|
+
const state: PersistedThread = { runtimeKey: this.mainSessionKey, threadId };
|
|
166
|
+
const effectiveSha = this.effectiveInstructionsShas.get(this.mainSessionKey);
|
|
167
|
+
if (effectiveSha) state.effectiveInstructionsSha = effectiveSha;
|
|
168
|
+
fs.writeFileSync(tmpPath, JSON.stringify(state, null, 2));
|
|
101
169
|
fs.renameSync(tmpPath, this.stateFilePath);
|
|
102
170
|
} catch (error) {
|
|
103
171
|
this.logger?.warn(
|
package/src/turn-sink.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { RuntimeEvent } from '@parall/agent-core';
|
|
2
|
+
import { EventMapper } from './event-mapping.js';
|
|
3
|
+
|
|
4
|
+
export type TurnEventEnvelope =
|
|
5
|
+
| { kind: 'runtime'; event: RuntimeEvent }
|
|
6
|
+
| { kind: 'turn_end'; threadId?: string }
|
|
7
|
+
| { kind: 'error'; message: string };
|
|
8
|
+
|
|
9
|
+
/** Per-turn buffered sink backed by an unbounded promise queue. */
|
|
10
|
+
export class TurnSink {
|
|
11
|
+
readonly mapper = new EventMapper();
|
|
12
|
+
private readonly queue: TurnEventEnvelope[] = [];
|
|
13
|
+
private resolver: ((value: TurnEventEnvelope) => void) | null = null;
|
|
14
|
+
private closed = false;
|
|
15
|
+
|
|
16
|
+
push(envelope: TurnEventEnvelope) {
|
|
17
|
+
if (this.closed) return;
|
|
18
|
+
if (this.resolver) {
|
|
19
|
+
const r = this.resolver;
|
|
20
|
+
this.resolver = null;
|
|
21
|
+
r(envelope);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
this.queue.push(envelope);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
next(): Promise<TurnEventEnvelope> {
|
|
28
|
+
// Drain any queued envelopes first, even after close(). Otherwise a final
|
|
29
|
+
// error envelope enqueued right before close() (e.g. by
|
|
30
|
+
// handleSubprocessClose) is silently dropped because the consumer would
|
|
31
|
+
// see turn_end before it.
|
|
32
|
+
const pending = this.queue.shift();
|
|
33
|
+
if (pending) return Promise.resolve(pending);
|
|
34
|
+
if (this.closed) {
|
|
35
|
+
return Promise.resolve({ kind: 'turn_end' });
|
|
36
|
+
}
|
|
37
|
+
return new Promise((resolve) => {
|
|
38
|
+
this.resolver = resolve;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
close() {
|
|
43
|
+
this.closed = true;
|
|
44
|
+
const r = this.resolver;
|
|
45
|
+
this.resolver = null;
|
|
46
|
+
r?.({ kind: 'turn_end' });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** True once close() ran — the sink's terminal state (queued envelopes may
|
|
50
|
+
* still drain via next()). */
|
|
51
|
+
get isClosed(): boolean {
|
|
52
|
+
return this.closed;
|
|
53
|
+
}
|
|
54
|
+
}
|