Golem mark read only ts

From golemcloud/golem · View interactive version

Golem mark read only ts is an AI agent skill from the GitHub repository golemcloud/golem. Marking TypeScript agent methods as read-only for a side-effect-free guarantee and result caching. Use when the user wants a cacheable query method, a method that must not write to the oplog, or HTTP GET endpoints that emit cache headers.

How to use this skill

Skill file

Marking Agent Methods as Read-Only (TypeScript)

Overview

A read-only agent method is one you promise is a pure read of the agent's already-loaded state: it must not mutate anything and its result must depend only on its inputs and the current state. Golem enforces the most important part of this contract — writes to persistent state, outgoing HTTP, and RPC calls trap at runtime with a ReadOnlyViolation agent error before they run — but it does not detect every source of impurity (in-memory mutation, clocks, randomness, env reads), so keeping the method pure is partly your responsibility (see What Works in a Read-Only Method).

Marking a method read-only also lets the host treat it as a side-effect-free query: it may bypass the invocation queue and agent loading, and read-only methods mapped to GET/HEAD participate in HTTP caching semantics.

Mark a method read-only by setting readOnly: true on the method(...) spec.

Usage

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

export const CounterAgent = defineAgent({
    name: 'CounterAgent',
    id: { name: z.string() },
    methods: {
        // Non-read-only: writes shared state
        increment: method({ input: {}, returns: z.number() }),
        // Read-only: pure read over already-loaded state
        getCount: method({ input: {}, returns: z.number(), readOnly: true }),
    },
});

export const CounterAgentImpl = CounterAgent.implement({
    init: () => ({ count: 0 }),
    methods: {
        increment() {
            this.count += 1;
            return this.count;
        },
        getCount() {
            return this.count;
        },
    },
});

Cache policy. readOnly: true uses the until-write cache policy (the base-SDK default). For finer control, pass an object instead of the boolean:

readOnly: { cache: 'no-cache' | 'until-write' | { ttlNanos: <bigint> } } — no-cache never caches, until-write caches until a mutating (non-read-only) method runs, and { ttlNanos } caches for that time-to-live. A direct s.principal() method input automatically keys the cache per caller principal. Principal values nested in other inputs or returned by the method do not enable per-principal caching. Reach for a regular (non-read-only) method whenever you need a side effect.

What Works in a Read-Only Method

A read-only method must be a pure function of the agent's already-loaded state and the method inputs. The operations in the middle column go through Golem's durability layer and trap with a ReadOnlyViolation agent error before they run and before anything is persisted. The operations in the right column are not detected — they do not trap, but they still break the contract and must be avoided by you.

| Allowed | Not allowed — traps with ReadOnlyViolation | Not allowed — not checked, your responsibility |

|---|---|---|

| Reading this state fields | Writing persistent state (storage, databases, …) | Mutating in-memory state |

| Computation over inputs | Outgoing HTTP (fetch) | Reading the clock / Date.now() |

| Returning derived values | RPC calls to other agents | Randomness (Math.random()) |

| | | Reading environment variables |

| | | Remote / blob reads |

Common Pitfalls

Key Points

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.