Skip to content

server: TENSORFOLD_REQUEST_LOG_REPLIES records each reply beside its request - #4918

Merged
ashhart merged 4 commits into
ashhart:mainfrom
devopsfortunato:request-log-replies
Nov 25, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
devopsfortunato:request-log-replies

Conversation

@devopsfortunato

@devopsfortunato devopsfortunato commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Part of #3666 (the "Ask" half): the request log records each reply beside its request, opt-in.

TENSORFOLD_REQUEST_LOG=PATH already appends every chat and completion request body to PATH as one JSON line, redacted and owner-only. It recorded nothing about the reply, so a finished session could not be inspected afterwards.

What it does

TENSORFOLD_REQUEST_LOG_REPLIES=1 (default 0) appends one record per finished reply to the same file:

{"type": "reply", "id": "chatcmpl-…", "status": 200, "stream": false, "finish_reason": "stop", "text": "…", "reasoning": "…", "usage": {"prompt_tokens": 7, "cached_tokens": 2, "completion_tokens": 3}, "timings": {"queue_ms": 1.2, "ttft_ms": 3.4, "decode_tps": 56.7}, "truncated": false}
  • Streaming replies are written once complete, never per chunk; SSE chunks only feed the text and reasoning buffers.
  • A refusal writes its status and message only: {"type": "reply", "id": …, "status": 400, "error": "…"}. A reply that failed after it started keeps its error too, at whatever status it got.
  • The text and reasoning are bounded at 256 KiB: a piece past that is kept up to the limit with "truncated": true, so a long reply still shows its head.
  • With the switch on, each request line becomes {"type": "request", "id": …, "body": <the redacted body>} so request and reply pair by the id the client already got — including /api/chat and /api/generate, whose request line is now written once inside the shared OpenAI path. docs/environment.md and the CHANGELOG say so.
  • The request line is written before the request's own validation, so a 400 refused in plan() pairs with its reply. A request record past 8 MiB is refused, and its reply is still recorded.
  • Background work writes neither kind, as it writes no request line today.
  • The file keeps today's rules: owner-only, images/salts redacted, no headers or keys.

Bounds and retention

One JSON record is written per call, through a writer that serializes on a .lock file beside the log:

  • a record past 8 MiB is refused before the file is opened, so no empty file and no partial line is left;
  • at 64 MiB the current file becomes <path>.1 and a fresh one takes the next record, so the log keeps the newest traffic and at most 128 MiB stays on disk;
  • a refusal's message is kept to 4 KiB, cut on a UTF-8 boundary so a cut record still parses;
  • flock on the never-rotated .lock file is held across the size read, the rotation, the append and the rollback; a short write truncates back only after it actually wrote bytes.

On Windows the log refuses the platform rather than write a file whose access it cannot set (NTFS keeps access in ACLs, not mode bits); docs/WINDOWS.md says so. TENSORFOLD_REQUEST_JOURNAL still has its pre-existing Windows behaviour and is unchanged here.

TENSORFOLD_REQUEST_LOG_REPLIES is declared in capabilities --json, validated at startup as 0/1, and covered by the platform environment tests. zig build test-request-log-replies-host runs the sixteen focused tests on their own, next to the existing test-request-log-host.

The second half of #3666 (a tensorfold log reader and the desktop view) is not in this PR.

Off, nothing changes

The server golden corpus compares the request-log file line by line. With the switch off it needs no regeneration: zig build test-golden reports 327/340 equal to server goldens, 0 unexpected differences and 69/69 equal, 0 unexpected differences.

Checks run

  • zig build test-request-log-replies-host — 48 passed, 1 skipped (the Windows-only test), exit 0.
  • zig build test-request-log-host — 34/34, zig build test-ollama-e2e-host — 46/46, zig build test-environment-host — green, zig build test-golden — 327/340 and 69/69 equal to server goldens with 0 unexpected differences, zig build test-ling-host — green.
  • zig fmt --check build.zig zig — clean; zig ast-check on every changed .zig — clean; git diff --check — clean.
  • End-to-end: the Ollama reply records pair with their request by id, and two API-key clients on two threads keep their replies paired with their own request and their keys out of the log.

On this 4-core aarch64 VPS the whole zig build test suite is not a reliable local gate: unrelated family tests (KDA numerics, Flash Next config, KV pool E98, an Inkling stop cell) fail under load, none in a file this branch touches. The workflow run is waiting for a maintainer's approval (first-time contributor), so the checks above are the evidence until it runs.

@devopsfortunato
devopsfortunato force-pushed the request-log-replies branch 3 times, most recently from 947a1c5 to 5de4747 Compare November 24, 2026 17:36
@eXsoR65

eXsoR65 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Independent host verification of head 5de4747918 on a Linux x86_64 machine (no GPU), Zig 0.17.0:

  • zig build test-request-log-replies-host: 36/36 passed — the new reply records: switch-off writes no file; whole JSON reply (id, outcome, usage, timings); SSE writes exactly one line after every delta; legacy choices[].text whole and streamed; a 400 refusal records status/message and no text; text past 256 KiB truncated; the request line keeps its id and redacted body; a background priority writes no request line.
  • zig build test-request-log-host: 34/34 passed — the existing default/JSON request-log tests (access/ended/refused, secrets redaction, 8 KiB line, control bytes) still pass; no regression.
  • zig fmt --check (changed files) and git diff --check: clean.
  • lean_new.py vs base 9edf2f89: 165 head / 165 base, 0 new (the serve.zig getenv finding is inherited).

Host-side looks clean. Not run here: the full no-GPU suite (fuzz-cortheon etc. — CI covers it) and GPU/real-model gates; this is a host-only server logging feature.

— eXsoR65's AI agent (DeepSeek Harness): eXsoR65 authorized this comment before posting.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @devopsfortunato. The reply capture is off by default, and the whole/streamed reply tests are useful.

Three fixes are needed: bound the file's total growth and complete records, including error messages; enforce private access on Windows, where the current helper accepts inherited ACLs; and keep request/reply IDs paired on Ollama routes, whose request currently gets a null ID. Please add focused tests for those paths and two-client isolation. The content cap alone does not cover them.

  • Synthena

@devopsfortunato
devopsfortunato force-pushed the request-log-replies branch 2 times, most recently from 85bc172 to c55e783 Compare November 24, 2026 19:31
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @devopsfortunato. Ollama request/reply pairing and selection of all nine focused tests are fixed in source. Reply capture remains off by default.

Two issues remain: the append writer has no total retention or complete-record limit, including error strings, and Windows still accepts inherited access without enforcing private permissions. Please add the bounds and private-access enforcement or platform refusal, with focused tests. Please also cover actual Ollama pairing and two-client isolation; the current direct trace tests do not exercise those routes.

  • Synthena

@devopsfortunato
devopsfortunato force-pushed the request-log-replies branch 2 times, most recently from ba2d1c2 to 0a48634 Compare November 24, 2026 22:29
@devopsfortunato

Copy link
Copy Markdown
Contributor Author

Both points are addressed in 0a48634.

Bounds and retention. One JSON record per write, one file at a time:

  • a record past 8 MiB is refused before the file is opened (no empty file, no partial line);
  • the log rotates to <path>.1 at 64 MiB rather than stopping, so it keeps the newest traffic and at most 128 MiB stays on disk; the current file is reopened under the lock and its size re-read, not assumed;
  • a refusal's message is cut to 4 KiB on a UTF-8 boundary, so a cut record still parses;
  • writers serialize on <path>.lock with flock held across the size read, the rotation, the append and the rollback, and a short write truncates back only after it actually wrote bytes. The lock lives on a file that never rotates, so a waiter never locks a moving inode and cannot unlink the fresh .1.

Windows. The request log now refuses the platform instead of writing a file whose access it cannot enforce: write() returns before touching the path with a single warning, docs/WINDOWS.md and the environment table say so. I did not change request_journal.zig, which still accepts an inherited-ACL file on Windows — that is pre-existing and separable; I can follow it in its own PR if you want it.

Tests added for those paths (zig build test-request-log-replies-host, 16 focused tests):
rotation keeps every line whole under 8 concurrent writers; a record past the limit is refused while valid records before and after it survive; the file rotates at its limit and keeps the newest records; a refusal's message is cut on a UTF-8 boundary through the real Trace path; a request refused in plan() still pairs with its reply; and the Windows refusal itself.

End-to-end, as asked: ollama HTTP: replies pair with their request by id and ollama HTTP: two clients' replies pair with their own request id run over real loopback sockets with two API keys on two threads, the fake engine answering each prompt differently, asserting each reply's id and text belong to its own request and that neither key appears in the log.

Green on this tree: the three host steps (test-request-log-replies-host 48 passed/1 Windows-only skip, test-request-log-host 34/34, test-ollama-e2e-host 46/46), zig fmt --check build.zig zig, ast-check on every changed file. The workflow runs are still waiting for approval (first-time contributor), so CI has not reported yet.

@devopsfortunato

Copy link
Copy Markdown
Contributor Author

Two coverage gaps from the revision are now closed in 0a8781c (tests only, no production change), both red-checked by removing the behaviour they cover:

  • request log replies: text truncation keeps a UTF-8 boundary through the trace — a multi-byte character straddling the 256 KiB cap through the real Trace path: the stored text stays within the cap, is valid UTF-8 and carries no U+FFFD.
  • request log replies: a lock file others can read gets no records — a pre-existing <path>.lock with mode 0644 makes the writer refuse, and no log file is created.

Rebased onto current main; the three host steps are green on the rebased tree (test-request-log-replies-host 51 passed/1 Windows-only skip, test-ollama-e2e-host 47/47, test-request-log-host 35/35), zig fmt --check clean.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @devopsfortunato. The record/error bounds, stable rotation lock, Windows refusal, UTF-8 checks and actual two-client Ollama pairing tests are present. The focused results are useful, and this head merges cleanly.

One retention case remains: an existing oversized log is renamed intact to .1, so a 2 GiB log still exceeds the stated 128 MiB maximum after the next write. Please enforce a defined policy for already oversized files and add a focused boundary case. The expanded Ollama HTTP test file is also 624 lines; please split it while retaining the new assertions and test selection.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I ran the reply log and Ollama loopback suites on a clean trial merge of 0a8781c1fe into 4c79cfe346. All eight build steps succeeded, with 98 tests passing and one skipped on the M5 Max.

zig build test-request-log-replies-host test-ollama-e2e-host -Dfixed-cpu -j2 --summary all

For the oversized-file case Ash flagged, there is a small fixture that uses the existing request_log.append helper and keeps every line valid JSON. Write 40 copies of {"model":"fixture"} with a 1,024-byte limit, assert that the current file is already over 512 bytes, then append once with a 256-byte limit. Check that the active file has exactly one parseable record, .1 is absent or at most 256 bytes, and the two files total at most 512 bytes. I added those assertions locally and reran test-request-log-replies-host. The new test fails at the archive-size assertion while the existing outcomes are unchanged. This reproduces the retention issue with a file under 1 KiB.

Igor Lessio - AIFLOW LABS

@devopsfortunato

Copy link
Copy Markdown
Contributor Author

Both points are addressed in 92afc16, rebased onto current main.

The oversized-log case now has a policy. When a record must rotate the current file, the writer renames it to <path>.1 and then cuts that archive to the limit if it is larger, so a log that was already over the bound is reduced on the next write instead of carried over whole: <path> and <path>.1 never hold more than 2 × limit together. docs/environment.md and the CHANGELOG say so. The focused case (request log replies: an oversized archive is cut to the limit) fills a log under a 1024-byte limit until it is past 512 bytes, appends one record at a 256-byte limit, and asserts exactly one parseable record in the current file, .1 at most 256 bytes, and both files at most 512; removing the cut makes it fail at the .1 assertion.

The Ollama test file is split. ollama_e2e_test.zig is 458 lines again; the reply-log tests and their helpers moved to ollama_log_test.zig (174 lines), with the same test names, the same assertions and the same Windows skips, selected by the same request log replies: and ollama HTTP: filters. The harness stays in one place and is pub for the moved tests.

The zig job's failure was the lean check, and it was ours: lean: 169 problems, 165 on the base, 4 new — two multi-line comments in request_log.zig, one in ollama.zig, and the 624-line test file. The three comments are one line each now and the split removed the fourth; lean_check.py reports nothing for any file in this diff on the rebased head.

Green on the rebased head (92afc16): test-request-log-replies-host 52 passed / 1 skipped, test-ollama-e2e-host 47/47, test-request-log-host 35/35, test-environment-host green, zig fmt --check build.zig zig clean.

On the other two red jobs: zig-b and zig-windows fail on dozens of test binaries across modules this diff does not touch (moss image generation, pull, and others), with no failure naming a request-log or Ollama test, so I have not attributed them to this change.

@devopsfortunato

Copy link
Copy Markdown
Contributor Author

Thanks for the independent run, and for the fixture — it is now the test in the tree, nearly verbatim: 40 records at a 1024-byte limit, one append at 256, then exactly one parseable record in the active file, .1 at most 256 bytes, and both files at most 512 bytes (request log replies: an oversized archive is cut to the limit in request_log_replies_test.zig). Your reading of the bug was right: the archive was renamed intact, so the bound only held for records written after it. The writer now cuts an archive larger than the limit on rotation, so a pre-existing oversized log shrinks on the next write and the two files never exceed 2 × limit. Please re-run at your convenience against 92afc16.

@AIFlowML

Copy link
Copy Markdown
Contributor

Thanks for adding the fixture. I trial-merged 92afc16bbc into a3c3e5cef and ran the four targets in an ARM64 Linux container with Zig 0.17.0. All 170 executed checks passed, with the Windows-only case skipped.

zig build test-request-log-replies-host test-request-log-host test-ollama-e2e-host test-environment-host -Dfixed-cpu -j2 --summary all

The active file and both size checks now pass. I extended the oversized-archive fixture with one additional assertion after those checks:

try t.expect((try records(a, older)).len > 0);

On macOS, the existing 40-record fixture now fails there with BadRecord. The archive is cut at byte 256, which lands inside a JSON record. Could the cut stop after the last full line within the limit? Checking both files with the same parser would keep the storage bound and let the planned reader process the archive without special partial-record handling.

Igor Lessio - AIFLOW LABS

@devopsfortunato

Copy link
Copy Markdown
Contributor Author

One more fix in 5f2dc7f: I re-ran zig build test-golden on the reviewed head and it caught a regression I had introduced — MISMATCH lifecycle/request-log (golden 198 lines, zig 213 lines).

Cause: moving the request line to the top of plan() (so a refused request pairs with its reply) also logged requests that plan() refuses when the reply switch is off, so the plain request log gained lines that upstream's frozen golden does not have. With replies off the log must stay byte-identical.

The early typed write now happens only when TENSORFOLD_REQUEST_LOG_REPLIES is on; with it off the plain line is written at upstream's own position (after the request's checks in plan). Pairing is unchanged where it matters, and the frozen log is untouched: 327/340 equal to server goldens, 0 unexpected differences and 69/69, exit 0. The four host steps are still green (test-request-log-replies-host 52 passed/1 skipped, test-ollama-e2e-host 47/47, test-request-log-host 35/35, test-environment-host green), zig fmt --check clean, and the lean check still reports nothing for any file in this diff.

For the red check: restoring the early write for both modes reproduces the 213-line mismatch and test-golden fails, so the golden now covers this path.

@AIFlowML

Copy link
Copy Markdown
Contributor

Thanks for catching the opt-out regression. I trial-merged 5f2dc7f854 into 0a5c699c8 and independently verified the fix on macOS with Zig 0.17.0:

zig build test-request-log-replies-host test-request-log-host test-ollama-e2e-host test-environment -Dfixed-cpu -j2 --summary all
zig build test-golden -Dfixed-cpu -j2 --summary all

All 170 executed host checks passed, with one platform skip. Both lifecycle goldens matched, including the request log. The full run had 327 matching parity cases, 13 known differences, and 69 matching security cases with zero unexpected differences.

The archive boundary case from my earlier note still reproduces at this head. In request log replies: an oversized archive is cut to the limit, add try t.expect((try records(a, older)).len > 0); after the size checks and rerun zig build test-request-log-replies-host -Dfixed-cpu -j2 --summary all. That fixture returns BadRecord when it parses .1. Keeping the newest complete records within the same byte limit would let the test check both file size and JSONL validity.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @devopsfortunato. The test split is in place, and the reply-off request-log placement is restored. Oversized archives are now cut to the byte limit on successful truncation.

The remaining retention issue is the record boundary: that cut can stop inside JSON, and the new fixture parses only the active file. Please retain complete records within the limit and parse both files in the boundary test, keeping the byte bounds and existing pairing checks.

  • Synthena

devopsfortunato added a commit to devopsfortunato/TensorFold that referenced this pull request Nov 25, 2026
…boundary (ashhart#4918)

An archive above its limit was cut at a raw byte offset, which can stop inside
a JSON record and leave a torn last line. It is now cut back to the last '\n'
within the limit, keeping that newline, or to nothing when no record boundary
lies within it; the archive's size still stays at or under the limit.

The boundary test parses both files, so a mid-record cut is a BadRecord rather
than an unchecked size, and every archived line is checked to be a whole
request whose id predates the one record the active file holds.

Windows keeps no request log because the writer cannot set NTFS ACLs, so
test-golden says it skips lifecycle/request-log there, as it already does for
SIGTERM, rather than comparing 198 frozen lines against an empty file.
@devopsfortunato

Copy link
Copy Markdown
Contributor Author

Both points are addressed in c4760d5, on top of 5f2dc7f.

Record boundary. The archive is no longer cut at a raw byte offset. cutToRecord walks back from the limit in 4 KiB windows to the last newline and truncates there, so .1 ends on a complete record and still stays within the limit; if no record boundary exists inside the limit it truncates to 0 rather than keep a partial line. The archive descriptor moves from write-only to read-write because the scan reads what it cuts (ftruncate also needs a writable descriptor, so read-only would not do).

Boundary test. It now parses both files - records(a, path) and records(a, older) - asserts every archived line is a whole request record, that the archive ends on a newline, and that the cut is maximal (the archive is a whole multiple of one record, and one more record would exceed the limit). The byte bounds and the request/reply pairing check are kept.

zig-windows. This PR makes the request log refuse Windows, where NTFS keeps access in ACLs, so lifecycle/request-log compared 198 golden lines against 0 and the failure is in test-golden, not test. golden_check now skips that one case on Windows with a printed line, in the same style as the existing SIGTERM skip, rather than reporting it equal.

Local (Linux x86_64, Zig 0.17.0): test-request-log-replies-host 52 pass / 1 pre-existing Windows-only skip, test-request-log-host 35 pass, test-golden 327/340 equal with 0 unexpected differences. Windows is the first real check of the skip - I have no Windows host here, so that branch is unrun rather than claimed.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you, @devopsfortunato. The archive now cuts at a complete-record boundary, and the fixture parses both files while keeping the size and pairing checks. The Windows golden branch prints a skip instead of reporting equality. Both earlier asks are closed, and this head merges cleanly.

Ready to land after the usual final checks. Windows execution remains unrun as disclosed; the existing passing receipts are retained.

  • Synthena

ashhart added a commit that referenced this pull request Nov 25, 2026
…ases.zig

environment_test.zig was at 599 lines, so any new setting's rows broke the 600-line cap (#4918's two rows made 601). The 248-line table of valid forms now has its own file; the tests read it unchanged.
ashhart added a commit that referenced this pull request Nov 25, 2026
…ases.zig

environment_test.zig was at 599 lines, so any new setting's rows broke the 600-line cap (#4918's two rows made 601). The 248-line table of valid forms now has its own file; the tests read it unchanged.
ashhart pushed a commit to devopsfortunato/TensorFold that referenced this pull request Nov 25, 2026
…boundary (ashhart#4918)

An archive above its limit was cut at a raw byte offset, which can stop inside
a JSON record and leave a torn last line. It is now cut back to the last '\n'
within the limit, keeping that newline, or to nothing when no record boundary
lies within it; the archive's size still stays at or under the limit.

The boundary test parses both files, so a mid-record cut is a BadRecord rather
than an unchecked size, and every archived line is checked to be a whole
request whose id predates the one record the active file holds.

Windows keeps no request log because the writer cannot set NTFS ACLs, so
test-golden says it skips lifecycle/request-log there, as it already does for
SIGTERM, rather than comparing 198 frozen lines against an empty file.
@ashhart
ashhart force-pushed the request-log-replies branch from c4760d5 to 32e02fe Compare November 25, 2026 20:20
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

@devopsfortunato, we rebased this onto current main and pushed it as 32e02fe595.

Main's environment_test.zig had reached 599 lines, so your two TENSORFOLD_REQUEST_LOG_REPLIES rows took it past the 600-line cap. #5089 moved the table of valid forms into zig/src/platform/environment_valid_cases.zig, and your two rows now sit there, right after TENSORFOLD_DEVICE_CHECK. Nothing else changed. test-environment-host, test-request-log-replies-host and test-request-log-host pass on the rebased head.

It's back in a landing train.

  • Synthena

devopsfortunato and others added 4 commits November 25, 2026 21:58
…request (ashhart#3666)

TENSORFOLD_REQUEST_LOG already appends every chat and completion request body
as one JSON line, redacted and owner-only. It records nothing about the reply,
so a finished session cannot be inspected afterwards.

TENSORFOLD_REQUEST_LOG_REPLIES=1 (default 0) appends one record per finished
reply to the same file: the request id, status, stream flag, finish reason, the
text and reasoning the client received, prompt/cached/completion tokens, queue
time, time to first token and decode rate, or a refusal's message. Streaming
replies are written once complete, never per chunk, and a reply longer than
256 KiB keeps its head with "truncated": true. With the switch on, each request
line becomes a typed record carrying the same id, so the two lines pair up.

Off, the file is byte-for-byte what it is today: the server golden corpus
compares it line by line and needs no regeneration. Background work writes
neither kind, and the file keeps its owner-only, redacted rules.

The Ask half of ashhart#3666. The `tensorfold log` reader and the desktop view it also
describes stay a follow-up.
…boundary (ashhart#4918)

An archive above its limit was cut at a raw byte offset, which can stop inside
a JSON record and leave a torn last line. It is now cut back to the last '\n'
within the limit, keeping that newline, or to nothing when no record boundary
lies within it; the archive's size still stays at or under the limit.

The boundary test parses both files, so a mid-record cut is a BadRecord rather
than an unchecked size, and every archived line is checked to be a whole
request whose id predates the one record the active file holds.

Windows keeps no request log because the writer cannot set NTFS ACLs, so
test-golden says it skips lifecycle/request-log there, as it already does for
SIGTERM, rather than comparing 198 frozen lines against an empty file.
@ashhart
ashhart force-pushed the request-log-replies branch from 32e02fe to 25832b9 Compare November 25, 2026 21:59
@ashhart
ashhart merged commit 44c42f0 into ashhart:main Nov 25, 2026
ashhart pushed a commit that referenced this pull request Nov 25, 2026
…boundary (#4918)

An archive above its limit was cut at a raw byte offset, which can stop inside
a JSON record and leave a torn last line. It is now cut back to the last '\n'
within the limit, keeping that newline, or to nothing when no record boundary
lies within it; the archive's size still stays at or under the limit.

The boundary test parses both files, so a mid-record cut is a BadRecord rather
than an unchecked size, and every archived line is checked to be a whole
request whose id predates the one record the active file holds.

Windows keeps no request log because the writer cannot set NTFS ACLs, so
test-golden says it skips lifecycle/request-log there, as it already does for
SIGTERM, rather than comparing 198 frozen lines against an empty file.
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @devopsfortunato. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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.

4 participants