|
| 1 | +/** |
| 2 | + * Regression tests for WorkflowRunner.scrubForChannel — the function that |
| 3 | + * strips PTY/TUI chrome from interactive-agent step output before it gets |
| 4 | + * surfaced in workflow logs and channel messages. |
| 5 | + * |
| 6 | + * The patterns covered here are taken from a real captured run of a |
| 7 | + * multi-turn workflow against Claude Code's PTY: when its TUI footer |
| 8 | + * overwrites itself faster than the PTY flushes whitespace, lines like |
| 9 | + * `bypasspermissionson`, `--INSERT--⏵⏵`, and `Opus 4.7 (1M context) ctx:5% |
| 10 | + * $1.45` end up in the captured stream. Before these regex additions, the |
| 11 | + * step "Output:" block was unreadable on interactive-agent steps. |
| 12 | + */ |
| 13 | +import { describe, it, expect } from 'vitest'; |
| 14 | + |
| 15 | +import { WorkflowRunner } from '../runner.js'; |
| 16 | + |
| 17 | +// scrubForChannel is `private static` — the cast is the minimal-invasive way |
| 18 | +// to exercise it from a test without exporting an internal-only helper. |
| 19 | +const scrub = (text: string): string => |
| 20 | + (WorkflowRunner as unknown as { scrubForChannel(t: string): string }).scrubForChannel(text); |
| 21 | + |
| 22 | +describe('WorkflowRunner.scrubForChannel — PTY chrome stripping', () => { |
| 23 | + it('strips the Claude Code bottom status bar (model + ctx% + cost)', () => { |
| 24 | + const input = [ |
| 25 | + 'real content line', |
| 26 | + 'workflows git:(main) Opus 4.7 (1M context) ctx:5% $1.45', |
| 27 | + 'Opus4.7(1Mcontext) ctx:6% $1.54', |
| 28 | + 'another real line', |
| 29 | + ].join('\n'); |
| 30 | + const out = scrub(input); |
| 31 | + expect(out).toContain('real content line'); |
| 32 | + expect(out).toContain('another real line'); |
| 33 | + expect(out).not.toMatch(/ctx\s*:\s*\d+%/); |
| 34 | + expect(out).not.toMatch(/\$\d+\.\d+/); |
| 35 | + }); |
| 36 | + |
| 37 | + it('strips vim-style mode indicators emitted by the input bar', () => { |
| 38 | + const input = [ |
| 39 | + 'pre-mode line', |
| 40 | + '--INSERT--', |
| 41 | + '--INSERT--⏵⏵bypasspermissionson (shift+tabtocycle)', |
| 42 | + 'post-mode line', |
| 43 | + ].join('\n'); |
| 44 | + const out = scrub(input); |
| 45 | + expect(out).toContain('pre-mode line'); |
| 46 | + expect(out).toContain('post-mode line'); |
| 47 | + expect(out).not.toMatch(/--INSERT--/); |
| 48 | + }); |
| 49 | + |
| 50 | + it('strips no-whitespace TUI hint variants (bypasspermissionson, pasteagaintoexpand)', () => { |
| 51 | + const input = ['before', 'bypasspermissionson', 'pasteagaintoexpand', 'shifttabto cycle', 'after'].join( |
| 52 | + '\n' |
| 53 | + ); |
| 54 | + const out = scrub(input); |
| 55 | + expect(out).toContain('before'); |
| 56 | + expect(out).toContain('after'); |
| 57 | + expect(out).not.toMatch(/bypasspermissionson/); |
| 58 | + expect(out).not.toMatch(/pasteagaintoexpand/); |
| 59 | + }); |
| 60 | + |
| 61 | + it('strips thinking-status fragments without ellipsis anchors', () => { |
| 62 | + const input = [ |
| 63 | + 'meaningful: round 3 codex-player guess=19 feedback=correct', |
| 64 | + 'thinking with high effort', |
| 65 | + '↓ 13 tokens · thinking with high effort', |
| 66 | + 'Crunched for 32s', |
| 67 | + 'Sautéed for 4s', |
| 68 | + 'Gitifying…55', |
| 69 | + ].join('\n'); |
| 70 | + const out = scrub(input); |
| 71 | + expect(out).toContain('feedback=correct'); |
| 72 | + expect(out).not.toMatch(/thinking with high effort/); |
| 73 | + expect(out).not.toMatch(/Crunched for/); |
| 74 | + expect(out).not.toMatch(/Gitifying/); |
| 75 | + }); |
| 76 | + |
| 77 | + it('strips malformed overwritten q0/qW0 PTY frame runs', () => { |
| 78 | + const input = [ |
| 79 | + 'first useful line', |
| 80 | + 'qW0 | q0 / ql0 _ qqm ~ lqq = qW0 | q0 / ql0 _ qqm', |
| 81 | + 'summary: kept qW0 | q0 / ql0 _ qqm ~ lqq = qW0 | q0 done', |
| 82 | + 'last useful line', |
| 83 | + ].join('\n'); |
| 84 | + const out = scrub(input); |
| 85 | + expect(out).toContain('first useful line'); |
| 86 | + expect(out).toContain('last useful line'); |
| 87 | + expect(out).toMatch(/summary: kept\s+done/); |
| 88 | + expect(out).not.toMatch(/qW0|ql0|qqm|lqq/); |
| 89 | + }); |
| 90 | + |
| 91 | + it('redacts secrets in the runner public preview path', () => { |
| 92 | + const out = scrub('deploy succeeded\napi_key=sk-abcdefghijklmnopqrstuvwxyz123456\n'); |
| 93 | + expect(out).toContain('deploy succeeded'); |
| 94 | + expect(out).toContain('[REDACTED]'); |
| 95 | + expect(out).not.toContain('sk-abcdefghijklmnopqrstuvwxyz123456'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('preserves real content and OWNER_DECISION signals', () => { |
| 99 | + const input = [ |
| 100 | + 'Read 1 file, calling relaycast 2 times', |
| 101 | + 'Transcript verification reports TRANSCRIPT_OK with all 6 lines well-formed.', |
| 102 | + 'OWNER_DECISION: COMPLETE', |
| 103 | + 'REASON: All 6 turns executed, history.log has 6 lines.', |
| 104 | + 'STEP_COMPLETE: repair-transcript', |
| 105 | + ].join('\n'); |
| 106 | + const out = scrub(input); |
| 107 | + expect(out).toContain('TRANSCRIPT_OK'); |
| 108 | + expect(out).toContain('OWNER_DECISION: COMPLETE'); |
| 109 | + expect(out).toContain('STEP_COMPLETE: repair-transcript'); |
| 110 | + expect(out).toContain('All 6 turns executed'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('does not strip lines that merely mention model names in prose', () => { |
| 114 | + // Guard against the new claudeFooterRe (which looks for `Opus|Sonnet|Haiku <num> |
| 115 | + // (...context...) ctx:N%`) being too eager and removing prose that |
| 116 | + // mentions a model name. |
| 117 | + const input = [ |
| 118 | + 'Compared output from Opus 4.7 against Sonnet 4.6 — both passed.', |
| 119 | + 'We chose Haiku 4.5 for its latency profile.', |
| 120 | + ].join('\n'); |
| 121 | + const out = scrub(input); |
| 122 | + expect(out).toContain('Opus 4.7 against Sonnet 4.6'); |
| 123 | + expect(out).toContain('Haiku 4.5 for its latency profile'); |
| 124 | + }); |
| 125 | +}); |
0 commit comments