Skip to content

Re-read ui/index.html when its mtime changes (#777) - #778

Merged
anilmurty merged 3 commits into
Metabuilder-Labs:mainfrom
ardaylmz30:fix/reload-index-html-on-change
Oct 6, 2026
Merged

anilmurty merged 3 commits into
Metabuilder-Labs:mainfrom
ardaylmz30:fix/reload-index-html-on-change

Conversation

@ardaylmz30

@ardaylmz30 ardaylmz30 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

tj serve cached ui/index.html at startup, so UI edits served stale markup until a restart.

  • _serve_ui now re-reads index.html when its mtime changes (one stat() per request); the cached string is otherwise reused.
  • The tj-api-key / tj-write-token meta injection still applies to the re-read HTML.
  • If the file is missing or unreadable, the last good copy is served.
  • Updated .claude/rules/web-ui.md to describe the new behaviour.
  • Added tests/unit/test_ui_index_reload.py (edit → reload shows new content; meta injection on re-read HTML; missing file keeps last good copy).

Related issue

Closes #777

Checklist

  • Tests pass (pytest tests/unit/test_ui_index_reload.py tests/unit/test_ui_offline.py tests/unit/test_budget_route.py)
  • Lint clean (ruff check tokenjam/)
  • Type check clean (mypy tokenjam/api/app.py)
  • CLAUDE.md updated (not needed, no architecture change)
  • Test spans use tests/factories.py (n/a, no spans in these tests)
  • Requested @anilmurty as reviewer

tj serve cached index.html at startup, so UI edits served stale markup until a restart. Re-read the file when its mtime changes; the api-key and write-token meta injection still applies to the re-read HTML. Add tests and update .claude/rules/web-ui.md.
@ardaylmz30
ardaylmz30 requested a review from anilmurty as a code owner October 6, 2026 08:12
@greptile-apps

greptile-apps Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Web UI now re-reads index.html on file changes instead of at startup.

The PR appears safe to merge; no actionable issues remain.

What we checked:

  • UTF-8 matches the shipped page: The shipped page declares UTF-8, and the reader now uses that encoding. Both UI entry routes still use the same reader.
Summary

The PR refreshes cached index.html when its modification time changes, while keeping token injection and the last readable copy.

  • The latest change reads HTML explicitly as UTF-8 and adds encoding tests.
  • The earlier unreadable-file finding is fixed: UnicodeDecodeError is caught without advancing the cached modification time.
  • No new actionable issues were found. Tests were inspected, not run.

Reviews (3) · Last reviewed commit: "Pin the index.html read to utf-8 (#777)"

Comment thread tokenjam/api/app.py Resolved
@anilmurty

Copy link
Copy Markdown
Contributor

Thanks @ardaylmz30, and welcome. Good first PR. You took option (a) from #777, left the production read-once default intact rather than deleting the cache, and the tests are load-bearing: I reverted tokenjam/api/app.py to main and three of your four tests fail, which is exactly what I want to see on a bug fix.

What I verified

  • pytest tests/unit/test_ui_index_reload.py tests/unit/test_ui_offline.py tests/unit/test_lens_module_parses.py tests/unit/test_lens_dashboard_states.py tests/integration/test_api.py → 253 passed. A wider tests/unit/ tests/integration/ -k "ui or lens or app or serve or api" run → 1378 passed, 1 skipped. ruff check tokenjam/ clean, mypy tokenjam/ clean across 288 files.
  • Dogfooded against a real uvicorn server instead of TestClient: edited ui/index.html under a running server, and the next GET / served the edit, byte-identical to disk plus the two <meta> injections. Restoring the file reverted cleanly. The reload does what the issue asked for.
  • Critical Rule 50 in CLAUDE.md: test_lens_module_parses.py reads from disk and the served bytes now track disk more closely than before, so there is no new coverage gap in the parse check.
  • Critical Rule 24: the user path is real (edit the file, reload the tab), not just "the code is invoked."
  • Critical Rule 49: each response carries a whole document, so there is no half-mounted tree to worry about.
  • In a packaged install index.html sits in site-packages with a constant mtime, so shipped behaviour is the old read-once plus one stat() per page load. That is the right answer to the issue's "reading once is a reasonable production default."
  • Greptile's P2 on the escaping UnicodeDecodeError is already fixed by your second commit and its re-review is 5/5. I agree with that triage.

One change I want before this merges

index_path.read_text() passes no encoding, so it uses the locale default. ui/index.html carries about 3000 non-ASCII bytes, so under a non-UTF-8 locale (LC_ALL=C, which is what a bare container or a systemd unit with no locale set gives you) that read raises UnicodeDecodeError. On main that was a loud crash at create_app() naming the file. With the new except (OSError, UnicodeDecodeError): pass it is swallowed instead, and I measured what the user gets:

LC_ALL=C  →  GET /  →  HTTP 200, body length 0

A blank page behind a 200 with nothing in the console is the most expensive failure mode this UI has (Critical Rule 50 in CLAUDE.md). One keyword closes it:

_ui_cache["html"] = index_path.read_text(encoding="utf-8")

That also makes your UnicodeDecodeError catch mean what its comment says, a save caught mid-character, rather than quietly absorbing a locale misconfiguration. It matches test_lens_module_parses.py and test_ui_offline.py, which both pin encoding="utf-8", and it is the majority style in tokenjam/ (73 call sites to 31).

Two optional notes, neither blocking

  1. A truncated but still valid UTF-8 save gets served. I reproduced it live: writing half the file produced a 430KB response with no closing </html>, so a blank dashboard until the next reload. It self-corrects, because the next save bumps the mtime again, so I am fine shipping as is. If you want to close it, accept a re-read only when the content ends with </html> and otherwise keep the cached copy with the mtime unadvanced, the same shape as your decode path.
  2. mtime alone misses an edit on a filesystem with coarse mtime granularity, or with an editor that preserves mtime. Comparing (st_mtime_ns, st_size) costs nothing, since you already hold the stat_result.

One process note, not on you

CI has not actually run on this PR. Fork PRs need a maintainer to approve the workflow run, so the checklist in your description reflects your local run rather than our CI. We will start it. I want test (3.10/3.11/3.12) and test-ts green alongside the encoding change.

Add the encoding="utf-8" line and push, and this is ready to merge. The optional notes are yours to take or leave.

read_text() with no encoding= resolves to the LOCALE default, and
ui/index.html holds ~3KB of non-ASCII. Under LC_ALL=C -- a bare
container, a systemd unit with no locale -- the read raises
UnicodeDecodeError.

Before this PR that was a loud crash at create_app() naming the file.
With the new cache and its `except (OSError, UnicodeDecodeError)` it
becomes silent: the empty initial cache survives and `GET /` returns
HTTP 200 with a zero-length body. A blank dashboard, nothing in the
console. That is exactly the failure Critical Rule 50 exists for, and
the catch is right for the mid-write truncation it was written for --
it just also swallowed this.

Two tests. The first asserts the call passes encoding="utf-8" and fails
against the unfixed line; a behavioural version would have to mutate the
process-wide locale, which is unsafe under -n auto and resolves
differently across 3.10/3.11/3.12, so it would prove less while breaking
more. The second round-trips the dash, bullet, arrow and tick characters
the real file uses, so the pin has a companion that tests content rather
than call shape.

Side effect worth noting: this also makes the existing undecodable-file
test deterministic. It writes a truncated UTF-8 sequence and expects a
decode failure, which a latin-1 locale would have decoded happily.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-Authored-By: ardaylmz30 <[email protected]>
@anilmurty

Copy link
Copy Markdown
Contributor

Thanks @ardaylmz30 — CI is green across all six jobs now that I've approved the fork run, and the repair-and-retry commit handles the mid-write case cleanly.

I've pushed the one-keyword encoding fix to your branch myself (89c0c95) rather than make you do another round-trip on a first contribution. Nothing else changed, and the commit credits you as co-author.

For the record of why it mattered: read_text() with no encoding= resolves to the locale default, and ui/index.html carries about 3KB of non-ASCII. Under LC_ALL=C — a bare container, a systemd unit with no locale — the read raises UnicodeDecodeError. On main that was a loud crash at create_app() naming the file. With your cache and its except (OSError, UnicodeDecodeError) it became silent instead: the empty initial cache survives and GET / returns 200 with a zero-length body. Blank page, empty console.

That is not a criticism of the catch — it is the right fix for the truncation case it was written for, and the bot was right to ask for it. It just also swallowed something that used to be loud, which is the kind of thing only a second pair of eyes catches.

Two tests came with it: one asserts the call passes encoding="utf-8" and fails against the unfixed line, and one round-trips the dash, bullet, arrow and tick characters the real file uses. A side benefit is that your undecodable-file test is now deterministic — it writes a truncated UTF-8 sequence and expects a decode failure, which a latin-1 locale would have decoded happily.

Your two non-blocking nits from the review are worth doing but not here: gating the re-read on a trailing </html>, and using (st_mtime_ns, st_size) so a coarse-granularity filesystem or an mtime-preserving editor does not hide a change. Both are free follow-ups if you want them.

Merging once CI comes back green. Nice first contribution — the mtime approach was the right one of the two the issue offered, and the rules file update was accurate, including the point about ui/vendor/* never having been app-cached.

@anilmurty anilmurty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. CI is 6/6 green on 89c0c95, the branch is up to date with main, and all four of issue #777's acceptance criteria are met with no scope creep.

Verified locally rather than taken on CI's word: the encoding pin fails against the unfixed line and passes with it, 191 tests across the reload, UI-offline, module-parse and API-integration files pass, ruff check tokenjam/ is clean and mypy tokenjam/ is clean across 288 files. The rendered UI was also exercised against a real server, not just TestClient — edit the file, next request serves it, byte-identical to disk with both <meta> injections intact.

One process note for the record: Greptile reviewed the earlier head and returned 5/5 with nothing actionable, and has not re-reviewed since the encoding commit roughly twenty minutes ago. I am merging on the strength of the verification above rather than waiting indefinitely, since the delta since its pass is one keyword and two tests that I wrote and checked myself.

Thanks again @ardaylmz30.

@anilmurty
anilmurty merged commit d9ae1ed into Metabuilder-Labs:main Oct 6, 2026
6 checks passed
@anilmurty

Copy link
Copy Markdown
Contributor

Correcting something I wrote in the approval above: I said Greptile had not re-reviewed after the encoding commit. It had. It re-reviewed 89c0c95 at 17:30, sixteen minutes before the merge, and returned 5/5 with nothing actionable.

I had it wrong because Greptile edits its existing summary comment in place rather than posting a new review, so a poll that counts reviews or looks at submittedAt sees nothing change. The signals that actually move are the comment's updated_at and the "Last reviewed commit" line in its body.

So the merge was not made over a missing bot review, and the §5c process note I attached to it was based on a false premise. Nothing about the change itself is affected.

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.

tj serve caches index.html at startup, so UI edits silently serve stale markup

2 participants