shadok-ai 0.20.46 → 0.20.48
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 +8 -4
- package/dist/external-users.d.ts +102 -0
- package/dist/external-users.js +159 -2
- package/dist/external-users.js.map +1 -1
- package/dist/server.js +210 -24
- package/dist/server.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +40 -17
- package/public/line-icons.js +3 -0
package/README.md
CHANGED
|
@@ -421,9 +421,12 @@ Enter to skip; you can add it later from the web UI).
|
|
|
421
421
|
agent that goes looking can still read its neighbour's folder. It stops the
|
|
422
422
|
accident, not a determined agent. **An External group is where that becomes a
|
|
423
423
|
real wall**: one Unix user per conversation, its own home, its own files and
|
|
424
|
-
attachments, created the first time that person writes and kept for
|
|
425
|
-
|
|
426
|
-
|
|
424
|
+
attachments, created the first time that person writes and kept for as long
|
|
425
|
+
as their agent exists (an idle agent that was put to sleep still exists).
|
|
426
|
+
**Deleting someone's agent there erases their conversation**: the account, its
|
|
427
|
+
home, its attachments, its temp files and the secrets they handed over. If
|
|
428
|
+
they write again they start from nothing, under a fresh account. Deleting the
|
|
429
|
+
group does the same for every conversation it ever admitted.
|
|
427
430
|
What **is** a wall in both is the cockpit's own API: a guest's agent is boxed to its
|
|
428
431
|
own conversation, so it cannot see, drive or diff another guest's agent, reads
|
|
429
432
|
no secret and no shared ledger row, obtains no connected account, and can
|
|
@@ -444,7 +447,8 @@ Enter to skip; you can add it later from the web UI).
|
|
|
444
447
|
leaves what you set alone. Go back to
|
|
445
448
|
**Board group** and every such conversation is closed at once.
|
|
446
449
|
- **Link to a message** — every message (yours and the agent's, and a **room**'s
|
|
447
|
-
too) has a
|
|
450
|
+
too) has a link icon beside the copy icon — both bare icons, their words in a
|
|
451
|
+
tooltip on hover; a click turns the icon into a ✓. It copies a URL like
|
|
448
452
|
`…/a/<session>#msg=<id>` for an agent, `…/#room=<room>&msg=<id>` for a
|
|
449
453
|
room; whoever opens it — someone who already has access to this cockpit, after
|
|
450
454
|
the usual sign-in — lands on that conversation, scrolled to that message,
|
package/dist/external-users.d.ts
CHANGED
|
@@ -26,6 +26,17 @@ export interface ExternalUserRow {
|
|
|
26
26
|
/** The isolation unit it was minted for (`<group>/<conversation>`). */
|
|
27
27
|
unit: UnitId;
|
|
28
28
|
createdAt: number;
|
|
29
|
+
/**
|
|
30
|
+
* When the teardown of this conversation BEGAN — absent on a live row.
|
|
31
|
+
*
|
|
32
|
+
* Written before the first destructive step and dropped with the row once
|
|
33
|
+
* the account is gone, so a teardown that was interrupted (a crash, a
|
|
34
|
+
* `userdel` refused because a process was still dying) is a fact on disk and
|
|
35
|
+
* not a guess: the boot reconciler finishes it instead of re-provisioning the
|
|
36
|
+
* account, and an admission waits for it instead of landing in the files the
|
|
37
|
+
* operator asked to erase.
|
|
38
|
+
*/
|
|
39
|
+
retiring?: number;
|
|
29
40
|
}
|
|
30
41
|
/**
|
|
31
42
|
* What provisioning this conversation's user amounts to, against the registry.
|
|
@@ -60,6 +71,97 @@ export declare function externalUsersOfGroup(rows: readonly ExternalUserRow[], g
|
|
|
60
71
|
export declare function externalUserRow(rows: readonly ExternalUserRow[], groupId: string, conversation: string): ExternalUserRow | null;
|
|
61
72
|
/** Drop rows (new array) — a torn-down conversation, or a deleted group's lot. */
|
|
62
73
|
export declare function withoutExternalUsers(rows: readonly ExternalUserRow[], users: readonly string[]): ExternalUserRow[];
|
|
74
|
+
/**
|
|
75
|
+
* Mark a conversation's account as being torn down (new array). The first mark
|
|
76
|
+
* wins: a retry must not make an old interrupted teardown look recent.
|
|
77
|
+
*/
|
|
78
|
+
export declare function withRetiring(rows: readonly ExternalUserRow[], user: string, now: number): ExternalUserRow[];
|
|
79
|
+
/**
|
|
80
|
+
* Is this conversation still held by ANOTHER agent than the one being closed?
|
|
81
|
+
*
|
|
82
|
+
* A conversation's account is torn down with its LAST agent, and "last" is
|
|
83
|
+
* read from the channel registry — the authority on which agents exist — never
|
|
84
|
+
* assumed from "one conversation, one agent". That is true today; the day it
|
|
85
|
+
* is not, an assumption here would `userdel` the account under a live agent.
|
|
86
|
+
* `keyOf` is `guestConversationKey`, injected so this file stays free of the
|
|
87
|
+
* bridges it would otherwise import.
|
|
88
|
+
*/
|
|
89
|
+
export declare function conversationHeld<C extends {
|
|
90
|
+
sessionId: string;
|
|
91
|
+
group?: string | null;
|
|
92
|
+
}>(channels: readonly C[], closed: {
|
|
93
|
+
sessionId: string;
|
|
94
|
+
group: string;
|
|
95
|
+
conversation: string;
|
|
96
|
+
}, keyOf: (c: C) => string | null): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* The uids a LIVE process runs under — the four on the `Uid:` line of its
|
|
99
|
+
* `/proc/<pid>/status` (real, effective, saved, fs) — or `[]` for a text that
|
|
100
|
+
* carries none, and for a ZOMBIE.
|
|
101
|
+
*
|
|
102
|
+
* The zombie case is not a nicety. In a container pid 1 is the application,
|
|
103
|
+
* not an init that reaps (the reason `src/lock.ts` reads the same state): a
|
|
104
|
+
* killed process whose parent already exited stays in `/proc` under its uid
|
|
105
|
+
* until something collects it, which may be never. Counted as alive, every
|
|
106
|
+
* teardown would report "survived the kill" forever — measured on a real
|
|
107
|
+
* container, three of three. It holds no file open and can write nothing, so
|
|
108
|
+
* for an erasure it is gone.
|
|
109
|
+
*/
|
|
110
|
+
export declare function liveUidsOfProcStatus(status: string): number[];
|
|
111
|
+
export interface ConversationTeardownDeps {
|
|
112
|
+
/** The account's numeric uid, or null when the account does not exist. */
|
|
113
|
+
uidOf: (user: string) => number | null;
|
|
114
|
+
/** The uid the server itself runs as — never a uid to kill or sweep. */
|
|
115
|
+
selfUid: number;
|
|
116
|
+
/** Kill every process running as this uid; resolves to how many are LEFT. */
|
|
117
|
+
killProcesses: (uid: number) => Promise<number>;
|
|
118
|
+
/** Delete what this uid owns OUTSIDE its home (temp directories). */
|
|
119
|
+
sweepStray: (uid: number) => Promise<void>;
|
|
120
|
+
/** Delete the conversation's media directory (server-owned, outside the home). */
|
|
121
|
+
removeMedia: () => Promise<void> | void;
|
|
122
|
+
/** `userdel --remove <user>`: the account, its home, its mail spool. */
|
|
123
|
+
userdel: (user: string) => Promise<void>;
|
|
124
|
+
/** Delete the home of an account that NO LONGER EXISTS (nothing left for
|
|
125
|
+
* `userdel` to do it). */
|
|
126
|
+
removeHome: (user: string) => Promise<void>;
|
|
127
|
+
/** Pause between two attempts at an account a dying process still holds. */
|
|
128
|
+
wait?: (ms: number) => Promise<void>;
|
|
129
|
+
}
|
|
130
|
+
/** The only shape `osUserForUnit` gives a conversation's account. */
|
|
131
|
+
export declare const CONVERSATION_USER_RE: RegExp;
|
|
132
|
+
/** How many times the kill → `userdel` pair is tried before giving up. */
|
|
133
|
+
export declare const TEARDOWN_ATTEMPTS = 3;
|
|
134
|
+
/**
|
|
135
|
+
* Erase ONE conversation's account and everything it owns on this host.
|
|
136
|
+
*
|
|
137
|
+
* The order is the design, and each step is there for a failure that would
|
|
138
|
+
* otherwise be silent:
|
|
139
|
+
*
|
|
140
|
+
* 1. **Processes first.** Stopping the agent stops `claude`, not what it
|
|
141
|
+
* started, and `userdel` does NOT refuse an account with live processes
|
|
142
|
+
* (measured: it only looks at logins) — a background shell would simply run
|
|
143
|
+
* on, under a uid nothing names any more, rewriting what we delete.
|
|
144
|
+
* 2. **Then what the uid owns outside its home**, while the uid is still known.
|
|
145
|
+
* `userdel --remove` only takes the home, and `useradd` hands out the lowest
|
|
146
|
+
* free uid above the others — so the NEXT stranger admitted can receive this
|
|
147
|
+
* very uid and, with it, every temp file left behind. That is the uid
|
|
148
|
+
* recycling the design names as an anti-goal, and it is exactly what
|
|
149
|
+
* "delete, then recreate" walks into.
|
|
150
|
+
* 3. **The media directory**, which the server owns and no `userdel` knows of.
|
|
151
|
+
* 4. **The account and its home, last** — so a failure before it leaves an
|
|
152
|
+
* account whose registry row still says whose it was.
|
|
153
|
+
*
|
|
154
|
+
* It never throws. `ok: false` means the account may still be there: the
|
|
155
|
+
* caller keeps the registry row (marked `retiring`), which is what makes the
|
|
156
|
+
* retry possible and what stops a later admission from inheriting the files.
|
|
157
|
+
* An account that is already gone is a success once its home is gone too — the
|
|
158
|
+
* retry after a crash between the `userdel` and the row's removal must
|
|
159
|
+
* converge.
|
|
160
|
+
*/
|
|
161
|
+
export declare function teardownConversationUser(user: string, deps: ConversationTeardownDeps): Promise<{
|
|
162
|
+
ok: boolean;
|
|
163
|
+
warnings: string[];
|
|
164
|
+
}>;
|
|
63
165
|
/**
|
|
64
166
|
* A stored registry, cleaned. A malformed row is DROPPED rather than repaired:
|
|
65
167
|
* a row is a claim that an account belongs to one conversation, and a half-read
|
package/dist/external-users.js
CHANGED
|
@@ -56,6 +56,158 @@ export function withoutExternalUsers(rows, users) {
|
|
|
56
56
|
const gone = new Set(users);
|
|
57
57
|
return rows.filter((r) => !gone.has(r.user));
|
|
58
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Mark a conversation's account as being torn down (new array). The first mark
|
|
61
|
+
* wins: a retry must not make an old interrupted teardown look recent.
|
|
62
|
+
*/
|
|
63
|
+
export function withRetiring(rows, user, now) {
|
|
64
|
+
return rows.map((r) => (r.user === user && r.retiring == null ? { ...r, retiring: now } : r));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Is this conversation still held by ANOTHER agent than the one being closed?
|
|
68
|
+
*
|
|
69
|
+
* A conversation's account is torn down with its LAST agent, and "last" is
|
|
70
|
+
* read from the channel registry — the authority on which agents exist — never
|
|
71
|
+
* assumed from "one conversation, one agent". That is true today; the day it
|
|
72
|
+
* is not, an assumption here would `userdel` the account under a live agent.
|
|
73
|
+
* `keyOf` is `guestConversationKey`, injected so this file stays free of the
|
|
74
|
+
* bridges it would otherwise import.
|
|
75
|
+
*/
|
|
76
|
+
export function conversationHeld(channels, closed, keyOf) {
|
|
77
|
+
return channels.some((c) => c.sessionId !== closed.sessionId && c.group === closed.group && keyOf(c) === closed.conversation);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The uids a LIVE process runs under — the four on the `Uid:` line of its
|
|
81
|
+
* `/proc/<pid>/status` (real, effective, saved, fs) — or `[]` for a text that
|
|
82
|
+
* carries none, and for a ZOMBIE.
|
|
83
|
+
*
|
|
84
|
+
* The zombie case is not a nicety. In a container pid 1 is the application,
|
|
85
|
+
* not an init that reaps (the reason `src/lock.ts` reads the same state): a
|
|
86
|
+
* killed process whose parent already exited stays in `/proc` under its uid
|
|
87
|
+
* until something collects it, which may be never. Counted as alive, every
|
|
88
|
+
* teardown would report "survived the kill" forever — measured on a real
|
|
89
|
+
* container, three of three. It holds no file open and can write nothing, so
|
|
90
|
+
* for an erasure it is gone.
|
|
91
|
+
*/
|
|
92
|
+
export function liveUidsOfProcStatus(status) {
|
|
93
|
+
if (/^State:\s+[ZX]\b/m.test(status))
|
|
94
|
+
return [];
|
|
95
|
+
const m = /^Uid:\s+(.+)$/m.exec(status);
|
|
96
|
+
if (!m)
|
|
97
|
+
return [];
|
|
98
|
+
return m[1].trim().split(/\s+/).map(Number).filter((n) => Number.isInteger(n) && n >= 0);
|
|
99
|
+
}
|
|
100
|
+
/** The only shape `osUserForUnit` gives a conversation's account. */
|
|
101
|
+
export const CONVERSATION_USER_RE = /^sk-x[0-9a-f]{27}$/;
|
|
102
|
+
/** How many times the kill → `userdel` pair is tried before giving up. */
|
|
103
|
+
export const TEARDOWN_ATTEMPTS = 3;
|
|
104
|
+
/**
|
|
105
|
+
* Erase ONE conversation's account and everything it owns on this host.
|
|
106
|
+
*
|
|
107
|
+
* The order is the design, and each step is there for a failure that would
|
|
108
|
+
* otherwise be silent:
|
|
109
|
+
*
|
|
110
|
+
* 1. **Processes first.** Stopping the agent stops `claude`, not what it
|
|
111
|
+
* started, and `userdel` does NOT refuse an account with live processes
|
|
112
|
+
* (measured: it only looks at logins) — a background shell would simply run
|
|
113
|
+
* on, under a uid nothing names any more, rewriting what we delete.
|
|
114
|
+
* 2. **Then what the uid owns outside its home**, while the uid is still known.
|
|
115
|
+
* `userdel --remove` only takes the home, and `useradd` hands out the lowest
|
|
116
|
+
* free uid above the others — so the NEXT stranger admitted can receive this
|
|
117
|
+
* very uid and, with it, every temp file left behind. That is the uid
|
|
118
|
+
* recycling the design names as an anti-goal, and it is exactly what
|
|
119
|
+
* "delete, then recreate" walks into.
|
|
120
|
+
* 3. **The media directory**, which the server owns and no `userdel` knows of.
|
|
121
|
+
* 4. **The account and its home, last** — so a failure before it leaves an
|
|
122
|
+
* account whose registry row still says whose it was.
|
|
123
|
+
*
|
|
124
|
+
* It never throws. `ok: false` means the account may still be there: the
|
|
125
|
+
* caller keeps the registry row (marked `retiring`), which is what makes the
|
|
126
|
+
* retry possible and what stops a later admission from inheriting the files.
|
|
127
|
+
* An account that is already gone is a success once its home is gone too — the
|
|
128
|
+
* retry after a crash between the `userdel` and the row's removal must
|
|
129
|
+
* converge.
|
|
130
|
+
*/
|
|
131
|
+
export async function teardownConversationUser(user, deps) {
|
|
132
|
+
const warnings = [];
|
|
133
|
+
const why = (e) => (e instanceof Error ? e.message : String(e));
|
|
134
|
+
// Everything below kills by uid and deletes by uid, as root. The name comes
|
|
135
|
+
// from a file and the uid from `/etc/passwd`, so both are checked against
|
|
136
|
+
// what a conversation's account can possibly be before either is trusted:
|
|
137
|
+
// one wrong row must not be able to aim this at root, at the server, or at a
|
|
138
|
+
// group's account.
|
|
139
|
+
if (!CONVERSATION_USER_RE.test(user))
|
|
140
|
+
return { ok: false, warnings: [`"${user}" is not a conversation's account — refusing to tear it down`] };
|
|
141
|
+
let ok = true;
|
|
142
|
+
try {
|
|
143
|
+
await deps.removeMedia();
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
ok = false;
|
|
147
|
+
warnings.push(`removing the conversation's media failed: ${why(e)}`);
|
|
148
|
+
}
|
|
149
|
+
const uid = deps.uidOf(user);
|
|
150
|
+
if (uid == null) {
|
|
151
|
+
// No account does not mean no files. `/etc/passwd` is ephemeral in a
|
|
152
|
+
// container while the homes sit on a volume, so after a recreate the
|
|
153
|
+
// account is gone and its home is not — and the next `useradd
|
|
154
|
+
// --create-home` under this name would adopt it as it stands.
|
|
155
|
+
try {
|
|
156
|
+
await deps.removeHome(user);
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
ok = false;
|
|
160
|
+
warnings.push(`removing ${user}'s leftover home failed: ${why(e)}`);
|
|
161
|
+
}
|
|
162
|
+
return { ok, warnings };
|
|
163
|
+
}
|
|
164
|
+
if (uid === 0 || uid === deps.selfUid)
|
|
165
|
+
return { ok: false, warnings: [...warnings, `${user} resolves to uid ${uid}, which is not a conversation's — refusing to tear it down`] };
|
|
166
|
+
const wait = deps.wait ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
167
|
+
let gone = false;
|
|
168
|
+
for (let attempt = 1; attempt <= TEARDOWN_ATTEMPTS && !gone; attempt++) {
|
|
169
|
+
let left = 0;
|
|
170
|
+
try {
|
|
171
|
+
left = await deps.killProcesses(uid);
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
warnings.push(`killing ${user}'s processes failed: ${why(e)}`);
|
|
175
|
+
}
|
|
176
|
+
// A process that outlived the kill can still write: sweeping or deleting
|
|
177
|
+
// under it would only race it. Wait and try again.
|
|
178
|
+
if (left > 0) {
|
|
179
|
+
if (attempt < TEARDOWN_ATTEMPTS)
|
|
180
|
+
await wait(300 * attempt);
|
|
181
|
+
else
|
|
182
|
+
warnings.push(`${left} process(es) of ${user} survived the kill`);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
await deps.sweepStray(uid);
|
|
187
|
+
}
|
|
188
|
+
catch (e) {
|
|
189
|
+
// As blocking as a failed `userdel`, and before it on purpose: once the
|
|
190
|
+
// account is gone nothing names this uid any more, and a file left under
|
|
191
|
+
// it is precisely what the next account to receive the uid inherits.
|
|
192
|
+
if (attempt < TEARDOWN_ATTEMPTS)
|
|
193
|
+
await wait(300 * attempt);
|
|
194
|
+
else
|
|
195
|
+
warnings.push(`sweeping ${user}'s temp files failed: ${why(e)}`);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
await deps.userdel(user);
|
|
200
|
+
gone = true;
|
|
201
|
+
}
|
|
202
|
+
catch (e) {
|
|
203
|
+
if (attempt < TEARDOWN_ATTEMPTS)
|
|
204
|
+
await wait(300 * attempt);
|
|
205
|
+
else
|
|
206
|
+
warnings.push(`userdel --remove ${user} failed: ${why(e)}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return { ok: ok && gone, warnings };
|
|
210
|
+
}
|
|
59
211
|
/**
|
|
60
212
|
* A stored registry, cleaned. A malformed row is DROPPED rather than repaired:
|
|
61
213
|
* a row is a claim that an account belongs to one conversation, and a half-read
|
|
@@ -79,11 +231,16 @@ export function normalizeExternalUsers(raw) {
|
|
|
79
231
|
if (seen.has(user))
|
|
80
232
|
continue; // first wins: a duplicated name is unreadable
|
|
81
233
|
seen.add(user);
|
|
82
|
-
|
|
234
|
+
const kept = {
|
|
83
235
|
user,
|
|
84
236
|
unit,
|
|
85
237
|
createdAt: typeof row.createdAt === "number" && Number.isFinite(row.createdAt) ? row.createdAt : 0,
|
|
86
|
-
}
|
|
238
|
+
};
|
|
239
|
+
// ANY trace of the mark keeps it: a row that was being erased must never
|
|
240
|
+
// read as live because its timestamp was mangled.
|
|
241
|
+
if (row.retiring != null)
|
|
242
|
+
kept.retiring = typeof row.retiring === "number" && Number.isFinite(row.retiring) ? row.retiring : 0;
|
|
243
|
+
out.push(kept);
|
|
87
244
|
}
|
|
88
245
|
return out;
|
|
89
246
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"external-users.js","sourceRoot":"","sources":["../src/external-users.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,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1E,oDAAoD;QACpD,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,IAAgC;IAChE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACtD,EAAE,CAAC,aAAa,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IACvE,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,2CAA2C;AACxE,CAAC"}
|
package/dist/server.js
CHANGED
|
@@ -47,7 +47,7 @@ import { startWhatsApp } from "./whatsapp.js";
|
|
|
47
47
|
import { waGuestWorkDirName } from "./wa-guests.js";
|
|
48
48
|
import { GUEST_FULL_CODE, GUEST_RATE_WINDOW_MS, guestAdmitVerdict, guestConversationKey, guestLimitsOf, liveGuestKeys, requestedLimits, } from "./guest-admit.js";
|
|
49
49
|
import { conversationUnit, unitConversation, unitFor, unitGroup } from "./isolation-unit.js";
|
|
50
|
-
import { externalUserVerdict, externalUsersOfGroup, loadExternalUsers, saveExternalUsers, withExternalUser, withoutExternalUsers, } from "./external-users.js";
|
|
50
|
+
import { conversationHeld, externalUserRow, externalUserVerdict, externalUsersOfGroup, loadExternalUsers, saveExternalUsers, liveUidsOfProcStatus, teardownConversationUser, withExternalUser, withRetiring, withoutExternalUsers, } from "./external-users.js";
|
|
51
51
|
import { startWaBridge } from "./wa-bridge.js";
|
|
52
52
|
import { openInbox, waStateDir } from "./wa-inbox.js";
|
|
53
53
|
import { waSessionDir } from "./wa-transport.js";
|
|
@@ -3147,13 +3147,20 @@ app.get("/channels", (req, res) => {
|
|
|
3147
3147
|
const allCrons = loadCrons();
|
|
3148
3148
|
const allHooks = loadWebhooks();
|
|
3149
3149
|
const active = allCrons.filter((c) => c.enabled);
|
|
3150
|
+
const allGroups = loadGroups();
|
|
3151
|
+
const allChannels = loadChannels();
|
|
3150
3152
|
res.json(visible.map((ch) => {
|
|
3151
3153
|
const mine = active.filter((c) => c.sessionId === ch.sessionId);
|
|
3152
|
-
|
|
3154
|
+
// `conversation` — closing this agent erases a stranger's whole
|
|
3155
|
+
// conversation (its account and files), which is not recoverable either.
|
|
3156
|
+
const closes = {
|
|
3157
|
+
...triggerCount(ch.sessionId, allCrons, allHooks),
|
|
3158
|
+
...(conversationErasedWith(ch, allGroups, allChannels) ? { conversation: true } : {}),
|
|
3159
|
+
};
|
|
3153
3160
|
return {
|
|
3154
3161
|
...ch,
|
|
3155
3162
|
...(mine.length ? { crons: mine.map((c) => scheduleLabel(c.schedule, cronTimeZone(c))) } : {}),
|
|
3156
|
-
...(closes.schedules || closes.webhooks ? { closes } : {}),
|
|
3163
|
+
...(closes.schedules || closes.webhooks || closes.conversation ? { closes } : {}),
|
|
3157
3164
|
};
|
|
3158
3165
|
}));
|
|
3159
3166
|
});
|
|
@@ -4660,6 +4667,16 @@ async function ensureExternalUser(g, conversation) {
|
|
|
4660
4667
|
return { ok: false, why: r.why };
|
|
4661
4668
|
if (!r.user)
|
|
4662
4669
|
return { ok: false, why: `group ${g.id} resolves to no user of its own` };
|
|
4670
|
+
// A conversation whose agent was deleted is being ERASED, and the person may
|
|
4671
|
+
// write again before that has finished — or after it failed. Either way they
|
|
4672
|
+
// must not land in the files the operator asked to remove: finish the
|
|
4673
|
+
// teardown first, and refuse the admission if it cannot be finished.
|
|
4674
|
+
const retiring = loadExternalUsers().find((x) => x.user === r.user && x.retiring != null);
|
|
4675
|
+
if (retiring) {
|
|
4676
|
+
const done = await retireConversationUser(retiring);
|
|
4677
|
+
if (!done.ok)
|
|
4678
|
+
return { ok: false, why: `this conversation's previous files could not be erased yet (${done.warnings.join("; ") || "teardown incomplete"})` };
|
|
4679
|
+
}
|
|
4663
4680
|
const rows = loadExternalUsers();
|
|
4664
4681
|
const verdict = externalUserVerdict(rows, { user: r.user, unit: r.unit }, Date.now());
|
|
4665
4682
|
if (verdict.kind === "collision")
|
|
@@ -5452,22 +5469,180 @@ async function teardownGroupOrConversations(g) {
|
|
|
5452
5469
|
catch (e) {
|
|
5453
5470
|
return { ok: false, warning: `killing the group's agents failed: ${e instanceof Error ? e.message : String(e)}` };
|
|
5454
5471
|
}
|
|
5455
|
-
|
|
5472
|
+
// Closing each agent above already erased its own conversation
|
|
5473
|
+
// (`endChannel`); what is left in the registry is every conversation that had
|
|
5474
|
+
// no agent any more, plus any whose teardown failed. Same erasure, one by one
|
|
5475
|
+
// — re-read, since those closes rewrote the registry.
|
|
5476
|
+
const left = externalUsersOfGroup(loadExternalUsers(), g.id);
|
|
5477
|
+
let removed = mine.length - left.length;
|
|
5456
5478
|
const warnings = [];
|
|
5457
|
-
for (const row of
|
|
5479
|
+
for (const row of left) {
|
|
5480
|
+
const done = await retireConversationUser(row);
|
|
5481
|
+
if (done.ok)
|
|
5482
|
+
removed++;
|
|
5483
|
+
else
|
|
5484
|
+
warnings.push(...done.warnings);
|
|
5485
|
+
}
|
|
5486
|
+
if (mine.length)
|
|
5487
|
+
console.log(`groups: external ${g.id.slice(0, 8)} — removed ${removed}/${mine.length} conversation account(s)`);
|
|
5488
|
+
return warnings.length ? { ok: false, warning: warnings.join("; ") } : { ok: true };
|
|
5489
|
+
}
|
|
5490
|
+
/** Pids currently running as `uid` — any of the four ids on the `Uid:` line, so
|
|
5491
|
+
* a process that changed only its effective id is still found. Read from
|
|
5492
|
+
* `/proc` rather than through `pkill`: no binary to be missing from an image,
|
|
5493
|
+
* and the same read tells us afterwards whether the kill actually landed. */
|
|
5494
|
+
function pidsOfUid(uid) {
|
|
5495
|
+
const out = [];
|
|
5496
|
+
let names = [];
|
|
5497
|
+
try {
|
|
5498
|
+
names = fs.readdirSync("/proc");
|
|
5499
|
+
}
|
|
5500
|
+
catch {
|
|
5501
|
+
return out;
|
|
5502
|
+
}
|
|
5503
|
+
for (const name of names) {
|
|
5504
|
+
if (!/^\d+$/.test(name))
|
|
5505
|
+
continue;
|
|
5458
5506
|
try {
|
|
5459
|
-
|
|
5460
|
-
|
|
5507
|
+
if (liveUidsOfProcStatus(fs.readFileSync(`/proc/${name}/status`, "utf8")).includes(uid))
|
|
5508
|
+
out.push(Number(name));
|
|
5461
5509
|
}
|
|
5462
|
-
catch
|
|
5463
|
-
|
|
5510
|
+
catch {
|
|
5511
|
+
/* the process ended between the listing and the read */
|
|
5464
5512
|
}
|
|
5465
5513
|
}
|
|
5466
|
-
|
|
5467
|
-
|
|
5468
|
-
|
|
5469
|
-
|
|
5470
|
-
|
|
5514
|
+
return out;
|
|
5515
|
+
}
|
|
5516
|
+
/** SIGKILL everything running as `uid`, and say how many are still there. */
|
|
5517
|
+
async function killProcessesOfUid(uid) {
|
|
5518
|
+
const pids = pidsOfUid(uid);
|
|
5519
|
+
if (!pids.length)
|
|
5520
|
+
return 0;
|
|
5521
|
+
if (typeof process.getuid === "function" && process.getuid() === 0) {
|
|
5522
|
+
for (const pid of pids) {
|
|
5523
|
+
try {
|
|
5524
|
+
process.kill(pid, "SIGKILL");
|
|
5525
|
+
}
|
|
5526
|
+
catch {
|
|
5527
|
+
/* already gone */
|
|
5528
|
+
}
|
|
5529
|
+
}
|
|
5530
|
+
}
|
|
5531
|
+
else {
|
|
5532
|
+
// `kill` exits non-zero when ONE pid had already ended: not a failure, the
|
|
5533
|
+
// recount below is what answers.
|
|
5534
|
+
await runPrivileged("kill", ["-KILL", ...pids.map(String)]).catch(() => { });
|
|
5535
|
+
}
|
|
5536
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
5537
|
+
return pidsOfUid(uid).length;
|
|
5538
|
+
}
|
|
5539
|
+
/** Where an agent can leave files outside its home. `-xdev` keeps the sweep on
|
|
5540
|
+
* each of these filesystems; a root that does not exist is skipped, since
|
|
5541
|
+
* `find` would fail the whole run on it; and `-ignore_readdir_race` because
|
|
5542
|
+
* these directories are everyone's — a neighbour's file vanishing mid-walk is
|
|
5543
|
+
* not a failure of OUR sweep, and would otherwise read as one. */
|
|
5544
|
+
const STRAY_ROOTS = ["/tmp", "/var/tmp", "/dev/shm"];
|
|
5545
|
+
/** One teardown per account at a time: closing an agent and that person
|
|
5546
|
+
* writing again a moment later must not run two `userdel`s side by side. */
|
|
5547
|
+
const conversationTeardowns = new Map();
|
|
5548
|
+
/**
|
|
5549
|
+
* Erase one conversation of an external group: its processes, its temp files,
|
|
5550
|
+
* its media, its Unix account and home — then, and only then, its registry row.
|
|
5551
|
+
*
|
|
5552
|
+
* The row is MARKED before anything is destroyed and DROPPED only once the
|
|
5553
|
+
* account is gone. Between the two it says "being erased": the boot reconciler
|
|
5554
|
+
* finishes it rather than re-provisioning the account, and an admission waits
|
|
5555
|
+
* on it (`ensureExternalUser`). A row dropped while the account is still on
|
|
5556
|
+
* the host would let the next conversation mint the same name and inherit the
|
|
5557
|
+
* home — the trap `teardownGroupOrConversations` already documents.
|
|
5558
|
+
*/
|
|
5559
|
+
function retireConversationUser(row) {
|
|
5560
|
+
const running = conversationTeardowns.get(row.user);
|
|
5561
|
+
if (running)
|
|
5562
|
+
return running;
|
|
5563
|
+
const run = (async () => {
|
|
5564
|
+
saveExternalUsers(withRetiring(loadExternalUsers(), row.user, Date.now()));
|
|
5565
|
+
const done = await teardownConversationUser(row.user, {
|
|
5566
|
+
uidOf,
|
|
5567
|
+
selfUid: typeof process.getuid === "function" ? process.getuid() : 0,
|
|
5568
|
+
killProcesses: killProcessesOfUid,
|
|
5569
|
+
sweepStray: async (uid) => {
|
|
5570
|
+
const roots = STRAY_ROOTS.filter((d) => fs.existsSync(d));
|
|
5571
|
+
if (roots.length)
|
|
5572
|
+
await runPrivileged("find", [...roots, "-xdev", "-ignore_readdir_race", "-uid", String(uid), "-delete"]);
|
|
5573
|
+
},
|
|
5574
|
+
removeMedia: () => {
|
|
5575
|
+
// Server-owned, outside every home. `rmSync` unlinks a symlink rather
|
|
5576
|
+
// than following it, so a name swapped under us costs nothing.
|
|
5577
|
+
fs.rmSync(mediaDirForUnit(mediaDir(), row.unit, true), { recursive: true, force: true });
|
|
5578
|
+
},
|
|
5579
|
+
userdel: (user) => runPrivileged("userdel", ["--remove", user]),
|
|
5580
|
+
// `--one-file-system`: a home is a plain directory, and a mount an agent
|
|
5581
|
+
// managed to leave under it is not ours to empty.
|
|
5582
|
+
removeHome: (user) => runPrivileged("rm", ["-rf", "--one-file-system", "--", homeForUser(user, GROUP_HOME_BASE)], { timeoutMs: TREE_OP_TIMEOUT_MS }),
|
|
5583
|
+
});
|
|
5584
|
+
if (done.ok) {
|
|
5585
|
+
saveExternalUsers(withoutExternalUsers(loadExternalUsers(), [row.user]));
|
|
5586
|
+
console.log(`guests: erased ${row.user} (account, home, media) — its conversation was deleted`);
|
|
5587
|
+
}
|
|
5588
|
+
else {
|
|
5589
|
+
console.warn(`guests: could NOT fully erase ${row.user} — ${done.warnings.join("; ")}. It stays marked and is retried at the next start, and before that person is admitted again.`);
|
|
5590
|
+
}
|
|
5591
|
+
return done;
|
|
5592
|
+
})().finally(() => conversationTeardowns.delete(row.user));
|
|
5593
|
+
conversationTeardowns.set(row.user, run);
|
|
5594
|
+
return run;
|
|
5595
|
+
}
|
|
5596
|
+
/**
|
|
5597
|
+
* Deleting the LAST agent of an external conversation deletes the conversation:
|
|
5598
|
+
* the person who writes again starts from nothing, under a fresh account.
|
|
5599
|
+
*
|
|
5600
|
+
* This is the first half — the MARK, written synchronously as the channel
|
|
5601
|
+
* leaves the registry and long before the slow part of the close. It is what
|
|
5602
|
+
* closes the window in which that person writes again while their old agent is
|
|
5603
|
+
* still being stopped: the admission finds the mark and erases before it
|
|
5604
|
+
* provisions, instead of being handed the home we are about to delete under it.
|
|
5605
|
+
*
|
|
5606
|
+
* Returns the account to erase, or null for every other channel — an ordinary
|
|
5607
|
+
* group's agent, a guest of a non-external group (one shared account, nothing
|
|
5608
|
+
* of its own to remove), mono-tenant — so `endChannel` need not ask.
|
|
5609
|
+
*/
|
|
5610
|
+
function markConversationRetiring(ch) {
|
|
5611
|
+
const gone = conversationErasedWith(ch, loadGroups(), loadChannels());
|
|
5612
|
+
if (!gone)
|
|
5613
|
+
return null;
|
|
5614
|
+
const rows = loadExternalUsers();
|
|
5615
|
+
const row = externalUserRow(rows, gone.group, gone.conversation);
|
|
5616
|
+
if (!row)
|
|
5617
|
+
return null;
|
|
5618
|
+
saveExternalUsers(withRetiring(rows, row.user, Date.now()));
|
|
5619
|
+
return row.user;
|
|
5620
|
+
}
|
|
5621
|
+
/**
|
|
5622
|
+
* The conversation that closing this agent erases, or null — ONE answer for the
|
|
5623
|
+
* close itself and for the confirmation shown before it (`closes` on
|
|
5624
|
+
* `GET /channels`): a warning derived differently from the act it warns about
|
|
5625
|
+
* is a warning that drifts.
|
|
5626
|
+
*/
|
|
5627
|
+
function conversationErasedWith(ch, groups, chans) {
|
|
5628
|
+
if (DEPLOYMENT_MODE !== "multi-tenant" || !ch.group)
|
|
5629
|
+
return null;
|
|
5630
|
+
const conversation = guestConversationKey(ch);
|
|
5631
|
+
if (!conversation)
|
|
5632
|
+
return null;
|
|
5633
|
+
if (groups.find((x) => x.id === ch.group)?.nature !== "external")
|
|
5634
|
+
return null;
|
|
5635
|
+
if (conversationHeld(chans, { sessionId: ch.sessionId, group: ch.group, conversation }, guestConversationKey))
|
|
5636
|
+
return null;
|
|
5637
|
+
return { group: ch.group, conversation };
|
|
5638
|
+
}
|
|
5639
|
+
/** The second half: erase the account `markConversationRetiring` named — only
|
|
5640
|
+
* if it is STILL marked. An admission that arrived in between has already
|
|
5641
|
+
* erased it and minted a fresh one under the same name, and that one is live. */
|
|
5642
|
+
async function finishRetiring(user) {
|
|
5643
|
+
const row = loadExternalUsers().find((r) => r.user === user && r.retiring != null);
|
|
5644
|
+
if (row)
|
|
5645
|
+
await retireConversationUser(row);
|
|
5471
5646
|
}
|
|
5472
5647
|
/**
|
|
5473
5648
|
* The same boot reconciler, for the accounts an EXTERNAL group's conversations
|
|
@@ -5493,17 +5668,23 @@ async function reconcileExternalUsers() {
|
|
|
5493
5668
|
if (!rows.length)
|
|
5494
5669
|
return;
|
|
5495
5670
|
const groups = loadGroups();
|
|
5496
|
-
const live = [];
|
|
5497
5671
|
const stale = [];
|
|
5498
5672
|
let healed = 0;
|
|
5499
5673
|
let failed = 0;
|
|
5500
5674
|
for (const row of rows) {
|
|
5675
|
+
// An erasure that did not finish is FINISHED, never undone — before the
|
|
5676
|
+
// group is even looked at: re-provisioning would recreate the account over
|
|
5677
|
+
// the files it was meant to take away, and dropping the row of a group
|
|
5678
|
+
// that is gone would forget an account that may still be on the host.
|
|
5679
|
+
if (row.retiring != null) {
|
|
5680
|
+
await retireConversationUser(row);
|
|
5681
|
+
continue;
|
|
5682
|
+
}
|
|
5501
5683
|
const g = groups.find((x) => x.id === unitGroup(row.unit));
|
|
5502
5684
|
if (!g || g.nature !== "external") {
|
|
5503
5685
|
stale.push(row.user);
|
|
5504
5686
|
continue;
|
|
5505
5687
|
}
|
|
5506
|
-
live.push(row);
|
|
5507
5688
|
const conv = unitConversation(row.unit);
|
|
5508
5689
|
if (!conv)
|
|
5509
5690
|
continue;
|
|
@@ -5526,7 +5707,8 @@ async function reconcileExternalUsers() {
|
|
|
5526
5707
|
}
|
|
5527
5708
|
}
|
|
5528
5709
|
if (stale.length) {
|
|
5529
|
-
|
|
5710
|
+
// Re-read: an erasure finished above has already rewritten the registry.
|
|
5711
|
+
saveExternalUsers(withoutExternalUsers(loadExternalUsers(), stale));
|
|
5530
5712
|
console.log(`guests: dropped ${stale.length} registry row(s) whose group is gone`);
|
|
5531
5713
|
}
|
|
5532
5714
|
if (healed || failed)
|
|
@@ -10866,18 +11048,16 @@ async function endChannel(sessionId) {
|
|
|
10866
11048
|
// itself (`onChannelClosed`).
|
|
10867
11049
|
if (ch)
|
|
10868
11050
|
forgetChannelTriggers(sessionId);
|
|
10869
|
-
// THE VAULT SLICE DIES WITH THE AGENT (design decision 7)
|
|
10870
|
-
//
|
|
10871
|
-
//
|
|
10872
|
-
//
|
|
10873
|
-
// hand its files to whoever gets that uid next — while the credentials they
|
|
10874
|
-
// handed over do not. Minimum retention, no orphaned stranger credentials.
|
|
10875
|
-
// Tearing the USER down is its own gesture: deleting the conversation.
|
|
11051
|
+
// THE VAULT SLICE DIES WITH THE AGENT (design decision 7), first and on its
|
|
11052
|
+
// own: credentials a stranger handed over must not wait on the slower erasure
|
|
11053
|
+
// below, nor survive it failing. The conversation's Unix USER, HOME and media
|
|
11054
|
+
// follow: marked here, erased once the process is stopped (further down).
|
|
10876
11055
|
//
|
|
10877
11056
|
// Same guard as every other per-conversation store: only a slice we can NAME,
|
|
10878
11057
|
// never the group's as a consolation (`vaultUnitOfChannel` refuses instead).
|
|
10879
11058
|
if (ch)
|
|
10880
11059
|
dropGuestSliceOf(ch);
|
|
11060
|
+
const retiring = ch ? markConversationRetiring(ch) : null;
|
|
10881
11061
|
if (ch?.telegram?.threadId)
|
|
10882
11062
|
closeTelegramTopic(ch.telegram.chatId, ch.telegram.threadId);
|
|
10883
11063
|
const s = sessions.get(sessionId);
|
|
@@ -10902,6 +11082,12 @@ async function endChannel(sessionId) {
|
|
|
10902
11082
|
if (outcome !== "none")
|
|
10903
11083
|
console.log(`worktree ${worktreeBranch}: ${outcome} on session end`);
|
|
10904
11084
|
}
|
|
11085
|
+
// DELETING AN EXTERNAL CONVERSATION'S AGENT ERASES THE CONVERSATION (design
|
|
11086
|
+
// decision 7, as the operator settled it): account, home, media, temp files.
|
|
11087
|
+
// Last, because it needs the agent's process gone — and never fatal to the
|
|
11088
|
+
// close: a failure is logged, kept on the registry row and retried.
|
|
11089
|
+
if (retiring)
|
|
11090
|
+
await finishRetiring(retiring).catch((e) => console.warn(`guests: erasing a deleted conversation failed: ${e instanceof Error ? e.message : String(e)}`));
|
|
10905
11091
|
return true;
|
|
10906
11092
|
}
|
|
10907
11093
|
function detach(ws, s) {
|