AGENT SKILL

xstate-actor-model

暂无简介

License 许可
Apache-2.0
最近更新
2026-11-12
Stars
★ 13
Forks
2

03 SKILL.md 正文

/xstate-actor-model

When to Use

  • Model agent lifecycle explicitly: idle → running → awaiting-approval → isolated → stopped
  • Prevent illegal state transitions (e.g., agent cannot run while isolated)
  • Spawn child actors per task with isolated state machines
  • Visualize agent state transitions for audit/debugging

Do NOT use for

  • Simple boolean flags (on/off, enabled/disabled)
  • High-frequency event loops where FSM overhead is measurable

Agent lifecycle state machine

import { createMachine, createActor, assign } from 'xstate'

const agentMachine = createMachine({
  id:      'agent',
  initial: 'idle',
  context: { taskId: null as string | null, retries: 0 },

  states: {
    idle: {
      on: { START: { target: 'running', actions: assign({ taskId: ({ event }) => event.taskId }) } },
    },

    running: {
      on: {
        COMPLETE:       'idle',
        NEEDS_APPROVAL: 'awaiting-approval',
        ERROR:          { target: 'running', guard: ({ context }) => context.retries < 3,
                          actions: assign({ retries: ({ context }) => context.retries + 1 }) },
        FATAL:          'isolated',
      },
    },

    'awaiting-approval': {
      on: {
        APPROVED: 'running',
        DENIED:   'isolated',
        TIMEOUT:  'isolated',
      },
    },

    isolated: {
      type: 'final',
      entry: [() => console.log('[xstate] agent isolated — requires human review')],
    },
  },
})

const actor = createActor(agentMachine)
actor.subscribe(snapshot => {
  console.log('[xstate] state:', snapshot.value, '| context:', snapshot.context)
})
actor.start()
actor.send({ type: 'START', taskId: 'task-42' })
actor.send({ type: 'NEEDS_APPROVAL' })
actor.send({ type: 'APPROVED' })
actor.send({ type: 'COMPLETE' })

Guards (conditional transitions)

const machine = createMachine({
  // ...
  states: {
    running: {
      on: {
        ESCALATE: {
          target: 'escalated',
          guard: ({ context }) => context.trustScore < 50,
        },
      },
    },
  },
})

Spawned child actor (task isolation)

import { spawnChild, stopChild } from 'xstate'

const parentMachine = createMachine({
  context: { taskActor: null as any },
  states: {
    active: {
      on: {
        SPAWN_TASK: {
          actions: assign({
            taskActor: ({ spawn }) => spawn(taskMachine, { id: 'task' }),
          }),
        },
        STOP_TASK: {
          actions: [
            stopChild(({ context }) => context.taskActor),
            assign({ taskActor: null }),
          ],
        },
      },
    },
  },
})

Persist and restore state

// Persist snapshot to disk
const snapshot = actor.getSnapshot()
const persisted = JSON.stringify(snapshot)
await writeFile('agent-state.json', persisted)

// Restore on startup
const saved    = JSON.parse(await readFile('agent-state.json', 'utf8'))
const restored = createActor(agentMachine, { snapshot: agentMachine.resolveState(saved) })
restored.start()
console.log('[xstate] restored to:', restored.getSnapshot().value)

Anti-Fake-Pass Checklist

❌ Sending events before actor.start() → events silently dropped
❌ Missing guard on retry loop → agent retries forever without a ceiling
❌ final state receives events → events after final are silently ignored; check state before sending
❌ assign() with mutation instead of pure function → context mutation breaks time-travel debugging
❌ Spawned actors not stopped on parent stop → memory leak from orphaned child actors
❌ Serializing actor (not snapshot) → actor contains callbacks, not serializable

04 同仓库更多 Skills

Horonomos-laps
Horonomos-laps/Yana-AI

openai--sentry--sentry

Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

ito-basket-compare

Compare Itô prediction-market baskets against a user's knowledge base, portfolio notes, financial context, watchlist, or research thesis. Use for read-only basket comparison and gap analysis without investment advice or live trading.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

implementing-network-traffic-baselining

Build network traffic baselines from NetFlow/IPFIX data using Python pandas for statistical analysis, z-score anomaly detection, and hourly/daily traffic pattern profiling

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

detecting-evasion-techniques-in-endpoint-logs

Detects defense evasion techniques used by adversaries in endpoint logs including log tampering, timestomping, process injection, and security tool disabling. Use when investigating suspicious endpoint behavior, building detection rules for evasion tactics, or conducting threat hunting for stealthy adversary activity. Activates for requests involving evasion detection, defense evasion analysis, log tampering detection, or MITRE ATT&CK TA0005.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

implementing-microsegmentation-with-guardicore

Implementing microsegmentation using Akamai Guardicore Segmentation to map application dependencies, create granular network policies, visualize east-west traffic flows, and enforce least-privilege communication between workloads across data centers and cloud.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

ramdisk-tmpfs-patterns

暂无简介

★ 13