Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): fix three repeat-offender e2e flake mechanisms (PRD-8167) (#3798)

Andrew Mikofalvyddd524f55389
diff --git a/.changeset/doc-panel-chord-sticks-closed.md b/.changeset/doc-panel-chord-sticks-closed.md
new file mode 100644
index 000000000..0683c2928
--- /dev/null
+++ b/.changeset/doc-panel-chord-sticks-closed.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Pressing the document-panel shortcut (⌥⌘B) twice in quick succession no longer leaves the panel stuck shut. The second press was meant to reopen the panel the first one closed, but if it landed quickly enough it repeated the close instead — doing nothing visible, and leaving the toolbar button reporting a state the app disagreed with. The panel also recorded "closed" as your preference on the way out, so the wrong state could follow you into the next session. The same guard now covers the sidebar shortcut (⌥⌘S), which had a narrower version of the same gap.
diff --git a/packages/app/src/components/EditorArea.dom.test.tsx b/packages/app/src/components/EditorArea.dom.test.tsx
index aaca34db6..92554e637 100644
--- a/packages/app/src/components/EditorArea.dom.test.tsx
+++ b/packages/app/src/components/EditorArea.dom.test.tsx
@@ -760,6 +760,44 @@ describe('EditorArea right-rail layout assert on column mount/unmount', () => {
expect(corrected?.['editor-main']).toBeCloseTo(100 - pctOf(320), 3);
});
+ test('a second toggle re-opens the doc panel the first toggle collapsed', () => {
+ // `togglePanel` is re-bound on every render, and the ⌥⌘B keydown listener
+ // that calls it only re-installs when the passive effect phase flushes. A
+ // second invocation landing before that flush therefore runs the PREVIOUS
+ // closure — one that still sees the pre-collapse `isCollapsed`. Branching
+ // on that render-bound value made the second press repeat the collapse it
+ // had just done: a silent no-op leaving the panel shut, `aria-expanded`
+ // pinned at "false", and a spurious 'collapsed' pin persisted.
+ //
+ // This harness holds that stale condition open for the whole test rather
+ // than for one frame: the mocked group handle records `setLayout` without
+ // updating `groupLayout`, and nothing fires `onResize`, so `setIsCollapsed`
+ // never runs. That makes it the right rung for this invariant: the
+ // equivalent browser test has to land its second press inside a single
+ // frame, so it caught this in only 1 of 6 CI runs. The invariant is that
+ // `togglePanel` must not depend on React state having caught up, so it
+ // reads the ref `assertRightRailLayout` writes synchronously.
+ setViewportWidth(1400);
+ docCtx = DOC_LIVE_CTX;
+ render(<EditorArea {...baseProps} />);
+ // 1400px is above the collapse threshold, so the panel starts open; mirror
+ // the live layout the library would be holding for it.
+ groupLayout = {
+ 'editor-main': 75,
+ 'doc-panel': 25,
+ 'terminal-column': 0,
+ 'agents-column': 0,
+ };
+ groupSetLayoutCalls = [];
+
+ act(() => emitLocalMenuAction('toggle-doc-panel'));
+ expect(groupSetLayoutCalls.at(-1)?.['doc-panel']).toBe(0);
+
+ // The round-trip. Against the render-bound read this stayed 0.
+ act(() => emitLocalMenuAction('toggle-doc-panel'));
+ expect(groupSetLayoutCalls.at(-1)?.['doc-panel']).toBeCloseTo(pctOf(320), 3);
+ });
+
test('hiding the agents panel re-asserts the collapsed doc panel over the stale panel-set restore', async () => {
// Below 1280px the doc panel starts collapsed (no pin), so the intended
// post-hide state is "collapsed" even though the cached two-panel layout
diff --git a/packages/app/src/components/EditorArea.tsx b/packages/app/src/components/EditorArea.tsx
index 82904ac5e..fc78f31bd 100644
--- a/packages/app/src/components/EditorArea.tsx
+++ b/packages/app/src/components/EditorArea.tsx
@@ -1138,10 +1138,18 @@ function EditorAreaInner({
// threshold and immediately invokes the toggle before React commits the
// new partition.
const partition = rightPartitionRef.current;
+ // Same reason `partition` is read from a ref just above, one hop worse: a
+ // second chord arriving before the effect re-subscribes runs the previous
+ // closure, and branching on render-bound state made it repeat the collapse
+ // it had just done — a no-op leaving the panel shut, `aria-expanded` stuck
+ // at "false", and a spurious 'collapsed' pin persisted.
+ // `assertRightRailLayout` writes this ref synchronously so the live value
+ // survives that gap. Pinned in EditorArea.dom.test.tsx.
+ const collapsed = isCollapsedRef.current;
// The permanent rail columns remain adjacent at zero width while hidden.
// A per-panel expand/collapse would exchange with that zero-width neighbor
// and no-op, so every transition must route through the editor residual.
- if (isCollapsed) {
+ if (collapsed) {
applyToggle('right', partition, 'open');
assertRightRailLayout(false);
} else {
diff --git a/packages/app/src/components/ui/sidebar.tsx b/packages/app/src/components/ui/sidebar.tsx
index 809f63046..eed55c7f4 100644
--- a/packages/app/src/components/ui/sidebar.tsx
+++ b/packages/app/src/components/ui/sidebar.tsx
@@ -133,7 +133,19 @@ function SidebarProvider({
const open = openProp ?? _open;
const setOpen: OpenHandler = (value) => {
- const openState = typeof value === 'function' ? value(open) : value;
+ // Resolve the updater against the live ref, not the render-bound `open`.
+ // This is typed `Dispatch<SetStateAction<boolean>>` and handed to consumers
+ // through context, so the functional form owes them React's own contract:
+ // the updater receives the LATEST value. Resolving against the captured
+ // `open` breaks that for any caller holding a stale closure, including the
+ // ⌥⌘S listener below, which only re-subscribes once the passive effect
+ // phase flushes. The window is far narrower than the doc-panel toggle's
+ // equivalent, because this writes React state directly instead of
+ // round-tripping through the panel library's `onResize` — narrow enough
+ // that a jsdom test cannot observe it at all, so there is no pin here. The
+ // ref is safe to read regardless: its layout effect runs before any passive
+ // effect, so it is never staler than `open`.
+ const openState = typeof value === 'function' ? value(openRef.current) : value;
if (setOpenProp) {
setOpenProp(openState);
} else {
diff --git a/packages/app/tests/stress/crdt-stress.e2e.ts b/packages/app/tests/stress/crdt-stress.e2e.ts
index 81244fb62..3649fc2a9 100644
--- a/packages/app/tests/stress/crdt-stress.e2e.ts
+++ b/packages/app/tests/stress/crdt-stress.e2e.ts
@@ -44,6 +44,30 @@ test('S6: multi-turn stress — large content + user edits', async ({ page, api,
const markers = ['USER-E2E-MARK-1', 'USER-E2E-MARK-2', 'USER-E2E-MARK-3'];
for (const marker of markers) {
+ // Y.Text length BEFORE this turn's write. The propagation wait below is
+ // RELATIVE to it because the writes append: each turn adds roughly another
+ // fixture's worth of content, so an absolute `>= FIXTURE.length - 200`
+ // threshold is already satisfied by the PREVIOUS turn's content and
+ // returned without waiting at all on turns 2 and 3. Those turns never
+ // verified their own append, and the test went on to type its marker into a
+ // document with a full fixture still in flight. Waiting per-turn removes
+ // that overlap. It does NOT on its own make the marker wait below reliable
+ // — that still times out under load and is tracked separately — so read
+ // this as closing a guard that was doing nothing, not as the flake's cure.
+ // `grewFrom` in the turn log below records the baseline, so a run shows
+ // directly whether each turn's guard had anything to wait for.
+ // No `?? 0` fallback here: defaulting a missing provider to 0 would put the
+ // threshold back at exactly the absolute `FIXTURE.length - 200` this guard
+ // exists to replace, silently restoring the vacuous behaviour on a degraded
+ // path. Fail loudly instead — the provider is already awaited above.
+ const lengthBeforeWrite = await page.evaluate(() => {
+ const len = window.__activeProvider?.document?.getText('source')?.toString()?.length;
+ if (typeof len !== 'number') {
+ throw new Error('no live provider Y.Text before the agent write');
+ }
+ return len;
+ });
+
// Inject large content via agent API. Default `position: append` (omitted)
// so each turn stacks onto the previous — testing coexistence of agent
// writes + accumulated user typing across turns.
@@ -54,11 +78,11 @@ test('S6: multi-turn stress — large content + user edits', async ({ page, api,
});
expect(writeRes.ok).toBe(true);
- // Wait for content to propagate to Y.Text
+ // Wait for THIS turn's append to propagate to Y.Text.
await page.waitForFunction(
(expected: number) =>
- window.__activeProvider?.document?.getText('source')?.toString()?.length >= expected,
- FIXTURE.length - 200, // tolerance for whitespace normalization
+ (window.__activeProvider?.document?.getText('source')?.toString()?.length ?? 0) >= expected,
+ lengthBeforeWrite + FIXTURE.length - 200, // tolerance for whitespace normalization
{ timeout: 30_000 },
);
@@ -71,10 +95,17 @@ test('S6: multi-turn stress — large content + user edits', async ({ page, api,
// above, so it carries the same 30s budget. The marker round-trips client
// keystroke → XmlFragment → server Observer A → Y.Text; across the 3 turns the
// doc accumulates to ~3× the fixture, so by the later turns Observer A re-derives
- // Y.Text from a much larger fragment and that round-trip can exceed 10s under
- // workers=4 contention. The marker always lands (it is never dropped — the final
- // assertion below confirms all three survive), just slowly under accumulating
- // load, so a 10s budget cut a still-in-flight sync off mid-turn.
+ // Y.Text from a much larger fragment and that round-trip grows with it. The
+ // marker always lands (it is never dropped — the final assertion below confirms
+ // all three survive), just slowly under accumulating load.
+ //
+ // This budget was once raised from 10s to 30s on the theory that it was merely
+ // ungenerous. That turned out not to be the whole story, so do not read the
+ // current number as a solved problem: this wait STILL times out under load, and
+ // it is the open residual the comment at the top of the loop refers to. The
+ // suspected cost is Observer A re-deriving Y.Text per keystroke against a
+ // fragment that keeps growing, which is a server-side cost no client-side budget
+ // fixes. Measure that re-derive before considering another bump.
await page.waitForFunction(
(m: string) => window.__activeProvider?.document?.getText('source')?.toString()?.includes(m),
marker,
@@ -92,7 +123,8 @@ test('S6: multi-turn stress — large content + user edits', async ({ page, api,
};
});
console.log(
- `[Layer C] Turn complete: ytext=${turnState.ytextLen}, fragment=${turnState.fragChildren}`,
+ `[Layer C] Turn complete: ytext=${turnState.ytextLen}, fragment=${turnState.fragChildren}, ` +
+ `grewFrom=${lengthBeforeWrite}`,
);
}
diff --git a/packages/app/tests/stress/show-ok-folders.e2e.ts b/packages/app/tests/stress/show-ok-folders.e2e.ts
index 1c529be14..9e1bf5f53 100644
--- a/packages/app/tests/stress/show-ok-folders.e2e.ts
+++ b/packages/app/tests/stress/show-ok-folders.e2e.ts
@@ -291,5 +291,17 @@ test('a legacy __template__ bookmark redirects to the content doc and opens it',
await page.evaluate((name) => {
window.location.hash = `#/__template__/${name}`;
}, templateName);
- await expect(page.getByText(bodyMarker)).toBeVisible({ timeout: 15_000 });
+ // Anchor on the rendered heading, not on bare text. The outline panel lists
+ // every heading in the open doc as a button whose label and `title` are the
+ // heading text verbatim, so this marker necessarily exists on two surfaces
+ // once the outline has derived from the body — and an unscoped getByText
+ // matches both and trips Playwright strict mode. Both surfaces come from the
+ // same heading, so this is a race over which one renders first, not a slow
+ // assertion; raising the timeout would only give the outline entry more time
+ // to appear. (The file tree is not the second match: it labels its rows by
+ // filename, which is why `fileRow` above matches on `.md` names.) The outline
+ // entry is a button, so scoping to the heading role stays unique, and the
+ // template body is written as `# ${bodyMarker}` above — making the heading
+ // exactly the "its body renders" signal this test means to assert.
+ await expect(page.getByRole('heading', { name: bodyMarker })).toBeVisible({ timeout: 15_000 });
});