Skip to content

[lf.agentic.Session] Keep results that come in after an action's deadline. - #788

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_1008473954
Oct 28, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
test_1008473954

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

[lf.agentic.Session] Keep results that come in after an action's deadline.

  • While the session calls its event handler, it sets a thread-local flag
    (Session._running_event_handler()), and _log skips the deadline check
    under that flag. The flag is cleared again for the body of every action,
    including one that a handler starts. So only logs that event handlers write
    themselves, such as those of lf.agentic.SessionLogging, are exempt: a log
    from an action (session.info(...) in call) is still a cancellation
    point, as are query starts, action starts and concurrent_map, even when a
    handler makes them. The flag covers every handler and leaves the handler
    API unchanged. It is per thread, like the current action, so a handler
    running on one thread does not exempt the logs that actions write on other
    threads.
  • lf.query marks a successful query completed in the else clause of its
    try. An error raised there, for example by a track_queries end
    callback, propagates as itself instead of
    AssertionError('Query is already completed.'), and the query is marked
    completed once.
  • Session.track_action restores the parent action and execution in a
    finally, so they are restored even when on_action_end raises.
  • Session.concurrent_map turns a TimeoutError that escapes after the
    action's deadline into ActionTimeoutError, as its docstring now says. Its
    per-item timeout is capped at the action's remaining time, so after the
    deadline such a timeout means that the action ran out of time. Before, this
    happened only as a side effect of the deadline check in handler logs
    (SessionTest.test_max_execution_time relies on it); a TimeoutError
    within the budget is still raised as is.

Behavior change: in a session with a time budget, a model reply or an action
result that comes in after the deadline is now kept, logged and seen by every
handler, and an error raised after the deadline surfaces as itself instead of
being replaced by ActionTimeoutError (except the concurrent_map timeout
above). The action's next log, query, sub-action or concurrent_map after the
deadline still raises ActionTimeoutError. In any session, an error raised in
an lf.query end callback, including a session's on_query_end handler, now
reaches the caller as itself instead of AssertionError, and the current action
is restored even when on_action_end raises.

@copybara-service
copybara-service Bot force-pushed the test_1008473954 branch 25 times, most recently from 639bb0b to 828df1d Compare October 28, 2026 18:29
…adline.

*   While the session calls its event handler, it sets a thread-local flag
    (`Session._running_event_handler()`), and `_log` skips the deadline check
    under that flag. The flag is cleared again for the body of every action,
    including one that a handler starts. So only logs that event handlers write
    themselves, such as those of `lf.agentic.SessionLogging`, are exempt: a log
    from an action (`session.info(...)` in `call`) is still a cancellation
    point, as are query starts, action starts and `concurrent_map`, even when a
    handler makes them. The flag covers every handler and leaves the handler
    API unchanged. It is per thread, like the current action, so a handler
    running on one thread does not exempt the logs that actions write on other
    threads.
*   `lf.query` marks a successful query completed in the `else` clause of its
    `try`. An error raised there, for example by a `track_queries` end
    callback, propagates as itself instead of
    `AssertionError('Query is already completed.')`, and the query is marked
    completed once.
*   `Session.track_action` restores the parent action and execution in a
    `finally`, so they are restored even when `on_action_end` raises.
*   `Session.concurrent_map` turns a `TimeoutError` that escapes after the
    action's deadline into `ActionTimeoutError`, as its docstring now says. Its
    per-item timeout is capped at the action's remaining time, so after the
    deadline such a timeout means that the action ran out of time. Before, this
    happened only as a side effect of the deadline check in handler logs
    (`SessionTest.test_max_execution_time` relies on it); a `TimeoutError`
    within the budget is still raised as is.

Behavior change: in a session with a time budget, a model reply or an action
result that comes in after the deadline is now kept, logged and seen by every
handler, and an error raised after the deadline surfaces as itself instead of
being replaced by `ActionTimeoutError` (except the `concurrent_map` timeout
above). The action's next log, query, sub-action or `concurrent_map` after the
deadline still raises `ActionTimeoutError`. In any session, an error raised in
an `lf.query` end callback, including a session's `on_query_end` handler, now
reaches the caller as itself instead of `AssertionError`, and the current action
is restored even when `on_action_end` raises.

PiperOrigin-RevId: 1009059402
@copybara-service
copybara-service Bot merged commit deae0c9 into main Oct 28, 2026
6 checks passed
@copybara-service
copybara-service Bot deleted the test_1008473954 branch October 28, 2026 18:36
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