claude-code-session-manager 0.138.0 → 0.140.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.
- package/.claude-plugin/marketplace.json +1 -1
- package/README.md +352 -79
- package/dist/assets/AppSettings-CMFEM68R.js +2 -0
- package/dist/assets/History--JwsFnbn.js +2 -0
- package/dist/assets/HomeClaudeConfig-ijEXs--3.js +3 -0
- package/dist/assets/{Panel-CeDiyuNn.js → Panel-CALyU0pe.js} +1 -1
- package/dist/assets/ProjectClaudeConfig-sdlA-fUb.js +26 -0
- package/dist/assets/Scheduler-YT3j8SUa.js +16 -0
- package/dist/assets/{SkillReferenceGraph-CFMK8NJh.js → SkillReferenceGraph-CxAuwhNN.js} +1 -1
- package/dist/assets/Skills-DtRzghn5.js +67 -0
- package/dist/assets/{TiptapBody-D1kTj-tn.js → TiptapBody-DApeu7x8.js} +60 -54
- package/dist/assets/ViewTabs-B7qLXRAH.js +1 -0
- package/dist/assets/{VoiceButton-CZeyPM31.js → VoiceButton-DtStfv1H.js} +1 -1
- package/dist/assets/{historyTotals-B00l64Jw.js → historyTotals-ydfnwepc.js} +1 -1
- package/dist/assets/index-CtxruO1L.js +3121 -0
- package/dist/assets/{index-62tyjEQB.css → index-DKXdXfka.css} +1 -1
- package/dist/index.html +2 -2
- package/dist-web/app.css +1 -1
- package/dist-web/app.js +467 -451
- package/package.json +17 -6
- package/plugins/AGENTS.md +60 -0
- package/plugins/CLAUDE.md +1 -60
- package/plugins/session-manager-dev/.claude-plugin/plugin.json +1 -1
- package/plugins/session-manager-dev/skills/builder/SKILL.md +12 -4
- package/plugins/session-manager-dev/skills/builder/scripts/publish-preflight.cjs +255 -0
- package/plugins/session-manager-dev/skills/develop/SKILL.md +36 -22
- package/plugins/session-manager-dev/skills/maintaining-md-network/SKILL.md +5 -4
- package/plugins/session-manager-dev/skills/maintaining-md-network/audit.md +4 -2
- package/plugins/session-manager-dev/skills/maintaining-md-network/drift-check.md +16 -12
- package/plugins/session-manager-dev/skills/maintaining-md-network/migrate-to-agents-md.md +15 -0
- package/plugins/session-manager-dev/skills/maintaining-md-network/placement.md +50 -2
- package/plugins/session-manager-dev/skills/maintaining-md-network/plan.md +10 -10
- package/plugins/session-manager-dev/skills/maintaining-md-network/scripts/drift-check.cjs +117 -112
- package/plugins/session-manager-dev/skills/maintaining-md-network/scripts/mdDuplicates.cjs +182 -0
- package/plugins/session-manager-dev/skills/maintaining-md-network/scripts/mdLinks.cjs +340 -0
- package/scripts/README.md +2 -0
- package/scripts/hooks/guard-tool-risk.cjs +91 -0
- package/scripts/hooks/lib/guard-tool-risk-policy.cjs +217 -0
- package/scripts/ops-sweep.cjs +53 -23
- package/scripts/scheduler-mcp-server.cjs +360 -35
- package/src/main/build-info.json +4 -4
- package/src/main/chatRunner.cjs +2 -2
- package/src/main/config.cjs +112 -8
- package/src/main/docEdit.cjs +6 -1
- package/src/main/exchanges.cjs +70 -5
- package/src/main/files.cjs +7 -27
- package/src/main/health.cjs +48 -15
- package/src/main/historyAggregator.cjs +172 -63
- package/src/main/historyDashboard.cjs +87 -17
- package/src/main/index.cjs +78 -4
- package/src/main/ipcSchemas.cjs +233 -20
- package/src/main/lib/AGENTS.md +54 -0
- package/src/main/lib/CLAUDE.md +1 -54
- package/src/main/lib/agent-cli/adapters/codex.cjs +5 -2
- package/src/main/lib/agent-cli/agentCliIpc.cjs +18 -2
- package/src/main/lib/agent-cli/agentCliSessionStore.cjs +1 -0
- package/src/main/lib/agent-cli/agentCliTurn.cjs +20 -2
- package/src/main/lib/agent-cli/geminiContextDefaults.cjs +94 -0
- package/src/main/lib/claude/effectiveModelInfo.cjs +92 -22
- package/src/main/lib/claude/jobSandboxPolicy.cjs +202 -0
- package/src/main/lib/claude/modelPricing.cjs +148 -0
- package/src/main/lib/claude/personaToolScope.cjs +12 -6
- package/src/main/lib/claude/rateLimitDetect.cjs +13 -2
- package/src/main/lib/claude/runClaudeP.cjs +6 -1
- package/src/main/lib/claude/toolRiskIpc.cjs +177 -0
- package/src/main/lib/claude/toolRiskMarks.cjs +373 -0
- package/src/main/lib/demo-video-kit/seedKit.cjs +27 -6
- package/src/main/lib/ephemeralCwd.cjs +37 -2
- package/src/main/lib/epic/delegationReadiness.cjs +42 -9
- package/src/main/lib/epic/epicAdminRoutes.cjs +121 -0
- package/src/main/lib/epic/epicJobReceipt.cjs +87 -0
- package/src/main/lib/epic/epicOutsideRead.cjs +423 -0
- package/src/main/lib/epic/epicOutsideState.cjs +233 -0
- package/src/main/lib/epic/epicSettle.cjs +1 -1
- package/src/main/lib/epic/epicStateWatcher.cjs +157 -0
- package/src/main/lib/epic/epicSweepValidator.cjs +7 -2
- package/src/main/lib/epic/epicValidationHook.cjs +24 -4
- package/src/main/lib/epic/externalRef.cjs +37 -0
- package/src/main/lib/epic/externalRefIndex.cjs +136 -0
- package/src/main/lib/epic/guardSpawnSettings.cjs +57 -1
- package/src/main/lib/epic/restorableSessions.cjs +151 -17
- package/src/main/lib/epic/transcriptUsage.cjs +197 -0
- package/src/main/lib/externalContract.cjs +137 -0
- package/src/main/lib/feedback-remote/crossProjectFeedback.cjs +176 -5
- package/src/main/lib/feedback-remote/crossProjectReply.cjs +186 -9
- package/src/main/lib/feedback-remote/customerFeedbackAdminRoutes.cjs +16 -6
- package/src/main/lib/feedback-remote/customerFeedbackClient.cjs +50 -8
- package/src/main/lib/feedback-remote/customerFeedbackInbox.cjs +218 -4
- package/src/main/lib/feedback-remote/customerFeedbackIpc.cjs +19 -3
- package/src/main/lib/feedback-remote/feedbackImages.cjs +171 -0
- package/src/main/lib/feedback-remote/localAdminHttp.cjs +11 -10
- package/src/main/lib/feedback-remote/macroAdminRoutes.cjs +167 -21
- package/src/main/lib/feedback-remote/needsYouAdminRoutes.cjs +133 -0
- package/src/main/lib/feedback-remote/needsYouIpc.cjs +50 -0
- package/src/main/lib/feedback-remote/needsYouStore.cjs +285 -0
- package/src/main/lib/feedback-remote/projectHomeAdminRoutes.cjs +98 -1
- package/src/main/lib/feedback-remote/remoteIpcDispatch.cjs +5 -0
- package/src/main/lib/git/branchSweep.cjs +209 -4
- package/src/main/lib/git/gitWorktree.cjs +41 -5
- package/src/main/lib/guardShims.cjs +1 -1
- package/src/main/lib/historyRollup.cjs +58 -5
- package/src/main/lib/insideHome.cjs +27 -1
- package/src/main/lib/mcpToolCatalog.cjs +232 -25
- package/src/main/lib/mdDocCensus.cjs +59 -78
- package/src/main/lib/mdDocRefs.cjs +71 -93
- package/src/main/lib/mdNetworkScan.cjs +245 -250
- package/src/main/lib/mdReviewRecord.cjs +22 -9
- package/src/main/lib/memoryMdNetwork.cjs +5 -12
- package/src/main/lib/opsOwnership.cjs +18 -2
- package/src/main/lib/opsRetentionSettings.cjs +28 -4
- package/src/main/lib/persona-macro/agentAdminRoutes.cjs +121 -0
- package/src/main/lib/persona-macro/macroJobDispatch.cjs +90 -20
- package/src/main/lib/persona-macro/macroJobIpc.cjs +144 -46
- package/src/main/lib/persona-macro/macroJobStatus.cjs +87 -9
- package/src/main/lib/persona-macro/macroLibrary.cjs +95 -5
- package/src/main/lib/persona-macro/personaImportHealth.cjs +78 -17
- package/src/main/lib/persona-macro/projectVideoToolCheck.cjs +96 -0
- package/src/main/lib/persona-macro/shippedPersonaSeeds.cjs +11 -0
- package/src/main/lib/prd/depSlugResolve.cjs +104 -4
- package/src/main/lib/prd/prdAdminRoutes.cjs +35 -2
- package/src/main/lib/prd/prdCreate.cjs +351 -23
- package/src/main/lib/prd/prdDisposition.cjs +11 -2
- package/src/main/lib/prd/prdFrontmatter.cjs +96 -3
- package/src/main/lib/process/loginItem.cjs +171 -0
- package/src/main/lib/process/loginItemPlan.cjs +193 -0
- package/src/main/lib/project-pages/playerBridge.cjs +4 -0
- package/src/main/lib/project-pages/videoReview.cjs +268 -0
- package/src/main/lib/project-pages/videoReviewIpc.cjs +229 -0
- package/src/main/lib/promptSessionSchema.cjs +4 -0
- package/src/main/lib/remoteTranscriptTail.cjs +32 -1
- package/src/main/lib/scheduler/cpuSample.cjs +131 -4
- package/src/main/lib/scheduler/fixPlanSlug.cjs +4 -3
- package/src/main/lib/scheduler/loadGate.cjs +24 -6
- package/src/main/lib/scheduler/planRecordHarvest.cjs +108 -0
- package/src/main/lib/scheduler/planRecordRetention.cjs +144 -0
- package/src/main/lib/scheduler/queueStore.cjs +7 -0
- package/src/main/lib/scheduler/rcaReport.cjs +21 -1
- package/src/main/lib/scheduler/reaperHelpers.cjs +35 -5
- package/src/main/lib/scheduler/runLogRetention.cjs +104 -3
- package/src/main/lib/scheduler/scheduleJobSchema.cjs +3 -0
- package/src/main/lib/scheduler/schedulerBatch.cjs +25 -48
- package/src/main/lib/scheduler/schedulerPaths.cjs +12 -0
- package/src/main/lib/scheduler/sessionSlots.cjs +176 -21
- package/src/main/lib/scheduler/validationQuality.cjs +282 -0
- package/src/main/lib/scheduler/validatorVerdictSchema.cjs +254 -0
- package/src/main/lib/tasks/taskAdminRoutes.cjs +641 -0
- package/src/main/lib/tasks/taskIpc.cjs +80 -0
- package/src/main/lib/tasks/taskLedger.cjs +502 -0
- package/src/main/lib/tasks/taskRun.cjs +313 -0
- package/src/main/lib/tasks/taskSwitch.cjs +120 -0
- package/src/main/lib/telemetry/auditEvents.cjs +176 -0
- package/src/main/lib/telemetry/eventsAdminRoutes.cjs +45 -0
- package/src/main/loginItemIpc.cjs +35 -0
- package/src/main/loopEfficiencyHandlers.cjs +11 -2
- package/src/main/projectPages.cjs +6 -15
- package/src/main/promptSessionEvents.cjs +9 -1
- package/src/main/queueOps.cjs +260 -3
- package/src/main/runVerify.cjs +53 -25
- package/src/main/scheduler/AGENTS.md +26 -0
- package/src/main/scheduler/CLAUDE.md +1 -26
- package/src/main/scheduler/budget.cjs +239 -9
- package/src/main/scheduler/cancelDependents.cjs +154 -0
- package/src/main/scheduler/prdParser.cjs +4 -1
- package/src/main/scheduler/starvation.cjs +20 -18
- package/src/main/scheduler.cjs +774 -64
- package/src/main/supervisor.cjs +5 -1
- package/src/main/templates/PRD_AUTHORING.md +7 -1
- package/src/main/transcripts.cjs +62 -7
- package/src/main/validationQualityHandlers.cjs +199 -0
- package/src/main/webRemote.cjs +132 -20
- package/src/preload/api.d.ts +434 -28
- package/src/preload/apiSurface.cjs +7 -0
- package/src/preload/apiSurface.json +485 -81
- package/src/preload/index.cjs +38 -5
- package/src/seed/agents/validator.md +19 -8
- package/dist/assets/AgentLibrary-C1d_D7uQ.js +0 -2
- package/dist/assets/AppSettings-CgDhqrsU.js +0 -2
- package/dist/assets/Button-Bk23rEnZ.js +0 -1
- package/dist/assets/DocumentEditorPane-BqY5Nq6W.js +0 -1
- package/dist/assets/Extensions-CXem-uwI.js +0 -2
- package/dist/assets/History-BwO3coo4.js +0 -2
- package/dist/assets/Library-BDV69fSe.js +0 -32
- package/dist/assets/ListDetail-CD-Fm6mh.js +0 -1
- package/dist/assets/ProjectClaudeConfig-NjdKdwe8.js +0 -25
- package/dist/assets/ProvenanceBadge-Bu8NmI_R.js +0 -1
- package/dist/assets/Scheduler-CkhTvG7C.js +0 -16
- package/dist/assets/Settings-COT34hBF.js +0 -2
- package/dist/assets/Skills-BgexoUkQ.js +0 -29
- package/dist/assets/SourceChip-BXjcdeb1.js +0 -1
- package/dist/assets/SystemPrompt-CeeH3EeW.js +0 -1
- package/dist/assets/Toggle-CENi19iK.js +0 -1
- package/dist/assets/ViewTabs-DDBb0x96.js +0 -1
- package/dist/assets/claude-settings-schema-COigsajx.js +0 -3
- package/dist/assets/index-CgCxpunJ.js +0 -3116
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* mdNetworkScan.cjs — a project's markdown link network, mapped from its root
|
|
6
|
-
* CLAUDE.md, for Project Home's Tools > Review MD network (link map + findings).
|
|
6
|
+
* map doc (root AGENTS.md, else root CLAUDE.md — mdDocCensus rootMapDoc), for Project Home's Tools > Review MD network (link map + findings).
|
|
7
7
|
* No LLM, read-only, bounded.
|
|
8
8
|
*
|
|
9
9
|
* Top-level in lib/ because no subfolder fits: it reads the project's own docs,
|
|
@@ -11,20 +11,38 @@
|
|
|
11
11
|
* (git only lists the files), and it does I/O, so it is not util/. It sits
|
|
12
12
|
* beside memoryMdNetwork.cjs, the other reader of the same docs network.
|
|
13
13
|
*
|
|
14
|
-
* - buildMdNetwork({ files, root, exists, records })
|
|
15
|
-
* in-memory docs
|
|
16
|
-
*
|
|
14
|
+
* - buildMdNetwork({ files, root, exists, localOnly, records, loadedByTools, history })
|
|
15
|
+
* — pure graph builder over in-memory docs. Beside an AGENTS.md root, a root
|
|
16
|
+
* CLAUDE.md (the stub that imports it) is part of the map: an edge from the
|
|
17
|
+
* hub reaches it at depth 1. Docs a tool loads by name
|
|
18
|
+
* (LOADED_BY_TOOLS, any `/skills/` folder, the GitHub root files, folder notes
|
|
19
|
+
* — CLAUDE.md / AGENTS.md / CLAUDE.local.md at any depth — plus
|
|
20
|
+
* `loadedByTools` prefixes) are entry points, never orphans. `history`
|
|
21
|
+
* prefixes and `records` docs are never walked: a link into one is an edge
|
|
22
|
+
* and the doc a leaf node, but its own links pull nothing in; they are never
|
|
23
|
+
* orphans either.
|
|
24
|
+
* - scanMdNetwork(cwd, deps) — thin I/O wrapper: one `git ls-files -z`, at
|
|
17
25
|
* most MAX_DOCS docs read (the root, then breadth-first along its links, then
|
|
18
26
|
* the rest shallowest-first, so the cap drops unreachable docs before reachable
|
|
19
27
|
* ones), at most MAX_DOC_BYTES each, READ_BATCH reads in parallel, async fs only.
|
|
20
|
-
*
|
|
28
|
+
* A link target git does not track is a broken link in every checkout; it is
|
|
29
|
+
* flagged localOnly when it is gitignored (one `git check-ignore --no-index`
|
|
30
|
+
* per 100 targets) or present on disk here.
|
|
31
|
+
* Optional `mdNetwork.loadedByTools` / `mdNetwork.history` string-prefix
|
|
32
|
+
* arrays come from the project's tracked .session-manager.json.
|
|
21
33
|
* It also attaches a per-doc census (mdDocCensus.cjs + mdDocRefs.cjs) over
|
|
22
34
|
* every tracked .md: one `git log --max-count=2000 --name-only` (64 MB buffer)
|
|
23
35
|
* for recency, one `git grep -F -o` over non-.md files (16 MB buffer) for code
|
|
24
36
|
* mentions, and at most MAX_REF_PROBES on-disk probes (READ_BATCH at a time)
|
|
25
37
|
* for backticked paths git does not vouch for — unprobed ones count as
|
|
26
|
-
* existing. A
|
|
27
|
-
*
|
|
38
|
+
* existing. A probed-absent path that is gitignored (same check-ignore pass
|
|
39
|
+
* as link targets) is never dead; one naming exactly one tracked file or
|
|
40
|
+
* folder by suffix (`lib/x.ts`) resolves to it, two or more make it an
|
|
41
|
+
* ambiguousRef. A failing git call degrades to a census warning, never an error.
|
|
42
|
+
* `duplicates` is mdDuplicates.findDuplicates over every read doc except
|
|
43
|
+
* history prefixes and record-collection docs; a failure degrades to [] plus
|
|
44
|
+
* a census warning.
|
|
45
|
+
* Docs in record collections (the census heuristic) leave orphans.
|
|
28
46
|
*
|
|
29
47
|
* Followed — every reference an agent follows, matching the maintaining-md-network
|
|
30
48
|
* skill's link convention: inline `[text](target)` links (images too) and
|
|
@@ -43,15 +61,16 @@
|
|
|
43
61
|
const fs = require('node:fs');
|
|
44
62
|
const path = require('node:path');
|
|
45
63
|
const { execGit } = require('./git/gitExec.cjs');
|
|
46
|
-
const { censusMdDocs, parseGitLog,
|
|
47
|
-
const {
|
|
64
|
+
const { censusMdDocs, parseGitLog, recordCollectionDocs, rootMapDoc, mapRoots } = require('./mdDocCensus.cjs');
|
|
65
|
+
const { assessRefs, suffixIndex, mentionPatterns, parseMentions } = require('./mdDocRefs.cjs');
|
|
66
|
+
// The link convention (links, imports, backticked paths) is written once, in the skill's mdLinks.cjs.
|
|
67
|
+
const { linksOf, importsOf, backtickPaths, toRepoPath } = require('../../../plugins/session-manager-dev/skills/maintaining-md-network/scripts/mdLinks.cjs');
|
|
68
|
+
// Called through the module object (mdDuplicates.findDuplicates), so a test can stub it.
|
|
69
|
+
const mdDuplicates = require('../../../plugins/session-manager-dev/skills/maintaining-md-network/scripts/mdDuplicates.cjs');
|
|
48
70
|
|
|
49
|
-
const ROOT_DOC = 'CLAUDE.md';
|
|
50
71
|
const MAX_DOCS = 2000;
|
|
51
72
|
const MAX_DOC_BYTES = 256 * 1024;
|
|
52
73
|
const READ_BATCH = 32;
|
|
53
|
-
/** Longest link destination or title scanned, so a pathological line stays linear. */
|
|
54
|
-
const MAX_LINK_CHARS = 1024;
|
|
55
74
|
/** `git ls-files -z` stdout cap; node's 1 MB default overflows near 15k tracked files. */
|
|
56
75
|
const LS_FILES_MAX_BUFFER = 64 * 1024 * 1024;
|
|
57
76
|
const LOG_ARGS = ['log', '--max-count=2000', '--no-renames', '--format=%x01%ct', '--name-only', '-z'];
|
|
@@ -59,36 +78,50 @@ const LOG_MAX_BUFFER = 64 * 1024 * 1024;
|
|
|
59
78
|
const GREP_MAX_BUFFER = 16 * 1024 * 1024;
|
|
60
79
|
/** On-disk probes for backticked paths git does not vouch for, per scan. */
|
|
61
80
|
const MAX_REF_PROBES = 500;
|
|
81
|
+
const CLAUDE_DOC = 'CLAUDE.md';
|
|
62
82
|
const OWNER_NAMES = new Set(['CLAUDE.md', 'README.md', 'AGENTS.md']);
|
|
63
83
|
const MISSING_CODES = new Set(['ENOENT', 'ENOTDIR', 'EISDIR']);
|
|
64
84
|
const DETAIL_MISSING = 'The link target doesn’t exist.';
|
|
65
85
|
const DETAIL_ESCAPES = 'The link target is outside the project.';
|
|
86
|
+
const DETAIL_LOCAL_ONLY = 'Works only on this machine: the target is not tracked in git.';
|
|
87
|
+
const CONFIG_FILE = '.session-manager.json';
|
|
88
|
+
const CHECK_IGNORE_BATCH = 100;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Doc prefixes a tool loads by name (agents, plugin packs, seeds, GitHub files):
|
|
92
|
+
* entry points, never orphans. The doc-hierarchy lint's EXEMPT_PREFIXES calls
|
|
93
|
+
* these "tool-loaded", "seeded" and "github".
|
|
94
|
+
*/
|
|
95
|
+
const LOADED_BY_TOOLS = Object.freeze(['.claude/', '.github/', 'plugins/', 'src/seed/']);
|
|
96
|
+
/** Root files GitHub (or a contributor) opens by name. */
|
|
97
|
+
const LOADED_BY_TOOLS_ROOT_FILES = new Set(['README.md', 'CONTRIBUTING.md', 'CODE_OF_CONDUCT.md', 'SECURITY.md', 'CHANGELOG.md']);
|
|
98
|
+
/**
|
|
99
|
+
* Folder notes: the agent CLI loads one by name when it works in that folder (a CLAUDE.md is
|
|
100
|
+
* often just the `@AGENTS.md` stub — mdLinks isAgentsStub), so at any depth each is an entry point.
|
|
101
|
+
*/
|
|
102
|
+
const FOLDER_NOTE_NAMES = new Set(['CLAUDE.md', 'AGENTS.md', 'CLAUDE.local.md']);
|
|
66
103
|
|
|
67
104
|
const MD_RE = /\.md$/i;
|
|
68
|
-
const SCHEME_RE = /^[a-z][a-z0-9+.-]*:/i;
|
|
69
|
-
const FENCE_RE = /^\s*(`{3,}|~{3,})(.*)$/;
|
|
70
|
-
const INLINE_CODE_RE = /(`+).{0,1024}?\1/g;
|
|
71
|
-
const AUTOLINK_RE = /<([^<>\s]{1,1024})>/g;
|
|
72
105
|
|
|
73
106
|
/**
|
|
74
|
-
* @typedef {
|
|
75
|
-
* @typedef {
|
|
107
|
+
* @typedef {import('../../../plugins/session-manager-dev/skills/maintaining-md-network/scripts/mdLinks.cjs').MdFile} MdFile
|
|
108
|
+
* @typedef {import('../../../plugins/session-manager-dev/skills/maintaining-md-network/scripts/mdLinks.cjs').MdLink} MdLink
|
|
76
109
|
* @typedef {MdLink & { kind: 'link' | 'path' | 'import' }} MdRef a followed reference; only `link` ones can be broken
|
|
77
110
|
* `target` as written (fragment/query stripped); `to` resolved repo-relative
|
|
78
111
|
* @typedef {'hub' | 'owner' | 'doc'} MdNetworkNodeType
|
|
79
112
|
* @typedef {{ id: string, path: string, depth: number, type: MdNetworkNodeType, linksOut: number, linkedFrom: number }} MdNetworkNode
|
|
80
113
|
* @typedef {{ from: string, to: string, broken?: boolean }} MdNetworkEdge
|
|
81
114
|
* `to` of a broken edge is the missing (or escaping) resolved target, not a node
|
|
82
|
-
* @typedef {{ type: 'broken-link', file: string, line: number, target: string, to: string, where: string, detail: string }} MdNetworkBrokenLink
|
|
115
|
+
* @typedef {{ type: 'broken-link', file: string, line: number, target: string, to: string, where: string, detail: string, localOnly?: boolean }} MdNetworkBrokenLink
|
|
116
|
+
* `localOnly`: the target is not tracked but is gitignored or on disk here
|
|
83
117
|
* @typedef {{ type: 'orphan', where: string, count: number, paths: string[], detail: string }} MdNetworkOrphan
|
|
84
118
|
* @typedef {MdNetworkBrokenLink | MdNetworkOrphan} MdNetworkFinding
|
|
85
119
|
* @typedef {{ ok: true, root: string, nodes: MdNetworkNode[], edges: MdNetworkEdge[], findings: MdNetworkFinding[], docs: number, links: number, scopes: string[] }} MdLinkNetwork
|
|
86
120
|
* @typedef {MdLinkNetwork | { ok: false, error: string }} MdLinkNetworkResult
|
|
87
121
|
* @typedef {import('./mdDocCensus.cjs').MdCensus} MdCensus
|
|
88
|
-
* @typedef {
|
|
122
|
+
* @typedef {{ a: string, b: string, sharedRuns: number, samples: string[], copy?: true }} MdDuplicatePair
|
|
123
|
+
* @typedef {MdLinkNetwork & { census: MdCensus, duplicates: MdDuplicatePair[] }} MdNetwork
|
|
89
124
|
* @typedef {MdNetwork | { ok: false, error: string }} MdNetworkResult
|
|
90
|
-
* @typedef {{ recordFolders?: string[], docFolders?: string[] }} ScanOpts
|
|
91
|
-
* @typedef {{ fence: string, inComment: boolean }} LineState an open code fence / HTML comment
|
|
92
125
|
*/
|
|
93
126
|
|
|
94
127
|
/**
|
|
@@ -115,11 +148,6 @@ function byCodeUnit(a, b) {
|
|
|
115
148
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
116
149
|
}
|
|
117
150
|
|
|
118
|
-
/** Repo-relative POSIX form: no `./` or trailing `/`; `.` is the root folder. @param {string} p */
|
|
119
|
-
function toRepoPath(p) {
|
|
120
|
-
return path.posix.normalize(p).replace(/\/+$/, '') || '.';
|
|
121
|
-
}
|
|
122
|
-
|
|
123
151
|
/** @param {string} p @returns {string} the top-level folder, or `.` for a root-level path */
|
|
124
152
|
function topFolder(p) {
|
|
125
153
|
const slash = p.indexOf('/');
|
|
@@ -140,168 +168,6 @@ function foldersOf(paths) {
|
|
|
140
168
|
return folders;
|
|
141
169
|
}
|
|
142
170
|
|
|
143
|
-
/** @param {string} s @param {number} i @returns {number} the first index at or after `i` that is not a space/tab */
|
|
144
|
-
function skipBlanks(s, i) {
|
|
145
|
-
while (s[i] === ' ' || s[i] === '\t') i += 1;
|
|
146
|
-
return i;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/** @param {string} s @param {string} ch @param {number} from @returns {number} `ch`'s index within MAX_LINK_CHARS, or -1 */
|
|
150
|
-
function indexWithin(s, ch, from) {
|
|
151
|
-
const i = s.slice(from, from + MAX_LINK_CHARS).indexOf(ch);
|
|
152
|
-
return i === -1 ? -1 : from + i;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Parses an inline link's destination (+ optional title) starting just after `](`.
|
|
157
|
-
* @param {string} s
|
|
158
|
-
* @param {number} start
|
|
159
|
-
* @returns {{ target: string, end: number } | null} the raw target and the index after `)`
|
|
160
|
-
*/
|
|
161
|
-
function parseDestination(s, start) {
|
|
162
|
-
let j = skipBlanks(s, start);
|
|
163
|
-
let target;
|
|
164
|
-
if (s[j] === '<') {
|
|
165
|
-
const close = indexWithin(s, '>', j + 1);
|
|
166
|
-
if (close === -1) return null;
|
|
167
|
-
target = s.slice(j + 1, close);
|
|
168
|
-
j = close + 1;
|
|
169
|
-
} else {
|
|
170
|
-
const from = j;
|
|
171
|
-
const limit = Math.min(s.length, from + MAX_LINK_CHARS);
|
|
172
|
-
let depth = 0;
|
|
173
|
-
for (; j < limit; j += 1) {
|
|
174
|
-
const c = s[j];
|
|
175
|
-
if (c === ' ' || c === '\t') break;
|
|
176
|
-
if (c === '(') depth += 1;
|
|
177
|
-
else if (c === ')') {
|
|
178
|
-
if (depth === 0) break;
|
|
179
|
-
depth -= 1;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
if (j === limit && j < s.length) return null;
|
|
183
|
-
target = s.slice(from, j);
|
|
184
|
-
}
|
|
185
|
-
j = skipBlanks(s, j);
|
|
186
|
-
const quote = s[j];
|
|
187
|
-
if (quote === '"' || quote === "'" || quote === '(') {
|
|
188
|
-
const close = indexWithin(s, quote === '(' ? ')' : quote, j + 1);
|
|
189
|
-
if (close === -1) return null;
|
|
190
|
-
j = skipBlanks(s, close + 1);
|
|
191
|
-
}
|
|
192
|
-
return s[j] === ')' ? { target, end: j + 1 } : null;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/** Raw targets of one line's inline links, then of its `<x.md>` autolinks. @param {string} s @returns {string[]} */
|
|
196
|
-
function lineTargets(s) {
|
|
197
|
-
/** @type {string[]} */
|
|
198
|
-
const targets = [];
|
|
199
|
-
let outside = ''; // the line minus its inline links, so `[x](<a.md>)` is not also an autolink
|
|
200
|
-
let last = 0;
|
|
201
|
-
for (let i = s.indexOf(']('); i !== -1; ) {
|
|
202
|
-
const dest = parseDestination(s, i + 2);
|
|
203
|
-
if (dest) {
|
|
204
|
-
targets.push(dest.target);
|
|
205
|
-
outside += `${s.slice(last, i)} `;
|
|
206
|
-
last = dest.end;
|
|
207
|
-
i = s.indexOf('](', dest.end);
|
|
208
|
-
} else {
|
|
209
|
-
i = s.indexOf('](', i + 2);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
outside += s.slice(last);
|
|
213
|
-
for (const m of outside.matchAll(AUTOLINK_RE)) {
|
|
214
|
-
if (MD_RE.test(m[1].replace(/[?#].*$/, ''))) targets.push(m[1]);
|
|
215
|
-
}
|
|
216
|
-
return targets;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/** Drops HTML comments (which may span lines) from one line. @param {string} line @param {LineState} state */
|
|
220
|
-
function stripComments(line, state) {
|
|
221
|
-
let out = '';
|
|
222
|
-
let i = 0;
|
|
223
|
-
while (i < line.length) {
|
|
224
|
-
if (state.inComment) {
|
|
225
|
-
const close = line.indexOf('-->', i);
|
|
226
|
-
if (close === -1) return out;
|
|
227
|
-
state.inComment = false;
|
|
228
|
-
i = close + 3;
|
|
229
|
-
} else {
|
|
230
|
-
const open = line.indexOf('<!--', i);
|
|
231
|
-
if (open === -1) return out + line.slice(i);
|
|
232
|
-
out += `${line.slice(i, open)} `;
|
|
233
|
-
state.inComment = true;
|
|
234
|
-
i = open + 4;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
return out;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/** The part of one line that can hold links. @param {string} line @param {LineState} state @returns {string} */
|
|
241
|
-
function visibleText(line, state) {
|
|
242
|
-
const fence = state.inComment ? null : FENCE_RE.exec(line);
|
|
243
|
-
if (state.fence) {
|
|
244
|
-
if (fence && fence[1][0] === state.fence[0] && fence[1].length >= state.fence.length && fence[2].trim() === '') {
|
|
245
|
-
state.fence = '';
|
|
246
|
-
}
|
|
247
|
-
return '';
|
|
248
|
-
}
|
|
249
|
-
if (fence && !(fence[1][0] === '`' && fence[2].includes('`'))) {
|
|
250
|
-
state.fence = fence[1];
|
|
251
|
-
return '';
|
|
252
|
-
}
|
|
253
|
-
return stripComments(line.replace(INLINE_CODE_RE, ' '), state);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* The followed link for one raw target written in doc `from`, or null when ignored.
|
|
258
|
-
* @param {string} from @param {string} raw @param {number} line 1-based
|
|
259
|
-
* @returns {MdLink | null}
|
|
260
|
-
*/
|
|
261
|
-
function followLink(from, raw, line) {
|
|
262
|
-
const written = raw.trim();
|
|
263
|
-
if (written === '' || written[0] === '#' || written[0] === '/' || SCHEME_RE.test(written)) return null;
|
|
264
|
-
const target = written.replace(/[?#].*$/, '');
|
|
265
|
-
if (target === '') return null;
|
|
266
|
-
let decoded = target;
|
|
267
|
-
try {
|
|
268
|
-
decoded = decodeURIComponent(target);
|
|
269
|
-
} catch {
|
|
270
|
-
// a stray `%` is part of the file name
|
|
271
|
-
}
|
|
272
|
-
const to = toRepoPath(path.posix.join(path.posix.dirname(from), decoded));
|
|
273
|
-
return { line, target, to, escapes: to === '..' || to.startsWith('../') };
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* Memo for linksOf: scanMdNetwork walks the docs before buildMdNetwork does, so
|
|
278
|
-
* each doc is parsed once. File objects are treated as immutable.
|
|
279
|
-
* @type {WeakMap<MdFile, MdLink[]>}
|
|
280
|
-
*/
|
|
281
|
-
const linkMemo = new WeakMap();
|
|
282
|
-
|
|
283
|
-
/** A doc's followed links, in document order. @param {MdFile} file @returns {MdLink[]} */
|
|
284
|
-
function linksOf(file) {
|
|
285
|
-
const memo = linkMemo.get(file);
|
|
286
|
-
if (memo) return memo;
|
|
287
|
-
const from = toRepoPath(file.path);
|
|
288
|
-
/** @type {MdLink[]} */
|
|
289
|
-
const links = [];
|
|
290
|
-
/** @type {LineState} */
|
|
291
|
-
const state = { fence: '', inComment: false };
|
|
292
|
-
const lines = file.text.split(/\r?\n/);
|
|
293
|
-
for (let n = 0; n < lines.length; n += 1) {
|
|
294
|
-
const visible = visibleText(lines[n], state);
|
|
295
|
-
if (visible === '') continue;
|
|
296
|
-
for (const raw of lineTargets(visible)) {
|
|
297
|
-
const link = followLink(from, raw, n + 1);
|
|
298
|
-
if (link) links.push(link);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
linkMemo.set(file, links);
|
|
302
|
-
return links;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
171
|
/**
|
|
306
172
|
* Memo for refsOf's doc-independent parse: backticked `.md` tokens and `@` imports.
|
|
307
173
|
* @type {WeakMap<MdFile, { tokens: string[], imports: string[] }>}
|
|
@@ -341,6 +207,21 @@ function refsOf(file, isDoc) {
|
|
|
341
207
|
return refs;
|
|
342
208
|
}
|
|
343
209
|
|
|
210
|
+
/** @param {unknown} list @returns {string[]} the non-empty strings of `list` */
|
|
211
|
+
function prefixList(list) {
|
|
212
|
+
return Array.isArray(list) ? list.filter((p) => typeof p === 'string' && p !== '') : [];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @param {string} p @param {readonly string[]} prefixes */
|
|
216
|
+
function hasPrefix(p, prefixes) {
|
|
217
|
+
return prefixes.some((pre) => p.startsWith(pre));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** A doc a tool loads by name (incl. a folder note at any depth). @param {string} p @param {readonly string[]} extra */
|
|
221
|
+
function isLoadedByTools(p, extra) {
|
|
222
|
+
return LOADED_BY_TOOLS_ROOT_FILES.has(p) || FOLDER_NOTE_NAMES.has(path.posix.basename(p)) || p.startsWith('skills/') || p.includes('/skills/') || hasPrefix(p, LOADED_BY_TOOLS) || hasPrefix(p, extra);
|
|
223
|
+
}
|
|
224
|
+
|
|
344
225
|
/** @param {string} id @param {number} depth @returns {MdNetworkNodeType} */
|
|
345
226
|
function nodeType(id, depth) {
|
|
346
227
|
if (depth === 0) return 'hub';
|
|
@@ -363,25 +244,32 @@ function missingRoot(root) {
|
|
|
363
244
|
* Pure: the link network reachable from `root` over in-memory docs.
|
|
364
245
|
* `exists(relPath)` vouches for link targets that are neither one of `files`
|
|
365
246
|
* nor a folder holding one (code files, docs left unread, untracked files);
|
|
366
|
-
* without it such targets are missing. `
|
|
367
|
-
*
|
|
368
|
-
*
|
|
247
|
+
* without it such targets are missing. A missing target `localOnly` vouches
|
|
248
|
+
* for (untracked here, e.g. gitignored) is a broken link flagged localOnly.
|
|
249
|
+
* `records` (record-collection docs) and `history` prefixes are never walked
|
|
250
|
+
* and never orphans; docs a tool loads by name (LOADED_BY_TOOLS + `loadedByTools`
|
|
251
|
+
* prefixes) are never orphans.
|
|
252
|
+
* @param {{ files?: MdFile[], root?: string, exists?: (relPath: string) => boolean, localOnly?: (relPath: string) => boolean, records?: ReadonlySet<string>, loadedByTools?: string[], history?: string[] }} [input]
|
|
369
253
|
* @returns {MdLinkNetworkResult}
|
|
370
254
|
*/
|
|
371
|
-
function buildMdNetwork({ files = [], root
|
|
255
|
+
function buildMdNetwork({ files = [], root, exists, localOnly, records, loadedByTools, history } = {}) {
|
|
372
256
|
try {
|
|
373
|
-
const rootPath = toRepoPath(root);
|
|
374
257
|
/** @type {Map<string, MdFile>} */
|
|
375
258
|
const docs = new Map();
|
|
376
259
|
for (const file of files) {
|
|
377
260
|
if (file && typeof file.path === 'string' && typeof file.text === 'string') docs.set(toRepoPath(file.path), file);
|
|
378
261
|
}
|
|
262
|
+
const rootPath = toRepoPath(root ?? rootMapDoc((p) => docs.has(p)));
|
|
379
263
|
if (!docs.has(rootPath)) return missingRoot(rootPath);
|
|
380
264
|
const folders = foldersOf(docs.keys());
|
|
381
265
|
/** @param {string} p */
|
|
382
266
|
const present = (p) => p === '.' || docs.has(p) || folders.has(p) || (exists !== undefined && exists(p) === true);
|
|
383
267
|
/** @param {string} p */
|
|
384
268
|
const isDoc = (p) => docs.has(p);
|
|
269
|
+
const historyPrefixes = prefixList(history);
|
|
270
|
+
const toolPrefixes = prefixList(loadedByTools);
|
|
271
|
+
/** A doc whose own links are never followed. @param {string} p */
|
|
272
|
+
const isLeaf = (p) => p !== rootPath && ((records !== undefined && records.has(p)) || hasPrefix(p, historyPrefixes));
|
|
385
273
|
|
|
386
274
|
/** @type {Map<string, number>} */
|
|
387
275
|
const depth = new Map([[rootPath, 0]]);
|
|
@@ -397,10 +285,20 @@ function buildMdNetwork({ files = [], root = ROOT_DOC, exists, records } = {}) {
|
|
|
397
285
|
/** @type {Map<string, number>} */
|
|
398
286
|
const linkedFrom = new Map();
|
|
399
287
|
|
|
288
|
+
// A root CLAUDE.md beside an AGENTS.md root is part of the map: the hub reaches it.
|
|
289
|
+
for (const stub of mapRoots(rootPath, (p) => docs.has(p)).slice(1)) {
|
|
290
|
+
edgeKeys.add(`${rootPath}\0${stub}`);
|
|
291
|
+
edges.push({ from: rootPath, to: stub });
|
|
292
|
+
linksOut.set(rootPath, 1);
|
|
293
|
+
linkedFrom.set(stub, 1);
|
|
294
|
+
depth.set(stub, 1);
|
|
295
|
+
order.push(stub);
|
|
296
|
+
}
|
|
297
|
+
|
|
400
298
|
for (let i = 0; i < order.length; i += 1) {
|
|
401
299
|
const from = order[i];
|
|
402
300
|
const file = docs.get(from);
|
|
403
|
-
if (!file) continue;
|
|
301
|
+
if (!file || isLeaf(from)) continue;
|
|
404
302
|
const nextDepth = (depth.get(from) ?? 0) + 1;
|
|
405
303
|
for (const link of refsOf(file, isDoc)) {
|
|
406
304
|
const broken = link.kind === 'link' && (link.escapes || !present(link.to));
|
|
@@ -408,8 +306,12 @@ function buildMdNetwork({ files = [], root = ROOT_DOC, exists, records } = {}) {
|
|
|
408
306
|
const where = `${from}:${link.line} → ${link.target}`;
|
|
409
307
|
if (!findingKeys.has(where)) {
|
|
410
308
|
findingKeys.add(where);
|
|
411
|
-
const
|
|
412
|
-
|
|
309
|
+
const local = !link.escapes && localOnly !== undefined && localOnly(link.to) === true;
|
|
310
|
+
const detail = link.escapes ? DETAIL_ESCAPES : local ? DETAIL_LOCAL_ONLY : DETAIL_MISSING;
|
|
311
|
+
/** @type {MdNetworkBrokenLink} */
|
|
312
|
+
const finding = { type: 'broken-link', file: from, line: link.line, target: link.target, to: link.to, where, detail };
|
|
313
|
+
if (local) finding.localOnly = true;
|
|
314
|
+
findings.push(finding);
|
|
413
315
|
}
|
|
414
316
|
} else if (link.to === from || !docs.has(link.to)) {
|
|
415
317
|
continue; // a self link, or a present target that is not a doc: no edge
|
|
@@ -435,7 +337,7 @@ function buildMdNetwork({ files = [], root = ROOT_DOC, exists, records } = {}) {
|
|
|
435
337
|
/** @type {Map<string, string[]>} */
|
|
436
338
|
const orphans = new Map();
|
|
437
339
|
for (const p of docs.keys()) {
|
|
438
|
-
if (depth.has(p) || (
|
|
340
|
+
if (depth.has(p) || isLeaf(p) || isLoadedByTools(p, toolPrefixes)) continue;
|
|
439
341
|
const top = topFolder(p);
|
|
440
342
|
const group = orphans.get(top);
|
|
441
343
|
if (group) group.push(p);
|
|
@@ -482,6 +384,36 @@ function pathExists(absPath) {
|
|
|
482
384
|
);
|
|
483
385
|
}
|
|
484
386
|
|
|
387
|
+
/**
|
|
388
|
+
* The project's optional mdNetwork prefixes from its tracked .session-manager.json.
|
|
389
|
+
* @param {string} cwd @param {boolean} tracked
|
|
390
|
+
* @returns {Promise<{ loadedByTools: string[], history: string[], warning: string | null }>}
|
|
391
|
+
*/
|
|
392
|
+
async function readNetworkConfig(cwd, tracked) {
|
|
393
|
+
const none = { loadedByTools: [], history: [], warning: null };
|
|
394
|
+
if (!tracked) return none;
|
|
395
|
+
let text;
|
|
396
|
+
try {
|
|
397
|
+
text = await fs.promises.readFile(path.join(cwd, CONFIG_FILE), 'utf8');
|
|
398
|
+
} catch (err) {
|
|
399
|
+
if (MISSING_CODES.has(String(errorField(err, 'code')))) return none;
|
|
400
|
+
return { ...none, warning: `${CONFIG_FILE} unreadable, mdNetwork settings ignored: ${errorText(err)}` };
|
|
401
|
+
}
|
|
402
|
+
try {
|
|
403
|
+
const parsed = JSON.parse(text);
|
|
404
|
+
const net = parsed !== null && typeof parsed === 'object' ? parsed.mdNetwork : undefined;
|
|
405
|
+
if (net === undefined) return none;
|
|
406
|
+
if (net === null || typeof net !== 'object' || Array.isArray(net)) throw new Error('mdNetwork is not an object');
|
|
407
|
+
for (const key of ['loadedByTools', 'history']) {
|
|
408
|
+
const v = net[key];
|
|
409
|
+
if (v !== undefined && !(Array.isArray(v) && v.every((p) => typeof p === 'string'))) throw new Error(`mdNetwork.${key} is not a string array`);
|
|
410
|
+
}
|
|
411
|
+
return { loadedByTools: prefixList(net.loadedByTools), history: prefixList(net.history), warning: null };
|
|
412
|
+
} catch (err) {
|
|
413
|
+
return { ...none, warning: `${CONFIG_FILE} unreadable, mdNetwork settings ignored: ${errorText(err)}` };
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
485
417
|
/** The `fatal:`/`error:` line of a failed git call, else its first line. @param {unknown} err @returns {string} */
|
|
486
418
|
function gitFailure(err) {
|
|
487
419
|
const stderr = errorField(err, 'stderrText');
|
|
@@ -497,10 +429,9 @@ function gitFailure(err) {
|
|
|
497
429
|
* I/O: scans the git project at `cwd` and builds its network. Never throws.
|
|
498
430
|
* @param {string} cwd the project root (`git ls-files` paths are relative to it)
|
|
499
431
|
* @param {ScanDeps} [deps] injectable I/O; defaults are git + async fs
|
|
500
|
-
* @param {ScanOpts} [opts] record-collection overrides for the census
|
|
501
432
|
* @returns {Promise<MdNetworkResult>}
|
|
502
433
|
*/
|
|
503
|
-
async function scanMdNetwork(cwd, deps = {}
|
|
434
|
+
async function scanMdNetwork(cwd, deps = {}) {
|
|
504
435
|
try {
|
|
505
436
|
if (typeof cwd !== 'string' || cwd === '') return { ok: false, error: 'cwd must be a non-empty path' };
|
|
506
437
|
/** @type {NonNullable<ScanDeps['execGit']>} */
|
|
@@ -521,6 +452,8 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
521
452
|
const trackedFolders = foldersOf(tracked);
|
|
522
453
|
const docPaths = tracked.filter((p) => MD_RE.test(p));
|
|
523
454
|
const docSet = new Set(docPaths);
|
|
455
|
+
const config = await readNetworkConfig(cwd, trackedSet.has(CONFIG_FILE));
|
|
456
|
+
const { loadedByTools, history } = config;
|
|
524
457
|
|
|
525
458
|
/** @type {Set<string>} docs whose read was started; never more than MAX_DOCS */
|
|
526
459
|
const attempted = new Set();
|
|
@@ -535,7 +468,7 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
535
468
|
return { path: rel, text: typeof text === 'string' ? text.slice(0, MAX_DOC_BYTES) : '' };
|
|
536
469
|
} catch (err) {
|
|
537
470
|
if (MISSING_CODES.has(String(errorField(err, 'code')))) gone.add(rel);
|
|
538
|
-
else if (rel ===
|
|
471
|
+
else if (rel === rootDoc) throw err;
|
|
539
472
|
return null; // an unreadable doc is skipped; links to it still count as present
|
|
540
473
|
}
|
|
541
474
|
};
|
|
@@ -551,22 +484,34 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
551
484
|
return read;
|
|
552
485
|
};
|
|
553
486
|
|
|
554
|
-
// The root is
|
|
555
|
-
|
|
556
|
-
|
|
487
|
+
// The root map doc is a tracked AGENTS.md, else CLAUDE.md read straight from
|
|
488
|
+
// disk (so an untracked CLAUDE.md still counts). Beside an AGENTS.md root the
|
|
489
|
+
// CLAUDE.md stub is read too: it is part of the map.
|
|
490
|
+
let rootDoc = rootMapDoc((p) => trackedSet.has(p));
|
|
491
|
+
let rootFile = await readOne(rootDoc);
|
|
492
|
+
if (!rootFile && rootDoc !== CLAUDE_DOC) {
|
|
493
|
+
rootDoc = CLAUDE_DOC;
|
|
494
|
+
rootFile = await readOne(rootDoc);
|
|
495
|
+
}
|
|
496
|
+
if (!rootFile) return missingRoot(rootDoc);
|
|
497
|
+
const stubFile = rootDoc === CLAUDE_DOC ? null : await readOne(CLAUDE_DOC);
|
|
498
|
+
const mapFiles = stubFile ? [rootFile, stubFile] : [rootFile];
|
|
499
|
+
/** @param {string} p a root doc of the map (never pruned as history or a record) */
|
|
500
|
+
const isMapRoot = (p) => mapFiles.some((f) => f.path === p);
|
|
557
501
|
|
|
558
502
|
// Breadth-first along links, so the cap never drops a reachable doc for an
|
|
559
503
|
// unreachable one; collect targets git does not vouch for along the way.
|
|
560
|
-
const files = [
|
|
561
|
-
const queued = new Set(
|
|
504
|
+
const files = [...mapFiles];
|
|
505
|
+
const queued = new Set(mapFiles.map((f) => f.path));
|
|
562
506
|
/** @type {Set<string>} */
|
|
563
507
|
const unvouched = new Set();
|
|
564
508
|
/** @param {string} p */
|
|
565
509
|
const isDocPath = (p) => docSet.has(p);
|
|
566
|
-
for (let wave =
|
|
510
|
+
for (let wave = mapFiles; wave.length > 0; ) {
|
|
567
511
|
/** @type {string[]} */
|
|
568
512
|
const next = [];
|
|
569
513
|
for (const file of wave) {
|
|
514
|
+
if (!isMapRoot(file.path) && hasPrefix(file.path, history)) continue; // history docs are never walked
|
|
570
515
|
for (const link of refsOf(file, isDocPath)) {
|
|
571
516
|
if (link.escapes || queued.has(link.to)) continue;
|
|
572
517
|
if (docSet.has(link.to)) {
|
|
@@ -593,9 +538,14 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
593
538
|
files.push(...(await readAll(rest)));
|
|
594
539
|
}
|
|
595
540
|
|
|
596
|
-
// Untracked link targets
|
|
541
|
+
// Untracked link targets: on disk here, or gitignored, they work only on this machine.
|
|
597
542
|
/** @type {Set<string>} */
|
|
598
543
|
const onDisk = new Set();
|
|
544
|
+
/** @type {Set<string>} */
|
|
545
|
+
const ignored = new Set();
|
|
546
|
+
/** @type {string[]} */
|
|
547
|
+
const warnings = [];
|
|
548
|
+
if (config.warning) warnings.push(config.warning);
|
|
599
549
|
const candidates = [...unvouched];
|
|
600
550
|
/** @param {string} rel @returns {Promise<boolean>} */
|
|
601
551
|
const probe = async (rel) => {
|
|
@@ -612,40 +562,8 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
612
562
|
if (hits[k]) onDisk.add(rel);
|
|
613
563
|
});
|
|
614
564
|
}
|
|
615
|
-
|
|
616
565
|
/** @param {string} p */
|
|
617
566
|
const vouched = (p) => !gone.has(p) && (trackedSet.has(p) || trackedFolders.has(p));
|
|
618
|
-
/** @param {string} p */
|
|
619
|
-
const present = (p) => onDisk.has(p) || vouched(p);
|
|
620
|
-
const network = buildMdNetwork({ files, root: ROOT_DOC, exists: present });
|
|
621
|
-
if (!network.ok) return network;
|
|
622
|
-
|
|
623
|
-
// ── Census ──
|
|
624
|
-
/** @type {string[]} */
|
|
625
|
-
const warnings = [];
|
|
626
|
-
|
|
627
|
-
// Recency: one capped git log; newest first, so the first sighting wins.
|
|
628
|
-
/** @type {import('./mdDocCensus.cjs').GitLogEntry[]} */
|
|
629
|
-
let log = [];
|
|
630
|
-
try {
|
|
631
|
-
const out = String(await runGit(cwd, LOG_ARGS, { maxBuffer: LOG_MAX_BUFFER }));
|
|
632
|
-
log = parseGitLog(out);
|
|
633
|
-
if (log.length === 0 && out.trim() !== '') warnings.push('git history unavailable: unparseable git log output');
|
|
634
|
-
} catch (err) {
|
|
635
|
-
warnings.push(`git history unavailable: ${gitFailure(err)}`);
|
|
636
|
-
}
|
|
637
|
-
/** @type {Map<string, number>} */
|
|
638
|
-
const lastCommit = new Map();
|
|
639
|
-
for (const entry of log) {
|
|
640
|
-
for (const f of entry.files) if (!lastCommit.has(f)) lastCommit.set(f, entry.ms);
|
|
641
|
-
}
|
|
642
|
-
/** @param {string} p */
|
|
643
|
-
const lastCommitOf = (p) => lastCommit.get(p) ?? null;
|
|
644
|
-
|
|
645
|
-
/** @type {Map<string, { depth: number, linkedFrom: number }>} */
|
|
646
|
-
const reachable = new Map(network.nodes.map((n) => [n.path, { depth: n.depth, linkedFrom: n.linkedFrom }]));
|
|
647
|
-
/** @type {Map<string, MdFile>} */
|
|
648
|
-
const readByPath = new Map(files.map((f) => [f.path, f]));
|
|
649
567
|
|
|
650
568
|
// Backticked paths git does not vouch for: probed on disk, capped; the rest count as existing.
|
|
651
569
|
/** @type {Map<string, boolean>} */
|
|
@@ -653,6 +571,8 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
653
571
|
/** @type {string[]} */
|
|
654
572
|
const toProbe = [];
|
|
655
573
|
const queuedProbes = new Set();
|
|
574
|
+
/** @type {Set<string>} probed forms of `folder/` tokens */
|
|
575
|
+
const folderRefs = new Set();
|
|
656
576
|
/** @type {Map<MdFile, string[]>} */
|
|
657
577
|
const tokensOf = new Map();
|
|
658
578
|
for (const file of files) {
|
|
@@ -668,6 +588,7 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
668
588
|
if (onDisk.has(rel) || queuedProbes.has(rel) || toProbe.length >= MAX_REF_PROBES) continue;
|
|
669
589
|
queuedProbes.add(rel);
|
|
670
590
|
toProbe.push(rel);
|
|
591
|
+
if (tok.endsWith('/')) folderRefs.add(rel);
|
|
671
592
|
}
|
|
672
593
|
}
|
|
673
594
|
}
|
|
@@ -676,19 +597,81 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
676
597
|
const hits = await Promise.all(batch.map(probe));
|
|
677
598
|
batch.forEach((rel, k) => probed.set(rel, hits[k]));
|
|
678
599
|
}
|
|
600
|
+
const notOnDisk = candidates.filter((rel) => !onDisk.has(rel));
|
|
601
|
+
// One check-ignore pass for absent link targets and absent backticked refs;
|
|
602
|
+
// a folder ref is asked with its trailing '/' so dir-only patterns match.
|
|
603
|
+
const toCheck = [...new Set([...notOnDisk, ...toProbe.filter((rel) => probed.get(rel) === false).map((rel) => (folderRefs.has(rel) ? `${rel}/` : rel))])];
|
|
604
|
+
checkIgnore: for (let i = 0; i < toCheck.length; i += CHECK_IGNORE_BATCH) {
|
|
605
|
+
let batch = toCheck.slice(i, i + CHECK_IGNORE_BATCH);
|
|
606
|
+
while (batch.length > 0) {
|
|
607
|
+
try {
|
|
608
|
+
// -z needs --stdin, so paths go as args and come back one per line, unquoted.
|
|
609
|
+
const out = String(await runGit(cwd, ['-c', 'core.quotePath=false', 'check-ignore', '--no-index', '--', ...batch]));
|
|
610
|
+
for (const rel of out.split('\n')) if (rel !== '') ignored.add(toRepoPath(rel));
|
|
611
|
+
break;
|
|
612
|
+
} catch (err) {
|
|
613
|
+
if (errorField(err, 'code') === 1) break;
|
|
614
|
+
// A path under a symlink (a linked node_modules) fails the whole call: drop it, retry the rest.
|
|
615
|
+
const beyond = /pathspec '(.+)' is beyond a symbolic link/.exec(gitFailure(err));
|
|
616
|
+
const drop = beyond ? batch.indexOf(beyond[1]) : -1;
|
|
617
|
+
if (drop >= 0) {
|
|
618
|
+
batch = batch.filter((_, k) => k !== drop);
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
warnings.push(`gitignore check unavailable: ${gitFailure(err)}`);
|
|
622
|
+
break checkIgnore;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/** @param {string} p */
|
|
628
|
+
const present = (p) => vouched(p);
|
|
629
|
+
/** @param {string} p */
|
|
630
|
+
const localOnly = (p) => onDisk.has(p) || ignored.has(p);
|
|
631
|
+
const networkInput = { files, root: rootDoc, exists: present, localOnly, loadedByTools, history };
|
|
632
|
+
const network = buildMdNetwork(networkInput);
|
|
633
|
+
if (!network.ok) return network;
|
|
634
|
+
|
|
635
|
+
// ── Census ──
|
|
636
|
+
|
|
637
|
+
// Recency: one capped git log; newest first, so the first sighting wins.
|
|
638
|
+
/** @type {import('./mdDocCensus.cjs').GitLogEntry[]} */
|
|
639
|
+
let log = [];
|
|
640
|
+
try {
|
|
641
|
+
const out = String(await runGit(cwd, LOG_ARGS, { maxBuffer: LOG_MAX_BUFFER }));
|
|
642
|
+
log = parseGitLog(out);
|
|
643
|
+
if (log.length === 0 && out.trim() !== '') warnings.push('git history unavailable: unparseable git log output');
|
|
644
|
+
} catch (err) {
|
|
645
|
+
warnings.push(`git history unavailable: ${gitFailure(err)}`);
|
|
646
|
+
}
|
|
647
|
+
/** @type {Map<string, number>} */
|
|
648
|
+
const lastCommit = new Map();
|
|
649
|
+
for (const entry of log) {
|
|
650
|
+
for (const f of entry.files) if (!lastCommit.has(f)) lastCommit.set(f, entry.ms);
|
|
651
|
+
}
|
|
652
|
+
/** @param {string} p */
|
|
653
|
+
const lastCommitOf = (p) => lastCommit.get(p) ?? null;
|
|
654
|
+
|
|
655
|
+
/** @type {Map<string, { depth: number, linkedFrom: number }>} */
|
|
656
|
+
const reachable = new Map(network.nodes.map((n) => [n.path, { depth: n.depth, linkedFrom: n.linkedFrom }]));
|
|
657
|
+
/** @type {Map<string, MdFile>} */
|
|
658
|
+
const readByPath = new Map(files.map((f) => [f.path, f]));
|
|
659
|
+
|
|
679
660
|
/** @param {string} p */
|
|
680
661
|
const refExists = (p) => {
|
|
681
|
-
if (present(p)) return true;
|
|
662
|
+
if (present(p) || onDisk.has(p)) return true;
|
|
682
663
|
if (gone.has(p)) return false;
|
|
683
664
|
const hit = probed.get(p);
|
|
684
665
|
return hit === undefined ? true : hit;
|
|
685
666
|
};
|
|
686
667
|
|
|
668
|
+
// Shorthand refs (`lib/x.ts`) resolve by suffix against everything git tracks here.
|
|
669
|
+
const suffixMatches = suffixIndex([...tracked.filter((p) => !gone.has(p)), ...trackedFolders]);
|
|
687
670
|
/** @type {Map<string, import('./mdDocCensus.cjs').MdDocRefs>} */
|
|
688
671
|
const refs = new Map();
|
|
689
672
|
for (const file of files) {
|
|
690
673
|
const linkTargets = linksOf(file)
|
|
691
|
-
.filter((l) => !l.escapes && present(l.to))
|
|
674
|
+
.filter((l) => !l.escapes && (present(l.to) || onDisk.has(l.to)))
|
|
692
675
|
.map((l) => l.to);
|
|
693
676
|
refs.set(
|
|
694
677
|
file.path,
|
|
@@ -699,6 +682,8 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
699
682
|
docMs: lastCommitOf(file.path),
|
|
700
683
|
exists: refExists,
|
|
701
684
|
lastCommitOf,
|
|
685
|
+
ignored: (p) => ignored.has(p),
|
|
686
|
+
suffixMatches,
|
|
702
687
|
}),
|
|
703
688
|
);
|
|
704
689
|
}
|
|
@@ -725,24 +710,34 @@ async function scanMdNetwork(cwd, deps = {}, opts = {}) {
|
|
|
725
710
|
return { path: p, text, lines: text === '' ? 0 : text.split('\n').length, bytes: Buffer.byteLength(text) };
|
|
726
711
|
});
|
|
727
712
|
const census = censusMdDocs({
|
|
728
|
-
root:
|
|
713
|
+
root: rootDoc,
|
|
729
714
|
docs,
|
|
730
715
|
reachable,
|
|
731
716
|
log,
|
|
732
717
|
refs,
|
|
733
718
|
codeMentions,
|
|
734
|
-
recordFolders: opts.recordFolders,
|
|
735
|
-
docFolders: opts.docFolders,
|
|
736
719
|
});
|
|
737
720
|
census.warnings.unshift(...warnings);
|
|
738
721
|
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
722
|
+
// Record-collection docs leave orphans and are never walked, linked or not.
|
|
723
|
+
const records = new Set(recordCollectionDocs(census));
|
|
724
|
+
|
|
725
|
+
// Duplication over living docs only: never history, never record collections.
|
|
726
|
+
/** @type {MdDuplicatePair[]} */
|
|
727
|
+
let duplicates = [];
|
|
728
|
+
try {
|
|
729
|
+
const living = files.filter((f) => isMapRoot(f.path) || (!records.has(f.path) && !hasPrefix(f.path, history)));
|
|
730
|
+
duplicates = mdDuplicates.findDuplicates(living);
|
|
731
|
+
} catch (err) {
|
|
732
|
+
census.warnings.push(`duplicates unavailable: ${errorText(err)}`);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
if (records.size === 0) return { ...network, census, duplicates };
|
|
736
|
+
const pruned = buildMdNetwork({ ...networkInput, records });
|
|
737
|
+
return pruned.ok ? { ...pruned, census, duplicates } : pruned;
|
|
743
738
|
} catch (err) {
|
|
744
739
|
return { ok: false, error: errorText(err) };
|
|
745
740
|
}
|
|
746
741
|
}
|
|
747
742
|
|
|
748
|
-
module.exports = { buildMdNetwork, scanMdNetwork };
|
|
743
|
+
module.exports = { buildMdNetwork, scanMdNetwork, LOADED_BY_TOOLS };
|