watchlight-deepagents 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ """Watchlight deepagents — governance plugin for LangChain ``deepagents``.
2
+
3
+ ``deepagents`` is LangChain's opinionated agent harness built on
4
+ ``langchain.agents.create_agent`` + a LangGraph runtime. It standardises
5
+ on the ``AgentMiddleware`` protocol and exposes a first-class
6
+ ``middleware=`` insertion slot, so this plugin is **auto-registering** —
7
+ no monkey-patching, zero customer tool-code changes.
8
+
9
+ Two integration surfaces:
10
+
11
+ **Drop-in factory (recommended)** ::
12
+
13
+ from watchlight_deepagents import create_governed_deep_agent
14
+
15
+ agent = create_governed_deep_agent(
16
+ model="anthropic:claude-sonnet-4",
17
+ tools=[search, fetch],
18
+ subagents=[researcher],
19
+ agent_slug="research-orchestrator", # the ONE required extra arg
20
+ )
21
+
22
+ **Explicit middleware — ROOT-ONLY** (for customers who build their own
23
+ ``middleware=`` and do **not** delegate to sub-agents) ::
24
+
25
+ from deepagents import create_deep_agent
26
+ from watchlight_deepagents import WatchlightDeepAgentsPlugin
27
+
28
+ wl = WatchlightDeepAgentsPlugin.from_env()
29
+ agent = create_deep_agent(
30
+ ..., middleware=[wl.middleware(agent_slug="research-orchestrator")]
31
+ )
32
+
33
+ .. warning::
34
+
35
+ This "Option B" path governs the **root agent only**. deepagents does
36
+ not propagate the root ``middleware=`` list into sub-agents, so this
37
+ path cannot attenuate them — it therefore **refuses every ``task``
38
+ sub-agent spawn** (fail-closed). Any agent that delegates to
39
+ sub-agents MUST be built with :func:`create_governed_deep_agent`,
40
+ which injects child governance into every spawnable sub-agent
41
+ (including deepagents' otherwise-ungoverned auto ``general-purpose``).
42
+
43
+ With the factory, every model turn, every FS/tool/task call, and every
44
+ sub-agent action is governed, authorized against the correct (root or
45
+ attenuated-child) RunHandle, audited, and — with ``WATCHLIGHT_PROXY_URL``
46
+ set — egress-enforced at the Watchlight proxy.
47
+ """
48
+
49
+ from watchlight_core.langchain_tool_backstop import (
50
+ UngovernedModelCall,
51
+ UngovernedToolCall,
52
+ direct_model_use,
53
+ )
54
+ from watchlight_deepagents.factory import create_governed_deep_agent
55
+ from watchlight_deepagents.middleware import (
56
+ STATE_TOKEN_KEY,
57
+ build_governance_middleware,
58
+ current_active_handle,
59
+ root_run,
60
+ )
61
+ from watchlight_deepagents.plugin import RunHandle, WatchlightDeepAgentsPlugin
62
+ from watchlight_deepagents.state_adapter import state_from_deepagents_plugin
63
+ from watchlight_deepagents.subagents import (
64
+ GENERAL_PURPOSE_SUBAGENT_NAME,
65
+ TASK_TOOL_NAME,
66
+ ensure_general_purpose_subagent,
67
+ inject_child_governance_middleware,
68
+ requested_scope_for_subagent,
69
+ resolve_subagent_slug,
70
+ )
71
+ from watchlight_deepagents.synthetic_tools import build_declare_intent_tool
72
+ from watchlight_deepagents.traces import (
73
+ redacted_arg_metadata,
74
+ redacted_input_summary,
75
+ resolve_action_verb,
76
+ task_resource_uri,
77
+ tool_resource_uri,
78
+ )
79
+
80
+ __all__ = [
81
+ "WatchlightDeepAgentsPlugin",
82
+ "RunHandle",
83
+ "create_governed_deep_agent",
84
+ "direct_model_use",
85
+ "UngovernedModelCall",
86
+ "UngovernedToolCall",
87
+ "build_governance_middleware",
88
+ "current_active_handle",
89
+ "root_run",
90
+ "STATE_TOKEN_KEY",
91
+ "state_from_deepagents_plugin",
92
+ "TASK_TOOL_NAME",
93
+ "GENERAL_PURPOSE_SUBAGENT_NAME",
94
+ "resolve_subagent_slug",
95
+ "requested_scope_for_subagent",
96
+ "ensure_general_purpose_subagent",
97
+ "inject_child_governance_middleware",
98
+ "build_declare_intent_tool",
99
+ "resolve_action_verb",
100
+ "tool_resource_uri",
101
+ "task_resource_uri",
102
+ "redacted_arg_metadata",
103
+ "redacted_input_summary",
104
+ ]
105
+ __version__ = "0.1.2"
@@ -0,0 +1,25 @@
1
+ """The graph-state schema that carries the Watchlight run token.
2
+
3
+ Kept in its own module, imported lazily by ``middleware.py`` only when
4
+ the real LangChain ``AgentMiddleware`` base is in use, and written
5
+ WITHOUT ``from __future__ import annotations``: LangGraph resolves a
6
+ TypedDict's annotations with ``get_type_hints``, which needs the real
7
+ ``Annotated`` / ``NotRequired`` objects in this module's globals.
8
+ """
9
+
10
+ from typing import Annotated
11
+
12
+ from langchain.agents.middleware.types import AgentState, PrivateStateAttr
13
+ from typing_extensions import NotRequired
14
+
15
+
16
+ class WatchlightGovernanceState(AgentState): # type: ignore[misc]
17
+ """``AgentState`` plus the private ``wl_run_token`` key.
18
+
19
+ ``PrivateStateAttr`` omits the key from the graph's input and output
20
+ schema: a caller cannot inject a token through ``ainvoke`` input, a
21
+ run's token never leaks in its result, and deepagents neither copies
22
+ it into a sub-agent's state nor back out of it.
23
+ """
24
+
25
+ wl_run_token: NotRequired[Annotated[str, PrivateStateAttr]]
@@ -0,0 +1,127 @@
1
+ """``create_governed_deep_agent`` — the one-import drop-in factory.
2
+
3
+ Wraps ``deepagents.create_deep_agent`` so the customer's only changes
4
+ are: one import swap, one ``agent_slug`` argument, and ≤3 env knobs. The
5
+ factory:
6
+
7
+ (a) constructs the :class:`WatchlightDeepAgentsPlugin` from env
8
+ (fail-loud on missing ``WATCHLIGHT_APDP_URL``);
9
+ (b) prepends the root governance ``AgentMiddleware`` to ``middleware=``;
10
+ (c) pre-registers a **governed** ``general-purpose`` sub-agent so
11
+ deepagents does not auto-add its own *ungoverned* one;
12
+ (d) injects a child governance middleware into every ``SubAgent``'s
13
+ own ``middleware`` list so sub-agents authorize against their own
14
+ attenuated handle — **refusing** (fail-closed) any sub-agent that
15
+ cannot be governed by injection (``CompiledSubAgent`` /
16
+ ``AsyncSubAgent``);
17
+ (e) leaves tool bodies, sub-agent prompts, and the model loop
18
+ untouched (customers never modify their agent's tool code).
19
+
20
+ This factory is the **single governed-construction path** — the only
21
+ one that guarantees every spawnable sub-agent is attenuated. The
22
+ standalone ``plugin.middleware()`` ("Option B") governs the root agent
23
+ but **refuses sub-agent spawns** (it cannot reach ``subagents=`` to
24
+ inject child governance); use this factory whenever the agent delegates.
25
+ """
26
+
27
+ from __future__ import annotations
28
+
29
+ from typing import Any, Dict, List, Optional, Sequence
30
+
31
+ from .middleware import build_governance_middleware
32
+ from .plugin import WatchlightDeepAgentsPlugin
33
+ from .subagents import (
34
+ ensure_general_purpose_subagent,
35
+ inject_child_governance_middleware,
36
+ )
37
+
38
+
39
+ def create_governed_deep_agent(
40
+ *,
41
+ agent_slug: str,
42
+ subagent_scopes: Optional[Dict[str, Dict[str, Any]]] = None,
43
+ subagent_slugs: Optional[Dict[str, str]] = None,
44
+ plugin: Optional[WatchlightDeepAgentsPlugin] = None,
45
+ middleware: Sequence[Any] = (),
46
+ subagents: Optional[List[Any]] = None,
47
+ **create_deep_agent_kwargs: Any,
48
+ ) -> Any:
49
+ """Build a governed ``deepagents`` agent — the recommended drop-in.
50
+
51
+ Args:
52
+ agent_slug: The registered Watchlight slug for the root agent
53
+ (the ONE required extra arg). Resolved server-side to a
54
+ UUID — never inferred from the model's prompt.
55
+ subagent_scopes: Per-subagent attenuation keyed by deepagents
56
+ subagent ``name`` (strict subset; fail-closed when omitted).
57
+ subagent_slugs: Optional deepagents subagent ``name`` → Watchlight
58
+ slug map. Defaults to the subagent name.
59
+ plugin: An explicit plugin instance. Defaults to
60
+ :meth:`WatchlightDeepAgentsPlugin.from_env` — fail-loud on
61
+ missing config.
62
+ middleware: The customer's own middleware list; the root
63
+ governance middleware is prepended.
64
+ subagents: The ``subagents=`` value; each entry gets a child
65
+ governance middleware injected.
66
+ **create_deep_agent_kwargs: Forwarded verbatim to
67
+ ``deepagents.create_deep_agent`` (``model``, ``tools``,
68
+ ``system_prompt``, ``backend``, …).
69
+
70
+ Returns:
71
+ The ``CompiledStateGraph`` from ``create_deep_agent`` — the same
72
+ object the customer would otherwise have used, now governed.
73
+ """
74
+ from deepagents import create_deep_agent
75
+
76
+ if plugin is None:
77
+ plugin = WatchlightDeepAgentsPlugin.from_env()
78
+
79
+ scopes = subagent_scopes or {}
80
+ slugs = subagent_slugs or {}
81
+
82
+ def _child_mw_factory() -> Any:
83
+ return build_governance_middleware(
84
+ plugin=plugin,
85
+ agent_slug=agent_slug,
86
+ subagent_scopes=scopes,
87
+ subagent_slugs=slugs,
88
+ is_child=True,
89
+ )
90
+
91
+ # Pre-register a GOVERNED ``general-purpose`` sub-agent so
92
+ # deepagents does not auto-add its own UNGOVERNED one (which would
93
+ # carry the main agent's full tool set with no ``watchlight_governance``
94
+ # slot). Zero-privilege unless the operator scopes ``general-purpose``.
95
+ governed_subagents = ensure_general_purpose_subagent(subagents)
96
+
97
+ # Inject child governance into EVERY spawnable
98
+ # sub-agent (custom + the general-purpose we just registered). This
99
+ # RAISES (fail-closed) on any spec that cannot be governed by
100
+ # injection (CompiledSubAgent / AsyncSubAgent) — an un-attenuable
101
+ # sub-agent aborts construction rather than running ungoverned. Only
102
+ # once this returns can we truthfully mark child governance
103
+ # guaranteed on the root middleware.
104
+ inject_child_governance_middleware(governed_subagents, _child_mw_factory)
105
+
106
+ root_mw = build_governance_middleware(
107
+ plugin=plugin,
108
+ agent_slug=agent_slug,
109
+ subagent_scopes=scopes,
110
+ subagent_slugs=slugs,
111
+ is_child=False,
112
+ # The factory is the ONE governed-construction path: every
113
+ # spawnable sub-agent above has a child governance middleware.
114
+ # This unlocks the root ``task``-tool interception (Option B,
115
+ # which cannot inject, leaves this False and refuses spawns).
116
+ child_governance_guaranteed=True,
117
+ )
118
+
119
+ merged_middleware = [root_mw, *middleware]
120
+ return create_deep_agent(
121
+ middleware=merged_middleware,
122
+ subagents=governed_subagents,
123
+ **create_deep_agent_kwargs,
124
+ )
125
+
126
+
127
+ __all__ = ["create_governed_deep_agent"]