Skip to content

fix(agent): add authoritative UTC run context - #3517

Merged
kojiwakayama merged 5 commits into
mainfrom
fix/issue-17-authoritative-utc-context
Aug 9, 2026
Merged

fix(agent): add authoritative UTC run context#3517
kojiwakayama merged 5 commits into
mainfrom
fix/issue-17-authoritative-utc-context

Conversation

@kojiwakayama

@kojiwakayama kojiwakayama commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Closes veryfront/veryfront-issue-inbox#17

Summary

  • capture one immutable server UTC snapshot for every generate, stream, and respond run
  • append the reserved authoritative runtime context after refreshed system instructions on every provider step
  • expose the exact snapshot in response metadata, stream diagnostics, and trace attributes
  • cover fixed-clock scheduled and browser streaming paths and document the runtime contract

Verification

  • deno test --no-check --allow-all src/agent/runtime/refresh.test.ts src/agent/factory-call-context.test.ts
  • deno test --no-check --allow-all --parallel src/agent (1140 passed)
  • deno task verify:quick

Summary by CodeRabbit

  • New Features

    • Added a consistent UTC runtime snapshot for each agent run.
    • Included run start time and current UTC date in prompts, response metadata, and streaming events.
    • Ensured scheduled refresh steps retain the same runtime snapshot throughout a run.
    • Added runtime context events for AG-UI integrations.
  • Bug Fixes

    • Clarified that browser environment context does not override the server-authored runtime snapshot.
  • Documentation

    • Updated agent guidance and API documentation with runtime context behavior, precedence, and availability.

@kojiwakayama
kojiwakayama requested a review from kwakayama as a code owner August 9, 2026 16:17
@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The agent runtime now captures one immutable UTC snapshot per run. It injects that snapshot into provider prompts, tracing, streaming events, and response metadata. Authored runtime blocks are replaced, while browser environment context remains available for display.

Changes

Per-run UTC runtime context

Layer / File(s) Summary
Runtime context contract
src/agent/runtime/run-runtime-context.ts, src/agent/runtime/run-runtime-context.test.ts
Adds immutable UTC context capture, reserved prompt-block rendering, authored-block removal, response metadata composition, and focused tests.
Runtime propagation
src/agent/runtime/index.ts, src/agent/types.ts, docs/api-reference/veryfront/agent.md, docs/guides/agents.md
Captures context for generate and stream runs, passes it through agent loops, adds it to provider prompts and response data, and documents its precedence and exposure.
Runtime context validation
src/agent/factory-call-context.test.ts, src/agent/runtime/provider-transport.test.ts, src/agent/runtime/refresh.test.ts, src/agent/ag-ui/browser-encoder.test.ts
Tests authoritative prompt replacement, environment-context ordering, provider parity, streamed event encoding, and snapshot stability across refreshed steps.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AgentRuntime
  participant AgentRunRuntimeContext
  participant AgentLoop
  participant Provider
  participant Response
  AgentRuntime->>AgentRunRuntimeContext: capture UTC snapshot
  AgentRuntime->>AgentLoop: pass snapshot to generate or stream loop
  AgentLoop->>Provider: send prompt with authoritative runtime_context
  Provider-->>AgentLoop: return model response
  AgentLoop->>Response: attach runtimeContext metadata
Loading

Possibly related PRs

Suggested reviewers: kwakayama

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: adding authoritative UTC runtime context for agent runs.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-17-authoritative-utc-context

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
docs/guides/agents.md (1)

387-395: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Make the documentation example standalone.

The example calls agent() without importing it. A reader cannot paste this example into a blank module.

Proposed fix
+import { agent } from "veryfront/agent";
+
 export default agent({

As per coding guidelines, “Code examples must be complete, copyable, and safe to paste.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/guides/agents.md` around lines 387 - 395, Add the missing import for the
`agent` symbol used by the documentation example, using the package/module’s
established import path so the snippet is standalone and copyable without
changing its behavior.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/agent/runtime/refresh.test.ts`:
- Line 2325: Update the streaming refresh test around the observedSystems
assertion to advance FakeTime between stream steps, retain the runtime context
in the assertions, and verify one context block per prompt containing the
original run_started_at_utc value.

In `@src/agent/runtime/run-runtime-context.ts`:
- Around line 25-40: Update removeReservedRuntimeContextBlocks to recognize
runtime-context opening tags with whitespace or attributes, not only the exact
RUNTIME_CONTEXT_OPEN_TAG; remove each matched block through its closing tag, or
through the end of the result when unclosed. Add regression tests covering
attributed/whitespace opening tags and unclosed tags while preserving existing
exact-tag behavior.

In `@src/agent/types.ts`:
- Around line 171-172: Update the public description for environmentContext in
src/agent/types.ts lines 171-172 to directly instruct readers to use it for
host-supplied browser display facts, while stating it cannot replace the
server-authored UTC runtime_context snapshot. Regenerate or update the
corresponding API description in docs/api-reference/veryfront/agent.md line 144
with the same wording and direct, concise, present-tense style.

---

Outside diff comments:
In `@docs/guides/agents.md`:
- Around line 387-395: Add the missing import for the `agent` symbol used by the
documentation example, using the package/module’s established import path so the
snippet is standalone and copyable without changing its behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 23b427b2-7841-4a7b-9ade-51a14cf3763f

📥 Commits

Reviewing files that changed from the base of the PR and between a0b1c9b and 8b36825.

📒 Files selected for processing (8)
  • docs/api-reference/veryfront/agent.md
  • docs/guides/agents.md
  • src/agent/factory-call-context.test.ts
  • src/agent/runtime/index.ts
  • src/agent/runtime/provider-transport.test.ts
  • src/agent/runtime/refresh.test.ts
  • src/agent/runtime/run-runtime-context.ts
  • src/agent/types.ts

Comment thread src/agent/runtime/refresh.test.ts
Comment thread src/agent/runtime/run-runtime-context.ts Outdated
Comment thread src/agent/types.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8b368255e3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agent/runtime/index.ts Outdated
@kojiwakayama

Copy link
Copy Markdown
Contributor Author

Addressed the CodeRabbit outside-diff documentation finding in fc0f380: the dynamic system prompt example now imports agent from veryfront/agent, so the snippet is standalone and copyable.

@kojiwakayama
kojiwakayama added this pull request to the merge queue Aug 9, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Aug 9, 2026
…e-17-authoritative-utc-context

# Conflicts:
#	docs/api-reference/veryfront/agent.md
#	src/agent/runtime/index.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/agent/runtime/run-runtime-context.test.ts (1)

9-11: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Strengthen the assertions for the canonical snapshot.

The tests do not verify context.currentTimeUtc, context.currentDateUtc, or context.runStartedAtUtc. They also only remove one stale date from the assertions. A regression can retain source="user" or other authored fields, or emit an empty runtime block, and still pass.

Add a stale sentinel and assert that the authored opening tag and content are absent. Assert the canonical UTC values and that the fixed context is immutable.

Suggested assertions
   const context = captureAgentRunRuntimeContext(
     new Date("2026-07-19T07:30:00.000Z"),
   );
+
+  it("captures the fixed UTC snapshot", () => {
+    assertEquals(context.currentTimeUtc, "2026-07-19T07:30:00.000Z");
+    assertEquals(context.currentDateUtc, "2026-07-19");
+    assertEquals(context.runStartedAtUtc, "2026-07-19T07:30:00.000Z");
+    assertEquals(Object.isFrozen(context), true);
+  });

Also applies to: 20-28, 33-40

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/agent/runtime/run-runtime-context.test.ts` around lines 9 - 11,
Strengthen the canonical snapshot tests around captureAgentRunRuntimeContext by
adding a stale sentinel and asserting authored opening-tag attributes and
content are removed. Verify context.currentTimeUtc, context.currentDateUtc, and
context.runStartedAtUtc contain the expected UTC values, and assert the fixed
context is immutable, including that no empty runtime block is emitted.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/agent/runtime/run-runtime-context.test.ts`:
- Around line 9-11: Strengthen the canonical snapshot tests around
captureAgentRunRuntimeContext by adding a stale sentinel and asserting authored
opening-tag attributes and content are removed. Verify context.currentTimeUtc,
context.currentDateUtc, and context.runStartedAtUtc contain the expected UTC
values, and assert the fixed context is immutable, including that no empty
runtime block is emitted.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d3ee6f5b-f1fd-45ae-a94b-e74f3a01bb47

📥 Commits

Reviewing files that changed from the base of the PR and between 8b36825 and 55f5551.

📒 Files selected for processing (10)
  • docs/api-reference/veryfront/agent.md
  • docs/guides/agents.md
  • src/agent/ag-ui/browser-encoder.test.ts
  • src/agent/factory-call-context.test.ts
  • src/agent/runtime/index.ts
  • src/agent/runtime/provider-transport.test.ts
  • src/agent/runtime/refresh.test.ts
  • src/agent/runtime/run-runtime-context.test.ts
  • src/agent/runtime/run-runtime-context.ts
  • src/agent/types.ts
💤 Files with no reviewable changes (1)
  • docs/api-reference/veryfront/agent.md
🚧 Files skipped from review as they are similar to previous changes (7)
  • src/agent/types.ts
  • src/agent/runtime/run-runtime-context.ts
  • src/agent/runtime/refresh.test.ts
  • src/agent/runtime/provider-transport.test.ts
  • src/agent/factory-call-context.test.ts
  • src/agent/runtime/index.ts
  • docs/guides/agents.md

@kojiwakayama
kojiwakayama added this pull request to the merge queue Aug 9, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 9, 2026
@kojiwakayama
kojiwakayama added this pull request to the merge queue Aug 9, 2026
Merged via the queue into main with commit 2e96273 Aug 9, 2026
31 checks passed
@kojiwakayama
kojiwakayama deleted the fix/issue-17-authoritative-utc-context branch August 9, 2026 22:08
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.

1 participant