@clagentic/console 1.10.0-beta.2 → 1.10.0-beta.4
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/lib/activity-clock.js +23 -0
- package/lib/build-sha.json +2 -2
- package/lib/build-update-offer.js +29 -0
- package/lib/cli-transcript-title.js +131 -0
- package/lib/daemon.js +5 -9
- package/lib/debug-log.js +17 -0
- package/lib/exit-flush.js +44 -0
- package/lib/grant-keys.js +66 -0
- package/lib/identity/config.js +118 -0
- package/lib/identity/contract.js +61 -0
- package/lib/identity/delivery.js +224 -0
- package/lib/identity/holder.js +87 -0
- package/lib/identity/index.js +229 -0
- package/lib/identity/jose.js +13 -0
- package/lib/identity/origin.js +28 -0
- package/lib/identity/owner.js +30 -0
- package/lib/identity/providers/gatekeeper.js +91 -0
- package/lib/identity/providers/jwks-self.js +85 -0
- package/lib/identity/providers/module.js +98 -0
- package/lib/identity/providers/signing-key.js +118 -0
- package/lib/identity/record.js +23 -0
- package/lib/identity/unavailable.js +29 -0
- package/lib/log-field.js +34 -0
- package/lib/project-http.js +2 -0
- package/lib/project-notifications.js +3 -0
- package/lib/project-prompt-responses.js +2 -1
- package/lib/project-session-grants.js +73 -0
- package/lib/project-sessions.js +12 -0
- package/lib/project-user-message.js +6 -5
- package/lib/project.js +2 -0
- package/lib/prompt-label.js +60 -0
- package/lib/prompt-registry.js +105 -10
- package/lib/prompt-source.js +65 -0
- package/lib/public/css/notifications-center.css +9 -0
- package/lib/public/css/overlays.css +18 -0
- package/lib/public/css/rewind.css +7 -0
- package/lib/public/modules/app-messages.js +3 -0
- package/lib/public/modules/app-notifications.js +5 -0
- package/lib/public/modules/build-notice-gate.js +73 -0
- package/lib/public/modules/diagnostics.js +43 -1
- package/lib/public/modules/prompt-label.js +44 -0
- package/lib/public/modules/prompts.js +9 -0
- package/lib/public/modules/session-grants.js +131 -0
- package/lib/public/modules/sidebar-sessions.js +11 -0
- package/lib/runtime-env.js +1 -1
- package/lib/sdk-bridge.js +135 -104
- package/lib/sdk-message-processor.js +14 -67
- package/lib/session-ai-title.js +80 -0
- package/lib/session-input.js +32 -2
- package/lib/sessions.js +21 -18
- package/lib/turn-initiator.js +29 -14
- package/lib/utils.js +5 -46
- package/lib/ws-schema.js +6 -3
- package/lib/yoke/adapters/claude-worker.js +5 -5
- package/lib/yoke/adapters/claude.js +5 -62
- package/lib/yoke/adapters/codex.js +4 -75
- package/lib/yoke/interface.js +3 -7
- package/package.json +3 -1
- package/scripts/vendor-identity-vectors.js +52 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Class 1: an attestation provider that issues Identity Assertions.
|
|
4
|
+
//
|
|
5
|
+
// Transport. The console runs the issuer's published client, `gatekeeper hook
|
|
6
|
+
// process`, documented for a launcher that starts a runtime as a subprocess. The
|
|
7
|
+
// socket protocol itself is not part of the published contract documents, so it
|
|
8
|
+
// is not spoken here.
|
|
9
|
+
//
|
|
10
|
+
// Launcher binding. The issuer binds the assertion to the runtime above the
|
|
11
|
+
// requesting process, which for the process adapter is its parent: the console
|
|
12
|
+
// process that starts the session. The client is therefore started directly by
|
|
13
|
+
// this process (execFile, no shell, no wrapper), never through `sh -c`.
|
|
14
|
+
//
|
|
15
|
+
// Origin passing. The origin is handed over as `--origin`. The adapter does not
|
|
16
|
+
// issue the origin "human" (follow-up in the issuer: lr-f184b8), so for a typed
|
|
17
|
+
// session no client is run at all: the session starts without an identity, with
|
|
18
|
+
// the stable reason code origin_human_not_issued and no notice, and is graded
|
|
19
|
+
// declared by consumers. It is never given an identity claiming another origin.
|
|
20
|
+
// The runtime id is not sent: the issuer derives `rt` from the launcher itself.
|
|
21
|
+
|
|
22
|
+
var childProcess = require("child_process");
|
|
23
|
+
var contract = require("../contract");
|
|
24
|
+
var unavailableModule = require("../unavailable");
|
|
25
|
+
|
|
26
|
+
var unavailable = unavailableModule.unavailable;
|
|
27
|
+
|
|
28
|
+
var OUTPUT_PATTERN = new RegExp("^" + contract.IDENTITY_DIR_ENV + "=(/[A-Za-z0-9._/+-]+)\\n?$");
|
|
29
|
+
var MAX_OUTPUT_BYTES = 4096;
|
|
30
|
+
var HUMAN_ORIGIN_CODE = "origin_human_not_issued";
|
|
31
|
+
|
|
32
|
+
function clientArgs(cfg, definition, origin) {
|
|
33
|
+
var args = ["hook", "process", "--socket", cfg.socket];
|
|
34
|
+
if (contract.matches(contract.NAME_PATTERN, definition)) args.push("--definition", definition);
|
|
35
|
+
if (origin) args.push("--origin", origin);
|
|
36
|
+
return args;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function createGatekeeperProvider(cfg, deps) {
|
|
40
|
+
var execFile = (deps && deps.execFile) || childProcess.execFile;
|
|
41
|
+
var warn = (deps && deps.warn) || console.warn;
|
|
42
|
+
|
|
43
|
+
function obtainSession(definition, origin, rt, ctx) {
|
|
44
|
+
// The issuer never issues a session for a typed turn; the human signal is the
|
|
45
|
+
// per-turn origin, not the session assertion. Asking would only fail, so no
|
|
46
|
+
// client is run and the absence is expected (no notice), never worked around
|
|
47
|
+
// by another class.
|
|
48
|
+
if (origin === "human") {
|
|
49
|
+
return Promise.resolve(unavailable(HUMAN_ORIGIN_CODE,
|
|
50
|
+
"the issuer does not issue sessions for a human-typed turn", { quiet: true }));
|
|
51
|
+
}
|
|
52
|
+
if (ctx && ctx.owner && ctx.owner.uid !== process.getuid()) {
|
|
53
|
+
return Promise.resolve(unavailable("owner_unsupported",
|
|
54
|
+
"the issuer delivers to the console's own user only, not to a per-user OS account"));
|
|
55
|
+
}
|
|
56
|
+
var env = Object.assign({}, process.env);
|
|
57
|
+
delete env[contract.IDENTITY_DIR_ENV];
|
|
58
|
+
return new Promise(function (resolve) {
|
|
59
|
+
execFile(cfg.binary, clientArgs(cfg, definition, origin), {
|
|
60
|
+
env: env,
|
|
61
|
+
timeout: cfg.timeoutMs,
|
|
62
|
+
maxBuffer: MAX_OUTPUT_BYTES,
|
|
63
|
+
signal: ctx && ctx.signal,
|
|
64
|
+
windowsHide: true,
|
|
65
|
+
}, function (err, stdout, stderr) {
|
|
66
|
+
if (err) {
|
|
67
|
+
var detail = String(stderr || err.message || "").trim().split("\n")[0].slice(0, 200);
|
|
68
|
+
warn("[identity] gatekeeper client failed: " + (err.code === "ENOENT" ? "executable not found" : detail));
|
|
69
|
+
if (err.code === "ENOENT") {
|
|
70
|
+
resolve(unavailable("issuer_client_missing", "the identity issuer's client program was not found"));
|
|
71
|
+
} else if (err.killed) {
|
|
72
|
+
resolve(unavailable("issuer_timeout", "the identity issuer did not answer in time"));
|
|
73
|
+
} else {
|
|
74
|
+
resolve(unavailable("issuer_unavailable", "the identity issuer could not issue a session"));
|
|
75
|
+
}
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
var match = OUTPUT_PATTERN.exec(String(stdout));
|
|
79
|
+
if (!match) {
|
|
80
|
+
resolve(unavailable("issuer_output_invalid", "the identity issuer's client answered in a form the console does not recognise"));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
resolve({ provider: contract.PROVIDERS.GATEKEEPER, dir: match[1] });
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return { name: contract.PROVIDERS.GATEKEEPER, obtainSession: obtainSession };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
module.exports = { createGatekeeperProvider: createGatekeeperProvider };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Class 2: the console is its own issuer. It signs contract-conformant session
|
|
4
|
+
// assertions with its own key and publishes the matching key set, so any verifier
|
|
5
|
+
// that trusts this console's `iss` can check them with a standard JWS library and
|
|
6
|
+
// no other software installed.
|
|
7
|
+
//
|
|
8
|
+
// What this is, and is not. The console asserts what it knows about its own
|
|
9
|
+
// session. Its key is readable by the console's own user, so the assertion is
|
|
10
|
+
// attribution-grade (grade "harness": an actor that exists inside a host process,
|
|
11
|
+
// asserted by that host) and never workload-grade.
|
|
12
|
+
//
|
|
13
|
+
// No human. The console never signs a `human:` subject or actor. `sub` is the
|
|
14
|
+
// runtime and `act.sub` the persona agent when the session has one, else the
|
|
15
|
+
// runtime again. That a person typed the turn is carried by the `origin` claim
|
|
16
|
+
// only.
|
|
17
|
+
|
|
18
|
+
var crypto = require("crypto");
|
|
19
|
+
var contract = require("../contract");
|
|
20
|
+
var signingKey = require("./signing-key");
|
|
21
|
+
var unavailableModule = require("../unavailable");
|
|
22
|
+
var pkg = require("../../../package.json");
|
|
23
|
+
|
|
24
|
+
var unavailable = unavailableModule.unavailable;
|
|
25
|
+
|
|
26
|
+
function runtimeActor(rt) {
|
|
27
|
+
return "runtime:" + rt;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function buildClaims(cfg, definition, origin, rt, dir) {
|
|
31
|
+
var now = Math.floor(Date.now() / 1000);
|
|
32
|
+
var hasDefinition = contract.matches(contract.NAME_PATTERN, definition);
|
|
33
|
+
return {
|
|
34
|
+
iss: cfg.issuer,
|
|
35
|
+
sub: runtimeActor(rt),
|
|
36
|
+
act: { sub: hasDefinition ? "agent:" + definition : runtimeActor(rt) },
|
|
37
|
+
roles: cfg.roles.slice(),
|
|
38
|
+
grade: "harness",
|
|
39
|
+
rt: { id: rt, version: pkg.version },
|
|
40
|
+
sid: crypto.randomUUID(),
|
|
41
|
+
spawn: crypto.randomUUID(),
|
|
42
|
+
origin: origin,
|
|
43
|
+
dir: dir,
|
|
44
|
+
iat: now,
|
|
45
|
+
exp: now + cfg.ttlSeconds,
|
|
46
|
+
jti: crypto.randomUUID(),
|
|
47
|
+
cia: contract.CONTRACT_MAJOR,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function createJwksSelfProvider(cfg) {
|
|
52
|
+
var signerPromise = null;
|
|
53
|
+
|
|
54
|
+
function signer() {
|
|
55
|
+
if (!signerPromise) {
|
|
56
|
+
signerPromise = signingKey.loadSigner(cfg).catch(function (err) {
|
|
57
|
+
// A failure is not cached: fixing the key and starting the next session works.
|
|
58
|
+
signerPromise = null;
|
|
59
|
+
throw err;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return signerPromise;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function obtainSession(definition, origin, rt, ctx) {
|
|
66
|
+
if (!contract.matches(contract.NAME_PATTERN, rt)) return unavailable("runtime_invalid", "the runtime id is not a valid actor name");
|
|
67
|
+
if (contract.ORIGINS.indexOf(origin) === -1) return unavailable("origin_invalid", "the turn's origin is not one the contract defines");
|
|
68
|
+
var s;
|
|
69
|
+
try {
|
|
70
|
+
s = await signer();
|
|
71
|
+
} catch (err) {
|
|
72
|
+
if (err && err.identityCode) return unavailable(err.identityCode, err.message);
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
var dir = await ctx.allocateDir();
|
|
76
|
+
var assertion = await new s.jose.SignJWT(buildClaims(cfg, definition, origin, rt, dir))
|
|
77
|
+
.setProtectedHeader({ alg: s.alg, kid: s.kid })
|
|
78
|
+
.sign(s.privateKey);
|
|
79
|
+
return { provider: contract.PROVIDERS.JWKS_SELF, dir: dir, assertion: assertion };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return { name: contract.PROVIDERS.JWKS_SELF, obtainSession: obtainSession };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
module.exports = { createJwksSelfProvider: createJwksSelfProvider };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// A user-supplied provider (for example a client of a workload identity API),
|
|
4
|
+
// named by identity.provider_module as an absolute path. The module exports an
|
|
5
|
+
// object, or a function returning one, with:
|
|
6
|
+
//
|
|
7
|
+
// name recorded on the session; plain name
|
|
8
|
+
// obtainSession(definition, origin, rt, ctx)
|
|
9
|
+
// returns { dir, assertion? } or a typed unavailability
|
|
10
|
+
// (ctx.unavailable(code, reason, { quiet? })). `dir` is an absolute
|
|
11
|
+
// directory laid out as the contract's section 6 says, either one the
|
|
12
|
+
// module made or one it got from ctx.allocateDir(); `assertion`, when
|
|
13
|
+
// given, is written into it as identity.jwt.
|
|
14
|
+
// release(info) optional, called at most once when the
|
|
15
|
+
// session ends; the console does not wait for it. A directory from
|
|
16
|
+
// ctx.allocateDir() is removed by the console; a directory the module
|
|
17
|
+
// made is the module's, and the console removes nothing in it (end it in
|
|
18
|
+
// release()).
|
|
19
|
+
//
|
|
20
|
+
// ctx: { allocateDir(): string, owner: { uid, gid } | null,
|
|
21
|
+
// signal: AbortSignal, unavailable(code, reason, options): object }
|
|
22
|
+
//
|
|
23
|
+
// The module runs with the console's authority: it is code the person running the
|
|
24
|
+
// console chose, in the same way as the rest of the console's configuration.
|
|
25
|
+
|
|
26
|
+
var path = require("path");
|
|
27
|
+
var url = require("url");
|
|
28
|
+
var contract = require("../contract");
|
|
29
|
+
var unavailableModule = require("../unavailable");
|
|
30
|
+
|
|
31
|
+
var unavailable = unavailableModule.unavailable;
|
|
32
|
+
|
|
33
|
+
var RESERVED_NAMES = Object.keys(contract.PROVIDERS).map(function (k) { return contract.PROVIDERS[k]; });
|
|
34
|
+
var DEFAULT_NAME = "module";
|
|
35
|
+
|
|
36
|
+
async function loadModule(modulePath) {
|
|
37
|
+
var loaded = await import(url.pathToFileURL(path.normalize(modulePath)).href);
|
|
38
|
+
var exported = loaded.default !== undefined ? loaded.default : loaded;
|
|
39
|
+
if (typeof exported === "function") exported = await exported();
|
|
40
|
+
if (!exported || typeof exported.obtainSession !== "function") {
|
|
41
|
+
throw new Error("the module does not export obtainSession");
|
|
42
|
+
}
|
|
43
|
+
return exported;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function createModuleProvider(cfg, deps) {
|
|
47
|
+
var warn = (deps && deps.warn) || console.warn;
|
|
48
|
+
var loading = null;
|
|
49
|
+
|
|
50
|
+
function load() {
|
|
51
|
+
if (!loading) {
|
|
52
|
+
loading = loadModule(cfg.modulePath).catch(function (err) {
|
|
53
|
+
loading = null;
|
|
54
|
+
warn("[identity] provider module could not be loaded: " + (err && err.message));
|
|
55
|
+
return null;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return loading;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A reserved name would let a module pass its own sessions off as ones a
|
|
62
|
+
// built-in provider vouched for.
|
|
63
|
+
function nameOf(mod) {
|
|
64
|
+
var name = mod.name;
|
|
65
|
+
if (!contract.matches(contract.NAME_PATTERN, name) || RESERVED_NAMES.indexOf(name) !== -1) {
|
|
66
|
+
return DEFAULT_NAME;
|
|
67
|
+
}
|
|
68
|
+
return name;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function obtainSession(definition, origin, rt, ctx) {
|
|
72
|
+
var mod = await load();
|
|
73
|
+
if (!mod) return unavailable("module_load_failed", "the identity provider module could not be loaded");
|
|
74
|
+
var result;
|
|
75
|
+
try {
|
|
76
|
+
result = await mod.obtainSession(definition, origin, rt, ctx);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
warn("[identity] provider module failed: " + (err && err.message));
|
|
79
|
+
return unavailable("provider_error", "the identity provider module failed");
|
|
80
|
+
}
|
|
81
|
+
if (unavailableModule.isUnavailable(result)) return unavailable(result.code, result.reason, { quiet: result.quiet });
|
|
82
|
+
if (!result || typeof result.dir !== "string") {
|
|
83
|
+
return unavailable("provider_result_invalid", "the identity provider module returned no directory");
|
|
84
|
+
}
|
|
85
|
+
return { provider: nameOf(mod), dir: result.dir, assertion: result.assertion };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function release(info) {
|
|
89
|
+
return load().then(function (mod) {
|
|
90
|
+
if (mod && typeof mod.release === "function") return mod.release(info);
|
|
91
|
+
return undefined;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return { name: DEFAULT_NAME, obtainSession: obtainSession, release: release };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
module.exports = { createModuleProvider: createModuleProvider };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The console's own signing key and the key set it publishes, for the jwks-self
|
|
4
|
+
// provider. The key is an Ed25519 key in a PKCS#8 PEM file; the public half is
|
|
5
|
+
// published as a JWKS document (contract section 5.2: no private parameters).
|
|
6
|
+
|
|
7
|
+
var fs = require("fs");
|
|
8
|
+
var path = require("path");
|
|
9
|
+
var crypto = require("crypto");
|
|
10
|
+
var joseLoader = require("../jose");
|
|
11
|
+
|
|
12
|
+
var ALG = "EdDSA";
|
|
13
|
+
|
|
14
|
+
function keyError(code, message) {
|
|
15
|
+
var err = new Error(message);
|
|
16
|
+
err.identityCode = code;
|
|
17
|
+
return err;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function readPrivateKey(keyPath) {
|
|
21
|
+
var fd;
|
|
22
|
+
try {
|
|
23
|
+
fd = fs.openSync(keyPath, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
if (e.code === "ENOENT") return null;
|
|
26
|
+
throw keyError("key_unreadable", "the identity signing key could not be opened");
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
var st = fs.fstatSync(fd);
|
|
30
|
+
if (!st.isFile()) throw keyError("key_unsafe", "the identity signing key is not a regular file");
|
|
31
|
+
if (st.uid !== process.getuid() || (st.mode & 0o077) !== 0) {
|
|
32
|
+
throw keyError("key_unsafe", "the identity signing key must be owned by the console user and private to it");
|
|
33
|
+
}
|
|
34
|
+
return crypto.createPrivateKey(fs.readFileSync(fd, "utf8"));
|
|
35
|
+
} catch (e) {
|
|
36
|
+
if (e.identityCode) throw e;
|
|
37
|
+
throw keyError("key_unreadable", "the identity signing key could not be read as a PEM private key");
|
|
38
|
+
} finally {
|
|
39
|
+
fs.closeSync(fd);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
var PEER_READ_ATTEMPTS = 10;
|
|
44
|
+
var PEER_READ_WAIT_MS = 20;
|
|
45
|
+
|
|
46
|
+
function waitSync(ms) {
|
|
47
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// A peer that won the exclusive create may still be writing: its file is read
|
|
51
|
+
// again a few times before it is called unreadable.
|
|
52
|
+
function readPeerKey(keyPath) {
|
|
53
|
+
for (var attempt = 1; ; attempt++) {
|
|
54
|
+
try {
|
|
55
|
+
var key = readPrivateKey(keyPath);
|
|
56
|
+
if (key) return key;
|
|
57
|
+
} catch (e) {
|
|
58
|
+
if (e.identityCode !== "key_unreadable" || attempt >= PEER_READ_ATTEMPTS) throw e;
|
|
59
|
+
}
|
|
60
|
+
if (attempt >= PEER_READ_ATTEMPTS) throw keyError("key_unreadable", "the identity signing key could not be read");
|
|
61
|
+
waitSync(PEER_READ_WAIT_MS);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Creates the key when no file is there and returns the key that is in the file
|
|
66
|
+
// afterwards: when another process created it first, that process's key.
|
|
67
|
+
function createKeyIfAbsent(keyPath) {
|
|
68
|
+
var pair = crypto.generateKeyPairSync("ed25519");
|
|
69
|
+
fs.mkdirSync(path.dirname(keyPath), { recursive: true, mode: 0o700 });
|
|
70
|
+
try {
|
|
71
|
+
// The file is created private (mode at creation, exclusive) so the key is
|
|
72
|
+
// never readable by anyone else, even for an instant.
|
|
73
|
+
fs.writeFileSync(keyPath, pair.privateKey.export({ type: "pkcs8", format: "pem" }), { flag: "wx", mode: 0o600 });
|
|
74
|
+
} catch (e) {
|
|
75
|
+
if (e.code !== "EEXIST") throw e;
|
|
76
|
+
return readPeerKey(keyPath);
|
|
77
|
+
}
|
|
78
|
+
return pair.privateKey;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function requireEd25519(key) {
|
|
82
|
+
if (key.asymmetricKeyType !== "ed25519") {
|
|
83
|
+
throw keyError("key_unsupported", "the identity signing key must be an Ed25519 key");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Writes the JWKS only when its content changed, atomically, readable by others
|
|
88
|
+
// (a verifier under another user reads it) and writable by the console only.
|
|
89
|
+
function publishJwks(jwksPath, jwks) {
|
|
90
|
+
var text = JSON.stringify(jwks, null, 2) + "\n";
|
|
91
|
+
try {
|
|
92
|
+
if (fs.readFileSync(jwksPath, "utf8") === text) return;
|
|
93
|
+
} catch (e) {
|
|
94
|
+
// Not published yet, or unreadable: written below.
|
|
95
|
+
}
|
|
96
|
+
fs.mkdirSync(path.dirname(jwksPath), { recursive: true, mode: 0o755 });
|
|
97
|
+
var staging = jwksPath + ".tmp-" + process.pid;
|
|
98
|
+
fs.writeFileSync(staging, text, { flag: "w", mode: 0o644 });
|
|
99
|
+
fs.renameSync(staging, jwksPath);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Loads (and, for the default location, creates) the key, publishes its JWKS and
|
|
103
|
+
// returns what signing needs.
|
|
104
|
+
async function loadSigner(cfg) {
|
|
105
|
+
var privateKey = readPrivateKey(cfg.signingKey);
|
|
106
|
+
if (!privateKey) {
|
|
107
|
+
if (!cfg.generateKey) throw keyError("key_missing", "the identity signing key does not exist");
|
|
108
|
+
privateKey = createKeyIfAbsent(cfg.signingKey);
|
|
109
|
+
}
|
|
110
|
+
requireEd25519(privateKey);
|
|
111
|
+
var jose = await joseLoader.loadJose();
|
|
112
|
+
var jwk = crypto.createPublicKey(privateKey).export({ format: "jwk" });
|
|
113
|
+
var kid = await jose.calculateJwkThumbprint(jwk);
|
|
114
|
+
publishJwks(cfg.jwksPath, { keys: [Object.assign({}, jwk, { kid: kid, alg: ALG, use: "sig" })] });
|
|
115
|
+
return { privateKey: privateKey, kid: kid, alg: ALG, jose: jose };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
module.exports = { loadSigner: loadSigner, createKeyIfAbsent: createKeyIfAbsent, ALG: ALG };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The identity record a session carries: which provider vouched for it and at
|
|
4
|
+
// what grade. Read back from a session file, so it is rebuilt from known values
|
|
5
|
+
// only, whatever the file holds.
|
|
6
|
+
|
|
7
|
+
var contract = require("./contract");
|
|
8
|
+
var CODE_PATTERN = require("./unavailable").CODE_PATTERN;
|
|
9
|
+
|
|
10
|
+
function sanitizeRecord(value) {
|
|
11
|
+
if (!value || typeof value !== "object") return null;
|
|
12
|
+
var provider = value.provider;
|
|
13
|
+
if (!contract.matches(contract.NAME_PATTERN, provider)) return null;
|
|
14
|
+
var record = {
|
|
15
|
+
provider: provider,
|
|
16
|
+
grade: contract.GRADES.indexOf(value.grade) === -1 ? null : value.grade,
|
|
17
|
+
origin: contract.ORIGINS.indexOf(value.origin) === -1 ? null : value.origin,
|
|
18
|
+
};
|
|
19
|
+
if (contract.matches(CODE_PATTERN, value.unavailable)) record.unavailable = value.unavailable;
|
|
20
|
+
return record;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = { sanitizeRecord: sanitizeRecord };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The typed result of a provider that cannot supply an identity. A session that
|
|
4
|
+
// receives it starts without one: nothing is fabricated in its place.
|
|
5
|
+
//
|
|
6
|
+
// code a short stable identifier (lower-case letters, digits, underscore)
|
|
7
|
+
// reason one sentence for the person running the console, no secrets and no
|
|
8
|
+
// host paths
|
|
9
|
+
// quiet true when the absence is expected rather than a fault (a runtime the
|
|
10
|
+
// surface does not cover), so no notice is shown for it
|
|
11
|
+
|
|
12
|
+
var contract = require("./contract");
|
|
13
|
+
|
|
14
|
+
var CODE_PATTERN = /^[a-z][a-z0-9_]{0,63}$/;
|
|
15
|
+
|
|
16
|
+
function unavailable(code, reason, options) {
|
|
17
|
+
return {
|
|
18
|
+
unavailable: true,
|
|
19
|
+
code: contract.matches(CODE_PATTERN, code) ? code : "provider_error",
|
|
20
|
+
reason: typeof reason === "string" && reason ? reason.slice(0, 200) : "the identity provider could not supply an identity",
|
|
21
|
+
quiet: !!(options && options.quiet),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isUnavailable(value) {
|
|
26
|
+
return !!value && typeof value === "object" && value.unavailable === true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = { unavailable: unavailable, isUnavailable: isUnavailable, CODE_PATTERN: CODE_PATTERN };
|
package/lib/log-field.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// One value for a key=value log line. Session titles, tool names, user ids and
|
|
4
|
+
// project slugs can come from outside the daemon, so nothing that could end a
|
|
5
|
+
// line or split a field survives: every character outside a small allowlist
|
|
6
|
+
// becomes "_", and the value is cut to maxLen. Never pass prompt text through
|
|
7
|
+
// here: the answer to "may this be logged?" is decided by the caller, this
|
|
8
|
+
// only keeps a permitted value on one line.
|
|
9
|
+
|
|
10
|
+
var DISALLOWED = /[^A-Za-z0-9._:@\/+-]/g;
|
|
11
|
+
|
|
12
|
+
// Control characters plus the Unicode line and paragraph separators. Built from
|
|
13
|
+
// char codes so the pattern never holds a raw line-terminator character.
|
|
14
|
+
var LINE_BREAKERS = new RegExp(
|
|
15
|
+
"[" + String.fromCharCode(0) + "-" + String.fromCharCode(0x1f) +
|
|
16
|
+
String.fromCharCode(0x7f) + String.fromCharCode(0x2028) + String.fromCharCode(0x2029) + "]+",
|
|
17
|
+
"g"
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
function logField(value, maxLen) {
|
|
21
|
+
if (value === undefined || value === null || value === "") return "-";
|
|
22
|
+
return String(value).replace(DISALLOWED, "_").slice(0, maxLen || 64);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Free text that has to be readable (a refusal reason), for the END of a line:
|
|
26
|
+
// anything that could end the line or forge another becomes a space, and the
|
|
27
|
+
// text is cut to maxLen.
|
|
28
|
+
function logText(value, maxLen) {
|
|
29
|
+
return String(value === undefined || value === null ? "" : value)
|
|
30
|
+
.replace(LINE_BREAKERS, " ")
|
|
31
|
+
.slice(0, maxLen || 200);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = { logField: logField, logText: logText };
|
package/lib/project-http.js
CHANGED
|
@@ -7,6 +7,7 @@ var { fsAsUser } = require("./os-users");
|
|
|
7
7
|
var usersModule = require("./users");
|
|
8
8
|
var { promptsFor } = require("./prompt-registry");
|
|
9
9
|
var { mayAnswerPrompt } = require("./prompt-access");
|
|
10
|
+
var promptSource = require("./prompt-source");
|
|
10
11
|
var fileScope = require("./project-file-scope");
|
|
11
12
|
|
|
12
13
|
var IMAGE_EXTS = new Set([".png", ".jpg", ".jpeg", ".gif", ".webp", ".svg", ".bmp", ".ico"]);
|
|
@@ -244,6 +245,7 @@ function attachHTTP(ctx) {
|
|
|
244
245
|
: prompts.respond(requestId, { decision: decision }, {
|
|
245
246
|
kinds: ["permission", "plan"],
|
|
246
247
|
denyMessage: "Denied via push notification",
|
|
248
|
+
source: promptSource.fromPush(req, decision),
|
|
247
249
|
});
|
|
248
250
|
if (outcome.status === "resolved") {
|
|
249
251
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
@@ -75,6 +75,9 @@ var formatters = {
|
|
|
75
75
|
// So the banner offers the answers the card does (see prompt-banner.js).
|
|
76
76
|
suppressAlwaysAllowRule: data.suppressAlwaysAllowRule === true ? true : undefined,
|
|
77
77
|
defaultToNo: data.defaultToNo === true ? true : undefined,
|
|
78
|
+
// Who asked: a banner reaches every client whatever project it is
|
|
79
|
+
// viewing.
|
|
80
|
+
label: data.label || null,
|
|
78
81
|
},
|
|
79
82
|
};
|
|
80
83
|
},
|
|
@@ -16,6 +16,7 @@ var promptKinds = require("./prompt-kinds");
|
|
|
16
16
|
var { mayAnswerPrompt } = require("./prompt-access");
|
|
17
17
|
var { ownValue } = require("./prompt-kinds/codecs");
|
|
18
18
|
var { INITIATORS } = require("./turn-initiator");
|
|
19
|
+
var promptSource = require("./prompt-source");
|
|
19
20
|
|
|
20
21
|
// alias type -> { toPrompt(msg), kinds it may answer, stale reply type }
|
|
21
22
|
var LEGACY_RESPONSES = {
|
|
@@ -178,7 +179,7 @@ function attachPromptResponses(ctx) {
|
|
|
178
179
|
function answer(ws, msg, kinds, staleReplyType) {
|
|
179
180
|
var answerable = kinds.every(promptKinds.operatorMayAnswer) && mayAnswer(ws, msg.requestId);
|
|
180
181
|
var outcome = answerable
|
|
181
|
-
? promptsFor(sm).respond(msg.requestId, msg, { session: getSessionForWs(ws), kinds: kinds, responder: ws })
|
|
182
|
+
? promptsFor(sm).respond(msg.requestId, msg, { session: getSessionForWs(ws), kinds: kinds, responder: ws, source: promptSource.fromWs(ws) })
|
|
182
183
|
: { status: "stale" };
|
|
183
184
|
if (outcome.status === "resolved") return;
|
|
184
185
|
// Refused as it stands: still pending, so re-offer it to the responder,
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// WS entry point for a session's "Allow for Session" grants: list them and
|
|
4
|
+
// revoke one. The grants themselves live in the prompt registry
|
|
5
|
+
// (lib/prompt-registry.js, session.allowedTools); this module only maps the
|
|
6
|
+
// wire messages onto it and applies the same access rule as answering a
|
|
7
|
+
// prompt (lib/prompt-access.js), because revoking is the operator's decision
|
|
8
|
+
// about that session and listing shows what its agent may do unasked.
|
|
9
|
+
|
|
10
|
+
var { promptsFor } = require("./prompt-registry");
|
|
11
|
+
var { mayAnswerPrompt } = require("./prompt-access");
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* ctx fields: sm, sendTo, sendToSessionOthers, getSessionForWs
|
|
15
|
+
*/
|
|
16
|
+
function attachSessionGrants(ctx) {
|
|
17
|
+
var sm = ctx.sm;
|
|
18
|
+
var sendTo = ctx.sendTo;
|
|
19
|
+
var sendToSessionOthers = ctx.sendToSessionOthers;
|
|
20
|
+
var getSessionForWs = ctx.getSessionForWs;
|
|
21
|
+
|
|
22
|
+
// The session a message names (the connection's own when it names none), if
|
|
23
|
+
// the connection may act on it.
|
|
24
|
+
function sessionFor(ws, msg) {
|
|
25
|
+
var session = msg.sessionId === undefined || msg.sessionId === null
|
|
26
|
+
? getSessionForWs(ws)
|
|
27
|
+
: (typeof msg.sessionId === "number" && sm.sessions ? sm.sessions.get(msg.sessionId) : null);
|
|
28
|
+
if (!session || !mayAnswerPrompt(ws && ws._clagenticUser, session)) return null;
|
|
29
|
+
return session;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function grantsMessage(session) {
|
|
33
|
+
return { type: "session_grants", sessionId: session.localId, grants: promptsFor(sm).listGrants(session) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Said, not dropped, so the client can re-enable its button. It names nothing
|
|
37
|
+
// about the session, which this user may not see.
|
|
38
|
+
function unavailableMessage(msg) {
|
|
39
|
+
return { type: "session_grants", sessionId: msg.sessionId, error: "that session is not available to you" };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Returns true when msg was a grants message (handled). */
|
|
43
|
+
function handleGrantMessage(ws, msg) {
|
|
44
|
+
if (msg.type === "list_session_grants") {
|
|
45
|
+
var listed = sessionFor(ws, msg);
|
|
46
|
+
if (!listed) {
|
|
47
|
+
sendTo(ws, unavailableMessage(msg));
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
sendTo(ws, grantsMessage(listed));
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (msg.type === "revoke_session_grant") {
|
|
54
|
+
var session = sessionFor(ws, msg);
|
|
55
|
+
if (!session) {
|
|
56
|
+
sendTo(ws, unavailableMessage(msg));
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
promptsFor(sm).revokeGrant(session, msg.key);
|
|
60
|
+
// Every client viewing the session sees the list as it now is, whether
|
|
61
|
+
// or not the key was still there.
|
|
62
|
+
var now = grantsMessage(session);
|
|
63
|
+
sendTo(ws, now);
|
|
64
|
+
sendToSessionOthers(ws, session.localId, now);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return { handleGrantMessage: handleGrantMessage };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = { attachSessionGrants: attachSessionGrants };
|
package/lib/project-sessions.js
CHANGED
|
@@ -11,7 +11,9 @@ var agentsFavorites = require("./agents-favorites");
|
|
|
11
11
|
var sessionActivity = require("./session-activity");
|
|
12
12
|
var { promptsFor } = require("./prompt-registry");
|
|
13
13
|
var { attachPromptResponses } = require("./project-prompt-responses");
|
|
14
|
+
var { attachSessionGrants } = require("./project-session-grants");
|
|
14
15
|
var { ownValue } = require("./prompt-kinds/codecs");
|
|
16
|
+
var { logField, logText } = require("./log-field");
|
|
15
17
|
var { isKnownVendor } = require("./yoke");
|
|
16
18
|
var sessionCapabilities = require("./session-capabilities");
|
|
17
19
|
// Kick off SDK agent discovery in the background at module load.
|
|
@@ -149,6 +151,12 @@ function attachSessions(ctx) {
|
|
|
149
151
|
onProcessingChanged: onProcessingChanged,
|
|
150
152
|
ensureProjectAccessForSession: ensureProjectAccessForSession,
|
|
151
153
|
});
|
|
154
|
+
var sessionGrants = attachSessionGrants({
|
|
155
|
+
sm: sm,
|
|
156
|
+
sendTo: sendTo,
|
|
157
|
+
sendToSessionOthers: sendToSessionOthers,
|
|
158
|
+
getSessionForWs: getSessionForWs,
|
|
159
|
+
});
|
|
152
160
|
|
|
153
161
|
function sessionAcceptsMode(session, mode) {
|
|
154
162
|
if (!ownValue(CAPABILITY_GATED_MODES, mode)) return true;
|
|
@@ -159,6 +167,7 @@ function attachSessions(ctx) {
|
|
|
159
167
|
function handleSessionsMessage(ws, msg) {
|
|
160
168
|
|
|
161
169
|
if (promptResponses.handlePromptMessage(ws, msg)) return true;
|
|
170
|
+
if (sessionGrants.handleGrantMessage(ws, msg)) return true;
|
|
162
171
|
|
|
163
172
|
if (msg.type === "push_subscribe") {
|
|
164
173
|
var _pushUserId = ws._clagenticUser ? ws._clagenticUser.id : null;
|
|
@@ -494,6 +503,9 @@ function attachSessions(ctx) {
|
|
|
494
503
|
require("./session-resume").resumeStoredSession({ sm: sm, adapter: adapter, cwd: cwd }, msg.cliSessionId, { ws: ws }).then(function (resumed) {
|
|
495
504
|
if (resumed) ws._clagenticActiveSession = resumed.localId;
|
|
496
505
|
}, function (err) {
|
|
506
|
+
console.error("[project-sessions] resume_session failed: project=" + logField(slug)
|
|
507
|
+
+ " session=" + logField(msg.cliSessionId) + " reason="
|
|
508
|
+
+ logText(err && err.message ? err.message : err));
|
|
497
509
|
sendTo(ws, { type: "error", text: "Could not resume this session: " + (err && err.message ? err.message : err) });
|
|
498
510
|
});
|
|
499
511
|
return true;
|