-
Notifications
You must be signed in to change notification settings - Fork 224
AI-382: keep sandbox credentials out of workflow history #1745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xumaple
wants to merge
4
commits into
main
Choose a base branch
from
maplexu/AI-382-sandbox-secret-refs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
80fd8cf
AI-382: keep sandbox credentials out of workflow history
xumaple da712f1
AI-382: add changelog entries for sandbox secret references
xumaple cb1900a
AI-382: rename SecretRef to TemporalWorkerEnvValue
xumaple bde449c
Merge branch 'main' into maplexu/AI-382-sandbox-secret-refs
xumaple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| .venv | ||
| .mypy_cache | ||
| __pycache__ | ||
| /build | ||
| /dist | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
temporalio/contrib/openai_agents/sandbox/_temporal_worker_env_value.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Sandbox environment value resolved from the Temporal Worker's environment.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from typing import Literal | ||
|
|
||
| from agents.sandbox.manifest import EnvValue | ||
|
|
||
| from temporalio import workflow | ||
| from temporalio.exceptions import ApplicationError | ||
|
|
||
|
|
||
| class TemporalWorkerEnvValue(EnvValue): | ||
| """A sandbox environment variable whose value is read on the Temporal Worker. | ||
|
|
||
| .. warning:: | ||
| This class is experimental and may change in future versions. | ||
| Use with caution in production environments. | ||
|
|
||
| Use it wherever a sandbox environment value should come from the worker's | ||
| environment rather than being written into the manifest. It carries the name | ||
| of the variable, and the worker reads the value when the sandbox environment | ||
| is needed. Only the name is recorded in workflow history. | ||
|
|
||
| :: | ||
|
|
||
| from agents.sandbox import Manifest | ||
| from agents.sandbox.manifest import Environment | ||
|
|
||
| from temporalio.contrib.openai_agents import TemporalWorkerEnvValue | ||
|
|
||
| manifest = Manifest( | ||
| environment=Environment( | ||
| value={ | ||
| "OPENAI_API_KEY": TemporalWorkerEnvValue(key="OPENAI_API_KEY"), | ||
| "REGION": "us-west-2", | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| Set the variable on every worker that runs sandbox activities. The two | ||
| names need not match: ``{"OPENAI_API_KEY": TemporalWorkerEnvValue(key="PROD_KEY")}`` | ||
| reads ``PROD_KEY`` on the worker and sets ``OPENAI_API_KEY`` inside the | ||
| sandbox. | ||
| """ | ||
|
|
||
| type: Literal["temporal.worker_env_value"] = "temporal.worker_env_value" # type: ignore[assignment] | ||
| """Discriminator for this environment value type.""" | ||
|
|
||
| key: str | ||
| """Name of the environment variable to read on the worker.""" | ||
|
|
||
| async def resolve(self) -> str: | ||
| """Return the value read from the worker's environment. | ||
|
|
||
| Raises: | ||
| ApplicationError: If :py:attr:`key` is unset or empty, or if called | ||
| from workflow code. Non-retryable, with | ||
| ``type="TemporalWorkerEnvValueUnresolved"``. | ||
| """ | ||
| if workflow.in_workflow(): | ||
| raise ApplicationError( | ||
| "TemporalWorkerEnvValue.resolve() must run on a worker, not in workflow " | ||
| "code: it reads the process environment, which is non-deterministic on " | ||
| "replay and would pull the value into workflow state.", | ||
| type="TemporalWorkerEnvValueUnresolved", | ||
| non_retryable=True, | ||
| ) | ||
| value = os.environ.get(self.key) | ||
| if not value: | ||
| raise ApplicationError( | ||
| f"TemporalWorkerEnvValue environment variable {self.key!r} is not set, " | ||
| "or is empty, in the worker process environment.", | ||
| type="TemporalWorkerEnvValueUnresolved", | ||
| non_retryable=True, | ||
| ) | ||
| return value |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.