Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ Completed conversations are captured automatically:

On OpenCode 2, each directory keeps its own tools and hooks, and only handles events for its sessions. Accepted capture batches and fully submitted turn fingerprints are recorded in host plugin storage, scoped to the API destination, namespace, directory and session. Reloads and restarts suppress those confirmed uploads; a storage read error defers capture rather than treating history as unsaved. Failed writes remain retryable without a permanent attempt cap.

Older versions kept acceptance only in memory, so the first resume after upgrading has no durable evidence of earlier uploads and may retry historical content. Original host transcripts and configuration are retained; no historical batch is invented as accepted. A timed-out request may also have reached the server: neither stable IDs nor a queued acceptance proves exactly-once processing. If an ACK cannot be persisted, it is retained across same-process replacement, but a process exit before storage recovers still leaves that uncertainty. The existing 100,000-character upload limit remains; truncated turns are not recorded as fully submitted.
Older versions kept acceptance only in memory, so the first resume after upgrading has no durable evidence of earlier uploads and may retry historical content. Original host transcripts and configuration are retained; no historical batch is invented as accepted. A timed-out request may also have reached the server: neither stable IDs nor a queued acceptance proves exactly-once processing. If an ACK cannot be persisted, it is retained across same-process replacement, but a process exit before storage recovers still leaves that uncertainty.

OpenCode 2 keeps the 100,000-character formatted request budget without truncating eligible capture text. Oversized batches are sent by turn; oversized turns are split into deterministic message fragments that preserve internal whitespace and Unicode code points. Each received fragment ACK is checkpointed independently, failed fragments remain retryable, and a turn is complete only after all its pieces are accepted. Ordinary batches retain their existing IDs and formatting. OpenCode V1's existing truncation behavior is unchanged.

Fragment progress uses a version-two ledger. The version-one ledger from 2.0.18 is retained unchanged until explicit session deletion, and only its valid full-turn fingerprints and compaction ACKs are imported. That old format has no credential provenance, so importing its progress assumes an unchanged account at the first upgrade. An old truncated batch ACK is not proof of full coverage, so that first upgrade may repeat its previously accepted prefix while delivering the missing content. Downgrading to a runtime that cannot read fragment progress may also resend confirmed content; retained old records and host transcripts remain recoverable, not a no-replay downgrade guarantee.

Before sending, OpenCode 2 checkpoints only the selected bounded payloads and their manifest in plugin-private host storage. This adds local retention of capture-policy-selected text, not new redaction or encryption. UnACKed payloads survive compaction, process restart and unsuccessful deletion flushes; recovery drains the stored selection when its original project plugin loads, without backfilling unrelated history. Payloads are garbage-collected only after their ACK progress is durably committed. There is no automatic expiry/drop cutoff or unlimited disk-capacity guarantee: a failed pending checkpoint defers network delivery, and a process exit before that checkpoint succeeds still depends on recoverable host originals.

Pending records and new ACK ledgers bind the configured API destination, protocol, namespace, project, session and a one-way credential-context fingerprint. Changed or unrecognized provenance/schema fails closed and retains originals; even a same-account key rotation may require restoring the original context to recover pending content. Raw credentials and API URLs are not copied into these records. Late server acceptance before a received/durable ACK remains ambiguous and may be retried. Splitting oversized content adds write operations; production extraction, ordering and billing equivalence are not implied by complete text delivery.

### Keyword Detection

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opencode-supermemory",
"version": "2.0.18",
"version": "2.0.19",
"description": "OpenCode plugin that gives coding agents persistent memory using Supermemory",
"type": "module",
"main": "dist/index.js",
Expand Down
59 changes: 24 additions & 35 deletions src/services/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Supermemory from "supermemory-legacy";
import { createHash } from "node:crypto";
import { V5Client } from "./v5-client.js";
import {
CONFIG,
Expand All @@ -9,6 +10,11 @@ import {
isConfigured,
} from "../config.js";
import { log } from "./logger.js";
import {
formatConversationContent,
formatConversationMessage,
MAX_CONVERSATION_CHARS,
} from "./conversation.js";
import {
mergeListResponses,
mergeProfileResponses,
Expand All @@ -23,7 +29,6 @@ import type {
const TIMEOUT_MS = 30000;
const TIMEOUT_BACKSTOP_GRACE_MS = 250;
const SETTINGS_UPDATE_TIMEOUT_MS = 3_000;
const MAX_CONVERSATION_CHARS = 100_000;
const OPENCODE_SOURCE = "opencode";

export interface MemoryRequestOptions {
Expand Down Expand Up @@ -128,52 +133,32 @@ function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {

export class SupermemoryClient {
private client: Supermemory | V5Client | null = null;
private captureDestination: { baseUrl: string; apiVersion: "legacy" | "v5"; account: string } | undefined;

private formatConversationMessage(message: ConversationMessage): string {
const content =
typeof message.content === "string"
? message.content
: message.content
.map((part) =>
part.type === "text"
? part.text
: `[image] ${part.imageUrl.url}`,
)
.join("\n");

const trimmed = content.trim();
if (trimmed.length === 0) {
return `[${message.role}]`;
}
return `[${message.role}] ${trimmed}`;
}

private formatConversationTranscript(
messages: ConversationMessage[],
): string {
return messages
.map(
(message, idx) =>
`${idx + 1}. ${this.formatConversationMessage(message)}`,
)
.join("\n");
getCaptureDestination(): { baseUrl: string; apiVersion: "legacy" | "v5"; account: string } {
const bound = this.captureDestination ??= {
baseUrl: getApiBaseUrl(), apiVersion: getApiVersion(),
account: createHash("sha256").update(SUPERMEMORY_API_KEY ?? "unconfigured").digest("hex"),
};
return { ...bound };
}

private getClient(): Supermemory | V5Client {
if (!this.client) {
if (!isConfigured()) {
throw new Error("SUPERMEMORY_API_KEY not set");
}
const destination = this.getCaptureDestination();
const legacyClient = new Supermemory({
apiKey: SUPERMEMORY_API_KEY,
baseURL: getApiBaseUrl(),
baseURL: destination.baseUrl,
defaultHeaders: { "x-sm-source": OPENCODE_SOURCE },
});
this.client =
getApiVersion() === "v5"
destination.apiVersion === "v5"
? new V5Client(
SUPERMEMORY_API_KEY!,
getApiBaseUrl(),
destination.baseUrl,
legacyClient.settings,
TIMEOUT_MS - TIMEOUT_BACKSTOP_GRACE_MS,
)
Expand Down Expand Up @@ -644,6 +629,8 @@ export class SupermemoryClient {
entityContextByContainerTag?: Record<string, string>;
customId?: string;
timeoutMs?: number;
requireComplete?: boolean;
preserveWhitespace?: boolean;
},
) {
log("ingestConversation: start", {
Expand All @@ -666,16 +653,18 @@ export class SupermemoryClient {
};
}

const transcript = this.formatConversationTranscript(messages);
const rawContent = `[Conversation ${conversationId}]\n${transcript}`;
const rawContent = formatConversationContent(conversationId, messages, options?.preserveWhitespace);
if (options?.requireComplete && rawContent.length > MAX_CONVERSATION_CHARS) {
return { success: false as const, error: "Conversation exceeds the complete-submission budget" };
}
const content =
rawContent.length > MAX_CONVERSATION_CHARS
? `${rawContent.slice(0, MAX_CONVERSATION_CHARS)}\n...[truncated]`
: rawContent;
let submittedMessageCount = 0;
let submittedLength = `[Conversation ${conversationId}]\n`.length;
for (const [index, message] of messages.entries()) {
submittedLength += `${index + 1}. ${this.formatConversationMessage(message)}`.length;
submittedLength += `${index + 1}. ${formatConversationMessage(message, options?.preserveWhitespace)}`.length;
if (submittedLength > MAX_CONVERSATION_CHARS) break;
submittedMessageCount += 1;
submittedLength += 1;
Expand Down
30 changes: 30 additions & 0 deletions src/services/conversation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ConversationMessage } from "../types/index.js";

export const MAX_CONVERSATION_CHARS = 100_000;

export function conversationMessageText(message: ConversationMessage): string {
return (typeof message.content === "string"
? message.content
: message.content.map((part) => part.type === "text"
? part.text
: `[image] ${part.imageUrl.url}`).join("\n")).trim();
}

export function formatConversationMessage(
message: ConversationMessage,
preserveWhitespace = false,
): string {
const content = preserveWhitespace && typeof message.content === "string"
? message.content
: conversationMessageText(message);
return content.length === 0 ? `[${message.role}]` : `[${message.role}] ${content}`;
}

export function formatConversationContent(
conversationId: string,
messages: ConversationMessage[],
preserveWhitespace = false,
): string {
return `[Conversation ${conversationId}]\n${messages.map((message, index) =>
`${index + 1}. ${formatConversationMessage(message, preserveWhitespace)}`).join("\n")}`;
}
106 changes: 106 additions & 0 deletions src/v2/capture-plan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { createHash } from "node:crypto";

import { getCaptureId, type CaptureBatch, type CaptureTurn } from "../services/capture.js";
import {
conversationMessageText,
formatConversationContent,
MAX_CONVERSATION_CHARS,
} from "../services/conversation.js";
import type { ConversationMessage } from "../types/index.js";

export interface CapturePiece {
id: string;
acknowledgedId: string;
conversationId: string;
messages: ConversationMessage[];
startTurn: number;
endTurn: number;
preserveWhitespace: boolean;
metadata: Record<string, string | number | boolean>;
}

export interface CaptureGroup {
turnFingerprints: string[];
pieces: CapturePiece[];
}

function sha256(value: string): string {
return createHash("sha256").update(value).digest("hex");
}

function wholePiece(sessionID: string, batch: CaptureBatch): CapturePiece {
const messages = batch.turns.flatMap((turn) => turn.messages.map((message) => ({
role: message.role, content: conversationMessageText(message),
})));
const id = getCaptureId(sessionID, batch);
return {
id,
acknowledgedId: `${id}:${sha256(JSON.stringify(messages))}`,
conversationId: `${sessionID}:${batch.startTurn}-${batch.endTurn}`,
messages,
startTurn: batch.startTurn,
endTurn: batch.endTurn,
preserveWhitespace: false,
metadata: {},
};
}

export function planCapture(
sessionID: string,
batch: CaptureBatch,
positions?: readonly number[],
): CaptureGroup[] {
const whole = wholePiece(sessionID, batch);
if (formatConversationContent(whole.conversationId, whole.messages).length <= MAX_CONVERSATION_CHARS) {
return [{ turnFingerprints: batch.turns.map((turn) => sha256(JSON.stringify(turn))), pieces: [whole] }];
}

return batch.turns.map((turn, turnIndex) => {
const position = positions?.[turnIndex] ?? batch.startTurn + turnIndex;
const single = wholePiece(sessionID, { startTurn: position, endTurn: position, turns: [turn] });
if (formatConversationContent(single.conversationId, single.messages).length <= MAX_CONVERSATION_CHARS) {
return { turnFingerprints: [sha256(JSON.stringify(turn))], pieces: [single] };
}

const fingerprint = sha256(JSON.stringify(turn));
const pieces: CapturePiece[] = [];
for (const [messageIndex, message] of turn.messages.entries()) {
const text = conversationMessageText(message);
let start = 0;
let chunkIndex = 0;
do {
const conversationId = `${sessionID}:${turn.id}:m${messageIndex}:c${chunkIndex}`;
const empty = { role: message.role, content: "" };
const budget = MAX_CONVERSATION_CHARS - formatConversationContent(conversationId, [empty], true).length - 1;
if (budget < 2) throw new Error("Capture identity exceeds the fragment budget");
let end = Math.min(start + budget, text.length);
if (end < text.length &&
text.charCodeAt(end - 1) >= 0xd800 && text.charCodeAt(end - 1) <= 0xdbff &&
text.charCodeAt(end) >= 0xdc00 && text.charCodeAt(end) <= 0xdfff) end -= 1;
const messages = [{ role: message.role, content: text.slice(start, end) }];
const id = `opencode:capture:${sha256(JSON.stringify([sessionID, turn.id, fingerprint, messageIndex, chunkIndex]))}`;
pieces.push({
id,
acknowledgedId: `${id}:${sha256(JSON.stringify(messages))}`,
conversationId,
messages,
startTurn: position,
endTurn: position,
preserveWhitespace: true,
metadata: {
captureFragment: true,
captureTurnId: turn.id,
captureTurnFingerprint: fingerprint,
captureMessageIndex: messageIndex,
captureChunkIndex: chunkIndex,
captureChunkStart: start,
captureChunkEnd: end,
},
});
start = end;
chunkIndex += 1;
} while (start < text.length);
}
return { turnFingerprints: [fingerprint], pieces };
});
}
Loading
Loading