npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

agentrein

v1.0.18

Published

The safety net for AI agents. Automatic rollback, approval gates, and intent verification.

Readme

agentrein

The safety net for AI agents. Automatic rollback, approval gates, and intent verification.

npm version License: MIT

Installation

npm install agentrein

Quick Start

// ESM
import { AgentRein } from 'agentrein'

// CJS
const { AgentRein } = require('agentrein')

// Initialize the client
const agentrein = new AgentRein({
  apiKey: process.env.AGENTREIN_API_KEY!,
})

// Create a session describing what your agent intends to do
const session = await agentrein.newSession({
  agentId: 'billing-agent',
  intent: 'Send Q4 invoices to all customers',
})

// Wrap any API call — AgentRein logs it and auto-rolls back on failure
await agentrein.call(
  stripe.invoices.create,
  session,
  { customer: 'cus_123', amount: 5000 }
)
// If stripe.invoices.create throws, AgentRein automatically triggers
// a server-side LIFO rollback of all actions in this session.

Core Concepts

| Concept | Description | |---|---| | Sessions | One agent workflow. All actions are grouped under a single session so rollback can undo them as a unit. | | Actions | Every call() is logged with the full payload and response, creating a complete audit trail. | | Rollback | On any failure, AgentRein triggers a LIFO (last-in-first-out) undo of every action in the session. | | Approval Gate | Flag high-risk actions with requiresApproval: true to block execution until a human approves from the dashboard. | | Fail-Open | If the AgentRein server is unreachable, your agent continues normally by default — safety never blocks production. |

API Reference

new AgentRein(options)

Create a new AgentRein client instance.

| Option | Type | Default | Required | Description | |---|---|---|---|---| | apiKey | string | — | yes | Organization API key | | serverUrl | string | https://api.agentrein.com | no | Custom server URL | | failureMode | 'open' \| 'closed' | 'open' | no | Behavior when server is unreachable |

const agentrein = new AgentRein({
  apiKey: 'ak_live_...',
  serverUrl: 'https://api.agentrein.com',
  failureMode: 'open',
})

agentrein.newSession(options?)

Create a new agent session.

// With full options (recommended)
const session = await agentrein.newSession({
  agentId: 'billing-agent',
  intent: 'Send Q4 invoices to all customers',
})

// String shorthand (agentId only)
const session = await agentrein.newSession('billing-agent')

// No args — auto-generated agentId
const session = await agentrein.newSession()

Returns: Session object with id, organizationId, agentId, intent, status, startedAt, endedAt.


agentrein.call(fn, session, ...args, options?)

Execute a function under AgentRein's protection. The call is logged, and on failure the entire session is rolled back.

// Basic usage
await agentrein.call(
  stripe.invoices.create,
  session,
  { customer: 'cus_123', amount: 5000 }
)

// With custom action name
await agentrein.call(
  stripe.invoices.create,
  session,
  { customer: 'cus_123', amount: 5000 },
  { actionName: 'stripe.invoices.create' }
)

// With approval gate
await agentrein.call(
  stripe.invoices.create,
  session,
  { customer: 'cus_123', amount: 500000 },
  {
    requiresApproval: true,
    pollIntervalMs: 2000,
    timeoutMs: 300000,
  }
)

CallOptions

| Option | Type | Default | Description | |---|---|---|---| | actionName | string | fn.name | Override the logged action name | | requiresApproval | boolean | false | Block until a human approves from the dashboard | | pollIntervalMs | number | 2000 | Approval polling interval in ms | | timeoutMs | number | 300000 | Approval timeout in ms (5 min) |


agentrein.resumeSession(sessionId) / agentrein.getSession(sessionId)

Both are aliases — returns the full session with all actions.

const session = await agentrein.resumeSession('sess_abc123')
// or
const session = await agentrein.getSession('sess_abc123')

Approval Gate

Flag any action with requiresApproval: true to require human sign-off before execution.

Flow:

  1. call() logs the action as PENDING_APPROVAL
  2. SDK polls GET /approvals/:id at the configured interval
  3. A reviewer approves or rejects from the AgentRein dashboard
  4. Approvedfn() executes → action updated to SUCCESS
  5. RejectedApprovalRejectedError thrown → session rollback triggered

Error handling:

import { AgentRein, ApprovalRejectedError } from 'agentrein'

try {
  await agentrein.call(fn, session, args, { requiresApproval: true })
} catch (err) {
  if (err instanceof ApprovalRejectedError) {
    console.log('Rejected:', err.reason)
    // rollback already triggered automatically
  }
}

Error Reference

| Error | When Thrown | |---|---| | AgentReinUnavailableError | Server unreachable during newSession() or token fetch. In fail-closed mode, also thrown from call(). | | ApprovalRejectedError | Reviewer rejected the action. Has a .reason property. | | ApprovalTimeoutError | Approval not received within timeoutMs. |

Fail Modes

| Mode | Behavior | When to Use | |---|---|---| | 'open' (default) | If server is down, call() executes unprotected | Most use cases | | 'closed' | If server is down, call() throws AgentReinUnavailableError | Finance, healthcare |

Supported Connectors

Built-in undo strategies for popular services:

| Connector | Actions | Undo Strategy | |---|---|---| | Stripe | stripe.invoices.create, stripe.customers.create | Direct delete | | Slack | slack.chat.postMessage | Correction message in thread | | GitHub | github.issues.create | Close issue | | HubSpot | hubspot.contacts.create/update, hubspot.deals.create/update | Delete / restore beforeState | | Salesforce | salesforce.contacts.create/update, salesforce.opportunities.create/update | Delete / restore beforeState | | Notion | notion.pages.create/update, notion.database_items.create, notion.blocks.append | Archive / restore / delete blocks | | Gmail | gmail.messages.send/trash, gmail.drafts.create, gmail.labels.modify | Correction reply / untrash / delete / reverse labels | | Google Drive | gdrive.files.create/update/move/trash | Delete / restore / reverse move / untrash | | Google Sheets | gsheets.values.append/update, gsheets.spreadsheets.create, gsheets.sheets.add | Clear / restore / delete |

Links