Repository navigation
feat: provide trusted runtime context for structured tool review #178
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
nathankw
wants to merge
2
commits into
feat/opa-tool-policy-outcomes
Choose a base branch
from
feat/tool-runtime-review-context
base: feat/opa-tool-policy-outcomes
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| <!-- | ||
| SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| --> | ||
|
|
||
| # Host-reported runtime context for tool review | ||
|
|
||
| An application may show the model a logical workspace while executing a tool | ||
| against a different filesystem root. Nomos can accept an explicit, operator-trusted | ||
| mapping so reviewers can compare the model's declared target with the actual | ||
| operation. The feature is disabled by default. | ||
|
|
||
| ## Why a mapping matters | ||
|
|
||
| Suppose the model requests a new directory under `/workspace/project`, and the | ||
| host resolves that workspace to `/srv/workspaces/project` before review: | ||
|
|
||
| ```yaml | ||
| target_resource: /workspace/project/reports | ||
| tool_call: | ||
| tool_name: files.run | ||
| arguments: | ||
| script: mkdir reports | ||
| directory: /srv/workspaces/project | ||
| ``` | ||
|
|
||
| Without a mapping, a reviewer sees different target and execution paths. It | ||
| should not assume that two similar-looking directories are the same resource. | ||
| With admitted runtime context, it can see that the application reports the | ||
| logical and execution roots as corresponding: | ||
|
|
||
| ```yaml | ||
| tool_call: | ||
| runtime_context: | ||
| version: 1 | ||
| source: example.application | ||
| runtime: | ||
| working_directory: /srv/workspaces/project | ||
| workspace_mappings: | ||
| - logical_root: /workspace/project | ||
| execution_root: /srv/workspaces/project | ||
| ``` | ||
|
|
||
| This explains the location without rewriting the model's `target_resource` or | ||
| changing the actual tool arguments. It does not require a tool argument named | ||
| `cwd`: the application reports the initial working directory explicitly. | ||
|
|
||
| ## Trust is configured at the Council | ||
|
|
||
| Add an issuer to the Council's `council.yaml` only after establishing that the | ||
| application validates its runtime facts: | ||
|
|
||
| ```yaml | ||
| require_auth: true | ||
| runtime_context: | ||
| trusted_hosts: | ||
| - agent_id: example-agent | ||
| source: example.application | ||
| tools: [files.run] | ||
| ``` | ||
|
|
||
| The default trust list is empty; the shipped configuration shows the optional | ||
| section as a commented example. Supplying a | ||
| `source` string does not establish trust. The service requires all three matches: | ||
|
|
||
| | Gate | What must match | | ||
| | --- | --- | | ||
| | Authenticated account | The request's authenticated agent is `example-agent`; an agent name in the body is not sufficient. | | ||
| | Source | The context's `source` is exactly `example.application`. | | ||
| | Tool | The structured operation's tool name is exactly `files.run`; wildcards are not accepted. | | ||
|
|
||
| The ordinary authentication, Council membership, tool permissions and policy | ||
| checks still apply. A supplied but untrusted runtime context is rejected with | ||
| `status: error` and `error_code: untrusted_runtime_context`; it is not silently | ||
| discarded so review can proceed with fewer facts. Requests without runtime | ||
| context retain their existing behavior. | ||
|
|
||
| ## Core request shape | ||
|
|
||
| Runtime context belongs to a structured v2 `tool_call` request. This example | ||
| shows the full location-bearing portion of an authorization request; supply the | ||
| normal authentication and intent fields as well: | ||
|
|
||
| ```json | ||
| { | ||
| "api_version": "v2", | ||
| "agent_id": "example-agent", | ||
| "council_id": "example-council", | ||
| "target_resource": "/workspace/project/reports", | ||
| "tool_call": { | ||
| "schema_version": 1, | ||
| "tool_name": "files.run", | ||
| "subject": "example-user", | ||
| "arguments": { | ||
| "script": "mkdir reports", | ||
| "directory": "/srv/workspaces/project" | ||
| }, | ||
| "runtime_context": { | ||
| "version": 1, | ||
| "source": "example.application", | ||
| "runtime": { | ||
| "working_directory": "/srv/workspaces/project", | ||
| "workspace_mappings": [ | ||
| { | ||
| "logical_root": "/workspace/project", | ||
| "execution_root": "/srv/workspaces/project" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The core receives only `{version, source, runtime}` as runtime context. When | ||
| using the Relay worker, the host's transport assertion additionally contains the | ||
| exact tool name, tool-call ID and full business-argument snapshot. The worker | ||
| checks those bindings before stripping them from the forwarded context. They | ||
| are not additional fields accepted by the core's context model. | ||
|
|
||
| The schema requires an initial working directory, workspace mappings, or both. | ||
| Paths must be absolute, contain no control characters or dot segments, and fit | ||
| the field bounds. There may be at most 16 mappings; duplicate or overlapping | ||
| logical roots are rejected. The core validates remote path spelling without | ||
| inspecting its own filesystem. The context has a 16 KiB JSON byte limit. | ||
|
|
||
| ## What reviewers and policy receive | ||
|
|
||
| Accepted facts are held separately from model-authored intent text. Council | ||
| members see an **Operator-Trusted Host Runtime Resolution** section that explains | ||
| their provenance and limits. OPA receives the facts at `input.intent.runtime_context`; | ||
| the reported working directory also supplies `input.intent.working_directory`. | ||
| If the decision-model stage is enabled, it receives the admitted runtime context | ||
| and working directory too, within its normal redaction and request-size checks. | ||
|
|
||
| The signed operation still binds the exact actual tool arguments through their | ||
| existing digest. Runtime context does not replace those arguments, change their | ||
| digest, or become an execution permission. Policy and Council still decide | ||
| whether the proposed operation may run. | ||
|
|
||
| These are host assertions, not certified executor evidence. They do not prove | ||
| that a file exists, that execution occurred, that a person approved anything, | ||
| or that a command is confined to its initial working directory. They do not | ||
| override a denial or mandatory human approval. | ||
|
|
||
| ## Privacy and integration limits | ||
|
|
||
| The live runtime facts are private review material: the core does not add them | ||
| to serialized `Intent` data or the signed operation. This is not an assurance | ||
| that paths never enter durable records. Reviewers can quote them in reasons or | ||
| guidance, which may be persisted in the review ledger. Treat runtime paths as | ||
| information disclosed to the configured reviewers and policy services, and | ||
| apply the deployment's logging and data-handling requirements. | ||
|
|
||
| Relay transport and worker support are separate integration requirements. Its | ||
| complete per-call `execution_context.data` envelope, including the duplicate | ||
| argument snapshot and any human-approval assertion, must fit the combined 16 KiB | ||
| transport budget. Do not truncate or silently drop context to make it fit. This | ||
| core API addition does not by itself configure or activate a Relay worker. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Bounded host-reported runtime facts, separate from arguments and approval.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from pathlib import PurePosixPath, PureWindowsPath | ||
| from typing import Literal | ||
|
|
||
| from pydantic import ConfigDict, Field, field_validator, model_validator | ||
|
|
||
| from nomos.strict_json import DuplicateSafeJsonModel | ||
|
|
||
| MAX_RUNTIME_CONTEXT_BYTES = 16_384 | ||
|
|
||
|
|
||
| def _absolute_path(value: str) -> str: | ||
| """Validate a remote path lexically; never inspect this service's filesystem.""" | ||
| if value != value.strip() or re.search(r"[\x00-\x1f\x7f]", value): | ||
| raise ValueError("runtime paths must not contain surrounding whitespace or controls") | ||
| if not (PurePosixPath(value).is_absolute() or PureWindowsPath(value).is_absolute()): | ||
| raise ValueError("runtime paths must be absolute") | ||
| if any(part in {".", ".."} for part in re.split(r"[/\\]", value)): | ||
| raise ValueError("runtime paths must not contain dot segments") | ||
| return value | ||
|
|
||
|
|
||
| def _root_keys(value: str) -> set[tuple[str, str]]: | ||
| """Return every valid lexical identity without guessing the remote OS. | ||
|
|
||
| A forward-slash UNC spelling can also name a POSIX absolute path. Compare | ||
| both interpretations so ambiguous spellings cannot hide conflicting roots. | ||
| Windows comparison is conservatively case-insensitive; no filesystem is | ||
| inspected and the supplied paths themselves are left unchanged. | ||
| """ | ||
| keys: set[tuple[str, str]] = set() | ||
| if PureWindowsPath(value).is_absolute(): | ||
| keys.add(("windows", PureWindowsPath(value).as_posix().rstrip("/").casefold())) | ||
| if PurePosixPath(value).is_absolute(): | ||
| keys.add(("posix", "/" + "/".join(part for part in value.split("/") if part))) | ||
| return keys | ||
|
|
||
|
|
||
| class WorkspaceMapping(DuplicateSafeJsonModel): | ||
| """One host-validated logical filesystem root and its execution-side root.""" | ||
|
|
||
| model_config = ConfigDict(extra="forbid", frozen=True) | ||
| logical_root: str = Field(min_length=1, max_length=4096) | ||
| execution_root: str = Field(min_length=1, max_length=4096) | ||
| _paths = field_validator("logical_root", "execution_root")(_absolute_path) | ||
|
|
||
|
|
||
| class RuntimeLocation(DuplicateSafeJsonModel): | ||
| """Runtime facts supplied by the host/backend, not inferred from tool keys.""" | ||
|
|
||
| model_config = ConfigDict(extra="forbid", frozen=True) | ||
| working_directory: str | None = Field(default=None, min_length=1, max_length=4096) | ||
| workspace_mappings: list[WorkspaceMapping] = Field(default_factory=list, max_length=16) | ||
|
|
||
| @field_validator("working_directory") | ||
| @classmethod | ||
| def absolute_working_directory(cls, value: str | None) -> str | None: | ||
| return _absolute_path(value) if value is not None else None | ||
|
|
||
| @model_validator(mode="after") | ||
| def meaningful_location(self) -> RuntimeLocation: | ||
| if self.working_directory is None and not self.workspace_mappings: | ||
| raise ValueError("runtime context must supply a working directory or workspace mapping") | ||
| roots: set[tuple[str, str]] = set() | ||
| for mapping in self.workspace_mappings: | ||
| keys = _root_keys(mapping.logical_root) | ||
| if any( | ||
| style == other_style | ||
| and ( | ||
| root == other | ||
| or root.startswith(other.rstrip("/") + "/") | ||
| or other.startswith(root.rstrip("/") + "/") | ||
| ) | ||
| for style, root in keys | ||
| for other_style, other in roots | ||
| ): | ||
| raise ValueError( | ||
| "runtime workspace mappings must not duplicate or overlap logical roots" | ||
| ) | ||
| roots.update(keys) | ||
| return self | ||
|
|
||
|
|
||
| class ToolRuntimeContext(DuplicateSafeJsonModel): | ||
| """Live review context; an operator-trusted assertion, never execution approval.""" | ||
|
|
||
| model_config = ConfigDict(extra="forbid", frozen=True) | ||
| version: Literal[1] = 1 | ||
| source: str = Field(min_length=1, max_length=128, pattern=r"^[A-Za-z][A-Za-z0-9_.:-]*$") | ||
| runtime: RuntimeLocation | ||
|
|
||
| @field_validator("version", mode="before") | ||
| @classmethod | ||
| def strict_version(cls, value: object) -> object: | ||
| if type(value) is not int or value != 1: | ||
| raise ValueError("runtime context version must be integer 1") | ||
| return value | ||
|
|
||
| @model_validator(mode="after") | ||
| def bounded_context(self) -> ToolRuntimeContext: | ||
| if len(self.model_dump_json().encode("utf-8")) > MAX_RUNTIME_CONTEXT_BYTES: | ||
| raise ValueError("runtime context exceeds the byte limit") | ||
| return self | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Require an explicit runtime-context version.
If the caller omits
version, this default inserts1. Pydantic does not run the field validator for an omitted default, sostrict_versioncannot reject the missing version. Admission then revalidates a snapshot that already containsversion: 1. (pydantic.dev)Remove
= 1. Keep the strict validator, and add a regression test that rejects an otherwise valid context withoutversion.Based on learnings, flag versioned serialized data when an “invalid or missing version can pass validation.”
Source: Learnings