Skip to content

Commit 975008a

Browse files
committed
fix(wiki): index the operator's pages again when the folder's agent id changes
.meta/index.json recorded only the key version, so a folder indexed for one agent id and opened under another (agentId or name edited, or a second agent on the same folder) skipped every unchanged page: no chunk was filed under the new operator key and no turn recalled the operator's pages. The index now records the operator key it filed under. An index of another key is not used: every operator page is indexed again under the current key and the chunks filed under the other key are deleted. Signed-off-by: jddunn <[email protected]>
1 parent 2bc3eac commit 975008a

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

‎docs/SOUL_FILES.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ recall under a key of their own, the operator's conversation scope of the agent
174174
the version of that key as `keyVersion`. A folder an earlier version indexed has no
175175
`keyVersion`: its operator pages are indexed again under the operator key at the first
176176
start, and the chunks the earlier version filed under the agent's plain scope are left
177-
in the store. When an operator page is edited, its old chunks are deleted from the
177+
in the store. It records the key itself as `operatorKey`: a folder opened under another
178+
agent id (its `agentId` or `name` changed, or another agent opened it) has its operator
179+
pages indexed again under the current agent's key, and the chunks filed under the other
180+
key are deleted. When an operator page is edited, its old chunks are deleted from the
178181
store for good and the page is indexed again; the chunks of a page whose file is gone
179182
are deleted the same way.
180183

‎src/cognition/substrate/memory/wiki/WikiMemoryStore.ts‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,18 @@ export class WikiMemoryStore implements WikiPageStorePort {
348348
*
349349
* An index written before the operator key existed (no `keyVersion`) names chunks filed under the agent's plain
350350
* scope: its entries are not used, every operator page is indexed afresh, and those earlier chunks are left where
351-
* they are.
351+
* they are. An index whose entries were filed under another operator key (the folder's agent id changed, or another
352+
* agent opened the folder) is not used either: every operator page is indexed afresh under this agent's key, and
353+
* the chunks its entries name, which no turn of this agent reads, are deleted.
352354
*/
353355
async index(opts?: { force?: boolean }): Promise<IndexResult> {
354356
const pages = (await this.load()).filter((page) => originOfPage(page) === 'operator');
355-
const meta = await this.currentMeta();
357+
const { meta, stale } = await this.currentMeta();
356358
const result: IndexResult = { indexed: [], skipped: [], removed: [] };
359+
if (stale.length > 0) {
360+
await this._port.deleteTraces(stale);
361+
result.removed.push(...stale);
362+
}
357363

358364
for (const page of pages) {
359365
const hash = createHash('sha256').update(page.body).digest('hex');
@@ -398,21 +404,31 @@ export class WikiMemoryStore implements WikiPageStorePort {
398404

399405
/** Full rebuild: delete every indexed page's chunks, clear the meta map and re-embed everything. */
400406
async rebuildIndex(): Promise<IndexResult> {
401-
const meta = await this.currentMeta();
402-
const traceIds = Object.values(meta.pages).flatMap((entry) => entry.traceIds);
407+
const { meta, stale } = await this.currentMeta();
408+
const traceIds = [...stale, ...Object.values(meta.pages).flatMap((entry) => entry.traceIds)];
403409
if (traceIds.length > 0) await this._port.deleteTraces(traceIds);
404410
await this.writeMeta({ ...meta, pages: {} });
405411
return this.index({ force: true });
406412
}
407413

408414
/**
409-
* `.meta/index.json` as an index of chunks filed under the operator key: the file as it is when it carries that
410-
* key's version, and otherwise the file with no page entry, marked with the version for its next write.
415+
* `.meta/index.json` as an index of chunks filed under this agent's operator key: the file as it is when it carries
416+
* that key's version and names that key, and otherwise the file with no page entry, marked with the version and the
417+
* key for its next write.
418+
*
419+
* @returns The index, and the chunks of the entries it left out that were filed under another operator key, for the
420+
* caller to delete; an index of the agent's plain scope leaves its chunks where they are.
411421
*/
412-
private async currentMeta(): Promise<MetaIndex> {
422+
private async currentMeta(): Promise<{ meta: MetaIndex; stale: string[] }> {
413423
const meta = await this.readMeta();
414-
if (meta.keyVersion === META_KEY_VERSION) return meta;
415-
return { ...meta, keyVersion: META_KEY_VERSION, pages: Object.create(null) as MetaIndex['pages'] };
424+
const operatorKey = this.operatorKey;
425+
if (meta.keyVersion === META_KEY_VERSION && meta.operatorKey === operatorKey) return { meta, stale: [] };
426+
const stale =
427+
meta.keyVersion === META_KEY_VERSION ? Object.values(meta.pages).flatMap((entry) => entry.traceIds) : [];
428+
return {
429+
meta: { ...meta, keyVersion: META_KEY_VERSION, operatorKey, pages: Object.create(null) as MetaIndex['pages'] },
430+
stale,
431+
};
416432
}
417433

418434
/** The conversation key the operator's pages are filed under for this store's agent. */

‎src/cognition/substrate/memory/wiki/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export interface MetaIndex {
5050
* filed under the agent's plain scope: they are not used, and the next index replaces them.
5151
*/
5252
keyVersion?: string;
53+
/**
54+
* The operator key the pages' chunks are filed under (`conversationScopeKey(agentId, { kind: 'operator' })`). An
55+
* index that names another key, or none, was written for another agent id: its entries are not used, and the next
56+
* index deletes their chunks and files every page again under the current key.
57+
*/
58+
operatorKey?: string;
5359
}
5460

5561
export interface IndexResult {

0 commit comments

Comments
 (0)