Files
wehub-resource-sync b4fbd6fe9f
Deploy Site / deploy-vercel (push) Has been skipped
Deploy Site / deploy-docs (push) Has been skipped
Build Skills Index / build-index (push) Has been skipped
CI / Deny unrelated histories (push) Has been skipped
CI / Detect affected areas (push) Successful in 27m35s
CI / OSV scan (push) Failing after 4s
CI / Build&Test Docker image (push) Successful in 9s
CI / Supply-chain scan (push) Has been skipped
CI / Lint Docker scripts (push) Failing after 5m13s
CI / Check contributors (push) Failing after 12m8s
CI / Docs Site (push) Failing after 12m8s
CI / TypeScript (push) Failing after 12m8s
CI / Python lints (push) Failing after 12m9s
CI / Python tests (push) Failing after 12m9s
CI / Check uv.lock (push) Failing after 23m22s
CI / CI timing report (push) Has been cancelled
Build Skills Index / trigger-deploy (push) Has been cancelled
CI / All required checks pass (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:56:03 +08:00

79 lines
2.5 KiB
Python

"""Skill write-origin provenance — ContextVar for distinguishing agent-sediment skill writes from foreground user-directed writes.
The curator only consolidates/prunes skills it autonomously created via the
background self-improvement review fork. Skills a user asks a foreground
agent to write belong to the user and must never be auto-curated.
This module exposes a ContextVar that run_agent.py sets before each tool
loop so tool handlers (e.g. skill_manage create) can check whether they
are executing inside the background-review fork.
The signal piggybacks on AIAgent._memory_write_origin, which is already
set to "background_review" for review-fork instances (see
_spawn_background_review in run_agent.py) and defaults to "assistant_tool"
for normal (foreground) agents.
Usage:
from tools.skill_provenance import (
set_current_write_origin,
reset_current_write_origin,
get_current_write_origin,
)
token = set_current_write_origin("background_review")
try:
... # tool runs here
finally:
reset_current_write_origin(token)
# inside a tool:
if get_current_write_origin() == "background_review":
mark_agent_created(skill_name)
"""
import contextvars
_write_origin: contextvars.ContextVar[str] = contextvars.ContextVar(
"skill_write_origin",
default="foreground",
)
# The sentinel value the background review fork uses; mirrors
# run_agent.py's AIAgent._memory_write_origin override in
# _spawn_background_review().
BACKGROUND_REVIEW = "background_review"
def set_current_write_origin(origin: str) -> contextvars.Token[str]:
"""Bind the active write origin to the current context.
Returns a Token the caller must pass to reset_current_write_origin
in a finally block.
"""
return _write_origin.set(origin or "foreground")
def reset_current_write_origin(token: contextvars.Token[str]) -> None:
"""Restore the prior write origin context."""
_write_origin.reset(token)
def get_current_write_origin() -> str:
"""Return the active write origin.
Default: "foreground" — any tool call made by a regular (non-review)
agent, from the CLI, the gateway, cron, or a subagent.
"background_review" — the self-improvement review fork; only skills
created under this origin should be marked agent-created for curator
management.
"""
return _write_origin.get()
def is_background_review() -> bool:
"""Convenience: True iff the current write origin is the background
review fork."""
return get_current_write_origin() == BACKGROUND_REVIEW