connore 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +52 -2
- package/bin/ctxkit.js +5 -0
- package/package.json +33 -4
- 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 +28 -0
- package/packs/base/planning/blueprint-template.md +46 -0
- package/packs/base/planning/plan-template.md +45 -0
- package/packs/base/rules/anti-patterns.md +43 -0
- package/packs/base/rules/code-style.md +43 -0
- package/packs/base/rules/definition-of-done.md +47 -0
- package/packs/base/rules/dependency-policy.md +43 -0
- package/packs/base/rules/error-handling.md +43 -0
- package/packs/base/rules/security-baseline.md +43 -0
- package/packs/base/templates/bug-report.md +43 -0
- package/packs/base/templates/feature-spec.md +43 -0
- package/packs/base/templates/handoff.md +43 -0
- package/packs/base/templates/project-brief.md +43 -0
- package/packs/base/workflow/build-loop.md +48 -0
- package/packs/base/workflow/code-review.md +43 -0
- package/packs/base/workflow/debugging.md +43 -0
- package/packs/base/workflow/git-and-commits.md +43 -0
- package/packs/base/workflow/idea-to-spec.md +43 -0
- package/packs/base/workflow/session-handoff.md +43 -0
- package/packs/base/workflow/spec-to-plan.md +43 -0
- package/packs/frontend/components/button.md +25 -0
- package/packs/frontend/components/card.md +25 -0
- package/packs/frontend/components/input.md +25 -0
- package/packs/frontend/design/rules.md +24 -0
- package/packs/frontend/design/tokens.md +40 -0
- package/packs/nextjs-fullstack/backend/api-conventions.md +48 -0
- package/packs/nextjs-fullstack/backend/auth-and-roles.md +43 -0
- package/packs/nextjs-fullstack/backend/background-jobs.md +43 -0
- package/packs/nextjs-fullstack/backend/caching.md +43 -0
- package/packs/nextjs-fullstack/backend/database.md +43 -0
- package/packs/nextjs-fullstack/backend/file-uploads.md +43 -0
- package/packs/nextjs-fullstack/backend/logging.md +43 -0
- package/packs/nextjs-fullstack/backend/migrations.md +43 -0
- package/packs/nextjs-fullstack/backend/rate-limiting.md +43 -0
- package/packs/nextjs-fullstack/backend/secrets-and-env.md +43 -0
- package/packs/nextjs-fullstack/backend/validation.md +43 -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 +43 -0
- package/packs/nextjs-fullstack/build-deploy/deploy.md +43 -0
- package/packs/nextjs-fullstack/build-deploy/monitoring.md +43 -0
- package/packs/nextjs-fullstack/build-deploy/performance.md +43 -0
- package/packs/nextjs-fullstack/build-deploy/project-structure.md +43 -0
- package/packs/nextjs-fullstack/build-deploy/release-checklist.md +43 -0
- package/packs/nextjs-fullstack/build-deploy/testing.md +43 -0
- package/packs/nextjs-fullstack/evals/README.md +43 -0
- package/packs/nextjs-fullstack/examples/golden-app/README.md +43 -0
- package/packs/nextjs-fullstack/frontend/accessibility.md +43 -0
- package/packs/nextjs-fullstack/frontend/components.md +43 -0
- package/packs/nextjs-fullstack/frontend/forms.md +43 -0
- package/packs/nextjs-fullstack/frontend/layout.md +43 -0
- package/packs/nextjs-fullstack/frontend/loading-empty-error.md +43 -0
- package/packs/nextjs-fullstack/frontend/motion.md +43 -0
- package/packs/nextjs-fullstack/frontend/responsive.md +43 -0
- package/packs/nextjs-fullstack/frontend/seo.md +43 -0
- package/packs/nextjs-fullstack/frontend/tables.md +43 -0
- package/packs/nextjs-fullstack/frontend/tokens.md +43 -0
- package/packs/nextjs-fullstack/integrations/analytics.md +43 -0
- package/packs/nextjs-fullstack/integrations/email.md +43 -0
- package/packs/nextjs-fullstack/integrations/llm-apis.md +43 -0
- package/packs/nextjs-fullstack/integrations/oauth.md +43 -0
- package/packs/nextjs-fullstack/integrations/object-storage.md +43 -0
- package/packs/nextjs-fullstack/integrations/payments.md +43 -0
- package/packs/nextjs-fullstack/integrations/queues.md +43 -0
- package/packs/nextjs-fullstack/integrations/search.md +43 -0
- package/packs/nextjs-fullstack/integrations/webhooks.md +43 -0
- package/packs/nextjs-fullstack/playbooks/add-ai-feature.md +43 -0
- package/packs/nextjs-fullstack/playbooks/add-auth.md +43 -0
- package/packs/nextjs-fullstack/playbooks/add-billing.md +50 -0
- package/packs/nextjs-fullstack/playbooks/add-crud.md +43 -0
- package/packs/nextjs-fullstack/playbooks/add-email.md +43 -0
- package/packs/nextjs-fullstack/playbooks/add-file-upload.md +43 -0
- package/packs/nextjs-fullstack/playbooks/add-webhook.md +43 -0
- package/packs/nextjs-fullstack/playbooks/ship-to-prod.md +43 -0
- package/src/cli.js +288 -0
- package/src/commands/add.js +61 -0
- package/src/commands/catalog.js +21 -0
- package/src/commands/doctor.js +42 -0
- package/src/commands/hook.js +67 -0
- package/src/commands/init.js +69 -0
- package/src/commands/learn.js +39 -0
- package/src/commands/lint.js +5 -0
- package/src/commands/list.js +10 -0
- package/src/commands/log.js +25 -0
- package/src/commands/memory.js +14 -0
- package/src/commands/plan.js +41 -0
- package/src/commands/reindex.js +8 -0
- package/src/commands/review.js +31 -0
- package/src/commands/search.js +21 -0
- package/src/commands/stats.js +35 -0
- package/src/commands/undo.js +7 -0
- package/src/commands/update.js +16 -0
- package/src/lib/catalog.js +32 -0
- package/src/lib/config.js +64 -0
- package/src/lib/frontmatter.js +70 -0
- package/src/lib/fs-safe.js +87 -0
- package/src/lib/gitlog.js +75 -0
- package/src/lib/ignore.js +47 -0
- package/src/lib/index.js +72 -0
- package/src/lib/lessons.js +173 -0
- package/src/lib/lint.js +67 -0
- package/src/lib/lock.js +47 -0
- package/src/lib/memory.js +49 -0
- package/src/lib/mutate.js +24 -0
- package/src/lib/packs.js +10 -0
- package/src/lib/paths.js +25 -0
- package/src/lib/plan.js +156 -0
- package/src/lib/pointers.js +76 -0
- package/src/lib/provider.js +27 -0
- package/src/lib/report.js +33 -0
- package/src/lib/run.js +73 -0
- package/src/lib/search.js +40 -0
- package/src/lib/secrets.js +25 -0
- package/src/lib/signals.js +55 -0
- package/src/lib/sync.js +36 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MEMORIES, appendMemory } from "../lib/memory.js"
|
|
2
|
+
import { rebuildIndex } from "../lib/index.js"
|
|
3
|
+
|
|
4
|
+
export async function memory(root, args, report, stdout, flags) {
|
|
5
|
+
if (!args.length) {
|
|
6
|
+
const lines = MEMORIES.map((item) => `${item.id}\t.ctx/memory/${item.file}\t${item.description}`)
|
|
7
|
+
if (flags.json) stdout.write(`${JSON.stringify({ ok: true, memories: MEMORIES })}\n`)
|
|
8
|
+
else stdout.write(`${lines.join("\n")}\n`)
|
|
9
|
+
return
|
|
10
|
+
}
|
|
11
|
+
const [id, ...rest] = args
|
|
12
|
+
await appendMemory(root, id, rest.join(" "), report)
|
|
13
|
+
await rebuildIndex(root, report, { includeDrafts: flags.includeDrafts })
|
|
14
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { join } from "node:path"
|
|
2
|
+
import { readOptional } from "../lib/fs-safe.js"
|
|
3
|
+
import { rebuildIndex } from "../lib/index.js"
|
|
4
|
+
import { commitWrite } from "../lib/mutate.js"
|
|
5
|
+
import { markTaskDone, parseBlueprint, planView, readBlueprint, renderPlan } from "../lib/plan.js"
|
|
6
|
+
|
|
7
|
+
function planPath(root) {
|
|
8
|
+
return join(root, ".ctx", "mine", "plan.md")
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function requirePlan(root) {
|
|
12
|
+
const file = planPath(root)
|
|
13
|
+
const raw = await readOptional(file)
|
|
14
|
+
if (!raw) throw new Error("No plan. Run connore plan init <blueprint>.")
|
|
15
|
+
return { file, text: raw.toString("utf8") }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function plan(root, args, report) {
|
|
19
|
+
if (!(await readOptional(join(root, ".ctx", "PROCESS.md")))) throw new Error("No .ctx/ folder. Run connore init first.")
|
|
20
|
+
const [sub, ...rest] = args
|
|
21
|
+
if (sub === "init") {
|
|
22
|
+
const name = rest[0]
|
|
23
|
+
const file = planPath(root)
|
|
24
|
+
if (await readOptional(file)) throw new Error("A plan already exists at .ctx/mine/plan.md.")
|
|
25
|
+
const source = await readBlueprint(name)
|
|
26
|
+
const rendered = renderPlan(name, parseBlueprint(source.text))
|
|
27
|
+
if (!parseBlueprint(source.text).phases.length) throw new Error(`Blueprint "${name}" has no phases.`)
|
|
28
|
+
await commitWrite(file, rendered, report, ".ctx/mine/plan.md")
|
|
29
|
+
await rebuildIndex(root, report)
|
|
30
|
+
return planView(rendered)
|
|
31
|
+
}
|
|
32
|
+
const current = await requirePlan(root)
|
|
33
|
+
if (sub === "status" || sub === "next") return planView(current.text)
|
|
34
|
+
if (sub === "done") {
|
|
35
|
+
const updated = markTaskDone(current.text, rest.join(" "))
|
|
36
|
+
await commitWrite(current.file, updated, report, ".ctx/mine/plan.md")
|
|
37
|
+
await rebuildIndex(root, report)
|
|
38
|
+
return planView(updated)
|
|
39
|
+
}
|
|
40
|
+
throw Object.assign(new Error("Usage: connore plan init <blueprint> | status | next | done <task>."), { exitCode: 2 })
|
|
41
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { join } from "node:path"
|
|
2
|
+
import { readOptional } from "../lib/fs-safe.js"
|
|
3
|
+
import { rebuildIndex } from "../lib/index.js"
|
|
4
|
+
|
|
5
|
+
export async function reindex(root, flags, report) {
|
|
6
|
+
if (!(await readOptional(join(root, ".ctx", "PROCESS.md")))) throw new Error("No .ctx/ folder. Run connore init first.")
|
|
7
|
+
await rebuildIndex(root, report, { includeDrafts: flags.includeDrafts })
|
|
8
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { join } from "node:path"
|
|
2
|
+
import { readOptional } from "../lib/fs-safe.js"
|
|
3
|
+
import { rebuildIndex } from "../lib/index.js"
|
|
4
|
+
import { lessonFile, listProposals, moveProposal } from "../lib/lessons.js"
|
|
5
|
+
import { commitWrite } from "../lib/mutate.js"
|
|
6
|
+
import { fromRoot } from "../lib/paths.js"
|
|
7
|
+
|
|
8
|
+
export async function review(root, args, flags, report) {
|
|
9
|
+
if (!(await readOptional(join(root, ".ctx", "PROCESS.md")))) throw new Error("No .ctx/ folder. Run connore init first.")
|
|
10
|
+
const [action, id] = args
|
|
11
|
+
if (!action) return { proposals: await listProposals(root) }
|
|
12
|
+
if (!id || !["accept", "reject", "edit"].includes(action)) {
|
|
13
|
+
throw Object.assign(new Error("Usage: connore review [accept|reject|edit <id>]."), { exitCode: 2 })
|
|
14
|
+
}
|
|
15
|
+
if (action === "edit") {
|
|
16
|
+
const proposals = await listProposals(root)
|
|
17
|
+
const lesson = proposals.find((item) => item.id === id)
|
|
18
|
+
if (!lesson) throw new Error(`No proposal "${id}".`)
|
|
19
|
+
const next = {
|
|
20
|
+
...lesson,
|
|
21
|
+
mistake: flags.mistake || lesson.mistake,
|
|
22
|
+
fix: flags.fix || lesson.fix,
|
|
23
|
+
status: "proposed",
|
|
24
|
+
}
|
|
25
|
+
await commitWrite(lesson.file, lessonFile(next), report, fromRoot(root, lesson.file))
|
|
26
|
+
return { proposals: [next] }
|
|
27
|
+
}
|
|
28
|
+
const lesson = await moveProposal(root, id, action === "accept" ? "accepted" : "rejected", report)
|
|
29
|
+
if (action === "accept") await rebuildIndex(root, report, { includeDrafts: flags.includeDrafts })
|
|
30
|
+
return { proposals: [lesson] }
|
|
31
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { readOptional } from "../lib/fs-safe.js"
|
|
4
|
+
import { collectEntries } from "../lib/index.js"
|
|
5
|
+
import { absFromPosix } from "../lib/paths.js"
|
|
6
|
+
import { rankEntries } from "../lib/search.js"
|
|
7
|
+
|
|
8
|
+
export async function search(root, query, flags) {
|
|
9
|
+
if (!(await readOptional(join(root, ".ctx", "PROCESS.md")))) throw new Error("No .ctx/ folder. Run connore init first.")
|
|
10
|
+
const entries = await collectEntries(root, { includeDrafts: flags.includeDrafts })
|
|
11
|
+
const ranked = []
|
|
12
|
+
for (const entry of entries) {
|
|
13
|
+
const abs = absFromPosix(root, entry.path)
|
|
14
|
+
const text = (await readFile(abs, "utf8")).replace(/\r\n/g, "\n")
|
|
15
|
+
const body = text.replace(/^---[\s\S]*?---\n/, "")
|
|
16
|
+
const headings = [...body.matchAll(/^#{1,3} (.+)$/gm)].map((match) => match[1]).join(" ")
|
|
17
|
+
const title = body.match(/^# (.+)$/m)?.[1] || entry.path
|
|
18
|
+
ranked.push({ ...entry, title, headings, body })
|
|
19
|
+
}
|
|
20
|
+
return rankEntries(ranked, query).map(({ path, score, description, title }) => ({ path, score, title, description }))
|
|
21
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { listAccepted } from "../lib/lessons.js"
|
|
2
|
+
import { readSignals } from "../lib/signals.js"
|
|
3
|
+
|
|
4
|
+
function isoWeek(date) {
|
|
5
|
+
const copy = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()))
|
|
6
|
+
const day = copy.getUTCDay() || 7
|
|
7
|
+
copy.setUTCDate(copy.getUTCDate() + 4 - day)
|
|
8
|
+
const start = new Date(Date.UTC(copy.getUTCFullYear(), 0, 1))
|
|
9
|
+
const week = Math.ceil(((copy - start) / 86400000 + 1) / 7)
|
|
10
|
+
return `${copy.getUTCFullYear()}-W${String(week).padStart(2, "0")}`
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function buildStats(signals, lessons) {
|
|
14
|
+
const weeks = new Map()
|
|
15
|
+
function bump(week, key) {
|
|
16
|
+
if (!week) return
|
|
17
|
+
if (!weeks.has(week)) weeks.set(week, { week, signals: 0, accepted: 0, recurring: 0 })
|
|
18
|
+
weeks.get(week)[key] += 1
|
|
19
|
+
}
|
|
20
|
+
for (const signal of signals) {
|
|
21
|
+
const at = new Date(signal.at)
|
|
22
|
+
if (!Number.isNaN(at.getTime())) bump(isoWeek(at), "signals")
|
|
23
|
+
}
|
|
24
|
+
for (const lesson of lessons) {
|
|
25
|
+
const at = new Date(lesson.lastSeen)
|
|
26
|
+
const week = Number.isNaN(at.getTime()) ? null : isoWeek(at)
|
|
27
|
+
if (lesson.status === "accepted") bump(week, "accepted")
|
|
28
|
+
if (lesson.flagged === "recurring") bump(week, "recurring")
|
|
29
|
+
}
|
|
30
|
+
return { weeks: [...weeks.values()].sort((a, b) => a.week.localeCompare(b.week)) }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function stats(root) {
|
|
34
|
+
return buildStats(await readSignals(root), await listAccepted(root))
|
|
35
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { join } from "node:path"
|
|
2
|
+
import { readOptional } from "../lib/fs-safe.js"
|
|
3
|
+
import { rebuildIndex } from "../lib/index.js"
|
|
4
|
+
import { packDir } from "../lib/packs.js"
|
|
5
|
+
import { syncTree } from "../lib/sync.js"
|
|
6
|
+
|
|
7
|
+
export async function update(root, flags, report) {
|
|
8
|
+
if (!(await readOptional(join(root, ".ctx", "PROCESS.md")))) throw new Error("No .ctx/ folder. Run connore init first.")
|
|
9
|
+
const source = await packDir(flags.pack || "frontend")
|
|
10
|
+
await syncTree(source, join(root, ".ctx", "library"), root, {
|
|
11
|
+
overwrite: true,
|
|
12
|
+
report,
|
|
13
|
+
includeDrafts: flags.includeDrafts,
|
|
14
|
+
})
|
|
15
|
+
await rebuildIndex(root, report, { includeDrafts: flags.includeDrafts })
|
|
16
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { readdir } from "node:fs/promises"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
|
|
4
|
+
export const GROUPS = [
|
|
5
|
+
{ id: "screenshots", label: "UI screenshots", target: 20 },
|
|
6
|
+
{ id: "pages", label: "Complete page references", target: 5 },
|
|
7
|
+
{ id: "tokens", label: "Design token system", target: 1 },
|
|
8
|
+
{ id: "rules", label: "Design rules", target: 10 },
|
|
9
|
+
{ id: "components", label: "Component notes", target: 15 },
|
|
10
|
+
{ id: "motion", label: "Motion notes", target: 10 },
|
|
11
|
+
{ id: "custom", label: "Custom components", target: 5 },
|
|
12
|
+
{ id: "golden", label: "Golden projects", target: 3 },
|
|
13
|
+
{ id: "blueprints", label: "Page blueprints", target: 5 },
|
|
14
|
+
{ id: "patterns", label: "Frontend patterns", target: 5 },
|
|
15
|
+
{ id: "architecture", label: "Backend architecture", target: 3 },
|
|
16
|
+
{ id: "schemas", label: "Database schemas", target: 5 },
|
|
17
|
+
{ id: "api", label: "API examples", target: 5 },
|
|
18
|
+
{ id: "auth", label: "Authentication workflows", target: 3 },
|
|
19
|
+
{ id: "docs", label: "Versioned documentation sets", target: 5 },
|
|
20
|
+
{ id: "coding-rules", label: "Coding rules", target: 10 },
|
|
21
|
+
{ id: "skills", label: "Agent skills", target: 5 },
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
export async function countGroup(root, id) {
|
|
25
|
+
try {
|
|
26
|
+
const names = await readdir(join(root, ".ctx", "mine", "catalog", id))
|
|
27
|
+
return names.filter((name) => name.endsWith(".md")).length
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (error.code === "ENOENT") return 0
|
|
30
|
+
throw error
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { join } from "node:path"
|
|
2
|
+
import { readOptional } from "./fs-safe.js"
|
|
3
|
+
import { commitWrite } from "./mutate.js"
|
|
4
|
+
|
|
5
|
+
export const SCHEMA_VERSION = 1
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_IGNORE = ["node_modules", ".git", "dist", "build", ".next", "out", "coverage", ".turbo", "vault"]
|
|
8
|
+
|
|
9
|
+
export function defaultConfig(tools = []) {
|
|
10
|
+
return {
|
|
11
|
+
schemaVersion: SCHEMA_VERSION,
|
|
12
|
+
tools: [...tools],
|
|
13
|
+
ignore: [...DEFAULT_IGNORE],
|
|
14
|
+
provider: "local",
|
|
15
|
+
lessonBudget: 400,
|
|
16
|
+
lessonLimit: 5,
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function configText(config) {
|
|
21
|
+
return `${JSON.stringify(config, null, 2)}\n`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function migrateConfig(input) {
|
|
25
|
+
const source = input && typeof input === "object" && !Array.isArray(input) ? input : {}
|
|
26
|
+
const version = Number.isInteger(source.schemaVersion) ? source.schemaVersion : 0
|
|
27
|
+
if (version > SCHEMA_VERSION) {
|
|
28
|
+
throw new Error(`This folder uses schemaVersion ${version}. Upgrade connore before writing to it.`)
|
|
29
|
+
}
|
|
30
|
+
const next = defaultConfig(Array.isArray(source.tools) ? source.tools.filter((tool) => typeof tool === "string") : [])
|
|
31
|
+
if (Array.isArray(source.ignore) && source.ignore.length) next.ignore = source.ignore.map(String)
|
|
32
|
+
if (typeof source.provider === "string" && source.provider) next.provider = source.provider
|
|
33
|
+
if (Number.isInteger(source.lessonBudget) && source.lessonBudget > 0) next.lessonBudget = source.lessonBudget
|
|
34
|
+
if (Number.isInteger(source.lessonLimit) && source.lessonLimit > 0) next.lessonLimit = source.lessonLimit
|
|
35
|
+
next.schemaVersion = SCHEMA_VERSION
|
|
36
|
+
return next
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function configPath(root) {
|
|
40
|
+
return join(root, ".ctx", "config.json")
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function readConfig(root) {
|
|
44
|
+
const file = configPath(root)
|
|
45
|
+
const raw = await readOptional(file)
|
|
46
|
+
if (!raw) return { file, raw: null, parsed: null, config: null }
|
|
47
|
+
let parsed
|
|
48
|
+
try {
|
|
49
|
+
parsed = JSON.parse(raw.toString("utf8"))
|
|
50
|
+
} catch {
|
|
51
|
+
throw new Error(".ctx/config.json is not valid JSON.")
|
|
52
|
+
}
|
|
53
|
+
return { file, raw, parsed, config: migrateConfig(parsed) }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function ensureConfig(root, report, tools, options = {}) {
|
|
57
|
+
const current = await readConfig(root)
|
|
58
|
+
const config = current.config || defaultConfig(tools || [])
|
|
59
|
+
if (!current.raw || options.replaceTools) config.tools = [...(tools || [])]
|
|
60
|
+
const next = configText(config)
|
|
61
|
+
if (!current.raw || current.raw.toString("utf8") !== next) await commitWrite(current.file, next, report, ".ctx/config.json")
|
|
62
|
+
else report.add("skipped", ".ctx/config.json")
|
|
63
|
+
return config
|
|
64
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export function parseFrontmatter(text) {
|
|
2
|
+
const normalized = text.replace(/^\uFEFF/, "").replace(/\r\n/g, "\n")
|
|
3
|
+
if (!normalized.startsWith("---")) return null
|
|
4
|
+
const end = normalized.indexOf("\n---", 3)
|
|
5
|
+
if (end === -1) return null
|
|
6
|
+
const block = normalized.slice(normalized.indexOf("\n") + 1, end)
|
|
7
|
+
const data = {}
|
|
8
|
+
for (const line of block.split(/\r?\n/)) {
|
|
9
|
+
const match = /^([A-Za-z0-9_-]+):\s*(.*)$/.exec(line)
|
|
10
|
+
if (!match) continue
|
|
11
|
+
const value = match[2].trim()
|
|
12
|
+
if (value.startsWith("[") && value.endsWith("]")) {
|
|
13
|
+
data[match[1]] = value
|
|
14
|
+
.slice(1, -1)
|
|
15
|
+
.split(",")
|
|
16
|
+
.map((part) => part.trim())
|
|
17
|
+
.filter(Boolean)
|
|
18
|
+
} else data[match[1]] = value.replace(/^["']|["']$/g, "")
|
|
19
|
+
}
|
|
20
|
+
return data
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function today() {
|
|
24
|
+
const date = new Date()
|
|
25
|
+
const month = String(date.getMonth() + 1).padStart(2, "0")
|
|
26
|
+
const day = String(date.getDate()).padStart(2, "0")
|
|
27
|
+
return `${date.getFullYear()}-${month}-${day}`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function twinStub({ title, type, tags, date }) {
|
|
31
|
+
const tagList = tags.length ? tags.join(", ") : "TODO"
|
|
32
|
+
return `---
|
|
33
|
+
title: ${title}
|
|
34
|
+
type: ${type}
|
|
35
|
+
tags: [${tagList}]
|
|
36
|
+
source: user
|
|
37
|
+
updated: ${date}
|
|
38
|
+
description: TODO
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
# ${title}
|
|
42
|
+
|
|
43
|
+
## Purpose
|
|
44
|
+
TODO
|
|
45
|
+
|
|
46
|
+
## Layout
|
|
47
|
+
TODO
|
|
48
|
+
|
|
49
|
+
## Colors
|
|
50
|
+
TODO hex values.
|
|
51
|
+
|
|
52
|
+
## Type
|
|
53
|
+
TODO font and sizes.
|
|
54
|
+
|
|
55
|
+
## Spacing
|
|
56
|
+
TODO
|
|
57
|
+
|
|
58
|
+
## Radius
|
|
59
|
+
TODO
|
|
60
|
+
|
|
61
|
+
## Shadows
|
|
62
|
+
TODO
|
|
63
|
+
|
|
64
|
+
## Animation
|
|
65
|
+
TODO
|
|
66
|
+
|
|
67
|
+
## Components
|
|
68
|
+
TODO
|
|
69
|
+
`
|
|
70
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto"
|
|
2
|
+
import { mkdir, readdir, readFile, rename, rm, stat, unlink, writeFile } from "node:fs/promises"
|
|
3
|
+
import { basename, dirname, join } from "node:path"
|
|
4
|
+
|
|
5
|
+
const RETRYABLE = new Set(["EPERM", "EBUSY", "EACCES"])
|
|
6
|
+
|
|
7
|
+
export async function exists(filePath) {
|
|
8
|
+
try {
|
|
9
|
+
await stat(filePath)
|
|
10
|
+
return true
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (error.code === "ENOENT") return false
|
|
13
|
+
throw error
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function readOptional(filePath) {
|
|
18
|
+
try {
|
|
19
|
+
return await readFile(filePath)
|
|
20
|
+
} catch (error) {
|
|
21
|
+
if (error.code === "ENOENT") return null
|
|
22
|
+
throw error
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function writeOnce(filePath, data, io) {
|
|
27
|
+
await io.mkdir(dirname(filePath), { recursive: true })
|
|
28
|
+
const tmp = join(dirname(filePath), `.${basename(filePath)}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`)
|
|
29
|
+
await io.writeFile(tmp, data)
|
|
30
|
+
try {
|
|
31
|
+
await io.rename(tmp, filePath)
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (error.code !== "EEXIST" && error.code !== "EPERM") {
|
|
34
|
+
await io.unlink(tmp).catch(() => {})
|
|
35
|
+
throw error
|
|
36
|
+
}
|
|
37
|
+
const backup = `${filePath}.${process.pid}.bak`
|
|
38
|
+
await io.rename(filePath, backup)
|
|
39
|
+
try {
|
|
40
|
+
await io.rename(tmp, filePath)
|
|
41
|
+
} catch (renameError) {
|
|
42
|
+
await io.rename(backup, filePath).catch(() => {})
|
|
43
|
+
throw renameError
|
|
44
|
+
}
|
|
45
|
+
await io.unlink(backup).catch(() => {})
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function writeAtomic(filePath, data, io = {}) {
|
|
50
|
+
const fsio = {
|
|
51
|
+
mkdir: io.mkdir || mkdir,
|
|
52
|
+
writeFile: io.writeFile || writeFile,
|
|
53
|
+
rename: io.rename || rename,
|
|
54
|
+
unlink: io.unlink || unlink,
|
|
55
|
+
}
|
|
56
|
+
const sleep = io.sleep || ((ms) => new Promise((resolve) => setTimeout(resolve, ms)))
|
|
57
|
+
let last
|
|
58
|
+
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
59
|
+
try {
|
|
60
|
+
await writeOnce(filePath, data, fsio)
|
|
61
|
+
return
|
|
62
|
+
} catch (error) {
|
|
63
|
+
last = error
|
|
64
|
+
if (!RETRYABLE.has(error.code) || attempt === 4) throw error
|
|
65
|
+
await sleep(20 * (attempt + 1))
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
throw last
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function removeFile(filePath) {
|
|
72
|
+
await rm(filePath, { force: true })
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function walkFiles(dir, ignore = new Set()) {
|
|
76
|
+
if (!(await exists(dir))) return []
|
|
77
|
+
const entries = await readdir(dir, { withFileTypes: true })
|
|
78
|
+
entries.sort((a, b) => a.name.localeCompare(b.name))
|
|
79
|
+
const files = []
|
|
80
|
+
for (const entry of entries) {
|
|
81
|
+
if (ignore.has(entry.name)) continue
|
|
82
|
+
const filePath = join(dir, entry.name)
|
|
83
|
+
if (entry.isDirectory()) files.push(...(await walkFiles(filePath, ignore)))
|
|
84
|
+
else if (entry.isFile()) files.push(filePath)
|
|
85
|
+
}
|
|
86
|
+
return files
|
|
87
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { execFile } from "node:child_process"
|
|
2
|
+
import { promisify } from "node:util"
|
|
3
|
+
|
|
4
|
+
const exec = promisify(execFile)
|
|
5
|
+
|
|
6
|
+
export function signalsFromGitLog(text) {
|
|
7
|
+
const commits = []
|
|
8
|
+
let current = null
|
|
9
|
+
for (const line of String(text || "").split(/\r?\n/)) {
|
|
10
|
+
if (!line.trim()) {
|
|
11
|
+
if (current) commits.push(current)
|
|
12
|
+
current = null
|
|
13
|
+
continue
|
|
14
|
+
}
|
|
15
|
+
if (!current) {
|
|
16
|
+
const [hash, date, ...rest] = line.split("\t")
|
|
17
|
+
current = { hash, date, subject: rest.join("\t"), files: [] }
|
|
18
|
+
} else current.files.push(line.trim())
|
|
19
|
+
}
|
|
20
|
+
if (current) commits.push(current)
|
|
21
|
+
|
|
22
|
+
const signals = []
|
|
23
|
+
const seen = new Map()
|
|
24
|
+
for (const commit of commits.reverse()) {
|
|
25
|
+
const when = Date.parse(commit.date)
|
|
26
|
+
if (/^revert\b/i.test(commit.subject)) {
|
|
27
|
+
signals.push({
|
|
28
|
+
at: commit.date,
|
|
29
|
+
kind: "failure",
|
|
30
|
+
text: commit.subject,
|
|
31
|
+
files: commit.files,
|
|
32
|
+
source: "git",
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
if (!/^fix\b/i.test(commit.subject)) {
|
|
36
|
+
for (const file of commit.files) seen.set(file, when)
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
for (const file of commit.files) {
|
|
40
|
+
const earlier = seen.get(file)
|
|
41
|
+
if (Number.isFinite(earlier) && Number.isFinite(when) && Math.abs(when - earlier) <= 24 * 60 * 60 * 1000) {
|
|
42
|
+
signals.push({
|
|
43
|
+
at: commit.date,
|
|
44
|
+
kind: "fix",
|
|
45
|
+
text: `fix after a recent change in ${file}`,
|
|
46
|
+
files: [file],
|
|
47
|
+
source: "git",
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return signals
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function readGitSignals(root) {
|
|
56
|
+
try {
|
|
57
|
+
const { stdout } = await exec("git", ["-C", root, "log", "-n", "80", "--pretty=format:%H%x09%cI%x09%s", "--name-only"], {
|
|
58
|
+
timeout: 5000,
|
|
59
|
+
windowsHide: true,
|
|
60
|
+
})
|
|
61
|
+
return signalsFromGitLog(stdout)
|
|
62
|
+
} catch {
|
|
63
|
+
return []
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function changedFiles(root) {
|
|
68
|
+
try {
|
|
69
|
+
const unstaged = await exec("git", ["-C", root, "diff", "--name-only", "HEAD"], { timeout: 5000, windowsHide: true })
|
|
70
|
+
const staged = await exec("git", ["-C", root, "diff", "--name-only", "--cached"], { timeout: 5000, windowsHide: true })
|
|
71
|
+
return [...new Set(`${unstaged.stdout}\n${staged.stdout}`.split(/\r?\n/).map((line) => line.trim()).filter(Boolean))]
|
|
72
|
+
} catch {
|
|
73
|
+
return []
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { join } from "node:path"
|
|
2
|
+
import { readOptional } from "./fs-safe.js"
|
|
3
|
+
import { commitWrite } from "./mutate.js"
|
|
4
|
+
|
|
5
|
+
export const CTXIGNORE = `# Folders connore skips while scanning
|
|
6
|
+
node_modules
|
|
7
|
+
.git
|
|
8
|
+
dist
|
|
9
|
+
build
|
|
10
|
+
.next
|
|
11
|
+
out
|
|
12
|
+
coverage
|
|
13
|
+
.turbo
|
|
14
|
+
vault
|
|
15
|
+
`
|
|
16
|
+
|
|
17
|
+
export const GITIGNORE_BLOCK = `# connore local state
|
|
18
|
+
.ctx/.signals/
|
|
19
|
+
.ctx/.runs/
|
|
20
|
+
.ctx/.lock
|
|
21
|
+
.ctx/vault/
|
|
22
|
+
`
|
|
23
|
+
|
|
24
|
+
export async function ignoreNames(root, config) {
|
|
25
|
+
const names = new Set(config?.ignore || [])
|
|
26
|
+
const file = await readOptional(join(root, ".ctxignore"))
|
|
27
|
+
if (!file) return names
|
|
28
|
+
for (const line of file.toString("utf8").split(/\r?\n/)) {
|
|
29
|
+
const trimmed = line.trim()
|
|
30
|
+
if (!trimmed || trimmed.startsWith("#")) continue
|
|
31
|
+
names.add(trimmed.replace(/^\//, "").replace(/\/$/, ""))
|
|
32
|
+
}
|
|
33
|
+
return names
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function ensureLocalIgnores(root, report) {
|
|
37
|
+
await commitWrite(join(root, ".ctxignore"), CTXIGNORE, report, ".ctxignore")
|
|
38
|
+
const gitignore = join(root, ".gitignore")
|
|
39
|
+
const current = await readOptional(gitignore)
|
|
40
|
+
const text = current ? current.toString("utf8") : ""
|
|
41
|
+
if (text.includes(".ctx/.signals/")) {
|
|
42
|
+
report.add("skipped", ".gitignore")
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
const next = text ? `${text.replace(/\s*$/, "\n\n")}${GITIGNORE_BLOCK}` : GITIGNORE_BLOCK
|
|
46
|
+
await commitWrite(gitignore, next, report, ".gitignore")
|
|
47
|
+
}
|
package/src/lib/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
|
|
8
|
+
export function entryVisible(data, file, { includeDrafts = false } = {}) {
|
|
9
|
+
if (!data) return false
|
|
10
|
+
const path = file.replace(/\\/g, "/")
|
|
11
|
+
if (path.includes("/_proposed/") || path.includes("/_rejected/") || path.includes("/vault/")) return false
|
|
12
|
+
if (path.includes("/library/catalog/")) return false
|
|
13
|
+
if (data.status === "proposed" || data.status === "rejected") return false
|
|
14
|
+
if (data.status === "draft" && !includeDrafts) return false
|
|
15
|
+
return true
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function collectEntries(root, options = {}) {
|
|
19
|
+
const files = []
|
|
20
|
+
for (const dir of [ctxPath(root, "library"), ctxPath(root, "mine"), ctxPath(root, "memory")]) files.push(...(await walkFiles(dir)))
|
|
21
|
+
const processFile = ctxPath(root, "PROCESS.md")
|
|
22
|
+
if (await exists(processFile)) files.push(processFile)
|
|
23
|
+
const startFile = ctxPath(root, "START.md")
|
|
24
|
+
if (await exists(startFile)) files.push(startFile)
|
|
25
|
+
|
|
26
|
+
const entries = []
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
if (!file.endsWith(".md")) continue
|
|
29
|
+
const text = await readFile(file, "utf8")
|
|
30
|
+
const data = parseFrontmatter(text)
|
|
31
|
+
if (!entryVisible(data, file, options)) continue
|
|
32
|
+
if (file.replace(/\\/g, "/").includes("/memory/") && !/^## \d{4}-\d{2}-\d{2}\s*$/m.test(text)) continue
|
|
33
|
+
entries.push({
|
|
34
|
+
path: fromRoot(root, file),
|
|
35
|
+
type: data.type || "",
|
|
36
|
+
tags: Array.isArray(data.tags) ? data.tags.join(", ") : data.tags || "",
|
|
37
|
+
description: data.description || "",
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
entries.sort((a, b) => a.path.localeCompare(b.path))
|
|
41
|
+
const start = entries.filter((entry) => entry.path === ".ctx/START.md")
|
|
42
|
+
const rest = entries.filter((entry) => entry.path !== ".ctx/START.md")
|
|
43
|
+
return [...start, ...rest]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function renderIndex(entries) {
|
|
47
|
+
const lines = [
|
|
48
|
+
"# Index",
|
|
49
|
+
"",
|
|
50
|
+
"Read .ctx/START.md before new work. Load only the entries relevant to the task. Prefer text specs over opening images.",
|
|
51
|
+
"",
|
|
52
|
+
"| Path | Type | Tags | Description |",
|
|
53
|
+
"| --- | --- | --- | --- |",
|
|
54
|
+
]
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
lines.push(`| \`${entry.path}\` | ${entry.type} | ${entry.tags} | ${entry.description} |`)
|
|
57
|
+
}
|
|
58
|
+
lines.push("")
|
|
59
|
+
return lines.join("\n")
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function rebuildIndex(root, report, options = {}) {
|
|
63
|
+
const file = join(root, ".ctx", "INDEX.md")
|
|
64
|
+
const next = renderIndex(await collectEntries(root, options))
|
|
65
|
+
const current = await readOptional(file)
|
|
66
|
+
const currentText = current ? current.toString("utf8") : null
|
|
67
|
+
if (currentText === next) {
|
|
68
|
+
report.add("skipped", ".ctx/INDEX.md")
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
await commitWrite(file, next, report, ".ctx/INDEX.md")
|
|
72
|
+
}
|