JDogo
Open playground

Context sync

Long agent sessions resend the same state on every turn. Send it once as a snapshot, then only what changed as a delta. Versions are checked, snapshots carry a hash, envelopes are signed, and Java, Python and TypeScript write the same bytes.

Experimental: Context sync is marked experimental in JDogo 2.1. Wire format, delta layout and session synchronization APIs are subject to stabilization in 2.2.

In Python jdogo.context, in TypeScript context from "jdogo", in Java jdogo-context-core plus the store modules. Agents get the same operations through the MCP server tools jdogo_diff and jdogo_patch. Shared vectors in context_vectors.json check all three languages byte for byte on every build.

Snapshot, diff, apply

from jdogo import context

v1 = context.create_snapshot("acme", "session-42", {"user": "ana", "step": 1})
delta = context.diff(v1, {"user": "ana", "step": 2, "cart": ["sku-1"]})
v2 = context.apply(v1, delta)

context.resolve(v2, "cart")             # ["sku-1"]
text = delta.to_wire()                  # send this
same = context.ContextDelta.from_wire(text)

On the wire, a snapshot carries its version and a SHA-256 hash of its state; a delta names the version it applies to:

!JDOGO/2 @mode SNAPSHOT @context session-42 @tenant acme @version 1 @time 1795788793538 @hash c76d70…
user=ana
step=1
@end

!JDOGO/2 @mode DELTA @context session-42 @tenant acme @base 1 @version 2
~step=2
+cart=[sku-1]
@end
LineMeans
~path=valueChange a value
+path=valueAdd a value
-pathRemove a value

Signed envelopes

Wrap a snapshot or delta with a digest and an HMAC-SHA256 signature, so a receiver can tell who sent it and that nothing changed on the way.

wire = context.encode_envelope(delta, "billing-svc", key)

env = context.decode_envelope(wire)
if not context.verify_signature(env, key):
    raise PermissionError("rejected context")
received = context.ContextDelta.from_wire(env.payload)

Verify before you apply. Keep keys out of code and rotate them like any other secret.

Live updates with server-sent events

In Python context.sse_event(delta) and in TypeScript context.sseEvent(delta) write the same frame as Java.

import com.jdogo.context.transport.*;

var bus = new InMemoryEventTransport();
AutoCloseable sub = bus.subscribe("acme", "session-42",
    event -> sseStream.send(SseEventFormatter.format(event)));

bus.publish(ContextEvent.delta("acme", "session-42", v2.getVersion(), delta.toWireFormat()));

SseEventFormatter writes a standard text/event-stream frame (event:, id:, data: lines). InMemoryEventTransport works within one process; across processes, publish the same frames through your own broker.

Stores

Every store keeps the latest snapshot and each version, per tenant.

ContextStore store = new InMemoryContextStore(3_600_000);                 // TTL in ms; tests and single process
ContextStore redis = RedisContextStore.connectOrFallback("redis", 6379, 86_400);
ContextStore sql   = SqliteContextStore.open("jdbc:sqlite:context.db");   // or any JDBC DataSource

store.save(v2);
store.load("acme", "session-42");               // latest
store.loadVersion("acme", "session-42", 1);     // a pinned version
store.listContexts("acme");
store.delete("acme", "session-42");             // latest and every version
StoreNotes
InMemoryContextStoreOne process; optional TTL
RedisContextStoreKeys under jdogo:context: with a TTL in seconds. clear() deletes only keys under its prefix, never the rest of the database. For a password, use new SocketRedisAdapter(host, port, timeoutMs, password). connectOrFallback logs a warning and keeps data in memory if Redis is unreachable; in production, construct the adapter yourself so a missing Redis fails at startup
SqliteContextStorePlain JDBC with one table, jdogo_context_snapshots. Add the driver yourself, for example org.xerial:sqlite-jdbc. The test suite runs it on H2

The Redis store checks tenant and context IDs before it builds keys, so a value such as acme\nevil is rejected. The SQL store passes them as bound parameters.

Metrics and tracing

jdogo-observability has a small recorder and W3C trace context, with no dependencies, to feed your own metrics system.

import com.jdogo.observability.*;

var metrics = new ContextMetricsRecorder.InMemoryMetricsRecorder();
metrics.recordDeltaProcessed();
metrics.recordTokensSaved(jsonTokens - deltaTokens);
var snap = metrics.snapshot();                  // totals and average diff and patch times

var trace = ContextTraceContext.parseW3CTraceparent(request.getHeader("traceparent"));
outgoing.setHeader("traceparent", trace.createChildSpan().toW3CTraceparent());