session: a torn tail no longer swallows the next record

A write that ran out of disk leaves half a JSONL line at the end of the
session file. The next record was appended straight onto it, the two became
one line, and the loader (which drops unterminated lines) lost the NEW
message - seen for real today when /tmp filled up.

The writer now closes a torn line with a newline before appending, so the
damage stays the one record that was never finished. The file is opened
O_RDWR for that: O_WRONLY cannot pread.

tests/test_session.c writes a torn line, appends a turn, and checks the
conversation still loads with the new message: red before the fix
(loaded == 2 was 1), green after. make check: all tests passed, rc 0.
This commit is contained in:
lotherk committed 2026-11-23 00:06:21 +00:00
1 parent 30e202eca0
commit ea772e1851
2 files changed
+73 -2

No files matched your search

+36 -2
View File
@@ -367,6 +367,31 @@ write_all(int fd, const char *p, size_t n)
return 0;
}
/*
* End a line that a previous write left unterminated. The session file is
* append-only and one record per line; if the last byte is not a newline, the
* record in front is torn (out of disk, a kill) and appending straight onto it
* would make the two one line - the loader drops both. The file is opened
* O_RDWR for this: O_WRONLY cannot pread.
*/
static int
terminate_torn_line(int fd)
{
char last = '\0';
off_t end;
ssize_t got;
end = lseek(fd, 0, SEEK_END);
if (end == 0)
return 0;
got = pread(fd, &last, 1, end - 1);
if (got < 0)
return errno == EINTR ? terminate_torn_line(fd) : -1;
if (got == 1 && last == '\n')
return 0;
return write_all(fd, "\n", 1);
}
static int
brief_cmp(const void *a, const void *b)
{
@@ -497,14 +522,23 @@ sp_session_append_rec(const struct sp_session_info *info,
}
snprintf(path, sizeof(path), "%s/%s.jsonl", info->dir, info->id);
fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0600);
fd = open(path, O_RDWR | O_CREAT | O_APPEND, 0600);
if (fd < 0) {
sp_buf_printf(err, "session: cannot open %s: %s", path,
strerror(errno));
sp_buf_free(&line);
return -1;
}
rc = write_all(fd, line.p, line.len);
/*
* A write that ran out of space (ENOSPC) leaves a half line behind, and
* the next record appended after it would join it: the loader skips the
* torn line, so the record behind it disappeared from the transcript -
* seen 2026-11-22 when /tmp filled up. Close the torn line first, so
* the damage stays the one record that was lost.
*/
rc = terminate_torn_line(fd);
if (rc == 0)
rc = write_all(fd, line.p, line.len);
if (rc == 0 && fsync(fd) != 0)
rc = -1;
if (rc != 0)
+37
View File
@@ -65,6 +65,43 @@ main(void)
fclose(f);
}
/*
* A torn tail must not swallow what is written after it. A run that
* hits ENOSPC leaves half a line; appending the next record straight
* onto it made one unparseable line out of two, and the loader - which
* skips a torn line - then lost the record behind it too (seen
* 2026-11-22 when /tmp filled). The writer closes the torn line first.
*/
{
struct sp_conv c;
size_t loaded = 0;
CHECK(sp_session_open(&s, dir, "torn", &err) == 0);
CHECK(sp_session_append(&s, "user", "vor dem absturz",
&err) == 0);
snprintf(path, sizeof(path), "%s/torn.jsonl", dir);
f = fopen(path, "a");
CHECK(f != NULL);
if (f != NULL) {
fputs("{\"role\":\"assistant\",\"text\":\"halb", f);
fclose(f);
}
CHECK(sp_session_append(&s, "user", "nach dem absturz",
&err) == 0);
sp_conv_init(&c);
CHECK(sp_session_load(&s, &c, &loaded, &err) == 0);
CHECK(loaded == 2);
CHECK(c.n == 2);
if (c.n == 2) {
CHECK(strcmp(c.v[0].content, "vor dem absturz") == 0);
CHECK(strcmp(c.v[1].content, "nach dem absturz") == 0);
}
sp_conv_free(&c);
CHECK(sp_session_delete(dir, "torn", &err) == 0);
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
snprintf(path, sizeof(path), "%s/s1.jsonl", dir);
}
/* a torn tail must not be counted as a record and must not be an error */
f = fopen(path, "a");
CHECK(f != NULL);