@openviking/openclaw-plugin 2026.10.21-2 → 2026.10.23
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/client.ts +10 -1
- package/commands/setup.ts +1 -0
- package/config.ts +23 -1
- package/dist/client.js +8 -1
- package/dist/commands/setup.js +1 -0
- package/dist/config.js +12 -0
- package/dist/plugin/openviking-client-runtime.js +1 -0
- package/dist/shared/redact.mjs +546 -0
- package/dist/shared/secret-rules.mjs +289 -0
- package/dist/shared/workspace-identity.mjs +339 -0
- package/install-manifest.json +1 -1
- package/openclaw.plugin.json +14 -0
- package/package.json +1 -1
- package/plugin/openviking-client-runtime.ts +3 -0
- package/shared/redact.d.mts +26 -0
- package/shared/redact.mjs +522 -0
- package/shared/secret-rules.mjs +294 -0
- package/shared/workspace-identity.mjs +334 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
// GENERATED FROM examples/memory-plugin-shared/lib. DO NOT EDIT.
|
|
2
|
+
/**
|
|
3
|
+
* Workspace identity: where the workspace root is, and what git calls it.
|
|
4
|
+
*
|
|
5
|
+
* Every hook is a fresh Node process and this runs on prompt-level paths, so
|
|
6
|
+
* the derivation is pure filesystem work — no `git` subprocess. That keeps it
|
|
7
|
+
* inside the tightest hook budget (Codex allows SessionEnd 3s against
|
|
8
|
+
* SessionStart's 70s), and it keeps working where git is absent from PATH or
|
|
9
|
+
* would refuse the repo over dubious ownership.
|
|
10
|
+
*
|
|
11
|
+
* The result is cached per cwd under the state dir so the several hooks of one
|
|
12
|
+
* turn pay for the walk once.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { createHash } from "node:crypto";
|
|
16
|
+
import { mkdirSync, readFileSync, realpathSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
17
|
+
import { homedir } from "node:os";
|
|
18
|
+
import { dirname, isAbsolute, join, parse, resolve, sep } from "node:path";
|
|
19
|
+
|
|
20
|
+
// The workspace config file names live here, and `workspace-config.mjs`
|
|
21
|
+
// re-exports them: the walk below needs the filenames to recognise a directory
|
|
22
|
+
// someone marked as a workspace, and it must not pull in the config layer to
|
|
23
|
+
// learn them.
|
|
24
|
+
export const CONFIG_DIR_NAME = ".openviking";
|
|
25
|
+
export const TEAM_FILE = "config.json";
|
|
26
|
+
export const LOCAL_FILE = "config.local.json";
|
|
27
|
+
|
|
28
|
+
const IDENTITY_CACHE_TTL_MS = 60_000;
|
|
29
|
+
// 255 is the AGFS path-segment limit; stopping well short leaves room for the
|
|
30
|
+
// hash suffix and for anything that later prefixes a peer id.
|
|
31
|
+
const MAX_PEER_ID_LENGTH = 100;
|
|
32
|
+
|
|
33
|
+
export function stateDir(env = process.env) {
|
|
34
|
+
const explicit = String(env.OPENVIKING_STATE_DIR || "").trim();
|
|
35
|
+
if (explicit) return explicit;
|
|
36
|
+
const home = String(env.OPENVIKING_HOME || "").trim();
|
|
37
|
+
const base = home ? home.replace(/^~(?=$|\/)/, homedir()) : join(homedir(), ".openviking");
|
|
38
|
+
return join(base, "state");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function shortHash(value) {
|
|
42
|
+
return createHash("sha256").update(String(value)).digest("hex").slice(0, 12);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Legacy peer sanitation: one byte in, one byte out, no collapsing and no
|
|
47
|
+
* trimming, so a leading `/` still becomes a leading `-`. Kept byte-exact
|
|
48
|
+
* because `peer.source: "cwd"` and the legacy id that dual-read recomputes both
|
|
49
|
+
* depend on it — do not "improve" it.
|
|
50
|
+
*/
|
|
51
|
+
export function legacySanitize(value) {
|
|
52
|
+
return String(value || "").replace(/[^A-Za-z0-9]/g, "-");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Readable sanitation for values that were never path-shaped — a normalized
|
|
57
|
+
* remote, a directory name. Mirrors the server's own `_sanitize_component`
|
|
58
|
+
* (`openviking/ingest/peer.py:32`) so both languages agree on the id, then
|
|
59
|
+
* enforces what `validate_identifier_part` additionally requires.
|
|
60
|
+
*/
|
|
61
|
+
export function sanitizePeerId(value) {
|
|
62
|
+
const raw = String(value || "").trim();
|
|
63
|
+
let cleaned = raw.replace(/[^a-zA-Z0-9_.@-]+/g, "-").replace(/-{2,}/g, "-").replace(/^[-.]+|[-.]+$/g, "");
|
|
64
|
+
if (!cleaned) return "";
|
|
65
|
+
// The server accepts at most one `@` in an identifier part.
|
|
66
|
+
const at = cleaned.indexOf("@");
|
|
67
|
+
if (at !== -1) cleaned = cleaned.slice(0, at + 1) + cleaned.slice(at + 1).replace(/@/g, "-");
|
|
68
|
+
// `ext-` is the server's namespace for base64-encoded external identities,
|
|
69
|
+
// and `__self` its operation-target sentinel. Neither is ours to occupy.
|
|
70
|
+
if (cleaned.startsWith("ext-")) cleaned = `x-${cleaned}`;
|
|
71
|
+
if (cleaned === "__self") cleaned = "self";
|
|
72
|
+
if (cleaned === "." || cleaned === "..") return "";
|
|
73
|
+
if (cleaned.length > MAX_PEER_ID_LENGTH) {
|
|
74
|
+
cleaned = `${cleaned.slice(0, MAX_PEER_ID_LENGTH - 13).replace(/[-.]+$/, "")}-${shortHash(raw)}`;
|
|
75
|
+
}
|
|
76
|
+
return cleaned;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Normalize a remote URL to `host/path`, lowercased.
|
|
81
|
+
*
|
|
82
|
+
* Both spellings of the same repo converge, and userinfo is dropped — so a
|
|
83
|
+
* remote with an embedded token cannot leak it into a peer id. The cost of
|
|
84
|
+
* case folding is that two repos differing only in case share one namespace on
|
|
85
|
+
* the rare case-sensitive forge.
|
|
86
|
+
*/
|
|
87
|
+
export function normalizeGitRemote(url) {
|
|
88
|
+
const raw = String(url || "").trim();
|
|
89
|
+
if (!raw) return "";
|
|
90
|
+
|
|
91
|
+
// `C:\src\repo` and `C:/src/repo` are one machine's directory, not a shared
|
|
92
|
+
// identity — and the scp pattern would happily read the drive as a host.
|
|
93
|
+
if (/^[A-Za-z]:[\\/]/.test(raw)) return "";
|
|
94
|
+
|
|
95
|
+
let host = "";
|
|
96
|
+
let path = "";
|
|
97
|
+
const scp = /^(?:[^@/\\]+@)?([^:/\\]+):(?!\/)(.+)$/.exec(raw);
|
|
98
|
+
if (scp && !/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(raw)) {
|
|
99
|
+
host = scp[1];
|
|
100
|
+
path = scp[2];
|
|
101
|
+
} else {
|
|
102
|
+
let parsed;
|
|
103
|
+
try {
|
|
104
|
+
parsed = new URL(raw);
|
|
105
|
+
} catch {
|
|
106
|
+
return "";
|
|
107
|
+
}
|
|
108
|
+
// A local clone has no stable shared identity — fall through to the path
|
|
109
|
+
// rules instead of minting one from `file://` or a bare directory.
|
|
110
|
+
if (parsed.protocol === "file:" || !parsed.hostname) return "";
|
|
111
|
+
host = parsed.hostname;
|
|
112
|
+
path = parsed.pathname;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
host = host.toLowerCase().replace(/^\[|\]$/g, "");
|
|
116
|
+
path = path.replace(/^\/+/, "").replace(/\/+$/, "").replace(/\.git$/i, "").toLowerCase();
|
|
117
|
+
if (!host || !path) return "";
|
|
118
|
+
return `${host}/${path}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function readFileOrEmpty(path) {
|
|
122
|
+
try {
|
|
123
|
+
return readFileSync(path, "utf-8");
|
|
124
|
+
} catch {
|
|
125
|
+
return "";
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Read `[remote "origin"] url` out of a git config with a minimal INI parse.
|
|
131
|
+
*
|
|
132
|
+
* `include` / `includeIf` are deliberately not followed: resolving them means
|
|
133
|
+
* more filesystem walking for a value the fallback chain already covers, so an
|
|
134
|
+
* unreadable remote just falls through to the next template.
|
|
135
|
+
*/
|
|
136
|
+
export function readGitRemoteUrl(commonDir, remote = "origin") {
|
|
137
|
+
const text = readFileOrEmpty(join(commonDir, "config"));
|
|
138
|
+
if (!text) return "";
|
|
139
|
+
|
|
140
|
+
let inSection = false;
|
|
141
|
+
for (const line of text.split("\n")) {
|
|
142
|
+
const trimmed = line.trim();
|
|
143
|
+
if (!trimmed || trimmed.startsWith("#") || trimmed.startsWith(";")) continue;
|
|
144
|
+
const section = /^\[([^\]]+)\]/.exec(trimmed);
|
|
145
|
+
if (section) {
|
|
146
|
+
const header = section[1].trim();
|
|
147
|
+
// git folds section and key names to lower case but keeps a quoted
|
|
148
|
+
// subsection exact, so `[Remote "origin"]` is the same section and
|
|
149
|
+
// `[remote "Origin"]` is not.
|
|
150
|
+
const quoted = /^remote\s+"(.*)"$/i.exec(header);
|
|
151
|
+
inSection = Boolean(quoted && quoted[1] === remote);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (!inSection) continue;
|
|
155
|
+
const pair = /^url\s*=\s*(.*)$/i.exec(trimmed);
|
|
156
|
+
// git treats an unquoted `#` or `;` as starting a comment anywhere on the
|
|
157
|
+
// line, so a trailing note is not part of the URL.
|
|
158
|
+
if (pair) return pair[1].split(/[#;]/)[0].trim().replace(/^["']|["']$/g, "");
|
|
159
|
+
}
|
|
160
|
+
return "";
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function resolveGitDir(root) {
|
|
164
|
+
const dotGit = join(root, ".git");
|
|
165
|
+
let stat;
|
|
166
|
+
try {
|
|
167
|
+
stat = statSync(dotGit);
|
|
168
|
+
} catch {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
if (stat.isDirectory()) return { gitDir: dotGit, commonDir: dotGit, kind: "repo" };
|
|
172
|
+
if (!stat.isFile()) return null;
|
|
173
|
+
|
|
174
|
+
const pointer = /^gitdir:\s*(.+)$/m.exec(readFileOrEmpty(dotGit));
|
|
175
|
+
if (!pointer) return null;
|
|
176
|
+
const gitDir = resolve(root, pointer[1].trim());
|
|
177
|
+
|
|
178
|
+
// `commondir` is the worktree signal, so it is read first — a repository
|
|
179
|
+
// that merely lives under a directory called `modules` is not a submodule.
|
|
180
|
+
const commonRef = readFileOrEmpty(join(gitDir, "commondir")).trim();
|
|
181
|
+
const commonDir = commonRef
|
|
182
|
+
? (isAbsolute(commonRef) ? commonRef : resolve(gitDir, commonRef))
|
|
183
|
+
: gitDir;
|
|
184
|
+
|
|
185
|
+
// A submodule keeps its own remote under the superproject's
|
|
186
|
+
// `.git/modules/<name>`; converging it onto the superproject would merge two
|
|
187
|
+
// repositories that release, and are reviewed, separately. Only the segment
|
|
188
|
+
// right after a `.git` directory means that, which is also why a worktree of
|
|
189
|
+
// a submodule resolves here through its own commondir.
|
|
190
|
+
const kind = /[/\\]\.git[/\\]modules[/\\]/.test(`${commonDir}${sep}`)
|
|
191
|
+
? "submodule"
|
|
192
|
+
: (commonRef ? "worktree" : "repo");
|
|
193
|
+
return { gitDir, commonDir, kind };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** A directory the user made a workspace on purpose, by giving it a config file. */
|
|
197
|
+
function hasWorkspaceMarker(dir) {
|
|
198
|
+
for (const name of [TEAM_FILE, LOCAL_FILE]) {
|
|
199
|
+
try {
|
|
200
|
+
if (statSync(join(dir, CONFIG_DIR_NAME, name)).isFile()) return true;
|
|
201
|
+
} catch { /* not marked here */ }
|
|
202
|
+
}
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const NO_ROOT = Object.freeze({ root: "", rootKind: "", git: null, gitRoot: "" });
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Walk up from `cwd` to the nearest workspace root.
|
|
210
|
+
*
|
|
211
|
+
* A workspace is a git repository, or a directory someone marked as one by
|
|
212
|
+
* creating `.openviking/config.json` (or `config.local.json`) in it. Nearest
|
|
213
|
+
* wins. Above a marked directory the walk continues to the enclosing
|
|
214
|
+
* repository, if any, so the git-shaped variables still resolve there.
|
|
215
|
+
* Anything else — a scratch folder, a download, an app's per-task directory —
|
|
216
|
+
* is not a workspace and gets no peer of its own.
|
|
217
|
+
*
|
|
218
|
+
* `$HOME` and the filesystem root are never workspace roots: a stray `.git`
|
|
219
|
+
* in either would silently make every unrelated directory one workspace.
|
|
220
|
+
*/
|
|
221
|
+
export function findWorkspaceRoot(cwd, env = process.env) {
|
|
222
|
+
const start = String(cwd || "").trim();
|
|
223
|
+
if (!start) return NO_ROOT;
|
|
224
|
+
|
|
225
|
+
let absolute;
|
|
226
|
+
try {
|
|
227
|
+
// A relative `start` makes `resolve` read process.cwd(), which throws when
|
|
228
|
+
// the directory is gone — the very case the catch below is meant to cover.
|
|
229
|
+
absolute = resolve(start);
|
|
230
|
+
} catch {
|
|
231
|
+
return NO_ROOT;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// `current` and `stopAt` are compared as strings, so they must be
|
|
235
|
+
// canonicalized together. A `$HOME` reached through a symlink plus an
|
|
236
|
+
// unresolvable cwd would otherwise walk straight past the guard.
|
|
237
|
+
const home = String(env.HOME || "").trim() || homedir();
|
|
238
|
+
let current;
|
|
239
|
+
let stopAt;
|
|
240
|
+
try {
|
|
241
|
+
current = realpathSync(absolute);
|
|
242
|
+
stopAt = realpathSync(home);
|
|
243
|
+
} catch {
|
|
244
|
+
current = absolute;
|
|
245
|
+
stopAt = home;
|
|
246
|
+
}
|
|
247
|
+
const filesystemRoot = parse(current).root;
|
|
248
|
+
if (current === stopAt || current === filesystemRoot) return NO_ROOT;
|
|
249
|
+
|
|
250
|
+
let root = "";
|
|
251
|
+
let rootKind = "";
|
|
252
|
+
while (current && current !== filesystemRoot && current !== stopAt) {
|
|
253
|
+
const git = resolveGitDir(current);
|
|
254
|
+
if (git) return { root: root || current, rootKind: rootKind || "git", git, gitRoot: current };
|
|
255
|
+
if (!root && hasWorkspaceMarker(current)) {
|
|
256
|
+
root = current;
|
|
257
|
+
rootKind = "config";
|
|
258
|
+
}
|
|
259
|
+
const parent = dirname(current);
|
|
260
|
+
if (parent === current) break;
|
|
261
|
+
current = parent;
|
|
262
|
+
}
|
|
263
|
+
return root ? { root, rootKind, git: null, gitRoot: "" } : NO_ROOT;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function cachePath(cwd, env) {
|
|
267
|
+
return join(stateDir(env), `ws-identity-${shortHash(cwd)}.json`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function readCache(path, now) {
|
|
271
|
+
try {
|
|
272
|
+
const cached = JSON.parse(readFileSync(path, "utf-8"));
|
|
273
|
+
// Bounded on both sides: an entry stamped in the future — a clock that ran
|
|
274
|
+
// fast, or `Infinity` — would otherwise pin a stale identity indefinitely.
|
|
275
|
+
if (typeof cached?.ts !== "number" || cached.ts > now || now - cached.ts > IDENTITY_CACHE_TTL_MS) return null;
|
|
276
|
+
const identity = cached.identity;
|
|
277
|
+
if (identity && typeof identity === "object" && identity.vars && typeof identity.vars === "object") {
|
|
278
|
+
return identity;
|
|
279
|
+
}
|
|
280
|
+
} catch { /* a cold or corrupt cache just costs one walk */ }
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function writeCache(path, identity, now) {
|
|
285
|
+
try {
|
|
286
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
287
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
288
|
+
writeFileSync(tmp, JSON.stringify({ ts: now, identity }), { mode: 0o600 });
|
|
289
|
+
renameSync(tmp, path);
|
|
290
|
+
} catch { /* best effort */ }
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Everything the peer templates can substitute, for one cwd.
|
|
295
|
+
*
|
|
296
|
+
* `git_remote` and `dir` are already sanitized; `git_root` and `cwd` carry the
|
|
297
|
+
* legacy byte-for-byte rule, because they are the two that must reproduce a
|
|
298
|
+
* peer minted before any of this existed.
|
|
299
|
+
*/
|
|
300
|
+
export function resolveWorkspaceIdentity({ cwd = "", env = process.env, cache = true, now = Date.now() } = {}) {
|
|
301
|
+
const key = String(cwd || "");
|
|
302
|
+
const path = cachePath(key, env);
|
|
303
|
+
if (cache) {
|
|
304
|
+
const hit = readCache(path, now);
|
|
305
|
+
if (hit) return hit;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const { root, rootKind, git, gitRoot } = findWorkspaceRoot(key, env);
|
|
309
|
+
// Only the normalized form is kept. The raw URL may carry a token, and this
|
|
310
|
+
// file outlives the process — writing it here would undo the care
|
|
311
|
+
// `normalizeGitRemote` takes to drop userinfo.
|
|
312
|
+
const remote = git ? normalizeGitRemote(readGitRemoteUrl(git.commonDir)) : "";
|
|
313
|
+
const identity = {
|
|
314
|
+
cwd: key,
|
|
315
|
+
root,
|
|
316
|
+
rootKind,
|
|
317
|
+
isGit: Boolean(git),
|
|
318
|
+
gitKind: git?.kind || "",
|
|
319
|
+
gitCommonDir: git?.commonDir || "",
|
|
320
|
+
gitRoot,
|
|
321
|
+
remote,
|
|
322
|
+
vars: {
|
|
323
|
+
git_remote: sanitizePeerId(remote),
|
|
324
|
+
// The enclosing repository's root, which is not the workspace root when a
|
|
325
|
+
// marker file below it won. Empty outside a repository, so the `git`
|
|
326
|
+
// preset resolves to nothing there rather than to a bare path.
|
|
327
|
+
git_root: git ? legacySanitize(gitRoot) : "",
|
|
328
|
+
cwd: legacySanitize(key),
|
|
329
|
+
dir: root ? sanitizePeerId(root.split(/[/\\]/).filter(Boolean).pop() || "") : "",
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
if (cache) writeCache(path, identity, now);
|
|
333
|
+
return identity;
|
|
334
|
+
}
|