Golem mark read only scala

From golemcloud/golem · View interactive version

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

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). In exchange Golem:

Mark a method read-only with the @readOnly annotation.

Usage

import golem.runtime.annotations.{agentDefinition, readOnly}
import golem.BaseAgent

import scala.concurrent.Future

@agentDefinition()
trait CounterAgent extends BaseAgent {
  class Id(val name: String)

  // Non-read-only: writes shared state
  def increment(): Future[Long]

  // Read-only: pure read over already-loaded state
  @readOnly
  def getCount(): Future[Long]
}

Cache Policy

Pass cache = "..." (a string) to choose how long a cached result stays valid. The default is "until-write".

| Policy | Annotation | Behavior |

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

| Until write (default) | @readOnly or @readOnly(cache = "until-write") | Cached until the next non-read-only invocation on the same agent |

| TTL | @readOnly(cache = "ttl(30 seconds)") | Expires after the given duration even without a write |

| No cache | @readOnly(cache = "no-cache") | Runs every time; still side-effect-free, never cached |

The annotation takes a single cache: String argument. For TTL use the "ttl(<duration>)" form, where the duration parses as a Scala Duration (e.g. "ttl(30 seconds)", "ttl(500 millis)", "ttl(1 minute)").

import golem.runtime.annotations.readOnly

@readOnly(cache = "ttl(30 seconds)")
def recentSummary(): Future[Summary]

@readOnly(cache = "no-cache")
def pureCompute(x: Int, y: Int): Future[Int]

Per-Principal Caching (automatic)

There is no usesPrincipal argument on the annotation. Whether the cache is per-principal is derived automatically by the SDK from the method signature: if the method receives a Principal, the result is cached per principal and HTTP responses switch to Cache-Control: private with Vary: Authorization.

import golem.Principal

// Principal-aware: cached per principal (usesPrincipal auto-derived = true)
@readOnly
def myVisibleItems(principal: Principal): Future[List[Item]]

When the method does not receive a Principal, the result is shared across all callers and HTTP responses are Cache-Control: public (CDN-friendly). The Principal is auto-injected by the runtime — it is not part of the method's input schema and is not passed by callers (see golem-add-http-auth-scala).

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 cache contract and must be avoided by you.

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

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

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

| Computation over inputs | Outgoing HTTP requests | Reading the clock / current time |

| Returning derived values | RPC calls to other agents | Randomness |

| | | 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.