Sign In

@codmir/engine

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@codmir/engine

Codmir autonomous execution engine - event-driven contract executor

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

@codmir/engine

Codmir Autonomous Execution Engine - Event-driven contract executor for autonomous agent runs.

Overview

The engine is the central orchestrator for autonomous agent execution in Codmir. It:

  • Consumes events from the event bus
  • Matches triggers to registered contracts
  • Creates and manages runs with deterministic state
  • Advances runs through step graphs via a pure state machine

All side effects happen through adapters, making the engine deterministic and replayable.

Architecture

┌─────────────────────────────────────────────────────────────────┐
│                     Domain Events                               │
│              (ticket.created, github.pr.opened, etc.)           │
└─────────────────────────┬───────────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────────────┐
│                    CodmirEngine                                 │
│  ┌─────────────┐  ┌──────────────┐  ┌────────────────────┐     │
│  │   Contract  │  │    Run       │  │    Advance Loop    │     │
│  │   Registry  │  │   Manager    │  │  (State Machine)   │     │
│  └─────────────┘  └──────────────┘  └────────────────────┘     │
└─────────────────────────┬───────────────────────────────────────┘
                          │
         ┌────────────────┼────────────────┐
         │                │                │
         ▼                ▼                ▼
    ┌─────────┐     ┌──────────┐     ┌───────────┐
    │ Runtime │     │ Events   │     │ Storage   │
    │(Railway)│     │ Adapter  │     │ Adapter   │
    └─────────┘     └──────────┘     └───────────┘

Installation

pnpm add @codmir/engine @codmir/contracts

Quick Start

import { createEngine, InMemoryStorageAdapter, InMemoryEventsAdapter, InMemoryRuntimeAdapter, DefaultPolicyAdapter, SystemClockAdapter, ConsoleLoggerAdapter } from "@codmir/engine";
import { ticketTriageContract } from "@codmir/contracts";

// Create adapters
const events = new InMemoryEventsAdapter();
const storage = new InMemoryStorageAdapter();
const runtime = new InMemoryRuntimeAdapter({ eventPublisher: events.publish.bind(events) });
const policy = new DefaultPolicyAdapter();
const clock = new SystemClockAdapter();
const logger = new ConsoleLoggerAdapter();

// Create engine
const engine = createEngine({
  events,
  storage,
  runtime,
  policy,
  clock,
  logger,
});

// Register contracts
await storage.saveContract(ticketTriageContract);

// Start the engine
await engine.start();

// Trigger a run manually
const runId = await engine.triggerRun({
  contractId: "codmir.ticket-triage",
  triggerEvent: {
    name: "ticket.created",
    payload: {
      id: "T-123",
      title: "Bug in login flow",
      body: "Users can't login...",
    },
  },
});

console.log("Started run:", runId);

Integration with @codmir/events

import { emit, getGlobalDispatcher } from "@codmir/events";
import { CodmirEventsAdapter } from "@codmir/engine/adapters";

// Create adapter that bridges to @codmir/events
const eventsAdapter = new CodmirEventsAdapter({
  emit,
  dispatcher: getGlobalDispatcher(),
  actorId: "codmir-engine",
});

// Use in engine
const engine = createEngine({
  events: eventsAdapter,
  // ... other adapters
});

Contracts

Contracts define what autonomous agents can do. See @codmir/contracts for the full schema.

Step Types

StepDescriptionBlocking
taskExecute a task on the runtimeYes
llmInvoke an LLM via intelligence adapterNo*
emitEmit an event to the event busNo
waitWait for an external eventYes
branchConditional branchingNo
transformTransform data in contextNo
approvalHuman-in-the-loop gate (Overseer)Yes

Example Contract

import type { CodmirContract } from "@codmir/contracts";

const myContract: CodmirContract = {
  schemaVersion: "codmir.contract.v1",
  id: "my-agent",
  version: "1.0.0",
  title: "My Agent",
  
  triggers: [
    { type: "event", event: "my.trigger" },
  ],
  
  permissions: [
    { kind: "event:emit", pattern: "my.*" },
    { kind: "llm:invoke" },
  ],
  
  limits: {
    maxRunMs: 60000,
    maxSteps: 10,
  },
  
  entryStepId: "start",
  
  steps: [
    {
      id: "start",
      name: "Start",
      kind: "emit",
      event: "my.started",
      payload: { triggerId: "{{trigger.id}}" },
      next: null,
    },
  ],
};

Adapters

The engine uses adapters for all external interactions:

EngineEventsAdapter

Publish and subscribe to events.

EngineStorageAdapter

Store contracts and run records.

EngineRuntimeAdapter

Execute tasks on remote runtime (Railway).

EngineIntelligenceAdapter

Invoke LLMs (optional).

EnginePolicyAdapter

Enforce permissions and policies.

EngineClockAdapter

Get current time (deterministic for replay).

EngineLoggerAdapter

Log engine activity.

Overseer Pattern (Human-in-the-Loop)

Use approval steps to pause execution until a human approves:

{
  id: "require-approval",
  name: "Require Approval",
  kind: "approval",
  prompt: "Deploy to production?",
  approvers: ["admin", "devops"],
  nextOnApproved: "deploy",
  nextOnDenied: "cancel",
  timeout: { ms: 86400000 }, // 24 hours
}

Approve via event:

await events.publish({
  name: "engine.approval.granted",
  payload: {
    runId: "run_123",
    stepId: "require-approval",
    grantedBy: "admin@example.com",
  },
});

Engine Events

EventDescription
engine.run.requestedRun creation requested
engine.run.startedRun started executing
engine.run.completedRun completed successfully
engine.run.failedRun failed
engine.run.cancelledRun was cancelled
engine.run.pausedRun was paused
engine.run.resumedRun was resumed
engine.run.waitingRun is waiting for event
engine.step.requestedStep execution requested
engine.step.completedStep completed
engine.step.failedStep failed
engine.approval.requestedApproval requested
engine.approval.grantedApproval granted
engine.approval.deniedApproval denied

Integration with apps/agent (Railway Agent)

The engine can delegate heavy task execution to the apps/agent service:

import { createEngine, RailwayAgentAdapter, CodmirEventsAdapter } from "@codmir/engine";

// Create Railway agent adapter
const agentAdapter = new RailwayAgentAdapter({
  baseUrl: process.env.AGENT_SERVICE_URL ?? "https://agent.railway.internal",
  callbackUrl: process.env.ENGINE_CALLBACK_URL ?? "https://engine.railway.internal/callbacks",
  callbackSecret: process.env.CALLBACK_SECRET!,
  apiKey: process.env.AGENT_API_KEY,
});

// Create engine with agent runtime
const engine = createEngine({
  runtime: agentAdapter,
  events: eventsAdapter,
  storage: storageAdapter,
  policy: policyAdapter,
  clock: clockAdapter,
  logger: loggerAdapter,
});

Supported Agent Tasks

Task NameAgent ModeDescription
code-tasktaskCode generation/modification
analyze-codetaskCode analysis
test-runtaskRun tests
analyze-ticketticketTicket analysis
knowledge-basetaskKnowledge base operations

Handling Agent Callbacks

Set up a callback endpoint to receive task completion events:

import { handleAgentCallback } from "@codmir/engine/adapters";

// Express endpoint
app.post('/callbacks', async (req, res) => {
  const result = handleAgentCallback(
    agentAdapter,
    req.body,
    req.headers['x-callback-secret'] as string,
    process.env.CALLBACK_SECRET
  );

  if (result) {
    // Publish completion event to engine
    await eventsAdapter.publish({
      name: result.success ? 'engine.step.completed' : 'engine.step.failed',
      payload: {
        runId: result.runId,
        stepId: result.stepId,
        output: result.output,
        error: result.error,
      },
      correlationId: result.runId,
    });
  }

  res.json({ ok: true });
});

Architecture

┌─────────────────────────────────────────────────────────────────┐
│                      @codmir/engine                             │
│  Contract orchestration, triggers, state machine, approvals     │
└─────────────────────────┬───────────────────────────────────────┘
                          │ POST /jobs
                          ▼
┌─────────────────────────────────────────────────────────────────┐
│                      apps/agent                                  │
│  Heavy task execution: code gen, tests, analysis, git ops       │
└─────────────────────────┬───────────────────────────────────────┘
                          │ POST /callbacks (on completion)
                          ▼
┌─────────────────────────────────────────────────────────────────┐
│                      Engine Callback Handler                     │
│  Converts to engine.step.completed/failed events                │
└─────────────────────────────────────────────────────────────────┘

License

MIT

Keywords

codmir

FAQs

Package last updated on 06 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts