@christang/keel 5.84.0 → 5.85.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 +22 -0
- package/README.zh-CN.md +21 -0
- package/assets/bootstrap/AGENTS.md +1 -1
- package/bin/keel.js +5 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/plugins/keel/.claude-plugin/plugin.json +1 -1
- package/plugins/keel/.codex-plugin/plugin.json +2 -1
- package/plugins/keel/hooks/codex.json +44 -0
- package/plugins/keel/scripts/codex-mail-hook.js +75 -0
- package/plugins/keel/scripts/mail-hook.js +11 -9
- package/scripts/fake_slack.py +277 -0
- package/scripts/validate_codex_receiving.py +296 -0
- package/scripts/validate_plugin.py +1476 -178
- package/src/core/chat/archive.js +234 -0
- package/src/core/chat/bridge.js +808 -0
- package/src/core/chat/cli.js +453 -0
- package/src/core/chat/config.js +64 -0
- package/src/core/chat/guards.js +96 -0
- package/src/core/chat/lifecycle.js +214 -0
- package/src/core/chat/migrate.js +142 -0
- package/src/core/chat/notice.js +179 -0
- package/src/core/chat/redact.js +37 -0
- package/src/core/chat/slack.js +104 -0
- package/src/core/chat/store.js +776 -0
- package/src/core/chat/view.js +165 -0
- package/src/core/mail.js +123 -433
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The chat's long-term home (issue #187, design D19). Slack's free plan shows
|
|
4
|
+
// 90 days and deletes after a year; the project's own repository keeps
|
|
5
|
+
// everything, on an orphan branch `keel-chat` that never meets `main`.
|
|
6
|
+
//
|
|
7
|
+
// Only the synced layer is archived — records, cursors, aliases, presence —
|
|
8
|
+
// the files that are written by one writer and never rewritten in place, so
|
|
9
|
+
// two machines' archives merge by the union of their paths. The commit is
|
|
10
|
+
// made with plumbing and a temporary index: the worktree, the real index, and
|
|
11
|
+
// every other branch stay exactly as they were.
|
|
12
|
+
//
|
|
13
|
+
// A public repository is not pushed unless the project says so, because a
|
|
14
|
+
// group's conversation is not something to publish by default.
|
|
15
|
+
|
|
16
|
+
const fs = require("fs");
|
|
17
|
+
const os = require("os");
|
|
18
|
+
const path = require("path");
|
|
19
|
+
const crypto = require("crypto");
|
|
20
|
+
const { spawnSync } = require("child_process");
|
|
21
|
+
const store = require("./store");
|
|
22
|
+
const { loadChatConfig } = require("./config");
|
|
23
|
+
|
|
24
|
+
const { ChatError } = store;
|
|
25
|
+
const BRANCH = "refs/heads/keel-chat";
|
|
26
|
+
const SYNCED_ROOTS = ["groups", "cursors", "members", "presence"];
|
|
27
|
+
|
|
28
|
+
function git(where, args, { env = {}, input = null, allowFail = false } = {}) {
|
|
29
|
+
const result = spawnSync("git", args, {
|
|
30
|
+
cwd: where.worktree,
|
|
31
|
+
encoding: input !== null && Buffer.isBuffer(input) ? undefined : "utf8",
|
|
32
|
+
env: { ...process.env, ...env },
|
|
33
|
+
input: input === null ? undefined : input,
|
|
34
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
35
|
+
});
|
|
36
|
+
if (result.status !== 0 && !allowFail) {
|
|
37
|
+
throw new ChatError(`git ${args[0]} failed: ${String(result.stderr || "").trim()}`);
|
|
38
|
+
}
|
|
39
|
+
return { ok: result.status === 0, stdout: String(result.stdout || "").trim(), stderr: String(result.stderr || "").trim() };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// `archive`: absent or "auto" archives when Slack is on; "off" never does on
|
|
43
|
+
// its own; `{ "remote": <name> }` names where to push. `archive_public:
|
|
44
|
+
// "accept"` allows pushing a public repository.
|
|
45
|
+
function archiveSettings(worktree) {
|
|
46
|
+
const { config } = loadChatConfig(worktree);
|
|
47
|
+
const archive = config.archive === undefined ? "auto" : config.archive;
|
|
48
|
+
const slackOn = Boolean(config.slack && config.slack.enabled === true);
|
|
49
|
+
const declaredRemote = archive && typeof archive === "object" && typeof archive.remote === "string" ? archive.remote : null;
|
|
50
|
+
return {
|
|
51
|
+
automatic: archive === "auto" ? slackOn : archive !== "off" && archive !== false,
|
|
52
|
+
declaredRemote,
|
|
53
|
+
publicAccepted: config.archive_public === "accept",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function listSynced(where) {
|
|
58
|
+
const files = [];
|
|
59
|
+
const walk = (relative) => {
|
|
60
|
+
const absolute = path.join(where.root, relative);
|
|
61
|
+
let entries = [];
|
|
62
|
+
try {
|
|
63
|
+
entries = fs.readdirSync(absolute, { withFileTypes: true });
|
|
64
|
+
} catch {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
for (const entry of entries) {
|
|
68
|
+
const child = path.posix.join(relative, entry.name);
|
|
69
|
+
if (entry.isDirectory()) walk(child);
|
|
70
|
+
else if (entry.isFile()) files.push(child);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
for (const root of SYNCED_ROOTS) walk(root);
|
|
74
|
+
return files.sort();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function remoteName(where, settings) {
|
|
78
|
+
if (settings.declaredRemote) return settings.declaredRemote;
|
|
79
|
+
const remotes = git(where, ["remote"], { allowFail: true }).stdout.split(/\s+/).filter(Boolean);
|
|
80
|
+
return remotes.includes("origin") ? "origin" : remotes[0] || null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function fetchRemote(where, remote) {
|
|
84
|
+
if (!remote) return null;
|
|
85
|
+
// Into FETCH_HEAD only: no ref, branch, or worktree file changes.
|
|
86
|
+
const fetched = git(where, ["fetch", "--quiet", remote, BRANCH], { allowFail: true });
|
|
87
|
+
if (!fetched.ok) return null;
|
|
88
|
+
const tip = git(where, ["rev-parse", "--verify", "--quiet", "FETCH_HEAD^{commit}"], { allowFail: true });
|
|
89
|
+
return tip.ok ? tip.stdout : null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function treeEntries(where, commit) {
|
|
93
|
+
if (!commit) return new Map();
|
|
94
|
+
const listed = git(where, ["ls-tree", "-r", "-z", commit], { allowFail: true });
|
|
95
|
+
const entries = new Map();
|
|
96
|
+
for (const line of listed.stdout.split("\0").filter(Boolean)) {
|
|
97
|
+
const match = line.match(/^\d+ blob ([0-9a-f]+)\t(.+)$/);
|
|
98
|
+
if (match) entries.set(match[2], match[1]);
|
|
99
|
+
}
|
|
100
|
+
return entries;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Writes into the local store every archived file it lacks. Existing files
|
|
104
|
+
// are never overwritten: a record is immutable, and a cursor, alias, or
|
|
105
|
+
// presence file belongs to the one role that writes it.
|
|
106
|
+
function importMissing(where, entries) {
|
|
107
|
+
let imported = 0;
|
|
108
|
+
for (const [file, blob] of entries) {
|
|
109
|
+
if (!SYNCED_ROOTS.includes(file.split("/")[0])) continue;
|
|
110
|
+
const target = path.join(where.root, ...file.split("/"));
|
|
111
|
+
if (fs.existsSync(target)) continue;
|
|
112
|
+
const content = spawnSync("git", ["cat-file", "blob", blob], { cwd: where.worktree, maxBuffer: 64 * 1024 * 1024 });
|
|
113
|
+
if (content.status !== 0) continue;
|
|
114
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
115
|
+
const temp = path.join(where.root, ".tmp", `${process.pid}-${crypto.randomBytes(6).toString("hex")}`);
|
|
116
|
+
fs.mkdirSync(path.dirname(temp), { recursive: true });
|
|
117
|
+
fs.writeFileSync(temp, content.stdout);
|
|
118
|
+
fs.renameSync(temp, target);
|
|
119
|
+
imported += 1;
|
|
120
|
+
}
|
|
121
|
+
return imported;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function visibility(where) {
|
|
125
|
+
const gh = process.env.KEEL_CHAT_GH || "gh";
|
|
126
|
+
const result = spawnSync(gh, ["repo", "view", "--json", "visibility", "-q", ".visibility"], {
|
|
127
|
+
cwd: where.worktree, encoding: "utf8", timeout: 20000,
|
|
128
|
+
});
|
|
129
|
+
if (result.status !== 0) return null;
|
|
130
|
+
return String(result.stdout || "").trim().toUpperCase() || null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const IDENTITY = {
|
|
134
|
+
GIT_AUTHOR_NAME: "keel chat archive",
|
|
135
|
+
GIT_AUTHOR_EMAIL: "keel-chat@localhost",
|
|
136
|
+
GIT_COMMITTER_NAME: "keel chat archive",
|
|
137
|
+
GIT_COMMITTER_EMAIL: "keel-chat@localhost",
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
function commitArchive(where, localTip, remoteTip) {
|
|
141
|
+
const index = path.join(os.tmpdir(), `keel-chat-index-${process.pid}-${crypto.randomBytes(4).toString("hex")}`);
|
|
142
|
+
const env = { GIT_INDEX_FILE: index };
|
|
143
|
+
const conflicts = [];
|
|
144
|
+
try {
|
|
145
|
+
if (localTip) git(where, ["read-tree", localTip], { env });
|
|
146
|
+
else git(where, ["read-tree", "--empty"], { env });
|
|
147
|
+
const present = treeEntries(where, localTip);
|
|
148
|
+
for (const [file, blob] of treeEntries(where, remoteTip)) {
|
|
149
|
+
if (!present.has(file)) {
|
|
150
|
+
git(where, ["update-index", "--add", "--cacheinfo", `100644,${blob},${file}`], { env });
|
|
151
|
+
present.set(file, blob);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
for (const file of listSynced(where)) {
|
|
155
|
+
const blob = git(where, ["hash-object", "-w", path.join(where.root, ...file.split("/"))]).stdout;
|
|
156
|
+
const archived = present.get(file);
|
|
157
|
+
if (archived === blob) continue;
|
|
158
|
+
// A record is immutable, so one id with two contents is worth saying;
|
|
159
|
+
// cursors and presence legitimately move on.
|
|
160
|
+
if (archived && file.startsWith("groups/")) conflicts.push(file);
|
|
161
|
+
git(where, ["update-index", "--add", "--cacheinfo", `100644,${blob},${file}`], { env });
|
|
162
|
+
}
|
|
163
|
+
const tree = git(where, ["write-tree"], { env }).stdout;
|
|
164
|
+
const parents = [localTip, remoteTip].filter((tip, i, all) => tip && all.indexOf(tip) === i);
|
|
165
|
+
const localTree = localTip ? git(where, ["rev-parse", `${localTip}^{tree}`]).stdout : null;
|
|
166
|
+
if (localTip && tree === localTree && (!remoteTip || parents.length === 1 || isAncestor(where, remoteTip, localTip))) {
|
|
167
|
+
return { commit: localTip, changed: false, conflicts };
|
|
168
|
+
}
|
|
169
|
+
const count = listSynced(where).filter((file) => file.startsWith("groups/")).length;
|
|
170
|
+
const args = ["commit-tree", tree, "-m", `keel chat archive: ${count} records`];
|
|
171
|
+
for (const parent of parents) args.push("-p", parent);
|
|
172
|
+
const commit = git(where, args, { env: IDENTITY }).stdout;
|
|
173
|
+
git(where, ["update-ref", BRANCH, commit, ...(localTip ? [localTip] : [])]);
|
|
174
|
+
return { commit, changed: true, conflicts };
|
|
175
|
+
} finally {
|
|
176
|
+
fs.rmSync(index, { force: true });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function isAncestor(where, ancestor, descendant) {
|
|
181
|
+
return git(where, ["merge-base", "--is-ancestor", ancestor, descendant], { allowFail: true }).ok;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function pushDecision(where, settings, remote) {
|
|
185
|
+
if (!remote) return { push: false, reason: "this repository has no remote" };
|
|
186
|
+
if (settings.declaredRemote) return { push: true };
|
|
187
|
+
if (settings.publicAccepted) return { push: true };
|
|
188
|
+
const seen = visibility(where);
|
|
189
|
+
if (seen === "PRIVATE" || seen === "INTERNAL") return { push: true };
|
|
190
|
+
const why = seen === "PUBLIC" ? "this repository is public" : "this repository's visibility could not be confirmed";
|
|
191
|
+
return {
|
|
192
|
+
push: false,
|
|
193
|
+
reason: `${why}, so the chat archive was committed locally and not pushed. To push it, name a private remote `
|
|
194
|
+
+ 'with `"archive": { "remote": "<name>" }` (archive.remote) in keel/chat.json, or publish it deliberately '
|
|
195
|
+
+ 'with `"archive_public": "accept"` (archive_public).',
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function sync(where) {
|
|
200
|
+
const settings = archiveSettings(where.worktree);
|
|
201
|
+
const remote = remoteName(where, settings);
|
|
202
|
+
const remoteTip = fetchRemote(where, remote);
|
|
203
|
+
const imported = importMissing(where, treeEntries(where, remoteTip));
|
|
204
|
+
const localTip = git(where, ["rev-parse", "--verify", "--quiet", BRANCH], { allowFail: true }).stdout || null;
|
|
205
|
+
const { commit, changed, conflicts } = commitArchive(where, localTip, remoteTip);
|
|
206
|
+
const decision = pushDecision(where, settings, remote);
|
|
207
|
+
let pushed = false;
|
|
208
|
+
if (decision.push && commit && commit !== remoteTip) {
|
|
209
|
+
const result = git(where, ["push", "--quiet", remote, `${BRANCH}:${BRANCH}`], { allowFail: true });
|
|
210
|
+
if (!result.ok) throw new ChatError(`git push of the chat archive to ${remote} failed: ${result.stderr}`);
|
|
211
|
+
pushed = true;
|
|
212
|
+
}
|
|
213
|
+
return { commit, changed, imported, pushed, remote, refused: decision.push ? null : decision.reason, conflicts };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function pull(where) {
|
|
217
|
+
const settings = archiveSettings(where.worktree);
|
|
218
|
+
const remote = remoteName(where, settings);
|
|
219
|
+
const remoteTip = fetchRemote(where, remote);
|
|
220
|
+
const localTip = git(where, ["rev-parse", "--verify", "--quiet", BRANCH], { allowFail: true }).stdout || null;
|
|
221
|
+
let imported = importMissing(where, treeEntries(where, remoteTip));
|
|
222
|
+
imported += importMissing(where, treeEntries(where, localTip));
|
|
223
|
+
if (!remoteTip && !localTip) {
|
|
224
|
+
throw new ChatError(`No chat archive found: neither ${remote ? `${remote}'s` : "a remote"} keel-chat branch nor a local one exists.`);
|
|
225
|
+
}
|
|
226
|
+
return { imported, remote };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
module.exports = {
|
|
230
|
+
BRANCH,
|
|
231
|
+
archiveSettings,
|
|
232
|
+
pull,
|
|
233
|
+
sync,
|
|
234
|
+
};
|