|
6 | 6 | * lifecycle are already covered by `test/history-shadow.test.ts` and are |
7 | 7 | * intentionally not duplicated here. |
8 | 8 | * |
9 | | - * Current-bug note: the section-boundary regex in `appendToHistory` |
10 | | - * (`packages/squad-sdk/src/agents/history-shadow.ts`) is built as |
11 | | - * `` (?=^##\s|\Z) `` and intends `\Z` to mean "end of string". JavaScript has |
12 | | - * no `\Z` end-of-string escape; it is parsed as the literal character "Z". |
13 | | - * For every section except the last one, a following `## ` header still |
14 | | - * lets the lookahead succeed, so those appends work, but the same |
15 | | - * `trimEnd()`-before-splice step also collapses the blank line that used to |
16 | | - * separate sections. For the terminal section (`References`, which has no |
17 | | - * following header and no literal "Z" in its body) the lookahead can never |
18 | | - * succeed, so the existing header is never matched and a *second* |
19 | | - * `## References` header is appended at the tail of the file instead of the |
20 | | - * entry being inserted inline. Both effects are characterized below exactly |
21 | | - * as they behave today; neither assertion below endorses the behavior as |
22 | | - * desired. See the tracked follow-up issue for the production fix. |
| 9 | + * Regression note: the section-boundary regex in `appendToHistory` |
| 10 | + * (`packages/squad-sdk/src/agents/history-shadow.ts`) previously ended in |
| 11 | + * `\Z`, which JavaScript parses as the literal character "Z", so the terminal |
| 12 | + * `References` section was never matched (a duplicate header was appended) and |
| 13 | + * non-terminal appends dropped the blank line before the next header. The |
| 14 | + * regex now uses `(?![\s\S])` for end-of-input; both behaviors are pinned |
| 15 | + * below in their fixed form. |
23 | 16 | * |
24 | 17 | * All I/O is contained inside a hermetic temp root; repository `.squad/**` |
25 | 18 | * is never touched. |
@@ -47,18 +40,17 @@ const CANONICAL_SECTIONS: HistorySection[] = [ |
47 | 40 | 'References', |
48 | 41 | ]; |
49 | 42 |
|
50 | | -// The terminal section is characterized separately below: current |
51 | | -// production behavior duplicates its header instead of appending inline |
52 | | -// (see file-level comment). The remaining sections are each followed by |
53 | | -// another section header, so their append path succeeds. |
| 43 | +// The terminal section is characterized separately below because it has no |
| 44 | +// following header. The remaining sections are each followed by another |
| 45 | +// section header. |
54 | 46 | const NON_TERMINAL_SECTIONS = CANONICAL_SECTIONS.slice(0, -1); |
55 | 47 | const TERMINAL_SECTION = CANONICAL_SECTIONS[CANONICAL_SECTIONS.length - 1]; |
56 | 48 |
|
57 | 49 | const TIMESTAMP_LINE = /^### (\d{4}-\d{2}-\d{2})$/m; |
58 | 50 |
|
59 | 51 | describe('history-shadow characterization (H1)', () => { |
60 | 52 | it.each(NON_TERMINAL_SECTIONS)( |
61 | | - 'appends a block to the %s section, immediately abutting the next header', |
| 53 | + 'appends a block to the %s section, preserving a blank line before the next header', |
62 | 54 | async (section) => { |
63 | 55 | await withHermeticRoot(async (root) => { |
64 | 56 | const storage = makeWriteGuardedStorage(new FSStorageProvider(), root); |
@@ -89,65 +81,37 @@ describe('history-shadow characterization (H1)', () => { |
89 | 81 | expect(timestampMatch, 'timestamp header missing').not.toBeNull(); |
90 | 82 | const timestamp = timestampMatch![1]; |
91 | 83 |
|
92 | | - // Current behavior: appendToHistory trims trailing whitespace off |
93 | | - // the prior section content before splicing in the new entry. That |
94 | | - // collapses the blank line that originally separated sections, so |
95 | | - // the entry's own trailing newline becomes the single newline |
96 | | - // immediately preceding the next `## ` header rather than a |
97 | | - // preserved blank line plus a separate separator. The section body, |
98 | | - // as sliced up to that boundary, therefore ends with the entry text |
99 | | - // minus its final newline (that newline is the boundary itself). |
100 | | - const entryWithoutTrailingNewline = `\n### ${timestamp}\n\n${content}`; |
101 | | - expect(sectionBody.endsWith(entryWithoutTrailingNewline)).toBe(true); |
| 84 | + // appendToHistory trims the prior section content, splices in the |
| 85 | + // entry, and keeps a blank line before the next `## ` header. The |
| 86 | + // body sliced up to that header's "\n## " therefore ends with the |
| 87 | + // entry plus its own trailing newline (the blank line is the |
| 88 | + // remaining newline that is part of the boundary match). |
| 89 | + const entryWithTrailingNewline = `\n### ${timestamp}\n\n${content}\n`; |
| 90 | + expect(sectionBody.endsWith(entryWithTrailingNewline)).toBe(true); |
102 | 91 | }); |
103 | 92 | }, |
104 | 93 | ); |
105 | 94 |
|
106 | | - it( |
107 | | - `current bug: appending to the terminal ${TERMINAL_SECTION} section duplicates its header ` + |
108 | | - 'instead of inserting inline', |
109 | | - async () => { |
110 | | - await withHermeticRoot(async (root) => { |
111 | | - const storage = makeWriteGuardedStorage(new FSStorageProvider(), root); |
112 | | - const agent = 'char-agent-terminal'; |
113 | | - const content = `sample body for ${TERMINAL_SECTION} section with punctuation: !? and unicode \u2603`; |
| 95 | + it(`appends to the terminal ${TERMINAL_SECTION} section inline without duplicating its header`, async () => { |
| 96 | + await withHermeticRoot(async (root) => { |
| 97 | + const storage = makeWriteGuardedStorage(new FSStorageProvider(), root); |
| 98 | + const agent = 'char-agent-terminal'; |
| 99 | + const content = `sample body for ${TERMINAL_SECTION} section with punctuation: !? and unicode \u2603`; |
114 | 100 |
|
115 | | - const shadowPath = await createHistoryShadow(root, agent, undefined, storage); |
116 | | - const before = await fs.readFile(shadowPath, 'utf8'); |
117 | | - await appendToHistory(root, agent, TERMINAL_SECTION, content, storage); |
118 | | - const after = await fs.readFile(shadowPath, 'utf8'); |
119 | | - |
120 | | - const sectionHeader = `## ${TERMINAL_SECTION}`; |
121 | | - |
122 | | - // See file-level comment: the `\Z` typo means the section-boundary |
123 | | - // regex never matches for the terminal section, so appendToHistory |
124 | | - // falls back to its "section not found" branch and appends a brand |
125 | | - // new header at the tail instead of writing into the existing one. |
126 | | - const occurrences = after.split(sectionHeader).length - 1; |
127 | | - expect(occurrences, 'current bug duplicates the header').toBe(2); |
128 | | - |
129 | | - const firstIndex = after.indexOf(sectionHeader); |
130 | | - const secondIndex = after.indexOf(sectionHeader, firstIndex + sectionHeader.length); |
131 | | - expect(secondIndex).toBeGreaterThan(firstIndex); |
132 | | - |
133 | | - // The original section's content is left untouched by the append |
134 | | - // (aside from trailing-whitespace normalization performed by the |
135 | | - // "section not found" branch's own trimEnd(), which happens before |
136 | | - // the duplicate header is spliced in). |
137 | | - const originalSectionSlice = before.slice(before.indexOf(sectionHeader)); |
138 | | - const untouchedSlice = after.slice(firstIndex, secondIndex); |
139 | | - expect(untouchedSlice.trimEnd()).toBe(originalSectionSlice.trimEnd()); |
140 | | - |
141 | | - // The duplicated (second) header starts a fresh section containing |
142 | | - // only the new entry, and is the exact tail of the file. |
143 | | - const timestampMatch = after.match(TIMESTAMP_LINE); |
144 | | - expect(timestampMatch, 'timestamp header missing').not.toBeNull(); |
145 | | - const timestamp = timestampMatch![1]; |
146 | | - const expectedTail = `${sectionHeader}\n### ${timestamp}\n\n${content}\n`; |
147 | | - expect(after.slice(secondIndex)).toBe(expectedTail); |
148 | | - }); |
149 | | - }, |
150 | | - ); |
| 101 | + const shadowPath = await createHistoryShadow(root, agent, undefined, storage); |
| 102 | + const before = await fs.readFile(shadowPath, 'utf8'); |
| 103 | + await appendToHistory(root, agent, TERMINAL_SECTION, content, storage); |
| 104 | + const after = await fs.readFile(shadowPath, 'utf8'); |
| 105 | + |
| 106 | + const sectionHeader = `## ${TERMINAL_SECTION}`; |
| 107 | + expect(after.split(sectionHeader).length - 1, 'header should appear once').toBe(1); |
| 108 | + |
| 109 | + const timestampMatch = after.match(TIMESTAMP_LINE); |
| 110 | + expect(timestampMatch, 'timestamp header missing').not.toBeNull(); |
| 111 | + const timestamp = timestampMatch![1]; |
| 112 | + expect(after).toBe(`${before.trimEnd()}\n### ${timestamp}\n\n${content}\n`); |
| 113 | + }); |
| 114 | + }); |
151 | 115 |
|
152 | 116 | it('appends an unknown section at the end when no matching header exists', async () => { |
153 | 117 | await withHermeticRoot(async (root) => { |
|
0 commit comments