@clagentic/console 1.12.0-beta.1 → 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 (82) hide show
  1. package/README.md +1 -0
  2. package/bin/cli.js +2 -1
  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/duration.js +27 -0
  8. package/lib/cli/schedule-input-subcommands.js +1 -22
  9. package/lib/cli/session-subcommands.js +38 -7
  10. package/lib/daemon.js +6 -0
  11. package/lib/notification-prefs.js +2 -0
  12. package/lib/notify/core.js +175 -0
  13. package/lib/notify/index.js +31 -0
  14. package/lib/notify/live-prompts.js +36 -0
  15. package/lib/notify/payload.js +63 -0
  16. package/lib/notify/target.js +5 -0
  17. package/lib/notify/type-registry.js +64 -0
  18. package/lib/notify/types.js +20 -0
  19. package/lib/open-sockets.js +27 -0
  20. package/lib/outbound-access.js +74 -0
  21. package/lib/outbound-entry.js +109 -0
  22. package/lib/outbound-ledger.js +306 -0
  23. package/lib/outbound-query.js +153 -0
  24. package/lib/pending-prompts.js +31 -16
  25. package/lib/project-connection.js +40 -1
  26. package/lib/project-filesystem.js +20 -5
  27. package/lib/project-http.js +9 -1
  28. package/lib/project-loop.js +22 -7
  29. package/lib/project-notifications.js +54 -32
  30. package/lib/project-prompt-responses.js +1 -1
  31. package/lib/project-sessions.js +2 -1
  32. package/lib/project.js +15 -1
  33. package/lib/prompt-registry.js +74 -24
  34. package/lib/prompt-reminders.js +38 -33
  35. package/lib/prompt-source.js +43 -8
  36. package/lib/public/app.js +1 -0
  37. package/lib/public/css/diagnostics.css +28 -0
  38. package/lib/public/css/home-hub.css +12 -0
  39. package/lib/public/css/icon-strip.css +9 -0
  40. package/lib/public/css/mobile-nav.css +11 -0
  41. package/lib/public/css/sidebar.css +7 -0
  42. package/lib/public/index.html +3 -0
  43. package/lib/public/modules/activity-state.js +20 -4
  44. package/lib/public/modules/app-connection.js +22 -5
  45. package/lib/public/modules/app-notifications.js +47 -48
  46. package/lib/public/modules/app-projects.js +3 -1
  47. package/lib/public/modules/app-rendering.js +10 -0
  48. package/lib/public/modules/build-update-sessions.js +81 -0
  49. package/lib/public/modules/diagnostics.js +9 -2
  50. package/lib/public/modules/env-shadow-notice.js +34 -0
  51. package/lib/public/modules/notify-client.js +153 -0
  52. package/lib/public/modules/project-settings.js +5 -0
  53. package/lib/public/modules/prompt-state.js +3 -3
  54. package/lib/public/modules/prompts.js +1 -1
  55. package/lib/public/modules/server-settings.js +3 -0
  56. package/lib/public/modules/terminal-links.js +175 -0
  57. package/lib/public/modules/terminal.js +19 -103
  58. package/lib/public/notify-sw-logic.js +300 -0
  59. package/lib/public/notify-target.js +97 -0
  60. package/lib/public/sw.js +15 -68
  61. package/lib/push.js +63 -19
  62. package/lib/runtime-env.js +1 -0
  63. package/lib/sdk-bridge.js +62 -30
  64. package/lib/sdk-message-processor.js +78 -37
  65. package/lib/server.js +34 -2
  66. package/lib/session-directory.js +6 -4
  67. package/lib/session-input-ipc.js +11 -1
  68. package/lib/session-input.js +89 -14
  69. package/lib/session-liveness.js +53 -0
  70. package/lib/session-reclaim.js +37 -2
  71. package/lib/sessions.js +31 -4
  72. package/lib/usage-store.js +29 -7
  73. package/lib/vendor-auth.js +33 -2
  74. package/lib/ws-schema.js +6 -1
  75. package/lib/yoke/adapters/claude-vendor.js +11 -1
  76. package/lib/yoke/adapters/claude.js +7 -0
  77. package/lib/yoke/auth-checks.js +17 -2
  78. package/lib/yoke/index.js +21 -3
  79. package/lib/yoke/provider-credentials.js +24 -0
  80. package/lib/yoke/registry.js +8 -1
  81. package/package.json +2 -2
  82. package/scripts/check-test-count.js +14 -0
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+
3
+ // The notification core: the one place that turns a target and a message into a
4
+ // push, collapses repeats, and closes a push when its prompt resolves.
5
+ // Emitters call notify() and resolve() and keep no tag, route or dedupe logic.
6
+ // A notification reaches the surfaces its type registers: push, and the in-app
7
+ // banner (lib/project-notifications.js is the banner store the core delivers
8
+ // to; it is not a second core).
9
+ //
10
+ // The core keeps no memory of what it showed. Every tag is derived from the
11
+ // type's collapse rule and the target alone (payload.tagFor), so resolve() can
12
+ // name the tags to close from the target it is given, in any process: a close
13
+ // still works after a daemon restart.
14
+
15
+ var target = require("./target");
16
+ var payloads = require("./payload");
17
+ var { pushForSession, audienceFor } = require("../push-audience");
18
+
19
+ /**
20
+ * @param {object} deps
21
+ * @param {object} deps.registry a type registry (type-registry.js).
22
+ * @param {?object} deps.push the push module (sendPush, sendPushToUser).
23
+ * @param {?{show: function, dismissKeys: function}} [deps.inApp] the in-app
24
+ * banner store: show(event, data, meta) creates a banner, replacing the one
25
+ * under the same meta.key; dismissKeys(keys) removes the banners under them.
26
+ */
27
+ function createNotifyCore(deps) {
28
+ var registry = deps.registry;
29
+ var push = deps.push || null;
30
+ var inApp = deps.inApp || null;
31
+
32
+ function audienceOf(audience) {
33
+ return {
34
+ ownerId: audience && audience.ownerId || null,
35
+ isMultiUser: audience && audience.isMultiUser,
36
+ listAdmins: audience && audience.listAdmins,
37
+ };
38
+ }
39
+
40
+ function perSurface(content) {
41
+ var c = content || {};
42
+ if (c.push || c.banner) return { push: c.push || null, banner: c.banner || null };
43
+ return { push: c, banner: null };
44
+ }
45
+
46
+ function deliver(audience, payload) {
47
+ return pushForSession(push, audience.ownerId, payload, audience.isMultiUser, audience.listAdmins);
48
+ }
49
+
50
+ // The banner follows the push audience: the owner, or the admins for an
51
+ // ownerless session in multi-user mode, so no one else reads its title.
52
+ function bannerData(type, norm, raw, audience, data) {
53
+ var aud = audienceFor(audience.ownerId, audience.isMultiUser, audience.listAdmins);
54
+ // The audience and the target are applied last: emitter content cannot
55
+ // widen or redirect who reads the banner, or where it points.
56
+ return Object.assign({}, data, {
57
+ ownerId: audience.ownerId,
58
+ targetUserId: aud.kind === "owner" ? aud.userIds[0] : null,
59
+ targetUserIds: aud.kind === "admins" ? aud.userIds : null,
60
+ slug: norm.slug,
61
+ sessionId: raw.sessionId,
62
+ requestId: norm.requestId,
63
+ });
64
+ }
65
+
66
+ /**
67
+ * Shows (or replaces) the notification for a target, on every surface the
68
+ * type registers.
69
+ * @param {string} typeName a registered type.
70
+ * @param {{slug: ?string, sessionId: ?(number|string), requestId: ?string}} t
71
+ * @param {{push: ?object, banner: ?object}|object} content what each surface
72
+ * shows. push: the push text ({title, body}, or what the type's format
73
+ * reads); banner: the in-app store's own content (the target and audience
74
+ * fields are added here). A surface without content is not delivered to;
75
+ * the core picks the surfaces from the type's registration, never the
76
+ * emitter. A content naming neither key is the push content itself.
77
+ * @param {{ownerId: ?string, isMultiUser: ?function, listAdmins: ?function}} [audience]
78
+ * who the session belongs to; the push audience rules (lib/push-audience.js)
79
+ * decide the recipients from it, for the push and the banner alike. The
80
+ * mode and admin readers are optional.
81
+ * @returns {?object} the payload built, or null when nothing was delivered.
82
+ */
83
+ function notify(typeName, t, content, audience) {
84
+ var type = registry.get(typeName);
85
+ if (!type) throw new Error("unregistered notification type: " + typeName);
86
+ var norm = target.makeTarget(t);
87
+ if (!norm) throw new Error("invalid notification target for " + typeName);
88
+ if (type.collapse === "request" && !norm.requestId) {
89
+ throw new Error("notification type " + typeName + " needs a requestId");
90
+ }
91
+ var bySurface = perSurface(content);
92
+ var aud = audienceOf(audience);
93
+ var payload = payloads.buildPayload(type, norm, bySurface.push || bySurface.banner);
94
+ var delivered = false;
95
+ if (push && bySurface.push) {
96
+ deliver(aud, payload);
97
+ delivered = true;
98
+ }
99
+ if (inApp && type.banner && bySurface.banner) {
100
+ inApp.show(typeName, bannerData(type, norm, t, aud, bySurface.banner), {
101
+ key: payload.tag,
102
+ collapse: type.collapse,
103
+ persistent: type.persistent,
104
+ liveInState: type.liveInState,
105
+ });
106
+ delivered = true;
107
+ }
108
+ return delivered ? payload : null;
109
+ }
110
+
111
+ // The close-on-resolve types the targets' resolve reaches, grouped by
112
+ // notification category: a close push follows the preference of the
113
+ // notification it closes, and types of one category share a preference, so
114
+ // one push per category carries all its tags.
115
+ function closeGroups(norms) {
116
+ var groups = Object.create(null);
117
+ var order = [];
118
+ var bannerKeys = [];
119
+ norms.forEach(function (norm) {
120
+ registry.names().forEach(function (name) {
121
+ var type = registry.get(name);
122
+ if (!type.closeOnResolve) return;
123
+ // With a request named, that prompt's request-level notifications;
124
+ // without one, the session-level ones.
125
+ if ((type.collapse === "request") !== !!norm.requestId) return;
126
+ var key = type.category || name;
127
+ if (!groups[key]) {
128
+ groups[key] = { type: type, tags: [] };
129
+ order.push(key);
130
+ }
131
+ var tag = payloads.tagFor(type, norm);
132
+ if (groups[key].tags.indexOf(tag) === -1) groups[key].tags.push(tag);
133
+ if (type.banner && bannerKeys.indexOf(tag) === -1) bannerKeys.push(tag);
134
+ });
135
+ });
136
+ return { groups: order.map(function (key) { return groups[key]; }), bannerKeys: bannerKeys };
137
+ }
138
+
139
+ /**
140
+ * Closes what is shown for a target, on every surface and every device in
141
+ * its audience: the push on each device, and the banner in every page.
142
+ * @param {object|object[]} what a target { slug, sessionId, requestId }, or a
143
+ * list of them (a prompt known by more than one id). With a requestId it
144
+ * closes that prompt's request-level notifications; without one (a session
145
+ * whose prompts all settled) it closes the session-level ones. A target
146
+ * without a session id closes no push (a request-only target retires the
147
+ * banners of that request).
148
+ * @param {{ownerId: ?string, isMultiUser: ?function, listAdmins: ?function}} [audience]
149
+ * the same audience the notification was sent to.
150
+ * @returns {number} how many close pushes were sent.
151
+ */
152
+ function resolve(what, audience) {
153
+ var all = [].concat(what).map(target.makeTarget).filter(Boolean);
154
+ var norms = all.filter(function (n) { return n.sessionId; });
155
+ var closing = closeGroups(norms);
156
+ if (inApp && closing.bannerKeys.length) inApp.dismissKeys(closing.bannerKeys);
157
+ // An answer that arrives for a prompt no session claims (a stale one) names
158
+ // only its request: the banners of that request are retired, in its project.
159
+ if (inApp && typeof inApp.dismissRequest === "function") {
160
+ all.filter(function (n) { return !n.sessionId && n.requestId; }).forEach(function (n) {
161
+ inApp.dismissRequest(n.slug, n.requestId);
162
+ });
163
+ }
164
+ if (!push) return 0;
165
+ var aud = audienceOf(audience);
166
+ closing.groups.forEach(function (g) {
167
+ deliver(aud, payloads.buildClose(g.type, g.tags));
168
+ });
169
+ return closing.groups.length;
170
+ }
171
+
172
+ return { notify: notify, resolve: resolve };
173
+ }
174
+
175
+ module.exports = { createNotifyCore: createNotifyCore };
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ // The notification core's public surface. notifyFor(push, inApp) returns the
4
+ // core bound to a push module and the in-app banner store.
5
+
6
+ var { createTypeRegistry } = require("./type-registry");
7
+ var { registerBuiltins } = require("./types");
8
+ var { createNotifyCore } = require("./core");
9
+ var target = require("./target");
10
+
11
+ var registry = registerBuiltins(createTypeRegistry());
12
+
13
+ /**
14
+ * The core is stateless (every tag derives from the target), so a notification
15
+ * raised through one instance is closed through another.
16
+ * @param {?object} push the push module; null for no push.
17
+ * @param {?object} [inApp] the in-app banner store (lib/project-notifications.js).
18
+ */
19
+ function notifyFor(push, inApp) {
20
+ return createNotifyCore({ registry: registry, push: push || null, inApp: inApp || null });
21
+ }
22
+
23
+ module.exports = {
24
+ notifyFor: notifyFor,
25
+ registerType: registry.register,
26
+ getType: registry.get,
27
+ targetKey: target.targetKey,
28
+ makeTarget: target.makeTarget,
29
+ createNotifyCore: createNotifyCore,
30
+ createTypeRegistry: createTypeRegistry,
31
+ };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ // The record the page checks a shown prompt notification against: which
4
+ // prompts of a project await the operator right now, as seen by one viewer.
5
+ // Ids only, never prompt content. It is what makes every persistent
6
+ // close-on-resolve type verifiable on the page, including those with no banner
7
+ // (ask_user, elicitation).
8
+
9
+ var { audienceFor } = require("../push-audience");
10
+
11
+ /**
12
+ * @param {object} deps
13
+ * @param {string} deps.slug the project the sessions belong to.
14
+ * @param {{forEach: function}} deps.sessions the project's sessions.
15
+ * @param {{pendingInfo: function}} deps.prompts the project's prompt registry.
16
+ * @param {?string} deps.viewerId the connecting user, if any.
17
+ * @param {function(): boolean} [deps.isMultiUser] see push-audience.audienceFor.
18
+ * @param {function(): string[]} [deps.listAdmins] see push-audience.audienceFor.
19
+ * @returns {{slug: string, at: number, items: Array<{sessionId: string, requestId: string, toolUseId: ?string}>}}
20
+ * at is the server time of the snapshot: a notification sent after it is not
21
+ * judged by it.
22
+ */
23
+ function snapshotLivePrompts(deps) {
24
+ var items = [];
25
+ deps.sessions.forEach(function (session) {
26
+ // Only prompts the viewer would have been pushed.
27
+ var audience = audienceFor(session.ownerId || null, deps.isMultiUser, deps.listAdmins);
28
+ if (audience.kind !== "broadcast" && audience.userIds.indexOf(deps.viewerId) === -1) return;
29
+ deps.prompts.pendingInfo(session).forEach(function (info) {
30
+ items.push({ sessionId: String(session.localId), requestId: info.requestId, toolUseId: info.toolUseId || null });
31
+ });
32
+ });
33
+ return { slug: deps.slug, at: Date.now(), items: items };
34
+ }
35
+
36
+ module.exports = { snapshotLivePrompts: snapshotLivePrompts };
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+
3
+ // Builds the wire payloads. The tag is derived here and nowhere else: from the
4
+ // type's collapse rule and the target key, so two notifications for the same
5
+ // target share a tag (the platform replaces, never stacks) and different
6
+ // targets never do.
7
+
8
+ var target = require("./target");
9
+
10
+ // Marks a payload the generic service worker handles from its own fields. A
11
+ // payload without it is a legacy one, shown as before.
12
+ var CORE_MARK = 1;
13
+ var CLOSE_TYPE = "notify_close";
14
+
15
+ function tagFor(type, t) {
16
+ var key = type.collapse === "request" ? target.targetKey(t) : target.sessionKey(t);
17
+ return "n:" + type.name + ":" + key;
18
+ }
19
+
20
+ /**
21
+ * @param {object} type a registered type.
22
+ * @param {object} t a normalised target.
23
+ * @param {{title: string, body: string}} content
24
+ */
25
+ function buildPayload(type, t, content) {
26
+ var text = type.format(content || {});
27
+ return {
28
+ core: CORE_MARK,
29
+ type: type.name,
30
+ title: text.title,
31
+ body: text.body,
32
+ target: t,
33
+ tag: tagFor(type, t),
34
+ collapse: type.collapse,
35
+ url: target.urlForTarget(t),
36
+ requireInteraction: type.persistent,
37
+ renotify: type.renotify,
38
+ liveInState: type.liveInState,
39
+ // Server time of the send: the page's reconcile only judges a notification
40
+ // sent before the state snapshot it checks against.
41
+ sentAt: Date.now(),
42
+ };
43
+ }
44
+
45
+ /**
46
+ * The push that closes a shown notification. It names the type it closes so
47
+ * the push filter applies that type's preference: a device never told of the
48
+ * notification is not told of its closing.
49
+ * @param {object} type a type of the category being closed.
50
+ * @param {string|string[]} tags the tag or tags to close.
51
+ */
52
+ function buildClose(type, tags) {
53
+ return {
54
+ core: CORE_MARK,
55
+ type: CLOSE_TYPE,
56
+ closes: type.name,
57
+ tags: [].concat(tags),
58
+ title: "Clagentic: Console",
59
+ body: "No longer waiting for input",
60
+ };
61
+ }
62
+
63
+ module.exports = { tagFor: tagFor, buildPayload: buildPayload, buildClose: buildClose, CLOSE_TYPE: CLOSE_TYPE };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ // The target model is defined once, in the file the browser and the service
4
+ // worker load too, so the server and the client cannot disagree on identity.
5
+ module.exports = require("../public/notify-target.js");
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ // The notification types the core knows. A new type is one register() call:
4
+ // the core derives its tag, payload and close behaviour from what is declared
5
+ // here, so no emitter carries any of it.
6
+
7
+ var COLLAPSE = ["request", "session"];
8
+
9
+ function defaultFormat(content) {
10
+ return { title: content.title, body: content.body };
11
+ }
12
+
13
+ function createTypeRegistry() {
14
+ var types = Object.create(null);
15
+
16
+ /**
17
+ * @param {string} name the push payload type.
18
+ * @param {object} def
19
+ * @param {string} def.category the notification-prefs category (documentation
20
+ * of what lib/notification-prefs.js filters the type under).
21
+ * @param {"request"|"session"} def.collapse "request": one notification per
22
+ * prompt; "session": one per session, a repeat replaces it.
23
+ * @param {boolean} def.persistent stays on screen until acted on.
24
+ * @param {boolean} [def.renotify] a replacement alerts again.
25
+ * @param {boolean} [def.closeOnResolve] the notification is closed when its
26
+ * target resolves (default true).
27
+ * @param {boolean} [def.banner] the type also shows as an in-app banner,
28
+ * closed with the notification (default false).
29
+ * @param {boolean} [def.liveInState] the page can verify the notification
30
+ * against notifications_state (default false).
31
+ * @param {function(object): {title: string, body: string}} [def.format]
32
+ */
33
+ function register(name, def) {
34
+ if (typeof name !== "string" || !name) throw new Error("notification type needs a name");
35
+ if (types[name]) throw new Error("notification type already registered: " + name);
36
+ if (!def || COLLAPSE.indexOf(def.collapse) === -1) {
37
+ throw new Error("notification type " + name + " needs collapse: request|session");
38
+ }
39
+ types[name] = Object.freeze({
40
+ name: name,
41
+ category: def.category || null,
42
+ collapse: def.collapse,
43
+ persistent: def.persistent === true,
44
+ renotify: def.renotify === true,
45
+ closeOnResolve: def.closeOnResolve !== false,
46
+ banner: def.banner === true,
47
+ liveInState: def.liveInState === true,
48
+ format: typeof def.format === "function" ? def.format : defaultFormat,
49
+ });
50
+ return types[name];
51
+ }
52
+
53
+ function get(name) {
54
+ return typeof name === "string" && Object.prototype.hasOwnProperty.call(types, name) ? types[name] : null;
55
+ }
56
+
57
+ function names() {
58
+ return Object.keys(types);
59
+ }
60
+
61
+ return { register: register, get: get, names: names };
62
+ }
63
+
64
+ module.exports = { createTypeRegistry: createTypeRegistry, COLLAPSE: COLLAPSE };
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ // The notification types registered with the core. Adding a type is adding an
4
+ // entry here; emitters then call notify()/resolve() with its name.
5
+
6
+ var PROMPT_TYPES = {
7
+ permission_request: { category: "approval", collapse: "request", persistent: true, banner: true, liveInState: true },
8
+ permission_reminder: { category: "approval_reminder", collapse: "session", persistent: true, renotify: true, banner: true, liveInState: true },
9
+ ask_user: { category: "approval", collapse: "request", persistent: true, liveInState: true },
10
+ elicitation: { category: "approval", collapse: "request", persistent: true, liveInState: true },
11
+ };
12
+
13
+ function registerBuiltins(registry) {
14
+ Object.keys(PROMPT_TYPES).forEach(function (name) {
15
+ registry.register(name, PROMPT_TYPES[name]);
16
+ });
17
+ return registry;
18
+ }
19
+
20
+ module.exports = { registerBuiltins: registerBuiltins, PROMPT_TYPES: PROMPT_TYPES };
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ // Open browser sockets per project, for the activity diagnostics. A tab holds
4
+ // one socket, so a project showing more sockets than tabs is the sign of an
5
+ // orphaned connection. The count is the daemon's own and holds no connection
6
+ // detail: only the project slug and a number.
7
+
8
+ var counts = new Map();
9
+
10
+ function opened(slug) {
11
+ counts.set(slug, (counts.get(slug) || 0) + 1);
12
+ }
13
+
14
+ function closed(slug) {
15
+ var n = (counts.get(slug) || 0) - 1;
16
+ if (n > 0) counts.set(slug, n);
17
+ else counts.delete(slug);
18
+ }
19
+
20
+ // { slug: openSocketCount } for every project with at least one open socket.
21
+ function snapshot() {
22
+ var out = {};
23
+ counts.forEach(function (n, slug) { out[slug] = n; });
24
+ return out;
25
+ }
26
+
27
+ module.exports = { opened: opened, closed: closed, snapshot: snapshot };
@@ -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 };