Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(store): keep ADR line endings intact when splicing a section

Martin Vogeld90f98f5f4f1
diff --git a/src/store/store.c b/src/store/store.c
index dd97ef2d8..ee58d10f6 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -8900,6 +8900,44 @@ static void adr_find_cb(void *ctx, const cbm_adr_heading_t *h) {
f->body_end = h->body_end;
}
+/* The line ending this code should WRITE into `doc`: whichever of "\r\n" and
+ * "\n" the document already uses more often, LF on a tie or an empty document.
+ * Lines that already exist are never rewritten — this decides only the
+ * separator and the "## NAME" line an append adds, so a CRLF document does not
+ * acquire a stray bare newline nobody asked for. */
+static const char *adr_dominant_eol(const char *doc, size_t doc_len) {
+ size_t crlf = 0;
+ size_t lf = 0;
+ for (size_t i = 0; i < doc_len; i++) {
+ if (doc[i] != '\n') {
+ continue;
+ }
+ if (i > 0 && doc[i - SKIP_ONE] == '\r') {
+ crlf++;
+ } else {
+ lf++;
+ }
+ }
+ return crlf > lf ? "\r\n" : "\n";
+}
+
+/* Trailing line breaks, counting a "\r\n" PAIR as ONE break. Counting raw '\n'
+ * characters gets the count right but loses the shape, and the separator that
+ * is then appended has to match the ending it is continuing. */
+static size_t adr_trailing_breaks(const char *doc, size_t doc_len) {
+ size_t idx = doc_len;
+ size_t breaks = 0;
+ while (idx > 0 && (doc[idx - SKIP_ONE] == '\n' || doc[idx - SKIP_ONE] == '\r')) {
+ bool was_lf = doc[idx - SKIP_ONE] == '\n';
+ idx--;
+ if (was_lf && idx > 0 && doc[idx - SKIP_ONE] == '\r') {
+ idx--;
+ }
+ breaks++;
+ }
+ return breaks;
+}
+
char *cbm_adr_splice_section(const char *content, const char *name, const char *body) {
if (!name || !body) {
return NULL;
@@ -8951,37 +8989,40 @@ char *cbm_adr_splice_section(const char *content, const char *name, const char *
return out;
}
- /* Absent: append. Enough newlines are added to leave exactly one blank
+ /* Absent: append. Enough line breaks are added to leave exactly one blank
* line before the new heading — never fewer, and never rewriting the
- * newlines the document already ends with. */
- size_t trailing = 0;
- while (trailing < doc_len && doc[doc_len - SKIP_ONE - trailing] == '\n') {
- trailing++;
- }
+ * breaks the document already ends with. Both the separator and the
+ * heading line use the document's OWN ending: counting raw '\n' here would
+ * drop a bare newline into a CRLF document, leaving it mixed. */
+ const char *eol = adr_dominant_eol(doc, doc_len);
+ size_t eol_len = strlen(eol);
+ size_t breaks = adr_trailing_breaks(doc, doc_len);
size_t sep = 0;
- if (doc_len > 0 && trailing < PAIR_LEN) {
- sep = PAIR_LEN - trailing;
+ if (doc_len > 0 && breaks < PAIR_LEN) {
+ sep = PAIR_LEN - breaks;
}
size_t name_len = strlen(name);
- size_t total = doc_len + sep + ST_HEADER_PREFIX + name_len + SKIP_ONE + body_len;
+ size_t total = doc_len + (sep * eol_len) + ST_HEADER_PREFIX + name_len + eol_len + body_len;
out = malloc(total + SKIP_ONE);
if (!out) {
return NULL;
}
- size_t at = 0;
+ size_t cursor = 0;
memcpy(out, doc, doc_len);
- at += doc_len;
+ cursor += doc_len;
for (size_t i = 0; i < sep; i++) {
- out[at++] = '\n';
- }
- memcpy(out + at, "## ", ST_HEADER_PREFIX);
- at += ST_HEADER_PREFIX;
- memcpy(out + at, name, name_len);
- at += name_len;
- out[at++] = '\n';
- memcpy(out + at, body, body_len);
- at += body_len;
- out[at] = '\0';
+ memcpy(out + cursor, eol, eol_len);
+ cursor += eol_len;
+ }
+ memcpy(out + cursor, "## ", ST_HEADER_PREFIX);
+ cursor += ST_HEADER_PREFIX;
+ memcpy(out + cursor, name, name_len);
+ cursor += name_len;
+ memcpy(out + cursor, eol, eol_len);
+ cursor += eol_len;
+ memcpy(out + cursor, body, body_len);
+ cursor += body_len;
+ out[cursor] = '\0';
return out;
}
diff --git a/tests/test_store_arch.c b/tests/test_store_arch.c
index 3a93c4722..4e9f59068 100644
--- a/tests/test_store_arch.c
+++ b/tests/test_store_arch.c
@@ -1011,6 +1011,103 @@ TEST(adr_splice_preserves_untouched_bytes) {
PASS();
}
+/* Line endings. Every other ADR fixture in this file uses \n, so nothing
+ * exercised a CRLF-stored document — and CI could not have caught that,
+ * because the fixtures are LF whichever platform runs them.
+ *
+ * The exposure is specific to splicing: the old rebuild normalised everything
+ * on the way out, so ending confusion was invisible. A byte-span replacement
+ * can cut mid-"\r\n" or miscount a separator run, and byte-identity is the
+ * property this design exists to provide.
+ *
+ * Contract: bytes that already exist are never rewritten, so each line keeps
+ * whatever ending it had. Text this code writes itself — the separator before
+ * an appended section and the "## NAME" line — follows the document's majority
+ * ending, LF on a tie. A caller's body is inserted verbatim; rewriting the
+ * bytes a caller supplied would be the same silent modification this whole
+ * change removes. */
+TEST(adr_splice_preserves_line_endings) {
+ /* CRLF throughout: untouched bytes keep their \r\n. */
+ CHECK_SPLICE("## PURPOSE\r\nFoo\r\n\r\n## STACK\r\nBar", "STACK", "New bar",
+ "## PURPOSE\r\nFoo\r\n\r\n## STACK\r\nNew bar");
+
+ /* The separator run before the next heading survives as CRLF, not as the
+ * two bare newlines a naive walk-back would leave. */
+ CHECK_SPLICE("## A\r\nx\r\n\r\n## B\r\ny", "A", "z", "## A\r\nz\r\n\r\n## B\r\ny");
+
+ /* A trailing CRLF on the document is preserved as CRLF. */
+ CHECK_SPLICE("## A\r\nx\r\n", "A", "z", "## A\r\nz\r\n");
+
+ /* Appending to a CRLF document writes CRLF — a bare \n here would leave a
+ * mixed document that neither the author nor the tool asked for. */
+ CHECK_SPLICE("## PURPOSE\r\nFoo", "DECISIONS", "- New.",
+ "## PURPOSE\r\nFoo\r\n\r\n## DECISIONS\r\n- New.");
+
+ /* One existing trailing break means one more is added, counting \r\n as a
+ * single break rather than as two characters. */
+ CHECK_SPLICE("## PURPOSE\r\nFoo\r\n", "DECISIONS", "- New.",
+ "## PURPOSE\r\nFoo\r\n\r\n## DECISIONS\r\n- New.");
+
+ /* A document already ending in a CRLF blank line gains no extra break. */
+ CHECK_SPLICE("## PURPOSE\r\nFoo\r\n\r\n", "DECISIONS", "- New.",
+ "## PURPOSE\r\nFoo\r\n\r\n## DECISIONS\r\n- New.");
+
+ /* LF documents are unaffected by any of the above. */
+ CHECK_SPLICE("## PURPOSE\nFoo\n", "DECISIONS", "- New.",
+ "## PURPOSE\nFoo\n\n## DECISIONS\n- New.");
+ PASS();
+}
+
+/* Mixed endings are what real files become through editors and merges. Lines
+ * that already exist keep their own endings; the majority decides only what
+ * this code writes itself. */
+TEST(adr_splice_mixed_line_endings) {
+ /* Majority CRLF (2 of 3) -> the appended block is CRLF, and the one bare
+ * LF line in the middle is left exactly as it was. */
+ CHECK_SPLICE("## PURPOSE\r\nFoo\nBar\r\n", "DECISIONS", "- New.",
+ "## PURPOSE\r\nFoo\nBar\r\n\r\n## DECISIONS\r\n- New.");
+
+ /* Majority LF (2 of 3) -> the appended block is LF, and the lone CRLF line
+ * survives untouched. */
+ CHECK_SPLICE("## PURPOSE\nFoo\r\nBar\n", "DECISIONS", "- New.",
+ "## PURPOSE\nFoo\r\nBar\n\n## DECISIONS\n- New.");
+
+ /* A tie falls to LF. */
+ CHECK_SPLICE("## PURPOSE\r\nFoo\n", "DECISIONS", "- New.",
+ "## PURPOSE\r\nFoo\n\n## DECISIONS\n- New.");
+
+ /* Replacing a section in a mixed document rewrites nothing around it. */
+ CHECK_SPLICE("## PURPOSE\r\nFoo\n\n## STACK\nBar\r\n", "STACK", "New",
+ "## PURPOSE\r\nFoo\n\n## STACK\nNew\r\n");
+ PASS();
+}
+
+/* "## PURPOSE\r\n" must locate exactly as "## PURPOSE\n" does. The scanner
+ * trims \r from heading names, but trimming and matching are different claims
+ * and only one of them was pinned. */
+TEST(adr_splice_matches_headings_across_line_endings) {
+ adr_name_collect_t c;
+ memset(&c, 0, sizeof(c));
+ ASSERT_EQ(cbm_adr_scan_headings("## PURPOSE\r\nFoo\r\n\r\n## STACK\r\nBar",
+ adr_collect_names, &c),
+ CBM_STORE_OK);
+ ASSERT_STR_EQ(c.buf, "[PURPOSE][STACK]");
+
+ /* A fenced block with CRLF still hides its heading, and still closes. */
+ memset(&c, 0, sizeof(c));
+ ASSERT_EQ(cbm_adr_scan_headings("## PURPOSE\r\nFoo\r\n\r\n```md\r\n## Example\r\n```"
+ "\r\n\r\n## STACK\r\nBar",
+ adr_collect_names, &c),
+ CBM_STORE_OK);
+ ASSERT_STR_EQ(c.buf, "[PURPOSE][STACK]");
+
+ /* And an unterminated CRLF fence is still refused. */
+ char errbuf[256];
+ ASSERT_TRUE(cbm_adr_check_structure("## PURPOSE\r\nFoo\r\n\r\n```\r\nopen\r\n", errbuf,
+ sizeof(errbuf)) != CBM_STORE_OK);
+ PASS();
+}
+
/* The original use case: add an entry under its own heading. */
TEST(adr_splice_appends_arbitrary_heading) {
CHECK_SPLICE("## PURPOSE\nFoo", "DECISIONS", "- Chose SQLite.",
@@ -1620,6 +1717,9 @@ SUITE(store_arch) {
RUN_TEST(adr_validate_keys_accepts_arbitrary_names);
RUN_TEST(adr_validate_keys_rejects_unroundtrippable);
RUN_TEST(adr_splice_preserves_untouched_bytes);
+ RUN_TEST(adr_splice_preserves_line_endings);
+ RUN_TEST(adr_splice_mixed_line_endings);
+ RUN_TEST(adr_splice_matches_headings_across_line_endings);
RUN_TEST(adr_splice_appends_arbitrary_heading);
RUN_TEST(adr_splice_is_idempotent);
RUN_TEST(adr_splice_matches_case_exactly);