@caupulican/pi-adaptative 0.80.70 → 0.80.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/dist/core/agent-session.d.ts +11 -1
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +38 -5
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/catalog-manager.d.ts +1 -1
- package/dist/core/catalog-manager.d.ts.map +1 -1
- package/dist/core/catalog-manager.js +0 -0
- package/dist/core/catalog-manager.js.map +1 -1
- package/dist/core/gateways/channel-provider.d.ts.map +1 -1
- package/dist/core/gateways/channel-provider.js +7 -0
- package/dist/core/gateways/channel-provider.js.map +1 -1
- package/dist/core/memory/effectiveness-tracker.d.ts.map +1 -1
- package/dist/core/memory/effectiveness-tracker.js +11 -2
- package/dist/core/memory/effectiveness-tracker.js.map +1 -1
- package/dist/core/memory/providers/file-store.d.ts.map +1 -1
- package/dist/core/memory/providers/file-store.js +4 -0
- package/dist/core/memory/providers/file-store.js.map +1 -1
- package/dist/core/memory/providers/transcript-recall.d.ts.map +1 -1
- package/dist/core/memory/providers/transcript-recall.js +12 -2
- package/dist/core/memory/providers/transcript-recall.js.map +1 -1
- package/dist/core/security/untrusted-boundary.d.ts.map +1 -1
- package/dist/core/security/untrusted-boundary.js +5 -3
- package/dist/core/security/untrusted-boundary.js.map +1 -1
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +4 -3
- package/dist/core/settings-manager.js.map +1 -1
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +12 -12
- package/package.json +4 -4
|
@@ -31,7 +31,7 @@ export declare class CatalogManager {
|
|
|
31
31
|
list(): CatalogEntry[];
|
|
32
32
|
/** Status of one catalog entry relative to what is installed at user level. */
|
|
33
33
|
status(entry: CatalogEntry): CatalogStatus;
|
|
34
|
-
/** Copy a catalog entry into the user level (install or overwrite). */
|
|
34
|
+
/** Copy a catalog entry into the user level (install or overwrite), atomically. */
|
|
35
35
|
install(entry: CatalogEntry): void;
|
|
36
36
|
/**
|
|
37
37
|
* Re-sync every INSTALLED resource that is outdated relative to the catalog (catalog → user level).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"catalog-manager.d.ts","sourceRoot":"","sources":["../../src/core/catalog-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAMH,eAAO,MAAM,aAAa,kEAAmE,CAAC;AAC9F,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzD,MAAM,MAAM,aAAa,GAAG,eAAe,GAAG,YAAY,GAAG,UAAU,CAAC;AAExE,MAAM,WAAW,YAAY;IAC5B,IAAI,EAAE,WAAW,CAAC;IAClB,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAC;IACb,iDAAiD;IACjD,WAAW,EAAE,MAAM,CAAC;IACpB,qDAAqD;IACrD,WAAW,EAAE,MAAM,CAAC;CACpB;AAED,qBAAa,cAAc;IAC1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IAEpC,YAAY,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAG/C;IAED,kEAAkE;IAClE,IAAI,IAAI,YAAY,EAAE,CAsBrB;IAED,+EAA+E;IAC/E,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,aAAa,CAGzC;IAED,
|
|
1
|
+
{"version":3,"file":"catalog-manager.d.ts","sourceRoot":"","sources":["../../src/core/catalog-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAMH,eAAO,MAAM,aAAa,kEAAmE,CAAC;AAC9F,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzD,MAAM,MAAM,aAAa,GAAG,eAAe,GAAG,YAAY,GAAG,UAAU,CAAC;AAExE,MAAM,WAAW,YAAY;IAC5B,IAAI,EAAE,WAAW,CAAC;IAClB,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAC;IACb,iDAAiD;IACjD,WAAW,EAAE,MAAM,CAAC;IACpB,qDAAqD;IACrD,WAAW,EAAE,MAAM,CAAC;CACpB;AAED,qBAAa,cAAc;IAC1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IAEpC,YAAY,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAG/C;IAED,kEAAkE;IAClE,IAAI,IAAI,YAAY,EAAE,CAsBrB;IAED,+EAA+E;IAC/E,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,aAAa,CAGzC;IAED,mFAAmF;IACnF,OAAO,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAEjC;IAED;;;OAGG;IACH,MAAM,IAAI,YAAY,EAAE,CASvB;IAED,qGAAqG;IACrG,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAKhC;CACD;AAqBD;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAwC/C","sourcesContent":["/**\n * CatalogManager — round resource management (resource-management design).\n *\n * A user-pointed **catalog directory** (a folder/repo holding `{skills,extensions,agents,prompts,themes}`)\n * is the easy-to-setup source of a user's workflows. From it pi can:\n * - **install** a resource = copy it into the pi user level (`<agentDir>/<kind>/`),\n * - **update** = hash-compare what's installed against the catalog and re-sync changed/outdated ones,\n * - **backup** = copy a user-level resource back into the catalog.\n * Per machine, the user installs only the subset they want; `update` refreshes exactly those.\n *\n * Copies are content-recursive (`cpSync`); status is decided by a recursive content hash so it is\n * portable across machines (mtimes differ; content does not).\n */\n\nimport { createHash, randomBytes } from \"node:crypto\";\nimport { cpSync, existsSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync } from \"node:fs\";\nimport { join } from \"node:path\";\n\nexport const CATALOG_KINDS = [\"skills\", \"extensions\", \"agents\", \"prompts\", \"themes\"] as const;\nexport type CatalogKind = (typeof CATALOG_KINDS)[number];\n\nexport type CatalogStatus = \"not-installed\" | \"up-to-date\" | \"outdated\";\n\nexport interface CatalogEntry {\n\tkind: CatalogKind;\n\t/** Top-level name in the catalog (a directory or file name). */\n\tname: string;\n\t/** Absolute path of the entry in the catalog. */\n\tcatalogPath: string;\n\t/** Absolute path where it installs at user level. */\n\tinstallPath: string;\n}\n\nexport class CatalogManager {\n\tprivate readonly agentDir: string;\n\tprivate readonly catalogDir: string;\n\n\tconstructor(agentDir: string, catalogDir: string) {\n\t\tthis.agentDir = agentDir;\n\t\tthis.catalogDir = catalogDir;\n\t}\n\n\t/** Every resource discovered in the catalog, across all kinds. */\n\tlist(): CatalogEntry[] {\n\t\tconst entries: CatalogEntry[] = [];\n\t\tfor (const kind of CATALOG_KINDS) {\n\t\t\tconst kindDir = join(this.catalogDir, kind);\n\t\t\tif (!existsSync(kindDir)) continue;\n\t\t\tlet names: string[];\n\t\t\ttry {\n\t\t\t\tnames = readdirSync(kindDir);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (const name of names) {\n\t\t\t\tif (name.startsWith(\".\")) continue;\n\t\t\t\tentries.push({\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tcatalogPath: join(kindDir, name),\n\t\t\t\t\tinstallPath: join(this.agentDir, kind, name),\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn entries;\n\t}\n\n\t/** Status of one catalog entry relative to what is installed at user level. */\n\tstatus(entry: CatalogEntry): CatalogStatus {\n\t\tif (!existsSync(entry.installPath)) return \"not-installed\";\n\t\treturn hashPath(entry.catalogPath) === hashPath(entry.installPath) ? \"up-to-date\" : \"outdated\";\n\t}\n\n\t/** Copy a catalog entry into the user level (install or overwrite), atomically. */\n\tinstall(entry: CatalogEntry): void {\n\t\tsafeCopy(entry.catalogPath, entry.installPath);\n\t}\n\n\t/**\n\t * Re-sync every INSTALLED resource that is outdated relative to the catalog (catalog → user level).\n\t * Never installs things the user has not already chosen on this machine. Returns the updated entries.\n\t */\n\tupdate(): CatalogEntry[] {\n\t\tconst updated: CatalogEntry[] = [];\n\t\tfor (const entry of this.list()) {\n\t\t\tif (this.status(entry) === \"outdated\") {\n\t\t\t\tthis.install(entry);\n\t\t\t\tupdated.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn updated;\n\t}\n\n\t/** Copy a user-level resource back into the catalog (backup). Requires it to exist at user level. */\n\tbackup(entry: CatalogEntry): void {\n\t\tif (!existsSync(entry.installPath)) {\n\t\t\tthrow new Error(`Cannot back up \"${entry.kind}/${entry.name}\": not installed at user level.`);\n\t\t}\n\t\tsafeCopy(entry.installPath, entry.catalogPath);\n\t}\n}\n\n/**\n * Copy `src` over `dest` without ever leaving a partially-copied tree at `dest` (Bug #18): copy into a\n * sibling temp first, then swap it into place; on any failure the temp is removed and `dest` is left\n * untouched.\n */\nfunction safeCopy(src: string, dest: string): void {\n\tconst tmp = `${dest}.pi-tmp-${randomBytes(6).toString(\"hex\")}`;\n\ttry {\n\t\tcpSync(src, tmp, { recursive: true, force: true });\n\t\trmSync(dest, { recursive: true, force: true });\n\t\trenameSync(tmp, dest);\n\t} catch (err) {\n\t\ttry {\n\t\t\trmSync(tmp, { recursive: true, force: true });\n\t\t} catch {}\n\t\tthrow err;\n\t}\n}\n\n/**\n * Content hash of a file or directory tree: stable across machines (ignores mtimes/paths outside the\n * tree). Files contribute their relative path + bytes; directories are walked in sorted order so the\n * hash is deterministic.\n */\nexport function hashPath(target: string): string {\n\tconst hash = createHash(\"sha256\");\n\tconst seen = new Set<string>(); // real paths already visited — breaks circular symlinks (Bug #19)\n\tconst walk = (abs: string, rel: string, depth: number): void => {\n\t\tif (depth > 64) return; // hard depth cap as a backstop\n\t\tlet real: string;\n\t\ttry {\n\t\t\treal = realpathSync(abs);\n\t\t} catch {\n\t\t\treturn;\n\t\t}\n\t\tif (seen.has(real)) return; // already walked (symlink loop or shared target)\n\t\tseen.add(real);\n\t\tlet stats: ReturnType<typeof statSync>;\n\t\ttry {\n\t\t\tstats = statSync(abs);\n\t\t} catch {\n\t\t\treturn;\n\t\t}\n\t\tif (stats.isDirectory()) {\n\t\t\tlet children: string[];\n\t\t\ttry {\n\t\t\t\tchildren = readdirSync(abs).sort();\n\t\t\t} catch {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor (const child of children) {\n\t\t\t\twalk(join(abs, child), rel ? `${rel}/${child}` : child, depth + 1);\n\t\t\t}\n\t\t} else if (stats.isFile()) {\n\t\t\thash.update(`\u0000${rel}\u0000`);\n\t\t\ttry {\n\t\t\t\thash.update(readFileSync(abs));\n\t\t\t} catch {\n\t\t\t\t// unreadable file → contributes only its path\n\t\t\t}\n\t\t}\n\t};\n\twalk(target, \"\", 0);\n\treturn hash.digest(\"hex\");\n}\n"]}
|
|
Binary file
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"catalog-manager.js","sourceRoot":"","sources":["../../src/core/catalog-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAClF,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,QAAQ,EAAE,YAAY,EAAE,QAAQ,EAAE,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AAG,QAAQ,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC;QAAC,MAAM,CAAC;YACR,OAAO;QACR,CAAC;QACD,IAAI,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC;YACzB,IAAI,QAAkB,CAAC;YACvB,IAAI,CAAC;gBACJ,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;YACpC,CAAC;YAAC,MAAM,CAAC;gBACR,OAAO;YACR,CAAC;YACD,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;gBAC9B,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;YACzD,CAAC;QACF,CAAC;aAAM,IAAI,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;YAC3B,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC;YACxB,IAAI,CAAC;gBACJ,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC;YAChC,CAAC;YAAC,MAAM,CAAC;gBACR,gDAA8C;YAC/C,CAAC;QACF,CAAC;IAAA,CACD,CAAC;IACF,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACjB,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAAA,CAC1B","sourcesContent":["/**\n * CatalogManager — round resource management (resource-management design).\n *\n * A user-pointed **catalog directory** (a folder/repo holding `{skills,extensions,agents,prompts,themes}`)\n * is the easy-to-setup source of a user's workflows. From it pi can:\n * - **install** a resource = copy it into the pi user level (`<agentDir>/<kind>/`),\n * - **update** = hash-compare what's installed against the catalog and re-sync changed/outdated ones,\n * - **backup** = copy a user-level resource back into the catalog.\n * Per machine, the user installs only the subset they want; `update` refreshes exactly those.\n *\n * Copies are content-recursive (`cpSync`); status is decided by a recursive content hash so it is\n * portable across machines (mtimes differ; content does not).\n */\n\nimport { createHash } from \"node:crypto\";\nimport { cpSync, existsSync, readdirSync, readFileSync, statSync } from \"node:fs\";\nimport { join } from \"node:path\";\n\nexport const CATALOG_KINDS = [\"skills\", \"extensions\", \"agents\", \"prompts\", \"themes\"] as const;\nexport type CatalogKind = (typeof CATALOG_KINDS)[number];\n\nexport type CatalogStatus = \"not-installed\" | \"up-to-date\" | \"outdated\";\n\nexport interface CatalogEntry {\n\tkind: CatalogKind;\n\t/** Top-level name in the catalog (a directory or file name). */\n\tname: string;\n\t/** Absolute path of the entry in the catalog. */\n\tcatalogPath: string;\n\t/** Absolute path where it installs at user level. */\n\tinstallPath: string;\n}\n\nexport class CatalogManager {\n\tprivate readonly agentDir: string;\n\tprivate readonly catalogDir: string;\n\n\tconstructor(agentDir: string, catalogDir: string) {\n\t\tthis.agentDir = agentDir;\n\t\tthis.catalogDir = catalogDir;\n\t}\n\n\t/** Every resource discovered in the catalog, across all kinds. */\n\tlist(): CatalogEntry[] {\n\t\tconst entries: CatalogEntry[] = [];\n\t\tfor (const kind of CATALOG_KINDS) {\n\t\t\tconst kindDir = join(this.catalogDir, kind);\n\t\t\tif (!existsSync(kindDir)) continue;\n\t\t\tlet names: string[];\n\t\t\ttry {\n\t\t\t\tnames = readdirSync(kindDir);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (const name of names) {\n\t\t\t\tif (name.startsWith(\".\")) continue;\n\t\t\t\tentries.push({\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tcatalogPath: join(kindDir, name),\n\t\t\t\t\tinstallPath: join(this.agentDir, kind, name),\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn entries;\n\t}\n\n\t/** Status of one catalog entry relative to what is installed at user level. */\n\tstatus(entry: CatalogEntry): CatalogStatus {\n\t\tif (!existsSync(entry.installPath)) return \"not-installed\";\n\t\treturn hashPath(entry.catalogPath) === hashPath(entry.installPath) ? \"up-to-date\" : \"outdated\";\n\t}\n\n\t/** Copy a catalog entry into the user level (install or overwrite). */\n\tinstall(entry: CatalogEntry): void {\n\t\tcpSync(entry.catalogPath, entry.installPath, { recursive: true, force: true });\n\t}\n\n\t/**\n\t * Re-sync every INSTALLED resource that is outdated relative to the catalog (catalog → user level).\n\t * Never installs things the user has not already chosen on this machine. Returns the updated entries.\n\t */\n\tupdate(): CatalogEntry[] {\n\t\tconst updated: CatalogEntry[] = [];\n\t\tfor (const entry of this.list()) {\n\t\t\tif (this.status(entry) === \"outdated\") {\n\t\t\t\tthis.install(entry);\n\t\t\t\tupdated.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn updated;\n\t}\n\n\t/** Copy a user-level resource back into the catalog (backup). Requires it to exist at user level. */\n\tbackup(entry: CatalogEntry): void {\n\t\tif (!existsSync(entry.installPath)) {\n\t\t\tthrow new Error(`Cannot back up \"${entry.kind}/${entry.name}\": not installed at user level.`);\n\t\t}\n\t\tcpSync(entry.installPath, entry.catalogPath, { recursive: true, force: true });\n\t}\n}\n\n/**\n * Content hash of a file or directory tree: stable across machines (ignores mtimes/paths outside the\n * tree). Files contribute their relative path + bytes; directories are walked in sorted order so the\n * hash is deterministic.\n */\nexport function hashPath(target: string): string {\n\tconst hash = createHash(\"sha256\");\n\tconst walk = (abs: string, rel: string): void => {\n\t\tlet stats: ReturnType<typeof statSync>;\n\t\ttry {\n\t\t\tstats = statSync(abs);\n\t\t} catch {\n\t\t\treturn;\n\t\t}\n\t\tif (stats.isDirectory()) {\n\t\t\tlet children: string[];\n\t\t\ttry {\n\t\t\t\tchildren = readdirSync(abs).sort();\n\t\t\t} catch {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor (const child of children) {\n\t\t\t\twalk(join(abs, child), rel ? `${rel}/${child}` : child);\n\t\t\t}\n\t\t} else if (stats.isFile()) {\n\t\t\thash.update(`\u0000${rel}\u0000`);\n\t\t\ttry {\n\t\t\t\thash.update(readFileSync(abs));\n\t\t\t} catch {\n\t\t\t\t// unreadable file → contributes only its path\n\t\t\t}\n\t\t}\n\t};\n\twalk(target, \"\");\n\treturn hash.digest(\"hex\");\n}\n"]}
|
|
1
|
+
{"version":3,"file":"catalog-manager.js","sourceRoot":"","sources":["../../src/core/catalog-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACpH,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,QAAQ,EAAE,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,CAAU,CAAC;AAe9F,MAAM,OAAO,cAAc;IACT,QAAQ,CAAS;IACjB,UAAU,CAAS;IAEpC,YAAY,QAAgB,EAAE,UAAkB,EAAE;QACjD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAAA,CAC7B;IAED,kEAAkE;IAClE,IAAI,GAAmB;QACtB,MAAM,OAAO,GAAmB,EAAE,CAAC;QACnC,KAAK,MAAM,IAAI,IAAI,aAAa,EAAE,CAAC;YAClC,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;YAC5C,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC;gBAAE,SAAS;YACnC,IAAI,KAAe,CAAC;YACpB,IAAI,CAAC;gBACJ,KAAK,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;YAC9B,CAAC;YAAC,MAAM,CAAC;gBACR,SAAS;YACV,CAAC;YACD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;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AAAA,CAC1B","sourcesContent":["/**\n * CatalogManager — round resource management (resource-management design).\n *\n * A user-pointed **catalog directory** (a folder/repo holding `{skills,extensions,agents,prompts,themes}`)\n * is the easy-to-setup source of a user's workflows. From it pi can:\n * - **install** a resource = copy it into the pi user level (`<agentDir>/<kind>/`),\n * - **update** = hash-compare what's installed against the catalog and re-sync changed/outdated ones,\n * - **backup** = copy a user-level resource back into the catalog.\n * Per machine, the user installs only the subset they want; `update` refreshes exactly those.\n *\n * Copies are content-recursive (`cpSync`); status is decided by a recursive content hash so it is\n * portable across machines (mtimes differ; content does not).\n */\n\nimport { createHash, randomBytes } from \"node:crypto\";\nimport { cpSync, existsSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync } from \"node:fs\";\nimport { join } from \"node:path\";\n\nexport const CATALOG_KINDS = [\"skills\", \"extensions\", \"agents\", \"prompts\", \"themes\"] as const;\nexport type CatalogKind = (typeof CATALOG_KINDS)[number];\n\nexport type CatalogStatus = \"not-installed\" | \"up-to-date\" | \"outdated\";\n\nexport interface CatalogEntry {\n\tkind: CatalogKind;\n\t/** Top-level name in the catalog (a directory or file name). */\n\tname: string;\n\t/** Absolute path of the entry in the catalog. */\n\tcatalogPath: string;\n\t/** Absolute path where it installs at user level. */\n\tinstallPath: string;\n}\n\nexport class CatalogManager {\n\tprivate readonly agentDir: string;\n\tprivate readonly catalogDir: string;\n\n\tconstructor(agentDir: string, catalogDir: string) {\n\t\tthis.agentDir = agentDir;\n\t\tthis.catalogDir = catalogDir;\n\t}\n\n\t/** Every resource discovered in the catalog, across all kinds. */\n\tlist(): CatalogEntry[] {\n\t\tconst entries: CatalogEntry[] = [];\n\t\tfor (const kind of CATALOG_KINDS) {\n\t\t\tconst kindDir = join(this.catalogDir, kind);\n\t\t\tif (!existsSync(kindDir)) continue;\n\t\t\tlet names: string[];\n\t\t\ttry {\n\t\t\t\tnames = readdirSync(kindDir);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (const name of names) {\n\t\t\t\tif (name.startsWith(\".\")) continue;\n\t\t\t\tentries.push({\n\t\t\t\t\tkind,\n\t\t\t\t\tname,\n\t\t\t\t\tcatalogPath: join(kindDir, name),\n\t\t\t\t\tinstallPath: join(this.agentDir, kind, name),\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn entries;\n\t}\n\n\t/** Status of one catalog entry relative to what is installed at user level. */\n\tstatus(entry: CatalogEntry): CatalogStatus {\n\t\tif (!existsSync(entry.installPath)) return \"not-installed\";\n\t\treturn hashPath(entry.catalogPath) === hashPath(entry.installPath) ? \"up-to-date\" : \"outdated\";\n\t}\n\n\t/** Copy a catalog entry into the user level (install or overwrite), atomically. */\n\tinstall(entry: CatalogEntry): void {\n\t\tsafeCopy(entry.catalogPath, entry.installPath);\n\t}\n\n\t/**\n\t * Re-sync every INSTALLED resource that is outdated relative to the catalog (catalog → user level).\n\t * Never installs things the user has not already chosen on this machine. Returns the updated entries.\n\t */\n\tupdate(): CatalogEntry[] {\n\t\tconst updated: CatalogEntry[] = [];\n\t\tfor (const entry of this.list()) {\n\t\t\tif (this.status(entry) === \"outdated\") {\n\t\t\t\tthis.install(entry);\n\t\t\t\tupdated.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn updated;\n\t}\n\n\t/** Copy a user-level resource back into the catalog (backup). Requires it to exist at user level. */\n\tbackup(entry: CatalogEntry): void {\n\t\tif (!existsSync(entry.installPath)) {\n\t\t\tthrow new Error(`Cannot back up \"${entry.kind}/${entry.name}\": not installed at user level.`);\n\t\t}\n\t\tsafeCopy(entry.installPath, entry.catalogPath);\n\t}\n}\n\n/**\n * Copy `src` over `dest` without ever leaving a partially-copied tree at `dest` (Bug #18): copy into a\n * sibling temp first, then swap it into place; on any failure the temp is removed and `dest` is left\n * untouched.\n */\nfunction safeCopy(src: string, dest: string): void {\n\tconst tmp = `${dest}.pi-tmp-${randomBytes(6).toString(\"hex\")}`;\n\ttry {\n\t\tcpSync(src, tmp, { recursive: true, force: true });\n\t\trmSync(dest, { recursive: true, force: true });\n\t\trenameSync(tmp, dest);\n\t} catch (err) {\n\t\ttry {\n\t\t\trmSync(tmp, { recursive: true, force: true });\n\t\t} catch {}\n\t\tthrow err;\n\t}\n}\n\n/**\n * Content hash of a file or directory tree: stable across machines (ignores mtimes/paths outside the\n * tree). Files contribute their relative path + bytes; directories are walked in sorted order so the\n * hash is deterministic.\n */\nexport function hashPath(target: string): string {\n\tconst hash = createHash(\"sha256\");\n\tconst seen = new Set<string>(); // real paths already visited — breaks circular symlinks (Bug #19)\n\tconst walk = (abs: string, rel: string, depth: number): void => {\n\t\tif (depth > 64) return; // hard depth cap as a backstop\n\t\tlet real: string;\n\t\ttry {\n\t\t\treal = realpathSync(abs);\n\t\t} catch {\n\t\t\treturn;\n\t\t}\n\t\tif (seen.has(real)) return; // already walked (symlink loop or shared target)\n\t\tseen.add(real);\n\t\tlet stats: ReturnType<typeof statSync>;\n\t\ttry {\n\t\t\tstats = statSync(abs);\n\t\t} catch {\n\t\t\treturn;\n\t\t}\n\t\tif (stats.isDirectory()) {\n\t\t\tlet children: string[];\n\t\t\ttry {\n\t\t\t\tchildren = readdirSync(abs).sort();\n\t\t\t} catch {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor (const child of children) {\n\t\t\t\twalk(join(abs, child), rel ? `${rel}/${child}` : child, depth + 1);\n\t\t\t}\n\t\t} else if (stats.isFile()) {\n\t\t\thash.update(`\u0000${rel}\u0000`);\n\t\t\ttry {\n\t\t\t\thash.update(readFileSync(abs));\n\t\t\t} catch {\n\t\t\t\t// unreadable file → contributes only its path\n\t\t\t}\n\t\t}\n\t};\n\twalk(target, \"\", 0);\n\treturn hash.digest(\"hex\");\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel-provider.d.ts","sourceRoot":"","sources":["../../../src/core/gateways/channel-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,uEAAuE;AACvE,MAAM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,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,YAAY,GAAG,IAAI,CAAC;IAClC,KAAK,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7B;AAED;;;;GAIG;AACH,qBAAa,eAAe;IAC3B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAsC;IAC/D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA2C;IACtE,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,cAAc,CAAmC;IAEzD,eAAe,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAM/C;IAED,iBAAiB,CAAC,QAAQ,EAAE,oBAAoB,GAAG,IAAI,CAKtD;IAED,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CAEpD;IAED,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,cAAc,IAAI,MAAM,CAE3B;IAED,2FAA2F;IACrF,KAAK,CAAC,SAAS,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAgB3D;IAED,uFAAuF;IACjF,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAa1B;CACD","sourcesContent":["/**\n * Interface-driven gateways & scheduling (adaptive-agent design R8).\n *\n * pi does NOT bake transports (Slack/Discord/email/webhooks) or a cron tick loop into the core — that\n * would bloat the agent and couple it to deployment concerns. Instead it exposes two provider contracts\n * that a deployment wrapper (server, headless runner) implements and registers; the core only manages\n * their lifecycle. This keeps the CLI/agent transport- and schedule-agnostic while still offering a\n * first-class, uniform extension point.\n */\n\n/** A message arriving from / sent to an external channel (gateway). */\nexport interface ChannelMessage {\n\t/** Stable conversation/session key for this channel (e.g. `slack_C123`). */\n\tconversationKey: string;\n\t/** Message text. */\n\ttext: string;\n\t/** Optional opaque metadata the provider round-trips. */\n\tmeta?: Record<string, unknown>;\n}\n\n/** Handler the core supplies to a channel for inbound messages. */\nexport type ChannelInboundHandler = (message: ChannelMessage) => void | Promise<void>;\n\n/**\n * A transport channel (Telegram/Slack/email/webhook/...). Implemented by a deployment wrapper and\n * registered via {@link GatewayRegistry}. The core starts it (handing it an inbound handler) and stops\n * it on shutdown; it never imports any transport SDK itself.\n */\nexport interface ChannelProvider {\n\treadonly name: string;\n\t/** Begin listening; deliver inbound messages to `onInbound`. */\n\tstart(onInbound: ChannelInboundHandler): void | Promise<void>;\n\t/** Send an outbound message on this channel. */\n\tsend(message: ChannelMessage): void | Promise<void>;\n\t/** Stop listening and release resources. */\n\tstop(): void | Promise<void>;\n}\n\n/** A scheduled job definition. */\nexport interface ScheduledJob {\n\tid: string;\n\t/** Cron expression or interval spec the provider understands. */\n\tschedule: string;\n\t/** Invoked when the job fires. */\n\trun: () => void | Promise<void>;\n}\n\n/**\n * A scheduler (cron-like). Implemented by a deployment wrapper and registered via\n * {@link GatewayRegistry}. The core registers jobs + starts/stops it; it owns no tick loop itself.\n */\nexport interface JobSchedulerProvider {\n\treadonly name: string;\n\tschedule(job: ScheduledJob): void;\n\tstart(): void | Promise<void>;\n\tstop(): void | Promise<void>;\n}\n\n/**\n * Holds registered channel + scheduler providers and drives their lifecycle. A session starts all\n * registered providers when it binds and stops them on dispose. Registration is additive and idempotent\n * by provider name (last registration wins).\n */\nexport class GatewayRegistry {\n\tprivate readonly channels = new Map<string, ChannelProvider>();\n\tprivate readonly schedulers = new Map<string, JobSchedulerProvider>();\n\tprivate started = false;\n\tprivate inboundHandler: ChannelInboundHandler = () => {};\n\n\tregisterChannel(provider: ChannelProvider): void {\n\t\t// Stop a same-named provider being replaced so its listeners/sockets don't leak (Bug #17).\n\t\tconst existing = this.channels.get(provider.name);\n\t\tif (existing && existing !== provider) void Promise.resolve(existing.stop()).catch(() => {});\n\t\tthis.channels.set(provider.name, provider);\n\t\tif (this.started) void Promise.resolve(provider.start(this.inboundHandler)).catch(() => {});\n\t}\n\n\tregisterScheduler(provider: JobSchedulerProvider): void {\n\t\tconst existing = this.schedulers.get(provider.name);\n\t\tif (existing && existing !== provider) void Promise.resolve(existing.stop()).catch(() => {});\n\t\tthis.schedulers.set(provider.name, provider);\n\t\tif (this.started) void Promise.resolve(provider.start()).catch(() => {});\n\t}\n\n\tgetChannel(name: string): ChannelProvider | undefined {\n\t\treturn this.channels.get(name);\n\t}\n\n\tget channelCount(): number {\n\t\treturn this.channels.size;\n\t}\n\n\tget schedulerCount(): number {\n\t\treturn this.schedulers.size;\n\t}\n\n\t/** Start every registered provider; inbound channel messages are routed to `onInbound`. */\n\tasync start(onInbound: ChannelInboundHandler): Promise<void> {\n\t\tif (this.started) return;\n\t\tthis.started = true;\n\t\tthis.inboundHandler = onInbound;\n\t\tfor (const channel of this.channels.values()) {\n\t\t\ttry {\n\t\t\t\tawait channel.start(onInbound);\n\t\t\t} catch {\n\t\t\t\t// a failing channel must not block the others\n\t\t\t}\n\t\t}\n\t\tfor (const scheduler of this.schedulers.values()) {\n\t\t\ttry {\n\t\t\t\tawait scheduler.start();\n\t\t\t} catch {}\n\t\t}\n\t}\n\n\t/** Stop every registered provider. Best-effort; always leaves the registry stopped. */\n\tasync stop(): Promise<void> {\n\t\tif (!this.started) return;\n\t\tthis.started = false;\n\t\tfor (const channel of this.channels.values()) {\n\t\t\ttry {\n\t\t\t\tawait channel.stop();\n\t\t\t} catch {}\n\t\t}\n\t\tfor (const scheduler of this.schedulers.values()) {\n\t\t\ttry {\n\t\t\t\tawait scheduler.stop();\n\t\t\t} catch {}\n\t\t}\n\t}\n}\n"]}
|
|
@@ -18,11 +18,18 @@ export class GatewayRegistry {
|
|
|
18
18
|
started = false;
|
|
19
19
|
inboundHandler = () => { };
|
|
20
20
|
registerChannel(provider) {
|
|
21
|
+
// Stop a same-named provider being replaced so its listeners/sockets don't leak (Bug #17).
|
|
22
|
+
const existing = this.channels.get(provider.name);
|
|
23
|
+
if (existing && existing !== provider)
|
|
24
|
+
void Promise.resolve(existing.stop()).catch(() => { });
|
|
21
25
|
this.channels.set(provider.name, provider);
|
|
22
26
|
if (this.started)
|
|
23
27
|
void Promise.resolve(provider.start(this.inboundHandler)).catch(() => { });
|
|
24
28
|
}
|
|
25
29
|
registerScheduler(provider) {
|
|
30
|
+
const existing = this.schedulers.get(provider.name);
|
|
31
|
+
if (existing && existing !== provider)
|
|
32
|
+
void Promise.resolve(existing.stop()).catch(() => { });
|
|
26
33
|
this.schedulers.set(provider.name, provider);
|
|
27
34
|
if (this.started)
|
|
28
35
|
void Promise.resolve(provider.start()).catch(() => { });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel-provider.js","sourceRoot":"","sources":["../../../src/core/gateways/channel-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAkDH;;;;GAIG;AACH,MAAM,OAAO,eAAe;IACV,QAAQ,GAAG,IAAI,GAAG,EAA2B,CAAC;IAC9C,UAAU,GAAG,IAAI,GAAG,EAAgC,CAAC;IAC9D,OAAO,GAAG,KAAK,CAAC;IAChB,cAAc,GAA0B,GAAG,EAAE,CAAC,EAAC,CAAC,CAAC;IAEzD,eAAe,CAAC,QAAyB,EAAQ;QAChD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC3C,IAAI,IAAI,CAAC,OAAO;YAAE,KAAK,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAC,CAAC,CAAC,CAAC;IAAA,CAC5F;IAED,iBAAiB,CAAC,QAA8B,EAAQ;QACvD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC7C,IAAI,IAAI,CAAC,OAAO;YAAE,KAAK,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,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,EAAE,CAAC;YACtB,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACX,CAAC;QACD,KAAK,MAAM,SAAS,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,CAAC;YAClD,IAAI,CAAC;gBACJ,MAAM,SAAS,CAAC,IAAI,EAAE,CAAC;YACxB,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACX,CAAC;IAAA,CACD;CACD","sourcesContent":["/**\n * Interface-driven gateways & scheduling (adaptive-agent design R8).\n *\n * pi does NOT bake transports (Slack/Discord/email/webhooks) or a cron tick loop into the core — that\n * would bloat the agent and couple it to deployment concerns. Instead it exposes two provider contracts\n * that a deployment wrapper (server, headless runner) implements and registers; the core only manages\n * their lifecycle. This keeps the CLI/agent transport- and schedule-agnostic while still offering a\n * first-class, uniform extension point.\n */\n\n/** A message arriving from / sent to an external channel (gateway). */\nexport interface ChannelMessage {\n\t/** Stable conversation/session key for this channel (e.g. `slack_C123`). */\n\tconversationKey: string;\n\t/** Message text. */\n\ttext: string;\n\t/** Optional opaque metadata the provider round-trips. */\n\tmeta?: Record<string, unknown>;\n}\n\n/** Handler the core supplies to a channel for inbound messages. */\nexport type ChannelInboundHandler = (message: ChannelMessage) => void | Promise<void>;\n\n/**\n * A transport channel (Telegram/Slack/email/webhook/...). Implemented by a deployment wrapper and\n * registered via {@link GatewayRegistry}. The core starts it (handing it an inbound handler) and stops\n * it on shutdown; it never imports any transport SDK itself.\n */\nexport interface ChannelProvider {\n\treadonly name: string;\n\t/** Begin listening; deliver inbound messages to `onInbound`. */\n\tstart(onInbound: ChannelInboundHandler): void | Promise<void>;\n\t/** Send an outbound message on this channel. */\n\tsend(message: ChannelMessage): void | Promise<void>;\n\t/** Stop listening and release resources. */\n\tstop(): void | Promise<void>;\n}\n\n/** A scheduled job definition. */\nexport interface ScheduledJob {\n\tid: string;\n\t/** Cron expression or interval spec the provider understands. */\n\tschedule: string;\n\t/** Invoked when the job fires. */\n\trun: () => void | Promise<void>;\n}\n\n/**\n * A scheduler (cron-like). Implemented by a deployment wrapper and registered via\n * {@link GatewayRegistry}. The core registers jobs + starts/stops it; it owns no tick loop itself.\n */\nexport interface JobSchedulerProvider {\n\treadonly name: string;\n\tschedule(job: ScheduledJob): void;\n\tstart(): void | Promise<void>;\n\tstop(): void | Promise<void>;\n}\n\n/**\n * Holds registered channel + scheduler providers and drives their lifecycle. A session starts all\n * registered providers when it binds and stops them on dispose. Registration is additive and idempotent\n * by provider name (last registration wins).\n */\nexport class GatewayRegistry {\n\tprivate readonly channels = new Map<string, ChannelProvider>();\n\tprivate readonly schedulers = new Map<string, JobSchedulerProvider>();\n\tprivate started = false;\n\tprivate inboundHandler: ChannelInboundHandler = () => {};\n\n\tregisterChannel(provider: ChannelProvider): void {\n\t\tthis.channels.set(provider.name, provider);\n\t\tif (this.started) void Promise.resolve(provider.start(this.inboundHandler)).catch(() => {});\n\t}\n\n\tregisterScheduler(provider: JobSchedulerProvider): void {\n\t\tthis.schedulers.set(provider.name, provider);\n\t\tif (this.started) void Promise.resolve(provider.start()).catch(() => {});\n\t}\n\n\tgetChannel(name: string): ChannelProvider | undefined {\n\t\treturn this.channels.get(name);\n\t}\n\n\tget channelCount(): number {\n\t\treturn this.channels.size;\n\t}\n\n\tget schedulerCount(): number {\n\t\treturn this.schedulers.size;\n\t}\n\n\t/** Start every registered provider; inbound channel messages are routed to `onInbound`. */\n\tasync start(onInbound: ChannelInboundHandler): Promise<void> {\n\t\tif (this.started) return;\n\t\tthis.started = true;\n\t\tthis.inboundHandler = onInbound;\n\t\tfor (const channel of this.channels.values()) {\n\t\t\ttry {\n\t\t\t\tawait channel.start(onInbound);\n\t\t\t} catch {\n\t\t\t\t// a failing channel must not block the others\n\t\t\t}\n\t\t}\n\t\tfor (const scheduler of this.schedulers.values()) {\n\t\t\ttry {\n\t\t\t\tawait scheduler.start();\n\t\t\t} catch {}\n\t\t}\n\t}\n\n\t/** Stop every registered provider. Best-effort; always leaves the registry stopped. */\n\tasync stop(): Promise<void> {\n\t\tif (!this.started) return;\n\t\tthis.started = false;\n\t\tfor (const channel of this.channels.values()) {\n\t\t\ttry {\n\t\t\t\tawait channel.stop();\n\t\t\t} catch {}\n\t\t}\n\t\tfor (const scheduler of this.schedulers.values()) {\n\t\t\ttry {\n\t\t\t\tawait scheduler.stop();\n\t\t\t} catch {}\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"channel-provider.js","sourceRoot":"","sources":["../../../src/core/gateways/channel-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAkDH;;;;GAIG;AACH,MAAM,OAAO,eAAe;IACV,QAAQ,GAAG,IAAI,GAAG,EAA2B,CAAC;IAC9C,UAAU,GAAG,IAAI,GAAG,EAAgC,CAAC;IAC9D,OAAO,GAAG,KAAK,CAAC;IAChB,cAAc,GAA0B,GAAG,EAAE,CAAC,EAAC,CAAC,CAAC;IAEzD,eAAe,CAAC,QAAyB,EAAQ;QAChD,2FAA2F;QAC3F,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAClD,IAAI,QAAQ,IAAI,QAAQ,KAAK,QAAQ;YAAE,KAAK,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAC,CAAC,CAAC,CAAC;QAC7F,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC3C,IAAI,IAAI,CAAC,OAAO;YAAE,KAAK,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAC,CAAC,CAAC,CAAC;IAAA,CAC5F;IAED,iBAAiB,CAAC,QAA8B,EAAQ;QACvD,MAAM,QAAQ,GAAG,I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AAU,CAAC,MAAM,EAAE,EAAE,CAAC;YAClD,IAAI,CAAC;gBACJ,MAAM,SAAS,CAAC,KAAK,EAAE,CAAC;YACzB,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACX,CAAC;IAAA,CACD;IAED,uFAAuF;IACvF,KAAK,CAAC,IAAI,GAAkB;QAC3B,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO;QAC1B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;YAC9C,IAAI,CAAC;gBACJ,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC;YACtB,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACX,CAAC;QACD,KAAK,MAAM,SAAS,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,CAAC;YAClD,IAAI,CAAC;gBACJ,MAAM,SAAS,CAAC,IAAI,EAAE,CAAC;YACxB,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACX,CAAC;IAAA,CACD;CACD","sourcesContent":["/**\n * Interface-driven gateways & scheduling (adaptive-agent design R8).\n *\n * pi does NOT bake transports (Slack/Discord/email/webhooks) or a cron tick loop into the core — that\n * would bloat the agent and couple it to deployment concerns. Instead it exposes two provider contracts\n * that a deployment wrapper (server, headless runner) implements and registers; the core only manages\n * their lifecycle. This keeps the CLI/agent transport- and schedule-agnostic while still offering a\n * first-class, uniform extension point.\n */\n\n/** A message arriving from / sent to an external channel (gateway). */\nexport interface ChannelMessage {\n\t/** Stable conversation/session key for this channel (e.g. `slack_C123`). */\n\tconversationKey: string;\n\t/** Message text. */\n\ttext: string;\n\t/** Optional opaque metadata the provider round-trips. */\n\tmeta?: Record<string, unknown>;\n}\n\n/** Handler the core supplies to a channel for inbound messages. */\nexport type ChannelInboundHandler = (message: ChannelMessage) => void | Promise<void>;\n\n/**\n * A transport channel (Telegram/Slack/email/webhook/...). Implemented by a deployment wrapper and\n * registered via {@link GatewayRegistry}. The core starts it (handing it an inbound handler) and stops\n * it on shutdown; it never imports any transport SDK itself.\n */\nexport interface ChannelProvider {\n\treadonly name: string;\n\t/** Begin listening; deliver inbound messages to `onInbound`. */\n\tstart(onInbound: ChannelInboundHandler): void | Promise<void>;\n\t/** Send an outbound message on this channel. */\n\tsend(message: ChannelMessage): void | Promise<void>;\n\t/** Stop listening and release resources. */\n\tstop(): void | Promise<void>;\n}\n\n/** A scheduled job definition. */\nexport interface ScheduledJob {\n\tid: string;\n\t/** Cron expression or interval spec the provider understands. */\n\tschedule: string;\n\t/** Invoked when the job fires. */\n\trun: () => void | Promise<void>;\n}\n\n/**\n * A scheduler (cron-like). Implemented by a deployment wrapper and registered via\n * {@link GatewayRegistry}. The core registers jobs + starts/stops it; it owns no tick loop itself.\n */\nexport interface JobSchedulerProvider {\n\treadonly name: string;\n\tschedule(job: ScheduledJob): void;\n\tstart(): void | Promise<void>;\n\tstop(): void | Promise<void>;\n}\n\n/**\n * Holds registered channel + scheduler providers and drives their lifecycle. A session starts all\n * registered providers when it binds and stops them on dispose. Registration is additive and idempotent\n * by provider name (last registration wins).\n */\nexport class GatewayRegistry {\n\tprivate readonly channels = new Map<string, ChannelProvider>();\n\tprivate readonly schedulers = new Map<string, JobSchedulerProvider>();\n\tprivate started = false;\n\tprivate inboundHandler: ChannelInboundHandler = () => {};\n\n\tregisterChannel(provider: ChannelProvider): void {\n\t\t// Stop a same-named provider being replaced so its listeners/sockets don't leak (Bug #17).\n\t\tconst existing = this.channels.get(provider.name);\n\t\tif (existing && existing !== provider) void Promise.resolve(existing.stop()).catch(() => {});\n\t\tthis.channels.set(provider.name, provider);\n\t\tif (this.started) void Promise.resolve(provider.start(this.inboundHandler)).catch(() => {});\n\t}\n\n\tregisterScheduler(provider: JobSchedulerProvider): void {\n\t\tconst existing = this.schedulers.get(provider.name);\n\t\tif (existing && existing !== provider) void Promise.resolve(existing.stop()).catch(() => {});\n\t\tthis.schedulers.set(provider.name, provider);\n\t\tif (this.started) void Promise.resolve(provider.start()).catch(() => {});\n\t}\n\n\tgetChannel(name: string): ChannelProvider | undefined {\n\t\treturn this.channels.get(name);\n\t}\n\n\tget channelCount(): number {\n\t\treturn this.channels.size;\n\t}\n\n\tget schedulerCount(): number {\n\t\treturn this.schedulers.size;\n\t}\n\n\t/** Start every registered provider; inbound channel messages are routed to `onInbound`. */\n\tasync start(onInbound: ChannelInboundHandler): Promise<void> {\n\t\tif (this.started) return;\n\t\tthis.started = true;\n\t\tthis.inboundHandler = onInbound;\n\t\tfor (const channel of this.channels.values()) {\n\t\t\ttry {\n\t\t\t\tawait channel.start(onInbound);\n\t\t\t} catch {\n\t\t\t\t// a failing channel must not block the others\n\t\t\t}\n\t\t}\n\t\tfor (const scheduler of this.schedulers.values()) {\n\t\t\ttry {\n\t\t\t\tawait scheduler.start();\n\t\t\t} catch {}\n\t\t}\n\t}\n\n\t/** Stop every registered provider. Best-effort; always leaves the registry stopped. */\n\tasync stop(): Promise<void> {\n\t\tif (!this.started) return;\n\t\tthis.started = false;\n\t\tfor (const channel of this.channels.values()) {\n\t\t\ttry {\n\t\t\t\tawait channel.stop();\n\t\t\t} catch {}\n\t\t}\n\t\tfor (const scheduler of this.schedulers.values()) {\n\t\t\ttry {\n\t\t\t\tawait scheduler.stop();\n\t\t\t} catch {}\n\t\t}\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effectiveness-tracker.d.ts","sourceRoot":"","sources":["../../../src/core/memory/effectiveness-tracker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAOH,qBAAa,oBAAoB;IAChC,OAAO,CAAC,GAAG,CAAiB;IAC5B,OAAO,CAAC,OAAO,CAAK;IAEpB;;;OAGG;IACH,mBAAmB,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"effectiveness-tracker.d.ts","sourceRoot":"","sources":["../../../src/core/memory/effectiveness-tracker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAOH,qBAAa,oBAAoB;IAChC,OAAO,CAAC,GAAG,CAAiB;IAC5B,OAAO,CAAC,OAAO,CAAK;IAEpB;;;OAGG;IACH,mBAAmB,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,IAAI,CAOrF;IAED,uFAAuF;IACvF,YAAY,IAAI,MAAM,CAErB;IAED,0CAA0C;IAC1C,IAAI,WAAW,IAAI,MAAM,CAExB;CACD;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,CAe1G","sourcesContent":["/**\n * EffectivenessTracker — the closed adaptive loop (adaptive-agent design R4, leapfrog #9).\n *\n * Recall (R3) injects a `<memory_context>` page; this tracks whether the agent actually USED it, so the\n * recall gate can adapt — recall more when it's paying off, back off when it isn't. \"Used\" = the\n * fraction of the recall page's DISTINCTIVE tokens (those not already in the user's query) that reappear\n * in the assistant's response. We isolate distinctive tokens so we measure recall's own contribution,\n * not the baseline overlap every response shares with the query.\n *\n * The score is an exponential moving average (\"useful lately\") in [0,1], starting at a neutral prior so\n * recall is given a fair chance before the loop adapts.\n */\n\nimport { tokenize } from \"../tools/skill-audit.ts\";\n\nconst NEUTRAL_PRIOR = 0.5;\nconst ALPHA = 0.3; // EMA weight on the newest outcome\n\nexport class EffectivenessTracker {\n\tprivate ema = NEUTRAL_PRIOR;\n\tprivate samples = 0;\n\n\t/**\n\t * Record the outcome of a turn that received a recall page: how much of the recall's distinctive\n\t * content the assistant's response actually drew on.\n\t */\n\trecordRecallOutcome(recallText: string, queryText: string, responseText: string): void {\n\t\tconst used = distinctiveRecallUsage(recallText, queryText, responseText);\n\t\t// A negative score means \"no signal\" (recall added nothing beyond the query) — don't let a\n\t\t// redundant recall drag the score down; just skip the sample.\n\t\tif (used < 0) return;\n\t\tthis.ema = ALPHA * used + (1 - ALPHA) * this.ema;\n\t\tthis.samples += 1;\n\t}\n\n\t/** Rolling \"useful lately\" score in [0,1]. Neutral until enough samples accumulate. */\n\tusefulLately(): number {\n\t\treturn this.ema;\n\t}\n\n\t/** Number of recorded recall outcomes. */\n\tget sampleCount(): number {\n\t\treturn this.samples;\n\t}\n}\n\n/**\n * Fraction of the recall page's distinctive tokens (present in recall but NOT in the query) that appear\n * in the response. 0 when recall added nothing the query didn't already carry.\n */\nexport function distinctiveRecallUsage(recallText: string, queryText: string, responseText: string): number {\n\tconst queryTokens = new Set(tokenize(queryText));\n\tconst distinctive = tokenize(recallText).filter((t) => !queryTokens.has(t));\n\t// No distinctive content → recall added nothing the query didn't already carry → no usable signal.\n\tif (distinctive.length === 0) return -1;\n\tconst responseTokens = new Set(tokenize(responseText));\n\tlet hits = 0;\n\tfor (const token of distinctive) {\n\t\tif (responseTokens.has(token)) hits++;\n\t}\n\t// Reward ANY meaningful reuse without penalizing long snippets (Bug #14): a concise answer reuses\n\t// only a few key recall tokens, so saturate the denominator at a small number of key tokens rather\n\t// than dividing by the full distinctive vocabulary.\n\tconst KEY_TOKENS = 4;\n\treturn Math.min(1, hits / Math.min(distinctive.length, KEY_TOKENS));\n}\n"]}
|
|
@@ -22,6 +22,10 @@ export class EffectivenessTracker {
|
|
|
22
22
|
*/
|
|
23
23
|
recordRecallOutcome(recallText, queryText, responseText) {
|
|
24
24
|
const used = distinctiveRecallUsage(recallText, queryText, responseText);
|
|
25
|
+
// A negative score means "no signal" (recall added nothing beyond the query) — don't let a
|
|
26
|
+
// redundant recall drag the score down; just skip the sample.
|
|
27
|
+
if (used < 0)
|
|
28
|
+
return;
|
|
25
29
|
this.ema = ALPHA * used + (1 - ALPHA) * this.ema;
|
|
26
30
|
this.samples += 1;
|
|
27
31
|
}
|
|
@@ -41,14 +45,19 @@ export class EffectivenessTracker {
|
|
|
41
45
|
export function distinctiveRecallUsage(recallText, queryText, responseText) {
|
|
42
46
|
const queryTokens = new Set(tokenize(queryText));
|
|
43
47
|
const distinctive = tokenize(recallText).filter((t) => !queryTokens.has(t));
|
|
48
|
+
// No distinctive content → recall added nothing the query didn't already carry → no usable signal.
|
|
44
49
|
if (distinctive.length === 0)
|
|
45
|
-
return
|
|
50
|
+
return -1;
|
|
46
51
|
const responseTokens = new Set(tokenize(responseText));
|
|
47
52
|
let hits = 0;
|
|
48
53
|
for (const token of distinctive) {
|
|
49
54
|
if (responseTokens.has(token))
|
|
50
55
|
hits++;
|
|
51
56
|
}
|
|
52
|
-
|
|
57
|
+
// Reward ANY meaningful reuse without penalizing long snippets (Bug #14): a concise answer reuses
|
|
58
|
+
// only a few key recall tokens, so saturate the denominator at a small number of key tokens rather
|
|
59
|
+
// than dividing by the full distinctive vocabulary.
|
|
60
|
+
const KEY_TOKENS = 4;
|
|
61
|
+
return Math.min(1, hits / Math.min(distinctive.length, KEY_TOKENS));
|
|
53
62
|
}
|
|
54
63
|
//# sourceMappingURL=effectiveness-tracker.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effectiveness-tracker.js","sourceRoot":"","sources":["../../../src/core/memory/effectiveness-tracker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAEnD,MAAM,aAAa,GAAG,GAAG,CAAC;AAC1B,MAAM,KAAK,GAAG,GAAG,CAAC,CAAC,mCAAmC;AAEtD,MAAM,OAAO,oBAAoB;IACxB,GAAG,GAAG,aAAa,CAAC;IACpB,OAAO,GAAG,CAAC,CAAC;IAEpB;;;OAGG;IACH,mBAAmB,CAAC,UAAkB,EAAE,SAAiB,EAAE,YAAoB,EAAQ;QACtF,MAAM,IAAI,GAAG,sBAAsB,CAAC,UAAU,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC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AAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;AAAA,CACpE","sourcesContent":["/**\n * EffectivenessTracker — the closed adaptive loop (adaptive-agent design R4, leapfrog #9).\n *\n * Recall (R3) injects a `<memory_context>` page; this tracks whether the agent actually USED it, so the\n * recall gate can adapt — recall more when it's paying off, back off when it isn't. \"Used\" = the\n * fraction of the recall page's DISTINCTIVE tokens (those not already in the user's query) that reappear\n * in the assistant's response. We isolate distinctive tokens so we measure recall's own contribution,\n * not the baseline overlap every response shares with the query.\n *\n * The score is an exponential moving average (\"useful lately\") in [0,1], starting at a neutral prior so\n * recall is given a fair chance before the loop adapts.\n */\n\nimport { tokenize } from \"../tools/skill-audit.ts\";\n\nconst NEUTRAL_PRIOR = 0.5;\nconst ALPHA = 0.3; // EMA weight on the newest outcome\n\nexport class EffectivenessTracker {\n\tprivate ema = NEUTRAL_PRIOR;\n\tprivate samples = 0;\n\n\t/**\n\t * Record the outcome of a turn that received a recall page: how much of the recall's distinctive\n\t * content the assistant's response actually drew on.\n\t */\n\trecordRecallOutcome(recallText: string, queryText: string, responseText: string): void {\n\t\tconst used = distinctiveRecallUsage(recallText, queryText, responseText);\n\t\t// A negative score means \"no signal\" (recall added nothing beyond the query) — don't let a\n\t\t// redundant recall drag the score down; just skip the sample.\n\t\tif (used < 0) return;\n\t\tthis.ema = ALPHA * used + (1 - ALPHA) * this.ema;\n\t\tthis.samples += 1;\n\t}\n\n\t/** Rolling \"useful lately\" score in [0,1]. Neutral until enough samples accumulate. */\n\tusefulLately(): number {\n\t\treturn this.ema;\n\t}\n\n\t/** Number of recorded recall outcomes. */\n\tget sampleCount(): number {\n\t\treturn this.samples;\n\t}\n}\n\n/**\n * Fraction of the recall page's distinctive tokens (present in recall but NOT in the query) that appear\n * in the response. 0 when recall added nothing the query didn't already carry.\n */\nexport function distinctiveRecallUsage(recallText: string, queryText: string, responseText: string): number {\n\tconst queryTokens = new Set(tokenize(queryText));\n\tconst distinctive = tokenize(recallText).filter((t) => !queryTokens.has(t));\n\t// No distinctive content → recall added nothing the query didn't already carry → no usable signal.\n\tif (distinctive.length === 0) return -1;\n\tconst responseTokens = new Set(tokenize(responseText));\n\tlet hits = 0;\n\tfor (const token of distinctive) {\n\t\tif (responseTokens.has(token)) hits++;\n\t}\n\t// Reward ANY meaningful reuse without penalizing long snippets (Bug #14): a concise answer reuses\n\t// only a few key recall tokens, so saturate the denominator at a small number of key tokens rather\n\t// than dividing by the full distinctive vocabulary.\n\tconst KEY_TOKENS = 4;\n\treturn Math.min(1, hits / Math.min(distinctive.length, KEY_TOKENS));\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-store.d.ts","sourceRoot":"","sources":["../../../../src/core/memory/providers/file-store.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAGhE,OAAO,KAAK,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEpF;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAmB3F;AAiBD,qBAAa,iBAAkB,YAAW,cAAc;IACvD,SAAgB,IAAI,gBAAgB;IAEpC,OAAO,CAAC,GAAG,CAAC,CAAyB;IACrC,OAAO,CAAC,cAAc,CAAM;IAC5B,OAAO,CAAC,YAAY,CAAM;IAE1B,OAAO,CAAC,iBAAiB,CAAM;IAC/B,OAAO,CAAC,eAAe,CAAM;IAG7B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAQ;IAC7C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAQ;IAEpC,WAAW,IAAI,OAAO,CAE5B;IAEM,eAAe;;MAErB;IAEY,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAqBtF;IAEM,iBAAiB,IAAI,MAAM,CA6BjC;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAGrD;IAEY,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAErC;IAEM,iBAAiB,IAAI,MAAM,EAAE,CAEnC;IAEM,kBAAkB,IAAI,cAAc,EAAE,CAqI5C;CACD","sourcesContent":["import { existsSync, promises as fs, mkdirSync, writeFileSync } from \"fs\";\nimport { join } from \"path\";\nimport lockfile from \"proper-lockfile\";\nimport { type Static, Type } from \"typebox\";\nimport type { ToolDefinition } from \"../../extensions/types.ts\";\nimport { scanContextFileThreats } from \"../../resource-loader.ts\";\nimport { jaccard, tokenize } from \"../../tools/skill-audit.ts\";\nimport type { MemoryLifecycleContext, MemoryProvider } from \"../memory-provider.ts\";\n\n/**\n * R5 confront-before-write (anti append-rot): if `content` is a near-duplicate of an existing\n * non-empty line (token Jaccard ≥ threshold — i.e. the same fact reworded), supersede that line in\n * place and return the rewritten file; otherwise return null (the caller appends normally).\n */\nexport function supersedeNearDuplicateLine(existing: string, content: string): string | null {\n\tconst NEAR_DUP_THRESHOLD = 0.6;\n\tconst contentTokens = tokenize(content);\n\tif (contentTokens.length === 0) return null;\n\tconst lines = existing.split(\"\\n\");\n\tlet bestIdx = -1;\n\tlet bestScore = NEAR_DUP_THRESHOLD;\n\tfor (let i = 0; i < lines.length; i++) {\n\t\tconst line = lines[i].trim();\n\t\tif (!line) continue;\n\t\tconst score = jaccard(contentTokens, tokenize(line));\n\t\tif (score >= bestScore) {\n\t\t\tbestScore = score;\n\t\t\tbestIdx = i;\n\t\t}\n\t}\n\tif (bestIdx === -1) return null;\n\tlines[bestIdx] = content;\n\treturn lines.join(\"\\n\");\n}\n\nconst memorySchema = Type.Object({\n\taction: Type.Union([Type.Literal(\"add\"), Type.Literal(\"replace\"), Type.Literal(\"remove\")], {\n\t\tdescription: \"Action to perform: add new content, replace existing content, or remove content\",\n\t}),\n\ttarget: Type.Union([Type.Literal(\"memory\"), Type.Literal(\"user\")], {\n\t\tdescription: \"Target file: 'memory' for MEMORY.md, 'user' for USER.md\",\n\t}),\n\tcontent: Type.Optional(Type.String({ description: \"Content to write (required for 'add' or 'replace')\" })),\n\toldContent: Type.Optional(\n\t\tType.String({ description: \"Exact substring to replace or remove (required for 'replace' or 'remove')\" }),\n\t),\n});\n\ntype MemoryParams = Static<typeof memorySchema>;\n\nexport class FileStoreProvider implements MemoryProvider {\n\tpublic readonly name = \"file-store\";\n\n\tprivate ctx?: MemoryLifecycleContext;\n\tprivate memoryFilePath = \"\";\n\tprivate userFilePath = \"\";\n\n\tprivate lastWrittenMemory = \"\";\n\tprivate lastWrittenUser = \"\";\n\n\t// Character budgets\n\tprivate static readonly BUDGET_MEMORY = 2200;\n\tprivate static readonly BUDGET_USER = 1375;\n\n\tpublic isAvailable(): boolean {\n\t\treturn true;\n\t}\n\n\tpublic getCapabilities() {\n\t\treturn { surfaces: [\"context\" as const] };\n\t}\n\n\tpublic async initialize(_sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.ctx = ctx;\n\t\tthis.memoryFilePath = join(ctx.agentDir, \"MEMORY.md\");\n\t\tthis.userFilePath = join(ctx.agentDir, \"USER.md\");\n\n\t\t// Ensure agentDir exists\n\t\tif (!existsSync(ctx.agentDir)) {\n\t\t\tmkdirSync(ctx.agentDir, { recursive: true });\n\t\t}\n\n\t\t// Initialize files if they do not exist\n\t\tif (!existsSync(this.memoryFilePath)) {\n\t\t\twriteFileSync(this.memoryFilePath, \"\", \"utf-8\");\n\t\t}\n\t\tif (!existsSync(this.userFilePath)) {\n\t\t\twriteFileSync(this.userFilePath, \"\", \"utf-8\");\n\t\t}\n\n\t\t// Load initial contents\n\t\tthis.lastWrittenMemory = await fs.readFile(this.memoryFilePath, \"utf-8\");\n\t\tthis.lastWrittenUser = await fs.readFile(this.userFilePath, \"utf-8\");\n\t}\n\n\tpublic systemPromptBlock(): string {\n\t\tconst sanitize = (content: string) => {\n\t\t\tconst lines = content.split(\"\\n\");\n\t\t\tconst sanitizedLines = lines.map((line) => {\n\t\t\t\tconst threats = scanContextFileThreats(line);\n\t\t\t\tif (threats.length > 0) {\n\t\t\t\t\treturn `[BLOCKED: potential threat detected (${threats.join(\", \")})]`;\n\t\t\t\t}\n\t\t\t\treturn line;\n\t\t\t});\n\t\t\treturn sanitizedLines.join(\"\\n\");\n\t\t};\n\n\t\tconst mem = sanitize(this.lastWrittenMemory);\n\t\tconst usr = sanitize(this.lastWrittenUser);\n\n\t\tconst blocks: string[] = [];\n\t\tif (mem.trim()) {\n\t\t\tblocks.push(`## MEMORY.md:\\n${mem}`);\n\t\t}\n\t\tif (usr.trim()) {\n\t\t\tblocks.push(`## USER.md:\\n${usr}`);\n\t\t}\n\n\t\tif (blocks.length === 0) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\treturn `=== Persistent Memory (file-store) ===\\n[System Note: Below is a snapshot of your persistent memory. You can update these using the 'memory' tool.]\\n\\n${blocks.join(\"\\n\\n\")}`;\n\t}\n\n\tpublic async prefetch(_query: string): Promise<string> {\n\t\t// static system prompt block is sufficient for file-store default; no-op prefetch\n\t\treturn \"\";\n\t}\n\n\tpublic async shutdown(): Promise<void> {\n\t\t// no-op\n\t}\n\n\tpublic getContextMarkers(): string[] {\n\t\treturn [];\n\t}\n\n\tpublic getToolDefinitions(): ToolDefinition[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\tname: \"memory\",\n\t\t\t\tlabel: \"Persistent Memory Manager\",\n\t\t\t\tdescription: \"Add, replace, or remove contents in persistent memory files (MEMORY.md/USER.md).\",\n\t\t\t\tparameters: memorySchema,\n\t\t\t\texecute: async (_toolCallId, params: MemoryParams, _signal, _onUpdate, _execCtx) => {\n\t\t\t\t\tif (this.ctx?.isChildSession) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: \"Error: Writes to persistent memory are not allowed in child sessions (subagents).\",\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: false, error: \"Child session write-gated\" },\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tconst { action, target, content, oldContent } = params;\n\t\t\t\t\tconst filePath = target === \"memory\" ? this.memoryFilePath : this.userFilePath;\n\t\t\t\t\tconst budget = target === \"memory\" ? FileStoreProvider.BUDGET_MEMORY : FileStoreProvider.BUDGET_USER;\n\n\t\t\t\t\tlet release: (() => Promise<void>) | undefined;\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// File lock\n\t\t\t\t\t\trelease = await lockfile.lock(filePath, { realpath: false, retries: 5 });\n\n\t\t\t\t\t\tconst lastWritten = target === \"memory\" ? this.lastWrittenMemory : this.lastWrittenUser;\n\t\t\t\t\t\t// Read current file content on disk for drift detection\n\t\t\t\t\t\tconst currentOnDisk = await fs.readFile(filePath, \"utf-8\");\n\t\t\t\t\t\tif (currentOnDisk !== lastWritten) {\n\t\t\t\t\t\t\t// Drift detected. Backup current file and refuse write.\n\t\t\t\t\t\t\tconst backupPath = `${filePath}.bak.${Date.now()}`;\n\t\t\t\t\t\t\tawait fs.writeFile(backupPath, currentOnDisk, \"utf-8\");\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\t\ttext: `Error: Drift detected. The memory file has been modified out-of-band by an external process. A backup was created at ${backupPath}. Operation aborted.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tdetails: { success: false, error: \"Drift detected\" },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet newContent = currentOnDisk;\n\t\t\t\t\t\tif (action === \"add\") {\n\t\t\t\t\t\t\tif (content === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameter 'content' is required for action 'add'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// R5: confront before write. If this fact is a near-duplicate of an existing line,\n\t\t\t\t\t\t\t// supersede it in place instead of appending a redundant copy (prevents append-rot).\n\t\t\t\t\t\t\tconst superseded = supersedeNearDuplicateLine(currentOnDisk, content);\n\t\t\t\t\t\t\tif (superseded !== null) {\n\t\t\t\t\t\t\t\tnewContent = superseded;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tnewContent =\n\t\t\t\t\t\t\t\t\tnewContent.endsWith(\"\\n\") || newContent === \"\"\n\t\t\t\t\t\t\t\t\t\t? `${newContent}${content}\\n`\n\t\t\t\t\t\t\t\t\t\t: `${newContent}\\n${content}\\n`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if (action === \"replace\") {\n\t\t\t\t\t\t\tif (content === undefined || oldContent === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameters 'content' and 'oldContent' are required for action 'replace'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!currentOnDisk.includes(oldContent)) {\n\t\t\t\t\t\t\t\tthrow new Error(`The content to replace ('oldContent') was not found in the file.`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnewContent = currentOnDisk.replace(oldContent, content);\n\t\t\t\t\t\t} else if (action === \"remove\") {\n\t\t\t\t\t\t\tif (oldContent === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameter 'oldContent' is required for action 'remove'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!currentOnDisk.includes(oldContent)) {\n\t\t\t\t\t\t\t\tthrow new Error(`The content to remove ('oldContent') was not found in the file.`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnewContent = currentOnDisk.replace(oldContent, \"\");\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Budget check\n\t\t\t\t\t\tif (newContent.length > budget) {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\t\ttext: `Error: Memory budget exceeded. ${target === \"memory\" ? \"MEMORY.md\" : \"USER.md\"} limit is ${budget} characters. Current operation would result in ${newContent.length} characters.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tdetails: { success: false, error: \"Memory budget exceeded\" },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Atomic write\n\t\t\t\t\t\tconst tmpPath = `${filePath}.tmp`;\n\t\t\t\t\t\tawait fs.writeFile(tmpPath, newContent, \"utf-8\");\n\t\t\t\t\t\tawait fs.rename(tmpPath, filePath);\n\n\t\t\t\t\t\t// Update in-memory tracker\n\t\t\t\t\t\tif (target === \"memory\") {\n\t\t\t\t\t\t\tthis.lastWrittenMemory = newContent;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthis.lastWrittenUser = newContent;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: `Successfully updated ${target === \"memory\" ? \"MEMORY.md\" : \"USER.md\"}.`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: true },\n\t\t\t\t\t\t};\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: `Error: Failed to perform memory operation: ${String(err)}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: false, error: String(err) },\n\t\t\t\t\t\t};\n\t\t\t\t\t} finally {\n\t\t\t\t\t\tif (release) {\n\t\t\t\t\t\t\tawait release();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"file-store.d.ts","sourceRoot":"","sources":["../../../../src/core/memory/providers/file-store.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAGhE,OAAO,KAAK,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEpF;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAsB3F;AAiBD,qBAAa,iBAAkB,YAAW,cAAc;IACvD,SAAgB,IAAI,gBAAgB;IAEpC,OAAO,CAAC,GAAG,CAAC,CAAyB;IACrC,OAAO,CAAC,cAAc,CAAM;IAC5B,OAAO,CAAC,YAAY,CAAM;IAE1B,OAAO,CAAC,iBAAiB,CAAM;IAC/B,OAAO,CAAC,eAAe,CAAM;IAG7B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAQ;IAC7C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAQ;IAEpC,WAAW,IAAI,OAAO,CAE5B;IAEM,eAAe;;MAErB;IAEY,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAqBtF;IAEM,iBAAiB,IAAI,MAAM,CA6BjC;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAGrD;IAEY,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAErC;IAEM,iBAAiB,IAAI,MAAM,EAAE,CAEnC;IAEM,kBAAkB,IAAI,cAAc,EAAE,CAqI5C;CACD","sourcesContent":["import { existsSync, promises as fs, mkdirSync, writeFileSync } from \"fs\";\nimport { join } from \"path\";\nimport lockfile from \"proper-lockfile\";\nimport { type Static, Type } from \"typebox\";\nimport type { ToolDefinition } from \"../../extensions/types.ts\";\nimport { scanContextFileThreats } from \"../../resource-loader.ts\";\nimport { jaccard, tokenize } from \"../../tools/skill-audit.ts\";\nimport type { MemoryLifecycleContext, MemoryProvider } from \"../memory-provider.ts\";\n\n/**\n * R5 confront-before-write (anti append-rot): if `content` is a near-duplicate of an existing\n * non-empty line (token Jaccard ≥ threshold — i.e. the same fact reworded), supersede that line in\n * place and return the rewritten file; otherwise return null (the caller appends normally).\n */\nexport function supersedeNearDuplicateLine(existing: string, content: string): string | null {\n\tconst NEAR_DUP_THRESHOLD = 0.6;\n\tconst contentTokens = tokenize(content);\n\tif (contentTokens.length === 0) return null;\n\tconst lines = existing.split(\"\\n\");\n\tlet bestIdx = -1;\n\tlet bestScore = NEAR_DUP_THRESHOLD;\n\tfor (let i = 0; i < lines.length; i++) {\n\t\tconst line = lines[i].trim();\n\t\tif (!line) continue;\n\t\t// Never supersede structural Markdown (headings, list markers as headings) — a fact must not\n\t\t// silently overwrite section structure (Bug #15).\n\t\tif (line.startsWith(\"#\")) continue;\n\t\tconst score = jaccard(contentTokens, tokenize(line));\n\t\tif (score >= bestScore) {\n\t\t\tbestScore = score;\n\t\t\tbestIdx = i;\n\t\t}\n\t}\n\tif (bestIdx === -1) return null;\n\tlines[bestIdx] = content;\n\treturn lines.join(\"\\n\");\n}\n\nconst memorySchema = Type.Object({\n\taction: Type.Union([Type.Literal(\"add\"), Type.Literal(\"replace\"), Type.Literal(\"remove\")], {\n\t\tdescription: \"Action to perform: add new content, replace existing content, or remove content\",\n\t}),\n\ttarget: Type.Union([Type.Literal(\"memory\"), Type.Literal(\"user\")], {\n\t\tdescription: \"Target file: 'memory' for MEMORY.md, 'user' for USER.md\",\n\t}),\n\tcontent: Type.Optional(Type.String({ description: \"Content to write (required for 'add' or 'replace')\" })),\n\toldContent: Type.Optional(\n\t\tType.String({ description: \"Exact substring to replace or remove (required for 'replace' or 'remove')\" }),\n\t),\n});\n\ntype MemoryParams = Static<typeof memorySchema>;\n\nexport class FileStoreProvider implements MemoryProvider {\n\tpublic readonly name = \"file-store\";\n\n\tprivate ctx?: MemoryLifecycleContext;\n\tprivate memoryFilePath = \"\";\n\tprivate userFilePath = \"\";\n\n\tprivate lastWrittenMemory = \"\";\n\tprivate lastWrittenUser = \"\";\n\n\t// Character budgets\n\tprivate static readonly BUDGET_MEMORY = 2200;\n\tprivate static readonly BUDGET_USER = 1375;\n\n\tpublic isAvailable(): boolean {\n\t\treturn true;\n\t}\n\n\tpublic getCapabilities() {\n\t\treturn { surfaces: [\"context\" as const] };\n\t}\n\n\tpublic async initialize(_sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.ctx = ctx;\n\t\tthis.memoryFilePath = join(ctx.agentDir, \"MEMORY.md\");\n\t\tthis.userFilePath = join(ctx.agentDir, \"USER.md\");\n\n\t\t// Ensure agentDir exists\n\t\tif (!existsSync(ctx.agentDir)) {\n\t\t\tmkdirSync(ctx.agentDir, { recursive: true });\n\t\t}\n\n\t\t// Initialize files if they do not exist\n\t\tif (!existsSync(this.memoryFilePath)) {\n\t\t\twriteFileSync(this.memoryFilePath, \"\", \"utf-8\");\n\t\t}\n\t\tif (!existsSync(this.userFilePath)) {\n\t\t\twriteFileSync(this.userFilePath, \"\", \"utf-8\");\n\t\t}\n\n\t\t// Load initial contents\n\t\tthis.lastWrittenMemory = await fs.readFile(this.memoryFilePath, \"utf-8\");\n\t\tthis.lastWrittenUser = await fs.readFile(this.userFilePath, \"utf-8\");\n\t}\n\n\tpublic systemPromptBlock(): string {\n\t\tconst sanitize = (content: string) => {\n\t\t\tconst lines = content.split(\"\\n\");\n\t\t\tconst sanitizedLines = lines.map((line) => {\n\t\t\t\tconst threats = scanContextFileThreats(line);\n\t\t\t\tif (threats.length > 0) {\n\t\t\t\t\treturn `[BLOCKED: potential threat detected (${threats.join(\", \")})]`;\n\t\t\t\t}\n\t\t\t\treturn line;\n\t\t\t});\n\t\t\treturn sanitizedLines.join(\"\\n\");\n\t\t};\n\n\t\tconst mem = sanitize(this.lastWrittenMemory);\n\t\tconst usr = sanitize(this.lastWrittenUser);\n\n\t\tconst blocks: string[] = [];\n\t\tif (mem.trim()) {\n\t\t\tblocks.push(`## MEMORY.md:\\n${mem}`);\n\t\t}\n\t\tif (usr.trim()) {\n\t\t\tblocks.push(`## USER.md:\\n${usr}`);\n\t\t}\n\n\t\tif (blocks.length === 0) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\treturn `=== Persistent Memory (file-store) ===\\n[System Note: Below is a snapshot of your persistent memory. You can update these using the 'memory' tool.]\\n\\n${blocks.join(\"\\n\\n\")}`;\n\t}\n\n\tpublic async prefetch(_query: string): Promise<string> {\n\t\t// static system prompt block is sufficient for file-store default; no-op prefetch\n\t\treturn \"\";\n\t}\n\n\tpublic async shutdown(): Promise<void> {\n\t\t// no-op\n\t}\n\n\tpublic getContextMarkers(): string[] {\n\t\treturn [];\n\t}\n\n\tpublic getToolDefinitions(): ToolDefinition[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\tname: \"memory\",\n\t\t\t\tlabel: \"Persistent Memory Manager\",\n\t\t\t\tdescription: \"Add, replace, or remove contents in persistent memory files (MEMORY.md/USER.md).\",\n\t\t\t\tparameters: memorySchema,\n\t\t\t\texecute: async (_toolCallId, params: MemoryParams, _signal, _onUpdate, _execCtx) => {\n\t\t\t\t\tif (this.ctx?.isChildSession) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: \"Error: Writes to persistent memory are not allowed in child sessions (subagents).\",\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: false, error: \"Child session write-gated\" },\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tconst { action, target, content, oldContent } = params;\n\t\t\t\t\tconst filePath = target === \"memory\" ? this.memoryFilePath : this.userFilePath;\n\t\t\t\t\tconst budget = target === \"memory\" ? FileStoreProvider.BUDGET_MEMORY : FileStoreProvider.BUDGET_USER;\n\n\t\t\t\t\tlet release: (() => Promise<void>) | undefined;\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// File lock\n\t\t\t\t\t\trelease = await lockfile.lock(filePath, { realpath: false, retries: 5 });\n\n\t\t\t\t\t\tconst lastWritten = target === \"memory\" ? this.lastWrittenMemory : this.lastWrittenUser;\n\t\t\t\t\t\t// Read current file content on disk for drift detection\n\t\t\t\t\t\tconst currentOnDisk = await fs.readFile(filePath, \"utf-8\");\n\t\t\t\t\t\tif (currentOnDisk !== lastWritten) {\n\t\t\t\t\t\t\t// Drift detected. Backup current file and refuse write.\n\t\t\t\t\t\t\tconst backupPath = `${filePath}.bak.${Date.now()}`;\n\t\t\t\t\t\t\tawait fs.writeFile(backupPath, currentOnDisk, \"utf-8\");\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\t\ttext: `Error: Drift detected. The memory file has been modified out-of-band by an external process. A backup was created at ${backupPath}. Operation aborted.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tdetails: { success: false, error: \"Drift detected\" },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet newContent = currentOnDisk;\n\t\t\t\t\t\tif (action === \"add\") {\n\t\t\t\t\t\t\tif (content === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameter 'content' is required for action 'add'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// R5: confront before write. If this fact is a near-duplicate of an existing line,\n\t\t\t\t\t\t\t// supersede it in place instead of appending a redundant copy (prevents append-rot).\n\t\t\t\t\t\t\tconst superseded = supersedeNearDuplicateLine(currentOnDisk, content);\n\t\t\t\t\t\t\tif (superseded !== null) {\n\t\t\t\t\t\t\t\tnewContent = superseded;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tnewContent =\n\t\t\t\t\t\t\t\t\tnewContent.endsWith(\"\\n\") || newContent === \"\"\n\t\t\t\t\t\t\t\t\t\t? `${newContent}${content}\\n`\n\t\t\t\t\t\t\t\t\t\t: `${newContent}\\n${content}\\n`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if (action === \"replace\") {\n\t\t\t\t\t\t\tif (content === undefined || oldContent === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameters 'content' and 'oldContent' are required for action 'replace'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!currentOnDisk.includes(oldContent)) {\n\t\t\t\t\t\t\t\tthrow new Error(`The content to replace ('oldContent') was not found in the file.`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnewContent = currentOnDisk.replace(oldContent, content);\n\t\t\t\t\t\t} else if (action === \"remove\") {\n\t\t\t\t\t\t\tif (oldContent === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameter 'oldContent' is required for action 'remove'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!currentOnDisk.includes(oldContent)) {\n\t\t\t\t\t\t\t\tthrow new Error(`The content to remove ('oldContent') was not found in the file.`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnewContent = currentOnDisk.replace(oldContent, \"\");\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Budget check\n\t\t\t\t\t\tif (newContent.length > budget) {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\t\ttext: `Error: Memory budget exceeded. ${target === \"memory\" ? \"MEMORY.md\" : \"USER.md\"} limit is ${budget} characters. Current operation would result in ${newContent.length} characters.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tdetails: { success: false, error: \"Memory budget exceeded\" },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Atomic write\n\t\t\t\t\t\tconst tmpPath = `${filePath}.tmp`;\n\t\t\t\t\t\tawait fs.writeFile(tmpPath, newContent, \"utf-8\");\n\t\t\t\t\t\tawait fs.rename(tmpPath, filePath);\n\n\t\t\t\t\t\t// Update in-memory tracker\n\t\t\t\t\t\tif (target === \"memory\") {\n\t\t\t\t\t\t\tthis.lastWrittenMemory = newContent;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthis.lastWrittenUser = newContent;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: `Successfully updated ${target === \"memory\" ? \"MEMORY.md\" : \"USER.md\"}.`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: true },\n\t\t\t\t\t\t};\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: `Error: Failed to perform memory operation: ${String(err)}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: false, error: String(err) },\n\t\t\t\t\t\t};\n\t\t\t\t\t} finally {\n\t\t\t\t\t\tif (release) {\n\t\t\t\t\t\t\tawait release();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\t}\n}\n"]}
|
|
@@ -21,6 +21,10 @@ export function supersedeNearDuplicateLine(existing, content) {
|
|
|
21
21
|
const line = lines[i].trim();
|
|
22
22
|
if (!line)
|
|
23
23
|
continue;
|
|
24
|
+
// Never supersede structural Markdown (headings, list markers as headings) — a fact must not
|
|
25
|
+
// silently overwrite section structure (Bug #15).
|
|
26
|
+
if (line.startsWith("#"))
|
|
27
|
+
continue;
|
|
24
28
|
const score = jaccard(contentTokens, tokenize(line));
|
|
25
29
|
if (score >= bestScore) {
|
|
26
30
|
bestScore = score;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-store.js","sourceRoot":"","sources":["../../../../src/core/memory/providers/file-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,IAAI,EAAE,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAC1E,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,QAAQ,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAG/D;;;;GAIG;AACH,MAAM,UAAU,0BAA0B,CAAC,QAAgB,EAAE,OAAe,EAAiB;IAC5F,MAAM,kBAAkB,GAAG,GAAG,CAAC;IAC/B,MAAM,aAAa,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;IACxC,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAC5C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,C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AC;oBACF,CAAC;gBAAA,CACD;aACD;SACD,CAAC;IAAA,CACF;CACD","sourcesContent":["import { existsSync, promises as fs, mkdirSync, writeFileSync } from \"fs\";\nimport { join } from \"path\";\nimport lockfile from \"proper-lockfile\";\nimport { type Static, Type } from \"typebox\";\nimport type { ToolDefinition } from \"../../extensions/types.ts\";\nimport { scanContextFileThreats } from \"../../resource-loader.ts\";\nimport { jaccard, tokenize } from \"../../tools/skill-audit.ts\";\nimport type { MemoryLifecycleContext, MemoryProvider } from \"../memory-provider.ts\";\n\n/**\n * R5 confront-before-write (anti append-rot): if `content` is a near-duplicate of an existing\n * non-empty line (token Jaccard ≥ threshold — i.e. the same fact reworded), supersede that line in\n * place and return the rewritten file; otherwise return null (the caller appends normally).\n */\nexport function supersedeNearDuplicateLine(existing: string, content: string): string | null {\n\tconst NEAR_DUP_THRESHOLD = 0.6;\n\tconst contentTokens = tokenize(content);\n\tif (contentTokens.length === 0) return null;\n\tconst lines = existing.split(\"\\n\");\n\tlet bestIdx = -1;\n\tlet bestScore = NEAR_DUP_THRESHOLD;\n\tfor (let i = 0; i < lines.length; i++) {\n\t\tconst line = lines[i].trim();\n\t\tif (!line) continue;\n\t\tconst score = jaccard(contentTokens, tokenize(line));\n\t\tif (score >= bestScore) {\n\t\t\tbestScore = score;\n\t\t\tbestIdx = i;\n\t\t}\n\t}\n\tif (bestIdx === -1) return null;\n\tlines[bestIdx] = content;\n\treturn lines.join(\"\\n\");\n}\n\nconst memorySchema = Type.Object({\n\taction: Type.Union([Type.Literal(\"add\"), Type.Literal(\"replace\"), Type.Literal(\"remove\")], {\n\t\tdescription: \"Action to perform: add new content, replace existing content, or remove content\",\n\t}),\n\ttarget: Type.Union([Type.Literal(\"memory\"), Type.Literal(\"user\")], {\n\t\tdescription: \"Target file: 'memory' for MEMORY.md, 'user' for USER.md\",\n\t}),\n\tcontent: Type.Optional(Type.String({ description: \"Content to write (required for 'add' or 'replace')\" })),\n\toldContent: Type.Optional(\n\t\tType.String({ description: \"Exact substring to replace or remove (required for 'replace' or 'remove')\" }),\n\t),\n});\n\ntype MemoryParams = Static<typeof memorySchema>;\n\nexport class FileStoreProvider implements MemoryProvider {\n\tpublic readonly name = \"file-store\";\n\n\tprivate ctx?: MemoryLifecycleContext;\n\tprivate memoryFilePath = \"\";\n\tprivate userFilePath = \"\";\n\n\tprivate lastWrittenMemory = \"\";\n\tprivate lastWrittenUser = \"\";\n\n\t// Character budgets\n\tprivate static readonly BUDGET_MEMORY = 2200;\n\tprivate static readonly BUDGET_USER = 1375;\n\n\tpublic isAvailable(): boolean {\n\t\treturn true;\n\t}\n\n\tpublic getCapabilities() {\n\t\treturn { surfaces: [\"context\" as const] };\n\t}\n\n\tpublic async initialize(_sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.ctx = ctx;\n\t\tthis.memoryFilePath = join(ctx.agentDir, \"MEMORY.md\");\n\t\tthis.userFilePath = join(ctx.agentDir, \"USER.md\");\n\n\t\t// Ensure agentDir exists\n\t\tif (!existsSync(ctx.agentDir)) {\n\t\t\tmkdirSync(ctx.agentDir, { recursive: true });\n\t\t}\n\n\t\t// Initialize files if they do not exist\n\t\tif (!existsSync(this.memoryFilePath)) {\n\t\t\twriteFileSync(this.memoryFilePath, \"\", \"utf-8\");\n\t\t}\n\t\tif (!existsSync(this.userFilePath)) {\n\t\t\twriteFileSync(this.userFilePath, \"\", \"utf-8\");\n\t\t}\n\n\t\t// Load initial contents\n\t\tthis.lastWrittenMemory = await fs.readFile(this.memoryFilePath, \"utf-8\");\n\t\tthis.lastWrittenUser = await fs.readFile(this.userFilePath, \"utf-8\");\n\t}\n\n\tpublic systemPromptBlock(): string {\n\t\tconst sanitize = (content: string) => {\n\t\t\tconst lines = content.split(\"\\n\");\n\t\t\tconst sanitizedLines = lines.map((line) => {\n\t\t\t\tconst threats = scanContextFileThreats(line);\n\t\t\t\tif (threats.length > 0) {\n\t\t\t\t\treturn `[BLOCKED: potential threat detected (${threats.join(\", \")})]`;\n\t\t\t\t}\n\t\t\t\treturn line;\n\t\t\t});\n\t\t\treturn sanitizedLines.join(\"\\n\");\n\t\t};\n\n\t\tconst mem = sanitize(this.lastWrittenMemory);\n\t\tconst usr = sanitize(this.lastWrittenUser);\n\n\t\tconst blocks: string[] = [];\n\t\tif (mem.trim()) {\n\t\t\tblocks.push(`## MEMORY.md:\\n${mem}`);\n\t\t}\n\t\tif (usr.trim()) {\n\t\t\tblocks.push(`## USER.md:\\n${usr}`);\n\t\t}\n\n\t\tif (blocks.length === 0) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\treturn `=== Persistent Memory (file-store) ===\\n[System Note: Below is a snapshot of your persistent memory. You can update these using the 'memory' tool.]\\n\\n${blocks.join(\"\\n\\n\")}`;\n\t}\n\n\tpublic async prefetch(_query: string): Promise<string> {\n\t\t// static system prompt block is sufficient for file-store default; no-op prefetch\n\t\treturn \"\";\n\t}\n\n\tpublic async shutdown(): Promise<void> {\n\t\t// no-op\n\t}\n\n\tpublic getContextMarkers(): string[] {\n\t\treturn [];\n\t}\n\n\tpublic getToolDefinitions(): ToolDefinition[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\tname: \"memory\",\n\t\t\t\tlabel: \"Persistent Memory Manager\",\n\t\t\t\tdescription: \"Add, replace, or remove contents in persistent memory files (MEMORY.md/USER.md).\",\n\t\t\t\tparameters: memorySchema,\n\t\t\t\texecute: async (_toolCallId, params: MemoryParams, _signal, _onUpdate, _execCtx) => {\n\t\t\t\t\tif (this.ctx?.isChildSession) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: \"Error: Writes to persistent memory are not allowed in child sessions (subagents).\",\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: false, error: \"Child session write-gated\" },\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tconst { action, target, content, oldContent } = params;\n\t\t\t\t\tconst filePath = target === \"memory\" ? this.memoryFilePath : this.userFilePath;\n\t\t\t\t\tconst budget = target === \"memory\" ? FileStoreProvider.BUDGET_MEMORY : FileStoreProvider.BUDGET_USER;\n\n\t\t\t\t\tlet release: (() => Promise<void>) | undefined;\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// File lock\n\t\t\t\t\t\trelease = await lockfile.lock(filePath, { realpath: false, retries: 5 });\n\n\t\t\t\t\t\tconst lastWritten = target === \"memory\" ? this.lastWrittenMemory : this.lastWrittenUser;\n\t\t\t\t\t\t// Read current file content on disk for drift detection\n\t\t\t\t\t\tconst currentOnDisk = await fs.readFile(filePath, \"utf-8\");\n\t\t\t\t\t\tif (currentOnDisk !== lastWritten) {\n\t\t\t\t\t\t\t// Drift detected. Backup current file and refuse write.\n\t\t\t\t\t\t\tconst backupPath = `${filePath}.bak.${Date.now()}`;\n\t\t\t\t\t\t\tawait fs.writeFile(backupPath, currentOnDisk, \"utf-8\");\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\t\ttext: `Error: Drift detected. The memory file has been modified out-of-band by an external process. A backup was created at ${backupPath}. Operation aborted.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tdetails: { success: false, error: \"Drift detected\" },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet newContent = currentOnDisk;\n\t\t\t\t\t\tif (action === \"add\") {\n\t\t\t\t\t\t\tif (content === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameter 'content' is required for action 'add'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// R5: confront before write. If this fact is a near-duplicate of an existing line,\n\t\t\t\t\t\t\t// supersede it in place instead of appending a redundant copy (prevents append-rot).\n\t\t\t\t\t\t\tconst superseded = supersedeNearDuplicateLine(currentOnDisk, content);\n\t\t\t\t\t\t\tif (superseded !== null) {\n\t\t\t\t\t\t\t\tnewContent = superseded;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tnewContent =\n\t\t\t\t\t\t\t\t\tnewContent.endsWith(\"\\n\") || newContent === \"\"\n\t\t\t\t\t\t\t\t\t\t? `${newContent}${content}\\n`\n\t\t\t\t\t\t\t\t\t\t: `${newContent}\\n${content}\\n`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if (action === \"replace\") {\n\t\t\t\t\t\t\tif (content === undefined || oldContent === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameters 'content' and 'oldContent' are required for action 'replace'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!currentOnDisk.includes(oldContent)) {\n\t\t\t\t\t\t\t\tthrow new Error(`The content to replace ('oldContent') was not found in the file.`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnewContent = currentOnDisk.replace(oldContent, content);\n\t\t\t\t\t\t} else if (action === \"remove\") {\n\t\t\t\t\t\t\tif (oldContent === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameter 'oldContent' is required for action 'remove'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!currentOnDisk.includes(oldContent)) {\n\t\t\t\t\t\t\t\tthrow new Error(`The content to remove ('oldContent') was not found in the file.`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnewContent = currentOnDisk.replace(oldContent, \"\");\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Budget check\n\t\t\t\t\t\tif (newContent.length > budget) {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\t\ttext: `Error: Memory budget exceeded. ${target === \"memory\" ? \"MEMORY.md\" : \"USER.md\"} limit is ${budget} characters. Current operation would result in ${newContent.length} characters.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tdetails: { success: false, error: \"Memory budget exceeded\" },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Atomic write\n\t\t\t\t\t\tconst tmpPath = `${filePath}.tmp`;\n\t\t\t\t\t\tawait fs.writeFile(tmpPath, newContent, \"utf-8\");\n\t\t\t\t\t\tawait fs.rename(tmpPath, filePath);\n\n\t\t\t\t\t\t// Update in-memory tracker\n\t\t\t\t\t\tif (target === \"memory\") {\n\t\t\t\t\t\t\tthis.lastWrittenMemory = newContent;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthis.lastWrittenUser = newContent;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: `Successfully updated ${target === \"memory\" ? \"MEMORY.md\" : \"USER.md\"}.`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: true },\n\t\t\t\t\t\t};\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: `Error: Failed to perform memory operation: ${String(err)}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: false, error: String(err) },\n\t\t\t\t\t\t};\n\t\t\t\t\t} finally {\n\t\t\t\t\t\tif (release) {\n\t\t\t\t\t\t\tawait release();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},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jD,MAAM,EAAE,CAAC,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;wBAEnC,2BAA2B;wBAC3B,IAAI,MAAM,KAAK,QAAQ,EAAE,CAAC;4BACzB,IAAI,CAAC,iBAAiB,GAAG,UAAU,CAAC;wBACrC,CAAC;6BAAM,CAAC;4BACP,IAAI,CAAC,eAAe,GAAG,UAAU,CAAC;wBACnC,CAAC;wBAED,OAAO;4BACN,OAAO,EAAE;gCACR;oCACC,IAAI,EAAE,MAAM;oCACZ,IAAI,EAAE,wBAAwB,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,GAAG;iCAC9E;6BACD;4BACD,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;yBAC1B,CAAC;oBACH,CAAC;oBAAC,OAAO,GAAG,EAAE,CAAC;wBACd,OAAO;4BACN,OAAO,EAAE;gCACR;oCACC,IAAI,EAAE,MAAM;oCACZ,IAAI,EAAE,8CAA8C,MAAM,CAAC,GAAG,CAAC,EAAE;iCACjE;6BACD;4BACD,OAAO,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE;yBAC/C,CAAC;oBACH,CAAC;4BAAS,CAAC;wBACV,IAAI,OAAO,EAAE,CAAC;4BACb,MAAM,OAAO,EAAE,CAAC;wBACjB,CAAC;oBACF,CAAC;gBAAA,CACD;aACD;SACD,CAAC;IAAA,CACF;CACD","sourcesContent":["import { existsSync, promises as fs, mkdirSync, writeFileSync } from \"fs\";\nimport { join } from \"path\";\nimport lockfile from \"proper-lockfile\";\nimport { type Static, Type } from \"typebox\";\nimport type { ToolDefinition } from \"../../extensions/types.ts\";\nimport { scanContextFileThreats } from \"../../resource-loader.ts\";\nimport { jaccard, tokenize } from \"../../tools/skill-audit.ts\";\nimport type { MemoryLifecycleContext, MemoryProvider } from \"../memory-provider.ts\";\n\n/**\n * R5 confront-before-write (anti append-rot): if `content` is a near-duplicate of an existing\n * non-empty line (token Jaccard ≥ threshold — i.e. the same fact reworded), supersede that line in\n * place and return the rewritten file; otherwise return null (the caller appends normally).\n */\nexport function supersedeNearDuplicateLine(existing: string, content: string): string | null {\n\tconst NEAR_DUP_THRESHOLD = 0.6;\n\tconst contentTokens = tokenize(content);\n\tif (contentTokens.length === 0) return null;\n\tconst lines = existing.split(\"\\n\");\n\tlet bestIdx = -1;\n\tlet bestScore = NEAR_DUP_THRESHOLD;\n\tfor (let i = 0; i < lines.length; i++) {\n\t\tconst line = lines[i].trim();\n\t\tif (!line) continue;\n\t\t// Never supersede structural Markdown (headings, list markers as headings) — a fact must not\n\t\t// silently overwrite section structure (Bug #15).\n\t\tif (line.startsWith(\"#\")) continue;\n\t\tconst score = jaccard(contentTokens, tokenize(line));\n\t\tif (score >= bestScore) {\n\t\t\tbestScore = score;\n\t\t\tbestIdx = i;\n\t\t}\n\t}\n\tif (bestIdx === -1) return null;\n\tlines[bestIdx] = content;\n\treturn lines.join(\"\\n\");\n}\n\nconst memorySchema = Type.Object({\n\taction: Type.Union([Type.Literal(\"add\"), Type.Literal(\"replace\"), Type.Literal(\"remove\")], {\n\t\tdescription: \"Action to perform: add new content, replace existing content, or remove content\",\n\t}),\n\ttarget: Type.Union([Type.Literal(\"memory\"), Type.Literal(\"user\")], {\n\t\tdescription: \"Target file: 'memory' for MEMORY.md, 'user' for USER.md\",\n\t}),\n\tcontent: Type.Optional(Type.String({ description: \"Content to write (required for 'add' or 'replace')\" })),\n\toldContent: Type.Optional(\n\t\tType.String({ description: \"Exact substring to replace or remove (required for 'replace' or 'remove')\" }),\n\t),\n});\n\ntype MemoryParams = Static<typeof memorySchema>;\n\nexport class FileStoreProvider implements MemoryProvider {\n\tpublic readonly name = \"file-store\";\n\n\tprivate ctx?: MemoryLifecycleContext;\n\tprivate memoryFilePath = \"\";\n\tprivate userFilePath = \"\";\n\n\tprivate lastWrittenMemory = \"\";\n\tprivate lastWrittenUser = \"\";\n\n\t// Character budgets\n\tprivate static readonly BUDGET_MEMORY = 2200;\n\tprivate static readonly BUDGET_USER = 1375;\n\n\tpublic isAvailable(): boolean {\n\t\treturn true;\n\t}\n\n\tpublic getCapabilities() {\n\t\treturn { surfaces: [\"context\" as const] };\n\t}\n\n\tpublic async initialize(_sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.ctx = ctx;\n\t\tthis.memoryFilePath = join(ctx.agentDir, \"MEMORY.md\");\n\t\tthis.userFilePath = join(ctx.agentDir, \"USER.md\");\n\n\t\t// Ensure agentDir exists\n\t\tif (!existsSync(ctx.agentDir)) {\n\t\t\tmkdirSync(ctx.agentDir, { recursive: true });\n\t\t}\n\n\t\t// Initialize files if they do not exist\n\t\tif (!existsSync(this.memoryFilePath)) {\n\t\t\twriteFileSync(this.memoryFilePath, \"\", \"utf-8\");\n\t\t}\n\t\tif (!existsSync(this.userFilePath)) {\n\t\t\twriteFileSync(this.userFilePath, \"\", \"utf-8\");\n\t\t}\n\n\t\t// Load initial contents\n\t\tthis.lastWrittenMemory = await fs.readFile(this.memoryFilePath, \"utf-8\");\n\t\tthis.lastWrittenUser = await fs.readFile(this.userFilePath, \"utf-8\");\n\t}\n\n\tpublic systemPromptBlock(): string {\n\t\tconst sanitize = (content: string) => {\n\t\t\tconst lines = content.split(\"\\n\");\n\t\t\tconst sanitizedLines = lines.map((line) => {\n\t\t\t\tconst threats = scanContextFileThreats(line);\n\t\t\t\tif (threats.length > 0) {\n\t\t\t\t\treturn `[BLOCKED: potential threat detected (${threats.join(\", \")})]`;\n\t\t\t\t}\n\t\t\t\treturn line;\n\t\t\t});\n\t\t\treturn sanitizedLines.join(\"\\n\");\n\t\t};\n\n\t\tconst mem = sanitize(this.lastWrittenMemory);\n\t\tconst usr = sanitize(this.lastWrittenUser);\n\n\t\tconst blocks: string[] = [];\n\t\tif (mem.trim()) {\n\t\t\tblocks.push(`## MEMORY.md:\\n${mem}`);\n\t\t}\n\t\tif (usr.trim()) {\n\t\t\tblocks.push(`## USER.md:\\n${usr}`);\n\t\t}\n\n\t\tif (blocks.length === 0) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\treturn `=== Persistent Memory (file-store) ===\\n[System Note: Below is a snapshot of your persistent memory. You can update these using the 'memory' tool.]\\n\\n${blocks.join(\"\\n\\n\")}`;\n\t}\n\n\tpublic async prefetch(_query: string): Promise<string> {\n\t\t// static system prompt block is sufficient for file-store default; no-op prefetch\n\t\treturn \"\";\n\t}\n\n\tpublic async shutdown(): Promise<void> {\n\t\t// no-op\n\t}\n\n\tpublic getContextMarkers(): string[] {\n\t\treturn [];\n\t}\n\n\tpublic getToolDefinitions(): ToolDefinition[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\tname: \"memory\",\n\t\t\t\tlabel: \"Persistent Memory Manager\",\n\t\t\t\tdescription: \"Add, replace, or remove contents in persistent memory files (MEMORY.md/USER.md).\",\n\t\t\t\tparameters: memorySchema,\n\t\t\t\texecute: async (_toolCallId, params: MemoryParams, _signal, _onUpdate, _execCtx) => {\n\t\t\t\t\tif (this.ctx?.isChildSession) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: \"Error: Writes to persistent memory are not allowed in child sessions (subagents).\",\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: false, error: \"Child session write-gated\" },\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tconst { action, target, content, oldContent } = params;\n\t\t\t\t\tconst filePath = target === \"memory\" ? this.memoryFilePath : this.userFilePath;\n\t\t\t\t\tconst budget = target === \"memory\" ? FileStoreProvider.BUDGET_MEMORY : FileStoreProvider.BUDGET_USER;\n\n\t\t\t\t\tlet release: (() => Promise<void>) | undefined;\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// File lock\n\t\t\t\t\t\trelease = await lockfile.lock(filePath, { realpath: false, retries: 5 });\n\n\t\t\t\t\t\tconst lastWritten = target === \"memory\" ? this.lastWrittenMemory : this.lastWrittenUser;\n\t\t\t\t\t\t// Read current file content on disk for drift detection\n\t\t\t\t\t\tconst currentOnDisk = await fs.readFile(filePath, \"utf-8\");\n\t\t\t\t\t\tif (currentOnDisk !== lastWritten) {\n\t\t\t\t\t\t\t// Drift detected. Backup current file and refuse write.\n\t\t\t\t\t\t\tconst backupPath = `${filePath}.bak.${Date.now()}`;\n\t\t\t\t\t\t\tawait fs.writeFile(backupPath, currentOnDisk, \"utf-8\");\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\t\ttext: `Error: Drift detected. The memory file has been modified out-of-band by an external process. A backup was created at ${backupPath}. Operation aborted.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tdetails: { success: false, error: \"Drift detected\" },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet newContent = currentOnDisk;\n\t\t\t\t\t\tif (action === \"add\") {\n\t\t\t\t\t\t\tif (content === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameter 'content' is required for action 'add'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// R5: confront before write. If this fact is a near-duplicate of an existing line,\n\t\t\t\t\t\t\t// supersede it in place instead of appending a redundant copy (prevents append-rot).\n\t\t\t\t\t\t\tconst superseded = supersedeNearDuplicateLine(currentOnDisk, content);\n\t\t\t\t\t\t\tif (superseded !== null) {\n\t\t\t\t\t\t\t\tnewContent = superseded;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tnewContent =\n\t\t\t\t\t\t\t\t\tnewContent.endsWith(\"\\n\") || newContent === \"\"\n\t\t\t\t\t\t\t\t\t\t? `${newContent}${content}\\n`\n\t\t\t\t\t\t\t\t\t\t: `${newContent}\\n${content}\\n`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if (action === \"replace\") {\n\t\t\t\t\t\t\tif (content === undefined || oldContent === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameters 'content' and 'oldContent' are required for action 'replace'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!currentOnDisk.includes(oldContent)) {\n\t\t\t\t\t\t\t\tthrow new Error(`The content to replace ('oldContent') was not found in the file.`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnewContent = currentOnDisk.replace(oldContent, content);\n\t\t\t\t\t\t} else if (action === \"remove\") {\n\t\t\t\t\t\t\tif (oldContent === undefined) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Parameter 'oldContent' is required for action 'remove'.\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!currentOnDisk.includes(oldContent)) {\n\t\t\t\t\t\t\t\tthrow new Error(`The content to remove ('oldContent') was not found in the file.`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnewContent = currentOnDisk.replace(oldContent, \"\");\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Budget check\n\t\t\t\t\t\tif (newContent.length > budget) {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\t\ttext: `Error: Memory budget exceeded. ${target === \"memory\" ? \"MEMORY.md\" : \"USER.md\"} limit is ${budget} characters. Current operation would result in ${newContent.length} characters.`,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tdetails: { success: false, error: \"Memory budget exceeded\" },\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Atomic write\n\t\t\t\t\t\tconst tmpPath = `${filePath}.tmp`;\n\t\t\t\t\t\tawait fs.writeFile(tmpPath, newContent, \"utf-8\");\n\t\t\t\t\t\tawait fs.rename(tmpPath, filePath);\n\n\t\t\t\t\t\t// Update in-memory tracker\n\t\t\t\t\t\tif (target === \"memory\") {\n\t\t\t\t\t\t\tthis.lastWrittenMemory = newContent;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthis.lastWrittenUser = newContent;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: `Successfully updated ${target === \"memory\" ? \"MEMORY.md\" : \"USER.md\"}.`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: true },\n\t\t\t\t\t\t};\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\t\ttext: `Error: Failed to perform memory operation: ${String(err)}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdetails: { success: false, error: String(err) },\n\t\t\t\t\t\t};\n\t\t\t\t\t} finally {\n\t\t\t\t\t\tif (release) {\n\t\t\t\t\t\t\tawait release();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transcript-recall.d.ts","sourceRoot":"","sources":["../../../../src/core/memory/providers/transcript-recall.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;
|
|
1
|
+
{"version":3,"file":"transcript-recall.d.ts","sourceRoot":"","sources":["../../../../src/core/memory/providers/transcript-recall.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAWH,OAAO,KAAK,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAUxG,qBAAa,wBAAyB,YAAW,cAAc;IAC9D,QAAQ,CAAC,IAAI,uBAAuB;IACpC,OAAO,CAAC,KAAK,CAA8B;IAC3C,OAAO,CAAC,gBAAgB,CAAM;IAC9B,OAAO,CAAC,GAAG,CAAM;IACjB,OAAO,CAAC,QAAQ,CAAM;IAEtB,WAAW,IAAI,OAAO,CAErB;IAED,eAAe,IAAI,kBAAkB,CAEpC;IAEK,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAK9E;IAEK,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAE9B;IAED,wFAAwF;IACxF,iBAAiB,IAAI,MAAM,EAAE,CAE5B;IAEK,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAqB7C;IAED,OAAO,CAAC,WAAW;IAOnB,OAAO,CAAC,SAAS;CA+CjB","sourcesContent":["/**\n * TranscriptRecallProvider — cross-session similarity recall (adaptive-agent design R3).\n *\n * A read-only CONTEXT memory provider: it indexes the most-recent past session transcripts (the JSONL\n * corpus) with a dependency-free token/Jaccard index ({@link TranscriptIndex}, reusing skill_audit's\n * tokenizer) and answers `prefetch(query)` with a small `<memory_context>` recall page of the most\n * relevant past snippets. The current session and auto-learn sessions are excluded. It never writes —\n * the file-store remains the write target; this is the recall corpus.\n */\n\nimport { readdirSync, statSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { wrapUntrustedText } from \"../../security/untrusted-boundary.ts\";\nimport {\n\ttype FileEntry,\n\tgetDefaultSessionDir,\n\tisAutoLearnSessionId,\n\tloadEntriesFromFile,\n} from \"../../session-manager.ts\";\nimport type { MemoryCapabilities, MemoryLifecycleContext, MemoryProvider } from \"../memory-provider.ts\";\nimport { type TranscriptDoc, TranscriptIndex } from \"../transcript-index.ts\";\n\n/** Most-recent past sessions to consider. */\nconst MAX_SESSIONS = 60;\n/** Per-session text cap (keeps the index light and snippets relevant). */\nconst MAX_DOC_CHARS = 8_000;\n/** Overall corpus cap across all docs. */\nconst MAX_TOTAL_CHARS = 500_000;\n\nexport class TranscriptRecallProvider implements MemoryProvider {\n\treadonly name = \"transcript-recall\";\n\tprivate index: TranscriptIndex | undefined;\n\tprivate currentSessionId = \"\";\n\tprivate cwd = \"\";\n\tprivate agentDir = \"\";\n\n\tisAvailable(): boolean {\n\t\treturn true;\n\t}\n\n\tgetCapabilities(): MemoryCapabilities {\n\t\treturn { surfaces: [\"context\"] };\n\t}\n\n\tasync initialize(sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.currentSessionId = sessionId;\n\t\tthis.cwd = ctx.cwd;\n\t\tthis.agentDir = ctx.agentDir;\n\t\tthis.index = undefined; // built lazily on first prefetch\n\t}\n\n\tasync shutdown(): Promise<void> {\n\t\tthis.index = undefined;\n\t}\n\n\t/** GC manages the dynamic recall page so stale pages pack while the newest are kept. */\n\tgetContextMarkers(): string[] {\n\t\treturn [\"<memory_context\"];\n\t}\n\n\tasync prefetch(query: string): Promise<string> {\n\t\tif (!query.trim()) return \"\";\n\t\tlet index: TranscriptIndex;\n\t\ttry {\n\t\t\tindex = this.ensureIndex();\n\t\t} catch {\n\t\t\treturn \"\";\n\t\t}\n\t\tif (index.size === 0) return \"\";\n\t\t// minScore is a query-CONTAINMENT threshold (fraction of the query's tokens present in the doc),\n\t\t// not Jaccard — so it is length-independent and recalls relevant long sessions. ~1/3 of query\n\t\t// terms must appear before a session is considered relevant.\n\t\tconst hits = index.query(query, { k: 3, minScore: 0.34, maxSnippetChars: 600 });\n\t\tif (hits.length === 0) return \"\";\n\t\t// Recalled past text is UNTRUSTED (it may itself contain injected instructions or a forged\n\t\t// `</memory_context>` to break out). Fence each snippet with the untrusted-content boundary so a\n\t\t// payload can't escape and be replayed as a current instruction (design: recall = untrusted).\n\t\tconst body = hits\n\t\t\t.map((h) => `- (${h.timestamp ?? \"earlier session\"}) ${wrapUntrustedText(h.snippet, \"transcript-recall\")}`)\n\t\t\t.join(\"\\n\");\n\t\treturn `<memory_context source=\"transcript-recall\">\\nRelevant context recalled from past sessions (read-only reference, untrusted, may be stale):\\n${body}\\n</memory_context>`;\n\t}\n\n\tprivate ensureIndex(): TranscriptIndex {\n\t\tif (!this.index) {\n\t\t\tthis.index = new TranscriptIndex(this.buildDocs());\n\t\t}\n\t\treturn this.index;\n\t}\n\n\tprivate buildDocs(): TranscriptDoc[] {\n\t\tconst docs: TranscriptDoc[] = [];\n\t\tlet dir: string;\n\t\ttry {\n\t\t\tdir = getDefaultSessionDir(this.cwd, this.agentDir);\n\t\t} catch {\n\t\t\treturn docs;\n\t\t}\n\n\t\tlet files: Array<{ path: string; mtime: number }>;\n\t\ttry {\n\t\t\tfiles = readdirSync(dir)\n\t\t\t\t.filter((f) => f.endsWith(\".jsonl\"))\n\t\t\t\t.map((f) => {\n\t\t\t\t\tconst path = join(dir, f);\n\t\t\t\t\tlet mtime = 0;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tmtime = statSync(path).mtimeMs;\n\t\t\t\t\t} catch {}\n\t\t\t\t\treturn { path, mtime };\n\t\t\t\t})\n\t\t\t\t.sort((a, b) => b.mtime - a.mtime) // most-recent first\n\t\t\t\t.slice(0, MAX_SESSIONS);\n\t\t} catch {\n\t\t\treturn docs;\n\t\t}\n\n\t\tlet total = 0;\n\t\tfor (const { path } of files) {\n\t\t\tlet entries: FileEntry[];\n\t\t\ttry {\n\t\t\t\tentries = loadEntriesFromFile(path);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst header = entries.find((e): e is Extract<FileEntry, { type: \"session\" }> => e.type === \"session\");\n\t\t\tconst sessionId = header?.id;\n\t\t\tif (!sessionId || sessionId === this.currentSessionId || isAutoLearnSessionId(sessionId)) continue;\n\n\t\t\tconst text = extractSessionText(entries, MAX_DOC_CHARS);\n\t\t\tif (!text.trim()) continue;\n\t\t\tdocs.push({ sessionId, timestamp: header?.timestamp, text });\n\t\t\ttotal += text.length;\n\t\t\tif (total >= MAX_TOTAL_CHARS) break;\n\t\t}\n\t\treturn docs;\n\t}\n}\n\n/** Concatenate user+assistant text from a session's entries, capped to `maxChars`. */\nfunction extractSessionText(entries: FileEntry[], maxChars: number): string {\n\tconst parts: string[] = [];\n\tlet len = 0;\n\tfor (const entry of entries) {\n\t\tif (entry.type !== \"message\") continue;\n\t\tconst message = entry.message;\n\t\tif (message.role !== \"user\" && message.role !== \"assistant\") continue;\n\t\tconst content = message.content;\n\t\tlet text = \"\";\n\t\tif (typeof content === \"string\") {\n\t\t\ttext = content;\n\t\t} else if (Array.isArray(content)) {\n\t\t\ttext = content\n\t\t\t\t.map((b) => (b && typeof b === \"object\" && \"type\" in b && b.type === \"text\" ? (b.text ?? \"\") : \"\"))\n\t\t\t\t.join(\" \");\n\t\t}\n\t\ttext = text.trim();\n\t\tif (!text) continue;\n\t\t// Skip our own previously-injected recall pages so recalled snippets don't recirculate and\n\t\t// amplify across sessions (Bug #10).\n\t\tif (text.includes('<memory_context source=\"transcript-recall\"')) continue;\n\t\tparts.push(text);\n\t\tlen += text.length;\n\t\tif (len >= maxChars) break;\n\t}\n\treturn parts.join(\"\\n\").slice(0, maxChars);\n}\n"]}
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { readdirSync, statSync } from "node:fs";
|
|
11
11
|
import { join } from "node:path";
|
|
12
|
+
import { wrapUntrustedText } from "../../security/untrusted-boundary.js";
|
|
12
13
|
import { getDefaultSessionDir, isAutoLearnSessionId, loadEntriesFromFile, } from "../../session-manager.js";
|
|
13
14
|
import { TranscriptIndex } from "../transcript-index.js";
|
|
14
15
|
/** Most-recent past sessions to consider. */
|
|
@@ -60,8 +61,13 @@ export class TranscriptRecallProvider {
|
|
|
60
61
|
const hits = index.query(query, { k: 3, minScore: 0.34, maxSnippetChars: 600 });
|
|
61
62
|
if (hits.length === 0)
|
|
62
63
|
return "";
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
// Recalled past text is UNTRUSTED (it may itself contain injected instructions or a forged
|
|
65
|
+
// `</memory_context>` to break out). Fence each snippet with the untrusted-content boundary so a
|
|
66
|
+
// payload can't escape and be replayed as a current instruction (design: recall = untrusted).
|
|
67
|
+
const body = hits
|
|
68
|
+
.map((h) => `- (${h.timestamp ?? "earlier session"}) ${wrapUntrustedText(h.snippet, "transcript-recall")}`)
|
|
69
|
+
.join("\n");
|
|
70
|
+
return `<memory_context source="transcript-recall">\nRelevant context recalled from past sessions (read-only reference, untrusted, may be stale):\n${body}\n</memory_context>`;
|
|
65
71
|
}
|
|
66
72
|
ensureIndex() {
|
|
67
73
|
if (!this.index) {
|
|
@@ -144,6 +150,10 @@ function extractSessionText(entries, maxChars) {
|
|
|
144
150
|
text = text.trim();
|
|
145
151
|
if (!text)
|
|
146
152
|
continue;
|
|
153
|
+
// Skip our own previously-injected recall pages so recalled snippets don't recirculate and
|
|
154
|
+
// amplify across sessions (Bug #10).
|
|
155
|
+
if (text.includes('<memory_context source="transcript-recall"'))
|
|
156
|
+
continue;
|
|
147
157
|
parts.push(text);
|
|
148
158
|
len += text.length;
|
|
149
159
|
if (len >= maxChars)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transcript-recall.js","sourceRoot":"","sources":["../../../../src/core/memory/providers/transcript-recall.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAEN,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,GACnB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAsB,eAAe,EAAE,MAAM,wBAAwB,CAAC;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AC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjB,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC;QACnB,IAAI,GAAG,IAAI,QAAQ;YAAE,MAAM;IAC5B,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;AAAA,CAC3C","sourcesContent":["/**\n * TranscriptRecallProvider — cross-session similarity recall (adaptive-agent design R3).\n *\n * A read-only CONTEXT memory provider: it indexes the most-recent past session transcripts (the JSONL\n * corpus) with a dependency-free token/Jaccard index ({@link TranscriptIndex}, reusing skill_audit's\n * tokenizer) and answers `prefetch(query)` with a small `<memory_context>` recall page of the most\n * relevant past snippets. The current session and auto-learn sessions are excluded. It never writes —\n * the file-store remains the write target; this is the recall corpus.\n */\n\nimport { readdirSync, statSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport {\n\ttype FileEntry,\n\tgetDefaultSessionDir,\n\tisAutoLearnSessionId,\n\tloadEntriesFromFile,\n} from \"../../session-manager.ts\";\nimport type { MemoryCapabilities, MemoryLifecycleContext, MemoryProvider } from \"../memory-provider.ts\";\nimport { type TranscriptDoc, TranscriptIndex } from \"../transcript-index.ts\";\n\n/** Most-recent past sessions to consider. */\nconst MAX_SESSIONS = 60;\n/** Per-session text cap (keeps the index light and snippets relevant). */\nconst MAX_DOC_CHARS = 8_000;\n/** Overall corpus cap across all docs. */\nconst MAX_TOTAL_CHARS = 500_000;\n\nexport class TranscriptRecallProvider implements MemoryProvider {\n\treadonly name = \"transcript-recall\";\n\tprivate index: TranscriptIndex | undefined;\n\tprivate currentSessionId = \"\";\n\tprivate cwd = \"\";\n\tprivate agentDir = \"\";\n\n\tisAvailable(): boolean {\n\t\treturn true;\n\t}\n\n\tgetCapabilities(): MemoryCapabilities {\n\t\treturn { surfaces: [\"context\"] };\n\t}\n\n\tasync initialize(sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.currentSessionId = sessionId;\n\t\tthis.cwd = ctx.cwd;\n\t\tthis.agentDir = ctx.agentDir;\n\t\tthis.index = undefined; // built lazily on first prefetch\n\t}\n\n\tasync shutdown(): Promise<void> {\n\t\tthis.index = undefined;\n\t}\n\n\t/** GC manages the dynamic recall page so stale pages pack while the newest are kept. */\n\tgetContextMarkers(): string[] {\n\t\treturn [\"<memory_context\"];\n\t}\n\n\tasync prefetch(query: string): Promise<string> {\n\t\tif (!query.trim()) return \"\";\n\t\tlet index: TranscriptIndex;\n\t\ttry {\n\t\t\tindex = this.ensureIndex();\n\t\t} catch {\n\t\t\treturn \"\";\n\t\t}\n\t\tif (index.size === 0) return \"\";\n\t\t// minScore is a query-CONTAINMENT threshold (fraction of the query's tokens present in the doc),\n\t\t// not Jaccard — so it is length-independent and recalls relevant long sessions. ~1/3 of query\n\t\t// terms must appear before a session is considered relevant.\n\t\tconst hits = index.query(query, { k: 3, minScore: 0.34, maxSnippetChars: 600 });\n\t\tif (hits.length === 0) return \"\";\n\t\tconst body = hits.map((h) => `- (${h.timestamp ?? \"earlier session\"}) ${h.snippet}`).join(\"\\n\");\n\t\treturn `<memory_context source=\"transcript-recall\">\\nRelevant context recalled from past sessions (read-only reference, may be stale):\\n${body}\\n</memory_context>`;\n\t}\n\n\tprivate ensureIndex(): TranscriptIndex {\n\t\tif (!this.index) {\n\t\t\tthis.index = new TranscriptIndex(this.buildDocs());\n\t\t}\n\t\treturn this.index;\n\t}\n\n\tprivate buildDocs(): TranscriptDoc[] {\n\t\tconst docs: TranscriptDoc[] = [];\n\t\tlet dir: string;\n\t\ttry {\n\t\t\tdir = getDefaultSessionDir(this.cwd, this.agentDir);\n\t\t} catch {\n\t\t\treturn docs;\n\t\t}\n\n\t\tlet files: Array<{ path: string; mtime: number }>;\n\t\ttry {\n\t\t\tfiles = readdirSync(dir)\n\t\t\t\t.filter((f) => f.endsWith(\".jsonl\"))\n\t\t\t\t.map((f) => {\n\t\t\t\t\tconst path = join(dir, f);\n\t\t\t\t\tlet mtime = 0;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tmtime = statSync(path).mtimeMs;\n\t\t\t\t\t} catch {}\n\t\t\t\t\treturn { path, mtime };\n\t\t\t\t})\n\t\t\t\t.sort((a, b) => b.mtime - a.mtime) // most-recent first\n\t\t\t\t.slice(0, MAX_SESSIONS);\n\t\t} catch {\n\t\t\treturn docs;\n\t\t}\n\n\t\tlet total = 0;\n\t\tfor (const { path } of files) {\n\t\t\tlet entries: FileEntry[];\n\t\t\ttry {\n\t\t\t\tentries = loadEntriesFromFile(path);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst header = entries.find((e): e is Extract<FileEntry, { type: \"session\" }> => e.type === \"session\");\n\t\t\tconst sessionId = header?.id;\n\t\t\tif (!sessionId || sessionId === this.currentSessionId || isAutoLearnSessionId(sessionId)) continue;\n\n\t\t\tconst text = extractSessionText(entries, MAX_DOC_CHARS);\n\t\t\tif (!text.trim()) continue;\n\t\t\tdocs.push({ sessionId, timestamp: header?.timestamp, text });\n\t\t\ttotal += text.length;\n\t\t\tif (total >= MAX_TOTAL_CHARS) break;\n\t\t}\n\t\treturn docs;\n\t}\n}\n\n/** Concatenate user+assistant text from a session's entries, capped to `maxChars`. */\nfunction extractSessionText(entries: FileEntry[], maxChars: number): string {\n\tconst parts: string[] = [];\n\tlet len = 0;\n\tfor (const entry of entries) {\n\t\tif (entry.type !== \"message\") continue;\n\t\tconst message = entry.message;\n\t\tif (message.role !== \"user\" && message.role !== \"assistant\") continue;\n\t\tconst content = message.content;\n\t\tlet text = \"\";\n\t\tif (typeof content === \"string\") {\n\t\t\ttext = content;\n\t\t} else if (Array.isArray(content)) {\n\t\t\ttext = content\n\t\t\t\t.map((b) => (b && typeof b === \"object\" && \"type\" in b && b.type === \"text\" ? (b.text ?? \"\") : \"\"))\n\t\t\t\t.join(\" \");\n\t\t}\n\t\ttext = text.trim();\n\t\tif (!text) continue;\n\t\tparts.push(text);\n\t\tlen += text.length;\n\t\tif (len >= maxChars) break;\n\t}\n\treturn parts.join(\"\\n\").slice(0, maxChars);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"transcript-recall.js","sourceRoot":"","sources":["../../../../src/core/memory/providers/transcript-recall.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AACzE,OAAO,EAEN,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,GACnB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAsB,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE7E,6CAA6C;AAC7C,MAAM,YAAY,GAAG,EAAE,CAAC;AACxB,0EAA0E;AAC1E,MAAM,aAAa,GAAG,KAAK,CAAC;AAC5B,0CAA0C;AAC1C,MAAM,eAAe,GAAG,OAAO,CAAC;AAEhC,MAAM,OAAO,wBAAwB;IAC3B,IAAI,GAAG,mBAAmB,CAAC;IAC5B,KAAK,CAA8B;IACnC,gBAAgB,GAAG,EAAE,CAAC;IACtB,GAAG,GAAG,EAAE,CAAC;IACT,QAAQ,GAAG,EAAE,CAAC;IAEtB,WAAW,GAAY;QACtB,OAAO,IAAI,CAAC;IAAA,CACZ;IAED,eAAe,GAAuB;QACrC,OAAO,EAAE,QAAQ,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC;IAAA,CACjC;IAED,KAAK,CAAC,UAAU,CAAC,SAAiB,EAAE,GAA2B,EAAiB;QAC/E,IAAI,CAAC,gBAAgB,GAAG,SAAS,CAAC;QAClC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;QAC7B,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC,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C;QACd,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YACjC,IAAI,GAAG,OAAO,CAAC;QAChB,CAAC;aAAM,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACnC,IAAI,GAAG,OAAO;iBACZ,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;iBAClG,IAAI,CAAC,GAAG,CAAC,CAAC;QACb,CAAC;QACD,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;QACnB,IAAI,CAAC,IAAI;YAAE,SAAS;QACpB,2FAA2F;QAC3F,qCAAqC;QACrC,IAAI,IAAI,CAAC,QAAQ,CAAC,4CAA4C,CAAC;YAAE,SAAS;QAC1E,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjB,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC;QACnB,IAAI,GAAG,IAAI,QAAQ;YAAE,MAAM;IAC5B,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;AAAA,CAC3C","sourcesContent":["/**\n * TranscriptRecallProvider — cross-session similarity recall (adaptive-agent design R3).\n *\n * A read-only CONTEXT memory provider: it indexes the most-recent past session transcripts (the JSONL\n * corpus) with a dependency-free token/Jaccard index ({@link TranscriptIndex}, reusing skill_audit's\n * tokenizer) and answers `prefetch(query)` with a small `<memory_context>` recall page of the most\n * relevant past snippets. The current session and auto-learn sessions are excluded. It never writes —\n * the file-store remains the write target; this is the recall corpus.\n */\n\nimport { readdirSync, statSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { wrapUntrustedText } from \"../../security/untrusted-boundary.ts\";\nimport {\n\ttype FileEntry,\n\tgetDefaultSessionDir,\n\tisAutoLearnSessionId,\n\tloadEntriesFromFile,\n} from \"../../session-manager.ts\";\nimport type { MemoryCapabilities, MemoryLifecycleContext, MemoryProvider } from \"../memory-provider.ts\";\nimport { type TranscriptDoc, TranscriptIndex } from \"../transcript-index.ts\";\n\n/** Most-recent past sessions to consider. */\nconst MAX_SESSIONS = 60;\n/** Per-session text cap (keeps the index light and snippets relevant). */\nconst MAX_DOC_CHARS = 8_000;\n/** Overall corpus cap across all docs. */\nconst MAX_TOTAL_CHARS = 500_000;\n\nexport class TranscriptRecallProvider implements MemoryProvider {\n\treadonly name = \"transcript-recall\";\n\tprivate index: TranscriptIndex | undefined;\n\tprivate currentSessionId = \"\";\n\tprivate cwd = \"\";\n\tprivate agentDir = \"\";\n\n\tisAvailable(): boolean {\n\t\treturn true;\n\t}\n\n\tgetCapabilities(): MemoryCapabilities {\n\t\treturn { surfaces: [\"context\"] };\n\t}\n\n\tasync initialize(sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.currentSessionId = sessionId;\n\t\tthis.cwd = ctx.cwd;\n\t\tthis.agentDir = ctx.agentDir;\n\t\tthis.index = undefined; // built lazily on first prefetch\n\t}\n\n\tasync shutdown(): Promise<void> {\n\t\tthis.index = undefined;\n\t}\n\n\t/** GC manages the dynamic recall page so stale pages pack while the newest are kept. */\n\tgetContextMarkers(): string[] {\n\t\treturn [\"<memory_context\"];\n\t}\n\n\tasync prefetch(query: string): Promise<string> {\n\t\tif (!query.trim()) return \"\";\n\t\tlet index: TranscriptIndex;\n\t\ttry {\n\t\t\tindex = this.ensureIndex();\n\t\t} catch {\n\t\t\treturn \"\";\n\t\t}\n\t\tif (index.size === 0) return \"\";\n\t\t// minScore is a query-CONTAINMENT threshold (fraction of the query's tokens present in the doc),\n\t\t// not Jaccard — so it is length-independent and recalls relevant long sessions. ~1/3 of query\n\t\t// terms must appear before a session is considered relevant.\n\t\tconst hits = index.query(query, { k: 3, minScore: 0.34, maxSnippetChars: 600 });\n\t\tif (hits.length === 0) return \"\";\n\t\t// Recalled past text is UNTRUSTED (it may itself contain injected instructions or a forged\n\t\t// `</memory_context>` to break out). Fence each snippet with the untrusted-content boundary so a\n\t\t// payload can't escape and be replayed as a current instruction (design: recall = untrusted).\n\t\tconst body = hits\n\t\t\t.map((h) => `- (${h.timestamp ?? \"earlier session\"}) ${wrapUntrustedText(h.snippet, \"transcript-recall\")}`)\n\t\t\t.join(\"\\n\");\n\t\treturn `<memory_context source=\"transcript-recall\">\\nRelevant context recalled from past sessions (read-only reference, untrusted, may be stale):\\n${body}\\n</memory_context>`;\n\t}\n\n\tprivate ensureIndex(): TranscriptIndex {\n\t\tif (!this.index) {\n\t\t\tthis.index = new TranscriptIndex(this.buildDocs());\n\t\t}\n\t\treturn this.index;\n\t}\n\n\tprivate buildDocs(): TranscriptDoc[] {\n\t\tconst docs: TranscriptDoc[] = [];\n\t\tlet dir: string;\n\t\ttry {\n\t\t\tdir = getDefaultSessionDir(this.cwd, this.agentDir);\n\t\t} catch {\n\t\t\treturn docs;\n\t\t}\n\n\t\tlet files: Array<{ path: string; mtime: number }>;\n\t\ttry {\n\t\t\tfiles = readdirSync(dir)\n\t\t\t\t.filter((f) => f.endsWith(\".jsonl\"))\n\t\t\t\t.map((f) => {\n\t\t\t\t\tconst path = join(dir, f);\n\t\t\t\t\tlet mtime = 0;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tmtime = statSync(path).mtimeMs;\n\t\t\t\t\t} catch {}\n\t\t\t\t\treturn { path, mtime };\n\t\t\t\t})\n\t\t\t\t.sort((a, b) => b.mtime - a.mtime) // most-recent first\n\t\t\t\t.slice(0, MAX_SESSIONS);\n\t\t} catch {\n\t\t\treturn docs;\n\t\t}\n\n\t\tlet total = 0;\n\t\tfor (const { path } of files) {\n\t\t\tlet entries: FileEntry[];\n\t\t\ttry {\n\t\t\t\tentries = loadEntriesFromFile(path);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst header = entries.find((e): e is Extract<FileEntry, { type: \"session\" }> => e.type === \"session\");\n\t\t\tconst sessionId = header?.id;\n\t\t\tif (!sessionId || sessionId === this.currentSessionId || isAutoLearnSessionId(sessionId)) continue;\n\n\t\t\tconst text = extractSessionText(entries, MAX_DOC_CHARS);\n\t\t\tif (!text.trim()) continue;\n\t\t\tdocs.push({ sessionId, timestamp: header?.timestamp, text });\n\t\t\ttotal += text.length;\n\t\t\tif (total >= MAX_TOTAL_CHARS) break;\n\t\t}\n\t\treturn docs;\n\t}\n}\n\n/** Concatenate user+assistant text from a session's entries, capped to `maxChars`. */\nfunction extractSessionText(entries: FileEntry[], maxChars: number): string {\n\tconst parts: string[] = [];\n\tlet len = 0;\n\tfor (const entry of entries) {\n\t\tif (entry.type !== \"message\") continue;\n\t\tconst message = entry.message;\n\t\tif (message.role !== \"user\" && message.role !== \"assistant\") continue;\n\t\tconst content = message.content;\n\t\tlet text = \"\";\n\t\tif (typeof content === \"string\") {\n\t\t\ttext = content;\n\t\t} else if (Array.isArray(content)) {\n\t\t\ttext = content\n\t\t\t\t.map((b) => (b && typeof b === \"object\" && \"type\" in b && b.type === \"text\" ? (b.text ?? \"\") : \"\"))\n\t\t\t\t.join(\" \");\n\t\t}\n\t\ttext = text.trim();\n\t\tif (!text) continue;\n\t\t// Skip our own previously-injected recall pages so recalled snippets don't recirculate and\n\t\t// amplify across sessions (Bug #10).\n\t\tif (text.includes('<memory_context source=\"transcript-recall\"')) continue;\n\t\tparts.push(text);\n\t\tlen += text.length;\n\t\tif (len >= maxChars) break;\n\t}\n\treturn parts.join(\"\\n\").slice(0, maxChars);\n}\n"]}
|