connore 0.1.0 → 0.2.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/README.md +58 -46
- package/package.json +36 -33
- package/packs/CONTRIBUTING.md +32 -0
- package/packs/base/catalog/README.md +24 -0
- package/packs/base/catalog/api.md +46 -0
- package/packs/base/catalog/auth.md +46 -0
- package/packs/base/catalog/backend.md +46 -0
- package/packs/base/catalog/blueprints.md +46 -0
- package/packs/base/catalog/components.md +46 -0
- package/packs/base/catalog/database.md +46 -0
- package/packs/base/catalog/docs.md +46 -0
- package/packs/base/catalog/github.md +46 -0
- package/packs/base/catalog/golden.md +46 -0
- package/packs/base/catalog/motion.md +46 -0
- package/packs/base/catalog/plan.md +46 -0
- package/packs/base/catalog/screenshots.md +46 -0
- package/packs/base/catalog/skills.md +46 -0
- package/packs/base/catalog/tokens.md +47 -0
- package/packs/base/planning/START.md +29 -0
- package/packs/base/planning/blueprint-template.md +46 -0
- package/packs/base/planning/plan-template.md +46 -0
- package/packs/base/rules/anti-patterns.md +46 -0
- package/packs/base/rules/code-style.md +46 -0
- package/packs/base/rules/definition-of-done.md +47 -0
- package/packs/base/rules/dependency-policy.md +46 -0
- package/packs/base/rules/error-handling.md +46 -0
- package/packs/base/rules/security-audit.md +47 -0
- package/packs/base/rules/security-baseline.md +46 -0
- package/packs/base/templates/bug-report.md +46 -0
- package/packs/base/templates/feature-spec.md +46 -0
- package/packs/base/templates/handoff.md +46 -0
- package/packs/base/templates/project-brief.md +46 -0
- package/packs/base/workflow/build-loop.md +48 -0
- package/packs/base/workflow/code-review.md +46 -0
- package/packs/base/workflow/debugging.md +47 -0
- package/packs/base/workflow/git-and-commits.md +46 -0
- package/packs/base/workflow/idea-to-spec.md +46 -0
- package/packs/base/workflow/session-handoff.md +46 -0
- package/packs/base/workflow/spec-to-plan.md +46 -0
- package/packs/knowledge/backend/api/conventions.md +21 -0
- package/packs/knowledge/backend/architecture/local-sqlite.md +20 -0
- package/packs/knowledge/backend/authentication/session-and-engine.md +24 -0
- package/packs/knowledge/backend/database/schema.md +21 -0
- package/packs/knowledge/components/custom/ambient-background.md +26 -0
- package/packs/knowledge/components/custom/ambient-background.tsx +12 -0
- package/packs/knowledge/components/custom/corner-frame.md +26 -0
- package/packs/knowledge/components/custom/corner-frame.tsx +48 -0
- package/packs/knowledge/components/custom/cta-button.md +26 -0
- package/packs/knowledge/components/custom/cta-button.tsx +10 -0
- package/packs/knowledge/components/custom/page-transition.md +26 -0
- package/packs/knowledge/components/custom/page-transition.tsx +10 -0
- package/packs/knowledge/components/custom/smooth-scroll.md +26 -0
- package/packs/knowledge/components/custom/smooth-scroll.tsx +32 -0
- package/packs/knowledge/components/motion-effects/README.md +30 -0
- package/packs/knowledge/components/ui-primitives/LICENSE +21 -0
- package/packs/knowledge/components/ui-primitives/README.md +22 -0
- package/packs/knowledge/components/ui-primitives/badge.md +26 -0
- package/packs/knowledge/components/ui-primitives/badge.tsx +48 -0
- package/packs/knowledge/components/ui-primitives/button.md +27 -0
- package/packs/knowledge/components/ui-primitives/button.tsx +66 -0
- package/packs/knowledge/components/ui-primitives/card.md +26 -0
- package/packs/knowledge/components/ui-primitives/card.tsx +102 -0
- package/packs/knowledge/components/ui-primitives/checkbox.md +26 -0
- package/packs/knowledge/components/ui-primitives/checkbox.tsx +32 -0
- package/packs/knowledge/components/ui-primitives/dialog.md +26 -0
- package/packs/knowledge/components/ui-primitives/dialog.tsx +168 -0
- package/packs/knowledge/components/ui-primitives/dropdown-menu.md +26 -0
- package/packs/knowledge/components/ui-primitives/dropdown-menu.tsx +268 -0
- package/packs/knowledge/components/ui-primitives/input.md +27 -0
- package/packs/knowledge/components/ui-primitives/input.tsx +18 -0
- package/packs/knowledge/components/ui-primitives/label.md +26 -0
- package/packs/knowledge/components/ui-primitives/label.tsx +23 -0
- package/packs/knowledge/components/ui-primitives/progress.md +26 -0
- package/packs/knowledge/components/ui-primitives/progress.tsx +30 -0
- package/packs/knowledge/components/ui-primitives/scroll-area.md +26 -0
- package/packs/knowledge/components/ui-primitives/scroll-area.tsx +54 -0
- package/packs/knowledge/components/ui-primitives/select.md +26 -0
- package/packs/knowledge/components/ui-primitives/select.tsx +191 -0
- package/packs/knowledge/components/ui-primitives/separator.md +26 -0
- package/packs/knowledge/components/ui-primitives/separator.tsx +27 -0
- package/packs/knowledge/components/ui-primitives/sonner.md +26 -0
- package/packs/knowledge/components/ui-primitives/sonner.tsx +49 -0
- package/packs/knowledge/components/ui-primitives/switch.md +26 -0
- package/packs/knowledge/components/ui-primitives/switch.tsx +32 -0
- package/packs/knowledge/components/ui-primitives/tabs.md +26 -0
- package/packs/knowledge/components/ui-primitives/tabs.tsx +89 -0
- package/packs/knowledge/components/ui-primitives/textarea.md +26 -0
- package/packs/knowledge/components/ui-primitives/textarea.tsx +17 -0
- package/packs/knowledge/components/ui-primitives/tooltip.md +26 -0
- package/packs/knowledge/components/ui-primitives/tooltip.tsx +56 -0
- package/packs/knowledge/design/aesthetics/app-shell.md +41 -0
- package/packs/knowledge/design/aesthetics/code-neutral.md +40 -0
- package/packs/knowledge/design/aesthetics/match.md +46 -0
- package/packs/knowledge/design/aesthetics/ruled-outline.md +40 -0
- package/packs/knowledge/design/aesthetics/solid-accent.md +43 -0
- package/packs/knowledge/design/animations/motion.md +40 -0
- package/packs/knowledge/design/project-style.md +50 -0
- package/packs/knowledge/design/references/benefit-card-grid.md +27 -0
- package/packs/knowledge/design/references/benefit-card-grid.png +0 -0
- package/packs/knowledge/design/references/cta-centered.md +27 -0
- package/packs/knowledge/design/references/cta-centered.png +0 -0
- package/packs/knowledge/design/references/cta-split-media.md +27 -0
- package/packs/knowledge/design/references/cta-split-media.png +0 -0
- package/packs/knowledge/design/references/dashboard-overview.md +31 -0
- package/packs/knowledge/design/references/dashboard-overview.png +0 -0
- package/packs/knowledge/design/references/faq-split-accordion.md +27 -0
- package/packs/knowledge/design/references/faq-split-accordion.png +0 -0
- package/packs/knowledge/design/references/feature-split-checklist.md +27 -0
- package/packs/knowledge/design/references/feature-split-checklist.png +0 -0
- package/packs/knowledge/design/references/footer-four-column.md +27 -0
- package/packs/knowledge/design/references/footer-four-column.png +0 -0
- package/packs/knowledge/design/references/footer-two-column.md +27 -0
- package/packs/knowledge/design/references/footer-two-column.png +0 -0
- package/packs/knowledge/design/references/hero-accent-curtain.md +27 -0
- package/packs/knowledge/design/references/hero-accent-curtain.png +0 -0
- package/packs/knowledge/design/references/hero-split-media.md +27 -0
- package/packs/knowledge/design/references/hero-split-media.png +0 -0
- package/packs/knowledge/design/references/logo-cloud-grid.md +27 -0
- package/packs/knowledge/design/references/logo-cloud-grid.png +0 -0
- package/packs/knowledge/design/references/logo-row-ruled.md +27 -0
- package/packs/knowledge/design/references/logo-row-ruled.png +0 -0
- package/packs/knowledge/design/references/pricing-three-card.md +27 -0
- package/packs/knowledge/design/references/pricing-three-card.png +0 -0
- package/packs/knowledge/design/references/steps-ruled-grid.md +27 -0
- package/packs/knowledge/design/references/steps-ruled-grid.png +0 -0
- package/packs/knowledge/design/rules.md +35 -0
- package/packs/knowledge/design/token-structure.md +46 -0
- package/packs/knowledge/design/tokens.md +55 -0
- package/packs/knowledge/frontend/README.md +50 -0
- package/packs/knowledge/frontend/SOURCES.md +40 -0
- package/packs/knowledge/frontend/accessibility/focus-and-dialogs.md +47 -0
- package/packs/knowledge/frontend/accessibility/semantics-and-keyboard.md +45 -0
- package/packs/knowledge/frontend/accessibility/wcag-targets.md +43 -0
- package/packs/knowledge/frontend/architecture/agent-context-files.md +43 -0
- package/packs/knowledge/frontend/architecture/app-foundations.md +43 -0
- package/packs/knowledge/frontend/architecture/error-handling.md +47 -0
- package/packs/knowledge/frontend/architecture/feature-modules.md +49 -0
- package/packs/knowledge/frontend/architecture/import-boundaries.md +44 -0
- package/packs/knowledge/frontend/architecture/naming.md +43 -0
- package/packs/knowledge/frontend/architecture/project-structure.md +48 -0
- package/packs/knowledge/frontend/architecture/routing.md +45 -0
- package/packs/knowledge/frontend/architecture/security.md +44 -0
- package/packs/knowledge/frontend/architecture/typescript.md +44 -0
- package/packs/knowledge/frontend/components/composition.md +45 -0
- package/packs/knowledge/frontend/components/data-display.md +47 -0
- package/packs/knowledge/frontend/components/library-choice.md +45 -0
- package/packs/knowledge/frontend/components/variants.md +44 -0
- package/packs/knowledge/frontend/forms/form-architecture.md +49 -0
- package/packs/knowledge/frontend/forms/multi-step.md +43 -0
- package/packs/knowledge/frontend/forms/validation.md +47 -0
- package/packs/knowledge/frontend/layout/responsive.md +41 -0
- package/packs/knowledge/frontend/page-blueprints/app-shell.md +16 -0
- package/packs/knowledge/frontend/page-blueprints/faq-footer.md +22 -0
- package/packs/knowledge/frontend/page-blueprints/feature-grid.md +22 -0
- package/packs/knowledge/frontend/page-blueprints/pricing.md +16 -0
- package/packs/knowledge/frontend/page-blueprints/saas-hero.md +24 -0
- package/packs/knowledge/frontend/patterns/dark-marketing.md +18 -0
- package/packs/knowledge/frontend/patterns/status-pill.md +19 -0
- package/packs/knowledge/frontend/performance/code-splitting.md +47 -0
- package/packs/knowledge/frontend/performance/images-and-vitals.md +44 -0
- package/packs/knowledge/frontend/performance/rendering.md +40 -0
- package/packs/knowledge/frontend/rendering/server-and-client.md +50 -0
- package/packs/knowledge/frontend/state/api-layer.md +49 -0
- package/packs/knowledge/frontend/state/async-ui-states.md +43 -0
- package/packs/knowledge/frontend/state/client-store.md +46 -0
- package/packs/knowledge/frontend/state/server-cache.md +42 -0
- package/packs/knowledge/frontend/state/state-categories.md +50 -0
- package/packs/knowledge/frontend/testing/component-tests.md +44 -0
- package/packs/knowledge/frontend/testing/strategy.md +46 -0
- package/packs/knowledge/inbox/README.md +14 -0
- package/packs/knowledge/workflows/authentication.md +18 -0
- package/packs/knowledge/workflows/crud.md +20 -0
- package/packs/knowledge/workflows/dashboard.md +22 -0
- package/packs/nextjs-fullstack/backend/api-conventions.md +48 -0
- package/packs/nextjs-fullstack/backend/auth-and-roles.md +46 -0
- package/packs/nextjs-fullstack/backend/background-jobs.md +46 -0
- package/packs/nextjs-fullstack/backend/caching.md +46 -0
- package/packs/nextjs-fullstack/backend/database.md +46 -0
- package/packs/nextjs-fullstack/backend/file-uploads.md +46 -0
- package/packs/nextjs-fullstack/backend/logging.md +46 -0
- package/packs/nextjs-fullstack/backend/migrations.md +46 -0
- package/packs/nextjs-fullstack/backend/rate-limiting.md +46 -0
- package/packs/nextjs-fullstack/backend/secrets-and-env.md +46 -0
- package/packs/nextjs-fullstack/backend/validation.md +46 -0
- package/packs/nextjs-fullstack/blueprints/ai-app.md +84 -0
- package/packs/nextjs-fullstack/blueprints/internal-tool.md +83 -0
- package/packs/nextjs-fullstack/blueprints/saas-app.md +83 -0
- package/packs/nextjs-fullstack/build-deploy/ci.md +45 -0
- package/packs/nextjs-fullstack/build-deploy/deploy.md +46 -0
- package/packs/nextjs-fullstack/build-deploy/monitoring.md +45 -0
- package/packs/nextjs-fullstack/build-deploy/performance.md +46 -0
- package/packs/nextjs-fullstack/build-deploy/project-structure.md +45 -0
- package/packs/nextjs-fullstack/build-deploy/release-checklist.md +47 -0
- package/packs/nextjs-fullstack/build-deploy/testing.md +46 -0
- package/packs/nextjs-fullstack/evals/README.md +46 -0
- package/packs/nextjs-fullstack/examples/golden-app/README.md +45 -0
- package/packs/nextjs-fullstack/frontend/accessibility.md +46 -0
- package/packs/nextjs-fullstack/frontend/components.md +46 -0
- package/packs/nextjs-fullstack/frontend/copy.md +46 -0
- package/packs/nextjs-fullstack/frontend/data-fetching.md +46 -0
- package/packs/nextjs-fullstack/frontend/forms.md +46 -0
- package/packs/nextjs-fullstack/frontend/layout.md +46 -0
- package/packs/nextjs-fullstack/frontend/loading-empty-error.md +46 -0
- package/packs/nextjs-fullstack/frontend/motion.md +46 -0
- package/packs/nextjs-fullstack/frontend/responsive.md +46 -0
- package/packs/nextjs-fullstack/frontend/seo.md +47 -0
- package/packs/nextjs-fullstack/frontend/tables.md +46 -0
- package/packs/nextjs-fullstack/frontend/tokens.md +45 -0
- package/packs/nextjs-fullstack/integrations/analytics.md +46 -0
- package/packs/nextjs-fullstack/integrations/email.md +46 -0
- package/packs/nextjs-fullstack/integrations/llm-apis.md +46 -0
- package/packs/nextjs-fullstack/integrations/oauth.md +46 -0
- package/packs/nextjs-fullstack/integrations/object-storage.md +46 -0
- package/packs/nextjs-fullstack/integrations/payments.md +46 -0
- package/packs/nextjs-fullstack/integrations/queues.md +46 -0
- package/packs/nextjs-fullstack/integrations/search.md +46 -0
- package/packs/nextjs-fullstack/integrations/webhooks.md +46 -0
- package/packs/nextjs-fullstack/playbooks/add-ai-feature.md +46 -0
- package/packs/nextjs-fullstack/playbooks/add-auth.md +46 -0
- package/packs/nextjs-fullstack/playbooks/add-billing.md +50 -0
- package/packs/nextjs-fullstack/playbooks/add-crud.md +46 -0
- package/packs/nextjs-fullstack/playbooks/add-email.md +46 -0
- package/packs/nextjs-fullstack/playbooks/add-file-upload.md +46 -0
- package/packs/nextjs-fullstack/playbooks/add-webhook.md +46 -0
- package/packs/nextjs-fullstack/playbooks/ship-to-prod.md +47 -0
- package/plugins/connore/.claude-plugin/plugin.json +10 -0
- package/plugins/connore/hooks/hooks.json +16 -0
- package/plugins/connore/skills/connore-backend/SKILL.md +32 -0
- package/plugins/connore/skills/connore-handoff/SKILL.md +15 -0
- package/plugins/connore/skills/connore-routes/SKILL.md +16 -0
- package/plugins/connore/skills/connore-validate/SKILL.md +15 -0
- package/src/cli.js +377 -90
- package/src/commands/add.js +61 -57
- package/src/commands/catalog.js +21 -0
- package/src/commands/doctor.js +44 -0
- package/src/commands/edit.js +88 -0
- package/src/commands/graph.js +20 -0
- package/src/commands/handoff.js +45 -0
- package/src/commands/hook.js +67 -50
- package/src/commands/init.js +74 -55
- package/src/commands/learn.js +39 -0
- package/src/commands/lint.js +13 -0
- package/src/commands/list.js +10 -10
- package/src/commands/log.js +49 -0
- package/src/commands/memory.js +14 -0
- package/src/commands/packs.js +21 -0
- package/src/commands/plan.js +41 -0
- package/src/commands/reindex.js +8 -8
- package/src/commands/review.js +31 -0
- package/src/commands/routes.js +13 -0
- package/src/commands/search.js +26 -0
- package/src/commands/stats.js +35 -0
- package/src/commands/undo.js +7 -0
- package/src/commands/update.js +20 -14
- package/src/lib/catalog.js +32 -0
- package/src/lib/config.js +67 -0
- package/src/lib/fs-safe.js +36 -11
- package/src/lib/gitlog.js +75 -0
- package/src/lib/graph.js +96 -0
- package/src/lib/ignore.js +47 -0
- package/src/lib/index.js +88 -57
- package/src/lib/lessons.js +173 -0
- package/src/lib/lint.js +185 -0
- package/src/lib/lock.js +47 -0
- package/src/lib/memory.js +49 -0
- package/src/lib/mutate.js +26 -0
- package/src/lib/packs.js +25 -0
- package/src/lib/paths.js +6 -2
- package/src/lib/plan.js +156 -0
- package/src/lib/pointers.js +3 -3
- package/src/lib/prompts.js +66 -0
- package/src/lib/provider.js +27 -0
- package/src/lib/report.js +41 -17
- package/src/lib/routes.js +59 -0
- package/src/lib/run.js +116 -0
- package/src/lib/search.js +64 -0
- package/src/lib/secrets.js +25 -0
- package/src/lib/signals.js +55 -0
- package/src/lib/sync.js +20 -12
- package/src/lib/tokens.js +8 -0
package/src/lib/index.js
CHANGED
|
@@ -1,57 +1,88 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises"
|
|
2
|
-
import { join } from "node:path"
|
|
3
|
-
import { exists, readOptional, walkFiles
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
"
|
|
32
|
-
""
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
1
|
+
import { readFile } from "node:fs/promises"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { exists, readOptional, walkFiles } from "./fs-safe.js"
|
|
4
|
+
import { commitWrite } from "./mutate.js"
|
|
5
|
+
import { parseFrontmatter } from "./frontmatter.js"
|
|
6
|
+
import { ctxPath, fromRoot } from "./paths.js"
|
|
7
|
+
import { estimateTokens } from "./tokens.js"
|
|
8
|
+
|
|
9
|
+
export function entryVisible(data, file, { includeDrafts = false } = {}) {
|
|
10
|
+
if (!data) return false
|
|
11
|
+
const path = file.replace(/\\/g, "/")
|
|
12
|
+
if (path.includes("/_proposed/") || path.includes("/_rejected/") || path.includes("/vault/")) return false
|
|
13
|
+
if (path.includes("/library/catalog/")) return false
|
|
14
|
+
if (data.status === "proposed" || data.status === "rejected") return false
|
|
15
|
+
if (data.status === "draft" && !includeDrafts) return false
|
|
16
|
+
return true
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function collectEntries(root, options = {}) {
|
|
20
|
+
const files = []
|
|
21
|
+
for (const dir of [ctxPath(root, "library"), ctxPath(root, "mine"), ctxPath(root, "memory"), ctxPath(root, "prompts")]) {
|
|
22
|
+
files.push(...(await walkFiles(dir)))
|
|
23
|
+
}
|
|
24
|
+
const processFile = ctxPath(root, "PROCESS.md")
|
|
25
|
+
if (await exists(processFile)) files.push(processFile)
|
|
26
|
+
const startFile = ctxPath(root, "START.md")
|
|
27
|
+
if (await exists(startFile)) files.push(startFile)
|
|
28
|
+
|
|
29
|
+
const entries = []
|
|
30
|
+
for (const file of files) {
|
|
31
|
+
if (!file.endsWith(".md")) continue
|
|
32
|
+
const text = await readFile(file, "utf8")
|
|
33
|
+
const data = parseFrontmatter(text)
|
|
34
|
+
if (!entryVisible(data, file, options)) continue
|
|
35
|
+
if (file.replace(/\\/g, "/").includes("/memory/") && !/^## \d{4}-\d{2}-\d{2}\s*$/m.test(text)) continue
|
|
36
|
+
entries.push({
|
|
37
|
+
path: fromRoot(root, file),
|
|
38
|
+
type: data.type || "",
|
|
39
|
+
tags: Array.isArray(data.tags) ? data.tags.join(", ") : data.tags || "",
|
|
40
|
+
tokens: estimateTokens(text),
|
|
41
|
+
description: data.description || "",
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
// A mine file with the same path under library replaces that library row.
|
|
45
|
+
const chosen = new Map()
|
|
46
|
+
for (const entry of entries) {
|
|
47
|
+
const match = /^\.ctx\/(?:library|mine)\/(.+)$/.exec(entry.path)
|
|
48
|
+
const key = match ? match[1] : entry.path
|
|
49
|
+
const prev = chosen.get(key)
|
|
50
|
+
if (prev && prev.path.includes("/mine/") && !entry.path.includes("/mine/")) continue
|
|
51
|
+
chosen.set(key, entry)
|
|
52
|
+
}
|
|
53
|
+
const visible = [...chosen.values()]
|
|
54
|
+
visible.sort((a, b) => a.path.localeCompare(b.path))
|
|
55
|
+
const start = visible.filter((entry) => entry.path === ".ctx/START.md")
|
|
56
|
+
const rest = visible.filter((entry) => entry.path !== ".ctx/START.md")
|
|
57
|
+
return [...start, ...rest]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function renderIndex(entries) {
|
|
61
|
+
const lines = [
|
|
62
|
+
"# Index",
|
|
63
|
+
"",
|
|
64
|
+
"Read .ctx/START.md before new work. Load only the entries relevant to the task. Prefer text specs over opening images.",
|
|
65
|
+
"",
|
|
66
|
+
"Tokens is a rough size estimate, not an exact count.",
|
|
67
|
+
"",
|
|
68
|
+
"| Path | Type | Tags | Tokens | Description |",
|
|
69
|
+
"| --- | --- | --- | --- | --- |",
|
|
70
|
+
]
|
|
71
|
+
for (const entry of entries) {
|
|
72
|
+
lines.push(`| \`${entry.path}\` | ${entry.type} | ${entry.tags} | ${entry.tokens ?? 0} | ${entry.description} |`)
|
|
73
|
+
}
|
|
74
|
+
lines.push("")
|
|
75
|
+
return lines.join("\n")
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function rebuildIndex(root, report, options = {}) {
|
|
79
|
+
const file = join(root, ".ctx", "INDEX.md")
|
|
80
|
+
const next = renderIndex(await collectEntries(root, options))
|
|
81
|
+
const current = await readOptional(file)
|
|
82
|
+
const currentText = current ? current.toString("utf8") : null
|
|
83
|
+
if (currentText === next) {
|
|
84
|
+
report.add("skipped", ".ctx/INDEX.md")
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
await commitWrite(file, next, report, ".ctx/INDEX.md")
|
|
88
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { exists, readOptional, removeFile, walkFiles } from "./fs-safe.js"
|
|
4
|
+
import { parseFrontmatter } from "./frontmatter.js"
|
|
5
|
+
import { commitWrite } from "./mutate.js"
|
|
6
|
+
import { fromRoot } from "./paths.js"
|
|
7
|
+
|
|
8
|
+
export function lessonFile(lesson) {
|
|
9
|
+
const files = lesson.files ? `\nfiles: ${lesson.files}` : ""
|
|
10
|
+
const flagged = lesson.flagged ? `\nflagged: ${lesson.flagged}` : ""
|
|
11
|
+
const stale = lesson.stale ? `\nstale: true` : ""
|
|
12
|
+
return `---
|
|
13
|
+
title: ${one(lesson.title)}
|
|
14
|
+
type: lesson
|
|
15
|
+
tags: [lesson]
|
|
16
|
+
stack: project
|
|
17
|
+
status: ${lesson.status || "proposed"}
|
|
18
|
+
scope: ${lesson.scope || "project"}
|
|
19
|
+
trigger: ${one(lesson.trigger)}
|
|
20
|
+
mistake: ${one(lesson.mistake)}
|
|
21
|
+
fix: ${one(lesson.fix)}
|
|
22
|
+
evidence: ${one(lesson.evidence)}
|
|
23
|
+
confidence: ${lesson.confidence || "medium"}
|
|
24
|
+
source: ${lesson.source || "agent-inferred"}
|
|
25
|
+
last_seen: ${lesson.lastSeen || ""}${files}${flagged}${stale}
|
|
26
|
+
description: ${one(lesson.mistake)}
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
# ${one(lesson.title)}
|
|
30
|
+
|
|
31
|
+
Watch-out: ${one(lesson.mistake)}
|
|
32
|
+
Fix: ${one(lesson.fix)}
|
|
33
|
+
`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function one(value) {
|
|
37
|
+
return String(value || "").replace(/\s+/g, " ").trim()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function proposedDir(root) {
|
|
41
|
+
return join(root, ".ctx", "mine", "lessons", "_proposed")
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function acceptedDir(root) {
|
|
45
|
+
return join(root, ".ctx", "mine", "lessons")
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function writeProposal(root, id, lesson, report) {
|
|
49
|
+
const file = join(proposedDir(root), `${id}.md`)
|
|
50
|
+
await commitWrite(file, lessonFile({ ...lesson, status: "proposed" }), report, fromRoot(root, file))
|
|
51
|
+
return file
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function readLessonFile(file) {
|
|
55
|
+
const text = await readFile(file, "utf8")
|
|
56
|
+
const data = parseFrontmatter(text)
|
|
57
|
+
if (!data || data.type !== "lesson") return null
|
|
58
|
+
return {
|
|
59
|
+
file,
|
|
60
|
+
id: file.split(/[/\\]/).pop().replace(/\.md$/, ""),
|
|
61
|
+
title: data.title || "",
|
|
62
|
+
status: data.status || "",
|
|
63
|
+
scope: data.scope || "project",
|
|
64
|
+
trigger: data.trigger || "",
|
|
65
|
+
mistake: data.mistake || "",
|
|
66
|
+
fix: data.fix || "",
|
|
67
|
+
evidence: data.evidence || "",
|
|
68
|
+
confidence: data.confidence || "medium",
|
|
69
|
+
source: data.source || "agent-inferred",
|
|
70
|
+
files: data.files || "",
|
|
71
|
+
lastSeen: data.last_seen || "",
|
|
72
|
+
flagged: data.flagged || "",
|
|
73
|
+
stale: data.stale === "true",
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function listProposals(root) {
|
|
78
|
+
const lessons = []
|
|
79
|
+
for (const file of await walkFiles(proposedDir(root))) {
|
|
80
|
+
if (!file.endsWith(".md")) continue
|
|
81
|
+
const lesson = await readLessonFile(file)
|
|
82
|
+
if (lesson?.status === "proposed") lessons.push(lesson)
|
|
83
|
+
}
|
|
84
|
+
return lessons
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function listAccepted(root) {
|
|
88
|
+
const dir = acceptedDir(root)
|
|
89
|
+
const lessons = []
|
|
90
|
+
for (const file of await walkFiles(dir)) {
|
|
91
|
+
if (!file.endsWith(".md")) continue
|
|
92
|
+
if (file.includes(`${join("lessons", "_proposed")}`) || file.includes(`${join("lessons", "_rejected")}`)) continue
|
|
93
|
+
const lesson = await readLessonFile(file)
|
|
94
|
+
if (lesson?.status === "accepted") lessons.push(lesson)
|
|
95
|
+
}
|
|
96
|
+
return lessons
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function moveProposal(root, id, status, report, edits = {}) {
|
|
100
|
+
const source = join(proposedDir(root), `${id}.md`)
|
|
101
|
+
if (!(await exists(source))) throw new Error(`No proposal "${id}".`)
|
|
102
|
+
const current = await readLessonFile(source)
|
|
103
|
+
const lesson = {
|
|
104
|
+
...current,
|
|
105
|
+
...edits,
|
|
106
|
+
status,
|
|
107
|
+
lastSeen: status === "accepted" ? new Date().toISOString().slice(0, 10) : current.lastSeen,
|
|
108
|
+
}
|
|
109
|
+
const destDir = status === "accepted" ? acceptedDir(root) : join(acceptedDir(root), "_rejected")
|
|
110
|
+
const dest = join(destDir, `${id}.md`)
|
|
111
|
+
await commitWrite(dest, lessonFile(lesson), report, fromRoot(root, dest))
|
|
112
|
+
const previous = await readOptional(source)
|
|
113
|
+
if (previous) {
|
|
114
|
+
if (!report.dryRun) await removeFile(source)
|
|
115
|
+
await report.record("removed", fromRoot(root, source), { previous })
|
|
116
|
+
}
|
|
117
|
+
return lesson
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function watchLine(lesson) {
|
|
121
|
+
return `- ${lesson.trigger}: ${lesson.mistake} Fix: ${lesson.fix}`
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function selectLessons(lessons, changed, { limit = 5, budget = 400 } = {}) {
|
|
125
|
+
const paths = changed.map((file) => file.replace(/\\/g, "/"))
|
|
126
|
+
const ranked = []
|
|
127
|
+
for (const lesson of lessons) {
|
|
128
|
+
if (lesson.status !== "accepted" || lesson.stale) continue
|
|
129
|
+
const files = String(lesson.files || "").split(",").map((file) => file.trim()).filter(Boolean)
|
|
130
|
+
const trigger = String(lesson.trigger || "").toLowerCase()
|
|
131
|
+
let score = 0
|
|
132
|
+
for (const path of paths) {
|
|
133
|
+
if (files.some((file) => path === file || path.endsWith(file))) score += 3
|
|
134
|
+
else if (trigger && path.toLowerCase().includes(trigger.replace(/^when editing /, ""))) score += 1
|
|
135
|
+
}
|
|
136
|
+
if (score > 0) ranked.push({ lesson, score })
|
|
137
|
+
}
|
|
138
|
+
ranked.sort((a, b) => b.score - a.score || a.lesson.id.localeCompare(b.lesson.id))
|
|
139
|
+
const chosen = []
|
|
140
|
+
let used = 0
|
|
141
|
+
for (const item of ranked) {
|
|
142
|
+
if (chosen.length >= limit) break
|
|
143
|
+
const cost = Math.max(1, Math.ceil(watchLine(item.lesson).length / 4))
|
|
144
|
+
if (used + cost > budget) continue
|
|
145
|
+
used += cost
|
|
146
|
+
chosen.push(item.lesson)
|
|
147
|
+
}
|
|
148
|
+
return chosen
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function lessonBlock(lessons) {
|
|
152
|
+
if (!lessons.length) return ""
|
|
153
|
+
return `\nWatch-outs\n${lessons.map(watchLine).join("\n")}\n`
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const DAY = 24 * 60 * 60 * 1000
|
|
157
|
+
|
|
158
|
+
export async function markLessonHealth(root, signals, report, now = new Date()) {
|
|
159
|
+
for (const lesson of await listAccepted(root)) {
|
|
160
|
+
const seen = Date.parse(lesson.lastSeen)
|
|
161
|
+
const stale = Number.isFinite(seen) && now.getTime() - seen > 90 * DAY
|
|
162
|
+
const recurring = signals.some((signal) => {
|
|
163
|
+
if (signal.kind !== "failure" || !lesson.mistake) return false
|
|
164
|
+
const at = Date.parse(signal.at)
|
|
165
|
+
if (Number.isFinite(seen) && Number.isFinite(at) && at < seen) return false
|
|
166
|
+
return String(signal.text || "").toLowerCase().includes(lesson.mistake.toLowerCase())
|
|
167
|
+
})
|
|
168
|
+
if (!stale && !recurring) continue
|
|
169
|
+
if (Boolean(lesson.stale) === stale && (lesson.flagged === "recurring") === recurring) continue
|
|
170
|
+
const next = { ...lesson, stale, flagged: recurring ? "recurring" : lesson.flagged }
|
|
171
|
+
await commitWrite(lesson.file, lessonFile(next), report, fromRoot(root, lesson.file))
|
|
172
|
+
}
|
|
173
|
+
}
|
package/src/lib/lint.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { walkFiles } from "./fs-safe.js"
|
|
4
|
+
import { parseFrontmatter } from "./frontmatter.js"
|
|
5
|
+
import { ctxPath, packRoot, toPosix, fromRoot } from "./paths.js"
|
|
6
|
+
import { buildGraph } from "./graph.js"
|
|
7
|
+
import { entryVisible } from "./index.js"
|
|
8
|
+
import { secretHits } from "./secrets.js"
|
|
9
|
+
|
|
10
|
+
// The brief's list, plus the three the shipped packs already use
|
|
11
|
+
// (eval, protocol, workflow). Renaming those would rewrite shipped entries.
|
|
12
|
+
export const ENTRY_TYPES = new Set([
|
|
13
|
+
"guide",
|
|
14
|
+
"rule",
|
|
15
|
+
"anti-pattern",
|
|
16
|
+
"playbook",
|
|
17
|
+
"blueprint",
|
|
18
|
+
"process",
|
|
19
|
+
"template",
|
|
20
|
+
"design-token",
|
|
21
|
+
"component",
|
|
22
|
+
"screenshot",
|
|
23
|
+
"flow",
|
|
24
|
+
"doc",
|
|
25
|
+
"plan",
|
|
26
|
+
"log",
|
|
27
|
+
"eval",
|
|
28
|
+
"protocol",
|
|
29
|
+
"workflow",
|
|
30
|
+
])
|
|
31
|
+
|
|
32
|
+
const STALE_DAYS = 180
|
|
33
|
+
|
|
34
|
+
// Entries that are appended to over the life of the project.
|
|
35
|
+
const LIVING = new Set(["plan", "log"])
|
|
36
|
+
|
|
37
|
+
const REQUIRED = ["title", "type", "tags", "stack", "status", "verified", "description"]
|
|
38
|
+
const HEADINGS = [
|
|
39
|
+
"Use when",
|
|
40
|
+
"Default",
|
|
41
|
+
"Use instead when",
|
|
42
|
+
"Never",
|
|
43
|
+
"Why",
|
|
44
|
+
"Steps",
|
|
45
|
+
"Done when",
|
|
46
|
+
"Example",
|
|
47
|
+
"Common mistakes",
|
|
48
|
+
"Sources",
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
function section(text, heading) {
|
|
52
|
+
const normalized = text.replace(/\r\n/g, "\n")
|
|
53
|
+
const marker = `## ${heading}\n`
|
|
54
|
+
const start = normalized.indexOf(marker)
|
|
55
|
+
if (start === -1) return ""
|
|
56
|
+
const rest = normalized.slice(start + marker.length)
|
|
57
|
+
// A following ### heading is part of this section. The next ## heading ends it.
|
|
58
|
+
const next = rest.search(/\n## (?!#)/)
|
|
59
|
+
return (next === -1 ? rest : rest.slice(0, next)).trim()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function lintText(file, text) {
|
|
63
|
+
const problems = []
|
|
64
|
+
const data = parseFrontmatter(text)
|
|
65
|
+
if (!data) {
|
|
66
|
+
problems.push(`${file}: missing frontmatter`)
|
|
67
|
+
return problems
|
|
68
|
+
}
|
|
69
|
+
if (!data.status) return problems
|
|
70
|
+
for (const key of REQUIRED) {
|
|
71
|
+
if (data[key] === undefined || data[key] === "" && key !== "verified") problems.push(`${file}: frontmatter ${key} is empty`)
|
|
72
|
+
}
|
|
73
|
+
if (data.status !== "draft" && data.status !== "reviewed") problems.push(`${file}: status must be draft or reviewed`)
|
|
74
|
+
if (data.status !== "reviewed") return problems
|
|
75
|
+
if (data.type === "blueprint") {
|
|
76
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(data.verified || "")) problems.push(`${file}: verified must be YYYY-MM-DD`)
|
|
77
|
+
for (const heading of ["Summary", "Capabilities", "Reference architecture", "Data model starter", "Integrations", "Phases", "Gate", "Pitfalls", "Confirm with user", "Entries to load"]) {
|
|
78
|
+
if (!section(text, heading)) problems.push(`${file}: empty or missing ${heading}`)
|
|
79
|
+
}
|
|
80
|
+
return problems
|
|
81
|
+
}
|
|
82
|
+
const lines = text.split(/\r?\n/).length
|
|
83
|
+
if (lines > 60) problems.push(`${file}: ${lines} lines, reviewed entries stay under 60`)
|
|
84
|
+
if (text.includes("TODO")) problems.push(`${file}: reviewed entries cannot contain TODO`)
|
|
85
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(data.verified || "")) problems.push(`${file}: verified must be YYYY-MM-DD`)
|
|
86
|
+
if (!/^license:/im.test(text)) problems.push(`${file}: missing a license line`)
|
|
87
|
+
for (const heading of HEADINGS) {
|
|
88
|
+
if (!section(text, heading)) problems.push(`${file}: empty or missing ${heading}`)
|
|
89
|
+
}
|
|
90
|
+
return problems
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function entryDate(data) {
|
|
94
|
+
const raw = data.verified || data.updated || ""
|
|
95
|
+
return /^\d{4}-\d{2}-\d{2}$/.test(raw) ? raw : null
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function daysSince(date, now) {
|
|
99
|
+
return Math.floor((now - Date.parse(`${date}T00:00:00Z`)) / 86400000)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function lintEntry(path, text, options = {}) {
|
|
103
|
+
const now = options.now ?? Date.now()
|
|
104
|
+
const staleDays = options.staleDays ?? STALE_DAYS
|
|
105
|
+
const errors = []
|
|
106
|
+
const warnings = []
|
|
107
|
+
const data = parseFrontmatter(text)
|
|
108
|
+
if (!data) {
|
|
109
|
+
errors.push(`${path}: missing frontmatter`)
|
|
110
|
+
return { errors, warnings, data: null }
|
|
111
|
+
}
|
|
112
|
+
const description = String(data.description || "").trim()
|
|
113
|
+
if (!description) errors.push(`${path}: description is empty`)
|
|
114
|
+
else if (description === "TODO" || description.length < 12) {
|
|
115
|
+
errors.push(`${path}: description is a stub, say when to load this entry`)
|
|
116
|
+
}
|
|
117
|
+
if (!data.type) errors.push(`${path}: type is empty`)
|
|
118
|
+
else if (!ENTRY_TYPES.has(String(data.type))) {
|
|
119
|
+
errors.push(`${path}: type "${data.type}" is not an allowed entry type`)
|
|
120
|
+
}
|
|
121
|
+
const hits = secretHits(text)
|
|
122
|
+
if (hits.length) errors.push(`${path}: looks like a secret (${hits.join(", ")})`)
|
|
123
|
+
|
|
124
|
+
// plan.md and journal.md grow as the project runs, so neither a length cap
|
|
125
|
+
// nor a verified date applies to them.
|
|
126
|
+
if (!LIVING.has(String(data.type))) {
|
|
127
|
+
const lines = text.split(/\r?\n/).length
|
|
128
|
+
if (lines > (options.maxLines ?? 60)) warnings.push(`${path}: ${lines} lines, entries read better under 60`)
|
|
129
|
+
const date = entryDate(data)
|
|
130
|
+
if (!date) warnings.push(`${path}: no verified date`)
|
|
131
|
+
else if (daysSince(date, now) > staleDays) warnings.push(`${path}: verified ${date}, older than ${staleDays} days`)
|
|
132
|
+
}
|
|
133
|
+
return { errors, warnings, data }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export async function lintProject(root, options = {}) {
|
|
137
|
+
const errors = []
|
|
138
|
+
const warnings = []
|
|
139
|
+
const titles = new Map()
|
|
140
|
+
|
|
141
|
+
const files = []
|
|
142
|
+
for (const dir of ["library", "mine", "memory"]) files.push(...(await walkFiles(ctxPath(root, dir))))
|
|
143
|
+
const present = new Set(files)
|
|
144
|
+
|
|
145
|
+
{
|
|
146
|
+
for (const file of files) {
|
|
147
|
+
const path = toPosix(fromRoot(root, file))
|
|
148
|
+
if (!file.endsWith(".md")) {
|
|
149
|
+
const twin = `${file.replace(/\.[^.]+$/, "")}.md`
|
|
150
|
+
if (!present.has(twin) && !file.endsWith(".gitkeep")) {
|
|
151
|
+
errors.push(`${path}: no text entry beside this file, add ${toPosix(fromRoot(root, twin))}`)
|
|
152
|
+
}
|
|
153
|
+
continue
|
|
154
|
+
}
|
|
155
|
+
const text = await readFile(file, "utf8")
|
|
156
|
+
const data = parseFrontmatter(text)
|
|
157
|
+
// A markdown file with no frontmatter is still a problem to report; only
|
|
158
|
+
// hide the ones entryVisible excludes on purpose (vault, proposals).
|
|
159
|
+
if (data && !entryVisible(data, file, options)) continue
|
|
160
|
+
if (!data && /\/(_proposed|_rejected|vault)\//.test(toPosix(path))) continue
|
|
161
|
+
const result = lintEntry(path, text, options)
|
|
162
|
+
errors.push(...result.errors)
|
|
163
|
+
warnings.push(...result.warnings)
|
|
164
|
+
const title = String(result.data?.title || "").trim().toLowerCase()
|
|
165
|
+
if (!title) continue
|
|
166
|
+
if (titles.has(title)) errors.push(`${path}: duplicate title, also used by ${titles.get(title)}`)
|
|
167
|
+
else titles.set(title, path)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const { broken } = await buildGraph(root, options)
|
|
172
|
+
errors.push(...broken)
|
|
173
|
+
return { errors: errors.sort(), warnings: warnings.sort() }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function lintPacks() {
|
|
177
|
+
const root = join(packRoot, "packs")
|
|
178
|
+
const files = (await walkFiles(root)).filter((file) => file.endsWith(".md") && !file.endsWith("CONTRIBUTING.md"))
|
|
179
|
+
const problems = []
|
|
180
|
+
for (const file of files) {
|
|
181
|
+
const text = await readFile(file, "utf8")
|
|
182
|
+
problems.push(...lintText(toPosix(fromRoot(packRoot, file)), text))
|
|
183
|
+
}
|
|
184
|
+
return problems
|
|
185
|
+
}
|
package/src/lib/lock.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { mkdir, unlink, writeFile } from "node:fs/promises"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { readOptional } from "./fs-safe.js"
|
|
4
|
+
|
|
5
|
+
const STALE_MS = 10 * 60 * 1000
|
|
6
|
+
|
|
7
|
+
function pidAlive(pid) {
|
|
8
|
+
if (!Number.isInteger(pid) || pid <= 0) return false
|
|
9
|
+
try {
|
|
10
|
+
process.kill(pid, 0)
|
|
11
|
+
return true
|
|
12
|
+
} catch (error) {
|
|
13
|
+
return error.code === "EPERM"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function lockIsStale(lock, now = Date.now()) {
|
|
18
|
+
const time = Date.parse(lock?.time)
|
|
19
|
+
if (!Number.isFinite(time)) return true
|
|
20
|
+
if (now - time > STALE_MS) return true
|
|
21
|
+
return !pidAlive(lock.pid)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function withLock(root, fn) {
|
|
25
|
+
const dir = join(root, ".ctx")
|
|
26
|
+
const file = join(dir, ".lock")
|
|
27
|
+
await mkdir(dir, { recursive: true })
|
|
28
|
+
const existing = await readOptional(file)
|
|
29
|
+
if (existing) {
|
|
30
|
+
let lock
|
|
31
|
+
try {
|
|
32
|
+
lock = JSON.parse(existing.toString("utf8"))
|
|
33
|
+
} catch {
|
|
34
|
+
lock = null
|
|
35
|
+
}
|
|
36
|
+
if (lock && !lockIsStale(lock)) {
|
|
37
|
+
throw new Error(`Another connore run holds .ctx/.lock (pid ${lock.pid}).`)
|
|
38
|
+
}
|
|
39
|
+
await unlink(file).catch(() => {})
|
|
40
|
+
}
|
|
41
|
+
await writeFile(file, JSON.stringify({ pid: process.pid, time: new Date().toISOString() }))
|
|
42
|
+
try {
|
|
43
|
+
return await fn()
|
|
44
|
+
} finally {
|
|
45
|
+
await unlink(file).catch(() => {})
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { join } from "node:path"
|
|
2
|
+
import { readOptional } from "./fs-safe.js"
|
|
3
|
+
import { commitWrite } from "./mutate.js"
|
|
4
|
+
import { redactSecrets } from "./secrets.js"
|
|
5
|
+
|
|
6
|
+
export const MEMORIES = [
|
|
7
|
+
{ id: "project", file: "project.md", title: "Project memory", description: "Notes saved with connore memory project." },
|
|
8
|
+
{ id: "architecture", file: "architecture.md", title: "Architecture memory", description: "Notes saved with connore memory architecture." },
|
|
9
|
+
{ id: "decision", file: "decision.md", title: "Decision memory", description: "Notes saved with connore memory decision." },
|
|
10
|
+
{ id: "agent", file: "agent.md", title: "Agent memory", description: "Notes saved with connore memory agent." },
|
|
11
|
+
{ id: "preferences", file: "preferences.md", title: "Developer preferences", description: "Notes saved with connore memory preferences." },
|
|
12
|
+
{ id: "task", file: "task.md", title: "Task context", description: "Notes saved with connore memory task." },
|
|
13
|
+
{ id: "knowledge", file: "knowledge.md", title: "Knowledge library", description: "Notes saved with connore memory knowledge." },
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
export function memoryById(id) {
|
|
17
|
+
return MEMORIES.find((memory) => memory.id === id)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function renderMemory(memory) {
|
|
21
|
+
return `---
|
|
22
|
+
title: ${memory.title}
|
|
23
|
+
type: memory
|
|
24
|
+
tags: [memory, ${memory.id}]
|
|
25
|
+
source: user
|
|
26
|
+
description: ${memory.description}
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
# ${memory.title}
|
|
30
|
+
`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function appendMemory(root, id, text, report) {
|
|
34
|
+
const memory = memoryById(id)
|
|
35
|
+
if (!memory) {
|
|
36
|
+
throw Object.assign(new Error(`Unknown memory "${id}". Use: ${MEMORIES.map((item) => item.id).join(", ")}.`), { exitCode: 2 })
|
|
37
|
+
}
|
|
38
|
+
const note = redactSecrets(String(text || "").trim())
|
|
39
|
+
if (!note) throw Object.assign(new Error(`Usage: connore memory ${id} "text".`), { exitCode: 2 })
|
|
40
|
+
if (!(await readOptional(join(root, ".ctx", "PROCESS.md")))) throw new Error("No .ctx/ folder. Run connore init first.")
|
|
41
|
+
const display = `.ctx/memory/${memory.file}`
|
|
42
|
+
const file = join(root, ".ctx", "memory", memory.file)
|
|
43
|
+
const current = await readOptional(file)
|
|
44
|
+
const base = current ? current.toString("utf8") : renderMemory(memory)
|
|
45
|
+
const stamp = new Date().toISOString().slice(0, 10)
|
|
46
|
+
const next = `${base.replace(/\s*$/, "")}\n\n## ${stamp}\n\n${note}\n`
|
|
47
|
+
await commitWrite(file, next, report, display)
|
|
48
|
+
return memory
|
|
49
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { readOptional, removeFile, writeAtomic } from "./fs-safe.js"
|
|
2
|
+
|
|
3
|
+
function asBuffer(data) {
|
|
4
|
+
return Buffer.isBuffer(data) ? data : Buffer.from(data)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export async function commitWrite(file, data, report, display) {
|
|
8
|
+
const previous = await readOptional(file)
|
|
9
|
+
const buf = asBuffer(data)
|
|
10
|
+
if (previous && previous.equals(buf)) {
|
|
11
|
+
report.add("skipped", display)
|
|
12
|
+
return
|
|
13
|
+
}
|
|
14
|
+
const kind = previous ? "changed" : "created"
|
|
15
|
+
// Record the change before touching the file, so an interrupted run still
|
|
16
|
+
// leaves a manifest undo can read.
|
|
17
|
+
await report.record(kind, display, { previous })
|
|
18
|
+
if (!report.dryRun) await writeAtomic(file, buf)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function commitRemove(file, report, display) {
|
|
22
|
+
const previous = await readOptional(file)
|
|
23
|
+
if (!previous) return
|
|
24
|
+
await report.record("removed", display, { previous })
|
|
25
|
+
if (!report.dryRun) await removeFile(file)
|
|
26
|
+
}
|
package/src/lib/packs.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { readdir } from "node:fs/promises"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { walkFiles } from "./fs-safe.js"
|
|
4
|
+
import { packRoot } from "./paths.js"
|
|
5
|
+
|
|
6
|
+
export async function listPacks() {
|
|
7
|
+
const root = join(packRoot, "packs")
|
|
8
|
+
const names = (await readdir(root, { withFileTypes: true }))
|
|
9
|
+
.filter((entry) => entry.isDirectory())
|
|
10
|
+
.map((entry) => entry.name)
|
|
11
|
+
.sort()
|
|
12
|
+
const packs = []
|
|
13
|
+
for (const name of names) {
|
|
14
|
+
const files = (await walkFiles(join(root, name))).filter((file) => file.endsWith(".md"))
|
|
15
|
+
if (files.length) packs.push({ name, entries: files.length })
|
|
16
|
+
}
|
|
17
|
+
return packs
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function packDir(name) {
|
|
21
|
+
const dir = join(packRoot, "packs", name)
|
|
22
|
+
const files = await walkFiles(dir)
|
|
23
|
+
if (!files.some((file) => file.endsWith(".md"))) throw new Error(`Unknown pack "${name}".`)
|
|
24
|
+
return dir
|
|
25
|
+
}
|