clavarium 0.2.43-beta.1 → 0.2.44-beta.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/npm-shrinkwrap.json +6 -6
- package/package.json +2 -2
- package/server/api.mjs +9 -1
- package/server/guide.mjs +2 -0
- package/server/main.mjs +21 -0
- package/server/secret-scan.mjs +93 -0
- package/server/secrets.mjs +66 -3
- package/server/talk.mjs +26 -5
- package/web/app.js +16 -3
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.44-beta.1",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "clavarium",
|
|
9
|
-
"version": "0.2.
|
|
9
|
+
"version": "0.2.44-beta.1",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
|
13
13
|
"@xterm/xterm": "^6.0.0",
|
|
14
14
|
"clavarium-binder": "0.1.8-next.1",
|
|
15
15
|
"clavarium-docket": "0.1.1",
|
|
16
|
-
"clavarium-parley": "0.17.
|
|
16
|
+
"clavarium-parley": "0.17.8-next.1",
|
|
17
17
|
"mermaid": "11.17.2",
|
|
18
18
|
"node-pty": "^1.1.0",
|
|
19
19
|
"pdfjs-dist": "6.4.299",
|
|
@@ -714,9 +714,9 @@
|
|
|
714
714
|
}
|
|
715
715
|
},
|
|
716
716
|
"node_modules/clavarium-parley": {
|
|
717
|
-
"version": "0.17.
|
|
718
|
-
"resolved": "https://registry.npmjs.org/clavarium-parley/-/clavarium-parley-0.17.
|
|
719
|
-
"integrity": "sha512
|
|
717
|
+
"version": "0.17.8-next.1",
|
|
718
|
+
"resolved": "https://registry.npmjs.org/clavarium-parley/-/clavarium-parley-0.17.8-next.1.tgz",
|
|
719
|
+
"integrity": "sha512-AJHz4V91heYRuCYh3KXRi8kw5ArNTLNsKpA2Svw6JoGKh7wCRr+Wp3ayiJDpukdjLHkijEuk7vhFE3c7L/LQ3g==",
|
|
720
720
|
"license": "MIT",
|
|
721
721
|
"bin": {
|
|
722
722
|
"parley": "bin/parley.mjs",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.44-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "A conversation is a VM with a terminal in it. Everything else is a folder.",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@xterm/xterm": "^6.0.0",
|
|
47
47
|
"clavarium-binder": "0.1.8-next.1",
|
|
48
48
|
"clavarium-docket": "0.1.1",
|
|
49
|
-
"clavarium-parley": "0.17.
|
|
49
|
+
"clavarium-parley": "0.17.8-next.1",
|
|
50
50
|
"mermaid": "11.17.2",
|
|
51
51
|
"node-pty": "^1.1.0",
|
|
52
52
|
"pdfjs-dist": "6.4.299",
|
package/server/api.mjs
CHANGED
|
@@ -235,7 +235,7 @@ export async function handleApi(request, response, conversations, headers, resou
|
|
|
235
235
|
if (pathname === '/api/tools') return send(200, conversations.tools()), true;
|
|
236
236
|
if (pathname === '/api/conversations') {
|
|
237
237
|
const lists = binders?.given() ?? {};
|
|
238
|
-
return send(200, conversations.list().map((c) => ({ ...c, lists: lists[c.name] ?? null, resources: resources?.of(c.name) ?? [], secrets: secrets?.of(c.name) ?? [], answerer: answerer?.of(c.name) ?? null, wakeups: wakeups?.of(c.name) ?? null, health: health?.of(c.name) ?? null, talk: talk?.brief(c.name) ?? null }))), true;
|
|
238
|
+
return send(200, conversations.list().map((c) => ({ ...c, lists: lists[c.name] ?? null, resources: resources?.of(c.name) ?? [], secrets: secrets?.of(c.name) ?? [], ephemeralSecrets: secrets?.liveFor(c.name) ?? [], answerer: answerer?.of(c.name) ?? null, wakeups: wakeups?.of(c.name) ?? null, health: health?.of(c.name) ?? null, talk: talk?.brief(c.name) ?? null }))), true;
|
|
239
239
|
}
|
|
240
240
|
if (pathname === '/api/resources' && resources) return send(200, resources.list()), true;
|
|
241
241
|
if (pathname === '/api/shortcuts' && shortcuts) return send(200, shortcuts.list()), true;
|
|
@@ -505,6 +505,14 @@ export async function handleApi(request, response, conversations, headers, resou
|
|
|
505
505
|
retell(conversation);
|
|
506
506
|
return send(200, { ok: true, secrets: secrets.of(conversation) }), true;
|
|
507
507
|
}
|
|
508
|
+
/* Forget an ephemeral secret now (its stars in the chat have a button): gone from the vault and the machine. */
|
|
509
|
+
const forgetSecret = /^\/api\/conversations\/([^/]+)\/secrets\/([^/]+)\/forget$/.exec(pathname);
|
|
510
|
+
if (forgetSecret && secrets) {
|
|
511
|
+
const conversation = decodeURIComponent(forgetSecret[1]);
|
|
512
|
+
if (!conversations.list().some((c) => c.name === conversation)) throw new Refusal(404, 'No such conversation.');
|
|
513
|
+
await secrets.drop(conversation, decodeURIComponent(forgetSecret[2]));
|
|
514
|
+
return send(200, { ok: true }), true;
|
|
515
|
+
}
|
|
508
516
|
const share = /^\/api\/conversations\/([^/]+)\/share$/.exec(pathname);
|
|
509
517
|
if (share && sharing) {
|
|
510
518
|
const conversation = decodeURIComponent(share[1]);
|
package/server/guide.mjs
CHANGED
|
@@ -129,6 +129,8 @@ export function ownText({ name, tool, workKept, resources = [], chat, online, se
|
|
|
129
129
|
if (chat) out.push('- When you work in batches: keep them in a binder of batches, shown beside the chat. None yet: make it once (binder new batches; MCP: new-list, template batches). Then one card each (binder add Batches "title"), kept up to date; start only those the person ticked.');
|
|
130
130
|
out.push(online === false ? '- Internet: off.' : '- Internet: on.');
|
|
131
131
|
if (secrets.length) out.push(`- Secrets given, by name only: ${list(secrets.map((s) => plain(s, 40)))}.`);
|
|
132
|
+
/* A secret pasted in the chat is lifted out (talk.mjs, secret-scan.mjs): the message shows a marker, the value waits in /run/secrets. */
|
|
133
|
+
if (chat) out.push('- A secret the person pastes in the chat is kept OUT of it: the message shows ⟨secret:NAME⟩ and the value waits at /run/secrets/NAME for a few minutes. Use it only from there, inside a command (clv-run, or $(cat /run/secrets/NAME)); never write its value back into the chat. It clears itself when its time runs out, or at once with `parley forget NAME` when you are done.');
|
|
132
134
|
const own = lines.map((l) => plain(l)).filter(Boolean).slice(0, LINES_MAX);
|
|
133
135
|
if (own.length) out.push('', '## From the owner', '', ...own.map((l) => `- ${l}`));
|
|
134
136
|
out.push('');
|
package/server/main.mjs
CHANGED
|
@@ -35,6 +35,7 @@ import { Devices } from './devices.mjs';
|
|
|
35
35
|
import { handleDoor, deviceTerminal } from './door.mjs';
|
|
36
36
|
import { Vault } from './vault.mjs';
|
|
37
37
|
import { Secrets, RUNNER } from './secrets.mjs';
|
|
38
|
+
import { scan, secretRef } from './secret-scan.mjs';
|
|
38
39
|
import { Network } from './network.mjs';
|
|
39
40
|
import { Answerer, answererConfig } from './answerer.mjs';
|
|
40
41
|
import { Dockets } from './dockets.mjs';
|
|
@@ -420,6 +421,26 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
|
|
|
420
421
|
what is already known of a machine used instead of asking (talk.mjs). */
|
|
421
422
|
terminal: { open: (name) => bridge?.open(name) ?? false, type: (name, line, ms) => bridge?.type(name, line, ms) ?? false, droppedAt: (name) => bridge?.droppedAt(name) },
|
|
422
423
|
health: { of: (name) => health?.of(name) ?? null },
|
|
424
|
+
/* A secret pasted in a message is lifted out before the message is kept:
|
|
425
|
+
it goes to the short-lived vault and the message keeps only a marker
|
|
426
|
+
the chat shows as stars; the value reaches the machine as
|
|
427
|
+
/run/secrets/<name>, and is swept after a few minutes. */
|
|
428
|
+
screen: async (conversation, text) => {
|
|
429
|
+
const hits = scan(text);
|
|
430
|
+
if (!hits.length) return text;
|
|
431
|
+
let out = ''; let last = 0; let any = false;
|
|
432
|
+
for (const h of hits) {
|
|
433
|
+
out += text.slice(last, h.start);
|
|
434
|
+
try { const { name, expiresAt } = secrets.stash(conversation, h.value); out += secretRef(name, Date.parse(expiresAt)); any = true; }
|
|
435
|
+
catch { out += text.slice(h.start, h.end); } /* cannot be stashed (too large): left as it is */
|
|
436
|
+
last = h.end;
|
|
437
|
+
}
|
|
438
|
+
out += text.slice(last);
|
|
439
|
+
if (any) { try { await secrets.deliver(conversation); } catch { /* machine not running: delivered at its next open */ } }
|
|
440
|
+
return out;
|
|
441
|
+
},
|
|
442
|
+
/* The agent asked to forget a short-lived secret (parley forget): drop it from the vault and machine. */
|
|
443
|
+
dropSecret: (conversation, name) => { void secrets.drop(conversation, name).catch(() => {}); },
|
|
423
444
|
alert: (payload) => { void alerts.tell(payload); } });
|
|
424
445
|
/* Its machine restarted or built again: said in its chat, and the tool told to read it again. */
|
|
425
446
|
all.renewed = (name, text, running) => talk.renewed(name, text, running);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* SECRET SCANNER -- finds likely secrets in a message, without any model.
|
|
2
|
+
*
|
|
3
|
+
* Used when a person's message arrives (talk.mjs): what looks like a token,
|
|
4
|
+
* key or password is lifted out, kept in the vault for a few minutes, and the
|
|
5
|
+
* message keeps only a reference. The aim is FEW FALSE POSITIVES: we match
|
|
6
|
+
* shapes that are almost never anything but a secret (known token prefixes, a
|
|
7
|
+
* PEM private key), plus long high-entropy blobs. Ordinary prose, file paths,
|
|
8
|
+
* URLs, git hashes and UUIDs are left alone. A person can still mark a
|
|
9
|
+
* selection by hand (talk.mjs) for what the shapes miss, or undo a wrong one.
|
|
10
|
+
*
|
|
11
|
+
* `scan(text)` returns the non-overlapping matches, each { start, end, value,
|
|
12
|
+
* kind }, left to right. Nothing is stored here: the caller decides.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/* Shapes that are almost always a secret. Order matters only for the `kind`
|
|
16
|
+
label; overlaps are resolved by position below. */
|
|
17
|
+
const SHAPES = [
|
|
18
|
+
{ kind: 'private-key', re: /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY-----[\s\S]*?-----END (?:[A-Z0-9]+ )*PRIVATE KEY-----/g },
|
|
19
|
+
{ kind: 'github', re: /\bgithub_pat_[0-9A-Za-z_]{40,}\b/g },
|
|
20
|
+
{ kind: 'github', re: /\bgh[pousr]_[0-9A-Za-z]{36,}\b/g },
|
|
21
|
+
{ kind: 'npm', re: /\bnpm_[0-9A-Za-z]{36}\b/g },
|
|
22
|
+
{ kind: 'aws', re: /\bAKIA[0-9A-Z]{16}\b/g },
|
|
23
|
+
{ kind: 'google', re: /\bAIza[0-9A-Za-z_-]{35}\b/g },
|
|
24
|
+
{ kind: 'slack', re: /\bxox[baprs]-[0-9A-Za-z-]{10,}\b/g },
|
|
25
|
+
{ kind: 'api-key', re: /\bsk-(?:ant-|proj-|live-|test-)?[0-9A-Za-z_-]{20,}\b/g },
|
|
26
|
+
{ kind: 'jwt', re: /\beyJ[0-9A-Za-z_-]{10,}\.[0-9A-Za-z_-]{10,}\.[0-9A-Za-z_-]{10,}\b/g },
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
/* A long run of token characters, caught only when it looks random (not a
|
|
30
|
+
path, a hash or a word). */
|
|
31
|
+
const BLOB = /[0-9A-Za-z+/=_-]{40,}/g;
|
|
32
|
+
/* A SHORTER run of letters and digits only (no symbols, so a UUID's or path's dashes break it up): caught
|
|
33
|
+
when it is improbable -- it mixes upper, lower AND digits, and its entropy is high. This is the "unusual
|
|
34
|
+
word" net: a random-looking password or key the known shapes missed, without flagging a hash or an id. */
|
|
35
|
+
const WORD = /[A-Za-z0-9]{20,39}/g;
|
|
36
|
+
const ENTROPY_MIN = 3.5; /* bits per character */
|
|
37
|
+
const CLASSES_MIN = 3; /* of: lower, upper, digit, symbol */
|
|
38
|
+
|
|
39
|
+
function entropy(s) {
|
|
40
|
+
const count = new Map();
|
|
41
|
+
for (const c of s) count.set(c, (count.get(c) ?? 0) + 1);
|
|
42
|
+
let bits = 0;
|
|
43
|
+
for (const n of count.values()) { const p = n / s.length; bits -= p * Math.log2(p); }
|
|
44
|
+
return bits;
|
|
45
|
+
}
|
|
46
|
+
function classes(s) {
|
|
47
|
+
let n = 0;
|
|
48
|
+
if (/[a-z]/.test(s)) n += 1;
|
|
49
|
+
if (/[A-Z]/.test(s)) n += 1;
|
|
50
|
+
if (/[0-9]/.test(s)) n += 1;
|
|
51
|
+
if (/[^0-9A-Za-z]/.test(s)) n += 1;
|
|
52
|
+
return n;
|
|
53
|
+
}
|
|
54
|
+
/* A blob that is only hex (a git sha, a checksum) or only one case is not a
|
|
55
|
+
secret for us: too many false positives, and real tokens mix classes. */
|
|
56
|
+
function looksRandom(s) {
|
|
57
|
+
return classes(s) >= CLASSES_MIN && entropy(s) >= ENTROPY_MIN;
|
|
58
|
+
}
|
|
59
|
+
/* The shorter net: all three of upper, lower and digit present (a hash or a word misses one), and high entropy. */
|
|
60
|
+
function improbableWord(s) {
|
|
61
|
+
return /[a-z]/.test(s) && /[A-Z]/.test(s) && /[0-9]/.test(s) && entropy(s) >= ENTROPY_MIN;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* How a lifted secret is written back into the message: a marker the chat
|
|
65
|
+
shows as stars (ui side), and the tool reads to use /run/secrets/<name>.
|
|
66
|
+
Named like `SECRET_` + 10 hex (secrets.mjs stash). The angle brackets
|
|
67
|
+
(U+27E8/U+27E9) are vanishingly rare in a real message. */
|
|
68
|
+
export const secretRef = (name, at) => `⟨secret:${name}${Number.isFinite(at) ? `@${at}` : ''}⟩`;
|
|
69
|
+
export const SECRET_REF = /⟨secret:(SECRET_[0-9A-F]{10})(?:@(\d+))?⟩/g;
|
|
70
|
+
|
|
71
|
+
export function scan(text) {
|
|
72
|
+
const str = String(text ?? '');
|
|
73
|
+
const found = [];
|
|
74
|
+
for (const { kind, re } of SHAPES) {
|
|
75
|
+
re.lastIndex = 0;
|
|
76
|
+
for (let m; (m = re.exec(str));) found.push({ start: m.index, end: m.index + m[0].length, value: m[0], kind });
|
|
77
|
+
}
|
|
78
|
+
for (let m; (m = BLOB.exec(str));) {
|
|
79
|
+
if (looksRandom(m[0])) found.push({ start: m.index, end: m.index + m[0].length, value: m[0], kind: 'high-entropy' });
|
|
80
|
+
}
|
|
81
|
+
BLOB.lastIndex = 0;
|
|
82
|
+
for (let m; (m = WORD.exec(str));) {
|
|
83
|
+
if (improbableWord(m[0])) found.push({ start: m.index, end: m.index + m[0].length, value: m[0], kind: 'high-entropy' });
|
|
84
|
+
}
|
|
85
|
+
WORD.lastIndex = 0;
|
|
86
|
+
/* Keep left-to-right, and when two matches overlap keep the one that starts
|
|
87
|
+
first (and, equal, the longer): a known prefix wins over the blob rule. */
|
|
88
|
+
found.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
89
|
+
const out = [];
|
|
90
|
+
let reach = -1;
|
|
91
|
+
for (const f of found) { if (f.start >= reach) { out.push(f); reach = f.end; } }
|
|
92
|
+
return out;
|
|
93
|
+
}
|
package/server/secrets.mjs
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* should use on the agent's behalf (a push key, a mailbox password) does not
|
|
22
22
|
* belong here, and never enters a VM.
|
|
23
23
|
*/
|
|
24
|
+
import { randomBytes } from 'node:crypto';
|
|
24
25
|
import { Refusal } from './conversations.mjs';
|
|
25
26
|
import { Grants, conversationSubject } from './grants.mjs';
|
|
26
27
|
import { isConversationName } from './names.mjs';
|
|
@@ -30,6 +31,12 @@ export const SECRETS_DIR = '/run/secrets';
|
|
|
30
31
|
export const RUNNER = '/usr/local/bin/clv-run';
|
|
31
32
|
export const VALUE_MAX = 8 * 1024;
|
|
32
33
|
export const WARN_DAYS = 7;
|
|
34
|
+
/* A secret lifted from a message (secret-scan.mjs): a random name, a short
|
|
35
|
+
life in MINUTES (not days), swept when it runs out. */
|
|
36
|
+
export const EPHEMERAL_PREFIX = 'SECRET_';
|
|
37
|
+
export const TTL_DEFAULT_MS = 10 * 60_000;
|
|
38
|
+
export const TTL_MIN_MS = 60_000;
|
|
39
|
+
export const TTL_MAX_MS = 60 * 60_000;
|
|
33
40
|
const DAY = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
34
41
|
const DAY_MS = 86_400_000;
|
|
35
42
|
const NAME = /^[A-Z][A-Z0-9_]{0,39}$/;
|
|
@@ -69,7 +76,7 @@ export function daysTo(day, now = new Date()) {
|
|
|
69
76
|
const firstLine = (text) => String(text ?? '').trim().split('\n').filter(Boolean).pop()?.slice(0, 200) ?? '';
|
|
70
77
|
|
|
71
78
|
export class Secrets {
|
|
72
|
-
#store; #vault; #vms; #grants; #now;
|
|
79
|
+
#store; #vault; #vms; #grants; #now; #timers = new Map();
|
|
73
80
|
|
|
74
81
|
/* `grants`: the one table of rights. "Handed to a conversation" is a
|
|
75
82
|
line of it: the conversation may USE the secret. */
|
|
@@ -80,9 +87,19 @@ export class Secrets {
|
|
|
80
87
|
for (const conversation of Object.keys(s.attachments)) this.#grants.give(conversationSubject(conversation), `secret:${s.name}`, 'x');
|
|
81
88
|
delete s.attachments; store.put(s);
|
|
82
89
|
}
|
|
90
|
+
/* A restart loses the sweep timers: drop the ephemeral secrets that ran
|
|
91
|
+
out while down, and set a fresh timer for those still alive. */
|
|
92
|
+
for (const s of store.all()) {
|
|
93
|
+
if (s.ephemeral !== true) continue;
|
|
94
|
+
const who = this.#users(s.name)[0];
|
|
95
|
+
const left = who ? Date.parse(s.expiresAt) - this.#now().getTime() : 0;
|
|
96
|
+
if (!who || !(left > 0)) { store.remove(s.name); this.#grants.forgetAsset(`secret:${s.name}`); }
|
|
97
|
+
else this.#schedule(who, s.name, left);
|
|
98
|
+
}
|
|
83
99
|
}
|
|
84
100
|
#handed(conversation, name) { return this.#grants.can(conversationSubject(conversation), `secret:${name}`, 'x'); }
|
|
85
|
-
|
|
101
|
+
/* Ephemeral: gone at its moment (minutes). Otherwise: gone from its day on. */
|
|
102
|
+
#expired(s) { return s.expiresAt ? Date.parse(s.expiresAt) <= this.#now().getTime() : (Boolean(s.expires) && daysTo(s.expires, this.#now()) <= 0); }
|
|
86
103
|
#users(name) { return this.#grants.on(`secret:${name}`).map((g) => g.subject.slice('conversation:'.length)); }
|
|
87
104
|
/* What is delivered to this conversation: handed, and not expired. */
|
|
88
105
|
#given(conversation) { return this.#store.all().filter((s) => this.#handed(conversation, s.name) && !this.#expired(s)); }
|
|
@@ -91,7 +108,8 @@ export class Secrets {
|
|
|
91
108
|
days left (null without an expiry); `expired`: no longer delivered. */
|
|
92
109
|
list() {
|
|
93
110
|
return this.#store.all().map((s) => ({ name: s.name, conversations: this.#users(s.name), createdAt: s.createdAt,
|
|
94
|
-
expires: s.expires ?? null, days: s.expires ? daysTo(s.expires, this.#now()) : null, expired: this.#expired(s)
|
|
111
|
+
expires: s.expires ?? null, days: s.expires ? daysTo(s.expires, this.#now()) : null, expired: this.#expired(s),
|
|
112
|
+
...(s.ephemeral ? { ephemeral: true, expiresAt: s.expiresAt, expiresInMs: Math.max(0, Date.parse(s.expiresAt) - this.#now().getTime()) } : {}) }));
|
|
95
113
|
}
|
|
96
114
|
/* Past its expiry day: kept, not used (nor by dictation, voice.mjs). */
|
|
97
115
|
isExpired(name) { const s = this.#store.get(name); return Boolean(s) && this.#expired(s); }
|
|
@@ -99,6 +117,9 @@ export class Secrets {
|
|
|
99
117
|
of(conversation) { return this.#store.all().filter((s) => this.#handed(conversation, s.name)).map((s) => s.name); }
|
|
100
118
|
/* Delivered to it at its next start. */
|
|
101
119
|
given(conversation) { return this.#given(conversation).map((s) => s.name); }
|
|
120
|
+
/* The EPHEMERAL secrets live for it now (a message of it holds their marker), each with how long it has
|
|
121
|
+
left -- the chat shows the stars with that, and a way to forget it (talk/parley-ui). Never a value. */
|
|
122
|
+
liveFor(conversation) { return this.#given(conversation).filter((s) => s.ephemeral === true).map((s) => ({ name: s.name, expiresInMs: Math.max(0, Date.parse(s.expiresAt) - this.#now().getTime()) })); }
|
|
102
123
|
|
|
103
124
|
/* The value, for this server's own use only (the key of the dictation
|
|
104
125
|
engine, put in a request's header): never returned by any route. */
|
|
@@ -187,6 +208,48 @@ export class Secrets {
|
|
|
187
208
|
for (const s of this.#store.all()) this.#grants.take(conversationSubject(conversation), `secret:${s.name}`);
|
|
188
209
|
}
|
|
189
210
|
|
|
211
|
+
/* AN EPHEMERAL SECRET lifted from a message of `conversation` (talk.mjs):
|
|
212
|
+
a random name, a few minutes to live, handed at once to that
|
|
213
|
+
conversation, swept when it runs out. Returns { name, expiresAt } -- the
|
|
214
|
+
name is the reference the message keeps (its value is masked there). As
|
|
215
|
+
with every secret the value is sealed and never returned by any route;
|
|
216
|
+
the tool uses it only as /run/secrets/<name>, and never sees it. */
|
|
217
|
+
stash(conversation, value, { ttlMs } = {}) {
|
|
218
|
+
if (!isConversationName(conversation)) throw new Refusal(400, 'Not a conversation.');
|
|
219
|
+
const sealed = this.#seal(value);
|
|
220
|
+
const ttl = Math.min(TTL_MAX_MS, Math.max(TTL_MIN_MS, Number.isFinite(ttlMs) ? ttlMs : TTL_DEFAULT_MS));
|
|
221
|
+
let name; do { name = EPHEMERAL_PREFIX + randomBytes(5).toString('hex').toUpperCase(); } while (this.#store.get(name));
|
|
222
|
+
const expiresAt = new Date(this.#now().getTime() + ttl).toISOString();
|
|
223
|
+
this.#store.put({ name, sealed, createdAt: this.#now().toISOString(), expiresAt, ephemeral: true });
|
|
224
|
+
this.#grants.give(conversationSubject(conversation), `secret:${name}`, 'x');
|
|
225
|
+
this.#schedule(conversation, name, ttl);
|
|
226
|
+
return { name, expiresAt };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/* Forgotten before it runs out, on the tool's word or the owner's: gone
|
|
230
|
+
from the vault and, at once, from the running machine. */
|
|
231
|
+
async drop(conversation, name) {
|
|
232
|
+
const s = this.#store.get(name);
|
|
233
|
+
if (!s || s.ephemeral !== true) throw new Refusal(404, 'No such short-lived secret.');
|
|
234
|
+
const t = this.#timers.get(name); if (t) { clearTimeout(t); this.#timers.delete(name); }
|
|
235
|
+
await this.#reap(conversation, name);
|
|
236
|
+
return { name };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
#schedule(conversation, name, ttl) {
|
|
240
|
+
const t = setTimeout(() => { this.#timers.delete(name); void this.#reap(conversation, name); }, ttl);
|
|
241
|
+
t.unref?.();
|
|
242
|
+
this.#timers.set(name, t);
|
|
243
|
+
}
|
|
244
|
+
/* Remove it, then rewrite the machine's /run/secrets without it (a tmpfs
|
|
245
|
+
already cleared if the machine is gone -- so a failure here is fine). */
|
|
246
|
+
async #reap(conversation, name) {
|
|
247
|
+
if (this.#store.get(name)) { this.#store.remove(name); this.#grants.forgetAsset(`secret:${name}`); }
|
|
248
|
+
try { await this.deliver(conversation); } catch { /* machine asleep or busy: /run/secrets is tmpfs */ }
|
|
249
|
+
try { this.reaped(conversation, name); } catch { /* told nowhere: never a reason to fail */ }
|
|
250
|
+
}
|
|
251
|
+
reaped = () => {};
|
|
252
|
+
|
|
190
253
|
/* What a delivery to this conversation would contain, as one string that
|
|
191
254
|
changes whenever a delivery would differ: names and sealed values. */
|
|
192
255
|
signature(conversation) {
|
package/server/talk.mjs
CHANGED
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
*/
|
|
61
61
|
import { posix, join } from 'node:path';
|
|
62
62
|
import { randomUUID, createHash } from 'node:crypto';
|
|
63
|
-
import { mkdirSync, lstatSync, rmSync } from 'node:fs';
|
|
63
|
+
import { mkdirSync, lstatSync, rmSync, readFileSync } from 'node:fs';
|
|
64
64
|
import { install, Parley, WAKE, reread, texts, fill, LANGUAGES, SHOWN_MAX, FILES_MAX } from 'clavarium-parley/host';
|
|
65
65
|
import { importFile } from './files.mjs';
|
|
66
66
|
import { KEYS } from 'clavarium-parley/ui';
|
|
@@ -182,7 +182,7 @@ const PART = /^[A-Za-z0-9._~-]{1,200}$/;
|
|
|
182
182
|
export class Talk {
|
|
183
183
|
#store; #words; #vms; #conversations; #resources; #toolbox; #warm; #alert; #listening = new Map(); #every; #now;
|
|
184
184
|
#typedAt = new Map(); #nextTry = new Map(); #trying = new Map(); #timer; #lastLang = 'en'; #rereadAfter;
|
|
185
|
-
#terminal; #health; #ranAt = new Map(); #kept;
|
|
185
|
+
#terminal; #health; #ranAt = new Map(); #kept; #screen; #dropSecret;
|
|
186
186
|
|
|
187
187
|
/* `alert({ title, body, url })`: told when the tool asks, or waits.
|
|
188
188
|
`words`: a store for the owner's choice of words (one record). */
|
|
@@ -192,8 +192,8 @@ export class Talk {
|
|
|
192
192
|
/* `keptDir`: where each conversation's thread is kept (its own folder),
|
|
193
193
|
in this server's data -- never in the work folder, which its machine
|
|
194
194
|
writes. Without it, the bridge folder is all there is. */
|
|
195
|
-
constructor({ store, words, vms, conversations, resources, toolbox = null, warm = async () => {}, alert = () => {}, listenEveryMs = 2000, now = () => Date.now(), rereadAfterMs = REREAD_AFTER_MS, terminal = null, health = null, keptDir = null }) {
|
|
196
|
-
this.#terminal = terminal; this.#health = health; this.#kept = keptDir;
|
|
195
|
+
constructor({ store, words, vms, conversations, resources, toolbox = null, warm = async () => {}, alert = () => {}, listenEveryMs = 2000, now = () => Date.now(), rereadAfterMs = REREAD_AFTER_MS, terminal = null, health = null, keptDir = null, screen = null, dropSecret = null }) {
|
|
196
|
+
this.#terminal = terminal; this.#health = health; this.#kept = keptDir; this.#screen = screen; this.#dropSecret = dropSecret;
|
|
197
197
|
this.#store = store; this.#words = words; this.#vms = vms; this.#conversations = conversations; this.#resources = resources; this.#toolbox = toolbox; this.#warm = warm; this.#alert = alert; this.#every = listenEveryMs; this.#now = now; this.#rereadAfter = rereadAfterMs;
|
|
198
198
|
for (const s of store.all()) if (s.on === true) this.#listen(s.name);
|
|
199
199
|
this.#timer = setInterval(() => { void this.#tick(); }, Math.min(listenEveryMs, 1000));
|
|
@@ -448,7 +448,13 @@ export class Talk {
|
|
|
448
448
|
const note = item && typeof item === 'object' && typeof item.note === 'string' ? item.note : '';
|
|
449
449
|
return { path, name: m[2], size: info.size, ...(note ? { note } : {}) };
|
|
450
450
|
});
|
|
451
|
-
|
|
451
|
+
/* A secret pasted in the message (a token, a key) is lifted into a
|
|
452
|
+
short-lived store, and the message keeps only a reference -- the
|
|
453
|
+
thread never holds the value (secret-scan.mjs, secrets.mjs). It must
|
|
454
|
+
never lose a message: on any trouble the text is sent as it came. */
|
|
455
|
+
let said = text ?? '';
|
|
456
|
+
if (this.#screen && said) { try { said = await this.#screen(name, said); } catch { said = text ?? ''; } }
|
|
457
|
+
try { this.#parley(name).say(said, answers, attached, { via: via === 'voice' ? 'voice' : undefined, ...(extra && typeof extra === 'object' ? { picked: extra.picked, replies: extra.replies, about: extra.about } : {}) }); } catch (error) { if (error instanceof RangeError) throw new Refusal(400, error.message); throw error; }
|
|
452
458
|
const told = await this.#wake(name);
|
|
453
459
|
return { ...this.of(name), told };
|
|
454
460
|
}
|
|
@@ -637,6 +643,7 @@ export class Talk {
|
|
|
637
643
|
|
|
638
644
|
async #tick() {
|
|
639
645
|
for (const r of this.#store.all()) {
|
|
646
|
+
if (r.on === true) this.#reapForgets(r);
|
|
640
647
|
if (r.on === true && r.reread === true) { await this.#tryReread(r.name).catch(() => {}); continue; }
|
|
641
648
|
if (r.on !== true || (!r.pending && !r.woke)) continue;
|
|
642
649
|
/* It has read: what waited is done, and the next message wakes it again. */
|
|
@@ -646,6 +653,20 @@ export class Talk {
|
|
|
646
653
|
}
|
|
647
654
|
}
|
|
648
655
|
|
|
656
|
+
/* The agent asked to forget short-lived secrets (parley forget -> forget.jsonl beside its thread): the
|
|
657
|
+
parloir drops each from its vault and machine at once. Each request is acted on once, counted in the store. */
|
|
658
|
+
#reapForgets(r) {
|
|
659
|
+
if (!this.#dropSecret) return;
|
|
660
|
+
let lines;
|
|
661
|
+
try { lines = readFileSync(`${this.#dir(r.name)}/forget.jsonl`, 'utf8').split('\n').filter(Boolean); } catch { return; }
|
|
662
|
+
const done = r.forgotten ?? 0;
|
|
663
|
+
if (lines.length <= done) return;
|
|
664
|
+
for (const line of lines.slice(done)) {
|
|
665
|
+
try { const name = JSON.parse(line)?.name; if (typeof name === 'string' && /^SECRET_[0-9A-F]{10}$/.test(name)) void this.#dropSecret(r.name, name); } catch { /* a stray line */ }
|
|
666
|
+
}
|
|
667
|
+
this.#store.put({ ...r, forgotten: lines.length });
|
|
668
|
+
}
|
|
669
|
+
|
|
649
670
|
forget(name) { this.#deaf(name); if (this.#keep(name)) rmSync(this.#keep(name), { recursive: true, force: true }); this.#store.remove(name); this.#typedAt.delete(name); this.#ranAt.delete(name); this.#nextTry.delete(name); this.#nextTry.delete(`reread:${name}`); }
|
|
650
671
|
stop() { clearInterval(this.#timer); for (const name of [...this.#listening.keys()]) this.#deaf(name); }
|
|
651
672
|
}
|
package/web/app.js
CHANGED
|
@@ -1022,7 +1022,8 @@
|
|
|
1022
1022
|
const whenWords = (t) => {
|
|
1023
1023
|
if (!t) return '';
|
|
1024
1024
|
const d = new Date(t); const now = new Date();
|
|
1025
|
-
|
|
1025
|
+
/* Today: how long ago, not the clock -- the list is sorted by it, so "2 min" reads better than "15:05". */
|
|
1026
|
+
if (d.toDateString() === now.toDateString()) { const m = Math.max(0, Math.floor((now - d) / 60_000)); return m < 1 ? 'now' : m < 60 ? `${m} min` : `${Math.floor(m / 60)} h`; }
|
|
1026
1027
|
const y = new Date(now); y.setDate(now.getDate() - 1);
|
|
1027
1028
|
if (d.toDateString() === y.toDateString()) return 'yesterday';
|
|
1028
1029
|
return now - d < 6 * 86_400_000 ? d.toLocaleDateString([], { weekday: 'short' }) : d.toLocaleDateString([], { day: 'numeric', month: 'short' });
|
|
@@ -1054,7 +1055,9 @@
|
|
|
1054
1055
|
const when = document.createElement('small'); when.className = 'when'; when.textContent = whenWords(activityOf(c));
|
|
1055
1056
|
b.append(title);
|
|
1056
1057
|
/* Its last message, and who said it: so it reads as the last message, not as a subject. */
|
|
1057
|
-
|
|
1058
|
+
/* Its last message. The agent is the usual speaker, so its name is left off (just the words); only
|
|
1059
|
+
the person's own last word is marked "You:", which tells you it is your move. */
|
|
1060
|
+
if (c.talk?.lastText) { const last = document.createElement('small'); last.className = 'last'; last.textContent = c.talk.lastFrom === 'person' ? `You: ${c.talk.lastText}` : c.talk.lastText; b.append(last); }
|
|
1058
1061
|
if (!plain) b.append(sub);
|
|
1059
1062
|
b.append(when); b.dataset.when = 'true';
|
|
1060
1063
|
} else b.append(title, ...(plain ? [] : [sub]));
|
|
@@ -2516,6 +2519,11 @@
|
|
|
2516
2519
|
const name = current;
|
|
2517
2520
|
if (name !== current || (thread && threadOf === current)) return;
|
|
2518
2521
|
threadOf = name;
|
|
2522
|
+
/* The short-lived secrets live in this conversation now (secret-scan.mjs): the chat shows each as
|
|
2523
|
+
stars with its time left and a way to forget it. Kept current on each reload, read on each repaint. */
|
|
2524
|
+
const chatSecrets = {};
|
|
2525
|
+
const refreshSecrets = () => { for (const k of Object.keys(chatSecrets)) delete chatSecrets[k]; for (const s of conversations.find((x) => x.name === name)?.ephemeralSecrets ?? []) chatSecrets[s.name] = { expiresInMs: s.expiresInMs }; };
|
|
2526
|
+
refreshSecrets();
|
|
2519
2527
|
/* The lines of its machine seen so far: what changed comes alone, and their buttons are worked out on all of them. */
|
|
2520
2528
|
const lines = new Map();
|
|
2521
2529
|
const withLines = (data, asked) => {
|
|
@@ -2529,7 +2537,7 @@
|
|
|
2529
2537
|
onView: (v) => { chatView = v; try { localStorage.setItem('clavarium-chat-view', v); } catch { /* as above */ } },
|
|
2530
2538
|
/* A part of the thread: its end at first, then what changed, the page before on demand (Parley's window). */
|
|
2531
2539
|
window: windowFor(chatWindow),
|
|
2532
|
-
load: (asked = {}) => fetch(talkPath(name, '', asked)).then((r) => r.json()).then((data) => withLines(data, asked)).then(machineLines).then((data) => withMachine(data, name)),
|
|
2540
|
+
load: (asked = {}) => fetch(talkPath(name, '', asked)).then((r) => r.json()).then((data) => withLines(data, asked)).then(machineLines).then((data) => withMachine(data, name)).then((data) => (refreshSecrets(), data)),
|
|
2533
2541
|
/* The whole thread searched, however little of it is shown. */
|
|
2534
2542
|
search: (q) => fetch(talkPath(name, '/search', { q })).then((r) => (r.ok ? r.json() : { matches: [], count: 0 })),
|
|
2535
2543
|
/* `via: 'voice'`: part of it was dictated, said to the tool. */
|
|
@@ -2562,6 +2570,9 @@
|
|
|
2562
2570
|
/* Its pictures, from the shared folders only, by the same route as the thumbnails of Files (type proved by the bytes, never SVG). */
|
|
2563
2571
|
picture: (file) => { if (!file.open) return null; const at = file.open.indexOf(':'); return `${API}/files/thumb?${new URLSearchParams({ c: name, r: file.open.slice(0, at), p: file.open.slice(at + 1) })}`; },
|
|
2564
2572
|
diagram: drawDiagram, highlight: colourCode,
|
|
2573
|
+
/* Short-lived secrets of this conversation: the stars say their time left and offer to forget them. */
|
|
2574
|
+
secrets: chatSecrets,
|
|
2575
|
+
forget: (secretName) => post(`${API}/conversations/${encodeURIComponent(name)}/secrets/${encodeURIComponent(secretName)}/forget`, {}).then(() => { delete chatSecrets[secretName]; if (thread && threadOf === name) void thread.refresh(); }).catch(() => {}),
|
|
2565
2576
|
remind: () => post(talkPath(name, '/remind'), {}),
|
|
2566
2577
|
/* The helpers of the terminal, in the chat: this page's voice, the owner's ready answers, the keyboard rule of this device. */
|
|
2567
2578
|
voice: chatVoice(), autoSend: chatAuto,
|
|
@@ -3217,6 +3228,8 @@
|
|
|
3217
3228
|
const gitSub = (r) => (r.from ? `from ${hostOf(r.from)}` : 'repository');
|
|
3218
3229
|
/* A secret's expiry, as said: `due` when it asks for the person (near, or past). */
|
|
3219
3230
|
const secretState = (s) => {
|
|
3231
|
+
/* Short-lived, lifted from a message (secrets.mjs): minutes, not days -- shown so it can be seen and let go. */
|
|
3232
|
+
if (s.ephemeral) { const mins = Math.max(0, Math.round((s.expiresInMs ?? 0) / 60_000)); return s.expired || mins <= 0 ? { text: 'Expired', tone: 'bad', due: true } : { text: `Short-lived, ${mins} min left`, tone: 'warn', due: true }; }
|
|
3220
3233
|
if (s.expired) return { text: 'Expired', tone: 'bad', due: true };
|
|
3221
3234
|
if (s.days === null || s.days === undefined) return { text: 'No expiry', tone: '', due: false };
|
|
3222
3235
|
if (secretDue(s)) return { text: s.days === 1 ? 'Expires tomorrow' : `Expires in ${s.days} days`, tone: 'warn', due: true };
|