system-transform-ordering.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { describe, expect, test } from 'bun:test';
  2. import { collapseSystemInPlace } from './utils/system-collapse';
  3. /**
  4. * Regression tests for orchestrator prompt ordering in the
  5. * experimental.chat.system.transform hook (PR #782).
  6. *
  7. * The hook places the orchestrator prompt AFTER AGENTS.md so the user's
  8. * behavioral rules retain their intended priority. These tests exercise
  9. * the core ordering logic that the hook performs.
  10. */
  11. const ORCHESTRATOR_PROMPT = 'You are the orchestrator agent.';
  12. function applySystemTransform(
  13. system: string[],
  14. agentName: string | undefined,
  15. orchestratorPrompt = ORCHESTRATOR_PROMPT,
  16. ): void {
  17. if (agentName === 'orchestrator') {
  18. const alreadyInjected = system.some(
  19. (s) =>
  20. typeof s === 'string' &&
  21. s.includes('<Role>') &&
  22. s.includes('orchestrator'),
  23. );
  24. if (!alreadyInjected) {
  25. system[0] = system[0]
  26. ? `${system[0]}\n\n${orchestratorPrompt}`
  27. : orchestratorPrompt;
  28. }
  29. }
  30. collapseSystemInPlace(system);
  31. }
  32. describe('system.transform hook ordering (PR #782)', () => {
  33. test('orchestrator prompt appears AFTER AGENTS.md content', () => {
  34. const system = ['AGENTS.md: always use TypeScript'];
  35. applySystemTransform(system, 'orchestrator');
  36. expect(system).toHaveLength(1);
  37. expect(system[0]).toBe(
  38. 'AGENTS.md: always use TypeScript\n\n' + ORCHESTRATOR_PROMPT,
  39. );
  40. });
  41. test('empty system array gets orchestrator prompt only', () => {
  42. const system: string[] = [];
  43. applySystemTransform(system, 'orchestrator');
  44. expect(system).toHaveLength(1);
  45. expect(system[0]).toBe(ORCHESTRATOR_PROMPT);
  46. });
  47. test('non-orchestrator agent is not modified', () => {
  48. const system = ['AGENTS.md: always use TypeScript'];
  49. applySystemTransform(system, 'explorer');
  50. expect(system).toHaveLength(1);
  51. expect(system[0]).toBe('AGENTS.md: always use TypeScript');
  52. });
  53. test('already-injected orchestrator prompt is not duplicated', () => {
  54. const system = ['<Role> orchestrator </Role>'];
  55. applySystemTransform(system, 'orchestrator');
  56. expect(system).toHaveLength(1);
  57. // No double-injection
  58. expect(system[0]).toBe('<Role> orchestrator </Role>');
  59. });
  60. });