Golem recurring task ts

From golemcloud/golem · View interactive version

Golem recurring task ts is an AI agent skill from the GitHub repository golemcloud/golem. Implementing a recurring (cron-like) task in a TypeScript Golem agent by self-scheduling future invocations. Use when the user asks about periodic tasks, recurring jobs, cron-like scheduling, polling loops, heartbeats, or self-scheduling agents.

How to use this skill

Skill file

Recurring Tasks via Self-Scheduling (TypeScript)

Overview

A Golem agent can act as its own scheduler by scheduling one of its own methods to run again at the end of each invocation. This creates a durable, crash-resilient recurring task — if the agent restarts, the scheduled invocation is still pending and will fire at the designated time.

Because a method handler's this is bound to the agent's state (not to its other methods), factor the self-scheduling logic into a small module-level helper that binds a client to the current agent with client.bindSelf() and calls .schedule(). bindSelf reaches the same agent whether or not it has a key; client.upsert(id) would reach the keyed agent of id, which is another agent when the current one was created without a key.

Basic Pattern

The agent schedules its own poll method to run again after a delay.

Call start only once per agent identity in the basic and backoff variants;

each call enqueues another independent recurring chain:

import { z } from 'zod';
import { defineAgent, method } from '@golemcloud/golem-ts-sdk';

export const PollerAgent = defineAgent({
    name: 'PollerAgent',
    id: { name: z.string() },
    methods: {
        start: method({ input: {}, returns: z.void() }),
        poll: method({ input: {}, returns: z.void() }),
    },
});

// Self-scheduling helper: enqueue this agent's own `poll` to run after a delay.
function scheduleNext(delaySecs: bigint): void {
    const nowSecs = BigInt(Math.floor(Date.now() / 1000));
    PollerAgent.client.bindSelf().poll.schedule({ seconds: nowSecs + delaySecs, nanoseconds: 0 });
}

export const PollerAgentImpl = PollerAgent.implement({
    init: ({ id }) => ({ name: id.name }),
    methods: {
        start() {
            // Kick off the loop by enqueueing the first poll.
            scheduleNext(0n);
        },
        poll() {
            // 1. Do the recurring work.
            doWork();
            // 2. Schedule the next run (60 seconds from now).
            scheduleNext(60n);
        },
    },
});

Exponential Backoff

Increase the delay on repeated failures, reset on success. Keep the counters in the agent's state:

export const PollerAgentImpl = PollerAgent.implement({
    init: ({ id }) => ({
        name: id.name,
        consecutiveFailures: 0,
        baseIntervalSecs: 60n,
        maxIntervalSecs: 3600n,
    }),
    methods: {
        start() {
            scheduleNext(0n);
        },
        poll() {
            const success = tryWork();

            let delay: bigint;
            if (success) {
                this.consecutiveFailures = 0;
                delay = this.baseIntervalSecs;
            } else {
                this.consecutiveFailures++;
                const exp = Math.min(this.consecutiveFailures, 6);
                delay = this.baseIntervalSecs * BigInt(2 ** exp);
                if (delay > this.maxIntervalSecs) delay = this.maxIntervalSecs;
            }

            scheduleNext(delay);
        },
    },
});

Cancellation

For a restartable loop, include a generation in every scheduled tick. cancel

stops work logically; a pending tick still arrives but exits without doing work.

Repeated start calls while running are no-ops. After cancellation, start

advances the generation so an old tick cannot create a second chain.

This complete variant replaces the basic definition and implementation. It stores

only logical state, which the typed snapshot schema can restore:

import { z } from 'zod';
import { defineAgent, method } from '@golemcloud/golem-ts-sdk';

const PollerState = z.object({
    name: z.string(), running: z.boolean(), generation: z.number(),
});

export const PollerAgent = defineAgent({
    name: 'PollerAgent',
    id: { name: z.string() },
    snapshotting: { state: PollerState, policy: { everyNInvocations: 10 } },
    methods: {
        start: method({ input: {}, returns: z.void() }),
        poll: method({ input: { generation: z.number() }, returns: z.void() }),
        cancel: method({ input: {}, returns: z.void() }),
    },
});

function scheduleNext(generation: number, delaySecs: bigint): void {
    const nowSecs = BigInt(Math.floor(Date.now() / 1000));
    PollerAgent.client.bindSelf().poll.schedule({
        seconds: nowSecs + delaySecs, nanoseconds: 0,
    }, { generation });
}

export const PollerAgentImpl = PollerAgent.implement<z.infer<typeof PollerState>>({
    init: ({ id }) => ({
        name: id.name,
        running: false,
        generation: 0,
    }),
    methods: {
        start() {
            if (this.running) return;
            this.running = true;
            this.generation++;
            scheduleNext(this.generation, 0n);
        },
        poll({ generation }) {
            if (!this.running || this.generation !== generation) return;
            console.log('Polling', this.name); // Replace with recurring work.
            scheduleNext(generation, 60n);
        },
        cancel() {
            this.running = false;
            this.generation++;
        },
    },
});

Restore the running flag and generation together. Do not reset the generation or

enqueue a replacement during restoration: an existing scheduled tick may still arrive.

For best-effort removal before a pending invocation starts, .schedule() returns

a CancellationToken with .cancel(). That live host resource is not snapshot

state. Keep the generation check even when using a token; cancellation cannot

stop work that has already started. The logical variant above needs no token or

custom snapshot hooks.

Cancellation from the CLI

For the basic no-input poll contract, schedule from the CLI with an explicit

idempotency key, then cancel the pending invocation by key:

# Schedule with a known idempotency key
golem agent invoke --trigger --schedule-at 2026-03-15T10:30:00Z -i 'poll-next' 'PollerAgent("my-poller")' poll

# Cancel the pending invocation
golem invocation cancel --agent 'PollerAgent("my-poller")' --idempotency-key 'poll-next'

Common Use Cases

Periodic Polling

Check an external API or queue for new work at regular intervals:

poll() {
    const items = fetchPendingItems();
    for (const item of items) {
        process(item);
    }
    scheduleNext(60n);
}

Periodic Cleanup

Replace the basic pattern's poll handler with cleanup work (and initialize

entries in state):

poll() {
    this.entries = this.entries.filter((e) => !e.isExpired());
    scheduleNext(3600n); // run hourly
}

Heartbeat / Keep-Alive

Replace the basic pattern's poll handler with heartbeat work (and initialize

serviceUrl in state):

poll() {
    sendHeartbeat(this.serviceUrl);
    scheduleNext(30n); // every 30s
}

Helper for Scheduling Self

Keep the scheduling logic in one module-level helper so every method stays clean. PollerAgent.client is the typed RPC factory for this same agent type; its bindSelf() targets this instance:

function scheduleNext(delaySecs: bigint): void {
    const nowSecs = BigInt(Math.floor(Date.now() / 1000));
    PollerAgent.client.bindSelf().poll.schedule({ seconds: nowSecs + delaySecs, nanoseconds: 0 });
}

Key Points

Recovery & Oplog Growth

Each scheduled tick (heartbeat, poll, cleanup) appends entries to the agent's oplog. For long-running or high-frequency recurring tasks, the oplog grows unboundedly, and recovery on crash will replay the full history — which becomes slow over time.

You cannot opt out of oplog writes for a durable agent. Enable snapshot-based recovery with a schema for every ordinary state field and a policy, as in the cancellation variant above. For the basic pattern, use snapshotting: { state: z.object({ name: z.string() }), policy: { everyNInvocations: 10 } }. A policy without a state schema requires custom snapshot: { save, load } hooks, even for JSON state. See CODE2.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.