Golem read only method effect

From golemcloud/golem · View interactive version

Golem read only method effect is an AI agent skill from the GitHub repository golemcloud/golem. Marking Effect 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 (Effect)

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 through durable host APIs (key-value/blob storage, databases, promises, agent management), outgoing HTTP, and RPC calls to other agents trap at runtime with a typed ReadOnlyViolation agent error before they run — but it does not detect every source of impurity (in-memory mutation, clocks, randomness, env reads, remote/blob reads, file writes), so keeping the method pure is the author's responsibility (see What Works in a Read-Only Method).

Marking a method read-only lets the host bypass the invocation queue and agent loading on a cache hit, and emit HTTP cache headers (Cache-Control, ETag, Vary) for GET/HEAD routes.

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

Usage

import { Effect, Ref, Schema } from "effect";
import { defineAgent, method, Principal } from "@golemcloud/effect-golem";

export const CounterAgent = defineAgent({
  name: "CounterAgent",
  mode: "durable",
  id: { name: Schema.String },
  methods: {
    // Non-read-only: mutates state held in Ref
    increment: method({ input: {}, success: Schema.Number }),
    // Read-only: pure read over already-loaded state (until-write cache default)
    getCount: method({ input: {}, success: Schema.Number, readOnly: true }),
    // Cache options: TTL expiration or never cached ({ cache: "no-cache" })
    recent: method({
      input: {},
      success: Schema.Number,
      readOnly: { cache: { ttlNanos: 30_000_000_000n } },
    }),
    // Principal-aware: cached per caller (derived from PrincipalSchema input)
    myCount: method({
      input: { caller: Principal.PrincipalSchema },
      success: Schema.Number,
      readOnly: true,
    }),
  },
}).implement({
  init: () => Ref.make(0),
  methods: (count) => ({
    increment: () => Ref.updateAndGet(count, (value) => value + 1),
    getCount: () => Ref.get(count),
    recent: () => Ref.get(count),
    myCount: () => Ref.get(count),
  }),
});

Cache policy & per-principal caching. readOnly: true uses the until-write policy (cached until the next mutating invocation on the agent). For finer control, pass an object: readOnly: { cache: 'no-cache' | 'until-write' | { ttlNanos: <bigint> } } (no-cache never caches but enforces side-effect-free rules; { ttlNanos } caches for that duration). Declaring a Principal.PrincipalSchema input automatically keys the cache per caller principal (Cache-Control: private). A read-only handler without that declared input cannot access the ambient Principal service (see golem-add-http-auth-effect). 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. Operations in the middle column go through Golem's durability layer and trap with a typed ReadOnlyViolation before they run and before anything is persisted. Operations in the right column are not detected — they do not trap, so keeping them out is your responsibility.

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

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

| Reading agent state (Ref.get(ref)) | Writing persistent state (storage, databases, promises, agent management) | Mutating in-memory state (Ref.set, Ref.update, property assignment) |

| Computation over inputs | Outgoing HTTP (fetch, HttpClient) | Reading the clock / current time (Date.now(), Clock.currentTimeMillis) |

| Returning derived / aggregated values | RPC calls to other agents | Randomness (Math.random(), Random) |

| | | Reading environment variables (process.env, ConfigProvider) |

| | | Remote / blob reads or file writes |

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.