Skip to content

feat: accept any safe OperationContext value and detect the coding agent for agent=unknown - #209

Merged
arorashivam96 merged 5 commits into
mainfrom
u/shivamarora/operation-context-agent-host-suffix
Nov 6, 2026
Merged

arorashivam96 merged 5 commits into
mainfrom
u/shivamarora/operation-context-agent-host-suffix

Conversation

@arorashivam96

@arorashivam96 arorashivam96 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What

Two changes to how OperationContext attribution reaches the User-Agent header:

  1. Accept any safe agent / skill value. Values are no longer checked against closed allowlists. The validator still enforces:

    • the key allowlist: app, skill, agent (unknown keys rejected)
    • the safe-character pattern: letters, digits, -, _, ., / (rejects spaces, control characters, and email-shaped text because @ is not allowed)
    • the app <name>/<version> format guard
  2. Fill agent=unknown from the coding agent that started the process. When the caller's context says agent=unknown (optionally unknown/<surface>), the client looks for the environment markers coding agents set on the processes they launch and sends the detected agent instead. The marker list and precedence follow the GitHub CLI (internal/agents/detect.go) and @vercel/detect-agent:

    Marker Reported as
    AI_AGENT=<name> (cross-vendor convention, checked first; e.g. VS Code Copilot sets github_copilot_vscode_agent) known aliases mapped to the plugin's names (copilot, claude-code, ...); other safe values passed through
    AGENT=amp amp
    CODEX_THREAD_ID, CODEX_SANDBOX, CODEX_CI codex
    GEMINI_CLI gemini-cli
    COPILOT_CLI copilot
    ANTIGRAVITY_AGENT antigravity-cli
    CLAUDECODE, CLAUDE_CODE claude-code
    CURSOR_AGENT, CURSOR_EXTENSION_HOST_ROLE=agent-exec cursor

    CURSOR_TRACE_ID is deliberately not used: it is present in every Cursor terminal, including commands a person runs by hand.

No effect on non-agent use of the SDK

Detection only runs when the caller supplied an OperationContext and that context says agent=unknown. In every other case the User-Agent is byte-for-byte what it was:

  • Client created without an operation context (the normal SDK case): the environment is never read; the header stays DataverseSvcPythonClient:<ver>. Covered by tests that set CLAUDECODE and AI_AGENT and assert the header is unchanged (sync and async).
  • Context that names an agent (agent=copilot): sent as given, never overridden.
  • Context with no agent key: an agent key is never added.
  • No marker present: agent=unknown is sent as given.
  • The caller's OperationContext object is not modified; only the outbound header is filled in.

Why

Loosening. New agents and skills are added constantly, and the lists had already drifted from the Dataverse plugin (_ALLOWED_AGENTS was missing gemini-cli / antigravity-cli, so a plugin script passing those names gets a ValueError). Accepting any safe value also lets an agent/<surface> suffix such as codex/jetbrains through.

Detection. About 30% of Dataverse plugin requests carry agent=unknown. The plugin decides the agent once, at setup, by asking the model to write DATAVERSE_PLUGIN_AGENT into .env, and many scripts never see that value. Production telemetry (Oct 22 - Nov 4, 2026) shows 12% of unknown requests come from a person whose agent was active and correctly named on MCP or CLI in the same hour, i.e. the Python process was started by the agent but could not name it. Process markers fix exactly that case, deterministically, without relying on .env. Unattended jobs (service accounts, no agent running) stay unknown, which is correct.

Privacy note: the character filter is structural, not a PII detector; callers must pass fixed attribution literals. Detected values come from agent-set environment variables, are lower-cased, limited to [a-z0-9_.-]{1,40}, and otherwise ignored.

Change

  • core/config.py: OperationContext.__post_init__ drops _ALLOWED_SKILLS / _ALLOWED_AGENTS and their per-value checks; docstring describes the agent=unknown fill.
  • core/_agent_detection.py (new, private): _detect_agent() and _fill_unknown_agent().
  • data/_odata_base.py: the outbound context is _fill_unknown_agent(ctx.user_agent_context); both the sync and async clients read it.

Backward compatible: every string that was valid before is still valid. Only previously rejected shapes now pass, and the header changes only for agent=unknown inside a detected agent process.

Tests

  • tests/unit/test_agent_detection.py (new): each marker, AI_AGENT precedence and aliases, unsafe AI_AGENT falls back, CURSOR_TRACE_ID alone is not an agent, amp over CLAUDECODE, surface suffix kept, explicit agent never overridden, no agent key never added, result is a valid OperationContext, header unchanged without a context, caller's object not modified.
  • tests/unit/aio/data/test_async_odata_internal.py: async client fills agent=unknown and leaves a context-less client unchanged.
  • tests/unit/test_operation_context.py: accept tests for any safe skill / agent value (incl. codex/jetbrains); structural rejection tests kept.
  • Full tests/unit: 2502 passed.

Live run against a Dataverse environment (aurorabapenv39ef7.crm10.dynamics.com) with this branch's SDK. Each User-Agent is the header actually sent, captured from the request; every call returned 200:

Case Environment Context passed User-Agent sent
unknown inside an agent AI_AGENT=github_copilot_vscode_agent ...;agent=unknown DataverseSvcPythonClient:0.1.0b10 (app=pr209-livetest/162819;skill=sdk;agent=copilot)
unknown, no agent none ...;agent=unknown ... (...;agent=unknown)
no context, inside an agent CLAUDECODE=1 none DataverseSvcPythonClient:0.1.0b10 (unchanged)
surface kept CODEX_THREAD_ID=t1 ...;agent=unknown/jetbrains ... (...;agent=codex/jetbrains)
explicit agent wins CLAUDECODE=1 ...;agent=cursor ... (...;agent=cursor)
loosened value none ...;agent=windsurf ... (...;agent=windsurf) (raised ValueError before)

The plugin-side change that fills the same gap for scripts using the plugin's auth.py (including its raw-HTTP path) is microsoft/Dataverse-skills#167.

Version / CHANGELOG

No version bump or CHANGELOG edit in this PR, per CONTRIBUTING.md. Suggested entries for the next release:

Changed - OperationContext no longer checks skill and agent values against fixed lists. Any value made of letters, digits, -, _, ., and / is accepted, including an agent/<surface> form such as codex/jetbrains. Unknown keys, unsafe characters, and a malformed app value are still rejected.

Added - When an OperationContext says agent=unknown, the client reports the coding agent that started the process (detected from AI_AGENT, CLAUDECODE, CODEX_THREAD_ID, GEMINI_CLI, and similar markers). Clients created without an operation context are unaffected.

OperationContext now accepts an optional IDE-surface suffix on the agent value, written as agent/<host> (e.g. codex/jetbrains), drawn from a new closed host allowlist (jetbrains, vscode, cli). The agent value is split on '/': the base is validated against the agent allowlist and the optional suffix against the host allowlist; unknown bases and unknown suffixes are still rejected. Enables plugin/tool attribution to record the IDE surface while preserving the closed key/value allowlist model. Bumped 1.0.1 -> 1.1.0.
@arorashivam96
arorashivam96 requested a review from a team as a code owner October 9, 2026 23:01
Copilot AI balanced review requested due to automatic review settings October 9, 2026 23:01

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.

🟡 Changes recommended

The new changelog release lacks an accurate comparison reference and leaves the Unreleased link stale.

1 open finding
What changed in this PR

Adds optional allowlisted host suffixes to OperationContext agent values.

Changes:

  • Validates agent bases and optional host suffixes separately.
  • Adds validation tests.
  • Bumps version to 1.1.0 and updates the changelog.
File Description
src/​PowerPlatform/​Dataverse/​core/​config.py Adds host-suffix validation.
tests/​unit/​test_operation_context.py Tests accepted and rejected suffixes.
pyproject.toml Bumps package version.
CHANGELOG.md Documents the feature release.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread CHANGELOG.md Resolved
Drop the closed skill/agent/host value allowlists. New agents and skills are added constantly, so enumerating values forces an SDK release for every addition and the enums had already drifted. The validator now enforces the key allowlist (app/skill/agent), the safe-character pattern (still excludes spaces/control chars/PII), and the app <name>/<version> format. Any safe-charset agent/skill value is accepted, including an agent/<surface> suffix like codex/jetbrains. Unknown keys and unsafe characters are still rejected.
@arorashivam96 arorashivam96 changed the title feat: accept optional agent/<host> suffix in OperationContext Oct 9, 2026
@arorashivam96
arorashivam96 requested a balanced review from Copilot October 9, 2026 23:32

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.

🟡 Changes recommended

Arbitrary safe-character values can contain PII despite the documented protection guarantee.

2 open findings

🧠 Review effort: Balanced

…on-context-agent-host-suffix

# Conflicts:
#	CHANGELOG.md
#	src/PowerPlatform/Dataverse/core/config.py
…ding

CONTRIBUTING.md: maintainers update CHANGELOG.md and the version in the release PR (see #207), so this PR should not bump to 1.2.0 or add a dated entry. Also corrects the ValueError docstring (values are no longer rejected, only unknown keys and a malformed app value), a code comment that implied the charset filter detects PII, and a test docstring that referenced the removed plugin allowlist.
When an OperationContext says agent=unknown, the outbound User-Agent reports the agent detected from process markers (AI_AGENT, CLAUDECODE, CODEX_THREAD_ID, GEMINI_CLI, ...), following the GitHub CLI's detection order. Clients without an operation context never read the environment, and an explicit agent is never overridden.
@arorashivam96 arorashivam96 changed the title refactor: loosen operation_context to a key allowlist + safe-value validation Nov 6, 2026
@arorashivam96
arorashivam96 merged commit e7e223a into main Nov 6, 2026
9 checks passed
@arorashivam96
arorashivam96 deleted the u/shivamarora/operation-context-agent-host-suffix branch November 6, 2026 22:28
arorashivam96 added a commit that referenced this pull request Nov 21, 2026
Prepares the **v1.2.0** release.

- Adds the `## [1.2.0]` CHANGELOG section for the user-facing changes
from #209:
- `OperationContext` accepts any safe `skill` and `agent` value,
including new agents such as `gemini-cli` and `antigravity-cli` and
`agent/<surface>` values such as `codex/jetbrains`. In 1.1.0 these
raised `ValueError`.
- When the operation context says `agent=unknown`, the client fills in
the coding agent detected from the environment. Clients without an
operation context are unaffected.
- Bumps `pyproject.toml` from `1.1.0` to `1.2.0`. The changes are
backward compatible and add behaviour, so this is a MINOR release under
SemVer.
- Fixes the `[Unreleased]` compare link, which still pointed at
`v1.0.1...HEAD`, and adds the `[1.2.0]` link.

The post-release bump after 1.1.0 was never made, so `main` still builds
as `1.1.0` even though it contains #209. This release removes that
ambiguity.

**Why now:** the Dataverse plugin
([microsoft/Dataverse-skills#167](microsoft/Dataverse-skills#167))
sends `gemini-cli` and `antigravity-cli` as the agent. With 1.1.0 those
values are rejected on the SDK path, so that traffic is still recorded
as `unknown`. Once 1.2.0 is on PyPI the plugin can require it.

**Validation**
- `pytest tests/unit`: 2502 passed.
- `black --check src tests`: clean.
- `python -m build` produces `powerplatform_dataverse_client-1.2.0`
(sdist and wheel), and the wheel includes `core/_agent_detection.py`.
- Live check against a Dataverse org with the same code built from
`main`: an SDK request made inside Antigravity, or with Gemini CLI's
environment marker set, is recorded as `antigravity-cli` / `gemini-cli`
instead of `unknown`.

After merge: run the `PowerPlatform-DataverseClient-Python-Official`
pipeline to publish to PyPI, tag `v1.2.0`, create the GitHub release,
then bump `main` to `1.2.1`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants