somora 2026.930.1
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/LICENSE +21 -0
- package/README.md +364 -0
- package/bin/node-version.d.mts +3 -0
- package/bin/node-version.mjs +45 -0
- package/bin/somora.mjs +77 -0
- package/config.example.yaml +896 -0
- package/docs/agents.md +677 -0
- package/docs/api.md +3194 -0
- package/docs/browser.md +317 -0
- package/docs/builder.md +331 -0
- package/docs/cache-strategy.md +373 -0
- package/docs/compaction.md +253 -0
- package/docs/display.md +154 -0
- package/docs/dream-phases.md +891 -0
- package/docs/files.md +387 -0
- package/docs/imagegen.md +366 -0
- package/docs/images/somora-hero.png +0 -0
- package/docs/images/somora-mobile-2026-05-23.png +0 -0
- package/docs/images/somora-tui-2026-05-23.png +0 -0
- package/docs/images/somora-web-2026-05-23.png +0 -0
- package/docs/index.md +178 -0
- package/docs/lsp.md +93 -0
- package/docs/mcp.md +332 -0
- package/docs/memory.md +407 -0
- package/docs/mobile.md +310 -0
- package/docs/models.md +379 -0
- package/docs/projects.md +425 -0
- package/docs/realtime-voice.md +293 -0
- package/docs/resources.md +230 -0
- package/docs/sampling.md +128 -0
- package/docs/security.md +189 -0
- package/docs/sentinel.md +295 -0
- package/docs/setup.md +1217 -0
- package/docs/skills.md +501 -0
- package/docs/team.md +189 -0
- package/docs/thinking.md +463 -0
- package/docs/tmux.md +399 -0
- package/docs/tools.md +117 -0
- package/docs/videogen.md +191 -0
- package/docs/voice.md +288 -0
- package/docs/web.md +893 -0
- package/docs/wiki.md +583 -0
- package/npm-shrinkwrap.json +5670 -0
- package/package.json +116 -0
- package/src/attachments/store.ts +196 -0
- package/src/browser/change-stream.ts +31 -0
- package/src/browser/display.ts +108 -0
- package/src/browser/policy.ts +121 -0
- package/src/browser/screencast.ts +374 -0
- package/src/browser/service.ts +1337 -0
- package/src/browser/session.ts +15 -0
- package/src/browser/snapshot.ts +40 -0
- package/src/cli/auth.ts +117 -0
- package/src/cli/codex.ts +41 -0
- package/src/cli/lsp.ts +71 -0
- package/src/cli/resolvers/clawhub.ts +400 -0
- package/src/cli/setup.ts +835 -0
- package/src/cli/skill.ts +619 -0
- package/src/cli/somora.ts +548 -0
- package/src/cli/systemd-unit.ts +72 -0
- package/src/cli/team.ts +148 -0
- package/src/cli/tui/activity-stream.ts +121 -0
- package/src/cli/tui/api.ts +479 -0
- package/src/cli/tui/app.tsx +1169 -0
- package/src/cli/tui/autocomplete.tsx +55 -0
- package/src/cli/tui/commands.ts +913 -0
- package/src/cli/tui/dream-glyphs.ts +22 -0
- package/src/cli/tui/footer.tsx +15 -0
- package/src/cli/tui/format.ts +28 -0
- package/src/cli/tui/header.tsx +279 -0
- package/src/cli/tui/index.tsx +120 -0
- package/src/cli/tui/markdown.tsx +63 -0
- package/src/cli/tui/sampling.ts +162 -0
- package/src/cli/tui/separator.tsx +13 -0
- package/src/cli/tui/state.ts +42 -0
- package/src/cli/tui/stream.ts +330 -0
- package/src/cli/tui/thinking-text.ts +54 -0
- package/src/cli/tui/turn-views.tsx +499 -0
- package/src/cli/tui/types.ts +410 -0
- package/src/cli/tui/work-queue.ts +161 -0
- package/src/cli/update-args.ts +55 -0
- package/src/cli/wiki.ts +406 -0
- package/src/compaction/index.ts +28 -0
- package/src/compaction/midturn.ts +210 -0
- package/src/compaction/policy.ts +154 -0
- package/src/compaction/summarize.ts +691 -0
- package/src/compaction/template.ts +90 -0
- package/src/compaction/types.ts +148 -0
- package/src/config/loader.ts +276 -0
- package/src/config/reload.ts +43 -0
- package/src/config/types.ts +2292 -0
- package/src/dream/deep-actions.ts +324 -0
- package/src/dream/deep-dispatcher.ts +237 -0
- package/src/dream/deep-llm.ts +388 -0
- package/src/dream/deep-prompts.ts +106 -0
- package/src/dream/deep-runner.ts +757 -0
- package/src/dream/deep-skip-cache.ts +199 -0
- package/src/dream/deep-worker.ts +229 -0
- package/src/dream/json-salvage.ts +34 -0
- package/src/dream/judge.ts +188 -0
- package/src/dream/loop-state.ts +286 -0
- package/src/dream/lucid-actions.ts +303 -0
- package/src/dream/lucid-batches.ts +94 -0
- package/src/dream/lucid-prompt.ts +82 -0
- package/src/dream/lucid-runner.ts +744 -0
- package/src/dream/lucid-storage.ts +277 -0
- package/src/dream/lucid-structure.ts +102 -0
- package/src/dream/lucid-types.ts +137 -0
- package/src/dream/lucid-worker.ts +224 -0
- package/src/dream/rem-dedup.ts +502 -0
- package/src/dream/rem-extract.ts +782 -0
- package/src/dream/rem-reset-run.ts +67 -0
- package/src/dream/rem-runner.ts +696 -0
- package/src/dream/rem-worker.ts +527 -0
- package/src/dream/review-window.ts +71 -0
- package/src/dream/scheduler-state.ts +129 -0
- package/src/dream/storage.ts +497 -0
- package/src/dream/types.ts +133 -0
- package/src/dream/wiki-context.ts +133 -0
- package/src/engine/a2a.ts +33 -0
- package/src/engine/availability.ts +63 -0
- package/src/engine/claude-cli.ts +969 -0
- package/src/engine/codex-app-server-client.ts +294 -0
- package/src/engine/codex-bin.ts +49 -0
- package/src/engine/codex-cli.ts +1014 -0
- package/src/engine/codex-dynamic-tools.ts +239 -0
- package/src/engine/codex-home.ts +63 -0
- package/src/engine/codex-thread-config.ts +74 -0
- package/src/engine/context-budget.ts +257 -0
- package/src/engine/engine-meta-labels.ts +211 -0
- package/src/engine/grok-cli.ts +941 -0
- package/src/engine/inline-think.ts +59 -0
- package/src/engine/model-availability.ts +105 -0
- package/src/engine/openai-compatible.ts +2464 -0
- package/src/engine/reasoning-retry.ts +83 -0
- package/src/engine/registry.ts +13 -0
- package/src/engine/replay.ts +195 -0
- package/src/engine/sampling.ts +90 -0
- package/src/engine/thinking-params.ts +264 -0
- package/src/engine/types.ts +269 -0
- package/src/engine/user-tag.ts +24 -0
- package/src/imagegen/aspect.ts +128 -0
- package/src/imagegen/generate.ts +642 -0
- package/src/imagegen/references.ts +75 -0
- package/src/imagegen/types.ts +154 -0
- package/src/lsp/client.ts +183 -0
- package/src/lsp/fake-server.helper.mjs +42 -0
- package/src/lsp/format.ts +29 -0
- package/src/lsp/index.ts +21 -0
- package/src/lsp/manager.ts +238 -0
- package/src/lsp/registry.ts +151 -0
- package/src/mcp/config.ts +117 -0
- package/src/mcp/hub/audit.ts +55 -0
- package/src/mcp/hub/bridge.ts +115 -0
- package/src/mcp/hub/catalog.ts +69 -0
- package/src/mcp/hub/credentials.ts +406 -0
- package/src/mcp/hub/env-expand.ts +14 -0
- package/src/mcp/hub/manager.ts +770 -0
- package/src/mcp/hub/normalize.ts +213 -0
- package/src/mcp/server.ts +368 -0
- package/src/mcp/tool-schema.ts +20 -0
- package/src/media/capabilities.ts +392 -0
- package/src/media/records.ts +175 -0
- package/src/media/store.ts +217 -0
- package/src/media/types.ts +69 -0
- package/src/memory/chunking.ts +168 -0
- package/src/memory/embeddings.ts +262 -0
- package/src/memory/index.ts +15 -0
- package/src/memory/inject.ts +197 -0
- package/src/memory/manager.ts +996 -0
- package/src/memory/registry.ts +176 -0
- package/src/memory/rescan.ts +101 -0
- package/src/memory/retrieval.ts +586 -0
- package/src/memory/shared-index.ts +372 -0
- package/src/memory/slug.ts +39 -0
- package/src/memory/storage.ts +385 -0
- package/src/memory/watcher.ts +134 -0
- package/src/multimodal/blocks.ts +101 -0
- package/src/multimodal/dimensions.ts +214 -0
- package/src/multimodal/load.ts +86 -0
- package/src/multimodal/mime.ts +135 -0
- package/src/multimodal/pdf-render.ts +80 -0
- package/src/multimodal/user-content.ts +266 -0
- package/src/persona/files.ts +144 -0
- package/src/persona/loader.ts +598 -0
- package/src/persona/skill-gating-store.ts +59 -0
- package/src/persona/tool-gating-store.ts +113 -0
- package/src/projects/focus.ts +186 -0
- package/src/projects/prompt-block.ts +106 -0
- package/src/projects/scheme.ts +107 -0
- package/src/projects/store.ts +144 -0
- package/src/projects/types.ts +73 -0
- package/src/sentinel/dispatcher.ts +97 -0
- package/src/sentinel/schedule.ts +271 -0
- package/src/sentinel/scheduler.ts +840 -0
- package/src/sentinel/store.ts +259 -0
- package/src/sentinel/types.ts +221 -0
- package/src/server/activity-stream.ts +160 -0
- package/src/server/ask-calls.ts +137 -0
- package/src/server/ask-wait-graph.ts +171 -0
- package/src/server/async-tasks.ts +199 -0
- package/src/server/builder-busy.ts +86 -0
- package/src/server/builder-prompt.ts +204 -0
- package/src/server/builder-questions.ts +117 -0
- package/src/server/builder-session.ts +249 -0
- package/src/server/chat-aborts.ts +83 -0
- package/src/server/claude-config-dir.ts +71 -0
- package/src/server/claude-credential-sync.ts +454 -0
- package/src/server/env-file.ts +100 -0
- package/src/server/env.ts +115 -0
- package/src/server/heal-session.ts +151 -0
- package/src/server/history-page.ts +46 -0
- package/src/server/index.ts +7244 -0
- package/src/server/lockfile.ts +89 -0
- package/src/server/logger.ts +67 -0
- package/src/server/logs.ts +187 -0
- package/src/server/loopback-fetch.ts +130 -0
- package/src/server/openai-client.ts +89 -0
- package/src/server/prompt-assembly.ts +334 -0
- package/src/server/restart-reconcile.ts +139 -0
- package/src/server/run-turn-fallback.ts +275 -0
- package/src/server/run-turn-types.ts +91 -0
- package/src/server/run-turn.ts +1474 -0
- package/src/server/sanitize-assistant-text.ts +112 -0
- package/src/server/session-export.ts +220 -0
- package/src/server/session-queue.ts +376 -0
- package/src/server/session-workdir.ts +49 -0
- package/src/server/sse-h2-safe.ts +61 -0
- package/src/server/sse-liveness.ts +162 -0
- package/src/server/sse-serializer.ts +161 -0
- package/src/server/start-turn.ts +249 -0
- package/src/server/steer-inbox.ts +107 -0
- package/src/server/tls-keeper.ts +153 -0
- package/src/server/tool-format.ts +167 -0
- package/src/server/turn-framing.ts +52 -0
- package/src/server/turn-origin-kind.ts +74 -0
- package/src/server/turn-origin.ts +40 -0
- package/src/server/work-ledger.ts +975 -0
- package/src/server/workspace.ts +114 -0
- package/src/setup/config-edit.ts +201 -0
- package/src/setup/presets.ts +71 -0
- package/src/setup/prompt.ts +163 -0
- package/src/setup/tailscale.ts +44 -0
- package/src/skills/bin-checks.ts +227 -0
- package/src/skills/bootstrap.ts +347 -0
- package/src/skills/env-scope.ts +175 -0
- package/src/skills/gating.ts +71 -0
- package/src/skills/lint.ts +202 -0
- package/src/skills/load.ts +368 -0
- package/src/skills/path-helpers.ts +102 -0
- package/src/skills/registry.ts +178 -0
- package/src/ssh/exec.ts +142 -0
- package/src/ssh/index.ts +12 -0
- package/src/ssh/known-hosts.ts +109 -0
- package/src/ssh/pool.ts +307 -0
- package/src/storage/sessions.ts +602 -0
- package/src/team/render.ts +149 -0
- package/src/team/resolve.ts +122 -0
- package/src/team/store.ts +124 -0
- package/src/team/types.ts +111 -0
- package/src/team/write.ts +95 -0
- package/src/testing/isolated-home.mts +46 -0
- package/src/tmux/attention-watcher.ts +272 -0
- package/src/tmux/attention.ts +336 -0
- package/src/tmux/list.ts +91 -0
- package/src/tmux/origin-store.ts +102 -0
- package/src/tmux/tui-markers.ts +267 -0
- package/src/tools/agents/ask-cancel.ts +94 -0
- package/src/tools/agents/ask-result.ts +235 -0
- package/src/tools/agents/ask.ts +704 -0
- package/src/tools/agents/builder-dispatch.ts +217 -0
- package/src/tools/agents/images.ts +104 -0
- package/src/tools/agents/index.ts +42 -0
- package/src/tools/agents/long-task-timeouts.ts +39 -0
- package/src/tools/agents/session-list.ts +136 -0
- package/src/tools/agents/session-model.ts +106 -0
- package/src/tools/agents/spawn.ts +845 -0
- package/src/tools/agents/status.ts +589 -0
- package/src/tools/browser/index.ts +6 -0
- package/src/tools/browser/ops.ts +145 -0
- package/src/tools/browser/tools.ts +184 -0
- package/src/tools/builder/short-descriptions.ts +80 -0
- package/src/tools/builder/tools.ts +222 -0
- package/src/tools/docs/index.ts +1 -0
- package/src/tools/docs/tools.ts +228 -0
- package/src/tools/dream/index.ts +11 -0
- package/src/tools/dream/tools.ts +1002 -0
- package/src/tools/exec/allowlist.ts +233 -0
- package/src/tools/exec/background-detach.helper.mts +27 -0
- package/src/tools/exec/blacklist.ts +336 -0
- package/src/tools/exec/concurrency.ts +150 -0
- package/src/tools/exec/fit-output.ts +82 -0
- package/src/tools/exec/index.ts +7 -0
- package/src/tools/exec/internal-env.ts +89 -0
- package/src/tools/exec/job-store.ts +403 -0
- package/src/tools/exec/local.ts +649 -0
- package/src/tools/exec/remote.ts +399 -0
- package/src/tools/exec/tools.ts +934 -0
- package/src/tools/file/analyze.ts +378 -0
- package/src/tools/file/index.ts +2 -0
- package/src/tools/file/local.ts +707 -0
- package/src/tools/file/lsp-hook.ts +72 -0
- package/src/tools/file/persona-guard.ts +61 -0
- package/src/tools/file/policy.ts +292 -0
- package/src/tools/file/read-format.ts +129 -0
- package/src/tools/file/remote.ts +680 -0
- package/src/tools/file/replace.ts +507 -0
- package/src/tools/file/search-window.ts +224 -0
- package/src/tools/file/tools.ts +658 -0
- package/src/tools/file/write-scope.ts +182 -0
- package/src/tools/gating.ts +106 -0
- package/src/tools/image/budget.ts +82 -0
- package/src/tools/image/index.ts +7 -0
- package/src/tools/image/tools.ts +601 -0
- package/src/tools/index.ts +99 -0
- package/src/tools/memory/index.ts +12 -0
- package/src/tools/memory/tools.ts +399 -0
- package/src/tools/projects/index.ts +1 -0
- package/src/tools/projects/tools.ts +643 -0
- package/src/tools/registry.ts +427 -0
- package/src/tools/resources/index.ts +8 -0
- package/src/tools/resources/tools.ts +176 -0
- package/src/tools/resources/visibility.ts +60 -0
- package/src/tools/sentinel/index.ts +1 -0
- package/src/tools/sentinel/tools.ts +592 -0
- package/src/tools/skill/index.ts +4 -0
- package/src/tools/skill/tools.ts +285 -0
- package/src/tools/time/index.ts +5 -0
- package/src/tools/time/tools.ts +123 -0
- package/src/tools/tmux/index.ts +1 -0
- package/src/tools/tmux/local.ts +277 -0
- package/src/tools/tmux/remote.ts +168 -0
- package/src/tools/tmux/tools.ts +1064 -0
- package/src/tools/tool-output.ts +97 -0
- package/src/tools/types.ts +227 -0
- package/src/tools/video/index.ts +1 -0
- package/src/tools/video/tools.ts +425 -0
- package/src/tools/web/fetch.ts +337 -0
- package/src/tools/web/index.ts +4 -0
- package/src/tools/web/tools.ts +186 -0
- package/src/tools/wiki/index.ts +1 -0
- package/src/tools/wiki/tools.ts +445 -0
- package/src/tts/cache-gc.ts +128 -0
- package/src/tts/prepare-for-tts.ts +114 -0
- package/src/tts/service.ts +332 -0
- package/src/types/events.ts +695 -0
- package/src/types/turn-origin.ts +53 -0
- package/src/version.ts +21 -0
- package/src/videogen/dialects.ts +222 -0
- package/src/videogen/generate.ts +484 -0
- package/src/videogen/jobs.ts +178 -0
- package/src/videogen/runner.ts +125 -0
- package/src/videogen/wake.ts +119 -0
- package/src/voice/realtime/call.ts +948 -0
- package/src/voice/realtime/consult.ts +293 -0
- package/src/voice/realtime/fake-provider.ts +134 -0
- package/src/voice/realtime/manager.ts +384 -0
- package/src/voice/realtime/openai-provider.ts +471 -0
- package/src/voice/realtime/persona.ts +227 -0
- package/src/voice/realtime/session-match.ts +116 -0
- package/src/voice/realtime/types.ts +182 -0
- package/src/wiki/conflict.ts +113 -0
- package/src/wiki/explorer.ts +578 -0
- package/src/wiki/index-builder.ts +172 -0
- package/src/wiki/language.ts +134 -0
- package/src/wiki/log-builder.ts +150 -0
- package/src/wiki/map.ts +258 -0
- package/src/wiki/migration/analyze.ts +304 -0
- package/src/wiki/migration/execute.ts +689 -0
- package/src/wiki/migration/refine.ts +402 -0
- package/src/wiki/migration/store.ts +100 -0
- package/src/wiki/move-page.ts +57 -0
- package/src/wiki/structure-file.ts +192 -0
- package/src/wiki/taxonomy.ts +140 -0
- package/src/wiki/templates.ts +163 -0
- package/src/wiki/types.ts +133 -0
- package/templates/skills/README.md +24 -0
- package/templates/skills/_templates/api-wrapper/SKILL.md +26 -0
- package/templates/skills/_templates/cli-wrapper/BOOTSTRAP.md +32 -0
- package/templates/skills/_templates/cli-wrapper/SKILL.md +24 -0
- package/templates/skills/_templates/default/SKILL.md +25 -0
- package/templates/skills/builder-handover/SKILL.md +96 -0
- package/templates/skills/builder-handover/assets/ORDER.md +12 -0
- package/templates/skills/skill-author/SKILL.md +110 -0
- package/tsconfig.json +22 -0
- package/web/dist/assets/index-BaVv6pB2.js +631 -0
- package/web/dist/assets/index-BaVv6pB2.js.map +1 -0
- package/web/dist/assets/index-V3FV3Z25.css +32 -0
- package/web/dist/favicon.svg +8 -0
- package/web/dist/index.html +20 -0
- package/web/package.json +36 -0
- package/web/tsconfig.json +25 -0
- package/web-mobile/dist/apple-touch-icon.png +0 -0
- package/web-mobile/dist/assets/index-BqKt7IMx.css +1 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js +88 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js.map +1 -0
- package/web-mobile/dist/favicon.svg +8 -0
- package/web-mobile/dist/icon-192.png +0 -0
- package/web-mobile/dist/icon-512.png +0 -0
- package/web-mobile/dist/index.html +27 -0
- package/web-mobile/dist/manifest.webmanifest +32 -0
- package/web-mobile/dist/service-worker.js +90 -0
- package/web-mobile/package.json +28 -0
- package/web-mobile/tsconfig.json +25 -0
package/src/wiki/map.ts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// The wiki map — what Deep sees instead of the first 4 KB of index.md.
|
|
2
|
+
//
|
|
3
|
+
// Every run: the folders that EXIST (from disk, with page counts and the
|
|
4
|
+
// purpose the structure file knows), then the folders the template
|
|
5
|
+
// still proposes (empty so far), then the rules. Reality wins: a folder
|
|
6
|
+
// a person or Deep created is listed as it is; the template only fills
|
|
7
|
+
// gaps. Also built here: the index of page basenames across the whole
|
|
8
|
+
// wiki, for the same-name check before a page is created.
|
|
9
|
+
|
|
10
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import type { WikiLanguage } from './language.ts';
|
|
13
|
+
import type { StructureFile } from './structure-file.ts';
|
|
14
|
+
import { taxonomyFor, taxonomyPaths, type WikiTaxonomy } from './taxonomy.ts';
|
|
15
|
+
|
|
16
|
+
export interface MapFolder {
|
|
17
|
+
path: string;
|
|
18
|
+
pages: number;
|
|
19
|
+
purpose: string;
|
|
20
|
+
origin: 'template' | 'deep' | 'user' | 'unknown';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface WikiMap {
|
|
24
|
+
folders: MapFolder[];
|
|
25
|
+
/** Template folders that do not exist on disk yet. */
|
|
26
|
+
planned: Array<{ path: string; purpose: string }>;
|
|
27
|
+
/** basename (lowercase, without .md) → wiki paths (without .md). */
|
|
28
|
+
sameName: Map<string, string[]>;
|
|
29
|
+
text: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const SKIP_DIRS = new Set(['logs', 'templates', 'attachments']);
|
|
33
|
+
|
|
34
|
+
/** Walk the wiki: folders up to two levels, page counts, basename index. */
|
|
35
|
+
export async function scanWiki(wikiAbs: string): Promise<{ folders: Map<string, number>; sameName: Map<string, string[]> }> {
|
|
36
|
+
const folders = new Map<string, number>();
|
|
37
|
+
const sameName = new Map<string, string[]>();
|
|
38
|
+
const walk = async (rel: string, depth: number): Promise<void> => {
|
|
39
|
+
let entries: string[];
|
|
40
|
+
try {
|
|
41
|
+
entries = await readdir(join(wikiAbs, rel));
|
|
42
|
+
} catch {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
for (const name of entries) {
|
|
46
|
+
if (name.startsWith('.') || name.startsWith('_')) continue;
|
|
47
|
+
const relPath = rel ? `${rel}/${name}` : name;
|
|
48
|
+
let st;
|
|
49
|
+
try {
|
|
50
|
+
st = await stat(join(wikiAbs, relPath));
|
|
51
|
+
} catch {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (st.isDirectory()) {
|
|
55
|
+
if (SKIP_DIRS.has(name) && depth === 0) continue;
|
|
56
|
+
if (!folders.has(relPath)) folders.set(relPath, 0);
|
|
57
|
+
if (depth < 2) await walk(relPath, depth + 1);
|
|
58
|
+
} else if (name.toLowerCase().endsWith('.md')) {
|
|
59
|
+
if (rel) folders.set(rel, (folders.get(rel) ?? 0) + 1);
|
|
60
|
+
const base = name.slice(0, -3).toLowerCase();
|
|
61
|
+
const list = sameName.get(base) ?? [];
|
|
62
|
+
list.push(relPath.slice(0, -3));
|
|
63
|
+
sameName.set(base, list);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
await walk('', 0);
|
|
68
|
+
return { folders, sameName };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function buildWikiMap(args: { wikiAbs: string; language: WikiLanguage; structure: StructureFile; taxonomy?: WikiTaxonomy }): Promise<WikiMap> {
|
|
72
|
+
const taxonomy = args.taxonomy ?? taxonomyFor(args.language);
|
|
73
|
+
const scanned = await scanWiki(args.wikiAbs);
|
|
74
|
+
const described = new Map(args.structure.folders.map((f) => [f.path, f]));
|
|
75
|
+
const templatePurpose = new Map<string, string>();
|
|
76
|
+
for (const f of taxonomy.folders) {
|
|
77
|
+
templatePurpose.set(f.path, f.purpose);
|
|
78
|
+
for (const s of f.subfolders ?? []) templatePurpose.set(s.path, s.purpose);
|
|
79
|
+
}
|
|
80
|
+
const folders: MapFolder[] = [...scanned.folders.entries()]
|
|
81
|
+
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
82
|
+
.map(([path, pages]) => {
|
|
83
|
+
const d = described.get(path);
|
|
84
|
+
if (d && d.purpose.trim()) return { path, pages, purpose: d.purpose, origin: d.origin };
|
|
85
|
+
const tp = templatePurpose.get(path);
|
|
86
|
+
if (tp) return { path, pages, purpose: tp, origin: 'template' };
|
|
87
|
+
return { path, pages, purpose: '', origin: 'unknown' };
|
|
88
|
+
});
|
|
89
|
+
const existing = new Set(folders.map((f) => f.path));
|
|
90
|
+
const planned = taxonomyPaths(taxonomy)
|
|
91
|
+
.filter((p) => !existing.has(p))
|
|
92
|
+
.map((p) => ({ path: p, purpose: templatePurpose.get(p) ?? '' }));
|
|
93
|
+
const text = renderMap(folders, planned);
|
|
94
|
+
return { folders, planned, sameName: scanned.sameName, text };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function renderMap(folders: MapFolder[], planned: Array<{ path: string; purpose: string }>): string {
|
|
98
|
+
const lines: string[] = ['<wiki_map>'];
|
|
99
|
+
lines.push('Existing folders (path — what lives there · pages):');
|
|
100
|
+
if (folders.length === 0) lines.push('- (no folders yet)');
|
|
101
|
+
for (const f of folders) lines.push(`- ${f.path} — ${f.purpose || '(no description yet)'} · ${f.pages}`);
|
|
102
|
+
if (planned.length > 0) {
|
|
103
|
+
lines.push('', 'Folders the template proposes that do not exist yet (create one by filing a page into it):');
|
|
104
|
+
for (const p of planned) lines.push(`- ${p.path} — ${p.purpose}`);
|
|
105
|
+
}
|
|
106
|
+
// The rules are model-facing and therefore English, like every prompt
|
|
107
|
+
// somora produces; the folder purposes above are content and stay in
|
|
108
|
+
// the wiki's language.
|
|
109
|
+
lines.push('', 'Rules:');
|
|
110
|
+
lines.push(
|
|
111
|
+
'- A folder says what KIND of page lives in it — never what a page is about. Topics live in the page, its links and index.md.',
|
|
112
|
+
'- A dated work report is not a page of its own but a timeline entry on the project or entity page it concerns.',
|
|
113
|
+
'- At most one level of subfolders.',
|
|
114
|
+
);
|
|
115
|
+
lines.push(
|
|
116
|
+
'- File a page into a DESCRIBED folder above (existing with a description, or proposed). A folder marked "(no description yet)" is not a home: file into the template folder for that kind instead, or describe the folder. Only when none fits, create a new one: give "subfolder" the new path and add "newFolder": {"path": "<the same path>", "purpose": "<one sentence, in the wiki language, what kind of page lives there>"} — the description is kept and shown here from the next run on. A new folder without a purpose is refused.',
|
|
117
|
+
);
|
|
118
|
+
lines.push('</wiki_map>');
|
|
119
|
+
return lines.join('\n');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Same-name check before a page is created: pages with the target's
|
|
124
|
+
* basename in OTHER folders. `preferredFolder` (the folder the model
|
|
125
|
+
* chose) picks one when several exist. Returns null when none.
|
|
126
|
+
*/
|
|
127
|
+
export function sameNamePages(map: Pick<WikiMap, 'sameName'>, targetPath: string, preferredFolder?: string): { target: string; others: string[] } | null {
|
|
128
|
+
const base = targetPath.replace(/^\/+/, '').replace(/\.md$/i, '').split('/').pop()!.toLowerCase();
|
|
129
|
+
const all = (map.sameName.get(base) ?? []).filter((p) => p.toLowerCase() !== targetPath.replace(/^\/+/, '').replace(/\.md$/i, '').toLowerCase());
|
|
130
|
+
if (all.length === 0) return null;
|
|
131
|
+
const pref = preferredFolder ? all.find((p) => p.startsWith(preferredFolder.replace(/\/+$/, '') + '/')) : undefined;
|
|
132
|
+
const target = pref ?? all[0]!;
|
|
133
|
+
return { target, others: all.filter((p) => p !== target) };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** A page was created: keep the in-memory map current for the rest of
|
|
137
|
+
* the run (folder counts, basename index) without another disk walk. */
|
|
138
|
+
export function noteNewPage(map: WikiMap, wikiPath: string, purposeIfNew?: { purpose: string; origin: MapFolder['origin'] }): void {
|
|
139
|
+
const rel = wikiPath.replace(/^\/+/, '').replace(/\.md$/i, '');
|
|
140
|
+
const parts = rel.split('/');
|
|
141
|
+
const base = parts[parts.length - 1]!.toLowerCase();
|
|
142
|
+
const list = map.sameName.get(base) ?? [];
|
|
143
|
+
if (!list.includes(rel)) list.push(rel);
|
|
144
|
+
map.sameName.set(base, list);
|
|
145
|
+
if (parts.length < 2) return;
|
|
146
|
+
const folder = parts.slice(0, -1).join('/');
|
|
147
|
+
const f = map.folders.find((x) => x.path === folder);
|
|
148
|
+
if (f) {
|
|
149
|
+
f.pages++;
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const planned = map.planned.find((p) => p.path === folder);
|
|
153
|
+
map.planned = map.planned.filter((p) => p.path !== folder);
|
|
154
|
+
map.folders.push({
|
|
155
|
+
path: folder,
|
|
156
|
+
pages: 1,
|
|
157
|
+
purpose: planned?.purpose ?? purposeIfNew?.purpose ?? '',
|
|
158
|
+
origin: planned ? 'template' : purposeIfNew?.origin ?? 'unknown',
|
|
159
|
+
});
|
|
160
|
+
map.folders.sort((a, b) => a.path.localeCompare(b.path));
|
|
161
|
+
// Every parent folder of a new subfolder exists after the write.
|
|
162
|
+
for (let i = 1; i < parts.length - 1; i++) {
|
|
163
|
+
const parent = parts.slice(0, i).join('/');
|
|
164
|
+
if (!map.folders.some((x) => x.path === parent)) {
|
|
165
|
+
const pl = map.planned.find((p) => p.path === parent);
|
|
166
|
+
map.planned = map.planned.filter((p) => p.path !== parent);
|
|
167
|
+
map.folders.push({ path: parent, pages: 0, purpose: pl?.purpose ?? '', origin: pl ? 'template' : 'unknown' });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Where a promote wants to write, checked against the map:
|
|
174
|
+
* - `sameName`: a page with that basename exists in another folder →
|
|
175
|
+
* the caller merges into it instead of creating a twin.
|
|
176
|
+
* - `unknownFolder`: the folder neither exists nor is proposed by the
|
|
177
|
+
* template, and the model gave no purpose → refused.
|
|
178
|
+
* - `tooDeep`: more than one level below a top folder → refused.
|
|
179
|
+
* - `ok`: with the folder entry to record when the folder is new.
|
|
180
|
+
*/
|
|
181
|
+
export type PromoteTargetCheck =
|
|
182
|
+
| { kind: 'ok'; folder: string; describe?: { path: string; purpose: string; origin: 'template' | 'deep' } }
|
|
183
|
+
| { kind: 'sameName'; target: string; others: string[] }
|
|
184
|
+
/** The new name extends an entity page's name (`enovom-kapitalruecklage`
|
|
185
|
+
* next to `unternehmen/enovom`): usually a detail of that entity. */
|
|
186
|
+
| { kind: 'subTopic'; target: string; prefix: string }
|
|
187
|
+
| { kind: 'unknownFolder'; folder: string }
|
|
188
|
+
| { kind: 'tooDeep'; folder: string };
|
|
189
|
+
|
|
190
|
+
export function checkPromoteTarget(map: WikiMap, decision: { slug: string; subfolder: string; newFolder?: { path: string; purpose: string } }, opts: { ignoreSubTopic?: boolean } = {}): PromoteTargetCheck {
|
|
191
|
+
const rel = decision.slug.replace(/^\/+/, '').replace(/\.md$/i, '');
|
|
192
|
+
const parts = rel.split('/');
|
|
193
|
+
const folder = parts.slice(0, -1).join('/');
|
|
194
|
+
if (parts.length > 3) return { kind: 'tooDeep', folder };
|
|
195
|
+
const same = sameNamePages(map, rel, decision.subfolder);
|
|
196
|
+
if (same) return { kind: 'sameName', target: same.target, others: same.others };
|
|
197
|
+
const sub = opts.ignoreSubTopic ? null : subTopicOf(map, rel);
|
|
198
|
+
if (sub) return { kind: 'subTopic', target: sub.target, prefix: sub.prefix };
|
|
199
|
+
if (!folder) return { kind: 'ok', folder };
|
|
200
|
+
const existing = map.folders.find((f) => f.path === folder);
|
|
201
|
+
if (existing) {
|
|
202
|
+
// An existing folder nobody has described yet is no home either
|
|
203
|
+
// (2026-09-29, Conny's wiki: Deep filed a new pet page into the
|
|
204
|
+
// grown `tiere/` although the template has `privat/` for pets) —
|
|
205
|
+
// unless the model describes it, which fills the gap for good.
|
|
206
|
+
if (!existing.purpose) {
|
|
207
|
+
if (decision.newFolder && decision.newFolder.path === folder && decision.newFolder.purpose.trim()) {
|
|
208
|
+
return { kind: 'ok', folder, describe: { path: folder, purpose: decision.newFolder.purpose.trim(), origin: 'deep' } };
|
|
209
|
+
}
|
|
210
|
+
return { kind: 'unknownFolder', folder };
|
|
211
|
+
}
|
|
212
|
+
return { kind: 'ok', folder };
|
|
213
|
+
}
|
|
214
|
+
const planned = map.planned.find((p) => p.path === folder);
|
|
215
|
+
if (planned) return { kind: 'ok', folder, describe: { path: folder, purpose: planned.purpose, origin: 'template' } };
|
|
216
|
+
if (decision.newFolder && decision.newFolder.path === folder && decision.newFolder.purpose.trim()) {
|
|
217
|
+
return { kind: 'ok', folder, describe: { path: folder, purpose: decision.newFolder.purpose.trim(), origin: 'deep' } };
|
|
218
|
+
}
|
|
219
|
+
return { kind: 'unknownFolder', folder };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Folders whose pages are entities a detail can belong to. */
|
|
223
|
+
const ENTITY_TOPS = new Set(['personen', 'people', 'unternehmen', 'companies', 'projekte', 'projects', 'orte', 'places', 'infrastruktur', 'infrastructure', 'besitz', 'possessions', 'agenten', 'agents']);
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* An entity page whose name the new page's name extends: `<name>-…`
|
|
227
|
+
* with `<name>` at least four characters (2026-09-29, Rene: the note
|
|
228
|
+
* on enovom's capital reserve became `projekte/enovom-kapitalruecklage-…`
|
|
229
|
+
* while `unternehmen/enovom` existed). The longest such name wins. Null
|
|
230
|
+
* when there is none — a page in a knowledge or event folder never
|
|
231
|
+
* counts, its name is a topic, not an entity.
|
|
232
|
+
*/
|
|
233
|
+
export function subTopicOf(map: Pick<WikiMap, 'sameName'>, targetPath: string): { target: string; prefix: string } | null {
|
|
234
|
+
const base = targetPath.replace(/^\/+/, '').replace(/\.md$/i, '').split('/').pop()!.toLowerCase();
|
|
235
|
+
let best: { target: string; prefix: string } | null = null;
|
|
236
|
+
for (const [name, paths] of map.sameName) {
|
|
237
|
+
if (name.length < 4 || !base.startsWith(name + '-')) continue;
|
|
238
|
+
const entity = paths.find((p) => ENTITY_TOPS.has(p.split('/')[0] ?? '') && p.split('/').length >= 2);
|
|
239
|
+
if (!entity) continue;
|
|
240
|
+
if (!best || name.length > best.prefix.length) best = { target: entity, prefix: name };
|
|
241
|
+
}
|
|
242
|
+
return best;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Bring the structure file up to date with what is on disk: every
|
|
247
|
+
* folder the walk found gets an entry (the template's purpose when the
|
|
248
|
+
* path is a template path, otherwise an empty `unknown` entry a person
|
|
249
|
+
* or the migration fills in). Returns true when something was added.
|
|
250
|
+
*/
|
|
251
|
+
export function syncStructureWithMap(structure: StructureFile, map: WikiMap, describe: (entry: { path: string; purpose: string; origin: MapFolder['origin'] }) => boolean): boolean {
|
|
252
|
+
let dirty = false;
|
|
253
|
+
for (const f of map.folders) {
|
|
254
|
+
if (structure.folders.some((s) => s.path === f.path)) continue;
|
|
255
|
+
if (describe({ path: f.path, purpose: f.purpose, origin: f.purpose ? f.origin : 'unknown' })) dirty = true;
|
|
256
|
+
}
|
|
257
|
+
return dirty;
|
|
258
|
+
}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
// The migration's first step — the plan, without a single write.
|
|
2
|
+
//
|
|
3
|
+
// Why (Rene, 2026-09-29): moving a grown wiki onto the template is the
|
|
4
|
+
// most delicate thing somora will ever do to an installation. So the
|
|
5
|
+
// migration is split: this module reads the whole wiki and writes down
|
|
6
|
+
// what it WOULD do, sorted by how sure it is. Rule-based items (a
|
|
7
|
+
// folder named `aktien` belongs in `finanzen/depot`) need no model;
|
|
8
|
+
// everything else is handed to the model in a later step, page by
|
|
9
|
+
// page, and every group is approved by a person before a file moves.
|
|
10
|
+
//
|
|
11
|
+
// Nothing here touches the wiki. The plan is a file under
|
|
12
|
+
// ~/.somora/wiki-migration/ that a person reads first.
|
|
13
|
+
|
|
14
|
+
import { readdir, readFile, stat } from 'node:fs/promises';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import matter from 'gray-matter';
|
|
17
|
+
import type { WikiLanguage } from '../language.ts';
|
|
18
|
+
import type { StructureFile } from '../structure-file.ts';
|
|
19
|
+
import { taxonomyFor, taxonomyPaths, type WikiTaxonomy } from '../taxonomy.ts';
|
|
20
|
+
|
|
21
|
+
export interface InventoryPage {
|
|
22
|
+
/** Wiki path without .md. */
|
|
23
|
+
path: string;
|
|
24
|
+
folder: string;
|
|
25
|
+
base: string;
|
|
26
|
+
type: string;
|
|
27
|
+
title: string;
|
|
28
|
+
bytes: number;
|
|
29
|
+
/** A date in the file name — the mark of a work report. */
|
|
30
|
+
dated: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface Inventory {
|
|
34
|
+
pages: InventoryPage[];
|
|
35
|
+
/** folder → pages directly in it. */
|
|
36
|
+
byFolder: Map<string, InventoryPage[]>;
|
|
37
|
+
/** lowercase basename → pages. */
|
|
38
|
+
byBase: Map<string, InventoryPage[]>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** One proposed step. `decidedBy: 'rule'` items can run without a
|
|
42
|
+
* model; `'model'` items are refined by the model in the next step. */
|
|
43
|
+
export type PlanItem =
|
|
44
|
+
| { id: number; kind: 'move_folder'; decidedBy: 'rule'; from: string; to: string; pages: string[]; why: string }
|
|
45
|
+
| { id: number; kind: 'unite_twins'; decidedBy: 'model'; name: string; keep: string; drop: string[]; why: string }
|
|
46
|
+
| { id: number; kind: 'fold_report'; decidedBy: 'model'; page: string; into: string | null; why: string }
|
|
47
|
+
| { id: number; kind: 'review_pages'; decidedBy: 'model'; folder: string; pages: string[]; why: string }
|
|
48
|
+
| { id: number; kind: 'describe_folder'; decidedBy: 'model'; folder: string; pages: number; why: string }
|
|
49
|
+
| { id: number; kind: 'unclear'; decidedBy: 'model'; pages: string[]; why: string };
|
|
50
|
+
|
|
51
|
+
export interface MigrationPlan {
|
|
52
|
+
language: WikiLanguage;
|
|
53
|
+
templateVersion: number;
|
|
54
|
+
createdAt: string;
|
|
55
|
+
pagesTotal: number;
|
|
56
|
+
foldersTotal: number;
|
|
57
|
+
items: PlanItem[];
|
|
58
|
+
summary: Record<PlanItem['kind'], { items: number; pages: number }>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const SKIP_TOP = new Set(['logs', 'templates', 'attachments']);
|
|
62
|
+
|
|
63
|
+
export async function readInventory(wikiAbs: string): Promise<Inventory> {
|
|
64
|
+
const pages: InventoryPage[] = [];
|
|
65
|
+
const walk = async (rel: string, depth: number): Promise<void> => {
|
|
66
|
+
let entries: string[];
|
|
67
|
+
try {
|
|
68
|
+
entries = await readdir(join(wikiAbs, rel));
|
|
69
|
+
} catch {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
for (const name of entries.sort()) {
|
|
73
|
+
if (name.startsWith('.') || name.startsWith('_')) continue;
|
|
74
|
+
const relPath = rel ? `${rel}/${name}` : name;
|
|
75
|
+
let st;
|
|
76
|
+
try {
|
|
77
|
+
st = await stat(join(wikiAbs, relPath));
|
|
78
|
+
} catch {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (st.isDirectory()) {
|
|
82
|
+
if (depth === 0 && SKIP_TOP.has(name)) continue;
|
|
83
|
+
await walk(relPath, depth + 1);
|
|
84
|
+
} else if (name.toLowerCase().endsWith('.md')) {
|
|
85
|
+
if (!rel && name === 'index.md') continue;
|
|
86
|
+
let fm: Record<string, unknown> = {};
|
|
87
|
+
let body = '';
|
|
88
|
+
try {
|
|
89
|
+
const raw = await readFile(join(wikiAbs, relPath), 'utf8');
|
|
90
|
+
const parsed = matter(raw);
|
|
91
|
+
fm = parsed.data as Record<string, unknown>;
|
|
92
|
+
body = parsed.content;
|
|
93
|
+
} catch {
|
|
94
|
+
/* unreadable page: listed without type */
|
|
95
|
+
}
|
|
96
|
+
const base = name.slice(0, -3);
|
|
97
|
+
const h1 = /^#\s+(.+)$/m.exec(body)?.[1]?.trim();
|
|
98
|
+
pages.push({
|
|
99
|
+
path: relPath.slice(0, -3),
|
|
100
|
+
folder: rel,
|
|
101
|
+
base,
|
|
102
|
+
type: typeof fm.type === 'string' ? fm.type.trim().toLowerCase() : '',
|
|
103
|
+
title: typeof fm.title === 'string' ? fm.title : h1 ?? base,
|
|
104
|
+
bytes: st.size,
|
|
105
|
+
dated: /\d{4}-\d{2}-\d{2}/.test(base),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
await walk('', 0);
|
|
111
|
+
const byFolder = new Map<string, InventoryPage[]>();
|
|
112
|
+
const byBase = new Map<string, InventoryPage[]>();
|
|
113
|
+
for (const p of pages) {
|
|
114
|
+
byFolder.set(p.folder, [...(byFolder.get(p.folder) ?? []), p]);
|
|
115
|
+
const b = p.base.toLowerCase();
|
|
116
|
+
byBase.set(b, [...(byBase.get(b) ?? []), p]);
|
|
117
|
+
}
|
|
118
|
+
return { pages, byFolder, byBase };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const topOf = (folder: string): string => folder.split('/')[0] ?? '';
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Where a grown folder belongs by rule: a template path stays; an
|
|
125
|
+
* alias maps; a template subfolder path given as the wrong parent
|
|
126
|
+
* (e.g. `hosts/cerebro`) follows its alias parent. Null = no rule.
|
|
127
|
+
*/
|
|
128
|
+
export function ruleTarget(folder: string, taxonomy: WikiTaxonomy, templatePaths: Set<string>): string | null {
|
|
129
|
+
if (!folder) return null;
|
|
130
|
+
if (templatePaths.has(folder)) return folder;
|
|
131
|
+
const parts = folder.split('/');
|
|
132
|
+
const top = parts[0]!;
|
|
133
|
+
const alias = taxonomy.aliases[top];
|
|
134
|
+
if (alias) return alias;
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function analyzeWiki(args: { wikiAbs: string; language: WikiLanguage; structure: StructureFile; taxonomy?: WikiTaxonomy; now?: Date }): Promise<MigrationPlan> {
|
|
139
|
+
const taxonomy = args.taxonomy ?? taxonomyFor(args.language);
|
|
140
|
+
const templatePaths = new Set(taxonomyPaths(taxonomy));
|
|
141
|
+
const inv = await readInventory(args.wikiAbs);
|
|
142
|
+
const items: PlanItem[] = [];
|
|
143
|
+
let id = 1;
|
|
144
|
+
const described = new Map(args.structure.folders.map((f) => [f.path, f.purpose]));
|
|
145
|
+
const agentsFolder = taxonomy.folders.find((f) => f.path === 'agenten' || f.path === 'agents')?.path ?? 'agents';
|
|
146
|
+
const projectsFolder = taxonomy.folders.find((f) => f.path === 'projekte' || f.path === 'projects')?.path ?? 'projects';
|
|
147
|
+
const knowledgeFolder = taxonomy.folders.find((f) => f.path === 'wissen' || f.path === 'knowledge')?.path ?? 'knowledge';
|
|
148
|
+
const projectBases = (inv.byFolder.get(projectsFolder) ?? []).map((p) => p.base.toLowerCase()).sort((a, b) => b.length - a.length);
|
|
149
|
+
|
|
150
|
+
// 1. Twins: the same name in several folders. Keep the copy in a
|
|
151
|
+
// template folder (or the larger one); the union of the two
|
|
152
|
+
// bodies is the model's job.
|
|
153
|
+
const twinPages = new Set<string>();
|
|
154
|
+
for (const [name, pages] of inv.byBase) {
|
|
155
|
+
if (pages.length < 2) continue;
|
|
156
|
+
const ranked = [...pages].sort((a, b) => Number(templatePaths.has(b.folder)) - Number(templatePaths.has(a.folder)) || b.bytes - a.bytes);
|
|
157
|
+
const keep = ranked[0]!;
|
|
158
|
+
items.push({ id: id++, kind: 'unite_twins', decidedBy: 'model', name, keep: keep.path, drop: ranked.slice(1).map((p) => p.path), why: `${pages.length} pages named "${name}" in different folders` });
|
|
159
|
+
for (const p of pages) twinPages.add(p.path);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// 2. Dated reports: fold into the project page they concern.
|
|
163
|
+
const reportPages = new Set<string>();
|
|
164
|
+
for (const p of inv.pages) {
|
|
165
|
+
if (!p.dated || twinPages.has(p.path)) continue; // a dated twin is judged in the review, united first
|
|
166
|
+
if (p.folder === projectsFolder) continue; // a dated project page is the model's call in the review below
|
|
167
|
+
const base = p.base.toLowerCase();
|
|
168
|
+
const into = projectBases.find((b) => base.startsWith(b + '-') || base.includes('-' + b + '-')) ?? null;
|
|
169
|
+
items.push({ id: id++, kind: 'fold_report', decidedBy: 'model', page: p.path, into: into ? `${projectsFolder}/${into}` : null, why: into ? `dated report, name matches project ${into}` : 'dated report, project to be found by the model' });
|
|
170
|
+
reportPages.add(p.path);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 3. Folders: by rule where an alias exists, otherwise a per-page
|
|
174
|
+
// review. Pages already handled above are left out of the lists.
|
|
175
|
+
// Twin pages stay in the per-page review: the union decides which
|
|
176
|
+
// copy survives, the review decides where the survivor belongs
|
|
177
|
+
// (2026-09-29, English demo: two `mortgage` pages united into
|
|
178
|
+
// notes/mortgage, which then stayed in a folder the template does
|
|
179
|
+
// not know). At execution the union wins over a move or fold of a
|
|
180
|
+
// dropped copy.
|
|
181
|
+
const handled = (p: InventoryPage): boolean => reportPages.has(p.path);
|
|
182
|
+
const foldersSeen = new Set<string>();
|
|
183
|
+
for (const [folder, pages] of [...inv.byFolder.entries()].sort((a, b) => a[0].localeCompare(b[0]))) {
|
|
184
|
+
foldersSeen.add(folder);
|
|
185
|
+
const rest = pages.filter((p) => !handled(p)).map((p) => p.path);
|
|
186
|
+
if (!folder) {
|
|
187
|
+
if (rest.length > 0) items.push({ id: id++, kind: 'unclear', decidedBy: 'model', pages: rest, why: 'pages in the wiki root' });
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const top = topOf(folder);
|
|
191
|
+
const target = ruleTarget(folder, taxonomy, templatePaths);
|
|
192
|
+
if (target && target !== folder) {
|
|
193
|
+
if (rest.length > 0) items.push({ id: id++, kind: 'move_folder', decidedBy: 'rule', from: folder, to: target, pages: rest, why: `"${top}" is a known name for ${target}` });
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (target === folder) {
|
|
197
|
+
// A template folder. Every page still gets the model's look
|
|
198
|
+
// (Rene, 2026-09-29: the first run folded notes into pages that
|
|
199
|
+
// were themselves misfiled, because template folders were
|
|
200
|
+
// trusted). Only an agent profile in the agents folder is
|
|
201
|
+
// exempt — it is what that folder is for.
|
|
202
|
+
const toReview = top === agentsFolder ? pages.filter((p) => !handled(p) && p.type !== 'agent').map((p) => p.path) : rest;
|
|
203
|
+
if (toReview.length > 0) {
|
|
204
|
+
const why = top === agentsFolder
|
|
205
|
+
? 'only agent profiles stay here; work belongs to the projects'
|
|
206
|
+
: top === knowledgeFolder
|
|
207
|
+
? 'only knowledge tied to no person, company or device stays here'
|
|
208
|
+
: 'template folder — each page is checked to be the kind that lives here';
|
|
209
|
+
items.push({ id: id++, kind: 'review_pages', decidedBy: 'model', folder, pages: toReview, why });
|
|
210
|
+
}
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
// Under a template top folder but not a template path
|
|
214
|
+
// (agenten/hans, projekte/somora, personen/rene-siegl, …).
|
|
215
|
+
if (templatePaths.has(top)) {
|
|
216
|
+
if (rest.length > 0) {
|
|
217
|
+
const why = top === agentsFolder
|
|
218
|
+
? 'an agent\'s own folder: reports go to the projects, the rest is judged page by page'
|
|
219
|
+
: top === projectsFolder
|
|
220
|
+
? 'sub-pages of a project: fold into the project page or keep as its own page'
|
|
221
|
+
: 'sub-pages under an entity: fold into the entity page or keep';
|
|
222
|
+
items.push({ id: id++, kind: 'review_pages', decidedBy: 'model', folder, pages: rest, why });
|
|
223
|
+
}
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
// Unknown top folder without a rule.
|
|
227
|
+
if (rest.length > 0) items.push({ id: id++, kind: 'review_pages', decidedBy: 'model', folder, pages: rest, why: `no rule for "${top}" — folder judged page by page` });
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 4. Folders that stay and have no description.
|
|
231
|
+
for (const folder of foldersSeen) {
|
|
232
|
+
if (!folder || !templatePaths.has(folder)) continue;
|
|
233
|
+
if ((described.get(folder) ?? '').trim()) continue;
|
|
234
|
+
const tpl = taxonomyPaths(taxonomy).includes(folder);
|
|
235
|
+
if (tpl) continue; // the template describes it
|
|
236
|
+
items.push({ id: id++, kind: 'describe_folder', decidedBy: 'model', folder, pages: inv.byFolder.get(folder)?.length ?? 0, why: 'folder stays but has no description' });
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const summary = {} as MigrationPlan['summary'];
|
|
240
|
+
for (const kind of ['move_folder', 'unite_twins', 'fold_report', 'review_pages', 'describe_folder', 'unclear'] as const) summary[kind] = { items: 0, pages: 0 };
|
|
241
|
+
for (const it of items) {
|
|
242
|
+
summary[it.kind].items++;
|
|
243
|
+
summary[it.kind].pages += it.kind === 'move_folder' || it.kind === 'review_pages' || it.kind === 'unclear' ? it.pages.length : it.kind === 'unite_twins' ? it.drop.length + 1 : it.kind === 'fold_report' ? 1 : it.pages;
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
language: args.language,
|
|
247
|
+
templateVersion: taxonomy.version,
|
|
248
|
+
createdAt: (args.now ?? new Date()).toISOString(),
|
|
249
|
+
pagesTotal: inv.pages.length,
|
|
250
|
+
foldersTotal: [...inv.byFolder.keys()].filter(Boolean).length,
|
|
251
|
+
items,
|
|
252
|
+
summary,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** The plan as a page a person reads — headings per kind, sure things first. */
|
|
257
|
+
export function renderPlan(plan: MigrationPlan): string {
|
|
258
|
+
const de = plan.language === 'de';
|
|
259
|
+
const L: string[] = [];
|
|
260
|
+
L.push(de ? '# Migrationsplan (Probelauf — nichts wurde verschoben)' : '# Migration plan (dry run — nothing was moved)');
|
|
261
|
+
L.push('', `${de ? 'Erstellt' : 'Created'}: ${plan.createdAt.slice(0, 16).replace('T', ' ')} · ${de ? 'Seiten' : 'pages'}: ${plan.pagesTotal} · ${de ? 'Ordner' : 'folders'}: ${plan.foldersTotal} · ${de ? 'Vorlage' : 'template'} v${plan.templateVersion}`, '');
|
|
262
|
+
L.push(de ? '| Schritt | Einträge | Seiten | entscheidet |' : '| Step | Items | Pages | decided by |', '|---|---|---|---|');
|
|
263
|
+
const names: Record<PlanItem['kind'], [string, string, string]> = {
|
|
264
|
+
move_folder: ['Ordner nach Regel verschieben', 'Move folder by rule', 'rule'],
|
|
265
|
+
unite_twins: ['Gleichnamige Seiten vereinen', 'Unite same-name pages', 'model + you'],
|
|
266
|
+
fold_report: ['Berichte in Projektseiten einarbeiten', 'Fold reports into project pages', 'model + you'],
|
|
267
|
+
review_pages: ['Seiten einzeln beurteilen', 'Judge pages one by one', 'model + you'],
|
|
268
|
+
describe_folder: ['Ordner beschreiben', 'Describe folder', 'model'],
|
|
269
|
+
unclear: ['Unklar', 'Unclear', 'you'],
|
|
270
|
+
};
|
|
271
|
+
for (const kind of Object.keys(names) as PlanItem['kind'][]) {
|
|
272
|
+
const s = plan.summary[kind];
|
|
273
|
+
L.push(`| ${de ? names[kind][0] : names[kind][1]} | ${s.items} | ${s.pages} | ${names[kind][2]} |`);
|
|
274
|
+
}
|
|
275
|
+
const section = (kind: PlanItem['kind']): void => {
|
|
276
|
+
const its = plan.items.filter((i) => i.kind === kind);
|
|
277
|
+
if (its.length === 0) return;
|
|
278
|
+
L.push('', `## ${de ? names[kind][0] : names[kind][1]}`, '');
|
|
279
|
+
for (const it of its) {
|
|
280
|
+
switch (it.kind) {
|
|
281
|
+
case 'move_folder':
|
|
282
|
+
L.push(`- #${it.id} \`${it.from}/\` → \`${it.to}/\` (${it.pages.length} ${de ? 'Seiten' : 'pages'}) — ${it.why}`);
|
|
283
|
+
break;
|
|
284
|
+
case 'unite_twins':
|
|
285
|
+
L.push(`- #${it.id} **${it.name}**: ${de ? 'behalten' : 'keep'} \`${it.keep}\`, ${de ? 'einarbeiten' : 'fold in'} ${it.drop.map((d) => `\`${d}\``).join(', ')}`);
|
|
286
|
+
break;
|
|
287
|
+
case 'fold_report':
|
|
288
|
+
L.push(`- #${it.id} \`${it.page}\` → ${it.into ? `\`${it.into}\`` : de ? '(Projekt offen)' : '(project open)'}`);
|
|
289
|
+
break;
|
|
290
|
+
case 'review_pages':
|
|
291
|
+
L.push(`- #${it.id} \`${it.folder}/\` (${it.pages.length} ${de ? 'Seiten' : 'pages'}) — ${it.why}`);
|
|
292
|
+
break;
|
|
293
|
+
case 'describe_folder':
|
|
294
|
+
L.push(`- #${it.id} \`${it.folder}/\` (${it.pages} ${de ? 'Seiten' : 'pages'})`);
|
|
295
|
+
break;
|
|
296
|
+
case 'unclear':
|
|
297
|
+
L.push(`- #${it.id} ${it.why}: ${it.pages.map((p) => `\`${p}\``).join(', ')}`);
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
for (const kind of Object.keys(names) as PlanItem['kind'][]) section(kind);
|
|
303
|
+
return L.join('\n') + '\n';
|
|
304
|
+
}
|