Skip to content

feat(gateway): fixed mode for the env clock, each agent tool call costs set task time, 60 s by default (FD-7183) - #1003

Open
sarah-chitty-1 wants to merge 9 commits into
mainfrom
sarahchitty/fd-7183-work-clock
Open

sarah-chitty-1 wants to merge 9 commits into
mainfrom
sarahchitty/fd-7183-work-clock

Conversation

@sarah-chitty-1

@sarah-chitty-1 sarah-chitty-1 commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

FD-7183. Today an always_on world clock either runs on wall time during the agent's turns, so provider latency costs in-world time, or it is frozen, so the agent's work costs no time at all. This PR adds a third clock mode, fixed: the clock is frozen, and each tool call the agent makes costs a set amount of in-world time, 60 s by default. The same work costs the same in-world time on every model and provider.

A task picks it the way it picks frozen today: "clock_mode": "fixed" on its sync_env_clock step, or on set_env_clock_rate. Fixed mode means a rate of 0, and a nonzero rate with it is an error. The charge defaults to 60 s; a task can set its own with fixed_time_increment_on_tool_call_seconds on the same step (a number above 0, at most a day, and only with fixed mode). The gateway takes it as seconds_per_tool_call when the mode turns on. Tasks without clock_mode behave exactly as before, and their step JSON is unchanged byte for byte.

How it works

  • Who is charged. prompt_agent opens a work window naming its agent's role around the conversation. always_on opens one around each turn, nudges included. Only calls inside an open window, from a role it names, are charged. Setup, verifiers, collect_artifacts, user sim, contacts, trigger actions and post-turn hooks are never charged. An agent call outside its window is logged as fixed_clock_outside_window.
  • The world catches up before the agent sees an answer. After a charged call, the gateway settles all in-world work runnable at the new time before it answers: time triggers the charge made due, deferred calls and the fires they start, fires the call itself provoked, and fires from parallel calls. Work set for later in-world time is not waited for. The barrier wait and this hold share one budget, the larger of 30 s and the barrier timeout. Without the hold, a contact's reply landed at 17:01 or 17:06 depending only on the contact model's latency.
  • Free calls. get_time and stay_quiet cost nothing when gateway-native.
  • Leaving fixed mode. Any later set_time, set_rate or clear turns it off; advance keeps it on.
  • Fail closed. A window that can't be opened or closed raises WorkWindowError, an infra error, rather than letting verifier calls be charged. A window left open is closed before the next agent starts: one a failed step recorded, or one the gateway still shows under the same step id from an earlier attempt. If it still can't be closed, the agent doesn't start. prompt_agent and always_on refuse more than one env in fixed mode, since their clocks would drift apart.
  • Role-less agents. An agent with no role is charged as default and as cli, the role the generated MCP CLI sends without one.
  • Gateway.
    • PUT /clock/fixed turns the mode on (with seconds_per_tool_call) or off, and PUT /clock/fixed/window opens or closes windows. Both are control routes.
    • /clock/state gains a fixed block, and the clock/v1 card lists the fixed method.
    • Each charge writes a fixed_clock_charge trace event: time before and after, held fires and hold seconds. Gateway-native and /step calls also carry tool_call_event_id; MCP calls proxied to a backing server don't yet.
    • The /step post-turn-hook exemption now needs the control token.
  • always_on reports, never configures. It reads the mode from /clock/state and records:
    • the work done in each turn in agent_turn_end;
    • each wake's work in the judge's per-wake line, nudge included;
    • counts["fixed_clock"] in the summary.

Known limits

  • An agent deployed with no role shares default with header-less harness callers while its window is open. The gateway can't tell them apart. Outside the window those callers are never charged.
  • Work a harness does in its own built-in tools (local shell, memory tools) never reaches the gateway, so it costs nothing.
  • Hub workers on an older agent-env silently ignore clock_mode. Tasks should adopt it only after workers run this version.
  • The hold's real time counts toward the turn's real-time limits (always_on timeout_seconds, a client's tool timeout). A charged call can wait up to 30 s for a slow contact reply, so a slow contact model can still bring a turn to its limit sooner. Task time is not affected.

What every task sees

Tasks without clock_mode behave as before. A few additions show up for every task:

  • /clock/state has a fixed block (on: false when unused), and the clock/v1 card lists the fixed methods.
  • always_on records output_tokens on each agent_turn_end and nudge when the agent reports usage.
  • set_env_clock_rate records deploy_step_id whenever env_step_id is set.

How it was tested

Tests were written first:

  • 74 of the new tests fail on the first commit (cf8c06b), before the review fixes.
  • The 4 fail-closed tests fail on 5d61fab.
  • The first commit's 22 step tests failed on cefa82d.

All of them pass now.

pytest tst/unit/env/gateway/fixed_clock_test.py (+ socket) 78 passed
pytest tst/unit/task_step/clock_mode_test.py               35 passed
pytest tst/unit/task_step/test_always_on_fixed_clock.py    13 passed
clock_watch + control_token suites                         530 passed
pytest tst/unit/env/gateway/ tst/unit/task_step/ -n 8      6734 passed, 6 failed

The 6 failures (computer_exec_test ×4, test_mcp_cli_builder ×2) fail the same way on the base commit, because of macOS base64 output. The timing-sensitive tests passed 5 runs in a row under load. Ruff is clean.

4749a02 makes the charge a per-task setting with a 60 s default, as review asked. A further independent review found two gaps, fixed in 586df8b with 10 tests that failed first: a role-less agent's CLI calls went uncharged, and a window whose open or close failed was never cleaned up.

Four independent reviewers covered the gateway lifecycle, charging paths, task steps and test adequacy. Their findings are fixed in 5d61fab and b6ebd40. 970152f renames the mode from "work" to "fixed" with no behaviour change.

A mutation run of 72 mutants killed 69. The 3 survivors are equivalent or harmless:

  • a window check that can only matter while the mode is on;
  • prompt_agent's early refusal, which still happens before anything is sent;
  • close-then-freeze order, which matters only beside a second clock running at a real rate.

🤖 Generated with Claude Code

Confidence Score: 5/5RetriggerView in Greptile

The PR appears safe to merge, with a non-blocking error-response issue for oversized charge values.

FindingsFix All in Cursor

  1. P2 Oversized integers cause server errors ▶
Fix with agent prompt
### Issue 1
src/agent_env/env/gateway/constants.py:119
`fixed_seconds_per_call_error` calls `math.isfinite(value)` before checking the upper bound. A JSON integer such as `10**400` raises `OverflowError` here. Since `/clock/fixed` catches only `ClockError`, the caller gets a server error instead of the promised 400.

Call `math.isfinite` only for floats, as `_parse_rate` already does, so oversized integers reach the maximum check.

```suggestion
    if (isinstance(value, float) and not math.isfinite(value)) or value <= 0:
```

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds fixed clock mode, with a task-selected charge per agent tool call and a 60-second default.

  • The gateway charges eligible calls and settles due world work before answering.
  • Tasks can set a fixed time charge for each agent tool call.
  • Always-on turns record the agent's fixed-clock work.
  • Work-window failures remain ungraded infrastructure errors.

Diagram

sequenceDiagram
    participant Step
    participant Agent
    participant Gateway
    participant World
    Step->>Gateway: Open work window
    Agent->>Gateway: Call tool
    Gateway->>World: Run tool and wait for barriers
    Gateway->>Gateway: Check window and advance clock
    Gateway->>World: Wait for work due now
    Gateway-->>Agent: Return tool answer
    Step->>Gateway: Close window and read totals
Loading

Reviews (7) · Last reviewed commit: "feat(clock): a task sets fixed mode's ch..." · Reviewed by Greptile

sarah-chitty-1 and others added 3 commits December 3, 2026 17:02
…s clock_mode (FD-7183)

A task whose clock is frozen lived through none of the agent's work: a turn took no task time, and a
trigger due mid-turn waited for the next one. In work mode the clock stays at rate 0 and the gateway
moves it on by a fixed 60 virtual seconds (WORK_SECONDS_PER_CALL, not configurable) for each answered
tool call the agent makes, so thinking and model latency cost nothing and every model sees the same
world for the same work.

- sync_env_clock and set_env_clock_rate take clock_mode: "work" (unset keeps today's behaviour). It
  arms or sets rate 0 and turns work mode on (clock/v1 work); a nonzero rate with it is an error, and a
  gateway whose card lacks work is refused before the clock is touched.
- The gateway charges calls on the MCP path and the step API's /step paths (not a post-turn hook's),
  after their barriers, through Clock.advance_by, then holds the answer until the time triggers the
  charge made due have landed (TriggerEngine.settle_due). Each charge is a work_clock_charge event.
- Only the agent is charged: roles whose calls provoke triggers, less executors
  (TriggerEngine.agent_roles). Setup, verifier, user sim, contacts, trigger tool actions and hooks
  never are. get_time and stay_quiet are free.
- set_time, set_rate and clear leave work mode, so set_env_clock_rate 0 after the agent stops it.
- always_on gets no setting: it reads the mode from /clock/state and reports each turn's work in
  agent_turn_end, the judge's per-wake line and counts["work_clock"], and refuses an agent whose role
  work mode doesn't charge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s for all runnable world work (FD-7183)

Review fixes for cf8c06b.

Who is charged: the role guess is gone (TriggerEngine.agent_roles). A
call with no AgentEnv-Role header resolved to "default", which the guess
charged, so collect_artifacts, a verifier and run_code scripts moved the
clock; an agent on its own role with no matching watch_roles was never
charged. Now prompt_agent opens a work window (PUT /clock/work/window)
naming its agent's role around its conversation, and always_on one around
each turn, nudges included; a call is charged only while a window naming
its role is open. Late calls of the agent's role outside a window are
logged as work_clock_outside_window. Both steps refuse more than one env
in work mode. always_on's roles check is removed.

The hold: a charged call's answer now waits for all world work runnable
now (every trigger fire at work, due deferred calls, and what they set
going), not only the triggers that one charge made due; a fire waiting
on later task time is excluded. Barrier wait and hold share one budget.
Virtual stamps no longer depend on executor latency.

Lifecycle: set_time leaves work mode before registering get_time;
/clock/work and /clock/work/window are control routes; a post-turn hook's
/step call is free only with the control token; get_time/stay_quiet are
free only as the gateway's own tools; _change_clock wakes watchers and
followers, and a charge is recorded, even when what follows the advance
raises.

Reporting: work_clock_charge is logged before the hold with
tool_call_event_id, and work_clock_hold in a finally (shielded), so a
cancelled call leaves a trace. always_on reads each turn's work from its
window's close, after freezing, within attempts=2/timeout=10; logs
work_mode_off once; the judge line adds the nudge turn's work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…an infra error (FD-7183)

A window left open would charge calls made after the agent (a verifier's
or collect_artifacts' header-less reads, for an agent on the default
role) and move the world on under the grading. So a close that still
fails after its retry now fails closed with WorkWindowError, an
AgentTurnInfraError, which a retry_config can redo:

- prompt_agent raises it once the agent's work is done; when the agent
  itself raised, its error stays the one raised, the close failure logged
  and added as a note on it.
- always_on ends the run with it at the turn's end (two tries, 10 s
  each); a run being cancelled stays cancelled, the failure logged.
- A window that can't be opened (other than work mode found off, a 409)
  fails the same way before the agent starts.

Also adds the tests the mutation run asked for: a deferred call due
later doesn't hold a call; a barrier's own longer timeout is the call's
budget; a call answered after its window closed counts in a retried
close; a second cancel doesn't drop the hold's record; the window goes
on the deployment in work mode only; a 409 on open runs the agent with
no window; work mode ended between always_on turns is found at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sarah-chitty-1
sarah-chitty-1 requested a review from a team as a code owner December 4, 2026 02:33
Comment thread src/agent_env/env/gateway/triggers.py Resolved
Comment thread src/agent_env/env/gateway/gateway.py Resolved
The mode charges a fixed 60 s per answered tool call, so its public
surface now says so, with no "work" alias (nothing is released yet):

- clock_mode: "fixed"; the clock/v1 card method "fixed"; the route
  /clock/fixed (windows at /clock/fixed/window); the "fixed" block in
  /clock/state and in the mode's and windows' answers
- trace events fixed_clock_charge, fixed_clock_hold, fixed_clock_skipped,
  fixed_clock_outside_window and fixed_mode_off; the run record's
  counts["fixed_clock"] and settings["clock"]["fixed"]
- work_clock.py -> fixed_clock.py (FixedClock, FixedModeOff,
  parse_fixed_body), CLOCK_FIXED_MODE, CLOCK_FIXED_PATH,
  FIXED_SECONDS_PER_CALL, start_fixed, require_fixed, fixed_mode_envs;
  error messages, docstrings and AGENTS.md

The agent's work window keeps its name (open_work_window,
close_work_window, the work_window event, WorkWindowError), as does each
turn's "work" field, since they describe the agent working, not the mode.
No behaviour change; a test now shows "work" is refused as a clock_mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sarah-chitty-1 sarah-chitty-1 changed the title feat(gateway): work mode for the env clock, a fixed 60 s of task time per agent tool call (FD-7183) feat(gateway): fixed mode for the env clock, a fixed 60 s of task time per agent tool call (FD-7183) Dec 4, 2026
…window, and window failures go ungraded (FD-7183)

Three P1s from review on PR 1003:

- State checks escaped the hold: at_work() now counts a state trigger whose
  check is under way (evaluating, its task kept as eval_task), so
  _settle_world waits for the check and then for the fire it starts, within
  the same budget, however slow the check.
- A charge decided before the call's barrier wait was applied after it even
  if fixed mode had ended meanwhile. The call now carries a ticket (the
  clock's generation, fixed mode's session, the role's windows by the serial
  they were opened with), re-checked in the same step as the advance:
  set_rate, set_time, clear, turning it off, or the window closing (even
  reopened by the same id) skips the charge as fixed_clock_skipped.
- A WorkWindowError (a window that couldn't be opened or closed) is now
  recorded as agent_turn_infra_error with error_code
  agent_env.work_window_failed before always_on's finish() saves the record,
  so ended_on_infra_failure is true and verifiers leave the run ungraded.
  prompt_agent's saved response gets error_type infra_error and the same code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sarah-chitty-1 and others added 2 commits December 3, 2026 23:40
…ngraded, not scored 0 (FD-7183)

Review on PR 1003: prompt_agent saves its response as infra_error when its
work window can't be closed, but the verifiers' skip check only knew
always_on records, so the gateway's failure scored 0 against the agent.

ended_on_infra_failure now also takes the prompt response and recognizes
one saved with error_code agent_env.work_window_failed
(clock_mode.work_window_failed). The response check and the judge
(rubrics_verifier's _not_graded_reason, and always_on_agent_verifier's)
leave such a run ungraded. Any other infra_error a prompt_agent turn ended
on still scores 0 with a prompt_error row, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…windows left open (FD-7183)

Two bugs an independent review found in fixed mode.

A role-less agent's CLI calls were never charged. prompt_agent and
always_on opened the window for `agent.role or "default"`, but
deploy_agent sets AGENT_ENV_ROLE only for an agent with a role, so the
generated MCP CLI sends its own default, `cli`, which no window named:
nothing was charged and nothing logged. The window now names both
`default` and `cli` for such an agent (clock_mode.agent_window_roles,
used by both steps). The CLI's default role is unchanged, so no task
outside fixed mode changes. No harness-side caller (verifier,
collect_artifacts, user sim, contacts, triggers, post-turn hooks) sends
role `cli`; only the generated CLI does.

A window whose open or close failed was never cleaned up, and could go
on charging later callers, a retry's or a later step's verifiers
included:
- After a failed open, the step now sends a close for that window id
  before raising (a 404 is fine), since the gateway may have opened it
  and only its answer was lost.
- A window that may still be open (its close failed twice, or the close
  after a failed open failed too) is recorded in
  context.metadata["work_windows_left_open"], and the next step to open
  a window on that env closes it first, failing closed with
  WorkWindowError (before the agent starts) if that close fails.
- Before opening, the step also closes any window the gateway shows open
  under its own step id. A step never runs beside itself, so such a
  window is an earlier attempt's: this covers a retry whose rollback
  undid the failed attempt's record (undo_steps drops everything the
  span wrote), and a worker that died with its context.
- always_on's cancelled-turn path now records the run as an
  infrastructure failure (error_code agent_env.work_window_failed), as
  its other close-failure path does; the run stays cancelled.

Why not have the gateway close an overlapping window on open: two
windows naming the same role can be open at once on purpose. Task steps
with no dependency between them run concurrently, so two prompt_agent
steps whose agents share a role (two role-less agents, say) each hold a
window naming it, and the gateway is built for that (per-window serials
in Ticket, test_two_windows_each_count_their_own_roles). Replacing on
open would close the first step's window when the second opens, and once
the second closed, the first agent's later calls would go uncharged.
always_on nudges don't overlap (each turn's window closes before the
next opens), and WindowConflict only concerns re-opening the same id.

Left open: a retry whose span starts after the clock step but before the
failed step, with header-less tool calls in between, still runs those
under the earlier attempt's window until the step itself re-runs and
closes it (a span that re-runs the clock step ends every window anyway).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale

Copy link
Copy Markdown
Collaborator

Make the per-call charge a parameter instead of a constant

Rather than hard-coding FIXED_SECONDS_PER_CALL = 60, could fixed mode take the charge as a parameter?

  • Gateway: PUT /clock/fixed requires the charge whenever it turns the mode on, e.g. {"on": true, "seconds_per_tool_call": 60}, and refuses a turn-on without it.
    • Valid values are a finite number above 0; refuse booleans, NaN, 0 and negatives, plus a sane upper bound.
    • FixedClock stores it, cost() charges it, and the fixed block's existing seconds_per_call reports it.
    • The clock/v1 card schema marks it required when on is true.
  • Clock steps: sync_env_clock and set_env_clock_rate take fixed_time_increment_on_tool_call_seconds, a plain number of seconds defaulting to 60. The unit goes in the name, as in timeout_seconds and virtual_seconds_per_real_second.
    • It's accepted only with clock_mode: "fixed", and is an error otherwise.
    • It's serialized only when set, so existing step JSON stays unchanged.
    • The step passes it to the gateway and records it in clock_configurations beside clock_mode.
  • Tests:
    • the gateway refuses a missing or invalid value;
    • a charge uses the configured value;
    • the step's default is 60;
    • the step refuses the field without fixed mode;
    • to_dict leaves it out when unset.

Tasks get 60 s without writing anything, and a task that needs finer or coarser steps can say so.

sarah-chitty-1 and others added 2 commits December 4, 2026 19:01
Greptile P2: the two close calls repeated attempts=2, timeout=10 inline.
They now share WINDOW_CLOSE_ATTEMPTS and WINDOW_CLOSE_TIMEOUT_SECONDS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s the default (FD-7183)

Fixed mode moved the frozen clock on by a constant FIXED_SECONDS_PER_CALL (60 s) for
each charged tool call. The charge is now a parameter, as the review asked.

Gateway:
- PUT /clock/fixed requires seconds_per_tool_call to turn the mode on, and answers 400
  without it. The value must be a finite number above 0 and at most
  FIXED_MAX_SECONDS_PER_CALL (86400, a day). Booleans, NaN, infinity, 0, negatives and
  non-numbers are refused. Turning the mode off takes no charge, and a request that
  sends one is refused too.
- FixedClock stores the charge and cost() charges it. The state's fixed block reports
  it as seconds_per_call, which is null while the mode is off.
- The clock/v1 card's fixed request schema marks seconds_per_tool_call as required
  when on is true (if/then), and forbids it when on is false.
- One validator, constants.fixed_seconds_per_call_error, is used by the gateway and by
  the steps.

Steps:
- sync_env_clock and set_env_clock_rate take fixed_time_increment_on_tool_call_seconds.
  Left unset it means FIXED_SECONDS_PER_CALL (60). It is checked at load the way the
  gateway checks it, and accepted only with clock_mode "fixed".
- to_dict writes it only when set, so existing step JSON is unchanged.
- The step sends it to the gateway (start_fixed) and records it in
  clock_configurations beside clock_mode.

Docs and docstrings no longer say the charge is a fixed 60 s. They now say it
defaults to 60 s and a task may set it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sarah-chitty-1 sarah-chitty-1 changed the title feat(gateway): fixed mode for the env clock, a fixed 60 s of task time per agent tool call (FD-7183) feat(gateway): fixed mode for the env clock, each agent tool call costs set task time, 60 s by default (FD-7183) Dec 5, 2026
@sarah-chitty-1

Copy link
Copy Markdown
Collaborator Author

@earakely-scale Done in 4749a02, as you described:

  • Gateway: PUT /clock/fixed needs seconds_per_tool_call to turn the mode on.
    • It must be a finite number above 0 and at most 86400. Booleans, NaN, infinity, 0 and negatives are refused with 400.
    • The card schema requires it when on is true. Sending it with on: false is also refused.
    • The fixed block reports it as seconds_per_call, which is null while the mode is off.
  • Steps: sync_env_clock and set_env_clock_rate take fixed_time_increment_on_tool_call_seconds, defaulting to 60.
    • It's an error without clock_mode: "fixed", and it's checked the same way at load.
    • to_dict writes it only when set. The step sends it to the gateway and records the value used in clock_configurations.
  • Tests: all the cases you listed, plus a check that the card schema accepts exactly what the gateway accepts. They failed before the change.

FIXED_MAX_SECONDS_PER_CALL, never a boolean. The gateway's /clock/fixed checks it, and the clock steps at load."""
if isinstance(value, bool) or not isinstance(value, (int, float)):
return f"must be a number of seconds, got {value!r}"
if not math.isfinite(value) or value <= 0:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Oversized integers cause server errors

fixed_seconds_per_call_error calls math.isfinite(value) before checking the upper bound. A JSON integer such as 10**400 raises OverflowError here. Since /clock/fixed catches only ClockError, the caller gets a server error instead of the promised 400.

Call math.isfinite only for floats, as _parse_rate already does, so oversized integers reach the maximum check.

Suggested change
if not math.isfinite(value) or value <= 0:
if (isinstance(value, float) and not math.isfinite(value)) or value <= 0:
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agent_env/env/gateway/constants.py
Line: 119

Comment:
**Oversized integers cause server errors**

`fixed_seconds_per_call_error` calls `math.isfinite(value)` before checking the upper bound. A JSON integer such as `10**400` raises `OverflowError` here. Since `/clock/fixed` catches only `ClockError`, the caller gets a server error instead of the promised 400.

Call `math.isfinite` only for floats, as `_parse_rate` already does, so oversized integers reach the maximum check.

```suggestion
    if (isinstance(value, float) and not math.isfinite(value)) or value <= 0:
```

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Cursor Fix in Claude Code Fix in Codex

This branch has not been deployed

No deployments
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.

2 participants