sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] PluginRegistry fails open on retry when initAgent() throws

Open liramon2 opened this issue 4 months ago • 1 comments

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

TypeScript

Strands Version

1.11.0

Language Runtime Version

Node.js 22.23.1

Operating System

macOS 26.5.2

Installation Method

npm

Steps to Reproduce

When a plugin's initAgent() throws during agent.initialize(), PluginRegistry consumes the plugin from its pending queue and records it as registered before the throw. On a subsequent initialize() call (or a retry via invoke()), the registry finds nothing pending and completes successfully. The agent transitions to "initialized" without the plugin's hooks or middleware installed. The plugin is silently dropped.

This affects any plugin that performs validation in initAgent() and throws on unsupported configurations. The agent appears healthy after retry but is missing the functionality the plugin was supposed to provide.

Minimal reproduction:

import { Agent } from '@strands-agents/sdk'
import type { Plugin } from '@strands-agents/sdk'
import type { LocalAgent } from '@strands-agents/sdk'

// A plugin that throws on first init but would install a hook if it succeeded
class FailOncePlugin implements Plugin {
  readonly name = 'fail-once'
  private _initCount = 0
  hookInstalled = false

  initAgent(agent: LocalAgent): void {
    this._initCount++
    if (this._initCount === 1) {
      throw new Error('Intentional first-init failure')
    }
    // If we ever get here on retry, the hook would be installed
    this.hookInstalled = true
  }
}

async function main() {
  const plugin = new FailOncePlugin()

  // Use any model — the bug is in PluginRegistry, not model interaction
  const agent = new Agent({
    model: 'us.anthropic.claude-sonnet-4-20250514',
    plugins: [plugin],
    printer: false,
  })

  // First initialize() — throws as expected
  try {
    await agent.initialize()
    console.log('ERROR: first initialize() should have thrown')
  } catch (e) {
    console.log(`✓ First initialize() threw: ${(e as Error).message}`)
  }

  // Second initialize() — should throw again, but succeeds silently
  try {
    await agent.initialize()
    console.log('✗ Second initialize() resolved — plugin was consumed, fail-open!')
  } catch (e) {
    console.log(`✓ Second initialize() threw (fail-closed): ${(e as Error).message}`)
  }

  // The plugin's hook was never installed
  console.log(`Plugin hook installed: ${plugin.hookInstalled}`)
}

main().catch(console.error)

Result:

✓ First initialize() threw: Intentional first-init failure
✗ Second initialize() resolved — plugin was consumed, fail-open!
Plugin hook installed: false

Expected Behavior

When a plugin's initAgent() throws during initialize(), the agent remains in a failed state. Subsequent calls to initialize() or invoke() should either reattempt the plugin or re-throw the original error, never silently succeed without the plugin's hooks installed.

Actual Behavior

The first initialize() throws as expected, but the plugin is consumed from the pending queue and recorded as registered before the throw. The second initialize() finds nothing pending, completes successfully, and marks the agent as initialized. The agent operates without the plugin's hooks, silently missing the functionality the user explicitly configured.

Additional Context

No response

Possible Solution

Roll back the plugin on failure in _addAndInit():

private async _addAndInit(plugin: Plugin, agent: LocalAgent): Promise<void> {
  if (this._plugins.has(plugin.name)) {
    throw new Error(`plugin_name=<${plugin.name}> | plugin already registered`)
  }
  this._plugins.set(plugin.name, plugin)

  try {
    const tools = plugin.getTools?.() ?? []
    if (tools.length > 0) agent.toolRegistry.add(tools)
    await plugin.initAgent(agent)
  } catch (error) {
    this._plugins.delete(plugin.name)
    this._pending.unshift(plugin) // put it back for retry
    throw error
  }
}

Related Issues

No response

liramon2 avatar Jul 24 '26 20:07 liramon2

I can take this. I will keep the change focused on preserving fail-closed initialization semantics, add regression coverage for a failed initAgent() followed by retry, and open a draft PR shortly.

tylergibbs1 avatar Jul 25 '26 01:07 tylergibbs1