Skip to content

FunctionManager reload can execute stale bytecode after same-size source edits #688

Open

Description

@jadelike-wine

FunctionManager.reload_functions() can continue executing a tool's old source after the file has been edited. The dynamic loader accepts a timestamp-based .pyc when the source has the same integer-second mtime and byte length. Two same-length saves within one second can therefore survive an explicit reload.

Reproduced against main a2ea4a263cebe9a7abd01ee3b5b0bf95698ed328 on Windows with Python 3.12.13:













import os

















import tempfile

















from pathlib import Path

















from utils.function_manager import FunctionManager



































with tempfile.TemporaryDirectory() as directory:

















    tool = Path(directory) / "tool.py"

















    tool.write_text("def value(): return 1\n", encoding="utf-8")

















    os.utime(tool, (1700000000, 1700000000))

















    manager = FunctionManager(directory)

















    assert manager.call_function("value") == 1

















    tool.write_text("def value(): return 2\n", encoding="utf-8")

















    os.utime(tool, (1700000000, 1700000000))

















    manager.reload_functions()

















    assert manager.call_function("value") == 2  # fails: still returns 1















The fixed timestamp makes the cache collision deterministic; ordinary same-second edits can have the same effect. Creating a new manager also accepts a stale existing bytecode cache.

A focused fix is to execute compiled current source in the module namespace already prepared by module_from_spec, rather than reading the timestamp-based bytecode cache. Three tests against the real module give 2 failed, 1 passed on main and 3 passed with this change; the control checks source encoding, function filtering, and removing a tool on reload. No model/provider calls are involved.

This concerns source freshness, independently of the dataclass/module-registration issue #681 and closed PR #682. Searches for FunctionManager, reload, and bytecode found no matching report or PR. Prepared with Codex assistance; the reproduction and tests were run locally.

Our implementation

PR: #689

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions