shipmates 0.2.0 → 0.4.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/dist/shipmates.d.ts +79 -4
- package/dist/shipmates.d.ts.map +1 -1
- package/dist/shipmates.js +179 -35
- package/dist/shipmates.js.map +1 -1
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +15 -0
- package/dist/store.js.map +1 -1
- package/dist/types.d.ts +28 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/shipmates.ts +277 -38
- package/src/store.ts +15 -0
- package/src/types.ts +30 -0
package/src/shipmates.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type Presence,
|
|
14
14
|
type Question,
|
|
15
15
|
type Relay,
|
|
16
|
+
type SessionSetting,
|
|
16
17
|
type SessionState,
|
|
17
18
|
} from './types.js';
|
|
18
19
|
import {newId} from './ids.js';
|
|
@@ -37,7 +38,14 @@ export interface PresencePatch {
|
|
|
37
38
|
waitingOn?: string | null;
|
|
38
39
|
branch?: string | null;
|
|
39
40
|
cwd?: string | null;
|
|
41
|
+
/** Kept per transcript file, across unregister and register (D40). */
|
|
40
42
|
wake?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* The session declared `waiting-for-human` (ADR 0006) and has not run since.
|
|
45
|
+
* Kept per transcript file like `wake`; `register` restores the state from it
|
|
46
|
+
* (D40). It does not set `state`: pass both.
|
|
47
|
+
*/
|
|
48
|
+
declaredWaiting?: boolean;
|
|
41
49
|
}
|
|
42
50
|
|
|
43
51
|
export interface ListFilter {
|
|
@@ -56,7 +64,9 @@ type PresenceRow = {
|
|
|
56
64
|
status: string | null;
|
|
57
65
|
refs: string | null;
|
|
58
66
|
waiting_on: string | null;
|
|
59
|
-
|
|
67
|
+
/** From session_setting (D40), not presence's own unused column. */
|
|
68
|
+
s_wake: number;
|
|
69
|
+
s_declared_waiting: number;
|
|
60
70
|
pid: number;
|
|
61
71
|
start_time: string | null;
|
|
62
72
|
boot_id: string | null;
|
|
@@ -65,6 +75,24 @@ type PresenceRow = {
|
|
|
65
75
|
settled_at: number | null;
|
|
66
76
|
};
|
|
67
77
|
|
|
78
|
+
type SettingRow = {
|
|
79
|
+
session_file: string;
|
|
80
|
+
wake: number;
|
|
81
|
+
declared_waiting: number;
|
|
82
|
+
updated_at: number;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const toSetting = (r: SettingRow): SessionSetting => ({
|
|
86
|
+
sessionFile: r.session_file,
|
|
87
|
+
wake: r.wake === 1,
|
|
88
|
+
declaredWaiting: r.declared_waiting === 1,
|
|
89
|
+
updatedAt: r.updated_at,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
/** Presence with the session's own settings (D40); a session with no row has the defaults. */
|
|
93
|
+
const PRESENCE_SELECT = `SELECT p.*, COALESCE(s.wake, 1) AS s_wake, COALESCE(s.declared_waiting, 0) AS s_declared_waiting
|
|
94
|
+
FROM presence p LEFT JOIN session_setting s ON s.session_file = p.session_file`;
|
|
95
|
+
|
|
68
96
|
/** D14: message text is short; content stays in git and messages point at it. */
|
|
69
97
|
const DAY = 86_400_000;
|
|
70
98
|
|
|
@@ -206,6 +234,7 @@ type ItemRow = {
|
|
|
206
234
|
answer: string | null;
|
|
207
235
|
answer_channel: string | null;
|
|
208
236
|
created_at: number;
|
|
237
|
+
finished_at: number | null;
|
|
209
238
|
};
|
|
210
239
|
|
|
211
240
|
function toItem(r: ItemRow): AttentionItem {
|
|
@@ -220,6 +249,7 @@ function toItem(r: ItemRow): AttentionItem {
|
|
|
220
249
|
answer: r.answer,
|
|
221
250
|
answerChannel: r.answer_channel,
|
|
222
251
|
createdAt: r.created_at,
|
|
252
|
+
finishedAt: r.finished_at,
|
|
223
253
|
};
|
|
224
254
|
}
|
|
225
255
|
|
|
@@ -296,22 +326,32 @@ export class Shipmates {
|
|
|
296
326
|
const me = this.probe.self();
|
|
297
327
|
const t = this.now();
|
|
298
328
|
this.tx(() => {
|
|
329
|
+
// A session that declared it waits for the human still does after an
|
|
330
|
+
// unload or a resume: it has not run since (D40).
|
|
331
|
+
const declared = this.db
|
|
332
|
+
.prepare(
|
|
333
|
+
`SELECT declared_waiting FROM session_setting WHERE session_file = ?`,
|
|
334
|
+
)
|
|
335
|
+
.get(input.sessionFile) as {declared_waiting: number} | undefined;
|
|
336
|
+
const state: SessionState =
|
|
337
|
+
declared?.declared_waiting === 1 ? 'waiting-for-human' : 'idle';
|
|
299
338
|
this.db
|
|
300
339
|
.prepare(
|
|
301
340
|
`INSERT INTO presence(session_file, session_id, cwd, branch, state, pid, start_time, boot_id, host, registered_at, updated_at)
|
|
302
|
-
VALUES(?, ?, ?, ?,
|
|
341
|
+
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
303
342
|
ON CONFLICT(session_file) DO UPDATE SET
|
|
304
343
|
session_id = excluded.session_id, cwd = excluded.cwd,
|
|
305
344
|
branch = COALESCE(excluded.branch, presence.branch),
|
|
306
345
|
pid = excluded.pid, start_time = excluded.start_time, boot_id = excluded.boot_id,
|
|
307
346
|
host = excluded.host, updated_at = excluded.updated_at,
|
|
308
|
-
state = CASE WHEN presence.pid = excluded.pid THEN presence.state ELSE
|
|
347
|
+
state = CASE WHEN presence.pid = excluded.pid THEN presence.state ELSE excluded.state END`,
|
|
309
348
|
)
|
|
310
349
|
.run(
|
|
311
350
|
input.sessionFile,
|
|
312
351
|
input.sessionId ?? null,
|
|
313
352
|
input.cwd ?? null,
|
|
314
353
|
input.branch ?? null,
|
|
354
|
+
state,
|
|
315
355
|
me.pid,
|
|
316
356
|
me.startTime,
|
|
317
357
|
me.bootId,
|
|
@@ -340,17 +380,53 @@ export class Shipmates {
|
|
|
340
380
|
if (patch.waitingOn !== undefined) put('waiting_on', patch.waitingOn);
|
|
341
381
|
if (patch.branch !== undefined) put('branch', patch.branch);
|
|
342
382
|
if (patch.cwd !== undefined) put('cwd', patch.cwd);
|
|
343
|
-
if (patch.wake !== undefined) put('wake', patch.wake ? 1 : 0);
|
|
344
383
|
put('updated_at', this.now());
|
|
384
|
+
const setting =
|
|
385
|
+
patch.wake !== undefined || patch.declaredWaiting !== undefined;
|
|
345
386
|
this.tx(() => {
|
|
346
387
|
this.db
|
|
347
388
|
.prepare(
|
|
348
389
|
`UPDATE presence SET ${sets.join(', ')} WHERE session_file = ?`,
|
|
349
390
|
)
|
|
350
391
|
.run(...vals, sessionFile);
|
|
392
|
+
if (setting) this.writeSetting(sessionFile, patch);
|
|
351
393
|
});
|
|
352
394
|
}
|
|
353
395
|
|
|
396
|
+
/**
|
|
397
|
+
* D40: the session's own choices, by transcript file. A row exists only while
|
|
398
|
+
* one differs from the default, so the table holds the few sessions that set
|
|
399
|
+
* something. Called inside a write.
|
|
400
|
+
*/
|
|
401
|
+
private writeSetting(
|
|
402
|
+
sessionFile: string,
|
|
403
|
+
patch: {wake?: boolean; declaredWaiting?: boolean},
|
|
404
|
+
): void {
|
|
405
|
+
const bit = (v: boolean | undefined) =>
|
|
406
|
+
v === undefined ? null : v ? 1 : 0;
|
|
407
|
+
this.db
|
|
408
|
+
.prepare(
|
|
409
|
+
`INSERT INTO session_setting(session_file, wake, declared_waiting, updated_at)
|
|
410
|
+
VALUES(?, COALESCE(?, 1), COALESCE(?, 0), ?)
|
|
411
|
+
ON CONFLICT(session_file) DO UPDATE SET
|
|
412
|
+
wake = COALESCE(?, wake), declared_waiting = COALESCE(?, declared_waiting),
|
|
413
|
+
updated_at = excluded.updated_at`,
|
|
414
|
+
)
|
|
415
|
+
.run(
|
|
416
|
+
sessionFile,
|
|
417
|
+
bit(patch.wake),
|
|
418
|
+
bit(patch.declaredWaiting),
|
|
419
|
+
this.now(),
|
|
420
|
+
bit(patch.wake),
|
|
421
|
+
bit(patch.declaredWaiting),
|
|
422
|
+
);
|
|
423
|
+
this.db
|
|
424
|
+
.prepare(
|
|
425
|
+
`DELETE FROM session_setting WHERE session_file = ? AND wake = 1 AND declared_waiting = 0`,
|
|
426
|
+
)
|
|
427
|
+
.run(sessionFile);
|
|
428
|
+
}
|
|
429
|
+
|
|
354
430
|
unregister(sessionFile: string): void {
|
|
355
431
|
const me = this.probe.self();
|
|
356
432
|
this.tx(() => {
|
|
@@ -362,9 +438,34 @@ export class Shipmates {
|
|
|
362
438
|
});
|
|
363
439
|
}
|
|
364
440
|
|
|
441
|
+
/**
|
|
442
|
+
* D40: every session whose own settings differ from the defaults, live or
|
|
443
|
+
* not, most recently changed first. A host uses it to show what a session
|
|
444
|
+
* that is not loaded chose (wherever's sidebar keeps "waiting for the human"
|
|
445
|
+
* for an unloaded session that declared it, which `list()` cannot show).
|
|
446
|
+
* Rows exist only for non-defaults, so this is the few that set something.
|
|
447
|
+
*/
|
|
448
|
+
settings(): SessionSetting[] {
|
|
449
|
+
return (
|
|
450
|
+
this.cached(
|
|
451
|
+
`SELECT * FROM session_setting ORDER BY updated_at DESC, session_file`,
|
|
452
|
+
).all() as SettingRow[]
|
|
453
|
+
).map(toSetting);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/** D40: one session's settings; the defaults (`updatedAt` null) when it set none. */
|
|
457
|
+
setting(sessionFile: string): SessionSetting {
|
|
458
|
+
const row = this.cached(
|
|
459
|
+
`SELECT * FROM session_setting WHERE session_file = ?`,
|
|
460
|
+
).get(sessionFile) as SettingRow | undefined;
|
|
461
|
+
return row
|
|
462
|
+
? toSetting(row)
|
|
463
|
+
: {sessionFile, wake: true, declaredWaiting: false, updatedAt: null};
|
|
464
|
+
}
|
|
465
|
+
|
|
365
466
|
presence(sessionFile: string): Presence | null {
|
|
366
467
|
const row = this.db
|
|
367
|
-
.prepare(
|
|
468
|
+
.prepare(`${PRESENCE_SELECT} WHERE p.session_file = ?`)
|
|
368
469
|
.get(sessionFile) as PresenceRow | undefined;
|
|
369
470
|
return row ? this.toPresence(row) : null;
|
|
370
471
|
}
|
|
@@ -375,7 +476,7 @@ export class Shipmates {
|
|
|
375
476
|
|
|
376
477
|
list(filter: ListFilter = {}): Presence[] {
|
|
377
478
|
const rows = this.db
|
|
378
|
-
.prepare(
|
|
479
|
+
.prepare(`${PRESENCE_SELECT} ORDER BY p.updated_at DESC`)
|
|
379
480
|
.all() as PresenceRow[];
|
|
380
481
|
return rows
|
|
381
482
|
.map((r) => this.toPresence(r))
|
|
@@ -881,45 +982,139 @@ export class Shipmates {
|
|
|
881
982
|
|
|
882
983
|
/**
|
|
883
984
|
* The human answered an item. Callers must only call this for a real human act
|
|
884
|
-
* (a slash command, the CLI at a terminal, a host's authenticated UI
|
|
985
|
+
* (a slash command, the CLI at a terminal, a host's authenticated UI, a human
|
|
986
|
+
* turn in the asking session): ADR 0003.
|
|
987
|
+
*
|
|
988
|
+
* The answer goes to the asking session as a message of origin `human`, unless
|
|
989
|
+
* `deliver` is false: the human said it in that session's own turn, so their
|
|
990
|
+
* words are already in its transcript and a second copy would repeat them (D39).
|
|
885
991
|
*/
|
|
886
|
-
answerItem(
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
992
|
+
answerItem(
|
|
993
|
+
itemId: string,
|
|
994
|
+
text: string,
|
|
995
|
+
opts: {channel: string; deliver?: true},
|
|
996
|
+
): Message;
|
|
997
|
+
answerItem(
|
|
998
|
+
itemId: string,
|
|
999
|
+
text: string,
|
|
1000
|
+
opts: {channel: string; deliver: boolean},
|
|
1001
|
+
): Message | null;
|
|
1002
|
+
answerItem(
|
|
1003
|
+
itemId: string,
|
|
1004
|
+
text: string,
|
|
1005
|
+
opts: {channel: string; deliver?: boolean},
|
|
1006
|
+
): Message | null {
|
|
1007
|
+
// Checked inside the write: the session's input handler and another
|
|
1008
|
+
// channel (the CLI, a host) may answer the same item at the same moment,
|
|
1009
|
+
// and only one of them may win (D39).
|
|
1010
|
+
const id = this.tx(() => {
|
|
1011
|
+
const item = this.item(itemId);
|
|
1012
|
+
if (!item) throw new ShipmatesError(`no item ${itemId}`, 'not-found');
|
|
1013
|
+
if (item.state !== 'open')
|
|
1014
|
+
throw new ShipmatesError(
|
|
1015
|
+
`item ${itemId} is already ${item.state}`,
|
|
1016
|
+
item.state,
|
|
1017
|
+
);
|
|
1018
|
+
return this.closeItem(item, text, opts);
|
|
1019
|
+
});
|
|
1020
|
+
return id ? this.message(id)! : null;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* The human replied in the session that raised these items (D39): every item
|
|
1025
|
+
* of `sessionFile` still open, except `except`, is answered with their words
|
|
1026
|
+
* on `opts.channel` (by convention `session`). Returns the items it closed.
|
|
1027
|
+
*
|
|
1028
|
+
* With `notice`, the session also gets one `notice` message (origin agent,
|
|
1029
|
+
* from shipmates) naming the items the reply closed: the human's words are in
|
|
1030
|
+
* its turn, but nothing else would tell the agent which questions they
|
|
1031
|
+
* closed, and it may still be waiting on one of them.
|
|
1032
|
+
*/
|
|
1033
|
+
answerOpenItemsOf(
|
|
1034
|
+
sessionFile: string,
|
|
1035
|
+
text: string,
|
|
1036
|
+
opts: {
|
|
1037
|
+
channel: string;
|
|
1038
|
+
deliver?: boolean;
|
|
1039
|
+
except?: string[];
|
|
1040
|
+
notice?: boolean;
|
|
1041
|
+
},
|
|
1042
|
+
): AttentionItem[] {
|
|
1043
|
+
const skip = new Set(opts.except ?? []);
|
|
1044
|
+
const open = () =>
|
|
1045
|
+
(
|
|
1046
|
+
this.db
|
|
1047
|
+
.prepare(
|
|
1048
|
+
`SELECT * FROM item WHERE session_file = ? AND state = 'open' ORDER BY created_at`,
|
|
1049
|
+
)
|
|
1050
|
+
.all(sessionFile) as ItemRow[]
|
|
1051
|
+
).filter((r) => !skip.has(r.id));
|
|
1052
|
+
// Read first: the write lock only when there is something to close
|
|
1053
|
+
// (every human input calls this; most sessions have no open item).
|
|
1054
|
+
if (!open().length) return [];
|
|
1055
|
+
return this.tx(() => {
|
|
1056
|
+
const rows = open();
|
|
1057
|
+
for (const r of rows) this.closeItem(toItem(r), text, opts);
|
|
1058
|
+
if (opts.notice && rows.length)
|
|
1059
|
+
this.insertMessage({
|
|
1060
|
+
from: null,
|
|
1061
|
+
fromLabel: 'shipmates',
|
|
1062
|
+
to: {kind: 'session', target: sessionFile},
|
|
1063
|
+
kind: 'notice',
|
|
1064
|
+
text: `The human's reply in this session closed ${rows.length === 1 ? 'your inbox item' : `${rows.length} of your inbox items`} (answered with their words, channel ${opts.channel}): ${rows.map((r) => `${r.id} "${r.question.slice(0, 120)}"`).join('; ')}. If it did not answer one of them, ask again.`,
|
|
1065
|
+
origin: 'agent',
|
|
1066
|
+
});
|
|
1067
|
+
return rows.map((r) => r.id);
|
|
1068
|
+
}).map((id) => this.item(id)!);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
/** Inside a write: mark an open item answered, and send the answer unless told not to. */
|
|
1072
|
+
private closeItem(
|
|
1073
|
+
item: AttentionItem,
|
|
1074
|
+
text: string,
|
|
1075
|
+
opts: {channel: string; deliver?: boolean},
|
|
1076
|
+
): string | null {
|
|
894
1077
|
const t = this.now();
|
|
1078
|
+
const r = this.db
|
|
1079
|
+
.prepare(
|
|
1080
|
+
`UPDATE item SET state = 'answered', answer = ?, answer_channel = ?, finished_at = ? WHERE id = ? AND state = 'open'`,
|
|
1081
|
+
)
|
|
1082
|
+
.run(text, opts.channel, t, item.id);
|
|
1083
|
+
if (Number(r.changes) !== 1)
|
|
1084
|
+
throw new ShipmatesError(`item ${item.id} is no longer open`, 'answered');
|
|
1085
|
+
if (opts.deliver === false) return null;
|
|
895
1086
|
const relay: Relay = {
|
|
896
1087
|
text,
|
|
897
1088
|
channel: opts.channel,
|
|
898
1089
|
session: null,
|
|
899
|
-
itemId,
|
|
1090
|
+
itemId: item.id,
|
|
900
1091
|
at: t,
|
|
901
1092
|
};
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
to: {kind: 'session', target: item.session},
|
|
912
|
-
kind: 'answer',
|
|
913
|
-
text,
|
|
914
|
-
origin: 'human',
|
|
915
|
-
relay,
|
|
916
|
-
itemId,
|
|
917
|
-
});
|
|
1093
|
+
return this.insertMessage({
|
|
1094
|
+
from: null,
|
|
1095
|
+
fromLabel: 'human',
|
|
1096
|
+
to: {kind: 'session', target: item.session},
|
|
1097
|
+
kind: 'answer',
|
|
1098
|
+
text,
|
|
1099
|
+
origin: 'human',
|
|
1100
|
+
relay,
|
|
1101
|
+
itemId: item.id,
|
|
918
1102
|
});
|
|
919
|
-
return this.message(id)!;
|
|
920
1103
|
}
|
|
921
1104
|
|
|
922
|
-
|
|
1105
|
+
/**
|
|
1106
|
+
* Withdraw an item this session raised and that is still open. Refused for
|
|
1107
|
+
* another session's item: only the asker knows the question no longer stands.
|
|
1108
|
+
* Returns the item as it now is (unchanged when it was already answered).
|
|
1109
|
+
*/
|
|
1110
|
+
withdrawItem(from: string, itemId: string): AttentionItem {
|
|
1111
|
+
const item = this.item(itemId);
|
|
1112
|
+
if (!item) throw new ShipmatesError(`no item ${itemId}`, 'not-found');
|
|
1113
|
+
if (item.session !== from)
|
|
1114
|
+
throw new ShipmatesError(
|
|
1115
|
+
`item ${itemId} was raised by ${this.labelOf(item.session)}, not by you`,
|
|
1116
|
+
'not-yours',
|
|
1117
|
+
);
|
|
923
1118
|
this.tx(() => {
|
|
924
1119
|
this.db
|
|
925
1120
|
.prepare(
|
|
@@ -927,6 +1122,21 @@ export class Shipmates {
|
|
|
927
1122
|
)
|
|
928
1123
|
.run(this.now(), itemId, from);
|
|
929
1124
|
});
|
|
1125
|
+
return this.item(itemId)!;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/**
|
|
1129
|
+
* Items that closed (answered or withdrawn) at or after `since`, oldest
|
|
1130
|
+
* first: what a host reads to update its own copy of an item it pushed to
|
|
1131
|
+
* the human (a Telegram message that must stop looking open, D39).
|
|
1132
|
+
*/
|
|
1133
|
+
itemsFinishedSince(since: number): AttentionItem[] {
|
|
1134
|
+
const rows = this.db
|
|
1135
|
+
.prepare(
|
|
1136
|
+
`SELECT * FROM item WHERE state != 'open' AND finished_at >= ? ORDER BY finished_at, rowid`,
|
|
1137
|
+
)
|
|
1138
|
+
.all(since) as ItemRow[];
|
|
1139
|
+
return rows.map(toItem);
|
|
930
1140
|
}
|
|
931
1141
|
|
|
932
1142
|
/** The human's answers to this session's items, newest first. */
|
|
@@ -1343,21 +1553,32 @@ export class Shipmates {
|
|
|
1343
1553
|
* arrived after their last stop, and are not archived. One cached query,
|
|
1344
1554
|
* since every session's poller asks on every change. Measured: 3 µs for a
|
|
1345
1555
|
* session that owns nothing, 42 µs with 5 children and one to wake, against
|
|
1346
|
-
* about 500 µs as separate reads per child
|
|
1556
|
+
* about 500 µs as separate reads per child; with the settings join (D40),
|
|
1557
|
+
* 1.2 µs and 17 µs on a later run of the same case. Liveness is left to the caller:
|
|
1347
1558
|
* it is a process probe, not SQL.
|
|
1559
|
+
*
|
|
1560
|
+
* The child's own settings apply, as `planDelivery` would apply them were it
|
|
1561
|
+
* up (D40): never with wake off, and while it declared waiting-for-human only
|
|
1562
|
+
* for a message of origin `human` (D37). Without that, a child whose
|
|
1563
|
+
* human_ask timed out was restarted by its owner's next message, which that
|
|
1564
|
+
* run then delivered before the human answered.
|
|
1348
1565
|
*/
|
|
1349
1566
|
wakeCandidates(owner: string): Ownership[] {
|
|
1350
1567
|
const rows = this.cached(
|
|
1351
1568
|
`SELECT o.* FROM ownership o
|
|
1352
1569
|
LEFT JOIN (SELECT session_file, max(at) AS at FROM stop_request GROUP BY session_file) s
|
|
1353
1570
|
ON s.session_file = o.session_file
|
|
1571
|
+
LEFT JOIN session_setting st ON st.session_file = o.session_file
|
|
1354
1572
|
WHERE o.owner = ? AND o.via = 'headless' AND o.session_file IS NOT NULL
|
|
1573
|
+
AND COALESCE(st.wake, 1) = 1
|
|
1355
1574
|
AND NOT EXISTS (SELECT 1 FROM archive a WHERE a.session_file = o.session_file)
|
|
1356
1575
|
AND (EXISTS (SELECT 1 FROM message m WHERE m.state = 'pending' AND m.to_kind = 'session'
|
|
1357
|
-
AND m.to_target = o.session_file AND m.created_at > COALESCE(s.at, -1)
|
|
1576
|
+
AND m.to_target = o.session_file AND m.created_at > COALESCE(s.at, -1)
|
|
1577
|
+
AND (COALESCE(st.declared_waiting, 0) = 0 OR m.origin = 'human'))
|
|
1358
1578
|
OR EXISTS (SELECT 1 FROM role r JOIN message m ON m.state = 'pending' AND m.to_kind = 'role'
|
|
1359
1579
|
AND m.to_target = r.role
|
|
1360
|
-
WHERE r.session_file = o.session_file AND m.created_at > COALESCE(s.at, -1)
|
|
1580
|
+
WHERE r.session_file = o.session_file AND m.created_at > COALESCE(s.at, -1)
|
|
1581
|
+
AND (COALESCE(st.declared_waiting, 0) = 0 OR m.origin = 'human')))
|
|
1361
1582
|
ORDER BY o.created_at`,
|
|
1362
1583
|
).all(owner) as OwnershipRow[];
|
|
1363
1584
|
return rows.map((r) => this.toOwnership(r));
|
|
@@ -1530,6 +1751,7 @@ export class Shipmates {
|
|
|
1530
1751
|
)
|
|
1531
1752
|
.run(finished);
|
|
1532
1753
|
this.pruneOwnership(t);
|
|
1754
|
+
this.pruneSettings(t);
|
|
1533
1755
|
for (const row of dead)
|
|
1534
1756
|
if (!this.probe.isAlive(this.identityOf(row)))
|
|
1535
1757
|
this.db
|
|
@@ -1577,6 +1799,22 @@ export class Shipmates {
|
|
|
1577
1799
|
.run(session_file);
|
|
1578
1800
|
}
|
|
1579
1801
|
|
|
1802
|
+
/**
|
|
1803
|
+
* D40: a session's settings are kept while its transcript exists, as D25 keeps
|
|
1804
|
+
* ownership: they go when the file is gone, nobody runs it, and they were last
|
|
1805
|
+
* changed over 30 days ago. Called inside prune's transaction.
|
|
1806
|
+
*/
|
|
1807
|
+
private pruneSettings(t: number): void {
|
|
1808
|
+
const old = this.db
|
|
1809
|
+
.prepare(`SELECT session_file FROM session_setting WHERE updated_at < ?`)
|
|
1810
|
+
.all(t - 30 * DAY) as {session_file: string}[];
|
|
1811
|
+
for (const {session_file} of old)
|
|
1812
|
+
if (!fs.existsSync(session_file) && !this.isLive(session_file))
|
|
1813
|
+
this.db
|
|
1814
|
+
.prepare(`DELETE FROM session_setting WHERE session_file = ?`)
|
|
1815
|
+
.run(session_file);
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1580
1818
|
/** Prune when the last prune is a day old. Cheap enough to call on every open. */
|
|
1581
1819
|
pruneIfDue(): boolean {
|
|
1582
1820
|
const row = this.db
|
|
@@ -1620,7 +1858,8 @@ export class Shipmates {
|
|
|
1620
1858
|
status: r.status,
|
|
1621
1859
|
refs: r.refs ? (JSON.parse(r.refs) as string[]) : [],
|
|
1622
1860
|
waitingOn: r.waiting_on,
|
|
1623
|
-
wake: r.
|
|
1861
|
+
wake: r.s_wake === 1,
|
|
1862
|
+
declaredWaiting: r.s_declared_waiting === 1,
|
|
1624
1863
|
live: this.probe.isAlive(this.identityOf(r)),
|
|
1625
1864
|
pid: r.pid,
|
|
1626
1865
|
updatedAt: r.updated_at,
|
package/src/store.ts
CHANGED
|
@@ -163,6 +163,21 @@ CREATE TABLE stop_request(
|
|
|
163
163
|
outcome TEXT -- 'stopped' | 'not-running'
|
|
164
164
|
);
|
|
165
165
|
CREATE INDEX stop_open ON stop_request(session_file, handled_at);
|
|
166
|
+
`,
|
|
167
|
+
// 3: what a session chose for itself, kept per transcript file (D40).
|
|
168
|
+
`
|
|
169
|
+
-- presence.wake is no longer read: a presence row is deleted when its process
|
|
170
|
+
-- unregisters (a host unloading an idle session, a quit before a resume), and
|
|
171
|
+
-- the choice went with it. A row exists only while a setting differs from the
|
|
172
|
+
-- default; it is pruned like ownership, once the transcript is gone (D40).
|
|
173
|
+
CREATE TABLE session_setting(
|
|
174
|
+
session_file TEXT PRIMARY KEY,
|
|
175
|
+
wake INTEGER NOT NULL DEFAULT 1, -- /shipmates wake off
|
|
176
|
+
declared_waiting INTEGER NOT NULL DEFAULT 0, -- declared waiting-for-human, until its next run
|
|
177
|
+
updated_at INTEGER NOT NULL
|
|
178
|
+
);
|
|
179
|
+
INSERT INTO session_setting(session_file, wake, updated_at)
|
|
180
|
+
SELECT session_file, 0, updated_at FROM presence WHERE wake = 0;
|
|
166
181
|
`,
|
|
167
182
|
];
|
|
168
183
|
|
package/src/types.ts
CHANGED
|
@@ -17,6 +17,11 @@ export interface Presence {
|
|
|
17
17
|
waitingOn: string | null;
|
|
18
18
|
/** Whether a message may start a turn here (`/shipmates wake off` clears it). */
|
|
19
19
|
wake: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* It declared `waiting-for-human` (ADR 0006) and has not run since. Like
|
|
22
|
+
* `wake`, kept per transcript file across an unload or a resume (D40).
|
|
23
|
+
*/
|
|
24
|
+
declaredWaiting: boolean;
|
|
20
25
|
/** False once the process that registered it is gone. */
|
|
21
26
|
live: boolean;
|
|
22
27
|
pid: number;
|
|
@@ -25,6 +30,18 @@ export interface Presence {
|
|
|
25
30
|
settledAt: number | null;
|
|
26
31
|
}
|
|
27
32
|
|
|
33
|
+
/**
|
|
34
|
+
* What a session chose for itself, kept per transcript file whether or not it
|
|
35
|
+
* is live (D40): `/shipmates wake off`, and a declared `waiting-for-human`
|
|
36
|
+
* with no run since. `updatedAt` is null for a session that set nothing.
|
|
37
|
+
*/
|
|
38
|
+
export interface SessionSetting {
|
|
39
|
+
sessionFile: string;
|
|
40
|
+
wake: boolean;
|
|
41
|
+
declaredWaiting: boolean;
|
|
42
|
+
updatedAt: number | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
28
45
|
/** The human words a human-relayed message carries, verbatim, and where they were said. */
|
|
29
46
|
export interface Relay {
|
|
30
47
|
text: string;
|
|
@@ -68,6 +85,12 @@ export interface Question {
|
|
|
68
85
|
createdAt: number;
|
|
69
86
|
}
|
|
70
87
|
|
|
88
|
+
/**
|
|
89
|
+
* The `answerChannel` of an item the human answered by replying in the asking
|
|
90
|
+
* session's own conversation rather than in an inbox (D39).
|
|
91
|
+
*/
|
|
92
|
+
export const SESSION_ANSWER_CHANNEL = 'session';
|
|
93
|
+
|
|
71
94
|
export interface AttentionItem {
|
|
72
95
|
id: string;
|
|
73
96
|
session: string;
|
|
@@ -77,8 +100,15 @@ export interface AttentionItem {
|
|
|
77
100
|
onBehalfOf: string[];
|
|
78
101
|
state: 'open' | 'answered' | 'withdrawn';
|
|
79
102
|
answer: string | null;
|
|
103
|
+
/**
|
|
104
|
+
* Where the human answered: `terminal` (a TUI dialog or `/shipmates inbox`),
|
|
105
|
+
* `cli`, a host's own channel (`web`, `telegram`), or `session` when the
|
|
106
|
+
* human replied in the asking session's own conversation (D39).
|
|
107
|
+
*/
|
|
80
108
|
answerChannel: string | null;
|
|
81
109
|
createdAt: number;
|
|
110
|
+
/** When it was answered or withdrawn; null while open. */
|
|
111
|
+
finishedAt: number | null;
|
|
82
112
|
}
|
|
83
113
|
|
|
84
114
|
/** How a session came to be owned (D23): through a host, or the terminal default (D19). */
|