pi-knowledge 0.3.1 → 0.3.4

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 CHANGED
@@ -1,5 +1,28 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.4] - 2026-06-15
4
+
5
+ ### Fixed
6
+ - Reject duplicate `knowledge_add` names with an actionable message instead of silently creating multiple same-name knowledge bases.
7
+ - Strengthened default directory indexing ignores for build outputs and common secret/config files.
8
+
9
+ ### Changed
10
+ - Updated `knowledge_add` tool guidance to prefer one directory-level indexing call and avoid per-file indexing loops.
11
+
12
+ ## [0.3.3] - 2026-06-15
13
+
14
+ ### Changed
15
+ - Added a mandatory async lifecycle review contract for timers, event handlers, dispose, and shutdown paths.
16
+ - Clarified review requirements for overlap analysis, guard-state updates before await points, idempotent cleanup, and recreate-after-dispose behavior.
17
+
18
+ ## [0.3.2] - 2026-06-15
19
+
20
+ ### Fixed
21
+ - Made local embedding and deep reranker model disposal idempotent to avoid concurrent native ONNX session teardown from idle timers and Pi `session_shutdown`.
22
+
23
+ ### Changed
24
+ - Updated the onnxruntime exit-crash pitfall notes with the double-dispose race mitigation.
25
+
3
26
  ## [0.3.1] - 2026-06-15
4
27
 
5
28
  ### Fixed
package/index.ts CHANGED
@@ -144,7 +144,10 @@ export default function (pi: ExtensionAPI) {
144
144
  promptSnippet: "Index files/dirs/text into a searchable knowledge base",
145
145
  promptGuidelines: [
146
146
  "Use knowledge_add when the user asks to index, remember, or learn from files or documentation",
147
- "Provide a descriptive name for the knowledge base",
147
+ "Prefer one knowledge_add call for the project root or relevant directory; do not call it once per file",
148
+ "If a knowledge base with the same name already exists, use knowledge_update or ask before replacing it",
149
+ "Do not index secrets or environment-specific config files unless the user explicitly asks for that exact file",
150
+ "Provide a descriptive name for this single knowledge base",
148
151
  ],
149
152
  parameters: Type.Object({
150
153
  source: Type.String({ description: "File path, directory path, or inline text to index" }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-knowledge",
3
- "version": "0.3.1",
3
+ "version": "0.3.4",
4
4
  "description": "Vector knowledge base extension for Pi — index files, hybrid search, local embeddings",
5
5
  "type": "module",
6
6
  "main": "index.ts",
@@ -8,6 +8,7 @@ type FeatureExtractionPipeline = {
8
8
 
9
9
  let pipelineInstance: FeatureExtractionPipeline | null = null;
10
10
  let disposeTimer: ReturnType<typeof setTimeout> | null = null;
11
+ let disposePromise: Promise<void> | null = null;
11
12
 
12
13
  const IDLE_TIMEOUT_MS = 30_000;
13
14
  const EMBEDDING_CONFIG = process.env.PI_KNOWLEDGE_EMBEDDING ?? "local:multilingual-e5-small";
@@ -18,6 +19,7 @@ function getModelCacheDir(): string {
18
19
 
19
20
  async function loadPipeline(): Promise<FeatureExtractionPipeline> {
20
21
  if (pipelineInstance) return pipelineInstance;
22
+ if (disposePromise) await disposePromise;
21
23
  const { pipeline, env } = await import("@huggingface/transformers");
22
24
  env.cacheDir = getModelCacheDir();
23
25
  pipelineInstance = await pipeline("feature-extraction", "Xenova/multilingual-e5-small", {
@@ -37,10 +39,14 @@ export async function dispose(): Promise<void> {
37
39
  clearTimeout(disposeTimer);
38
40
  disposeTimer = null;
39
41
  }
40
- if (pipelineInstance) {
41
- await pipelineInstance.dispose();
42
- pipelineInstance = null;
43
- }
42
+ if (disposePromise) return disposePromise;
43
+ const instance = pipelineInstance;
44
+ pipelineInstance = null;
45
+ if (!instance) return;
46
+ disposePromise = Promise.resolve(instance.dispose()).finally(() => {
47
+ disposePromise = null;
48
+ });
49
+ return disposePromise;
44
50
  }
45
51
 
46
52
  async function embedViaAPI(texts: string[], prefix: "query" | "passage"): Promise<Float32Array[]> {
package/src/engine.ts CHANGED
@@ -122,6 +122,13 @@ export class KnowledgeEngine {
122
122
  const isFile = !isUrl && existsSync(resolvedSource) && statSync(resolvedSource).isFile();
123
123
  const sourceType = isUrl ? "url" : isDir ? "directory" : isFile ? "file" : "text";
124
124
 
125
+ const existingKB = getKBByName(this.db, name);
126
+ if (existingKB) {
127
+ throw new Error(
128
+ `Knowledge base "${name}" already exists. Use knowledge_update to refresh it, or knowledge_remove before adding a replacement.`,
129
+ );
130
+ }
131
+
125
132
  const kb = createKB(this.db, {
126
133
  name,
127
134
  source_path: isDir || isFile ? resolvedSource : isUrl ? source : undefined,
@@ -59,8 +59,29 @@ const DEFAULT_IGNORE = [
59
59
  ".git",
60
60
  "dist",
61
61
  "build",
62
+ "bin",
63
+ "obj",
64
+ "out",
65
+ "target",
66
+ "coverage",
62
67
  ".next",
68
+ ".cache",
69
+ ".playwright",
63
70
  "__pycache__",
71
+ ".env",
72
+ ".env.*",
73
+ "*.pem",
74
+ "*.key",
75
+ "*.p12",
76
+ "*.pfx",
77
+ "*.crt",
78
+ "*.cert",
79
+ "*secret*",
80
+ "*secrets*",
81
+ "*credential*",
82
+ "*credentials*",
83
+ "setting*.json",
84
+ "appsettings*.json",
64
85
  "*.min.js",
65
86
  "*.min.css",
66
87
  "*.map",
@@ -8,10 +8,12 @@ type RerankerPipeline = {
8
8
 
9
9
  let rerankerPipeline: RerankerPipeline | null = null;
10
10
  let disposeTimer: ReturnType<typeof setTimeout> | null = null;
11
+ let disposePromise: Promise<void> | null = null;
11
12
  const IDLE_MS = 30_000;
12
13
 
13
14
  async function load(): Promise<RerankerPipeline> {
14
15
  if (rerankerPipeline) return rerankerPipeline;
16
+ if (disposePromise) await disposePromise;
15
17
  const { pipeline, env } = await import("@huggingface/transformers");
16
18
  env.cacheDir = join(getDefaultKnowledgeDir(), "models");
17
19
  rerankerPipeline = await pipeline("text-classification", "Xenova/ms-marco-MiniLM-L-4-v2");
@@ -28,10 +30,14 @@ export async function disposeReranker(): Promise<void> {
28
30
  clearTimeout(disposeTimer);
29
31
  disposeTimer = null;
30
32
  }
31
- if (rerankerPipeline) {
32
- await rerankerPipeline.dispose();
33
- rerankerPipeline = null;
34
- }
33
+ if (disposePromise) return disposePromise;
34
+ const instance = rerankerPipeline;
35
+ rerankerPipeline = null;
36
+ if (!instance) return;
37
+ disposePromise = Promise.resolve(instance.dispose()).finally(() => {
38
+ disposePromise = null;
39
+ });
40
+ return disposePromise;
35
41
  }
36
42
 
37
43
  export interface RerankCandidate {