Commit 9b5cdd8
authored
fix(daemon): move connected-MCP directive out of the cached system prompt (#5336)
* fix(daemon): move connected-MCP directive out of the cached system prompt
The '## External MCP servers — already authenticated' directive was
composed into daemonSystemPrompt, part of the cacheable system prefix
sent upstream. Its membership is driven by live OAuth Bearer validity
(isTokenExpired), so a token expiring/refreshing/reconnecting mid-
conversation changes the directive -> changes the system prompt bytes ->
invalidates the whole downstream prompt-cache prefix, including the
conversation-history cache AMR/Claude-on-Bedrock relies on. Production
telemetry (stable_prompt_cache_miss_reason='stable-prompt-changed')
attributes the bulk of resume-turn prefix drift to this directive, rising
with conversation length as more tokens cross expiry.
Move it from composeSystemPrompt into the per-turn client instruction
slice (alongside runContext/cwd hints): re-sent uncached every turn,
landing after the cached system+history prefix. The model still sees the
current MCP auth state each turn (more accurately -- live state, not the
seed snapshot), while the cacheable prefix stays byte-stable across
resumes. renderConnectedExternalMcpDirective is exported and its leading
separator dropped so it composes cleanly in the joined slice;
composeSystemPrompt no longer takes connectedExternalMcp.
* test(daemon): e2e red-spec for the connected-MCP directive moving to the per-turn slice
Drives a real two-turn codex session (native resume) with a connected
external MCP server (enabled config + live OAuth Bearer) through the
daemon HTTP boundary, capturing the exact prompt each turn reaches the
agent's stdin. Asserts the '## External MCP servers — already
authenticated' directive is present on turn 1 AND re-sent on the turn-2
clean resume — which only holds once the directive rides in the per-turn
instruction slice instead of the cached stable block. Verified red on
origin/main (turn-2 stdin lacks the directive because the cached block is
not re-sent on a clean resume) and green with the fix.
* test(daemon): keep the MCP-directive red-spec hermetic
The new test writes an authenticated github MCP server into the
process-wide OD_DATA_DIR (tests/setup.ts shares one data root for the
whole daemon Vitest run) and the shared afterEach never reset it, so the
connected-MCP state could leak into later test files and make them
suite-order dependent. Reset the MCP config (empty servers) and clear the
github token in afterEach — both idempotent no-ops for the other tests in
this file.
* test(daemon): let MCP teardown failures fail fast
Drop the unconditional .catch(() => {}) around the afterEach MCP reset.
writeMcpConfig always overwrites to an empty server list and clearToken
is a documented no-op when the entry is absent, so neither needs the
catch — and swallowing a real teardown failure would silently let MCP
state leak into later tests, defeating the isolation guarantee.1 parent 81b20dc commit 9b5cdd8
4 files changed
Lines changed: 149 additions & 64 deletions
File tree
- apps/daemon
- src
- prompts
- tests
- prompts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
520 | 520 | | |
521 | 521 | | |
522 | 522 | | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | 523 | | |
534 | 524 | | |
535 | 525 | | |
| |||
595 | 585 | | |
596 | 586 | | |
597 | 587 | | |
598 | | - | |
599 | 588 | | |
600 | 589 | | |
601 | 590 | | |
| |||
940 | 929 | | |
941 | 930 | | |
942 | 931 | | |
943 | | - | |
944 | | - | |
945 | | - | |
946 | 932 | | |
947 | 933 | | |
948 | 934 | | |
| |||
1072 | 1058 | | |
1073 | 1059 | | |
1074 | 1060 | | |
1075 | | - | |
| 1061 | + | |
1076 | 1062 | | |
1077 | 1063 | | |
1078 | 1064 | | |
| |||
1087 | 1073 | | |
1088 | 1074 | | |
1089 | 1075 | | |
| 1076 | + | |
1090 | 1077 | | |
1091 | | - | |
1092 | 1078 | | |
1093 | 1079 | | |
1094 | 1080 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
3373 | 3374 | | |
3374 | 3375 | | |
3375 | 3376 | | |
3376 | | - | |
3377 | 3377 | | |
3378 | 3378 | | |
3379 | 3379 | | |
| |||
3889 | 3889 | | |
3890 | 3890 | | |
3891 | 3891 | | |
3892 | | - | |
3893 | | - | |
3894 | | - | |
3895 | 3892 | | |
3896 | 3893 | | |
3897 | 3894 | | |
| |||
4385 | 4382 | | |
4386 | 4383 | | |
4387 | 4384 | | |
4388 | | - | |
4389 | 4385 | | |
4390 | 4386 | | |
4391 | 4387 | | |
| |||
4698 | 4694 | | |
4699 | 4695 | | |
4700 | 4696 | | |
| 4697 | + | |
| 4698 | + | |
| 4699 | + | |
| 4700 | + | |
| 4701 | + | |
| 4702 | + | |
| 4703 | + | |
4701 | 4704 | | |
4702 | | - | |
4703 | | - | |
| 4705 | + | |
| 4706 | + | |
4704 | 4707 | | |
4705 | 4708 | | |
4706 | 4709 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
62 | 77 | | |
63 | 78 | | |
64 | 79 | | |
| |||
224 | 239 | | |
225 | 240 | | |
226 | 241 | | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
227 | 316 | | |
228 | 317 | | |
229 | 318 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
| |||
415 | 419 | | |
416 | 420 | | |
417 | 421 | | |
418 | | - | |
419 | | - | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
420 | 429 | | |
421 | 430 | | |
422 | 431 | | |
423 | 432 | | |
424 | 433 | | |
425 | | - | |
426 | | - | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
427 | 441 | | |
428 | 442 | | |
| 443 | + | |
429 | 444 | | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
437 | 450 | | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
443 | 461 | | |
444 | 462 | | |
445 | | - | |
446 | | - | |
| 463 | + | |
| 464 | + | |
447 | 465 | | |
448 | 466 | | |
449 | | - | |
450 | | - | |
451 | | - | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
452 | 470 | | |
453 | 471 | | |
454 | | - | |
455 | | - | |
456 | | - | |
| 472 | + | |
| 473 | + | |
457 | 474 | | |
458 | 475 | | |
459 | 476 | | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
464 | | - | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
465 | 480 | | |
466 | 481 | | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
476 | | - | |
477 | | - | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
478 | 485 | | |
479 | 486 | | |
480 | 487 | | |
| |||
0 commit comments