Repository navigation
feat(agents): add proactive lifecycle hooks to base Agent (#1484) - #1797
zycaskevin wants to merge 4 commits into
Conversation
Every Hub agent needs a proactive contract (`on_first_run`/`on_heartbeat`) — but the base `Agent` was purely reactive. This PR adds the lifecycle hooks, a unified `Proposal` schema, and `GoalStore.propose()` integration so every agent gets first-run discovery and heartbeat propose-with-approval for free. **Before:** `on_first_run`/`on_heartbeat`/`propose` don't exist. `process_query()` duplicated `_agent_context` binding inline. No mechanism for agents to surface proposals for user approval. **After:** Base `Agent` defines default no-op hooks (backwards-compatible). `propose()` persists proposals to `GoalStore` with risk-tier auto-approval (low=risk→queued, medium/high→pending_approval). Shared `_agent_identity_context()` factors out the grant-binding so both `process_query` and future heartbeat code reuse the same contextvar path. ### Test plan - `pytest tests/unit/test_goal_store.py::TestPropose -v` — 5 propose tests - `pytest tests/unit/test_proactive_hooks.py -v` — 12 hook/propose/regression tests - `pytest tests/unit/test_goal_store.py -v` — all 45 tests still pass - `pytest tests/test_sdk.py::TestAgentBaseClass -v` — unchanged (pre-existing break in test_agent_process_query_interface not caused by this change) ### Related - Unblocks amd#1508 (proactive agents eval harness) - Depends on amd#1484 D4 decisions: unified Proposal schema, GoalStore for pending_approval, shared `_agent_context` binding
Review: proactive lifecycle hooks for base
|
kovtcharov
left a comment
There was a problem hiding this comment.
Thanks @zycaskevin — the scaffolding (no-op defaults, risk-tier auto-approval, behavior-preserving identity refactor) is genuinely solid and the test coverage is the right shape. Before this can merge, a few things from the earlier review are still open, plus two additions we need:
Blocking
Agent.propose()still swallows exceptions and returnsNone(agent.py) — this is the "No Silent Fallbacks" pattern CLAUDE.md prohibits.GoalStore.propose()already raises on DB error, so let the wrapper get out of the way:store = GoalStore(); return store.propose(proposal)(orraise RuntimeError(...) from eif you want context). Don't returnNone.- CI "Run Code Quality Checks" is failing —
python util/lint.py --all --fix, then push. - Example agent + user guide. The hooks aren't exercised by anything yet. Please add a small example agent that overrides
on_first_run/on_heartbeatand callspropose(), plus a short guide indocs/sdk/core/agent-system.mdx(or a new guide page wired intodocs/docs.json) walking through it. This both documents the new public API and proves the feature works end-to-end.
Cleanups (quick)
4. Restore the one-line #915 security WHY on the private _agent_context import inside _agent_identity_context.
5. Drop the redundant from ...goal_store import _now_iso self-import; call _now_iso() directly.
6. Collapse the source_label if/elif/else to source_label = source.
7. Fix the GoalStore.propose() docstring ("or None on DB error" — DB errors propagate).
8. Quote the return type (-> Optional["Goal"]) or import Goal in agent.py.
Knock these out and we're good to merge. Really appreciate the contribution! 🎉
Resolve the CHANGES_REQUESTED items on amd#1797: - Agent.propose() no longer swallows exceptions and returns None — it delegates to GoalStore.propose() and lets DB errors propagate (CLAUDE.md no-silent-fallbacks). Return type quoted as Optional["Goal"], and Goal/ Proposal imported under TYPE_CHECKING so pyflakes resolves the forward refs (fixes the F821s that were failing Code Quality). - Restore the amd#915 WHY comment on the private _agent_context import. - Drop the redundant goal_store self-import of _now_iso; collapse the source_label if/elif/else to `source_label = source`; fix the propose() docstring (DB errors propagate, not "None on DB error"). - Remove unused test imports (pytest, AgentConsole) — the other half of the flake8 failure. - Document the proactive lifecycle hooks in docs/sdk/core/agent-system.mdx with a runnable WorkspaceAgent example, and add an end-to-end test that drives that example through a real GoalStore (override -> propose -> persisted goal), proving the feature works, not just the mocks. flake8 (CI config) clean; 58 tests pass in test_proactive_hooks.py + test_goal_store.py.
|
Pushed a commit ( Blocking
Cleanups — ✅ restored the #915 WHY comment on the private Local: |
|
@claude perform a deep code review. identify any gaps or issues including a comprehensive documentation review. |
|
Deep review done. The earlier round's blockers are all fixed in the current diff — One thing needs a decision before merge, and it's both a security/trust question and a documentation contradiction: 🟡 Low-risk agent proposals now auto-execute, which contradicts the committed spec. A @kovtcharov-amd — this is an architecture/security call: is auto-approving self-assessed low-risk agent-inferred proposals the intended trust model? If yes, the spec's approval table + state machine need updating in this PR; if no, low-risk should still land in The rest are minor and non-blocking. 🔍 Technical details🟡 Trust/doc divergence —
The 🟢 Dead 🟢 🟢 🟢 Doc/docstring precision:
Verified: |
Black-format test_goal_store.py and mark the intentionally-unused interface arguments (on_first_run/on_heartbeat context, GoalStore.propose proposer) with pylint disable=unused-argument, matching the console.py convention. Fixes the Run Code Quality Checks failure.
…amd#1484) (amd#2187) Before this, the base `Agent` was purely reactive — it could only act when a user typed a prompt, with no way to surface useful work it discovered on its own. This adds opt-in `on_first_run` / `on_heartbeat` lifecycle hooks plus `propose()`, where **every agent-inferred proposal lands in `pending_approval` and waits for explicit user approval** before anything runs — matching the committed autonomous-agent spec (`docs/spec/autonomous-agent-mode.md` §6.3/§6.5). Risk tier is kept as prioritization signal for the approving user but never gates execution. Supersedes closed amd#1797 and resolves its trust-model blocker per maintainer decision: the low-risk auto-approve carve-out is removed (all proposals are approval-gated), and the dead `proposer` param is now persisted into proposal metadata. All other review items from amd#1797 (no-silent-fallback `propose()`, amd#915 security comment, docs section) were already addressed on the branch. Resolves amd#1484. ## Test plan - [x] `python -m pytest tests/unit/test_goal_store.py tests/unit/test_proactive_hooks.py -v` — 60 passed, incl. new tests: every risk tier → `pending_approval`, `proposer` persisted - [x] `python util/lint.py --all` — clean (mypy warnings pre-existing, unrelated files) - [x] Branch merged with latest `origin/main`, tests re-run post-merge - [ ] Docs: verify the "Proactive Lifecycle Hooks" section renders on the docs site preview --------- Co-authored-by: Nancy AI <[email protected]> Co-authored-by: Kalin Ovtcharov <[email protected]> Co-authored-by: kovtcharov-amd <[email protected]>
feat(agents): add proactive lifecycle hooks to base Agent (#1484)
Every Hub agent needs a proactive contract (
on_first_run/on_heartbeat) — but the baseAgentwas purely reactive. This PR adds the lifecycle hooks, a unifiedProposalschema, andGoalStore.propose()integration so every agent gets first-run discovery and heartbeat propose-with-approval for free.Before:
on_first_run/on_heartbeat/proposedon't exist.process_query()duplicated_agent_contextbinding inline. No mechanism for agents to surface proposals for user approval.After: Base
Agentdefines default no-op hooks (backwards-compatible).propose()persists proposals toGoalStorewith risk-tier auto-approval (low=risk→queued, medium/high→pending_approval). Shared_agent_identity_context()factors out the grant-binding so bothprocess_queryand future heartbeat code reuse the same contextvar path.Test plan
pytest tests/unit/test_goal_store.py::TestPropose -v— 5 propose testspytest tests/unit/test_proactive_hooks.py -v— 12 hook/propose/regression testspytest tests/unit/test_goal_store.py -v— all 45 tests still passpytest tests/test_sdk.py::TestAgentBaseClass -v— unchanged (pre-existing break in test_agent_process_query_interface not caused by this change)Related
_agent_contextbinding