Skip to content

refactor: simplify agent discovery result handling#74

Open
ALX99 wants to merge 2 commits into
masterfrom
refactor/simplify-agent-discovery-result
Open

refactor: simplify agent discovery result handling#74
ALX99 wants to merge 2 commits into
masterfrom
refactor/simplify-agent-discovery-result

Conversation

@ALX99

@ALX99 ALX99 commented Jul 3, 2026

Copy link
Copy Markdown
Owner

Simplification

Removes the neverthrow Result abstraction from the subagent discovery flow and replaces it with direct local return shapes:

  • discoverAgents() now returns { agents } | { error }.
  • resolveAgent() now returns AgentConfig | undefined.
  • spawn_agent keeps the same tool-boundary behavior by throwing the same registration and unknown-agent errors from index.ts.

Why the current design is overcomplicated

neverthrow was only used inside home/.pi/agent/extensions/subagents/agents.ts for two local call sites. The abstraction did not provide a meaningful extension point because both consumers immediately matched the value at the boundary and converted the result into normal control flow.

For this repository, the extra ok(...) / err(...) constructors and .match(...) calls added a second result-handling vocabulary without changing behavior or improving isolation.

Behavioral contract

The following behavior must remain unchanged:

  • Subagent discovery still reads ~/.pi/agent/extensions/subagents/agents/*.md through getAgentDir().
  • Unreadable agent directories still fail loud during extension registration.
  • Empty or fully invalid agent directories still fail loud during extension registration.
  • Per-file read or frontmatter parse failures are still skipped so one broken role does not hide the remaining usable roles.
  • Agent entries are still sorted by name before being used in the tool schema and prompt guidance.
  • Unknown agent_type values still throw from the pi tool boundary and include the requested name plus available agents.
  • Depth limiting, subprocess execution, rendering, throttling, stderr handling, abort behavior, model lookup, tool schema construction, and returned child output are not changed.
  • Public tool name, parameters, prompt guidance, and child process invocation are unchanged.

Before and after

Before:

  • agents.ts imported ok, err, and Result from neverthrow.
  • discoverAgents() returned Result<AgentConfig[], DiscoverError>.
  • resolveAgent() returned Result<AgentConfig, { requested; available }>.
  • index.ts used .match(...) twice to unwrap those local results.

After:

  • agents.ts no longer imports neverthrow.
  • discoverAgents() returns a local discriminated union, { agents } | { error }.
  • resolveAgent() returns AgentConfig | undefined.
  • index.ts uses normal if checks and throws the same error strings at the same boundary.

Evidence:

  • Removed the third-party Result vocabulary from home/.pi/agent/extensions/subagents/agents.ts.
  • Removed .match(...) control flow from home/.pi/agent/extensions/subagents/index.ts.
  • Reduced the agent lookup path to a direct find(...) result instead of wrapping and immediately unwrapping it.
  • Kept dependency files untouched because this PR is intentionally limited to behavior-preserving code simplification; removing neverthrow from package.json/package-lock.json can be done separately after local install validation.

Validation

Commands run:

  • Not run locally in this automation environment.

Static checks performed by inspection:

  • Verified the only repository code references to neverthrow were in home/.pi/agent/extensions/subagents/agents.ts, package.json, and package-lock.json.
  • Verified discoverAgents() still preserves all existing success and failure branches.
  • Verified resolveAgent() still uses the same exact name comparison against the same sorted agents array.
  • Verified index.ts still throws through the pi tool boundary for discovery failures and unknown agent_type values.

Expected validation for reviewer/local machine:

  • cd home/.pi/agent/extensions && npm run check

Risk assessment

Behavior-sensitive areas examined:

  • Extension registration: still throws before tool registration when discovery fails.
  • Unknown agent errors: still include the requested agent type and formatted available-agent list.
  • Agent discovery ordering: unchanged localeCompare sort remains in place.
  • Broken individual agent files: still skipped, not fatal.

The change is safe because it only removes a local result wrapper and leaves all IO, parsing, sorting, schema generation, execution, and rendering behavior unchanged.

Scope

Intentionally not simplified:

  • package.json / package-lock.json dependency removal for neverthrow, because lockfile changes should be generated and validated with npm install or equivalent local tooling.
  • zod schemas, because they validate frontmatter and child process JSON at trust boundaries.
  • Shell/profile configuration, because behavior preservation could not be proven safely without local shell validation.

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.

1 participant