Skip to content

Commit 128ea35

Browse files
committed
fix(workspace): address consensus review on workspace memory
- **C1**: `MemoryStore.list`/`listAll` accept `opts.directory` so callers with no ambient `Instance` — the `link` subcommand — can read project scope. `read`/`blockPath` now thread that directory too; without it `list` scanned the right directory but read every block back from the wrong one, so project blocks silently vanished. - **C2**: `hydrate` is idempotent per session id and no longer preceded by `resetOverlay` on every user turn (`step === 1` runs per turn, not per session), which made memory blink out of the prompt mid-fetch. - **C3**: overlay and hydration state are keyed by session id, so concurrent sessions in different workspaces cannot read each other's memory. - **M1**: reads report `truncated`; writes are suppressed against a truncated view rather than creating duplicates of records that were paged out. - **M3**: `expires` is mirrored as `block_expires` and honoured on read, so a TTL'd block cannot outlive its expiry on other machines. - **M4**: backfill prefetches known records once instead of re-listing per block. - **m1**: declined extractions are accounted separately from stored ones. - `listAll` uses `Promise.allSettled`, so an unreadable project scope no longer takes global memory down with it. - Remote blocks no longer drive `incrementApplied`; the training-meta comment regex is exported so the mirror's content hash cannot drift from the writer. Tests: `store-directory.test.ts` exercises the real `MemoryStore` (the existing `store.test.ts` re-implements its logic and so cannot catch path-resolution bugs). Every invariant above was mutation-checked — each fails when its guard is removed.
1 parent 9dd716a commit 128ea35

13 files changed

Lines changed: 843 additions & 117 deletions

File tree

‎packages/opencode/src/altimate/training/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
import z from "zod"
33

44
export const TRAINING_TAG = "training"
5+
6+
/** Matches the metadata comment ``embedTrainingMeta`` writes at the head of a
7+
* training block's content. Exported so readers that normalise it away — the
8+
* workspace mirror hashes content without it, because the applied counter is
9+
* rewritten every session — cannot drift from the writer. */
10+
export const TRAINING_META_COMMENT = /^<!--\s*training\n[\s\S]*?-->\n*/
511
export const TRAINING_ID_PREFIX = "training"
612
// altimate_change start — increase training limits for enterprise teams
713
// 20 entries per kind is too restrictive for teams with 200+ dbt models spanning

‎packages/opencode/src/altimate/workspace/memory-api.ts‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ export interface MirrorMetadata {
5959
repo_remote?: string
6060
project_path?: string
6161
block_tags?: string
62+
/** ISO-8601. Mirrored so a TTL'd block expires everywhere rather than living
63+
* forever in the workspace once it has left the machine that wrote it. */
64+
block_expires?: string
6265
archived?: "true"
6366
archived_at?: string
6467
}
@@ -73,14 +76,19 @@ export function isArchived(record: CloudMemoryRecord): boolean {
7376
return record.metadata?.archived === "true"
7477
}
7578

76-
/** Pull the created record's id out of a create response.
79+
/** Pull every created record id out of a create response.
7780
*
78-
* The store's payload shape is not part of any published contract, so this
79-
* accepts the observed forms and reports failure rather than guessing: a bare
80-
* array of records, or an object wrapping one under ``results``/``memories``.
81-
* Returns undefined when nothing was stored — which is the expected outcome
81+
* A create runs an extractor server-side, and an extractor is free to split one
82+
* submission into several records — each carrying the metadata we sent, so each
83+
* looks like our block. Returning only the first would leave the rest holding
84+
* rewritten text, never repaired, never indexed and never archived, while the
85+
* read path injected all of them under one block id.
86+
*
87+
* The payload shape is not a published contract, so this accepts the observed
88+
* forms rather than guessing: a bare array, or an object wrapping one under
89+
* ``results``/``memories``/``data``. An empty result is the expected outcome
8290
* when the extractor declines the content. */
83-
export function extractRecordId(result: unknown): string | undefined {
91+
export function extractRecordIds(result: unknown): string[] {
8492
const rows = (() => {
8593
if (Array.isArray(result)) return result
8694
if (result && typeof result === "object") {
@@ -92,22 +100,28 @@ export function extractRecordId(result: unknown): string | undefined {
92100
return []
93101
})()
94102

103+
const ids: string[] = []
95104
for (const row of rows) {
96105
if (!row || typeof row !== "object") continue
97106
const id = (row as Record<string, unknown>).id ?? (row as Record<string, unknown>).memory_id
98-
if (typeof id === "string" && id) return id
107+
if (typeof id === "string" && id) ids.push(id)
99108
}
100-
return undefined
109+
return ids
110+
}
111+
112+
/** Convenience for callers that only need to know whether anything was stored. */
113+
export function extractRecordId(result: unknown): string | undefined {
114+
return extractRecordIds(result)[0]
101115
}
102116

103117
export namespace MemoryApi {
104-
/** Create a record and report its id.
118+
/** Create a record and report the ids it produced.
105119
*
106-
* Returns undefined when the service stored nothing. That is not an error —
120+
* Returns an empty array when the service stored nothing. That is not an error —
107121
* the extractor declines content it judges unremarkable — so the caller
108122
* should leave the block unindexed and let a later edit retry, rather than
109123
* treating it as a failure. */
110-
export async function add(content: string, metadata: MirrorMetadata): Promise<string | undefined> {
124+
export async function add(content: string, metadata: MirrorMetadata): Promise<string[]> {
111125
const res = await altimateRequest<{ message?: string; result?: unknown }>("POST", "/", {
112126
base: BASE,
113127
allowEmptyBody: true,
@@ -116,7 +130,7 @@ export namespace MemoryApi {
116130
memory_options: { metadata },
117131
},
118132
})
119-
return extractRecordId(res?.result)
133+
return extractRecordIds(res?.result)
120134
}
121135

122136
/** Overwrite a record verbatim. Does not run the extractor, and replaces the

‎packages/opencode/src/altimate/workspace/memory-backfill.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import { MemoryStore } from "@/memory/store"
1313
import { Log } from "@/altimate/util/log"
1414
import { backfill, isEnabled } from "./memory-sync"
15+
import type { CachedBinding } from "./state"
1516

1617
const log = Log.create({ service: "altimate-workspace-memory-backfill" })
1718

@@ -22,12 +23,17 @@ const log = Log.create({ service: "altimate-workspace-memory-backfill" })
2223
* Covers both scopes: project blocks attach to the workspace just bound, and
2324
* global blocks go up account-level. A bind is the only moment global memory is
2425
* swept; blocks written later ride the ordinary per-write mirror. */
25-
export async function backfillOnBind(): Promise<void> {
26+
export async function backfillOnBind(directory: string, binding: CachedBinding): Promise<void> {
2627
if (!isEnabled()) return
2728
try {
28-
const blocks = await MemoryStore.listAll()
29+
// The directory and binding are passed in rather than rediscovered. The
30+
// `link` subcommand binds from a plain yargs handler with no instance
31+
// context, so resolving project scope from the ambient instance throws
32+
// there — silently, because this catch turns it into a log line while the
33+
// CLI still prints "Linked". Reading project memory was the entire point.
34+
const blocks = await MemoryStore.listAll({ directory })
2935
if (blocks.length === 0) return
30-
const result = await backfill(blocks)
36+
const result = await backfill(blocks, binding)
3137
log.info("workspace memory seeded after bind", result)
3238
} catch (err) {
3339
log.warn("workspace memory backfill after bind failed", { err: String(err) })

‎packages/opencode/src/altimate/workspace/memory-index.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,13 @@ function matches(file: IndexFile, scope: Scope): boolean {
141141

142142
// Serializes read-modify-write within this process. Two mirror tasks finishing
143143
// at once would otherwise both read the pre-write file and the second would
144-
// drop the first's entry. Cross-process races remain — the same known gap as
145-
// ./state.ts — and cost a duplicate record, which the next save repairs.
144+
// drop the first's entry.
145+
//
146+
// Cross-process races remain — the same known gap as ./state.ts. Two CLI
147+
// processes writing the same block concurrently can create two records, and
148+
// that duplicate is NOT self-repairing: a later save resolves one of them and
149+
// updates it, leaving the other live indefinitely. Closing it needs a file lock
150+
// here, or an idempotency key at the service.
146151
let writeChain: Promise<void> = Promise.resolve()
147152

148153
export async function readIndex(): Promise<Record<string, IndexEntry>> {

0 commit comments

Comments
 (0)