Skip to content

fix(docs): widen native-table search window in markdown append (#592)#628

Open
sebsnyk wants to merge 1 commit into
openclaw:mainfrom
sebsnyk:fix/592-append-table-insertion
Open

fix(docs): widen native-table search window in markdown append (#592)#628
sebsnyk wants to merge 1 commit into
openclaw:mainfrom
sebsnyk:fix/592-append-table-insertion

Conversation

@sebsnyk
Copy link
Copy Markdown

@sebsnyk sebsnyk commented May 21, 2026

Summary

Fixes #592gog docs write --markdown --append silently dropped any markdown table in the appended content, failing with insert native table: table not found near index N. Body text appended fine; only the native-table insert step blew up.

Root cause

After issuing InsertTableRequest, TableInserter.getTableCellIndices walks Documents.Get to bind cell indices to the freshly-inserted Table element. It matched the table by requiring element.StartIndex in [tableStartIndex-2, tableStartIndex+2], but the Docs API's actual reported StartIndex drifts further than 2 code units in practice — the auto-newline behaviour documented for InsertTableRequest combines with our pre-inserted placeholder paragraph to push the table several positions past the requested Location.Index. Outside the window, the matcher returned no hit and surfaced the table not found error, so the table was silently dropped while the surrounding text appended successfully.

The docs create --file --markdown path is unaffected because Drive's native markdown importer converts tables end-to-end without ever calling InsertNativeTable — the bug is specific to the markdown append/replace path.

Fix

Replace the tight ±2 window with a new pickTableNear helper that scans the body content and picks the Table element whose StartIndex is closest to tableStartIndex while staying within a small backward tolerance. The closest-match policy uniquely identifies the freshly-inserted table even if the doc already contains other tables, and the backward tolerance absorbs minor index quirks. InsertTable can only shift existing content forward, so requiring near-or-after positioning is correct by construction.

Test plan

🤖 Generated with Claude Code

…law#592)

`gog docs write --markdown --append` silently dropped any markdown table in
the appended content, failing with `insert native table: table not found near
index N`. Body text outside the table appended fine; only the native-table
insert step blew up.

Root cause: after BatchUpdate-ing the placeholder text and calling
InsertTableRequest, the inserter walked Documents.Get to bind cell indices to
the freshly-inserted Table element. It matched the table by requiring
`StartIndex in [tableStartIndex-2, tableStartIndex+2]`, but the Docs API's
actual reported StartIndex drifts further than 2 code units in practice — the
auto-newline behaviour documented for InsertTableRequest combined with our
pre-inserted placeholder paragraph pushes the table several positions past
the requested Location.Index. Outside the window, the matcher returned no
hit and surfaced the "table not found" error.

Fix: replace the tight +/-2 window with pickTableNear, which scans the body
content and picks the Table element whose StartIndex is closest to
tableStartIndex while staying within a small backward tolerance. The
closest-match policy uniquely identifies the freshly-inserted table even if
the doc already contains tables earlier in the body, and the backward
tolerance absorbs any minor index quirks.

The gog docs create --file --markdown path is unaffected because Drive's
native markdown importer converts tables end-to-end without ever calling
InsertNativeTable; the bug was specific to the markdown append/replace path.

Tests:
- internal/cmd/docs_append_table_test.go drives insertDocsMarkdownAt against
  an httptest-backed Docs mock and reproduces the issue: with a configurable
  drift of 5 between Location.Index and the table's reported StartIndex, the
  pre-fix code returns 'table not found near index 9'; with the fix the
  table inserts cleanly. Also covers a mixed prose+table append.
- TestPickTableNear_PrefersClosestForwardMatch documents the matcher
  semantics: exact match, +1/+5/+25 drift, earlier-in-doc tables ignored when
  outside backward tolerance, and absent-table -> nil.
- Existing openclaw#607/openclaw#608/openclaw#609 table tests continue to pass.

Proof:
- go test ./internal/cmd -run 'TestInsertDocsMarkdownAt|TestPickTableNear|TestNextTableInsertOffset|TestBuildTableCellRequests|TestParseMarkdown|TestMarkdownToDocsRequests' -count=1
- go test ./...
- go vet ./...
- gofmt -l internal/cmd/docs_table_inserter.go internal/cmd/docs_append_table_test.go (no output)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs write --markdown --append fails to insert tables: 'insert native table: table not found near index N'

1 participant