Skip to content

feat: emit session_ready & tools_resolve event with session object#110

Closed
NovusEdge wants to merge 4 commits into
tintinweb:masterfrom
engrammic-ai:feat/session-ready-event
Closed

feat: emit session_ready & tools_resolve event with session object#110
NovusEdge wants to merge 4 commits into
tintinweb:masterfrom
engrammic-ai:feat/session-ready-event

Conversation

@NovusEdge

@NovusEdge NovusEdge commented Jun 21, 2026

Copy link
Copy Markdown

Summary

Add two new lifecycle events that enable extensions to integrate properly with subagent sessions:

  1. subagents:session_ready - Fires when an agent's session is created and ready
  2. subagents:tools_resolve - Fires before session creation to allow tool injection

Motivation

Extensions like memory/context systems need to:

  1. Attach state to the subagent's session when it starts
  2. Inject additional tools (e.g., memory tools) into subagent sessions
  3. Access the session object to inject context or track activity
  4. Clean up when the agent completes

Currently, the existing events (subagents:started, subagents:completed) don't expose the session object or allow tool injection, making this impossible without relying on internal APIs.

Changes

  • Emit subagents:session_ready event in agent-manager.ts right after onSessionCreated fires
  • Emit subagents:tools_resolve event in agent-runner.ts before session creation
  • Add onToolsResolve callback option to SpawnOptions and RunOptions
  • Update README to document the new events

Event Payloads

session_ready

{
  id: string;            // agent ID
  type: string;          // subagent type (Explore, Plan, etc.)
  session: AgentSession; // the session object
  record: AgentRecord;   // full record for metadata
}

tools_resolve

{
  type: string;    // subagent type
  tools: any[];    // mutable array - push to inject tools
  agentId: string; // agent ID
}

Use Cases

Context propagation (session_ready)

pi.events.on("subagents:session_ready", ({ id, type, session, record }) => {
  const childHarness = parentHarness.fork({ mode: 'fork', tagPrefix: type });
  session.veilHarness = childHarness;
  trackForMerge(id, childHarness);
});

Tool injection (tools_resolve)

pi.events.on("subagents:tools_resolve", (event) => {
  const veilTools = parentHarness.getTools();
  event.tools.push(...veilTools);
});

Backward Compatibility

This is purely additive - existing code is unaffected. Extensions can opt-in to the new events.

Add a new lifecycle event that fires when an agent's session is created
and ready for use. This enables extensions that need to attach state to
subagent sessions (memory systems, context managers, telemetry) to do so
cleanly without relying on internal APIs.

Event payload:
- id: agent ID
- type: subagent type (Explore, Plan, etc.)
- session: the AgentSession object
- record: the full AgentRecord for metadata

Use case: Memory/context systems like Veil that fork parent context for
subagents and merge findings back on completion.
@NovusEdge

Copy link
Copy Markdown
Author

PS: heya! @tintinweb working on a fork of pi with smarter builtins for memory & context mgmt (think smart eviction while the user's still in the loop) and needed this :D

Allows extensions to inject additional tools into subagent sessions
before they are created. Two mechanisms:

1. SpawnOptions.onToolsResolve callback - per-spawn tool injection
2. subagents:tools_resolve event - global hook for all spawns

The event payload includes a mutable `tools` array that extensions
can modify to add their own tools (e.g., memory/context tools).

Use case: Veil injects veil_recall, veil_remember, etc. into subagents
so they can use the parent's context system.

Event fires after built-in tool filtering but before session creation.
@NovusEdge NovusEdge changed the title feat: emit subagents:session_ready event with session object feat: add session_ready and tools_resolve events for subagent extension integration Jun 21, 2026
@NovusEdge NovusEdge changed the title feat: add session_ready and tools_resolve events for subagent extension integration feat: emit session_ready & tools_resolve event with session object Jun 21, 2026
@NovusEdge
NovusEdge marked this pull request as draft June 21, 2026 21:21
@NovusEdge
NovusEdge marked this pull request as ready for review June 21, 2026 22:03
@tintinweb

Copy link
Copy Markdown
Owner

happy to add lifecycle events with a minimal proposal attached. for security reasons I cannot accept PRs with package changes or github workflows.

@NovusEdge

Copy link
Copy Markdown
Author

Superseded by #113 - clean version without package.json/workflow changes

@NovusEdge NovusEdge closed this Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants