Skip to content

Python: Add agent-framework-tenki (Tenki-backed CodeAct provider)#7312

Open
Patricio-Filice-Luxor wants to merge 6 commits into
microsoft:mainfrom
Patricio-Filice-Luxor:feat/add-tenki-codeact-integration
Open

Python: Add agent-framework-tenki (Tenki-backed CodeAct provider)#7312
Patricio-Filice-Luxor wants to merge 6 commits into
microsoft:mainfrom
Patricio-Filice-Luxor:feat/add-tenki-codeact-integration

Conversation

@Patricio-Filice-Luxor

Copy link
Copy Markdown

Motivation & Context

Adds a third CodeAct backend, agent-framework-tenki, wrapping Tenki Sandbox (managed Linux micro-VMs) behind the same *CodeActProvider / *ExecuteCodeTool shape as agent-framework-hyperlight and agent-framework-monty. Unlike the existing in-process backends it provides a remote, isolated, real Linux environment: pip/apt installs, subprocesses, and a persistent filesystem across calls.

Full rationale, backend comparison, and scope discussion: #7311

Description & Review Guide

  • Major changes: new alpha package python/packages/tenki/ (1.0.0a260722, PEP 561 typed) with TenkiCodeActProvider (run-scoped: fresh execute_code tool + sandbox per agent run, terminated in after_run so state never leaks across runs) and TenkiExecuteCodeTool (standalone: one sandbox reused until close()); a runnable sample; 66 unit + 4 opt-in integration tests; CI wiring.
  • Impact: additive only — not part of agent-framework[all] and no agent_framework.tenki lazy-loading shim (both deferred to beta promotion, per the alpha package policy). No existing package modified beyond PACKAGE_STATUS.md, workspace registration, and the CI workflow env.
  • Review focus: the sandbox lifecycle reconciliation and the run-scoped provider state contract (session state stores only the sandbox name — a plain JSON-serializable string; live tool handles stay on the provider so close() can reap runs that never reached after_run).

Sandbox lifecycle (highlights):

  • Lazy provision on the first execute_code call, reused per tool instance. Before each call the tool refreshes remote state: PAUSED / USER_SHUTDOWN → auto-resume, retried within a 120s poll budget (USER_SHUTDOWN links its pause snapshot asynchronously — verified live end-to-end); TERMINATED / TERMINATING → re-provision; refresh() failure → structured error, handle kept for retry.
  • Reconcile + sandbox.exec run under a single lock hold: close() serializes with in-flight execs, a failed terminate preserves the handle for retry, and the provider's close() attempts every leaked run tool and raises RuntimeError naming the survivors so a second close() retries exactly those.
  • Cost backstops: max_duration_seconds (default 900s) stops compute billing server-side even if the client process crashes; provider run-scoped sandboxes cap pause-snapshot retention at 1h (standalone keeps Tenki's 7-day default).

Implementation notes: sync SDK (tenki-sandbox>=0.4.0,<0.5) bridged via asyncio.to_thread + threading.Lock; typed CommandResult parsing (signal-killed processes correctly reported as failures); injected CodeAct instructions cover small-model footguns (print(...) requirement, subprocess for pip, no Jupyter magic); explicit constructor args — including empty string — always win over env fallbacks (TENKI_API_KEY, TENKI_PROJECT_ID, TENKI_WORKSPACE_ID); Tenki-specific options (snapshot_id, volumes, network policy, …) pass through extra_create_kwargs.

Tests: 66 hermetic unit tests through the public tool.invoke API (full reconcile matrix, kwargs forwarding, close semantics, provider run-scoping, CancelledError propagation); 4 opt-in integration tests against the live service, guarded on TENKI_API_KEY. Note for maintainers: the integration job needs secrets.TENKI_API_KEY and vars.TENKI_PROJECT_ID configured.

Out of scope for the alpha: host tool callbacks (the Tenki SDK has no bridge), dedicated constructor parameters for mounts/network/snapshots (reachable today via extra_create_kwargs), and a session-scoped sandbox mode.

Related Issue

Resolves #7311

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Patricio-Filice-Luxor and others added 4 commits July 20, 2026 22:31
Introduces agent-framework-tenki, a third code-executor backend that runs
Python inside a Tenki managed Linux microVM sandbox. Ships alongside the
existing hyperlight (WASM) and monty (in-process Rust) executors, and
mirrors their public API shape (TenkiCodeActProvider ContextProvider +
TenkiExecuteCodeTool FunctionTool).

Lifecycle: lazy provision, reuse-per-tool, and per-call reconciliation —
PAUSED sandboxes are transparently resumed, TERMINATED sandboxes are
replaced by a fresh provision.

Includes 32 hermetic unit tests plus one opt-in integration test guarded
on TENKI_API_KEY. Alpha-policy compliant: workspace uv sources only, no
core[all] extra, no lazy-loading shim.
- Retry sandbox resume within a 120s poll budget: USER_SHUTDOWN pause
  snapshots are linked asynchronously (~60s) and the server can revert
  an accepted resume, so single-shot resume failed live
- Keep run-scoped provider state JSON-serializable (store sandbox name
  in session state; live tool handles stay on the provider)
- Handle CommandResult diagnostics (ok/signal/reason/errno) directly
  and keep the sandbox handle when close/refresh fails so it can retry
- Run tool close under a single lock in a worker thread so a close
  during an in-flight reconcile cannot block the event loop
- Make provider.close() retryable: keep failed terminates in the
  live-tool set instead of dropping them up front
- Type __aexit__ signatures to match other resource-backed providers
- Pin tenki-sandbox>=0.4.0,<0.5, finite max_duration default (900s),
  document pause/terminate asymmetry on expiry
- Correct startup-latency docs to measured ~2s (was 10-30s) and
  clarify run-scoped vs standalone sandbox lifetime in the README
- Expand tests to 58 unit + 4 integration (resume retry, server revert,
  close/run serialization, retryable provider close, cancellation
  teardown, terminal-state re-provision); wire tenki into
  integration/merge CI workflows

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…-tenki

- Hold the sandbox lock across reconcile + exec so close() cannot
  terminate a sandbox while an exec is in flight
- Add pause_retention_seconds kwarg (tool + provider); run-scoped
  sandboxes default to 1h retention so orphans are GC'd server-side
- Provider close() raises RuntimeError after attempting all terminates,
  retaining failed handles for retry
- Harden the teardown integration test: assert provisioning succeeded
  and poll the live API until TERMINATED
- Clarify README max_duration None-semantics and pause retention

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
`snapshot_id` restores from a snapshot prepared beforehand with the Tenki
CLI/SDK — it does not snapshot the (already terminated) sandbox, so the
previous wording suggested an impossible workflow.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 24, 2026 19:56
@agent-framework-automation agent-framework-automation Bot added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Jul 24, 2026
@microsoft-github-policy-service

Copy link
Copy Markdown

@Patricio-Filice-Luxor please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"
Contributor License Agreement

Contribution License Agreement

This Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
and conveys certain license rights to Microsoft Corporation and its affiliates (“Microsoft”) for Your
contributions to Microsoft open source projects. This Agreement is effective as of the latest signature
date below.

  1. Definitions.
    “Code” means the computer software code, whether in human-readable or machine-executable form,
    that is delivered by You to Microsoft under this Agreement.
    “Project” means any of the projects owned or managed by Microsoft and offered under a license
    approved by the Open Source Initiative (www.opensource.org).
    “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any
    Project, including but not limited to communication on electronic mailing lists, source code control
    systems, and issue tracking systems that are managed by, or on behalf of, the Project for the purpose of
    discussing and improving that Project, but excluding communication that is conspicuously marked or
    otherwise designated in writing by You as “Not a Submission.”
    “Submission” means the Code and any other copyrightable material Submitted by You, including any
    associated comments and documentation.
  2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any
    Project. This Agreement covers any and all Submissions that You, now or in the future (except as
    described in Section 4 below), Submit to any Project.
  3. Originality of Work. You represent that each of Your Submissions is entirely Your original work.
    Should You wish to Submit materials that are not Your original work, You may Submit them separately
    to the Project if You (a) retain all copyright and license information that was in the materials as You
    received them, (b) in the description accompanying Your Submission, include the phrase “Submission
    containing materials of a third party:” followed by the names of the third party and any licenses or other
    restrictions of which You are aware, and (c) follow any other instructions in the Project’s written
    guidelines concerning Submissions.
  4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else
    for whom You are acting in making Your Submission, e.g. as a contractor, vendor, or agent. If Your
    Submission is made in the course of Your work for an employer or Your employer has intellectual
    property rights in Your Submission by contract or applicable law, You must secure permission from Your
    employer to make the Submission before signing this Agreement. In that case, the term “You” in this
    Agreement will refer to You and the employer collectively. If You change employers in the future and
    desire to Submit additional Submissions for the new employer, then You agree to sign a new Agreement
    and secure permission from the new employer before Submitting those Submissions.
  5. Licenses.
  • Copyright License. You grant Microsoft, and those who receive the Submission directly or
    indirectly from Microsoft, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license in the
    Submission to reproduce, prepare derivative works of, publicly display, publicly perform, and distribute
    the Submission and such derivative works, and to sublicense any or all of the foregoing rights to third
    parties.
  • Patent License. You grant Microsoft, and those who receive the Submission directly or
    indirectly from Microsoft, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license under
    Your patent claims that are necessarily infringed by the Submission or the combination of the
    Submission with the Project to which it was Submitted to make, have made, use, offer to sell, sell and
    import or otherwise dispose of the Submission alone or with the Project.
  • Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement.
    No additional licenses or rights whatsoever (including, without limitation, any implied licenses) are
    granted by implication, exhaustion, estoppel or otherwise.
  1. Representations and Warranties. You represent that You are legally entitled to grant the above
    licenses. You represent that each of Your Submissions is entirely Your original work (except as You may
    have disclosed under Section 3). You represent that You have secured permission from Your employer to
    make the Submission in cases where Your Submission is made in the course of Your work for Your
    employer or Your employer has intellectual property rights in Your Submission by contract or applicable
    law. If You are signing this Agreement on behalf of Your employer, You represent and warrant that You
    have the necessary authority to bind the listed employer to the obligations contained in this Agreement.
    You are not expected to provide support for Your Submission, unless You choose to do so. UNLESS
    REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, AND EXCEPT FOR THE WARRANTIES
    EXPRESSLY STATED IN SECTIONS 3, 4, AND 6, THE SUBMISSION PROVIDED UNDER THIS AGREEMENT IS
    PROVIDED WITHOUT WARRANTY OF ANY KIND, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY OF
    NONINFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
  2. Notice to Microsoft. You agree to notify Microsoft in writing of any facts or circumstances of which
    You later become aware that would make Your representations in this Agreement inaccurate in any
    respect.
  3. Information about Submissions. You agree that contributions to Projects and information about
    contributions may be maintained indefinitely and disclosed publicly, including Your name and other
    information that You submit with Your Submission.
  4. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and
    the parties consent to exclusive jurisdiction and venue in the federal courts sitting in King County,
    Washington, unless no federal subject matter jurisdiction exists, in which case the parties consent to
    exclusive jurisdiction and venue in the Superior Court of King County, Washington. The parties waive all
    defenses of lack of personal jurisdiction and forum non-conveniens.
  5. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and
    supersedes any and all prior agreements, understandings or communications, written or oral, between
    the parties relating to the subject matter hereof. This Agreement may be assigned by Microsoft.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a new Python alpha package, agent-framework-tenki, that implements a Tenki Sandbox–backed CodeAct backend. This extends the Agent Framework’s CodeAct provider/tool ecosystem with a remote, isolated Linux micro-VM execution environment (persistent filesystem across calls, subprocesses, package installs), alongside existing in-process backends.

Changes:

  • Introduces agent-framework-tenki package with TenkiCodeActProvider (run-scoped) and TenkiExecuteCodeTool (standalone, reusable sandbox).
  • Adds a runnable Tenki CodeAct sample and updates the CodeAct samples README to include the third backend.
  • Wires Tenki into the Python workspace/lockfile and includes its integration tests in CI workflows.

Reviewed changes

Copilot reviewed 13 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
python/uv.lock Registers agent-framework-tenki workspace member and locks tenki-sandbox dependency.
python/pyproject.toml Adds agent-framework-tenki to the Python workspace packages.
python/PACKAGE_STATUS.md Marks the new package as alpha.
python/samples/02-agents/context_providers/code_act/tenki_code_act.py New sample demonstrating Tenki-backed provider usage.
python/samples/02-agents/context_providers/code_act/README.md Updates sample matrix + install/run instructions to include Tenki.
python/packages/tenki/README.md Package documentation covering configuration, lifecycle, and Tenki-specific passthrough options.
python/packages/tenki/pyproject.toml New package metadata, dependencies, typing, lint/test configuration.
python/packages/tenki/LICENSE Adds MIT license file for the new package.
python/packages/tenki/agent_framework_tenki/py.typed Marks the package as typed (PEP 561).
python/packages/tenki/agent_framework_tenki/init.py Public exports for provider/tool + version wiring.
python/packages/tenki/agent_framework_tenki/_provider.py Implements run-scoped provider lifecycle (before_run/after_run/close).
python/packages/tenki/agent_framework_tenki/_execute_code_tool.py Implements sandbox lifecycle reconciliation + execute_code tool behavior.
python/packages/tenki/tests/tenki/test_tenki_codeact.py Unit + opt-in integration tests for tool/provider lifecycle and result parsing.
.github/workflows/python-merge-tests.yml Adds Tenki package tests to the misc integration test job; exports Tenki env vars.
.github/workflows/python-integration-tests.yml Same as above for scheduled/manual integration workflow.
Comments suppressed due to low confidence (3)

python/packages/tenki/tests/tenki/test_tenki_codeact.py:1373

  • These integration tests pass project_id=os.environ.get("TENKI_PROJECT_ID") directly. In GitHub Actions, an unset vars.TENKI_PROJECT_ID expands to an empty string, which then gets forwarded as project_id="" and can cause Tenki provisioning to fail even though project_id is optional (single-project API keys). Consider only passing project_id when it’s a non-empty string.
    project_id = os.environ.get("TENKI_PROJECT_ID")
    async with TenkiExecuteCodeTool(
        sandbox_name=f"agent-framework-ci-fs-{os.getpid()}",
        project_id=project_id,
        max_duration_seconds=300,

python/packages/tenki/tests/tenki/test_tenki_codeact.py:1392

  • These integration tests pass project_id=os.environ.get("TENKI_PROJECT_ID") directly. In GitHub Actions, an unset vars.TENKI_PROJECT_ID expands to an empty string, which then gets forwarded as project_id="" and can cause Tenki provisioning to fail even though project_id is optional (single-project API keys). Consider only passing project_id when it’s a non-empty string.
    project_id = os.environ.get("TENKI_PROJECT_ID")
    async with TenkiExecuteCodeTool(
        sandbox_name=f"agent-framework-ci-fail-{os.getpid()}",
        project_id=project_id,
        max_duration_seconds=300,

python/packages/tenki/tests/tenki/test_tenki_codeact.py:1422

  • This integration test passes project_id=os.environ.get("TENKI_PROJECT_ID") directly. In GitHub Actions, an unset vars.TENKI_PROJECT_ID expands to an empty string, which then gets forwarded as project_id="" and can cause Tenki provisioning to fail even though project_id is optional (single-project API keys). Consider only passing project_id when it’s a non-empty string.
    project_id = os.environ.get("TENKI_PROJECT_ID")
    unique_name = f"agent-framework-ci-teardown-{os.getpid()}"

    async with TenkiExecuteCodeTool(
        sandbox_name=unique_name,
        project_id=project_id,
        max_duration_seconds=300,
    ) as tool:

Comment thread python/packages/tenki/agent_framework_tenki/_execute_code_tool.py Outdated
Comment thread python/packages/tenki/tests/tenki/test_tenki_codeact.py Outdated
…t-integration

# Conflicts:
#	python/PACKAGE_STATUS.md
#	python/samples/02-agents/context_providers/code_act/README.md
CI systems expand unconfigured secrets/vars to "" (e.g. GitHub Actions
vars.TENKI_PROJECT_ID), which was forwarded to the Tenki SDK as
project_id=""/auth_token="" and failed provisioning in non-obvious ways.
Env fallbacks now normalize "" to unset; explicit constructor args keep
their documented precedence. Integration tests no longer pass an empty
project_id as an explicit arg. Addresses Copilot review on microsoft#7312.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Feature]: Add a CodeAct backend for remote isolated Linux micro-VM sandboxes

2 participants