pi-para 0.6.7 → 0.7.0

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.
Files changed (124) hide show
  1. package/ARCHITECTURE.md +4 -6
  2. package/CHANGELOG.md +14 -0
  3. package/CONFIGURATION.md +1 -16
  4. package/README.md +2 -13
  5. package/SETUP.md +1 -7
  6. package/STRUCTURE.md +7 -18
  7. package/dist/cli-legacy.d.ts +2 -1
  8. package/dist/cli-legacy.d.ts.map +1 -1
  9. package/dist/cli-legacy.js +13 -25
  10. package/dist/cli-legacy.js.map +1 -1
  11. package/dist/cli.d.ts +2 -24
  12. package/dist/cli.d.ts.map +1 -1
  13. package/dist/cli.js +29 -271
  14. package/dist/cli.js.map +1 -1
  15. package/dist/commands.d.ts.map +1 -1
  16. package/dist/commands.js +1 -28
  17. package/dist/commands.js.map +1 -1
  18. package/dist/config.d.ts +1 -5
  19. package/dist/config.d.ts.map +1 -1
  20. package/dist/config.js +0 -5
  21. package/dist/config.js.map +1 -1
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +6 -64
  24. package/dist/index.js.map +1 -1
  25. package/dist/link-utils.d.ts +1 -1
  26. package/dist/link-utils.js +1 -1
  27. package/dist/settings.d.ts +0 -3
  28. package/dist/settings.d.ts.map +1 -1
  29. package/dist/settings.js +0 -11
  30. package/dist/settings.js.map +1 -1
  31. package/dist/tools/edit-read.d.ts +18 -0
  32. package/dist/tools/edit-read.d.ts.map +1 -0
  33. package/dist/tools/edit-read.js +161 -0
  34. package/dist/tools/edit-read.js.map +1 -0
  35. package/dist/tools/ingest.d.ts +12 -0
  36. package/dist/tools/ingest.d.ts.map +1 -0
  37. package/dist/tools/ingest.js +48 -0
  38. package/dist/tools/ingest.js.map +1 -0
  39. package/dist/tools/lint.d.ts +10 -0
  40. package/dist/tools/lint.d.ts.map +1 -0
  41. package/dist/tools/lint.js +57 -0
  42. package/dist/tools/lint.js.map +1 -0
  43. package/dist/tools/migrate.d.ts +6 -0
  44. package/dist/tools/migrate.d.ts.map +1 -0
  45. package/dist/tools/migrate.js +36 -0
  46. package/dist/tools/migrate.js.map +1 -0
  47. package/dist/tools/move.d.ts +9 -0
  48. package/dist/tools/move.d.ts.map +1 -0
  49. package/dist/tools/move.js +61 -0
  50. package/dist/tools/move.js.map +1 -0
  51. package/dist/tools/query.d.ts +12 -0
  52. package/dist/tools/query.d.ts.map +1 -0
  53. package/dist/tools/query.js +47 -0
  54. package/dist/tools/query.js.map +1 -0
  55. package/dist/tools/schemas.d.ts +109 -0
  56. package/dist/tools/schemas.d.ts.map +1 -0
  57. package/dist/tools/schemas.js +80 -0
  58. package/dist/tools/schemas.js.map +1 -0
  59. package/dist/tools/shared.d.ts +14 -0
  60. package/dist/tools/shared.d.ts.map +1 -0
  61. package/dist/tools/shared.js +25 -0
  62. package/dist/tools/shared.js.map +1 -0
  63. package/dist/tools/summarize.d.ts +8 -0
  64. package/dist/tools/summarize.d.ts.map +1 -0
  65. package/dist/tools/summarize.js +82 -0
  66. package/dist/tools/summarize.js.map +1 -0
  67. package/dist/tools/write.d.ts +23 -0
  68. package/dist/tools/write.d.ts.map +1 -0
  69. package/dist/tools/write.js +215 -0
  70. package/dist/tools/write.js.map +1 -0
  71. package/dist/tools.d.ts +1 -8
  72. package/dist/tools.d.ts.map +1 -1
  73. package/dist/tools.js +13 -795
  74. package/dist/tools.js.map +1 -1
  75. package/package.json +2 -6
  76. package/src/cli-legacy.ts +14 -26
  77. package/src/cli.ts +29 -293
  78. package/src/commands.ts +0 -21
  79. package/src/config.ts +1 -6
  80. package/src/index.ts +5 -66
  81. package/src/link-utils.ts +1 -1
  82. package/src/settings.ts +0 -14
  83. package/src/tools/edit-read.ts +193 -0
  84. package/src/tools/ingest.ts +63 -0
  85. package/src/tools/lint.ts +74 -0
  86. package/src/tools/migrate.ts +44 -0
  87. package/src/tools/move.ts +79 -0
  88. package/src/tools/query.ts +63 -0
  89. package/src/tools/schemas.ts +157 -0
  90. package/src/tools/shared.ts +35 -0
  91. package/src/tools/summarize.ts +89 -0
  92. package/src/tools/write.ts +252 -0
  93. package/src/tools.ts +18 -1046
  94. package/dist/daemon.d.ts +0 -49
  95. package/dist/daemon.d.ts.map +0 -1
  96. package/dist/daemon.js +0 -226
  97. package/dist/daemon.js.map +0 -1
  98. package/dist/link-discovery.d.ts +0 -40
  99. package/dist/link-discovery.d.ts.map +0 -1
  100. package/dist/link-discovery.js +0 -122
  101. package/dist/link-discovery.js.map +0 -1
  102. package/dist/maintainer.d.ts +0 -24
  103. package/dist/maintainer.d.ts.map +0 -1
  104. package/dist/maintainer.js +0 -358
  105. package/dist/maintainer.js.map +0 -1
  106. package/dist/watcher.d.ts +0 -25
  107. package/dist/watcher.d.ts.map +0 -1
  108. package/dist/watcher.js +0 -84
  109. package/dist/watcher.js.map +0 -1
  110. package/dist/webui/client/dist/index.html +0 -1885
  111. package/dist/webui/server.d.ts +0 -31
  112. package/dist/webui/server.d.ts.map +0 -1
  113. package/dist/webui/server.js +0 -566
  114. package/dist/webui/server.js.map +0 -1
  115. package/scripts/build-assets.mjs +0 -17
  116. package/src/daemon.ts +0 -288
  117. package/src/link-discovery.ts +0 -170
  118. package/src/maintainer.ts +0 -404
  119. package/src/python/__pycache__/minimax_lm.cpython-312.pyc +0 -0
  120. package/src/python/minimax_lm.py +0 -198
  121. package/src/python/rlm_capture.py +0 -215
  122. package/src/watcher.ts +0 -101
  123. package/src/webui/client/dist/index.html +0 -1885
  124. package/src/webui/server.ts +0 -765
@@ -0,0 +1,63 @@
1
+ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
2
+ import type { QMDStore } from "qmd-engine";
3
+ import type { ProjectScope } from "../scope.js";
4
+ import type { ParaCategory } from "../wiki.js";
5
+ import { queryWiki as queryWikiLib, formatQueryResults } from "../query.js";
6
+ import { QUERY_PROMPT } from "../templates/prompts.js";
7
+ import type { WikiQueryDetails } from "./schemas.js";
8
+
9
+ // -- Factory: wiki_query execute ---------------------------------------------
10
+
11
+ export function createQueryExecute(
12
+ _wikiDir: string,
13
+ store: QMDStore,
14
+ getScope: () => ProjectScope,
15
+ getGraphBoost?: () => boolean,
16
+ ) {
17
+ return async (
18
+ params: { query: string; global?: boolean; category?: ParaCategory; limit?: number },
19
+ ): Promise<AgentToolResult<WikiQueryDetails>> => {
20
+ const scope = getScope();
21
+ const result = await queryWikiLib(store, {
22
+ query: params.query,
23
+ global: params.global,
24
+ category: params.category,
25
+ limit: params.limit,
26
+ graphBoost: getGraphBoost?.() ?? true,
27
+ }, scope);
28
+
29
+ if (result.results.length === 0) {
30
+ return {
31
+ content: [
32
+ {
33
+ type: "text",
34
+ text: `No wiki pages found for "${params.query}". The wiki may not have content on this topic yet.`,
35
+ },
36
+ ],
37
+ details: {
38
+ resultCount: 0,
39
+ scopeUsed: result.wasGlobal ? "global" : scope.name,
40
+ },
41
+ };
42
+ }
43
+
44
+ const formatted = formatQueryResults(result.results);
45
+ const toolResultText = [
46
+ QUERY_PROMPT,
47
+ "",
48
+ `Query: "${params.query}"`,
49
+ `Scope: ${result.wasGlobal ? "global (all scopes)" : scope.name}`,
50
+ `Results: ${result.results.length}`,
51
+ "",
52
+ formatted,
53
+ ].join("\n");
54
+
55
+ return {
56
+ content: [{ type: "text", text: toolResultText }],
57
+ details: {
58
+ resultCount: result.results.length,
59
+ scopeUsed: result.wasGlobal ? "global" : scope.name,
60
+ },
61
+ };
62
+ };
63
+ }
@@ -0,0 +1,157 @@
1
+ import { StringEnum } from "@earendil-works/pi-ai";
2
+ import { Type } from "typebox";
3
+ import type { LintReport } from "../lint.js";
4
+
5
+ // -- Parameter schemas -------------------------------------------------------
6
+
7
+ export const PARA_ENUM = StringEnum(
8
+ ["projects", "areas", "resources", "archives"] as const,
9
+ { description: "PARA category" },
10
+ );
11
+
12
+ export const PARA_ENUM_NO_ARCHIVES = StringEnum(
13
+ ["projects", "areas", "resources"] as const,
14
+ { description: "PARA category (archives excluded for new content)" },
15
+ );
16
+
17
+ export const WikiIngestParams = Type.Object({
18
+ source: Type.String({ description: "URL, file path, or raw text to ingest" }),
19
+ sourceType: Type.Optional(
20
+ StringEnum(["url", "file", "text"] as const, {
21
+ description: "Source type (auto-detected if omitted)",
22
+ }),
23
+ ),
24
+ category: Type.Optional(PARA_ENUM_NO_ARCHIVES),
25
+ scope: Type.Optional(
26
+ Type.Array(Type.String(), { description: "Scope tags for the ingested content" }),
27
+ ),
28
+ });
29
+
30
+ export const WikiQueryParams = Type.Object({
31
+ query: Type.String({ description: "Natural language search query" }),
32
+ global: Type.Optional(
33
+ Type.Boolean({ description: "Search all scopes, not just current project" }),
34
+ ),
35
+ category: Type.Optional(PARA_ENUM),
36
+ limit: Type.Optional(
37
+ Type.Number({ description: "Max results (default 10)" }),
38
+ ),
39
+ });
40
+
41
+ export const WikiEditSchema = Type.Object({
42
+ oldText: Type.String({ description: "Exact text to find in the page body" }),
43
+ newText: Type.String({ description: "Replacement text" }),
44
+ });
45
+
46
+ export const WikiWritePageSchema = Type.Object({
47
+ category: PARA_ENUM,
48
+ slug: Type.String({ description: "Page slug (lowercase, hyphens)" }),
49
+ title: Type.String({ description: "Page title" }),
50
+ scope: Type.Array(Type.String(), { description: "Scope tags" }),
51
+ tags: Type.Array(Type.String(), { description: "Topic tags" }),
52
+ body: Type.String({ description: "Page body in markdown (wiki summary format)" }),
53
+ mode: StringEnum(["create", "replace", "append", "edit"] as const, {
54
+ description: "Write mode: create new, replace existing, append to existing, or edit specific sections",
55
+ }),
56
+ edits: Type.Optional(Type.Array(WikiEditSchema, {
57
+ description: "For mode=edit: targeted text replacements (oldText→newText). Page must exist.",
58
+ })),
59
+ });
60
+
61
+ export const WikiWriteParams = Type.Object({
62
+ pages: Type.Array(WikiWritePageSchema, {
63
+ description: "Pages to write or update",
64
+ }),
65
+ // DEPRECATED: indexContent is ignored. Index is auto-rebuilt from all pages on disk.
66
+ // Kept in schema to avoid breaking existing callers.
67
+ indexContent: Type.Optional(
68
+ Type.String({ description: "Updated index.md content (full replacement)" }),
69
+ ),
70
+ logSummary: Type.Optional(
71
+ Type.String({ description: "One-line summary for log.md" }),
72
+ ),
73
+ });
74
+
75
+ export const WikiEditParams = Type.Object({
76
+ path: Type.String({ description: "Existing page path (e.g. 'resources/ssl-cert-gotchas')" }),
77
+ edits: Type.Array(WikiEditSchema, {
78
+ description: "Atomic exact text replacements. Every oldText must appear exactly once or no changes are written.",
79
+ }),
80
+ title: Type.Optional(Type.String({ description: "Optional replacement page title" })),
81
+ scope: Type.Optional(Type.Array(Type.String(), { description: "Optional replacement scope tags" })),
82
+ tags: Type.Optional(Type.Array(Type.String(), { description: "Optional replacement topic tags" })),
83
+ logSummary: Type.Optional(Type.String({ description: "One-line summary for log.md" })),
84
+ });
85
+
86
+ export const WikiReadParams = Type.Object({
87
+ path: Type.String({
88
+ description: "Page path (e.g. 'projects/auth-refactor') or page title",
89
+ }),
90
+ });
91
+
92
+ export const WikiMoveParams = Type.Object({
93
+ path: Type.String({
94
+ description: "Current page path (e.g. 'projects/auth-refactor')",
95
+ }),
96
+ to: PARA_ENUM,
97
+ });
98
+
99
+ export const WikiLintParams = Type.Object({
100
+ autoFix: Type.Optional(
101
+ Type.Boolean({ description: "Auto-fix simple issues (default true)" }),
102
+ ),
103
+ });
104
+
105
+ export const WikiMigrateParams = Type.Object({});
106
+
107
+ export const WikiSummarizeParams = Type.Object({
108
+ target: Type.String({
109
+ description: "Page path, category name (e.g. 'projects'), or 'all'",
110
+ }),
111
+ depth: Type.Optional(
112
+ StringEnum(["brief", "detailed"] as const, {
113
+ description: "Summary depth (default brief)",
114
+ }),
115
+ ),
116
+ });
117
+
118
+ // -- Detail types for tool results -------------------------------------------
119
+
120
+ export interface WikiIngestDetails {
121
+ sourceType: "url" | "file" | "text";
122
+ rawPath?: string;
123
+ sourceLength: number;
124
+ }
125
+
126
+ export interface WikiQueryDetails {
127
+ resultCount: number;
128
+ scopeUsed: string;
129
+ }
130
+
131
+ export interface WikiWriteDetails {
132
+ pagesWritten: string[];
133
+ indexUpdated: boolean;
134
+ logAppended: boolean;
135
+ pagesSkipped?: string[];
136
+ }
137
+
138
+ export interface WikiReadDetails {
139
+ found: boolean;
140
+ path?: string;
141
+ }
142
+
143
+ export interface WikiMoveDetails {
144
+ from: string;
145
+ to: string;
146
+ }
147
+
148
+ export interface WikiLintDetails {
149
+ issueCount: number;
150
+ fixedCount: number;
151
+ stats: LintReport["stats"];
152
+ }
153
+
154
+ export interface WikiSummarizeDetails {
155
+ target: string;
156
+ pageCount: number;
157
+ }
@@ -0,0 +1,35 @@
1
+ import type { QMDStore } from "qmd-engine";
2
+ import { enqueueWikiMaintenance } from "../scheduler/index.js";
3
+ import { PARA_CATEGORIES, type ParaCategory } from "../wiki.js";
4
+
5
+ // -- Source resolution helpers -----------------------------------------------
6
+
7
+ /** Resolve a page path like "projects/auth-refactor" into category + slug. */
8
+ export function parsePagePath(
9
+ path: string,
10
+ ): { category: ParaCategory; slug: string } | null {
11
+ const parts = path.split("/");
12
+ if (parts.length === 2) {
13
+ const cat = parts[0] as ParaCategory;
14
+ if (PARA_CATEGORIES.includes(cat)) {
15
+ const slug = parts[1].replace(/\.md$/, "");
16
+ return { category: cat, slug };
17
+ }
18
+ }
19
+ return null;
20
+ }
21
+
22
+ // -- Background maintenance --------------------------------------------------
23
+
24
+ /**
25
+ * Schedule index rebuild + QMD reindex after latency-sensitive edits.
26
+ * Debounced per wiki directory and coordinated through the scheduler queue so
27
+ * multiple Pi processes do not run the same maintenance task concurrently.
28
+ */
29
+ export function scheduleWikiMaintenance(
30
+ wikiDir: string,
31
+ store: QMDStore,
32
+ markDirty: () => void,
33
+ ): void {
34
+ enqueueWikiMaintenance(wikiDir, store, markDirty);
35
+ }
@@ -0,0 +1,89 @@
1
+ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
2
+ import type { ProjectScope } from "../scope.js";
3
+ import { generateOverviewPrompt } from "../summarize.js";
4
+ import { listPages, readPage, PARA_CATEGORIES, type ParaCategory, type WikiPage } from "../wiki.js";
5
+ import type { WikiSummarizeDetails } from "./schemas.js";
6
+ import { parsePagePath } from "./shared.js";
7
+
8
+ // -- Factory: wiki_summarize execute -----------------------------------------
9
+
10
+ export function createSummarizeExecute(
11
+ wikiDir: string,
12
+ getScope: () => ProjectScope,
13
+ ) {
14
+ return async (
15
+ params: { target: string; depth?: "brief" | "detailed" },
16
+ ): Promise<AgentToolResult<WikiSummarizeDetails>> => {
17
+ const scope = getScope();
18
+ const pages: WikiPage[] = [];
19
+
20
+ if (params.target === "all") {
21
+ // Summarize entire wiki
22
+ const refs = await listPages(wikiDir);
23
+ for (const ref of refs) {
24
+ const page = await readPage(wikiDir, ref.category, ref.slug);
25
+ if (page) pages.push(page);
26
+ }
27
+ } else if (PARA_CATEGORIES.includes(params.target as ParaCategory)) {
28
+ // Summarize a category
29
+ const refs = await listPages(wikiDir, params.target as ParaCategory);
30
+ for (const ref of refs) {
31
+ const page = await readPage(wikiDir, ref.category, ref.slug);
32
+ if (page) pages.push(page);
33
+ }
34
+ } else {
35
+ // Summarize a single page
36
+ const parsed = parsePagePath(params.target);
37
+ if (parsed) {
38
+ const page = await readPage(wikiDir, parsed.category, parsed.slug);
39
+ if (page) pages.push(page);
40
+ } else {
41
+ // Try finding by slug alone across categories
42
+ const allPages = await listPages(wikiDir);
43
+ const targetLower = params.target.toLowerCase();
44
+ for (const ref of allPages) {
45
+ if (ref.slug === targetLower || ref.title.toLowerCase() === targetLower) {
46
+ const page = await readPage(wikiDir, ref.category, ref.slug);
47
+ if (page) pages.push(page);
48
+ break;
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ if (pages.length === 0) {
55
+ return {
56
+ content: [
57
+ {
58
+ type: "text",
59
+ text: `No pages found for target "${params.target}". Nothing to summarize.`,
60
+ },
61
+ ],
62
+ details: { target: params.target, pageCount: 0 },
63
+ };
64
+ }
65
+
66
+ // Generate a bounded overview prompt for the LLM. For large targets like
67
+ // `all`, brief mode must not dump every full page body into model context.
68
+ const depth = params.depth ?? "brief";
69
+ const overviewPrompt = generateOverviewPrompt(pages, scope, { depth });
70
+ const depthNote =
71
+ depth === "detailed"
72
+ ? "\nProvide a DETAILED summary with full Key Facts, Insights, and Connections for each theme."
73
+ : "\nProvide a BRIEF overview — one paragraph per theme, focus on high-level patterns.";
74
+
75
+ return {
76
+ content: [
77
+ {
78
+ type: "text",
79
+ text: overviewPrompt + depthNote +
80
+ "\n\nYou may optionally create a summary page via wiki_write if the overview is worth preserving.",
81
+ },
82
+ ],
83
+ details: {
84
+ target: params.target,
85
+ pageCount: pages.length,
86
+ },
87
+ };
88
+ };
89
+ }
@@ -0,0 +1,252 @@
1
+ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
2
+ import type { QMDStore } from "qmd-engine";
3
+ import type { ProjectScope } from "../scope.js";
4
+ import { normalizeTags, normalizeScopes } from "../tag-registry.js";
5
+ import { redactSecrets } from "../redact.js";
6
+ import { autoLinkSlugs, syncFrontmatterLinks } from "../link-utils.js";
7
+ import { validateFrontmatter } from "../frontmatter.js";
8
+ import { appendLog, gitCommit, listPageSlugs, readPage, rebuildIndex, writePage, type ParaCategory, type WikiPage } from "../wiki.js";
9
+ import type { WikiWriteDetails } from "./schemas.js";
10
+ import { scheduleWikiMaintenance } from "./shared.js";
11
+
12
+ // -- Factory: wiki_write execute ---------------------------------------------
13
+
14
+ export function createWriteExecute(
15
+ wikiDir: string,
16
+ store: QMDStore,
17
+ getScope: () => ProjectScope,
18
+ markDirty: () => void,
19
+ ) {
20
+ return async (
21
+ params: {
22
+ pages: Array<{
23
+ category: ParaCategory;
24
+ slug: string;
25
+ title: string;
26
+ scope: string[];
27
+ tags: string[];
28
+ body: string;
29
+ mode: "create" | "replace" | "append" | "edit";
30
+ edits?: Array<{ oldText: string; newText: string }>;
31
+ }>;
32
+ indexContent?: string;
33
+ logSummary?: string;
34
+ },
35
+ ): Promise<AgentToolResult<WikiWriteDetails>> => {
36
+ const pagesWritten: string[] = [];
37
+ const pagesSkipped: string[] = [];
38
+ const now = new Date().toISOString();
39
+ const scope = getScope();
40
+
41
+ // Gather all existing slugs for auto-linking (slug-only readdir scan;
42
+ // reading/parsing every page here made wiki_write scale poorly)
43
+ const allSlugs = await listPageSlugs(wikiDir);
44
+
45
+ for (const pageSpec of params.pages) {
46
+ // Sanitize slug
47
+ const slug = pageSpec.slug
48
+ .toLowerCase()
49
+ .replace(/[^a-z0-9-]/g, "-")
50
+ .replace(/-+/g, "-")
51
+ .replace(/^-|-$/g, "");
52
+
53
+ if (!slug) {
54
+ continue; // skip invalid slugs
55
+ }
56
+
57
+ const existing = await readPage(wikiDir, pageSpec.category, slug);
58
+ const pagePath = `${pageSpec.category}/${slug}`;
59
+
60
+ // Track this slug for auto-linking other pages in this batch
61
+ allSlugs.add(slug);
62
+
63
+ if (pageSpec.mode === "create" && existing) {
64
+ pagesSkipped.push(`${pagePath} (already exists; use mode=replace, mode=append, or wiki_edit)`);
65
+ continue;
66
+ }
67
+
68
+ if (pageSpec.mode === "edit" && !existing) {
69
+ pagesSkipped.push(`${pagePath} (missing; wiki_write mode=edit requires an existing page)`);
70
+ continue;
71
+ }
72
+
73
+ if (pageSpec.mode === "edit" && existing) {
74
+ // Legacy edit mode: keep for compatibility, but make it atomic like wiki_edit.
75
+ const edits = pageSpec.edits ?? [];
76
+ if (edits.length === 0) {
77
+ continue; // no edits to apply
78
+ }
79
+ const errors: string[] = [];
80
+ for (const edit of edits) {
81
+ const first = existing.body.indexOf(edit.oldText);
82
+ const last = existing.body.lastIndexOf(edit.oldText);
83
+ if (first === -1) {
84
+ errors.push(`oldText not found: "${edit.oldText.slice(0, 60)}..."`);
85
+ } else if (first !== last) {
86
+ errors.push(`oldText is not unique: "${edit.oldText.slice(0, 60)}..."`);
87
+ }
88
+ }
89
+ if (errors.length > 0) {
90
+ pagesSkipped.push(`${pagePath} (${errors.join("; ")})`);
91
+ continue;
92
+ }
93
+ let editedBody = existing.body;
94
+ for (const edit of edits) {
95
+ editedBody = editedBody.replace(edit.oldText, edit.newText);
96
+ }
97
+ const linkedBody = redactSecrets(autoLinkSlugs(editedBody, allSlugs, slug)).text;
98
+ const rawScope = pageSpec.scope.length > 0 ? pageSpec.scope : existing.frontmatter.scope;
99
+ const newScope = normalizeScopes(rawScope);
100
+ const newTags = pageSpec.tags.length > 0 ? pageSpec.tags : existing.frontmatter.tags;
101
+ const updatedPage: WikiPage = {
102
+ category: pageSpec.category,
103
+ slug,
104
+ frontmatter: {
105
+ ...existing.frontmatter,
106
+ title: pageSpec.title || existing.frontmatter.title,
107
+ scope: newScope,
108
+ tags: normalizeTags(newTags, newScope),
109
+ updated: now,
110
+ links: syncFrontmatterLinks(linkedBody),
111
+ },
112
+ body: linkedBody,
113
+ };
114
+ await writePage(wikiDir, updatedPage);
115
+ pagesWritten.push(pagePath + (errors.length > 0 ? ` (${errors.length} edit(s) failed)` : ""));
116
+ } else if (pageSpec.mode === "append" && existing) {
117
+ // Append to existing page body
118
+ const combinedBody = existing.body.trimEnd() + "\n\n" + pageSpec.body;
119
+ const linkedBody = redactSecrets(autoLinkSlugs(combinedBody, allSlugs, slug)).text;
120
+ const mergedTags = [...new Set([...existing.frontmatter.tags, ...pageSpec.tags])];
121
+ const mergedScope = normalizeScopes([...new Set([...existing.frontmatter.scope, ...pageSpec.scope])]);
122
+ const updatedPage: WikiPage = {
123
+ ...existing,
124
+ body: linkedBody,
125
+ frontmatter: {
126
+ ...existing.frontmatter,
127
+ updated: now,
128
+ tags: normalizeTags(mergedTags, mergedScope),
129
+ scope: mergedScope,
130
+ links: syncFrontmatterLinks(linkedBody),
131
+ },
132
+ };
133
+ await writePage(wikiDir, updatedPage);
134
+ pagesWritten.push(pagePath);
135
+ } else if (pageSpec.mode === "replace" && existing) {
136
+ // Replace existing page body, update frontmatter
137
+ const linkedBody = redactSecrets(autoLinkSlugs(pageSpec.body, allSlugs, slug)).text;
138
+ const rawScope = pageSpec.scope.length > 0 ? pageSpec.scope : existing.frontmatter.scope;
139
+ const newScope = normalizeScopes(rawScope);
140
+ const newTags = pageSpec.tags.length > 0 ? pageSpec.tags : existing.frontmatter.tags;
141
+ const updatedPage: WikiPage = {
142
+ category: pageSpec.category,
143
+ slug,
144
+ frontmatter: {
145
+ ...existing.frontmatter,
146
+ title: pageSpec.title,
147
+ para: pageSpec.category,
148
+ scope: newScope,
149
+ tags: normalizeTags(newTags, newScope),
150
+ updated: now,
151
+ links: syncFrontmatterLinks(linkedBody),
152
+ },
153
+ body: linkedBody,
154
+ };
155
+ await writePage(wikiDir, updatedPage);
156
+ pagesWritten.push(pagePath);
157
+ } else {
158
+ // Create new page (or mode=create/replace on non-existing)
159
+ const linkedBody = redactSecrets(autoLinkSlugs(pageSpec.body, allSlugs, slug)).text;
160
+ const rawScope = pageSpec.scope.length > 0 ? pageSpec.scope : [scope.name];
161
+ const newScope = normalizeScopes(rawScope);
162
+ const frontmatter = validateFrontmatter({
163
+ title: pageSpec.title,
164
+ para: pageSpec.category,
165
+ scope: newScope,
166
+ tags: normalizeTags(pageSpec.tags, newScope),
167
+ sources: [],
168
+ created: now,
169
+ updated: now,
170
+ links: syncFrontmatterLinks(linkedBody),
171
+ });
172
+
173
+ const newPage: WikiPage = {
174
+ category: pageSpec.category,
175
+ slug,
176
+ frontmatter,
177
+ body: linkedBody,
178
+ };
179
+ await writePage(wikiDir, newPage);
180
+ pagesWritten.push(pagePath);
181
+ }
182
+ }
183
+
184
+ // Always auto-rebuild index from disk.
185
+ // The LLM's indexContent is unreliable — it only knows about pages
186
+ // in its context, so it produces partial indexes that overwrite
187
+ // the full one. indexContent parameter is deprecated and ignored.
188
+ let indexUpdated = false;
189
+ if (pagesWritten.length > 0) {
190
+ await rebuildIndex(wikiDir);
191
+ indexUpdated = true;
192
+ }
193
+
194
+ // Append to log.md if summary provided
195
+ let logAppended = false;
196
+ if (params.logSummary && pagesWritten.length > 0) {
197
+ await appendLog(wikiDir, {
198
+ date: now.split("T")[0],
199
+ operation: "ingest",
200
+ summary: params.logSummary,
201
+ pages: pagesWritten,
202
+ });
203
+ logAppended = true;
204
+ }
205
+
206
+ // Defer QMD BM25 reindex to the debounced background maintenance queue
207
+ // (same as wiki_edit). Immediate post-write queries are safe: queryWiki
208
+ // retries with a fresh store.update() before returning zero results.
209
+ if (pagesWritten.length > 0) {
210
+ scheduleWikiMaintenance(wikiDir, store, markDirty);
211
+ }
212
+
213
+ // Mark context cache dirty so before_agent_start rebuilds
214
+ markDirty();
215
+
216
+ // Git auto-commit
217
+ if (pagesWritten.length > 0) {
218
+ const commitMsg = params.logSummary
219
+ ? params.logSummary
220
+ : `wiki: ${pagesWritten.join(", ")}`;
221
+ await gitCommit(wikiDir, commitMsg);
222
+ }
223
+
224
+ const summary = [
225
+ pagesWritten.length > 0
226
+ ? `Wrote ${pagesWritten.length} page(s): ${pagesWritten.join(", ")}`
227
+ : "No pages written.",
228
+ pagesSkipped.length > 0 ? `Skipped ${pagesSkipped.length} page(s): ${pagesSkipped.join(", ")}` : "",
229
+ ].filter(Boolean).join("\n");
230
+
231
+ return {
232
+ content: [
233
+ {
234
+ type: "text",
235
+ text: [
236
+ summary,
237
+ indexUpdated ? "Updated index.md." : "",
238
+ logAppended ? `Logged: ${params.logSummary}` : "",
239
+ ]
240
+ .filter(Boolean)
241
+ .join("\n"),
242
+ },
243
+ ],
244
+ details: {
245
+ pagesWritten,
246
+ indexUpdated,
247
+ logAppended,
248
+ pagesSkipped,
249
+ },
250
+ };
251
+ };
252
+ }