wayform 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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +262 -0
  3. package/dist/cli.js +65 -0
  4. package/dist/cli.js.map +1 -0
  5. package/dist/config.js +174 -0
  6. package/dist/config.js.map +1 -0
  7. package/dist/context-format.js +18 -0
  8. package/dist/context-format.js.map +1 -0
  9. package/dist/doctor.js +182 -0
  10. package/dist/doctor.js.map +1 -0
  11. package/dist/frontmatter.js +50 -0
  12. package/dist/frontmatter.js.map +1 -0
  13. package/dist/git-repo.js +166 -0
  14. package/dist/git-repo.js.map +1 -0
  15. package/dist/hook-clients.js +90 -0
  16. package/dist/hook-clients.js.map +1 -0
  17. package/dist/hook.js +95 -0
  18. package/dist/hook.js.map +1 -0
  19. package/dist/index.js +213 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/init-configs.js +119 -0
  22. package/dist/init-configs.js.map +1 -0
  23. package/dist/init-env.js +70 -0
  24. package/dist/init-env.js.map +1 -0
  25. package/dist/init-remote.js +155 -0
  26. package/dist/init-remote.js.map +1 -0
  27. package/dist/init.js +132 -0
  28. package/dist/init.js.map +1 -0
  29. package/dist/is-main.js +14 -0
  30. package/dist/is-main.js.map +1 -0
  31. package/dist/metrics.js +35 -0
  32. package/dist/metrics.js.map +1 -0
  33. package/dist/remote-read.js +57 -0
  34. package/dist/remote-read.js.map +1 -0
  35. package/dist/review-prompt.js +23 -0
  36. package/dist/review-prompt.js.map +1 -0
  37. package/dist/slug.js +19 -0
  38. package/dist/slug.js.map +1 -0
  39. package/dist/space-create.js +195 -0
  40. package/dist/space-create.js.map +1 -0
  41. package/dist/stop-hook.js +66 -0
  42. package/dist/stop-hook.js.map +1 -0
  43. package/dist/store.js +177 -0
  44. package/dist/store.js.map +1 -0
  45. package/dist/token-budget.js +35 -0
  46. package/dist/token-budget.js.map +1 -0
  47. package/package.json +39 -0
package/dist/store.js ADDED
@@ -0,0 +1,177 @@
1
+ import fs from "node:fs/promises";
2
+ import { existsSync } from "node:fs";
3
+ import path from "node:path";
4
+ import { randomUUID } from "node:crypto";
5
+ import { GitRepo } from "./git-repo.js";
6
+ import { serializeEntry, parseEntry, } from "./frontmatter.js";
7
+ import { DEFAULT_BUDGET_TOKENS, packToBudget } from "./token-budget.js";
8
+ import { slug, fsSafeTimestamp } from "./slug.js";
9
+ // Re-exported so existing importers (init, metrics, tests) keep working.
10
+ export { slug } from "./slug.js";
11
+ /** Length of the random id suffix appended to an entry's timestamped filename. */
12
+ const ID_LENGTH = 8;
13
+ /** Max length of the commit-subject summary derived from a payload's first line. */
14
+ const COMMIT_SUBJECT_MAX = 72;
15
+ /**
16
+ * Git-backed context store. Git is the consistency layer (append-only log,
17
+ * immutable commits, per-write attribution). This class owns orchestration and
18
+ * path/identity logic; git plumbing lives in GitRepo and the entry markdown
19
+ * format in frontmatter.ts. Each write is its own file under
20
+ * context/<project>/<author>/, so concurrent writers never touch the same file.
21
+ */
22
+ export class ContextStore {
23
+ cfg;
24
+ repo;
25
+ // Serializes every git-touching operation for this clone. Two concurrent
26
+ // write()s (or a read racing a write) would otherwise collide on
27
+ // .git/index.lock, since each git command is a separate child process.
28
+ queue = Promise.resolve();
29
+ constructor(cfg) {
30
+ this.cfg = cfg;
31
+ this.repo = new GitRepo(cfg);
32
+ }
33
+ serialize(fn) {
34
+ const run = this.queue.then(fn, fn);
35
+ this.queue = run.catch(() => undefined);
36
+ return run;
37
+ }
38
+ /** Clone the shared repo if absent and pin the commit-author identity. */
39
+ async ensure() {
40
+ await this.repo.ensure();
41
+ }
42
+ projectDir(project) {
43
+ return path.join("context", slug(project));
44
+ }
45
+ /** Email used for a given author: the configured one for the owner, else derived. */
46
+ emailFor(author) {
47
+ return author === this.cfg.author
48
+ ? this.cfg.authorEmail
49
+ : `${slug(author)}@memorylayer.local`;
50
+ }
51
+ /** Append a decision/context entry as its own file, commit it, and push. */
52
+ write(project, entry) {
53
+ return this.serialize(() => this.writeImpl(project, entry));
54
+ }
55
+ async writeImpl(project, entry) {
56
+ await this.repo.pull();
57
+ await this.reconcileOrphanedEntries();
58
+ const timestamp = new Date().toISOString();
59
+ const id = randomUUID().slice(0, ID_LENGTH);
60
+ const authorEmail = this.emailFor(entry.author);
61
+ const relDir = path.join(this.projectDir(project), slug(entry.author));
62
+ const relFile = path.join(relDir, `${fsSafeTimestamp(timestamp)}-${id}.md`);
63
+ const absFile = path.join(this.cfg.repoPath, relFile);
64
+ const contents = serializeEntry({ author: entry.author, type: entry.type, timestamp, id, project }, entry.payload);
65
+ await fs.mkdir(path.dirname(absFile), { recursive: true });
66
+ await fs.writeFile(absFile, contents, "utf8");
67
+ await this.repo.commitFile(relFile, `${entry.type}(${slug(project)}): ${firstLine(entry.payload)}`, entry.author, authorEmail);
68
+ await this.repo.push();
69
+ return {
70
+ author: entry.author,
71
+ type: entry.type,
72
+ timestamp,
73
+ id,
74
+ payload: entry.payload.trim(),
75
+ file: relFile,
76
+ };
77
+ }
78
+ /**
79
+ * Pull latest, gather a project's entries in write order, and return as
80
+ * many of the most recent ones as fit `budgetTokens` plus the total count.
81
+ * Packing to a token budget (rather than a fixed entry count) keeps a read
82
+ * from flooding the agent's context as the store grows — long entries cost
83
+ * more of the budget than short ones — which would otherwise make agents
84
+ * skip reads and break the "already there, unpasted" loop.
85
+ *
86
+ * Also self-heals: pushes any decision committed locally but not yet shared
87
+ * (e.g. a prior write whose push failed while offline), so a stranded entry
88
+ * reaches collaborators on the next read instead of never.
89
+ */
90
+ read(project, budgetTokens = DEFAULT_BUDGET_TOKENS) {
91
+ return this.serialize(() => this.readImpl(project, budgetTokens));
92
+ }
93
+ async readImpl(project, budgetTokens) {
94
+ await this.repo.pull();
95
+ await this.reconcileOrphanedEntries();
96
+ await this.repo.selfHealPush();
97
+ const absDir = path.join(this.cfg.repoPath, this.projectDir(project));
98
+ if (!existsSync(absDir))
99
+ return { entries: [], total: 0 };
100
+ const files = await collectMarkdown(absDir);
101
+ const entries = [];
102
+ for (const abs of files) {
103
+ const raw = await fs.readFile(abs, "utf8");
104
+ const parsed = parseEntry(raw, path.relative(this.cfg.repoPath, abs));
105
+ if (parsed)
106
+ entries.push(parsed);
107
+ }
108
+ const commitDates = await this.repo.firstCommitDates(this.projectDir(project));
109
+ entries.sort((a, b) => compareEntries(a, b, commitDates));
110
+ return {
111
+ entries: packToBudget(entries, budgetTokens),
112
+ total: entries.length,
113
+ };
114
+ }
115
+ async reconcileOrphanedEntries() {
116
+ const files = (await this.repo.untrackedFiles("context")).filter((file) => file.endsWith(".md"));
117
+ for (const relFile of files) {
118
+ const absFile = path.join(this.cfg.repoPath, relFile);
119
+ const raw = await fs.readFile(absFile, "utf8");
120
+ const parsed = parseEntry(raw, relFile);
121
+ if (!parsed)
122
+ continue;
123
+ const project = relFile.split("/")[1] || "unknown";
124
+ await this.repo.commitFile(relFile, `${parsed.type}(${project}): ${firstLine(parsed.payload)}`, parsed.author, this.emailFor(parsed.author));
125
+ }
126
+ }
127
+ /**
128
+ * Commit and best-effort push the caller's metrics append log (written by
129
+ * recordMetric). Runs inside the same per-clone git mutex as read/write so
130
+ * metrics git never races entry git on .git/index.lock. Called ONLY after a
131
+ * write — it carries every read metric appended since the last write in one
132
+ * commit ("append local, flush on next write"). Best-effort: swallows errors
133
+ * so a failed push never surfaces on the write path; unpushed lines ride the
134
+ * next flush (or a read's selfHealPush). No-op if nothing was recorded.
135
+ */
136
+ flushMetrics() {
137
+ return this.serialize(() => this.flushMetricsImpl());
138
+ }
139
+ async flushMetricsImpl() {
140
+ const relFile = path.join("metrics", `${slug(this.cfg.author)}.jsonl`);
141
+ const absFile = path.join(this.cfg.repoPath, relFile);
142
+ if (!existsSync(absFile))
143
+ return; // nothing recorded yet
144
+ try {
145
+ await this.repo.commitFile(relFile, `metrics: sync ${slug(this.cfg.author)}`, this.cfg.author, this.cfg.authorEmail);
146
+ await this.repo.push();
147
+ }
148
+ catch {
149
+ // Best-effort: unpushed metrics ride the next flush / read selfHealPush.
150
+ }
151
+ }
152
+ }
153
+ function firstLine(s) {
154
+ return s.trim().split("\n")[0].slice(0, COMMIT_SUBJECT_MAX);
155
+ }
156
+ function compareEntries(a, b, commitDates) {
157
+ const aOrder = commitDates.get(a.file) || a.timestamp;
158
+ const bOrder = commitDates.get(b.file) || b.timestamp;
159
+ return aOrder === bOrder
160
+ ? a.file.localeCompare(b.file)
161
+ : aOrder.localeCompare(bOrder);
162
+ }
163
+ async function collectMarkdown(dir) {
164
+ const out = [];
165
+ const entries = await fs.readdir(dir, { withFileTypes: true });
166
+ for (const e of entries) {
167
+ const abs = path.join(dir, e.name);
168
+ if (e.isDirectory()) {
169
+ out.push(...(await collectMarkdown(abs)));
170
+ }
171
+ else if (e.isFile() && e.name.endsWith(".md")) {
172
+ out.push(abs);
173
+ }
174
+ }
175
+ return out;
176
+ }
177
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EACL,cAAc,EACd,UAAU,GAGX,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACxE,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAClD,yEAAyE;AACzE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAMjC,kFAAkF;AAClF,MAAM,SAAS,GAAG,CAAC,CAAC;AACpB,oFAAoF;AACpF,MAAM,kBAAkB,GAAG,EAAE,CAAC;AAE9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CAAc,EACd,CAAc,EACd,WAAgC;IAEhC,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC;IACtD,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC;IACtD,OAAO,MAAM,KAAK,MAAM;QACtB,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC;QAC9B,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;AACnC,CAAC;AAED,KAAK,UAAU,eAAe,CAAC,GAAW;IACxC,MAAM,GAAG,GAAa,EAAE,CAAC;IACzB,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/D,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC;QACnC,IAAI,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC;YACpB,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC5C,CAAC;aAAM,IAAI,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YAChD,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAChB,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Cheap token estimate (chars/4) so the read path can budget context size
3
+ * without a tokenizer dependency. Approximate by design: this is a soft
4
+ * budget, not an exact accounting — over-estimating trims one entry short,
5
+ * under-estimating is the failure mode that actually matters, so the ratio
6
+ * errs conservative (4 chars/token is the standard rough English average).
7
+ */
8
+ export function estimateTokens(text) {
9
+ return Math.ceil(text.length / 4);
10
+ }
11
+ /** Default token budget for a read when the caller doesn't specify one. */
12
+ export const DEFAULT_BUDGET_TOKENS = 4000;
13
+ /** Approx fixed overhead per rendered entry block (header line + separator). */
14
+ export const ENTRY_OVERHEAD_TOKENS = 12;
15
+ /**
16
+ * Select the most recent entries that fit `budgetTokens`, walking newest to
17
+ * oldest. Always keeps at least the single most recent entry — an oversized
18
+ * entry beats an empty read. `budgetTokens <= 0` means unlimited (returns
19
+ * every entry), preserving the old count-cap's `limit <= 0` escape hatch.
20
+ */
21
+ export function packToBudget(entries, budgetTokens) {
22
+ if (budgetTokens <= 0 || entries.length === 0)
23
+ return entries;
24
+ const selected = [];
25
+ let used = 0;
26
+ for (let i = entries.length - 1; i >= 0; i--) {
27
+ const cost = estimateTokens(entries[i].payload) + ENTRY_OVERHEAD_TOKENS;
28
+ if (selected.length > 0 && used + cost > budgetTokens)
29
+ break;
30
+ selected.push(entries[i]);
31
+ used += cost;
32
+ }
33
+ return selected.reverse();
34
+ }
35
+ //# sourceMappingURL=token-budget.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-budget.js","sourceRoot":"","sources":["../src/token-budget.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACpC,CAAC;AAED,2EAA2E;AAC3E,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,CAAC;AAE1C,gFAAgF;AAChF,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAIxC;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAC1B,OAAsB,EACtB,YAAoB;IAEpB,IAAI,YAAY,IAAI,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,OAAO,CAAC;IAE9D,MAAM,QAAQ,GAAkB,EAAE,CAAC;IACnC,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7C,MAAM,IAAI,GAAG,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,qBAAqB,CAAC;QACxE,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,GAAG,IAAI,GAAG,YAAY;YAAE,MAAM;QAC7D,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1B,IAAI,IAAI,IAAI,CAAC;IACf,CAAC;IACD,OAAO,QAAQ,CAAC,OAAO,EAAE,CAAC;AAC5B,CAAC"}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "wayform",
3
+ "version": "0.1.1",
4
+ "description": "Vendor-neutral multiplayer planning memory: a git-backed MCP server that shares planning context across Claude, Cursor, and Codex.",
5
+ "type": "module",
6
+ "bin": {
7
+ "wayform": "dist/cli.js"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "dev": "tsc --watch",
15
+ "start": "node dist/index.js",
16
+ "typecheck": "tsc --noEmit",
17
+ "lint": "eslint src",
18
+ "format": "prettier --write .",
19
+ "format:check": "prettier --check .",
20
+ "test": "npm run build && node --test test/*.test.mjs",
21
+ "test:gateway": "cd gateway && npm test"
22
+ },
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "dependencies": {
27
+ "@modelcontextprotocol/sdk": "^1.12.0",
28
+ "simple-git": "^3.27.0",
29
+ "zod": "^3.23.8"
30
+ },
31
+ "devDependencies": {
32
+ "@eslint/js": "^9.39.4",
33
+ "@types/node": "^22.10.0",
34
+ "eslint": "^9.39.4",
35
+ "prettier": "^3.9.4",
36
+ "typescript": "^5.7.0",
37
+ "typescript-eslint": "^8.62.1"
38
+ }
39
+ }