@clagentic/console 1.11.0 → 1.12.0-beta.2

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.
Files changed (99) hide show
  1. package/README.md +1 -0
  2. package/bin/cli.js +14 -2
  3. package/lib/build-changes.js +8 -2
  4. package/lib/build-sha.json +2 -2
  5. package/lib/build-update-offer.js +31 -7
  6. package/lib/claude-settings-env.js +142 -0
  7. package/lib/cli/arg-validation.js +1 -1
  8. package/lib/cli/duration.js +27 -0
  9. package/lib/cli/ipc-subcommands.js +26 -0
  10. package/lib/cli/schedule-input-subcommands.js +1 -22
  11. package/lib/cli/session-subcommands.js +38 -7
  12. package/lib/daemon.js +26 -0
  13. package/lib/notification-prefs.js +257 -0
  14. package/lib/notify/core.js +175 -0
  15. package/lib/notify/index.js +31 -0
  16. package/lib/notify/live-prompts.js +36 -0
  17. package/lib/notify/payload.js +63 -0
  18. package/lib/notify/target.js +5 -0
  19. package/lib/notify/type-registry.js +64 -0
  20. package/lib/notify/types.js +20 -0
  21. package/lib/open-sockets.js +27 -0
  22. package/lib/outbound-access.js +74 -0
  23. package/lib/outbound-entry.js +109 -0
  24. package/lib/outbound-ledger.js +306 -0
  25. package/lib/outbound-query.js +153 -0
  26. package/lib/pending-prompts.js +31 -16
  27. package/lib/project-connection.js +40 -1
  28. package/lib/project-filesystem.js +20 -5
  29. package/lib/project-http.js +9 -1
  30. package/lib/project-loop.js +25 -13
  31. package/lib/project-notifications.js +206 -42
  32. package/lib/project-prompt-responses.js +1 -1
  33. package/lib/project-sessions.js +30 -1
  34. package/lib/project.js +15 -1
  35. package/lib/prompt-registry.js +92 -24
  36. package/lib/prompt-reminders.js +282 -0
  37. package/lib/prompt-source.js +43 -8
  38. package/lib/public/app.js +1 -0
  39. package/lib/public/css/diagnostics.css +28 -0
  40. package/lib/public/css/home-hub.css +12 -0
  41. package/lib/public/css/icon-strip.css +9 -0
  42. package/lib/public/css/input.css +93 -0
  43. package/lib/public/css/menus.css +22 -0
  44. package/lib/public/css/mobile-nav.css +11 -0
  45. package/lib/public/css/server-settings.css +24 -0
  46. package/lib/public/css/sidebar.css +24 -0
  47. package/lib/public/index.html +6 -0
  48. package/lib/public/modules/activity-state.js +20 -4
  49. package/lib/public/modules/app-connection.js +22 -5
  50. package/lib/public/modules/app-header.js +2 -0
  51. package/lib/public/modules/app-messages.js +6 -2
  52. package/lib/public/modules/app-notifications.js +65 -40
  53. package/lib/public/modules/app-projects.js +3 -1
  54. package/lib/public/modules/app-rate-limit.js +12 -6
  55. package/lib/public/modules/app-rendering.js +26 -3
  56. package/lib/public/modules/build-update-sessions.js +81 -0
  57. package/lib/public/modules/collapsible-text.js +77 -0
  58. package/lib/public/modules/diagnostics.js +9 -2
  59. package/lib/public/modules/env-shadow-notice.js +34 -0
  60. package/lib/public/modules/keep-alive.js +66 -0
  61. package/lib/public/modules/notification-prefs.js +151 -0
  62. package/lib/public/modules/notify-client.js +153 -0
  63. package/lib/public/modules/project-settings.js +5 -0
  64. package/lib/public/modules/prompt-reminders-setting.js +13 -0
  65. package/lib/public/modules/prompt-state.js +3 -3
  66. package/lib/public/modules/prompts.js +1 -1
  67. package/lib/public/modules/server-settings.js +3 -0
  68. package/lib/public/modules/sidebar-mobile.js +8 -0
  69. package/lib/public/modules/sidebar-sessions.js +27 -0
  70. package/lib/public/modules/terminal-links.js +175 -0
  71. package/lib/public/modules/terminal.js +19 -103
  72. package/lib/public/notify-sw-logic.js +300 -0
  73. package/lib/public/notify-target.js +97 -0
  74. package/lib/public/sw.js +15 -62
  75. package/lib/push-audience.js +84 -0
  76. package/lib/push.js +87 -20
  77. package/lib/runtime-env.js +1 -0
  78. package/lib/sdk-bridge.js +83 -36
  79. package/lib/sdk-message-processor.js +148 -49
  80. package/lib/sender-ref.js +3 -0
  81. package/lib/server.js +80 -4
  82. package/lib/session-directory.js +6 -4
  83. package/lib/session-input-ipc.js +11 -1
  84. package/lib/session-input.js +95 -14
  85. package/lib/session-liveness.js +53 -0
  86. package/lib/session-reclaim.js +37 -2
  87. package/lib/sessions.js +58 -4
  88. package/lib/usage-store.js +403 -0
  89. package/lib/vendor-auth.js +33 -2
  90. package/lib/ws-schema.js +13 -2
  91. package/lib/yoke/adapters/claude-vendor.js +11 -1
  92. package/lib/yoke/adapters/claude.js +17 -0
  93. package/lib/yoke/adapters/codex.js +5 -1
  94. package/lib/yoke/auth-checks.js +17 -2
  95. package/lib/yoke/index.js +21 -3
  96. package/lib/yoke/provider-credentials.js +24 -0
  97. package/lib/yoke/registry.js +8 -1
  98. package/package.json +2 -2
  99. package/scripts/check-test-count.js +14 -0
@@ -0,0 +1,74 @@
1
+ // outbound-access.js - may this caller see the prompt text of an outbound-ledger
2
+ // entry? The prompt is also in the target session's history, so it is shown only
3
+ // to a caller who could read that session.
4
+ //
5
+ // The decision for a target that runs as another OS user is not made here. It is
6
+ // made when the entry is written (lib/outbound-entry.js): such an entry, and one
7
+ // that did not record the OS user of its target, stores no prompt and no context
8
+ // note, only metadata and the reason. No read path and no later mode can return
9
+ // text that was never stored, so this module has no os-users branch.
10
+ //
11
+ // The caller is one of two shapes:
12
+ // - the daemon-owner socket (the CLI verb) carries no user record; the OS
13
+ // gate on the socket proves the caller is the daemon's own OS user. It gets
14
+ // each entry as stored.
15
+ // - a caller that is a console user (a transport that authenticates one) is
16
+ // judged by canReadSession, which applies canAccessSession: project access,
17
+ // then owner, shared or private.
18
+ //
19
+ // Metadata is always shown. A text that is not shown is replaced by a stated
20
+ // reason, never silently dropped.
21
+
22
+ // deps:
23
+ // projectAccess lib/project-access.js createProjectAccess() result
24
+ // getProject(slug) -> project context | null
25
+ // caller: { user, authenticated } user is a user record or null
26
+ function createOutboundAccess(deps) {
27
+ var projectAccess = deps.projectAccess;
28
+ var getProject = deps.getProject;
29
+
30
+ // The session an entry's target names, or null. Only the vendor session id
31
+ // names a session for good. A console id is renumbered by a restart, so it
32
+ // never identifies a target here: an access decision that rested on it could
33
+ // hand the text of one session to the reader of another.
34
+ function findTarget(entry) {
35
+ var target = entry.target || {};
36
+ var project = target.project ? getProject(target.project) : null;
37
+ if (!project || !project.sm || !project.sm.sessions || !target.cliSessionId) return null;
38
+ var found = null;
39
+ project.sm.sessions.forEach(function (s) {
40
+ if (!found && s.cliSessionId === target.cliSessionId) found = s;
41
+ });
42
+ return found;
43
+ }
44
+
45
+ // Returns { allowed: true } or { allowed: false, reason }.
46
+ function canReadPrompt(entry, caller) {
47
+ caller = caller || {};
48
+ var principal = projectAccess.principalFor(caller.user || null, { authenticated: caller.authenticated === true });
49
+ if (!principal) {
50
+ if (!caller.user && caller.authenticated === true) return { allowed: true };
51
+ return { allowed: false, reason: "the caller is not authenticated; prompt text is shown only to a caller who can read the target session" };
52
+ }
53
+ // The single-user owner reads every session the console holds, so there is
54
+ // no session boundary to check, including for a target that is gone.
55
+ if (principal.implicit) return { allowed: true };
56
+
57
+ var target = entry.target || {};
58
+ if (!target.project || !projectAccess.canAccess(principal.id, target.project, { authenticated: true })) {
59
+ return { allowed: false, reason: "the caller cannot access the target project" };
60
+ }
61
+ var session = findTarget(entry);
62
+ if (!session) {
63
+ return { allowed: false, reason: "the target session is not held by the console, so access to it cannot be checked" };
64
+ }
65
+ if (!projectAccess.canReadSession(principal.user, session, { authenticated: true })) {
66
+ return { allowed: false, reason: "the caller cannot read the target session" };
67
+ }
68
+ return { allowed: true };
69
+ }
70
+
71
+ return { canReadPrompt: canReadPrompt };
72
+ }
73
+
74
+ module.exports = { createOutboundAccess: createOutboundAccess };
@@ -0,0 +1,109 @@
1
+ // outbound-entry.js - builds one outbound-ledger entry (lib/outbound-ledger.js)
2
+ // from what session-input.js knows at the moment a request is answered.
3
+ //
4
+ // Pure: no clock, no I/O. The sender fields are the ones senderFields() in
5
+ // injected-sender.js produced for the history entry, so the ledger and the
6
+ // target's history can never name different senders.
7
+
8
+ var { SENDER_KIND } = require("./injected-sender");
9
+
10
+ var VERB = Object.freeze({ SEND_INPUT: "send_input", OPEN_SESSION: "open_session" });
11
+
12
+ // target.runsAs for a session that runs as the daemon's own OS user; any other
13
+ // value is the name of the OS user it runs as.
14
+ var RUNS_AS_DAEMON = "daemon";
15
+
16
+ function put(obj, key, value) {
17
+ if (value !== undefined && value !== null && value !== "") obj[key] = value;
18
+ }
19
+
20
+ // How far the sender can be trusted. "extracted" is derived from the console's
21
+ // own records for a process it started; a same-user process can copy that
22
+ // reference. Anything else is whatever the caller said about itself.
23
+ function attributionOf(senderKind) {
24
+ return senderKind === SENDER_KIND.EXTRACTED ? "extracted" : "unverified";
25
+ }
26
+
27
+ function senderOf(fields) {
28
+ var s = { kind: fields.senderKind || SENDER_KIND.UNDECLARED, attribution: attributionOf(fields.senderKind) };
29
+ put(s, "name", fields.fromName);
30
+ put(s, "project", fields.senderProject);
31
+ put(s, "sessionId", fields.senderSessionId);
32
+ put(s, "cliSessionId", fields.senderCliSessionId);
33
+ put(s, "agent", fields.senderAgent);
34
+ put(s, "vendor", fields.senderVendor);
35
+ put(s, "declaredName", fields.declaredName);
36
+ // Self-reported on a declared sender; the console's own record on an extracted one.
37
+ put(s, "fromSession", fields.fromSession);
38
+ return s;
39
+ }
40
+
41
+ // Why message text is not stored for a target, or null when it is stored. An
42
+ // absent runsAs is treated as another OS user: it cannot be shown to be the daemon's.
43
+ function withholdReasonFor(runsAs) {
44
+ if (runsAs === RUNS_AS_DAEMON) return null;
45
+ if (typeof runsAs === "string" && runsAs) return "the target session runs as another OS user";
46
+ return "the OS user the target session runs as was not recorded, so it cannot be established";
47
+ }
48
+
49
+ // args:
50
+ // verb VERB.*
51
+ // ts ISO time of the request's outcome
52
+ // req the request as the caller gave it
53
+ // out the result (absent for a delivery, recorded before it is built)
54
+ // initiator the resolved initiator, or null when it was refused
55
+ // messageId set for a delivery
56
+ // sender senderFields() output
57
+ // target { project, consoleSessionId, sessionId, agent, vendor, name, runsAs } of the
58
+ // session that took (or was meant to take) the prompt
59
+ function buildEntry(args) {
60
+ var req = args.req || {};
61
+ var out = args.out || {};
62
+ var entry = {
63
+ formatVersion: 1,
64
+ ts: args.ts,
65
+ verb: args.verb,
66
+ result: out.result || (args.verb === VERB.OPEN_SESSION ? "opened" : "delivered"),
67
+ };
68
+ put(entry, "reason", out.reason);
69
+ if (out.retryable === true) entry.retryable = true;
70
+ put(entry, "messageId", args.messageId);
71
+ put(entry, "initiator", args.initiator);
72
+ entry.sender = senderOf(args.sender || {});
73
+
74
+ var t = args.target || {};
75
+ var target = {};
76
+ put(target, "project", t.project || req.projectSlug);
77
+ put(target, "sessionId", t.consoleSessionId);
78
+ put(target, "cliSessionId", t.sessionId);
79
+ put(target, "agent", t.agent);
80
+ put(target, "vendor", t.vendor);
81
+ put(target, "name", t.name);
82
+ // Recorded at write time: a console id is renumbered by a restart, so the
83
+ // reader cannot work out later which OS user the target ran as.
84
+ put(target, "runsAs", t.runsAs);
85
+ // What the caller asked for, kept when it differs from what it resolved to.
86
+ put(target, "address", typeof req.to === "string" ? req.to : undefined);
87
+ put(target, "requestedSessionId", typeof req.sessionId === "string" ? req.sessionId : undefined);
88
+ if (req.reply) target.reply = true;
89
+ entry.target = target;
90
+
91
+ // Message text (the prompt and the context note) is stored only for a target
92
+ // known to run as the daemon's OS user. For any other target, or one whose OS
93
+ // user was not recorded, it is never written: the access decision is made here,
94
+ // at write time, so no read path and no later mode can return it.
95
+ var withholdReason = withholdReasonFor(t.runsAs);
96
+ var hasPrompt = typeof req.prompt === "string" && req.prompt;
97
+ var hasNote = typeof req.contextNote === "string" && req.contextNote;
98
+ if (withholdReason) {
99
+ if (hasPrompt) entry.promptWithheld = true;
100
+ if (hasNote) entry.contextNoteWithheld = true;
101
+ if (hasPrompt || hasNote) entry.promptWithheldReason = withholdReason;
102
+ } else {
103
+ if (hasNote) entry.contextNote = req.contextNote;
104
+ if (hasPrompt) entry.prompt = req.prompt;
105
+ }
106
+ return entry;
107
+ }
108
+
109
+ module.exports = { VERB: VERB, RUNS_AS_DAEMON: RUNS_AS_DAEMON, buildEntry: buildEntry };
@@ -0,0 +1,306 @@
1
+ // outbound-ledger.js - the sender-side record of what a session sent out.
2
+ //
3
+ // Every open-session and send-input request (CLI verb or file drop) leaves one
4
+ // line here, written by the sender: the delivery, or the refusal or failure.
5
+ // The target's own history holds a delivered prompt too, but nothing there lists
6
+ // what one sender sent, and a refused request leaves no trace at all.
7
+ //
8
+ // Location: <console data dir>/outbound/ (0700; files 0600)
9
+ // sessions/<project>/<sender>.jsonl a sender the console identified from its
10
+ // own records (senderKind "extracted")
11
+ // unattributed/declared/<name>.jsonl a sender that only named itself (--from)
12
+ // unattributed/undeclared/undeclared.jsonl a sender that named nobody
13
+ // Unattributed lines are self-reported and unverified; they are kept apart so a
14
+ // reader never mistakes one for an identified sender.
15
+ //
16
+ // One JSON object per line, appended with a single write; a reader skips a line
17
+ // it cannot parse. The line holds the full prompt text, so the files are as
18
+ // private as the session history and are removed after the retention period
19
+ // (default 30 days, daemon.json "outboundRetentionDays"; 0 turns the ledger off).
20
+ //
21
+ // Writing never fails the request it describes: record() logs and returns.
22
+
23
+ var crypto = require("crypto");
24
+ var fs = require("fs");
25
+ var path = require("path");
26
+
27
+ var FORMAT_VERSION = 1;
28
+ var DIR_NAME = "outbound";
29
+ var DEFAULT_RETENTION_DAYS = 30;
30
+ var MAX_RETENTION_DAYS = 3650;
31
+ var DAY_MS = 24 * 60 * 60 * 1000;
32
+ var PRUNE_INTERVAL_MS = 6 * 60 * 60 * 1000;
33
+
34
+ var SENDER_DIR = "sessions";
35
+ var UNATTRIBUTED_DIR = "unattributed";
36
+
37
+ // The retention in days a configured value means. A missing value is the
38
+ // default; anything that is not a whole number of days from 0 to 3650 is the
39
+ // default too, and { invalid: true } says so, so a typo is not silently kept.
40
+ function retentionDaysFrom(raw) {
41
+ if (raw === undefined || raw === null) return { days: DEFAULT_RETENTION_DAYS };
42
+ if (typeof raw === "number" && Number.isInteger(raw) && raw >= 0 && raw <= MAX_RETENTION_DAYS) return { days: raw };
43
+ return { days: DEFAULT_RETENTION_DAYS, invalid: true };
44
+ }
45
+
46
+ // A name that is safe as one path component. A value that is not a plain id is
47
+ // replaced by a hash of itself, so two different values never share a file and
48
+ // nothing a sender chose can name a path.
49
+ function segment(raw) {
50
+ var s = String(raw);
51
+ if (/^[A-Za-z0-9_][A-Za-z0-9_-]{0,63}$/.test(s)) return s;
52
+ return "x-" + crypto.createHash("sha256").update(s).digest("hex").slice(0, 16);
53
+ }
54
+
55
+ // The file an entry belongs in, relative to the ledger root.
56
+ function relativePathFor(entry) {
57
+ var sender = entry.sender || {};
58
+ if (sender.attribution === "extracted" && sender.project && (sender.cliSessionId || sender.sessionId)) {
59
+ // The vendor session id survives a daemon restart; the console id is
60
+ // renumbered, so it names a sender only until the vendor has named it.
61
+ var key = sender.cliSessionId ? segment(sender.cliSessionId) : "console-" + segment(sender.sessionId);
62
+ return path.join(SENDER_DIR, segment(sender.project), key + ".jsonl");
63
+ }
64
+ if (sender.kind === "declared" && sender.name) {
65
+ return path.join(UNATTRIBUTED_DIR, "declared", segment(sender.name) + ".jsonl");
66
+ }
67
+ return path.join(UNATTRIBUTED_DIR, "undeclared", "undeclared.jsonl");
68
+ }
69
+
70
+ function parseLines(text) {
71
+ var out = [];
72
+ var lines = text.split("\n");
73
+ for (var i = 0; i < lines.length; i++) {
74
+ if (!lines[i]) continue;
75
+ try {
76
+ var obj = JSON.parse(lines[i]);
77
+ if (obj && typeof obj === "object" && !Array.isArray(obj)) out.push({ line: lines[i], entry: obj });
78
+ } catch (e) {
79
+ // A torn final line after a crash, or a damaged one: not readable, skipped.
80
+ }
81
+ }
82
+ return out;
83
+ }
84
+
85
+ function tsOf(entry) {
86
+ var ms = typeof entry.ts === "string" ? Date.parse(entry.ts) : NaN;
87
+ return isFinite(ms) ? ms : null;
88
+ }
89
+
90
+ function errorText(e) {
91
+ return e && e.message ? e.message : String(e);
92
+ }
93
+
94
+ // A ledger path that is not there holds no entries; any other failure to read
95
+ // it (permissions, a file where a directory belongs, I/O) is an error the
96
+ // reader must report, never an empty list that looks like "nothing was sent".
97
+ function isAbsent(e) {
98
+ return !!e && e.code === "ENOENT";
99
+ }
100
+
101
+ function unreadable(file, e) {
102
+ var err = new Error("the outbound ledger cannot be read: " + file + " (" + ((e && e.code) || errorText(e)) + ")");
103
+ err.code = (e && e.code) || "EIO";
104
+ err.path = file;
105
+ return err;
106
+ }
107
+
108
+ // opts:
109
+ // getBaseDir() the console data directory (read at use)
110
+ // getRetentionDays() the configured value, or undefined (read at use, so a
111
+ // change in daemon.json applies without a restart)
112
+ // now() ms clock (default Date.now), for tests
113
+ // log console-like (default console)
114
+ function createOutboundLedger(opts) {
115
+ var getBaseDir = opts.getBaseDir;
116
+ var getRetentionDays = opts.getRetentionDays || function () { return undefined; };
117
+ var now = opts.now || Date.now;
118
+ var log = opts.log || console;
119
+ var timer = null;
120
+ var warnedInvalid = null;
121
+
122
+ function root() {
123
+ return path.join(getBaseDir(), DIR_NAME);
124
+ }
125
+
126
+ function retentionDays() {
127
+ var raw = getRetentionDays();
128
+ var r = retentionDaysFrom(raw);
129
+ if (r.invalid && warnedInvalid !== JSON.stringify(raw)) {
130
+ warnedInvalid = JSON.stringify(raw);
131
+ log.warn("[outbound-ledger] outboundRetentionDays must be a whole number of days from 0 to " + MAX_RETENTION_DAYS + "; using " + DEFAULT_RETENTION_DAYS);
132
+ }
133
+ return r.days;
134
+ }
135
+
136
+ // Appends one entry. Never throws: the request it describes is already
137
+ // decided, and a ledger that cannot be written must not change that.
138
+ function record(entry) {
139
+ try {
140
+ if (retentionDays() === 0) return false;
141
+ var file = path.join(root(), relativePathFor(entry));
142
+ fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
143
+ fs.appendFileSync(file, JSON.stringify(entry) + "\n", { mode: 0o600 });
144
+ return true;
145
+ } catch (e) {
146
+ log.error("[outbound-ledger] could not write the entry:", errorText(e));
147
+ return false;
148
+ }
149
+ }
150
+
151
+ // The ledger files under a subdirectory: [{ file, dir }] (dir is the
152
+ // component under it, the project or the unattributed kind). A directory that
153
+ // is absent has none; one that cannot be read throws (see unreadable()).
154
+ function filesUnder(sub, onlyDir) {
155
+ var base = path.join(root(), sub);
156
+ var dirs;
157
+ try { dirs = fs.readdirSync(base); } catch (e) {
158
+ if (isAbsent(e)) return [];
159
+ throw unreadable(base, e);
160
+ }
161
+ var out = [];
162
+ dirs.forEach(function (d) {
163
+ if (onlyDir !== undefined && d !== onlyDir) return;
164
+ var names;
165
+ // Absent here means a prune removed the directory after the listing above.
166
+ try { names = fs.readdirSync(path.join(base, d)); } catch (e) {
167
+ if (isAbsent(e)) return;
168
+ throw unreadable(path.join(base, d), e);
169
+ }
170
+ names.forEach(function (n) {
171
+ if (n.endsWith(".jsonl")) out.push({ file: path.join(base, d, n), dir: d });
172
+ });
173
+ });
174
+ return out;
175
+ }
176
+
177
+ // Entries at or after sinceMs and within the retention, oldest first.
178
+ // A file last written before the cut-off holds nothing newer and is not read.
179
+ // Throws (see unreadable()) when a file that exists cannot be read.
180
+ function readFiles(files, sinceMs) {
181
+ var days = retentionDays();
182
+ if (days === 0) return [];
183
+ var cutoff = Math.max(typeof sinceMs === "number" ? sinceMs : 0, now() - days * DAY_MS);
184
+ var out = [];
185
+ files.forEach(function (f) {
186
+ var text;
187
+ try {
188
+ if (fs.statSync(f.file).mtimeMs < cutoff) return;
189
+ text = fs.readFileSync(f.file, "utf8");
190
+ } catch (e) {
191
+ // Absent: pruned between the listing and this read.
192
+ if (isAbsent(e)) return;
193
+ log.error("[outbound-ledger] could not read " + path.basename(f.file) + ":", errorText(e));
194
+ throw unreadable(f.file, e);
195
+ }
196
+ parseLines(text).forEach(function (l) {
197
+ var ts = tsOf(l.entry);
198
+ if (ts !== null && ts >= cutoff) out.push(l.entry);
199
+ });
200
+ });
201
+ out.sort(function (a, b) { return tsOf(a) - tsOf(b); });
202
+ return out;
203
+ }
204
+
205
+ // Entries of identified senders, optionally of one project.
206
+ function readAttributed(query) {
207
+ query = query || {};
208
+ var only = query.project === undefined ? undefined : segment(query.project);
209
+ return readFiles(filesUnder(SENDER_DIR, only), query.sinceMs);
210
+ }
211
+
212
+ // Entries of senders that only named themselves, or nobody.
213
+ function readUnattributed(query) {
214
+ query = query || {};
215
+ return readFiles(filesUnder(UNATTRIBUTED_DIR), query.sinceMs);
216
+ }
217
+
218
+ // Rewrites a file without its expired lines, through a temporary file and a
219
+ // rename; removes it when nothing is left. Returns the number of lines dropped.
220
+ function pruneFile(file, cutoff) {
221
+ var text = fs.readFileSync(file, "utf8");
222
+ var all = text.split("\n").filter(function (l) { return l; });
223
+ var kept = parseLines(text).filter(function (l) {
224
+ var ts = tsOf(l.entry);
225
+ return ts !== null && ts >= cutoff;
226
+ });
227
+ if (kept.length === all.length) return 0;
228
+ if (!kept.length) {
229
+ fs.unlinkSync(file);
230
+ return all.length;
231
+ }
232
+ var tmp = file + ".tmp";
233
+ fs.writeFileSync(tmp, kept.map(function (l) { return l.line; }).join("\n") + "\n", { mode: 0o600 });
234
+ fs.renameSync(tmp, file);
235
+ return all.length - kept.length;
236
+ }
237
+
238
+ // ENOTEMPTY (a file is still there) and ENOENT (already gone) are expected;
239
+ // anything else is logged once per path and errno.
240
+ var warnedRmdir = new Set();
241
+ function removeIfEmpty(dir) {
242
+ try { fs.rmdirSync(dir); } catch (e) {
243
+ if (e && (e.code === "ENOTEMPTY" || e.code === "ENOENT")) return;
244
+ var key = dir + "|" + (e && e.code);
245
+ if (warnedRmdir.has(key)) return;
246
+ warnedRmdir.add(key);
247
+ log.error("[outbound-ledger] could not remove the directory " + dir + " (" + ((e && e.code) || errorText(e)) + ")");
248
+ }
249
+ }
250
+
251
+ // Removes every entry older than the retention. Synchronous end to end, so no
252
+ // append interleaves with a rewrite. Returns { files, removed }.
253
+ function prune() {
254
+ var cutoff = now() - retentionDays() * DAY_MS;
255
+ var removed = 0;
256
+ var files = 0;
257
+ [SENDER_DIR, UNATTRIBUTED_DIR].forEach(function (sub) {
258
+ filesUnder(sub).forEach(function (f) {
259
+ try {
260
+ var n = pruneFile(f.file, cutoff);
261
+ if (n) { removed += n; files++; }
262
+ // An emptied directory is removed; one still holding files is left.
263
+ removeIfEmpty(path.dirname(f.file));
264
+ } catch (e) {
265
+ log.error("[outbound-ledger] could not prune " + path.basename(f.file) + ":", errorText(e));
266
+ }
267
+ });
268
+ });
269
+ if (removed) log.log("[outbound-ledger] pruned " + removed + " expired entr" + (removed === 1 ? "y" : "ies") + " from " + files + " file(s)");
270
+ return { files: files, removed: removed };
271
+ }
272
+
273
+ // Prunes now and then every few hours. The timer never keeps the process up.
274
+ function start() {
275
+ try { prune(); } catch (e) { log.error("[outbound-ledger] prune failed:", errorText(e)); }
276
+ if (timer) return;
277
+ timer = setInterval(function () {
278
+ try { prune(); } catch (e) { log.error("[outbound-ledger] prune failed:", errorText(e)); }
279
+ }, PRUNE_INTERVAL_MS);
280
+ if (typeof timer.unref === "function") timer.unref();
281
+ }
282
+
283
+ function stop() {
284
+ if (timer) clearInterval(timer);
285
+ timer = null;
286
+ }
287
+
288
+ return {
289
+ record: record,
290
+ readAttributed: readAttributed,
291
+ readUnattributed: readUnattributed,
292
+ prune: prune,
293
+ start: start,
294
+ stop: stop,
295
+ retentionDays: retentionDays,
296
+ };
297
+ }
298
+
299
+ module.exports = {
300
+ FORMAT_VERSION: FORMAT_VERSION,
301
+ DIR_NAME: DIR_NAME,
302
+ DEFAULT_RETENTION_DAYS: DEFAULT_RETENTION_DAYS,
303
+ MAX_RETENTION_DAYS: MAX_RETENTION_DAYS,
304
+ retentionDaysFrom: retentionDaysFrom,
305
+ createOutboundLedger: createOutboundLedger,
306
+ };
@@ -0,0 +1,153 @@
1
+ // outbound-query.js - the read side of the outbound ledger: what one session
2
+ // sent out through open-session and send-input, for the `list-outbound` verb.
3
+ //
4
+ // Read-only. Returns the outbound calls only (target, prompt, time, initiator,
5
+ // result), never any transcript content. A prompt is returned only when the
6
+ // caller may read the target session (lib/outbound-access.js); otherwise the
7
+ // entry carries its metadata, `prompt: null` and the reason it was withheld.
8
+ // For a target that runs as another OS user the text was never stored (the
9
+ // writer, lib/outbound-entry.js, withholds it), so the entry already carries
10
+ // promptWithheld, contextNoteWithheld and the reason as stored.
11
+
12
+ var { isSafeSessionId } = require("./session-id");
13
+ var { VERB } = require("./outbound-entry");
14
+
15
+ var KIND_TO_VERB = Object.freeze({ sent: VERB.SEND_INPUT, opens: VERB.OPEN_SESSION });
16
+ var KIND_OF_VERB = Object.freeze({ send_input: "sent", open_session: "opens" });
17
+
18
+ function refused(reason) {
19
+ return { ok: false, result: "refused", reason: reason };
20
+ }
21
+
22
+ // A ledger that cannot be read is an error result, not an empty list.
23
+ function unreadable(err) {
24
+ return { ok: false, result: "error", reason: err && err.message ? err.message : String(err), code: (err && err.code) || null };
25
+ }
26
+
27
+ function shape(entry, verdict) {
28
+ var out = {};
29
+ for (var k in entry) if (k !== "prompt" && k !== "formatVersion") out[k] = entry[k];
30
+ out.kind = KIND_OF_VERB[entry.verb] || null;
31
+ // The context note is message content the caller wrote alongside the prompt,
32
+ // so it is shown only when the prompt may be. The other free-text fields
33
+ // (reason, sender and target names, addresses) are console-made or are names
34
+ // and addresses, not message content.
35
+ if (!verdict.allowed && typeof entry.contextNote === "string") {
36
+ delete out.contextNote;
37
+ out.contextNoteWithheld = true;
38
+ }
39
+ if (typeof entry.prompt !== "string") {
40
+ out.prompt = null;
41
+ } else if (verdict.allowed) {
42
+ out.prompt = entry.prompt;
43
+ } else {
44
+ out.prompt = null;
45
+ out.promptWithheld = true;
46
+ out.promptWithheldReason = verdict.reason;
47
+ }
48
+ return out;
49
+ }
50
+
51
+ // deps:
52
+ // ledger lib/outbound-ledger.js createOutboundLedger() result
53
+ // access lib/outbound-access.js createOutboundAccess() result
54
+ // caller { user, authenticated } the principal behind this transport
55
+ function createOutboundQuery(deps) {
56
+ var ledger = deps.ledger;
57
+ var access = deps.access;
58
+ var caller = deps.caller;
59
+
60
+ function present(entries) {
61
+ return entries.map(function (e) { return shape(e, access.canReadPrompt(e, caller)); });
62
+ }
63
+
64
+ // req: { sessionId, projectSlug?, sinceMs?, kind? }
65
+ // sessionId the vendor's own session id of the sender (never a console id)
66
+ // sinceMs only entries at or after this time (epoch ms)
67
+ // kind "sent" (send-input) or "opens" (open-session)
68
+ function list(req) {
69
+ req = req || {};
70
+ if (typeof req.sessionId !== "string" || !isSafeSessionId(req.sessionId)) {
71
+ return refused("a session id is required and must be a plain id (letters, digits, _ and -)");
72
+ }
73
+ // A console id is renumbered at a daemon restart, so one number can name
74
+ // different sessions over the ledger's retention; it is never an identity.
75
+ if (/^[0-9]+$/.test(req.sessionId)) {
76
+ return refused("--session takes the vendor session id, not a console id: console ids are renumbered when the daemon restarts, so one can name different sessions over time. The vendor id is shown by list-sessions (sessionId).");
77
+ }
78
+ if (req.projectSlug !== undefined && (typeof req.projectSlug !== "string" || !req.projectSlug)) {
79
+ return refused("project must be a non-empty string");
80
+ }
81
+ if (req.sinceMs !== undefined && !(typeof req.sinceMs === "number" && isFinite(req.sinceMs))) {
82
+ return refused("since must be a time");
83
+ }
84
+ var verb = null;
85
+ if (req.kind !== undefined) {
86
+ verb = Object.prototype.hasOwnProperty.call(KIND_TO_VERB, req.kind) ? KIND_TO_VERB[req.kind] : null;
87
+ if (!verb) return refused("unknown kind " + JSON.stringify(String(req.kind).slice(0, 32)) + " (accepted: sent, opens)");
88
+ }
89
+
90
+ var days = ledger.retentionDays();
91
+ var base = { ok: true, result: "listed", retentionDays: days };
92
+ if (days === 0) {
93
+ return Object.assign(base, { count: 0, messages: [], unattributed: [], note: "the outbound ledger is turned off (outboundRetentionDays is 0)" });
94
+ }
95
+
96
+ var id = req.sessionId;
97
+ var mine;
98
+ try {
99
+ mine = ledger.readAttributed({ project: req.projectSlug, sinceMs: req.sinceMs }).filter(function (e) {
100
+ return (e.sender || {}).cliSessionId === id;
101
+ });
102
+ } catch (e) {
103
+ return unreadable(e);
104
+ }
105
+
106
+ // Vendor ids are unique, but a ledger written by hand or by an older
107
+ // writer could repeat one across projects; never merge those silently.
108
+ // A Set, not an object: a slug such as __proto__ must count as a key.
109
+ var projects = new Set();
110
+ mine.forEach(function (e) { projects.add(e.sender.project); });
111
+ var projectNames = Array.from(projects);
112
+ if (projectNames.length > 1) {
113
+ return {
114
+ ok: false,
115
+ result: "ambiguous",
116
+ reason: "the session id " + JSON.stringify(id) + " names a sender in " + projectNames.length + " projects; send again with --project",
117
+ projects: projectNames.sort(),
118
+ };
119
+ }
120
+
121
+ // The vendor ids this sender has used, to find calls that only named it.
122
+ var vendorIds = new Set([id]);
123
+ mine.forEach(function (e) { if (e.sender.cliSessionId) vendorIds.add(e.sender.cliSessionId); });
124
+
125
+ // Self-reported: a declared sender names the session it claims to be. Kept
126
+ // apart and labelled, never merged into the identified senders' list.
127
+ var claimed;
128
+ try {
129
+ claimed = ledger.readUnattributed({ sinceMs: req.sinceMs }).filter(function (e) {
130
+ return e.sender && typeof e.sender.fromSession === "string" && vendorIds.has(e.sender.fromSession);
131
+ });
132
+ } catch (e) {
133
+ return unreadable(e);
134
+ }
135
+
136
+ function wanted(e) { return !verb || e.verb === verb; }
137
+ var messages = mine.filter(wanted);
138
+ var unattributed = claimed.filter(wanted);
139
+
140
+ return Object.assign(base, {
141
+ session: { id: id, project: projectNames[0] || req.projectSlug || null },
142
+ count: messages.length,
143
+ messages: present(messages),
144
+ unattributedCount: unattributed.length,
145
+ unattributed: present(unattributed),
146
+ unattributedNote: "Calls whose sender only named itself (--from-session). The console cannot verify them; they are listed so they are not lost, not as this session's own calls.",
147
+ });
148
+ }
149
+
150
+ return { list: list };
151
+ }
152
+
153
+ module.exports = { createOutboundQuery: createOutboundQuery, KIND_TO_VERB: KIND_TO_VERB };