[PR #1380] [MERGED] Propagate user timezone into generation prompt assembly #5050

Closed
opened 2026-10-22 04:12:46 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/1380
Author: @Romuromylus
Created: 5/27/2026
Status: ✅ Merged
Merged: 5/27/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/refactor-issue-1356-user-timezone


📝 Commits (4)

  • 3313dc9 Propagate user timezone into generation prompt assembly
  • 9e16666 Merge branch 'refactor' into fix/refactor-issue-1356-user-timezone
  • e012a21 Merge branch 'refactor' into fix/refactor-issue-1356-user-timezone
  • ad42e15 Address review: centralize timezone fallback so all generation paths inherit

📊 Changes

6 files changed (+257 additions, -5 deletions)

View changed files

📝 src/engine/generation/prompt-assembly.ts (+27 -0)
📝 src/engine/generation/start-generation.ts (+7 -0)
➕ src/engine/shared/macros/macro-engine.test.ts (+71 -0)
📝 src/engine/shared/macros/macro-engine.ts (+21 -5)
➕ src/engine/shared/time/timezone.ts (+122 -0)
📝 src/features/runtime/generation/hooks/use-generate.ts (+9 -0)

📄 Description

Why this change

Closes #1356.

The legacy generation flow shipped the browser's resolved IANA timezone (userTimeZone) into prompt assembly so prompt-time macros ({{date}}, {{time}}, {{datetime}}, {{isotime}}, {{weekday}}) and any prompt code relying on local date context resolved in the user's local frame. The refactor's macro engine resolves {{date}} / {{datetime}} / {{isotime}} from Date.prototype.toISOString and so produces UTC, which silently drifts a calendar day for users west of UTC late at night and east of UTC early in the morning. No timezone string flows from the UI into the engine at all today.

What changed

  • src/engine/shared/time/timezone.ts (new): IANA-tz normalizer plus zoned-parts/date/time/weekday/ISO-with-offset helpers built on Intl.DateTimeFormat. The normalizer trims, length-caps to 100, and probes the value via Intl.DateTimeFormat so invalid strings drop quietly to undefined.
  • src/engine/shared/macros/macro-engine.ts: adds MacroContext.timeZone?: string and routes {{date}} / {{time}} / {{datetime}} / {{isotime}} / {{weekday}} through the new helpers. All helpers fall back to the host machine's local zone via Intl when no zone is provided, so a caller that skips the input plumbing never lands in UTC.
  • src/engine/generation/start-generation.ts: adds userTimeZone?: string to StartGenerationInput.
  • src/engine/generation/prompt-assembly.ts: resolvePromptTimeZone resolves in preference order persisted chat.metadata.promptTimeZone → caller-supplied request.userTimeZone → live Intl.DateTimeFormat().resolvedOptions().timeZone. The live fallback is the load-bearing piece — engine code runs in the user's Tauri webview, so Intl always reflects the user's OS. This means every startGeneration entry point (the chat hook here, the game-turn service, the background-autonomous chat hook, and previewGenerationPrompt) resolves macros in the user's local frame without per-callsite plumbing.
  • src/features/runtime/generation/hooks/use-generate.ts: resolves the live browser timezone via Intl.DateTimeFormat().resolvedOptions().timeZone and threads it into the startGeneration input. With the engine-side fallback in place this is explicit-intent rather than load-bearing; it stays for clarity and to keep an override surface for future non-default callers (e.g. remote runtime).

Two commits:

  1. Propagate user timezone into generation prompt assembly — initial fix, plumbed tz through the chat-message generation path.
  2. Address review: centralize timezone fallback so all generation paths inherit — moves the live-host fallback into resolvePromptTimeZone so game-mode and background-autonomous callers (which build their own startGeneration args without userTimeZone) and the prompt-preview UI all pick up the fix automatically. Also fixes formatZonedIsoDateTime(date, undefined) returning UTC (it now matches the host-local behavior of the other helpers) and extends the macro-engine fallback test with a {{datetime}} assertion.

Verification

  • pnpm check ✅ (architecture / dependency-cruiser, frontend tsc -b, cargo check, docs)
  • pnpm test ✅ (21 suites / 106 tests, including 6 Vitest specs for the macro engine that pin both the zoned-tz case and the host-local fallback)
  • Browser-tier repro spec exercised against pnpm dev confirms RED → GREEN: pre-fix the macro engine returned 2026-05-27 (UTC) for an instant that is 2026-05-26 in LA-local; post-fix it returns the LA-local date.
  • Smoked end-to-end in pnpm tauri dev: {{date}} / {{time}} / {{weekday}} now render in the user's local zone in real chat output, and a chat reply that includes those macros resolves correctly under normal usage.

Risks called out

  • Default macro behavior shift: previous {{date}} returned UTC, new default returns host-local. This is the bug being fixed, but it does change observable output for any preset that depended on the old UTC behavior. Couldn't find such a preset in the repo.
  • chat.metadata.promptTimeZone precedence: persisted overrides on legacy chats win over the live browser value. Intentional — matches the legacy server's chatMeta.promptTimeZone ?? input.userTimeZone order. Normalization rejects invalid stored strings so they fall through to the live host zone rather than producing UTC.
  • Mode parity: change is in mode-neutral prompt assembly + macro engine + engine-level fallback, so chat / roleplay / game / preview / background-autonomous all pick up the same fix in one path.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/1380 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/27/2026 **Status:** ✅ Merged **Merged:** 5/27/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/refactor-issue-1356-user-timezone` --- ### 📝 Commits (4) - [`3313dc9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3313dc917be81459b4c0612f9c97bcab1b25d10f) Propagate user timezone into generation prompt assembly - [`9e16666`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9e166662b6aa15163b6511ceaf69da3b6a9477bd) Merge branch 'refactor' into fix/refactor-issue-1356-user-timezone - [`e012a21`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e012a21d20f39a4bf07477dbd4bbe493480801d9) Merge branch 'refactor' into fix/refactor-issue-1356-user-timezone - [`ad42e15`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ad42e153e712051487dd75641ae12ebb1cecbf5c) Address review: centralize timezone fallback so all generation paths inherit ### 📊 Changes **6 files changed** (+257 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `src/engine/generation/prompt-assembly.ts` (+27 -0) 📝 `src/engine/generation/start-generation.ts` (+7 -0) ➕ `src/engine/shared/macros/macro-engine.test.ts` (+71 -0) 📝 `src/engine/shared/macros/macro-engine.ts` (+21 -5) ➕ `src/engine/shared/time/timezone.ts` (+122 -0) 📝 `src/features/runtime/generation/hooks/use-generate.ts` (+9 -0) </details> ### 📄 Description ## Why this change Closes #1356. The legacy generation flow shipped the browser's resolved IANA timezone (`userTimeZone`) into prompt assembly so prompt-time macros (`{{date}}`, `{{time}}`, `{{datetime}}`, `{{isotime}}`, `{{weekday}}`) and any prompt code relying on local date context resolved in the user's local frame. The refactor's macro engine resolves `{{date}}` / `{{datetime}}` / `{{isotime}}` from `Date.prototype.toISOString` and so produces UTC, which silently drifts a calendar day for users west of UTC late at night and east of UTC early in the morning. No timezone string flows from the UI into the engine at all today. ## What changed - `src/engine/shared/time/timezone.ts` (new): IANA-tz normalizer plus zoned-parts/date/time/weekday/ISO-with-offset helpers built on `Intl.DateTimeFormat`. The normalizer trims, length-caps to 100, and probes the value via `Intl.DateTimeFormat` so invalid strings drop quietly to `undefined`. - `src/engine/shared/macros/macro-engine.ts`: adds `MacroContext.timeZone?: string` and routes `{{date}} / {{time}} / {{datetime}} / {{isotime}} / {{weekday}}` through the new helpers. All helpers fall back to the host machine's local zone via `Intl` when no zone is provided, so a caller that skips the input plumbing never lands in UTC. - `src/engine/generation/start-generation.ts`: adds `userTimeZone?: string` to `StartGenerationInput`. - `src/engine/generation/prompt-assembly.ts`: `resolvePromptTimeZone` resolves in preference order **persisted `chat.metadata.promptTimeZone` → caller-supplied `request.userTimeZone` → live `Intl.DateTimeFormat().resolvedOptions().timeZone`**. The live fallback is the load-bearing piece — engine code runs in the user's Tauri webview, so `Intl` always reflects the user's OS. This means every `startGeneration` entry point (the chat hook here, the game-turn service, the background-autonomous chat hook, and `previewGenerationPrompt`) resolves macros in the user's local frame without per-callsite plumbing. - `src/features/runtime/generation/hooks/use-generate.ts`: resolves the live browser timezone via `Intl.DateTimeFormat().resolvedOptions().timeZone` and threads it into the `startGeneration` input. With the engine-side fallback in place this is explicit-intent rather than load-bearing; it stays for clarity and to keep an override surface for future non-default callers (e.g. remote runtime). Two commits: 1. **Propagate user timezone into generation prompt assembly** — initial fix, plumbed tz through the chat-message generation path. 2. **Address review: centralize timezone fallback so all generation paths inherit** — moves the live-host fallback into `resolvePromptTimeZone` so game-mode and background-autonomous callers (which build their own `startGeneration` args without `userTimeZone`) and the prompt-preview UI all pick up the fix automatically. Also fixes `formatZonedIsoDateTime(date, undefined)` returning UTC (it now matches the host-local behavior of the other helpers) and extends the macro-engine fallback test with a `{{datetime}}` assertion. ## Verification - `pnpm check` ✅ (architecture / dependency-cruiser, frontend `tsc -b`, `cargo check`, docs) - `pnpm test` ✅ (21 suites / 106 tests, including 6 Vitest specs for the macro engine that pin both the zoned-tz case and the host-local fallback) - Browser-tier repro spec exercised against `pnpm dev` confirms RED → GREEN: pre-fix the macro engine returned `2026-05-27` (UTC) for an instant that is `2026-05-26` in LA-local; post-fix it returns the LA-local date. - Smoked end-to-end in `pnpm tauri dev`: `{{date}}` / `{{time}}` / `{{weekday}}` now render in the user's local zone in real chat output, and a chat reply that includes those macros resolves correctly under normal usage. ## Risks called out - **Default macro behavior shift**: previous `{{date}}` returned UTC, new default returns host-local. This is the bug being fixed, but it does change observable output for any preset that depended on the old UTC behavior. Couldn't find such a preset in the repo. - **`chat.metadata.promptTimeZone` precedence**: persisted overrides on legacy chats win over the live browser value. Intentional — matches the legacy server's `chatMeta.promptTimeZone ?? input.userTimeZone` order. Normalization rejects invalid stored strings so they fall through to the live host zone rather than producing UTC. - **Mode parity**: change is in mode-neutral prompt assembly + macro engine + engine-level fallback, so chat / roleplay / game / preview / background-autonomous all pick up the same fix in one path. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:12:46 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5050
No description provided.