Skip to content

Instantly share code, notes, and snippets.

View igalshilman's full-sized avatar

Igal Shilman igalshilman

View GitHub Profile
@igalshilman
igalshilman / offload.ts
Created September 3, 2026 16:18
An example of how to asynchronously offload a long conversation to a cold storage.
/**
* Standalone example: lazy conversation history with cold-prefix offload.
*
* ## What is stored
*
* Metadata and message segments are stored as independent values. `append`
* and `offloadPrefix` opt into lazy state because they touch only selected
* segments; `applyOffload` and `history` use eager state:
*
* - `history/meta` holds `messageSequence`, `headSegment`, `tailSegment`, and
@igalshilman
igalshilman / conversation-history.ts
Created August 28, 2026 14:28
Restate lazy conversation history with filesystem prefix offload
/**
* Standalone example: lazy conversation history with cold-prefix offload.
*
* ## What is stored
*
* Metadata and message segments are stored as independent values. `append`
* and `offloadPrefix` opt into lazy state because they touch only selected
* segments; `applyOffload` and `history` use eager state:
*
* - `history/meta` holds `messageSequence`, `headSegment`, `tailSegment`, and
@igalshilman
igalshilman / durable-async-generator.ts
Created August 27, 2026 15:48
DurableAsyncGenerator for Restate — journals each next() so yielded values replay durably, with an inline handler demo
import {
service,
serve,
type Context,
RestatePromise,
TerminalError,
} from "@restatedev/restate-sdk";
type GeneratorState<T, TReturn> =
| { type: "open"; gen: AsyncGenerator<T, TReturn> }
@igalshilman
igalshilman / app.ts
Last active June 25, 2026 10:36
Restate + OpenAI: durably stream structured JSON (JSONL) agent events. Each line is parsed into a typed AgentEvent and journaled via durableSource, so replay never re-calls the model.
import {
Operation,
object,
run,
} from "@restatedev/restate-sdk-gen";
import { serve } from "@restatedev/restate-sdk";
import OpenAI from "openai";
import * as utils from "./utils";
const openai = new OpenAI(); // reads OPENAI_API_KEY from the environment
@igalshilman
igalshilman / app.ts
Created June 24, 2026 19:07
Restate signal-stream example: an agent VO consuming an LLM service's output chunk-by-chunk via durable signals
import {
Operation,
object,
service,
invocation,
sendClient,
handlerRequest,
run,
} from "@restatedev/restate-sdk-gen";
import { serve, InvocationId } from "@restatedev/restate-sdk";

Effect Handlers in TypeScript

OCaml-style algebraic effects built on TypeScript generators. The fiber holds the iterator and a small state machine. The continuation is a one-shot, safety-checked handle. The handler is an async scheduler that drains synchronously-runnable work and parks while external wakers are pending.

Three pieces, each with one job:

  • Fiber<T> — iteration state machine

restate-sdk-gen internals: fibers, the scheduler, and the stack

A walkthrough of how user generators, fibers, and the scheduler interact in @restatedev/restate-sdk-gen. Aimed at JS engineers who already know Restate and durable execution; we skip framing on journals/replay and focus on the runtime model.

Source files referenced throughout:

  • src/scheduler.ts — orchestration

RocksDB SIGTRAP on Apple Silicon: Hardened libc++ vs Custom Aligned Allocators

Summary

RocksDB crashes with SIGTRAP (EXC_BREAKPOINT, brk #0x1) on Apple Silicon (aarch64-apple-darwin) when statistics are enabled. The crash occurs inside StatisticsImpl::recordTick or StatisticsImpl::recordInHistogram during normal database operations, including DB::Open.

The root cause is an incompatibility between Apple Clang's hardened libc++ bounds checking and RocksDB's custom cache-line-aligned allocator used by StatisticsData.

Symptoms

R = typing.TypeVar('R', bound=typing.Callable[..., typing.Awaitable[typing.Any]])
P = typing.ParamSpec('P'
def handler(func):
func._is_handler = True
return func
class Box:
__slots__ = ('value',)
import {
service,
serve,
type Context,
RestatePromise,
TerminalError,
} from "@restatedev/restate-sdk";
/***
* A durable async generator that persists its state across restarts.