fix(docs): widen native-table search window in markdown append (#592)#628
Open
sebsnyk wants to merge 1 commit into
Open
fix(docs): widen native-table search window in markdown append (#592)#628sebsnyk wants to merge 1 commit into
sebsnyk wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #592 —
gog docs write --markdown --appendsilently dropped any markdown table in the appended content, failing withinsert 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.getTableCellIndiceswalksDocuments.Getto bind cell indices to the freshly-inserted Table element. It matched the table by requiringelement.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 forInsertTableRequestcombines with our pre-inserted placeholder paragraph to push the table several positions past the requestedLocation.Index. Outside the window, the matcher returned no hit and surfaced thetable not founderror, so the table was silently dropped while the surrounding text appended successfully.The
docs create --file --markdownpath is unaffected because Drive's native markdown importer converts tables end-to-end without ever callingInsertNativeTable— the bug is specific to the markdown append/replace path.Fix
Replace the tight ±2 window with a new
pickTableNearhelper that scans the body content and picks the Table element whoseStartIndexis closest totableStartIndexwhile 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.InsertTablecan only shift existing content forward, so requiring near-or-after positioning is correct by construction.Test plan
go test ./internal/cmd -run 'TestInsertDocsMarkdownAt|TestPickTableNear|TestNextTableInsertOffset|TestBuildTableCellRequests|TestParseMarkdown|TestMarkdownToDocsRequests' -count=1go test ./...— greengo vet ./...— cleangofmt -lon touched files — no diffinternal/cmd/docs_append_table_test.goreproduces the issue against anhttptest-backed Docs mock (configurable drift betweenLocation.Indexand the table's reportedStartIndex); a drift of 5 reliably triggerstable not found near index 9pre-fix and passes post-fixTestPickTableNear_PrefersClosestForwardMatchdocuments the matcher's semantics for exact match, +1/+5/+25 drift, earlier-in-doc tables outside the backward tolerance, and the no-tables case🤖 Generated with Claude Code