Plan Verification

advanced10 minplan-verification.ts

Before an agent executes a plan, check that every step’s preconditions hold and the goal is reached.

What it shows

  • Describe actions with ActionSchemaBuilder
  • Validate and simulate plans with validatePlan() and simulatePlan()

The code

plan-verification.ts
import {
  ActionRegistry,
  ActionSchemaBuilder,
  createAction,
  formatValidationResult,
  simulatePlan,
  validatePlan,
} from '@cogitator-ai/neuro-symbolic';
import type { Plan } from '@cogitator-ai/types';

const registry = new ActionRegistry();

registry.register(
  ActionSchemaBuilder.create('load_package')
    .describe('Load the package onto the truck at the warehouse')
    .preSimple('package', 'warehouse')
    .preSimple('truck', 'warehouse')
    .assign('package', 'on_truck')
    .setCost(1)
    .build()
);
registry.register(
  ActionSchemaBuilder.create('drive_to_store')
    .describe('Drive the truck from the warehouse to the store')
    .preSimple('truck', 'warehouse')
    .assign('truck', 'store')
    .setCost(3)
    .build()
);
registry.register(
  ActionSchemaBuilder.create('unload_package')
    .describe('Unload the package at the store')
    .preSimple('package', 'on_truck')
    .preSimple('truck', 'store')
    .assign('package', 'store')
    .setCost(1)
    .build()
);

const plan: Plan = {
  id: 'delivery-1',
  name: 'deliver-package',
  initialState: { id: 'start', variables: { package: 'warehouse', truck: 'warehouse' } },
  actions: [
    createAction('load_package', {}),
    createAction('drive_to_store', {}),
    createAction('unload_package', {}),
  ],
  goalConditions: [{ type: 'simple', variable: 'package', value: 'store' }],
};

console.log(formatValidationResult(validatePlan(plan, registry)));

const simulation = simulatePlan(plan, registry);
console.log('reaches goal:', simulation.success, simulation.finalState.variables);

const broken: Plan = { ...plan, id: 'delivery-2', actions: [plan.actions[1], plan.actions[0], plan.actions[2]] };
console.log(formatValidationResult(validatePlan(broken, registry)));

Run it

install · bash
pnpm add @cogitator-ai/neuro-symbolic @cogitator-ai/types
run · bash
npx tsx plan-verification.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/advanced/02-neuro-symbolic.ts

Learn more