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.
- package/LICENSE +21 -0
- package/README.md +262 -0
- package/dist/cli.js +65 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.js +174 -0
- package/dist/config.js.map +1 -0
- package/dist/context-format.js +18 -0
- package/dist/context-format.js.map +1 -0
- package/dist/doctor.js +182 -0
- package/dist/doctor.js.map +1 -0
- package/dist/frontmatter.js +50 -0
- package/dist/frontmatter.js.map +1 -0
- package/dist/git-repo.js +166 -0
- package/dist/git-repo.js.map +1 -0
- package/dist/hook-clients.js +90 -0
- package/dist/hook-clients.js.map +1 -0
- package/dist/hook.js +95 -0
- package/dist/hook.js.map +1 -0
- package/dist/index.js +213 -0
- package/dist/index.js.map +1 -0
- package/dist/init-configs.js +119 -0
- package/dist/init-configs.js.map +1 -0
- package/dist/init-env.js +70 -0
- package/dist/init-env.js.map +1 -0
- package/dist/init-remote.js +155 -0
- package/dist/init-remote.js.map +1 -0
- package/dist/init.js +132 -0
- package/dist/init.js.map +1 -0
- package/dist/is-main.js +14 -0
- package/dist/is-main.js.map +1 -0
- package/dist/metrics.js +35 -0
- package/dist/metrics.js.map +1 -0
- package/dist/remote-read.js +57 -0
- package/dist/remote-read.js.map +1 -0
- package/dist/review-prompt.js +23 -0
- package/dist/review-prompt.js.map +1 -0
- package/dist/slug.js +19 -0
- package/dist/slug.js.map +1 -0
- package/dist/space-create.js +195 -0
- package/dist/space-create.js.map +1 -0
- package/dist/stop-hook.js +66 -0
- package/dist/stop-hook.js.map +1 -0
- package/dist/store.js +177 -0
- package/dist/store.js.map +1 -0
- package/dist/token-budget.js +35 -0
- package/dist/token-budget.js.map +1 -0
- package/package.json +39 -0
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { execFile } from "node:child_process";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { defaultProject, loadConfig, normalizeRepoUrl, } from "./config.js";
|
|
6
|
+
const execFileAsync = promisify(execFile);
|
|
7
|
+
const REQUIRED_ENV_KEYS = ["CONTEXT_REPO_URL", "MEMORYLAYER_AUTHOR"];
|
|
8
|
+
export async function runDoctor(options = {}) {
|
|
9
|
+
const cwd = options.cwd ?? process.cwd();
|
|
10
|
+
const setExitCode = options.setExitCode ?? true;
|
|
11
|
+
const write = options.write ?? ((line) => process.stdout.write(`${line}\n`));
|
|
12
|
+
const gitRunner = options.gitRunner ?? runGit;
|
|
13
|
+
const results = [];
|
|
14
|
+
results.push(checkEnvFile(cwd, options.env ?? process.env));
|
|
15
|
+
let cfg;
|
|
16
|
+
try {
|
|
17
|
+
cfg = options.config ?? loadConfig();
|
|
18
|
+
results.push({
|
|
19
|
+
status: "ok",
|
|
20
|
+
name: "config",
|
|
21
|
+
message: `loaded for ${cfg.author}`,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
results.push({
|
|
26
|
+
status: "fail",
|
|
27
|
+
name: "config",
|
|
28
|
+
message: redactSecrets(err instanceof Error ? err.message : String(err)),
|
|
29
|
+
});
|
|
30
|
+
printResults(results, write);
|
|
31
|
+
if (setExitCode)
|
|
32
|
+
process.exitCode = 1;
|
|
33
|
+
return 1;
|
|
34
|
+
}
|
|
35
|
+
results.push(await checkClone(cfg, gitRunner));
|
|
36
|
+
results.push(await checkRemote(cfg, gitRunner));
|
|
37
|
+
results.push(await checkSync(cfg, gitRunner));
|
|
38
|
+
results.push(checkProject(cwd));
|
|
39
|
+
printResults(results, write);
|
|
40
|
+
const failed = results.some((result) => result.status === "fail");
|
|
41
|
+
if (setExitCode)
|
|
42
|
+
process.exitCode = failed ? 1 : 0;
|
|
43
|
+
return failed ? 1 : 0;
|
|
44
|
+
}
|
|
45
|
+
export function checkEnvFile(cwd, env) {
|
|
46
|
+
const file = path.join(cwd, ".memorylayer-hook.env");
|
|
47
|
+
if (!fs.existsSync(file)) {
|
|
48
|
+
return {
|
|
49
|
+
status: "fail",
|
|
50
|
+
name: "env file",
|
|
51
|
+
message: ".memorylayer-hook.env is missing",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const text = fs.readFileSync(file, "utf8");
|
|
55
|
+
const keys = new Set(text
|
|
56
|
+
.split(/\r?\n/)
|
|
57
|
+
.map((line) => line.trim())
|
|
58
|
+
.filter((line) => line && !line.startsWith("#"))
|
|
59
|
+
.map((line) => line.slice(0, line.indexOf("=")).trim())
|
|
60
|
+
.filter(Boolean));
|
|
61
|
+
const missing = REQUIRED_ENV_KEYS.filter((key) => !keys.has(key) && !env[key]?.trim());
|
|
62
|
+
if (missing.length > 0) {
|
|
63
|
+
return {
|
|
64
|
+
status: "fail",
|
|
65
|
+
name: "env file",
|
|
66
|
+
message: `missing ${missing.join(", ")}`,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
status: "ok",
|
|
71
|
+
name: "env file",
|
|
72
|
+
message: "required keys are present",
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export async function checkClone(cfg, gitRunner) {
|
|
76
|
+
if (!fs.existsSync(path.join(cfg.repoPath, ".git"))) {
|
|
77
|
+
return {
|
|
78
|
+
status: "fail",
|
|
79
|
+
name: "clone",
|
|
80
|
+
message: `clone missing at ${cfg.repoPath}`,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
const origin = (await gitRunner(cfg.repoPath, ["remote", "get-url", "origin"])).trim();
|
|
85
|
+
const matches = normalizeRepoUrl(origin) === normalizeRepoUrl(cfg.repoUrl);
|
|
86
|
+
return matches
|
|
87
|
+
? {
|
|
88
|
+
status: "ok",
|
|
89
|
+
name: "clone",
|
|
90
|
+
message: `origin matches ${redactSecrets(origin)}`,
|
|
91
|
+
}
|
|
92
|
+
: {
|
|
93
|
+
status: "fail",
|
|
94
|
+
name: "clone",
|
|
95
|
+
message: `origin ${redactSecrets(origin)} does not match configured repo`,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
return {
|
|
100
|
+
status: "fail",
|
|
101
|
+
name: "clone",
|
|
102
|
+
message: redactSecrets(err instanceof Error ? err.message : String(err)),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export async function checkRemote(cfg, gitRunner) {
|
|
107
|
+
try {
|
|
108
|
+
await gitRunner(undefined, [
|
|
109
|
+
"ls-remote",
|
|
110
|
+
"--exit-code",
|
|
111
|
+
cfg.repoUrl,
|
|
112
|
+
"HEAD",
|
|
113
|
+
]);
|
|
114
|
+
return {
|
|
115
|
+
status: "ok",
|
|
116
|
+
name: "remote",
|
|
117
|
+
message: `reachable ${redactSecrets(cfg.repoUrl)}`,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
return {
|
|
122
|
+
status: "fail",
|
|
123
|
+
name: "remote",
|
|
124
|
+
message: redactSecrets(err instanceof Error ? err.message : String(err)),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export async function checkSync(cfg, gitRunner) {
|
|
129
|
+
if (!fs.existsSync(path.join(cfg.repoPath, ".git"))) {
|
|
130
|
+
return {
|
|
131
|
+
status: "warn",
|
|
132
|
+
name: "sync",
|
|
133
|
+
message: "clone missing; run a read or write first",
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const branch = (await gitRunner(cfg.repoPath, ["rev-parse", "--abbrev-ref", "HEAD"])).trim();
|
|
138
|
+
const counts = (await gitRunner(cfg.repoPath, [
|
|
139
|
+
"rev-list",
|
|
140
|
+
"--left-right",
|
|
141
|
+
"--count",
|
|
142
|
+
`origin/${branch}...HEAD`,
|
|
143
|
+
]))
|
|
144
|
+
.trim()
|
|
145
|
+
.split(/\s+/)
|
|
146
|
+
.map(Number);
|
|
147
|
+
const [behind = 0, ahead = 0] = counts;
|
|
148
|
+
return {
|
|
149
|
+
status: ahead > 0 ? "warn" : "ok",
|
|
150
|
+
name: "sync",
|
|
151
|
+
message: `${ahead} unpushed commit(s), ${behind} remote commit(s) not pulled`,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
return {
|
|
156
|
+
status: "warn",
|
|
157
|
+
name: "sync",
|
|
158
|
+
message: redactSecrets(err instanceof Error ? err.message : String(err)),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
export function checkProject(cwd) {
|
|
163
|
+
return {
|
|
164
|
+
status: "ok",
|
|
165
|
+
name: "project",
|
|
166
|
+
message: `default project is ${defaultProject(cwd)}`,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
export function redactSecrets(message) {
|
|
170
|
+
return message.replace(/(https?:\/\/)([^@\s/]+)@/g, "$1***@");
|
|
171
|
+
}
|
|
172
|
+
async function runGit(cwd, args) {
|
|
173
|
+
const { stdout } = await execFileAsync("git", args, { cwd });
|
|
174
|
+
return stdout;
|
|
175
|
+
}
|
|
176
|
+
function printResults(results, write) {
|
|
177
|
+
write("Wayform doctor");
|
|
178
|
+
for (const result of results) {
|
|
179
|
+
write(`[${result.status}] ${result.name}: ${result.message}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=doctor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"doctor.js","sourceRoot":"","sources":["../src/doctor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EACL,cAAc,EACd,UAAU,EACV,gBAAgB,GAEjB,MAAM,aAAa,CAAC;A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E,aAAa,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;SACzE,CAAC;IACJ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,GAAW;IACtC,OAAO;QACL,MAAM,EAAE,IAAI;QACZ,IAAI,EAAE,SAAS;QACf,OAAO,EAAE,sBAAsB,cAAc,CAAC,GAAG,CAAC,EAAE;KACrD,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,OAAe;IAC3C,OAAO,OAAO,CAAC,OAAO,CAAC,2BAA2B,EAAE,QAAQ,CAAC,CAAC;AAChE,CAAC;AAED,KAAK,UAAU,MAAM,CACnB,GAAuB,EACvB,IAAc;IAEd,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,aAAa,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;IAC7D,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,YAAY,CACnB,OAAsB,EACtB,KAA6B;IAE7B,KAAK,CAAC,gBAAgB,CAAC,CAAC;IACxB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,KAAK,CAAC,IAAI,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,KAAK,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;IAChE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry markdown shape: one source of truth for how an entry is serialized
|
|
3
|
+
* to disk (write path) and parsed back (read path). Keeping both here means the
|
|
4
|
+
* two can never drift into incompatible formats — the same reason the projection
|
|
5
|
+
* lives once in context-format.ts.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Render an entry to its on-disk markdown: a frontmatter block followed by the
|
|
9
|
+
* trimmed payload. This is the ONLY place the write format is defined.
|
|
10
|
+
*/
|
|
11
|
+
export function serializeEntry(fm, payload) {
|
|
12
|
+
return (`---\n` +
|
|
13
|
+
`author: ${fm.author}\n` +
|
|
14
|
+
`type: ${fm.type}\n` +
|
|
15
|
+
`timestamp: ${fm.timestamp}\n` +
|
|
16
|
+
`id: ${fm.id}\n` +
|
|
17
|
+
`project: ${fm.project}\n` +
|
|
18
|
+
`---\n\n` +
|
|
19
|
+
`${payload.trim()}\n`);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parse an on-disk entry back into a ParsedEntry, or null if it is malformed
|
|
23
|
+
* (no frontmatter, or missing the required author/timestamp). This is the ONLY
|
|
24
|
+
* place the read format is defined.
|
|
25
|
+
*/
|
|
26
|
+
export function parseEntry(raw, file) {
|
|
27
|
+
const match = raw.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
|
|
28
|
+
if (!match)
|
|
29
|
+
return null;
|
|
30
|
+
const front = Object.fromEntries(match[1]
|
|
31
|
+
.split("\n")
|
|
32
|
+
.map((line) => {
|
|
33
|
+
const idx = line.indexOf(":");
|
|
34
|
+
return idx === -1
|
|
35
|
+
? null
|
|
36
|
+
: [line.slice(0, idx).trim(), line.slice(idx + 1).trim()];
|
|
37
|
+
})
|
|
38
|
+
.filter((x) => x !== null));
|
|
39
|
+
if (!front.timestamp || !front.author)
|
|
40
|
+
return null;
|
|
41
|
+
return {
|
|
42
|
+
author: front.author,
|
|
43
|
+
type: front.type || "context",
|
|
44
|
+
timestamp: front.timestamp,
|
|
45
|
+
id: front.id || "",
|
|
46
|
+
payload: match[2].trim(),
|
|
47
|
+
file,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=frontmatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frontmatter.js","sourceRoot":"","sources":["../src/frontmatter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA8BH;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,EAAoB,EAAE,O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AS;QAC1B,EAAE,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE;QAClB,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE;QACxB,IAAI;KACL,CAAC;AACJ,CAAC"}
|
package/dist/git-repo.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { simpleGit } from "simple-git";
|
|
5
|
+
const PUSH_ATTEMPTS = 3;
|
|
6
|
+
const PUSH_RETRY_DELAY_MS = 25;
|
|
7
|
+
/**
|
|
8
|
+
* Thin wrapper over the git plumbing the store needs: clone-if-absent, pull,
|
|
9
|
+
* single-file commit with explicit authorship, and push with a rebase-retry.
|
|
10
|
+
* It owns NO markdown, path, or projection logic — those live in the store and
|
|
11
|
+
* frontmatter modules. Serialization of concurrent git operations is the
|
|
12
|
+
* caller's responsibility (ContextStore holds the per-clone mutex).
|
|
13
|
+
*/
|
|
14
|
+
export class GitRepo {
|
|
15
|
+
cfg;
|
|
16
|
+
git;
|
|
17
|
+
constructor(cfg) {
|
|
18
|
+
this.cfg = cfg;
|
|
19
|
+
this.git = simpleGit();
|
|
20
|
+
}
|
|
21
|
+
/** Clone the shared repo if absent, then pin the commit-author identity. */
|
|
22
|
+
async ensure() {
|
|
23
|
+
const { repoPath, repoUrl, author, authorEmail } = this.cfg;
|
|
24
|
+
if (!existsSync(path.join(repoPath, ".git"))) {
|
|
25
|
+
await fs.mkdir(path.dirname(repoPath), { recursive: true });
|
|
26
|
+
await simpleGit().clone(repoUrl, repoPath);
|
|
27
|
+
}
|
|
28
|
+
this.git = simpleGit(repoPath);
|
|
29
|
+
await this.reconcileOrigin(repoUrl);
|
|
30
|
+
await this.git.addConfig("user.name", author);
|
|
31
|
+
await this.git.addConfig("user.email", authorEmail);
|
|
32
|
+
}
|
|
33
|
+
async reconcileOrigin(repoUrl) {
|
|
34
|
+
// Reconcile origin to the configured repoUrl every run: declared config is
|
|
35
|
+
// the source of truth, so a mis-pointed clone (e.g. a shared path from an
|
|
36
|
+
// older layout) or a rotated token self-corrects here instead of silently
|
|
37
|
+
// pushing to the wrong remote. A manually-created .git may not have origin
|
|
38
|
+
// yet, so add it instead of throwing during MCP startup.
|
|
39
|
+
const remotes = await this.git.getRemotes(true);
|
|
40
|
+
if (remotes.some((r) => r.name === "origin")) {
|
|
41
|
+
await this.git.remote(["set-url", "origin", repoUrl]);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
await this.git.addRemote("origin", repoUrl);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async currentBranch() {
|
|
48
|
+
return (await this.git.revparse(["--abbrev-ref", "HEAD"])).trim() || "main";
|
|
49
|
+
}
|
|
50
|
+
/** Rebase-pull the latest. Silent on failure (empty remote/no upstream/offline). */
|
|
51
|
+
async pull() {
|
|
52
|
+
try {
|
|
53
|
+
const branch = await this.currentBranch();
|
|
54
|
+
await this.git.pull("origin", branch, ["--rebase"]);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Empty remote, no upstream yet, or offline: local reads/writes still work.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Stage and commit exactly one file under a specific author. The explicit
|
|
62
|
+
* pathspec keeps a concurrent write's staged file out of this commit.
|
|
63
|
+
*/
|
|
64
|
+
async commitFile(relFile, message, authorName, authorEmail) {
|
|
65
|
+
await this.git.add(relFile);
|
|
66
|
+
await this.git.commit(message, relFile, {
|
|
67
|
+
"--author": `${authorName} <${authorEmail}>`,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Push, or if rejected (non-fast-forward/offline) rebase on latest and retry
|
|
72
|
+
* once. Per-author files make the rebase clean. Throws an honest error if it
|
|
73
|
+
* still can't reach the remote, so the caller can tell the user it's local-only.
|
|
74
|
+
*/
|
|
75
|
+
async push() {
|
|
76
|
+
if (!this.cfg.autoPush)
|
|
77
|
+
return;
|
|
78
|
+
const branch = await this.currentBranch();
|
|
79
|
+
let lastError;
|
|
80
|
+
for (let attempt = 0; attempt < PUSH_ATTEMPTS; attempt++) {
|
|
81
|
+
try {
|
|
82
|
+
if (attempt > 0) {
|
|
83
|
+
await delay(PUSH_RETRY_DELAY_MS * 2 ** (attempt - 1));
|
|
84
|
+
}
|
|
85
|
+
await this.git.push(["-u", "origin", branch]);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
lastError = err;
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
await this.git.pull("origin", branch, ["--rebase"]);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
lastError = err;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
throw new Error(`Recorded locally, NOT shared yet — push failed, will retry on the ` +
|
|
99
|
+
`next read/write: ${redactSecrets(String(lastError.message ?? lastError))}`);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Best-effort push of anything committed locally but not yet on the remote.
|
|
103
|
+
* Called from a read so a write whose push failed earlier still reaches
|
|
104
|
+
* collaborators. Silent on failure — it stays local and retries next read.
|
|
105
|
+
*/
|
|
106
|
+
async selfHealPush() {
|
|
107
|
+
if (!this.cfg.autoPush)
|
|
108
|
+
return;
|
|
109
|
+
try {
|
|
110
|
+
await this.push();
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// Still offline / unauthorized: entry remains local, retried next read.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** Untracked files under a pathspec, relative to repo root. */
|
|
117
|
+
async untrackedFiles(pathspec) {
|
|
118
|
+
const raw = await this.git.raw([
|
|
119
|
+
"ls-files",
|
|
120
|
+
"--others",
|
|
121
|
+
"--exclude-standard",
|
|
122
|
+
"--",
|
|
123
|
+
pathspec,
|
|
124
|
+
]);
|
|
125
|
+
return raw
|
|
126
|
+
.split(/\r?\n/)
|
|
127
|
+
.map((s) => s.trim())
|
|
128
|
+
.filter(Boolean);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Return the committer date of the commit that first introduced each file.
|
|
132
|
+
* The read path uses this as git-integrated order, avoiding wall-clock skew
|
|
133
|
+
* in entry frontmatter from different machines.
|
|
134
|
+
*/
|
|
135
|
+
async firstCommitDates(pathspec) {
|
|
136
|
+
const raw = await this.git.raw([
|
|
137
|
+
"log",
|
|
138
|
+
"--diff-filter=A",
|
|
139
|
+
"--name-only",
|
|
140
|
+
"--format=__ML_COMMIT__%cI",
|
|
141
|
+
"--",
|
|
142
|
+
pathspec,
|
|
143
|
+
]);
|
|
144
|
+
const out = new Map();
|
|
145
|
+
let currentDate = "";
|
|
146
|
+
for (const rawLine of raw.split(/\r?\n/)) {
|
|
147
|
+
const line = rawLine.trim();
|
|
148
|
+
if (!line)
|
|
149
|
+
continue;
|
|
150
|
+
if (line.startsWith("__ML_COMMIT__")) {
|
|
151
|
+
currentDate = line.slice("__ML_COMMIT__".length);
|
|
152
|
+
}
|
|
153
|
+
else if (currentDate && !out.has(line)) {
|
|
154
|
+
out.set(line, currentDate);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function delay(ms) {
|
|
161
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
162
|
+
}
|
|
163
|
+
function redactSecrets(message) {
|
|
164
|
+
return message.replace(/(https?:\/\/)([^@\s/]+)@/g, "$1***@");
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=git-repo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-repo.js","sourceRoot":"","sources":["../src/git-repo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,IAAI,MAA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;AAED,SAAS,aAAa,CAAC,OAAe;IACpC,OAAO,OAAO,CAAC,OAAO,CAAC,2BAA2B,EAAE,QAAQ,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/** Map the configured client string to a known adapter; default to Cursor. */
|
|
2
|
+
export function resolveClient(raw) {
|
|
3
|
+
switch ((raw ?? "").trim().toLowerCase()) {
|
|
4
|
+
case "claude-code":
|
|
5
|
+
case "claude_code":
|
|
6
|
+
case "claudecode":
|
|
7
|
+
return "claude-code";
|
|
8
|
+
case "raw":
|
|
9
|
+
return "raw";
|
|
10
|
+
case "codex":
|
|
11
|
+
return "codex";
|
|
12
|
+
default:
|
|
13
|
+
return "cursor";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Envelope emitted when there IS context to inject. */
|
|
17
|
+
export function renderContext(client, text) {
|
|
18
|
+
switch (client) {
|
|
19
|
+
case "raw":
|
|
20
|
+
// No envelope: clients whose start hook injects stdout verbatim.
|
|
21
|
+
return text;
|
|
22
|
+
case "claude-code":
|
|
23
|
+
return JSON.stringify({
|
|
24
|
+
hookSpecificOutput: {
|
|
25
|
+
hookEventName: "SessionStart",
|
|
26
|
+
additionalContext: text,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
case "codex":
|
|
30
|
+
// Codex SessionStart injection is byte-identical to Claude Code today, but
|
|
31
|
+
// kept a separate case so a future divergence in either tool is a one-line
|
|
32
|
+
// change (see spec decision a).
|
|
33
|
+
return JSON.stringify({
|
|
34
|
+
hookSpecificOutput: {
|
|
35
|
+
hookEventName: "SessionStart",
|
|
36
|
+
additionalContext: text,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
case "cursor":
|
|
40
|
+
return JSON.stringify({ additional_context: text });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Envelope emitted when there is NOTHING to inject (empty store, or the
|
|
45
|
+
* fail-open path). Must be a valid no-op for the client so the session is
|
|
46
|
+
* untouched.
|
|
47
|
+
*/
|
|
48
|
+
export function renderEmpty(client) {
|
|
49
|
+
return client === "raw" ? "" : "{}";
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Stop-hook envelope that ASKS the model to self-review (write-trigger Path C).
|
|
53
|
+
*
|
|
54
|
+
* Unlike SessionStart, the Stop event can re-engage the model: Claude Code accepts
|
|
55
|
+
* `hookSpecificOutput.additionalContext` on Stop as non-error feedback that continues
|
|
56
|
+
* the conversation. `raw` emits the text verbatim.
|
|
57
|
+
*
|
|
58
|
+
* Cursor and Codex are now supported: Cursor re-engages via followup_message,
|
|
59
|
+
* Codex uses decision:block with reason to trigger a re-engagement. Self-review
|
|
60
|
+
* is available to all three tools; raw still emits verbatim.
|
|
61
|
+
*/
|
|
62
|
+
export function renderStopReview(client, text) {
|
|
63
|
+
switch (client) {
|
|
64
|
+
case "raw":
|
|
65
|
+
return text;
|
|
66
|
+
case "claude-code":
|
|
67
|
+
return JSON.stringify({
|
|
68
|
+
hookSpecificOutput: {
|
|
69
|
+
hookEventName: "Stop",
|
|
70
|
+
additionalContext: text,
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
case "codex":
|
|
74
|
+
// Codex Stop re-engages differently from Claude Code: decision:block makes
|
|
75
|
+
// `reason` the next user prompt.
|
|
76
|
+
return JSON.stringify({ decision: "block", reason: text });
|
|
77
|
+
case "cursor":
|
|
78
|
+
// Cursor Stop re-engages via followup_message (auto-submitted as next user
|
|
79
|
+
// message); loop protection is loop_count/loop_limit (see stop-hook + config).
|
|
80
|
+
return JSON.stringify({ followup_message: text });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Stop-hook no-op: let the turn end with no injected review. Used on the loop-guard
|
|
85
|
+
* path (stop_hook_active) and the fail-open path.
|
|
86
|
+
*/
|
|
87
|
+
export function renderStopNoop(client) {
|
|
88
|
+
return client === "raw" ? "" : "{}";
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=hook-clients.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hook-clients.js","sourceRoot":"","sources":["../src/hook-clients.ts"],"names":[],"mappings":"AAUA,8EAA8E;AAC9E,MAAM,UAAU,aAAa,CAAC,GAAuB;IACnD,QAAQ,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC;QACzC,KAAK,aAAa,CAAC;QACnB,KAAK,aAAa,CAAC;QACnB,KAAK,YAAY;YACf,OAAO,aAAa,CAAC;QACvB,KAAK,KAAK;YACR,OAAO,KAAK,CAAC;QACf,KAAK,OAAO;YACV,OAAO,OAAO,CAAC;QACjB;YACE,OAAO,QAAQ,CAAC;IACpB,CAAC;AACH,CAAC;AAED,wDAAwD;AACxD,MAAM,UAAU,aAAa,CAAC,MAAkB,EAAE,IAAY;IAC5D,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,KAAK;YACR,iEAAiE;YACjE,OAAO,IAAI,CAAC;QACd,KAAK,aAAa;YAChB,OAAO,IAAI,CAAC,SAAS,CAAC;gBACpB,kBAAkB,EAAE;oBAClB,aAAa,EAAE,cAAc;oBAC7B,iBAAiB,EAAE,IAAI;iBACxB;aACF,CAAC,CAAC;QACL,KAAK,OAAO;YACV,2EAA2E;YAC3E,2EAA2E;YAC3E,gCAAgC;YAChC,OAAO,IAAI,CAAC,SAAS,CAAC;gBACpB,kBAAkB,EAAE;oBAClB,aAAa,EAAE,cAAc;oBAC7B,iBAAiB,EAAE,IAAI;iBACxB;aACF,CAAC,CAAC;QACL,KAAK,QAAQ;YACX,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC,CAAC;IACxD,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,MAAkB;IAC5C,OAAO,MAAM,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AACtC,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAkB,EAAE,IAAY;IAC/D,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,KAAK;YACR,OAAO,IAAI,CAAC;QACd,KAAK,aAAa;YAChB,OAAO,IAAI,CAAC,SAAS,CAAC;gBACpB,kBAAkB,EAAE;oBAClB,aAAa,EAAE,MAAM;oBACrB,iBAAiB,EAAE,IAAI;iBACxB;aACF,CAAC,CAAC;QACL,KAAK,OAAO;YACV,2EAA2E;YAC3E,iCAAiC;YACjC,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;QAC7D,KAAK,QAAQ;YACX,2EAA2E;YAC3E,+EAA+E;YAC/E,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAC;IACtD,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,MAAkB;IAC/C,OAAO,MAAM,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AACtC,CAAC"}
|
package/dist/hook.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Session-start hook entrypoint.
|
|
4
|
+
*
|
|
5
|
+
* A client (Cursor `sessionStart`, Claude Code `SessionStart`, ...) runs this at
|
|
6
|
+
* the start of every new session. It pulls the shared planning memory for a
|
|
7
|
+
* project and prints it as injectable context, so a collaborator's decisions are
|
|
8
|
+
* already present without anyone reading or pasting anything — the whole point of
|
|
9
|
+
* the "unprompted read."
|
|
10
|
+
*
|
|
11
|
+
* Neutrality: everything here (store read + projection) is vendor-neutral. The
|
|
12
|
+
* ONLY per-vendor thing is the output envelope, isolated in ./hook-clients.ts and
|
|
13
|
+
* selected by MEMORYLAYER_HOOK_CLIENT (cursor | claude-code | raw). A new tool is
|
|
14
|
+
* one `case` there; this file never changes.
|
|
15
|
+
*
|
|
16
|
+
* FAIL-OPEN is the load-bearing property: a broken read (offline, bad config,
|
|
17
|
+
* empty store) must NEVER break the user's session. On any failure we emit the
|
|
18
|
+
* client's empty no-op and exit 0, so the session starts as if no hook existed.
|
|
19
|
+
*/
|
|
20
|
+
import { loadConfig, defaultProject } from "./config.js";
|
|
21
|
+
import { ContextStore } from "./store.js";
|
|
22
|
+
import { remoteHookRead } from "./remote-read.js";
|
|
23
|
+
import { projectContext } from "./context-format.js";
|
|
24
|
+
import { recordMetric } from "./metrics.js";
|
|
25
|
+
import { resolveClient, renderContext, renderEmpty, } from "./hook-clients.js";
|
|
26
|
+
import { isMain } from "./is-main.js";
|
|
27
|
+
export async function runHook() {
|
|
28
|
+
// Resolved here (not at module load) so the dispatcher can set
|
|
29
|
+
// MEMORYLAYER_HOOK_CLIENT from the subcommand arg before calling. Cannot throw,
|
|
30
|
+
// so both the success and fail-open paths always know which envelope to emit.
|
|
31
|
+
const client = resolveClient(process.env.MEMORYLAYER_HOOK_CLIENT);
|
|
32
|
+
const emitEmpty = () => {
|
|
33
|
+
process.stdout.write(renderEmpty(client));
|
|
34
|
+
process.exit(0);
|
|
35
|
+
};
|
|
36
|
+
/** Drain stdin so the client's write never blocks; the payload is unused today. */
|
|
37
|
+
const drainStdin = async () => {
|
|
38
|
+
if (process.stdin.isTTY)
|
|
39
|
+
return;
|
|
40
|
+
try {
|
|
41
|
+
for await (const _ of process.stdin) {
|
|
42
|
+
// discard
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// stdin not readable — irrelevant to producing context.
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
try {
|
|
50
|
+
await drainStdin();
|
|
51
|
+
const project = process.env.MEMORYLAYER_PROJECT?.trim() || defaultProject();
|
|
52
|
+
const cfg = loadConfig();
|
|
53
|
+
// Remote-first (§2.2): the gateway's index serves the read; the local
|
|
54
|
+
// clone is the offline fallback. remoteHookRead returns ready-to-inject
|
|
55
|
+
// text ("" = empty store) or null meaning "gateway unusable — fall back".
|
|
56
|
+
const remote = await remoteHookRead(cfg, project, cfg.readBudgetTokens);
|
|
57
|
+
if (remote !== null) {
|
|
58
|
+
await recordMetric(cfg, { source: "hook", event: "read", project });
|
|
59
|
+
if (remote === "")
|
|
60
|
+
emitEmpty();
|
|
61
|
+
process.stdout.write(renderContext(client, remote));
|
|
62
|
+
process.exit(0);
|
|
63
|
+
}
|
|
64
|
+
// Gateway-only member (no local clone): there is nothing to fall back to.
|
|
65
|
+
// Fail-open to the client's empty no-op rather than constructing a store
|
|
66
|
+
// against an empty repo path.
|
|
67
|
+
if (!cfg.repoUrl)
|
|
68
|
+
emitEmpty();
|
|
69
|
+
const store = new ContextStore(cfg);
|
|
70
|
+
await store.ensure();
|
|
71
|
+
const { entries, total } = await store.read(project, cfg.readBudgetTokens);
|
|
72
|
+
// Record the read before branching so an empty-store read still counts toward
|
|
73
|
+
// read-rate. recordMetric is internally fail-open (never throws), so it cannot
|
|
74
|
+
// divert the non-empty path into the catch/emitEmpty branch.
|
|
75
|
+
await recordMetric(cfg, { source: "hook", event: "read", project, total });
|
|
76
|
+
// An empty store has nothing worth injecting — start clean rather than pushing
|
|
77
|
+
// a "(no entries yet)" placeholder into every session.
|
|
78
|
+
if (total === 0)
|
|
79
|
+
emitEmpty();
|
|
80
|
+
const body = projectContext(project, entries, total);
|
|
81
|
+
const text = `The following is shared planning memory (Wayform) for project ` +
|
|
82
|
+
`"${project}", loaded automatically at session start. Treat these recorded ` +
|
|
83
|
+
`decisions and context as already-known; do not ask the user to re-explain ` +
|
|
84
|
+
`them.\n\n${body}`;
|
|
85
|
+
process.stdout.write(renderContext(client, text));
|
|
86
|
+
process.exit(0);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
emitEmpty();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (isMain(import.meta.url)) {
|
|
93
|
+
void runHook();
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=hook.js.map
|
package/dist/hook.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hook.js","sourceRoot":"","sources":["../src/hook.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EACL,aAAa,EACb,aAAa,EACb,WAAW,GAEZ,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,MAAM,CAAC,KAAK,UAAU,OAAO;IAC3B,+DAA+D;IAC/D,gFAAgF;IAChF,8EAA8E;IAC9E,MAAM,MAAM,GAAe,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAE9E,MAAM,SAAS,GAAG,GAAU,EAAE;QAC5B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;QAC1C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC;IAEF,mFAAmF;IACnF,MAAM,UAAU,GAAG,KAAK,IAAmB,EAAE;QAC3C,IAAI,OAAO,CAAC,KAAK,CAAC,KAAK;YAAE,OAAO;QAChC,IAAI,CAAC;YACH,IAAI,KAAK,EAAE,MAAM,CAAC,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;gBACpC,UAAU;YACZ,CAAC;QACH,CAAC;Q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