@sjawhar/claude-envoy 6.0.2 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +1 -1
- package/.omp-plugin/plugin.json +1 -1
- package/dist/codex-hook.js +818 -141
- package/dist/dispatch.js +11949 -11485
- package/dist/envoy-channel.js +402 -60
- package/dist/session-hook.js +821 -144
- package/package.json +1 -1
package/dist/codex-hook.js
CHANGED
|
@@ -14512,7 +14512,11 @@ var projectKeyValidation = {
|
|
|
14512
14512
|
};
|
|
14513
14513
|
var ARCHITECTURE_SOURCE_REPO_PATTERN = /^[A-Za-z0-9_-]+\/(?!\.\.?$)[A-Za-z0-9._-]+$/;
|
|
14514
14514
|
var API_ROUTE_PATTERN = /^\/api\/v1(?=[/?]|$)/;
|
|
14515
|
-
var
|
|
14515
|
+
var API_ROUTE_REWRITTEN = /["<>^`{}]/;
|
|
14516
|
+
var DISPATCH_API_METHODS = ["POST", "PUT", "PATCH", "DELETE"];
|
|
14517
|
+
var API_PATH_RULE = 'path is a route under /api/v1: it begins /api/v1 and names no scheme or host, every character is printable ASCII other than a space, a backslash and #, no character before the query is ", <, >, ^, `, { or }, and no segment before the query is . or ..';
|
|
14518
|
+
var API_BODY_RULE = "body goes only with method: a call that names no method is a GET, which sends no body.";
|
|
14519
|
+
var API_CALL_RULE = `${API_PATH_RULE}; ${API_BODY_RULE}`;
|
|
14516
14520
|
function apiPathProblem(value) {
|
|
14517
14521
|
const { path } = value;
|
|
14518
14522
|
if (typeof path !== "string")
|
|
@@ -14525,11 +14529,21 @@ function apiPathProblem(value) {
|
|
|
14525
14529
|
return `path ${named} holds a space, a backslash, a # or a character outside printable ASCII: percent-encode it.`;
|
|
14526
14530
|
}
|
|
14527
14531
|
const route = path.split("?", 1)[0] ?? "";
|
|
14532
|
+
if (API_ROUTE_REWRITTEN.test(route)) {
|
|
14533
|
+
return `path ${named} holds before its query a character a URL parser rewrites in a path (", <, >, ^, \`, { or }): percent-encode it.`;
|
|
14534
|
+
}
|
|
14528
14535
|
if (route.split("/").some((segment) => /^(?:\.|%2e){1,2}$/i.test(segment))) {
|
|
14529
14536
|
return `path ${named} holds a . or .. segment: name the route itself.`;
|
|
14530
14537
|
}
|
|
14531
14538
|
return;
|
|
14532
14539
|
}
|
|
14540
|
+
function apiCallProblem(value) {
|
|
14541
|
+
const pathProblem = apiPathProblem(value);
|
|
14542
|
+
if (pathProblem !== undefined)
|
|
14543
|
+
return pathProblem;
|
|
14544
|
+
const { method, body } = value;
|
|
14545
|
+
return body !== undefined && method === undefined ? API_BODY_RULE : undefined;
|
|
14546
|
+
}
|
|
14533
14547
|
var ISSUE_STATUSES = [
|
|
14534
14548
|
"triage",
|
|
14535
14549
|
"icebox",
|
|
@@ -15136,7 +15150,7 @@ var dispatchToolSpecs = [
|
|
|
15136
15150
|
{
|
|
15137
15151
|
name: "dispatch_whoami",
|
|
15138
15152
|
example: {},
|
|
15139
|
-
description: "Who Dispatch takes this session for: {session, owner, service, project}. session is the session Dispatch records your writes as. It is the session your credential proved, and the tool says so when that is not the session you run in. owner is the lowercase email of the person you act for, and is the first choice of assignee for issues you create. owner is null under the shared token and under a broker token that names a service and no person. With a null owner, a create with no assignee falls back to the parent's assignee, when the parent is in the issue's own project, and then to the project's default assignee, which every project names, so every issue you create has an assignee. A question on an issue reaches the person it was handed to, else the issue's assignee, else the project's default assignee. service is the subject of the verified service-account token that authenticated you, or
|
|
15153
|
+
description: "Who Dispatch takes this session for: {session, owner, service, project, project_scope}. session is the session Dispatch records your writes as. It is the session your credential proved, and the tool says so when that is not the session you run in. owner is the lowercase email of the person you act for, and is the first choice of assignee for issues you create. owner is null under the shared token and under a broker token that names a service and no person. With a null owner, a create with no assignee falls back to the parent's assignee, when the parent is in the issue's own project, and then to the project's default assignee, which every project names, so every issue you create has an assignee. A question on an issue reaches the person it was handed to, else the issue's assignee, else the project's default assignee. service is the subject of the verified service-account token that authenticated you, or the service a broker token naming no person names: a job's identity as its job entry lists it, the Kubernetes account an exchange entry lists, or the fleet of a machine nobody has signed in on; else null. project_scope is a broker token's project scope, and null for every other credential. A job's broker token has the project scope named or public, holds no OAuth scope and writes as its service: a route that needs an OAuth scope refuses it with 403 OAUTH_SCOPE_REQUIRED before Dispatch's project check runs. named may write its one project, and that check judges its write to another project as it judges a person's named token's for the same project: 403 PROJECT_SCOPE until dispatch://INFRA-4799 changes that answer for both. public names no one project, so it may write wherever a token that names no project may. An exchange entry's broker token that names its account has the project scope named and no project, and writes as that account wherever a token that names no project may. A machine's broker token that names a service has the project scope person; it may read and is refused every write with SIGN_IN_REQUIRED, which names the command a person signs in on that machine with. project is the one project key your credential may write, or null when it names none.",
|
|
15140
15154
|
arguments: () => ({}),
|
|
15141
15155
|
strict: true
|
|
15142
15156
|
},
|
|
@@ -15150,14 +15164,16 @@ var dispatchToolSpecs = [
|
|
|
15150
15164
|
{
|
|
15151
15165
|
name: "dispatch_api",
|
|
15152
15166
|
example: { path: "/api/v1/issues/DSP-1/events?limit=200" },
|
|
15153
|
-
description: "
|
|
15167
|
+
description: "Send one request of a route of Dispatch's HTTP API under /api/v1, with this session's own credential, and print the answer's body: Dispatch's JSON, indented, for jq to read; any other body of a 2xx, such as a file version's bytes or a document version's Markdown, as it came. It sends a GET of path unless method names another: path is a route beginning /api/v1, with its query after ?; dispatch api --path /api/v1 lists every route. With method it sends POST, PUT, PATCH or DELETE instead, and body, when given, as the request's JSON body (Content-Type: application/json), read from a file with --body-file <file> or from stdin with --body-file -; a body goes only with a method. A write that a named dispatch command makes goes through that command (dispatch --help lists them): dispatch api is for a route no command sends. In a session whose AskGate reviews Dispatch writes it sends GET alone, and refuses each method, naming the dispatch command that makes that write, and exits 1. It sends the request once, and once more only after Dispatch answers that the token it presented is not live (Dispatch then authenticated nothing, so nothing ran), with no redirect followed and no Idempotency-Key. On a 2xx answer it exits 0 and prints Dispatch's JSON as above, and any other body unchanged, with nothing added: an answer with Content-Disposition: attachment (a file version, a ?download=true read) is stored content, printed byte for byte whatever its type, JSON included. A 2xx whose body holds a credential the command presented (its token or its request's DPoP proof) is not printed: it exits 1 with CREDENTIAL_ECHOED, naming the status and saying a write may have been made. Any other answer exits 1 with its code: when Dispatch refuses the request, the command prints Dispatch's error with its code and, where Dispatch gives one, its hint; an answer that is neither a 2xx nor Dispatch's JSON, such as a proxy's page, is named by its status. A usage error, such as a path outside /api/v1 or a body with no method, exits 2. Where no host runs the command, as in a GitHub Actions job, it reads Dispatch from the environment alone, and a broker trade names the session of DISPATCH_SESSION_ID, else of AGENT_SECRETS_SESSION. Under Claude Code, a JSON answer longer than the output limit is written to a file the output names; a body printed as it came is written whole.",
|
|
15154
15168
|
arguments: (z) => ({
|
|
15155
|
-
path: z.string({ min: 1 }).describe(
|
|
15169
|
+
path: z.string({ min: 1 }).describe('The route, beginning /api/v1, with its query after ?, such as /api/v1/issues/ACME-12/events?limit=200. No scheme or host, no . or .. segment, and no ", <, >, ^, `, { or } before the query: percent-encode them.'),
|
|
15170
|
+
method: z.enum(DISPATCH_API_METHODS).describe("The method sent in place of GET: POST, PUT, PATCH or DELETE. Omit it to send a GET.").optional(),
|
|
15171
|
+
body: z.string().describe("The request's JSON body, sent as written with Content-Type: application/json; only with method. Write it to a file and pass it with --body-file, or --body-file - for stdin.").optional()
|
|
15156
15172
|
}),
|
|
15157
15173
|
validation: {
|
|
15158
|
-
check: (value) =>
|
|
15159
|
-
message:
|
|
15160
|
-
refusal: (value) =>
|
|
15174
|
+
check: (value) => apiCallProblem(value) === undefined,
|
|
15175
|
+
message: API_CALL_RULE,
|
|
15176
|
+
refusal: (value) => apiCallProblem(value) ?? API_CALL_RULE
|
|
15161
15177
|
},
|
|
15162
15178
|
strict: true
|
|
15163
15179
|
},
|
|
@@ -23784,6 +23800,7 @@ var FORM_AS_OF = "as_of";
|
|
|
23784
23800
|
var HEADER_DPOP_HOLDER = "DPoP-Holder";
|
|
23785
23801
|
var SUBJECT_TOKEN_TYPE_KUBERNETES = "urn:envoy:params:oauth:token-type:kubernetes-service-account";
|
|
23786
23802
|
var SUBJECT_TOKEN_TYPE_GOOGLE = "urn:envoy:params:oauth:token-type:google-identity";
|
|
23803
|
+
var SUBJECT_TOKEN_TYPE_GITHUB = "urn:envoy:params:oauth:token-type:github-workflow-run";
|
|
23787
23804
|
var SUBJECT_TOKEN_TYPE_PERSON = "urn:envoy:params:oauth:token-type:person-google-identity";
|
|
23788
23805
|
var ACTOR_TOKEN_TYPE_MACHINE = "urn:envoy:params:oauth:token-type:machine-google-identity";
|
|
23789
23806
|
var TOKEN_TYPE_DPOP = "DPoP";
|
|
@@ -23817,6 +23834,7 @@ var WORKFLOW_REF = "[A-Za-z0-9_./@-]{1,400}";
|
|
|
23817
23834
|
var IDENTITY = `(?:${K8S_ACCOUNT}|${REPOSITORY}/${WORKFLOW_PATH}|${EMAIL})`;
|
|
23818
23835
|
var FIXED_SESSION = "[A-Za-z0-9._:@/+-]{1,200}";
|
|
23819
23836
|
var PROJECT_LIST = `${PROJECT_SLOT}(?: ${PROJECT_SLOT}){0,31}`;
|
|
23837
|
+
var JOB_LIST = `(?:public(?:,${PROJECT_SLOT}){0,16}|${PROJECT_SLOT})`;
|
|
23820
23838
|
var SERVICE = "[A-Za-z0-9._-]{1,100}";
|
|
23821
23839
|
var CLOUD_PROJECT = "[a-z][a-z0-9-]{4,29}";
|
|
23822
23840
|
var INSTANCE = "[a-z0-9][a-z0-9-]{0,62}";
|
|
@@ -23886,6 +23904,7 @@ var POLICY_REFUSAL_PATTERNS = [
|
|
|
23886
23904
|
policy("claimMissing", "the subject token's %s claim is not a non-empty string", CLAIM),
|
|
23887
23905
|
policy("projectsFixed", "%s's entry names no project, and the request named %s", IDENTITY, PROJECT_LIST),
|
|
23888
23906
|
policy("projectsFixed", "%s writes the project %s that its entry names, and the request named %s", IDENTITY, PROJECT_SLOT, PROJECT_LIST),
|
|
23907
|
+
policy("projectsFixed", "%s's entry gives its token the projects %s, and the request named %s, which that list does not name", IDENTITY, JOB_LIST, PROJECT_SLOT),
|
|
23889
23908
|
`${escapeRegExp("The subject token was refused: ")}${formatPattern("the subject token was not minted for %s, which is the audience this broker accepts from %s", [URI, URI])}\\.`
|
|
23890
23909
|
];
|
|
23891
23910
|
var KNOWN_BROKER_REFUSAL_PATTERNS = [
|
|
@@ -23911,6 +23930,7 @@ var KNOWN_BROKER_REFUSAL_PATTERNS = [
|
|
|
23911
23930
|
"The 'as_of' parameter is not an RFC 3339 time with at most six fractional digits\\.",
|
|
23912
23931
|
"The 'as_of' parameter names the zero time, which is no cut: a trade that reads the present names no cut\\.",
|
|
23913
23932
|
"The 'as_of' parameter names a time later than this trade, and a cut is a time that has passed\\.",
|
|
23933
|
+
"The scope 'read' narrows a token to reading, and a job entry's token is minted whole, with no scope: ask for it without that scope\\.",
|
|
23914
23934
|
"A read trade makes or moves no launcher credential, so it cannot send launcher_credential=true\\. Trade for read without the parameter\\.",
|
|
23915
23935
|
`${PERSON_SLOT}'s sign-in on this machine holds no launcher credential yet, and a read trade makes none\\. Sign in with \`agent-secrets login\` first\\.`,
|
|
23916
23936
|
`${PERSON_SLOT}'s sign-in on this machine holds a launcher credential that has lapsed, and a read trade renews none\\. One ordinary trade of the sign-in renews it, as \`agent-secrets\` makes; then trade for read again\\.`,
|
|
@@ -24972,6 +24992,14 @@ class DispatchGatewayError extends DispatchServiceError {
|
|
|
24972
24992
|
this.mayHaveReachedDispatch = reachesDispatch(status);
|
|
24973
24993
|
}
|
|
24974
24994
|
}
|
|
24995
|
+
var CREDENTIAL_ECHOED = "CREDENTIAL_ECHOED";
|
|
24996
|
+
|
|
24997
|
+
class DispatchCredentialEchoError extends DispatchServiceError {
|
|
24998
|
+
fromDispatch = false;
|
|
24999
|
+
constructor(status) {
|
|
25000
|
+
super(CREDENTIAL_ECHOED, status, `The answer had HTTP status ${status} (2xx), so a write may have been made. The body held a credential this command presented. It is not printed. Something in Dispatch's place appears to have echoed the request. Check whether any write took effect before sending it again.`);
|
|
25001
|
+
}
|
|
25002
|
+
}
|
|
24975
25003
|
|
|
24976
25004
|
class DispatchWriteUnsettledError extends Error {
|
|
24977
25005
|
last;
|
|
@@ -25030,6 +25058,34 @@ function isJson(response) {
|
|
|
25030
25058
|
const mediaType = type.trim().toLowerCase();
|
|
25031
25059
|
return mediaType === "application/json" || mediaType.length > "+json".length && mediaType.endsWith("+json");
|
|
25032
25060
|
}
|
|
25061
|
+
function isAttachment(response) {
|
|
25062
|
+
const [type = ""] = (response.headers.get("content-disposition") ?? "").split(";");
|
|
25063
|
+
return type.trim().toLowerCase() === "attachment";
|
|
25064
|
+
}
|
|
25065
|
+
function jsonHoldsCredential(payload, credentials) {
|
|
25066
|
+
const pending = [payload];
|
|
25067
|
+
while (pending.length > 0) {
|
|
25068
|
+
const value = pending.pop();
|
|
25069
|
+
if (typeof value === "string") {
|
|
25070
|
+
for (const credential of credentials) {
|
|
25071
|
+
if (value.includes(credential))
|
|
25072
|
+
return true;
|
|
25073
|
+
}
|
|
25074
|
+
} else if (value !== null && typeof value === "object") {
|
|
25075
|
+
const members = value;
|
|
25076
|
+
for (const key in members) {
|
|
25077
|
+
if (!Object.hasOwn(members, key))
|
|
25078
|
+
continue;
|
|
25079
|
+
for (const credential of credentials) {
|
|
25080
|
+
if (key.includes(credential))
|
|
25081
|
+
return true;
|
|
25082
|
+
}
|
|
25083
|
+
pending.push(members[key]);
|
|
25084
|
+
}
|
|
25085
|
+
}
|
|
25086
|
+
}
|
|
25087
|
+
return false;
|
|
25088
|
+
}
|
|
25033
25089
|
function isNoRoute(error) {
|
|
25034
25090
|
return error instanceof DispatchServiceError && error.status === 404 && error.code === "NOT_FOUND" && error.message.startsWith(NO_ROUTE_MESSAGE_PREFIX);
|
|
25035
25091
|
}
|
|
@@ -25132,6 +25188,21 @@ function parsedJson(response, text) {
|
|
|
25132
25188
|
return;
|
|
25133
25189
|
}
|
|
25134
25190
|
}
|
|
25191
|
+
function refusalIn(json) {
|
|
25192
|
+
const shape = asErrorShape(json?.value);
|
|
25193
|
+
if (typeof shape.error !== "string")
|
|
25194
|
+
return;
|
|
25195
|
+
return {
|
|
25196
|
+
code: typeof shape.code === "string" ? shape.code : undefined,
|
|
25197
|
+
error: shape.error,
|
|
25198
|
+
candidates: shape.candidates,
|
|
25199
|
+
changed: shape.changed,
|
|
25200
|
+
current: shape.current,
|
|
25201
|
+
hint: typeof shape.hint === "string" ? shape.hint : undefined,
|
|
25202
|
+
issues: shape.issues,
|
|
25203
|
+
mismatches: shape.mismatches
|
|
25204
|
+
};
|
|
25205
|
+
}
|
|
25135
25206
|
var NULL_BODY_STATUSES = new Set([101, 204, 205, 304]);
|
|
25136
25207
|
async function writeAnswer(response) {
|
|
25137
25208
|
if (NULL_BODY_STATUSES.has(response.status)) {
|
|
@@ -25150,16 +25221,15 @@ async function writeAnswer(response) {
|
|
|
25150
25221
|
const text = await response.text();
|
|
25151
25222
|
const typed = isJson(response);
|
|
25152
25223
|
const json = parsedJson(response, text);
|
|
25153
|
-
const
|
|
25154
|
-
const own = typeof shape.error === "string";
|
|
25224
|
+
const refusal = response.ok ? undefined : refusalIn(json);
|
|
25155
25225
|
return {
|
|
25156
25226
|
response,
|
|
25157
25227
|
malformed: text !== "" && typed && json === undefined,
|
|
25158
25228
|
notJson: text === "" || !typed,
|
|
25159
25229
|
text,
|
|
25160
25230
|
json,
|
|
25161
|
-
code:
|
|
25162
|
-
error:
|
|
25231
|
+
code: refusal?.code,
|
|
25232
|
+
error: refusal?.error,
|
|
25163
25233
|
replayed: response.headers.get(IDEMPOTENCY_REPLAYED_HEADER)?.trim() === "?1",
|
|
25164
25234
|
keyEcho: response.headers.get(IDEMPOTENCY_KEY_HEADER) ?? undefined
|
|
25165
25235
|
};
|
|
@@ -25445,8 +25515,10 @@ class DispatchClient {
|
|
|
25445
25515
|
return this.#json("GET", ["api", "v1", "limits"]);
|
|
25446
25516
|
}
|
|
25447
25517
|
async readRoute(path) {
|
|
25448
|
-
|
|
25449
|
-
|
|
25518
|
+
return this.#route("GET", path);
|
|
25519
|
+
}
|
|
25520
|
+
async sendRoute(method, path, body) {
|
|
25521
|
+
return this.#route(method, path, body);
|
|
25450
25522
|
}
|
|
25451
25523
|
async inbox(assignee) {
|
|
25452
25524
|
return this.#json("GET", ["api", "v1", "inbox"], undefined, { assignee });
|
|
@@ -25659,7 +25731,7 @@ class DispatchClient {
|
|
|
25659
25731
|
async fileVersion(id, version) {
|
|
25660
25732
|
const url = this.#url(["api", "v1", "artifacts", id, "versions", String(version)]);
|
|
25661
25733
|
return this.#noRoute(async () => {
|
|
25662
|
-
const response = await this.#send("GET", url, undefined, "*/*");
|
|
25734
|
+
const { answer: response } = await this.#send("GET", url, undefined, "*/*");
|
|
25663
25735
|
if (!response.ok)
|
|
25664
25736
|
return this.#response("GET", url, response);
|
|
25665
25737
|
return {
|
|
@@ -25791,7 +25863,7 @@ class DispatchClient {
|
|
|
25791
25863
|
return { status: answer.response.status, payload: this.#payloadOf(method, url, answer) };
|
|
25792
25864
|
}
|
|
25793
25865
|
return this.#noRoute(async () => {
|
|
25794
|
-
const response = await this.#send(method, url, encoded, undefined, signal);
|
|
25866
|
+
const { answer: response } = await this.#send(method, url, encoded, undefined, signal);
|
|
25795
25867
|
return { status: response.status, payload: await this.#response(method, url, response) };
|
|
25796
25868
|
}, signal);
|
|
25797
25869
|
}
|
|
@@ -25844,7 +25916,7 @@ class DispatchClient {
|
|
|
25844
25916
|
const answer = await this.#write(method, url, body, "application/json", undefined, write);
|
|
25845
25917
|
return this.#payloadOf(method, url, answer);
|
|
25846
25918
|
}
|
|
25847
|
-
return this.#noRoute(async () => this.#response(method, url, await this.#send(method, url, body)));
|
|
25919
|
+
return this.#noRoute(async () => this.#response(method, url, (await this.#send(method, url, body)).answer));
|
|
25848
25920
|
}
|
|
25849
25921
|
async#writingNothing(path, body, options = {}) {
|
|
25850
25922
|
for (let keys = 1;; keys++) {
|
|
@@ -25860,13 +25932,57 @@ class DispatchClient {
|
|
|
25860
25932
|
}
|
|
25861
25933
|
}
|
|
25862
25934
|
}
|
|
25935
|
+
async#route(method, path, body) {
|
|
25936
|
+
const url = new URL(path.slice(1), `${this.#baseUrl}/`).toString();
|
|
25937
|
+
const encoded = body === undefined ? undefined : { content: body, type: "application/json" };
|
|
25938
|
+
const {
|
|
25939
|
+
answer: response,
|
|
25940
|
+
presented,
|
|
25941
|
+
stalePresentation
|
|
25942
|
+
} = await this.#send(method, url, encoded);
|
|
25943
|
+
if (!response.ok)
|
|
25944
|
+
return { payload: await this.#response(method, url, response) };
|
|
25945
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
25946
|
+
const credentials = new Set(this.#sent.with(this.token));
|
|
25947
|
+
for (const headers of [presented, stalePresentation]) {
|
|
25948
|
+
if (headers === undefined)
|
|
25949
|
+
continue;
|
|
25950
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
25951
|
+
const header = name.toLowerCase();
|
|
25952
|
+
if (header !== "authorization" && header !== "dpop")
|
|
25953
|
+
continue;
|
|
25954
|
+
const credential = value.trim();
|
|
25955
|
+
if (credential !== "")
|
|
25956
|
+
credentials.add(credential);
|
|
25957
|
+
}
|
|
25958
|
+
}
|
|
25959
|
+
const raw = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
25960
|
+
for (const credential of credentials) {
|
|
25961
|
+
if (raw.includes(credential))
|
|
25962
|
+
throw new DispatchCredentialEchoError(response.status);
|
|
25963
|
+
}
|
|
25964
|
+
if (!isAttachment(response) && isJson(response)) {
|
|
25965
|
+
const text = new TextDecoder().decode(bytes);
|
|
25966
|
+
const json = parsedJson(response, text);
|
|
25967
|
+
if (json !== undefined) {
|
|
25968
|
+
if (text.includes("\\") && jsonHoldsCredential(json.value, credentials)) {
|
|
25969
|
+
throw new DispatchCredentialEchoError(response.status);
|
|
25970
|
+
}
|
|
25971
|
+
return { payload: this.#payloadOf(method, url, { response, text, json }) };
|
|
25972
|
+
}
|
|
25973
|
+
}
|
|
25974
|
+
return { body: bytes, type: response.headers.get("Content-Type") ?? undefined };
|
|
25975
|
+
}
|
|
25863
25976
|
async#send(method, url, body, accept = "application/json", signal) {
|
|
25864
25977
|
const sent = await this.#sendOnce(method, url, body, accept, signal);
|
|
25865
25978
|
if (this.#credential.stale === undefined || !await tokenNotLive(sent.answer)) {
|
|
25866
|
-
return sent
|
|
25979
|
+
return sent;
|
|
25867
25980
|
}
|
|
25868
25981
|
this.#credential.stale(undefined, sent.presented);
|
|
25869
|
-
return
|
|
25982
|
+
return {
|
|
25983
|
+
...await this.#sendOnce(method, url, body, accept, signal),
|
|
25984
|
+
stalePresentation: sent.presented
|
|
25985
|
+
};
|
|
25870
25986
|
}
|
|
25871
25987
|
async#sendOnce(method, url, body, accept, signal) {
|
|
25872
25988
|
const limit = signal === undefined ? this.#signal : AbortSignal.any([this.#signal, signal]);
|
|
@@ -26137,12 +26253,12 @@ class DispatchClient {
|
|
|
26137
26253
|
const { response, text, json } = answer;
|
|
26138
26254
|
const payload = json === undefined ? text : json.value;
|
|
26139
26255
|
if (!response.ok) {
|
|
26140
|
-
const
|
|
26141
|
-
if (
|
|
26142
|
-
if (response.status === 409 &&
|
|
26256
|
+
const refusal = refusalIn(json);
|
|
26257
|
+
if (refusal !== undefined) {
|
|
26258
|
+
if (response.status === 409 && refusal.code === IDEMPOTENCY_KEY_COMMITTED) {
|
|
26143
26259
|
throw new DispatchWriteLandedError(IDEMPOTENCY_KEY_COMMITTED, response.status, method, requestForText(this.#baseUrl, url, DISPATCH_URL_SETTING));
|
|
26144
26260
|
}
|
|
26145
|
-
throw new DispatchServiceError(
|
|
26261
|
+
throw new DispatchServiceError(refusal.code ?? `HTTP_${response.status}`, response.status, refusal.error, refusal);
|
|
26146
26262
|
}
|
|
26147
26263
|
throw this.#gatewayError(method, url, response, text, "Dispatch's error JSON");
|
|
26148
26264
|
}
|
|
@@ -30417,16 +30533,518 @@ async function resultsCommand(context) {
|
|
|
30417
30533
|
};
|
|
30418
30534
|
}
|
|
30419
30535
|
|
|
30420
|
-
// ../envoy-client/src/
|
|
30421
|
-
|
|
30422
|
-
|
|
30423
|
-
|
|
30536
|
+
// ../envoy-client/src/askgate-marker.ts
|
|
30537
|
+
import { createHash, randomBytes } from "crypto";
|
|
30538
|
+
import {
|
|
30539
|
+
closeSync,
|
|
30540
|
+
constants,
|
|
30541
|
+
mkdirSync,
|
|
30542
|
+
openSync,
|
|
30543
|
+
readdirSync,
|
|
30544
|
+
readFileSync,
|
|
30545
|
+
readlinkSync,
|
|
30546
|
+
renameSync,
|
|
30547
|
+
rmSync,
|
|
30548
|
+
statSync,
|
|
30549
|
+
unlinkSync,
|
|
30550
|
+
utimesSync,
|
|
30551
|
+
writeFileSync
|
|
30552
|
+
} from "fs";
|
|
30553
|
+
import { isAbsolute as isAbsolute2, join as join3, resolve as resolve2 } from "path";
|
|
30554
|
+
var ASKGATE_RECORD = "askgate";
|
|
30555
|
+
var MARKER_MAX_AGE_MS = 2 * 60 * 60000;
|
|
30556
|
+
var STORE_BEAT_MS = 10 * 60000;
|
|
30557
|
+
var MARKERS = "markers";
|
|
30558
|
+
var ALWAYS_GATED = {
|
|
30559
|
+
dispatch_ask: true,
|
|
30560
|
+
dispatch_message: true,
|
|
30561
|
+
dispatch_edit_ask: true,
|
|
30562
|
+
dispatch_comment: true,
|
|
30563
|
+
dispatch_edit_comment: true,
|
|
30564
|
+
dispatch_delete_comment: true,
|
|
30565
|
+
dispatch_edit_message: true,
|
|
30566
|
+
dispatch_delete_message: true,
|
|
30567
|
+
dispatch_doc_edit: true,
|
|
30568
|
+
dispatch_request_approval: true
|
|
30569
|
+
};
|
|
30570
|
+
var MARKDOWN_NAME = /\.md$/i;
|
|
30571
|
+
var MARKDOWN_PATH = /\.(md|markdown)$/i;
|
|
30572
|
+
function uploadIsDocument(args) {
|
|
30573
|
+
const { content, name, path } = args;
|
|
30574
|
+
return typeof content === "string" || typeof name === "string" && MARKDOWN_NAME.test(name.trim()) || typeof path === "string" && MARKDOWN_PATH.test(path);
|
|
30575
|
+
}
|
|
30576
|
+
function gatedWrite(tool, args) {
|
|
30577
|
+
if (ALWAYS_GATED[tool] === true)
|
|
30578
|
+
return true;
|
|
30579
|
+
const { spec } = args;
|
|
30580
|
+
if (tool === "dispatch_issue")
|
|
30581
|
+
return spec !== undefined;
|
|
30582
|
+
if (tool === "dispatch_artifact")
|
|
30583
|
+
return uploadIsDocument(args);
|
|
30584
|
+
return false;
|
|
30585
|
+
}
|
|
30586
|
+
var digest2 = (value) => createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
30587
|
+
function argvDigest(argv) {
|
|
30588
|
+
return digest2(argv);
|
|
30589
|
+
}
|
|
30590
|
+
function argsDigest(args) {
|
|
30591
|
+
return digest2(args);
|
|
30592
|
+
}
|
|
30593
|
+
function fileOperands(argv) {
|
|
30594
|
+
const [command, ...flags] = argv;
|
|
30595
|
+
const files = [];
|
|
30596
|
+
for (let index = 0;index < flags.length; index++) {
|
|
30597
|
+
const word = flags[index];
|
|
30598
|
+
const eq = word.indexOf("=");
|
|
30599
|
+
const flag = eq === -1 ? word : word.slice(0, eq);
|
|
30600
|
+
const named = flag.startsWith("--") && flag.endsWith("-file");
|
|
30601
|
+
if (!named && !(command === "artifact" && flag === "--path"))
|
|
30602
|
+
continue;
|
|
30603
|
+
const operand = eq === -1 ? flags[index + 1] : word.slice(eq + 1);
|
|
30604
|
+
if (operand !== undefined && operand !== "-")
|
|
30605
|
+
files.push({ flag, operand });
|
|
30606
|
+
}
|
|
30607
|
+
return files;
|
|
30608
|
+
}
|
|
30609
|
+
function operandPath(cwd, file) {
|
|
30610
|
+
if (file.flag === "--path")
|
|
30611
|
+
return resolve2(cwd, file.operand);
|
|
30612
|
+
return isAbsolute2(file.operand) ? file.operand : `${cwd}/${file.operand}`;
|
|
30613
|
+
}
|
|
30614
|
+
function bytesDigest(bytes) {
|
|
30615
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
30616
|
+
}
|
|
30617
|
+
function processStart(pid) {
|
|
30618
|
+
try {
|
|
30619
|
+
const stat = readFileSync(`/proc/${pid}/stat`, "utf-8");
|
|
30620
|
+
return stat.slice(stat.lastIndexOf(")") + 2).split(" ")[19] ?? null;
|
|
30621
|
+
} catch {
|
|
30622
|
+
return null;
|
|
30623
|
+
}
|
|
30624
|
+
}
|
|
30625
|
+
var ownNamespace;
|
|
30626
|
+
function pidNamespace() {
|
|
30627
|
+
if (ownNamespace === undefined) {
|
|
30628
|
+
try {
|
|
30629
|
+
ownNamespace = /^pid:\[([0-9]+)\]$/.exec(readlinkSync("/proc/self/ns/pid"))?.[1] ?? null;
|
|
30630
|
+
} catch {
|
|
30631
|
+
ownNamespace = null;
|
|
30632
|
+
}
|
|
30633
|
+
}
|
|
30634
|
+
return ownNamespace;
|
|
30635
|
+
}
|
|
30636
|
+
function ownerLive(owner, now) {
|
|
30637
|
+
if (owner.ns !== null && owner.ns !== pidNamespace()) {
|
|
30638
|
+
try {
|
|
30639
|
+
return now - storeTouched(owner.store) <= MARKER_MAX_AGE_MS;
|
|
30640
|
+
} catch {
|
|
30641
|
+
return false;
|
|
30642
|
+
}
|
|
30643
|
+
}
|
|
30644
|
+
if (owner.start !== null)
|
|
30645
|
+
return processStart(owner.pid) === owner.start;
|
|
30646
|
+
try {
|
|
30647
|
+
process.kill(owner.pid, 0);
|
|
30648
|
+
return true;
|
|
30649
|
+
} catch (error) {
|
|
30650
|
+
return !hasErrnoCode(error, "ESRCH");
|
|
30651
|
+
}
|
|
30652
|
+
}
|
|
30653
|
+
function isOwnerText(value) {
|
|
30654
|
+
if (typeof value !== "object" || value === null)
|
|
30655
|
+
return false;
|
|
30656
|
+
const { pid, start, ns, store } = value;
|
|
30657
|
+
return typeof pid === "number" && Number.isSafeInteger(pid) && pid > 0 && (start === null || typeof start === "string") && (ns === undefined || ns === null || typeof ns === "string" && /^[0-9]+$/.test(ns)) && typeof store === "string" && store.startsWith("/");
|
|
30658
|
+
}
|
|
30659
|
+
function parseOwner(text) {
|
|
30660
|
+
let data;
|
|
30661
|
+
try {
|
|
30662
|
+
data = JSON.parse(text);
|
|
30663
|
+
} catch {
|
|
30664
|
+
return;
|
|
30665
|
+
}
|
|
30666
|
+
if (!isOwnerText(data) || data.v !== 2)
|
|
30667
|
+
return;
|
|
30668
|
+
const { pid, start, ns, store, mode } = data;
|
|
30424
30669
|
return {
|
|
30425
|
-
|
|
30426
|
-
|
|
30670
|
+
owner: { pid, start, ns: ns ?? null, store },
|
|
30671
|
+
mode: mode === "block" ? "block" : "warn"
|
|
30672
|
+
};
|
|
30673
|
+
}
|
|
30674
|
+
var OWNER_FILE = ".json";
|
|
30675
|
+
var STORE_NAME = /^([1-9][0-9]*)(?:-([0-9]+)(?:-([0-9]+))?)?$/;
|
|
30676
|
+
function storeTouched(store) {
|
|
30677
|
+
const own = statSync(store).mtimeMs;
|
|
30678
|
+
try {
|
|
30679
|
+
return Math.max(own, statSync(join3(store, MARKERS)).mtimeMs);
|
|
30680
|
+
} catch (error) {
|
|
30681
|
+
if (hasErrnoCode(error, "ENOENT"))
|
|
30682
|
+
return own;
|
|
30683
|
+
throw error;
|
|
30684
|
+
}
|
|
30685
|
+
}
|
|
30686
|
+
var UNNAMESPACED_STORE_MAX_AGE_MS = 14 * 86400000;
|
|
30687
|
+
function sweepStores(root, now) {
|
|
30688
|
+
const parent = join3(root, "askgate");
|
|
30689
|
+
let names;
|
|
30690
|
+
try {
|
|
30691
|
+
names = readdirSync(parent);
|
|
30692
|
+
} catch {
|
|
30693
|
+
return;
|
|
30694
|
+
}
|
|
30695
|
+
for (const name of names) {
|
|
30696
|
+
const match = STORE_NAME.exec(name);
|
|
30697
|
+
if (match === null)
|
|
30698
|
+
continue;
|
|
30699
|
+
const store = join3(parent, name);
|
|
30700
|
+
try {
|
|
30701
|
+
const owner = { pid: Number(match[1]), start: match[2] ?? null, ns: match[3] ?? null, store };
|
|
30702
|
+
if (ownerLive(owner, now))
|
|
30703
|
+
continue;
|
|
30704
|
+
const unnamespaced = owner.start !== null && owner.ns === null;
|
|
30705
|
+
const keep = unnamespaced ? UNNAMESPACED_STORE_MAX_AGE_MS : MARKER_MAX_AGE_MS;
|
|
30706
|
+
if (now - storeTouched(store) <= keep)
|
|
30707
|
+
continue;
|
|
30708
|
+
rmSync(store, { recursive: true, force: true });
|
|
30709
|
+
} catch {}
|
|
30710
|
+
}
|
|
30711
|
+
}
|
|
30712
|
+
function recordNamesStore(dir) {
|
|
30713
|
+
const record = join3(dir, ASKGATE_RECORD);
|
|
30714
|
+
let names;
|
|
30715
|
+
try {
|
|
30716
|
+
names = readdirSync(record);
|
|
30717
|
+
} catch (error) {
|
|
30718
|
+
return !hasErrnoCode(error, "ENOENT");
|
|
30719
|
+
}
|
|
30720
|
+
for (const name of names) {
|
|
30721
|
+
if (!name.endsWith(OWNER_FILE))
|
|
30722
|
+
continue;
|
|
30723
|
+
try {
|
|
30724
|
+
const parsed = parseOwner(readFileSync(join3(record, name), "utf-8"));
|
|
30725
|
+
if (parsed === undefined)
|
|
30726
|
+
return true;
|
|
30727
|
+
statSync(parsed.owner.store);
|
|
30728
|
+
return true;
|
|
30729
|
+
} catch (error) {
|
|
30730
|
+
if (!hasErrnoCode(error, "ENOENT"))
|
|
30731
|
+
return true;
|
|
30732
|
+
}
|
|
30733
|
+
}
|
|
30734
|
+
return false;
|
|
30735
|
+
}
|
|
30736
|
+
function askgateState(dir, now = Date.now()) {
|
|
30737
|
+
const record = join3(dir, ASKGATE_RECORD);
|
|
30738
|
+
let names;
|
|
30739
|
+
try {
|
|
30740
|
+
names = readdirSync(record);
|
|
30741
|
+
} catch (error) {
|
|
30742
|
+
if (hasErrnoCode(error, "ENOENT"))
|
|
30743
|
+
return { kind: "off" };
|
|
30744
|
+
return { kind: "broken", problem: `${record}, could not be read: ${messageFor(error)}` };
|
|
30745
|
+
}
|
|
30746
|
+
const live = [];
|
|
30747
|
+
for (const name of names) {
|
|
30748
|
+
if (!name.endsWith(OWNER_FILE))
|
|
30749
|
+
continue;
|
|
30750
|
+
const file = join3(record, name);
|
|
30751
|
+
let text;
|
|
30752
|
+
try {
|
|
30753
|
+
text = readFileSync(file, "utf-8");
|
|
30754
|
+
} catch (error) {
|
|
30755
|
+
if (hasErrnoCode(error, "ENOENT"))
|
|
30756
|
+
continue;
|
|
30757
|
+
return { kind: "broken", problem: `${file}, could not be read: ${messageFor(error)}` };
|
|
30758
|
+
}
|
|
30759
|
+
const parsed = parseOwner(text);
|
|
30760
|
+
if (parsed === undefined)
|
|
30761
|
+
return { kind: "broken", problem: `${file}, is not an AskGate record` };
|
|
30762
|
+
if (ownerLive(parsed.owner, now)) {
|
|
30763
|
+
live.push({ file, store: parsed.owner.store, mode: parsed.mode });
|
|
30764
|
+
}
|
|
30765
|
+
}
|
|
30766
|
+
if (live.length === 0)
|
|
30767
|
+
return { kind: "off" };
|
|
30768
|
+
for (const { file, store } of live) {
|
|
30769
|
+
try {
|
|
30770
|
+
if (!statSync(join3(store, MARKERS)).isDirectory())
|
|
30771
|
+
throw new Error("not a directory");
|
|
30772
|
+
} catch (error) {
|
|
30773
|
+
return {
|
|
30774
|
+
kind: "broken",
|
|
30775
|
+
problem: `${file}, names the marker store ${store}, which cannot be read: ${messageFor(error)}`
|
|
30776
|
+
};
|
|
30777
|
+
}
|
|
30778
|
+
}
|
|
30779
|
+
return {
|
|
30780
|
+
kind: "on",
|
|
30781
|
+
stores: live.map(({ store }) => store),
|
|
30782
|
+
mode: live.some(({ mode }) => mode === "block") ? "block" : "warn"
|
|
30783
|
+
};
|
|
30784
|
+
}
|
|
30785
|
+
var isFileDigest = (value) => typeof value === "object" && value !== null && typeof value.flag === "string" && typeof value.operand === "string" && typeof value.sha256 === "string";
|
|
30786
|
+
function readMarker(file) {
|
|
30787
|
+
let text;
|
|
30788
|
+
try {
|
|
30789
|
+
const fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
30790
|
+
try {
|
|
30791
|
+
text = readFileSync(fd, "utf-8");
|
|
30792
|
+
} finally {
|
|
30793
|
+
closeSync(fd);
|
|
30794
|
+
}
|
|
30795
|
+
} catch {
|
|
30796
|
+
return;
|
|
30797
|
+
}
|
|
30798
|
+
try {
|
|
30799
|
+
const data = JSON.parse(text);
|
|
30800
|
+
if (typeof data !== "object" || data === null)
|
|
30801
|
+
return;
|
|
30802
|
+
const { v, argv, args, files, createdAt } = data;
|
|
30803
|
+
if (v !== 2 || typeof argv !== "string" || typeof createdAt !== "number")
|
|
30804
|
+
return;
|
|
30805
|
+
if (args !== null && typeof args !== "string")
|
|
30806
|
+
return;
|
|
30807
|
+
if (!Array.isArray(files) || !files.every(isFileDigest))
|
|
30808
|
+
return;
|
|
30809
|
+
return { argv, args, files, createdAt };
|
|
30810
|
+
} catch {
|
|
30811
|
+
return;
|
|
30812
|
+
}
|
|
30813
|
+
}
|
|
30814
|
+
function consumeMarker(stores, argv, args, files, now) {
|
|
30815
|
+
const key = argvDigest(argv);
|
|
30816
|
+
const reviewed = argsDigest(args);
|
|
30817
|
+
let changed;
|
|
30818
|
+
let mismatched = false;
|
|
30819
|
+
let expired = false;
|
|
30820
|
+
const usable = [];
|
|
30821
|
+
for (const store of stores) {
|
|
30822
|
+
const dir = join3(store, MARKERS);
|
|
30823
|
+
for (const name of readdirSync(dir)) {
|
|
30824
|
+
if (!name.startsWith(`${key}.`))
|
|
30825
|
+
continue;
|
|
30826
|
+
const file = join3(dir, name);
|
|
30827
|
+
const marker = readMarker(file);
|
|
30828
|
+
if (marker === undefined || marker.argv !== key)
|
|
30829
|
+
continue;
|
|
30830
|
+
if (now - marker.createdAt > MARKER_MAX_AGE_MS) {
|
|
30831
|
+
expired = true;
|
|
30832
|
+
rmSync(file, { force: true });
|
|
30833
|
+
continue;
|
|
30834
|
+
}
|
|
30835
|
+
if (marker.args !== null) {
|
|
30836
|
+
const differs = marker.files.find((bound, index) => bound.flag !== files[index]?.flag || bound.operand !== files[index]?.operand || bound.sha256 !== files[index]?.sha256);
|
|
30837
|
+
if (differs !== undefined) {
|
|
30838
|
+
changed ??= { flag: differs.flag, operand: differs.operand };
|
|
30839
|
+
continue;
|
|
30840
|
+
}
|
|
30841
|
+
if (marker.files.length !== files.length || marker.args !== reviewed) {
|
|
30842
|
+
mismatched = true;
|
|
30843
|
+
continue;
|
|
30844
|
+
}
|
|
30845
|
+
}
|
|
30846
|
+
usable.push({ file, bound: marker.args !== null });
|
|
30847
|
+
}
|
|
30848
|
+
}
|
|
30849
|
+
usable.sort((a, b) => Number(b.bound) - Number(a.bound));
|
|
30850
|
+
for (const { file } of usable) {
|
|
30851
|
+
try {
|
|
30852
|
+
unlinkSync(file);
|
|
30853
|
+
return { ok: true };
|
|
30854
|
+
} catch (error) {
|
|
30855
|
+
if (!hasErrnoCode(error, "ENOENT"))
|
|
30856
|
+
throw error;
|
|
30857
|
+
}
|
|
30858
|
+
}
|
|
30859
|
+
if (changed !== undefined)
|
|
30860
|
+
return {
|
|
30861
|
+
ok: false,
|
|
30862
|
+
why: `a file this write posts changed after AskGate read it (\`${changed.flag} ${changed.operand}\`)`,
|
|
30863
|
+
changed
|
|
30864
|
+
};
|
|
30865
|
+
return {
|
|
30866
|
+
ok: false,
|
|
30867
|
+
why: mismatched ? "its arguments differ from the ones AskGate reviewed" : expired ? "its AskGate marker expired before it ran" : "this call carries no AskGate marker"
|
|
30427
30868
|
};
|
|
30428
30869
|
}
|
|
30429
30870
|
|
|
30871
|
+
// ../envoy-client/src/dispatch-session-directory.ts
|
|
30872
|
+
import { randomUUID } from "crypto";
|
|
30873
|
+
import { renameSync as renameSync2, rmSync as rmSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
30874
|
+
import { homedir as homedir2 } from "os";
|
|
30875
|
+
import { join as join4 } from "path";
|
|
30876
|
+
var SESSION_ID_PATH_SEGMENT = /^[A-Za-z0-9._-]{1,128}$/;
|
|
30877
|
+
var PRIVATE_FILE = 384;
|
|
30878
|
+
var SESSION_VARIABLES = [
|
|
30879
|
+
"ENVOY_SESSION_ID",
|
|
30880
|
+
"CLAUDE_CODE_SESSION_ID",
|
|
30881
|
+
"DISPATCH_SESSION_ID",
|
|
30882
|
+
"CODEX_THREAD_ID"
|
|
30883
|
+
];
|
|
30884
|
+
function sessionStateRoot(env) {
|
|
30885
|
+
const stateDir = env["DISPATCH_STATE_DIR"];
|
|
30886
|
+
if (stateDir)
|
|
30887
|
+
return stateDir;
|
|
30888
|
+
return join4(env["XDG_STATE_HOME"] || join4(homedir2(), ".local", "state"), "dispatch");
|
|
30889
|
+
}
|
|
30890
|
+
function sessionDirectory(env, sessionId) {
|
|
30891
|
+
if (!SESSION_ID_PATH_SEGMENT.test(sessionId) || sessionId === "." || sessionId === "..") {
|
|
30892
|
+
throw new Error(`dispatch: the session id ${JSON.stringify(sessionId)} is not 1-128 letters, digits, ".", "_" or "-"`);
|
|
30893
|
+
}
|
|
30894
|
+
return join4(sessionStateRoot(env), "sessions", sessionId);
|
|
30895
|
+
}
|
|
30896
|
+
function writeAtomically(path, text) {
|
|
30897
|
+
const temporary = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
30898
|
+
try {
|
|
30899
|
+
writeFileSync2(temporary, text, { mode: PRIVATE_FILE });
|
|
30900
|
+
renameSync2(temporary, path);
|
|
30901
|
+
} catch (error) {
|
|
30902
|
+
rmSync2(temporary, { force: true });
|
|
30903
|
+
throw error;
|
|
30904
|
+
}
|
|
30905
|
+
}
|
|
30906
|
+
|
|
30907
|
+
// ../envoy-client/src/dispatch-commands/api.ts
|
|
30908
|
+
var GATED_WHY = "this session's AskGate reviews Dispatch writes, so dispatch api sends only GET here";
|
|
30909
|
+
var NO_ROW = "the dispatch command that makes it (dispatch --help lists them)";
|
|
30910
|
+
var WRITE_ROUTES = [
|
|
30911
|
+
{ method: "POST", pattern: "/api/v1/issues", commands: ["issue"] },
|
|
30912
|
+
{ method: "PATCH", pattern: "/api/v1/issues/{key}", commands: ["issue-update"] },
|
|
30913
|
+
{ method: "POST", pattern: "/api/v1/issues/{key}/status-change", commands: ["issue-update"] },
|
|
30914
|
+
{ method: "POST", pattern: "/api/v1/issues/{key}/claim", commands: ["claim"] },
|
|
30915
|
+
{ method: "DELETE", pattern: "/api/v1/issues/{key}/claim", commands: ["claim"] },
|
|
30916
|
+
{ method: "POST", pattern: "/api/v1/issues/{key}/asks", commands: ["ask"] },
|
|
30917
|
+
{ method: "POST", pattern: "/api/v1/artifacts/{id}/asks", commands: ["ask"] },
|
|
30918
|
+
{ method: "PATCH", pattern: "/api/v1/asks/{id}", commands: ["edit-ask"] },
|
|
30919
|
+
{ method: "POST", pattern: "/api/v1/asks/{id}/resolve", commands: ["resolve-ask"] },
|
|
30920
|
+
{ method: "PUT", pattern: "/api/v1/asks/{id}/followers/{session}", commands: ["follow"] },
|
|
30921
|
+
{ method: "DELETE", pattern: "/api/v1/asks/{id}/followers/{session}", commands: ["follow"] },
|
|
30922
|
+
{ method: "POST", pattern: "/api/v1/issues/{key}/comments", commands: ["comment", "suggest"] },
|
|
30923
|
+
{ method: "POST", pattern: "/api/v1/artifacts/{id}/comments", commands: ["comment", "suggest"] },
|
|
30924
|
+
{ method: "POST", pattern: "/api/v1/comments/{id}/resolve", commands: ["resolve-comment"] },
|
|
30925
|
+
{ method: "PATCH", pattern: "/api/v1/comments/{id}", commands: ["edit-comment"] },
|
|
30926
|
+
{ method: "DELETE", pattern: "/api/v1/comments/{id}", commands: ["delete-comment"] },
|
|
30927
|
+
{ method: "PATCH", pattern: "/api/v1/messages/{id}", commands: ["edit-message"] },
|
|
30928
|
+
{ method: "DELETE", pattern: "/api/v1/messages/{id}", commands: ["delete-message"] },
|
|
30929
|
+
{
|
|
30930
|
+
method: "POST",
|
|
30931
|
+
pattern: "/api/v1/issues/{key}/messages",
|
|
30932
|
+
commands: ["message", "issue-update"]
|
|
30933
|
+
},
|
|
30934
|
+
{ method: "POST", pattern: "/api/v1/messages/{id}/reply", commands: ["message"] },
|
|
30935
|
+
{ method: "POST", pattern: "/api/v1/agents/{session}/artifacts", commands: ["message"] },
|
|
30936
|
+
{ method: "POST", pattern: "/api/v1/agents/{session}/messages", commands: ["send-to-agent"] },
|
|
30937
|
+
{ method: "POST", pattern: "/api/v1/artifacts/{id}/edits", commands: ["doc-edit"] },
|
|
30938
|
+
{
|
|
30939
|
+
method: "POST",
|
|
30940
|
+
pattern: "/api/v1/artifacts/{id}/approval-requests",
|
|
30941
|
+
commands: ["request-approval"]
|
|
30942
|
+
},
|
|
30943
|
+
{ method: "POST", pattern: "/api/v1/artifacts/{id}/plan-reviews", commands: ["plan-approve"] },
|
|
30944
|
+
{
|
|
30945
|
+
method: "POST",
|
|
30946
|
+
pattern: "/api/v1/artifacts/{id}/versions/{number}/reviews",
|
|
30947
|
+
commands: ["review"]
|
|
30948
|
+
},
|
|
30949
|
+
{ method: "POST", pattern: "/api/v1/issues/{key}/results", commands: ["result"] },
|
|
30950
|
+
{ method: "POST", pattern: "/api/v1/issues/{key}/results/dry-run", commands: ["result"] },
|
|
30951
|
+
{ method: "POST", pattern: "/api/v1/results/{id}/retract", commands: ["result"] },
|
|
30952
|
+
{ method: "POST", pattern: "/api/v1/issues/{key}/handoffs/{phase}", commands: ["handoff-write"] },
|
|
30953
|
+
{ method: "POST", pattern: "/api/v1/issues/{key}/artifacts", commands: ["artifact"] },
|
|
30954
|
+
{ method: "POST", pattern: "/api/v1/projects/{key}/artifacts", commands: ["artifact"] },
|
|
30955
|
+
{ method: "POST", pattern: "/api/v1/markdown/round-trip", commands: ["artifact"] },
|
|
30956
|
+
{ method: "POST", pattern: "/api/v1/projects", commands: ["project"] },
|
|
30957
|
+
{
|
|
30958
|
+
method: "PUT",
|
|
30959
|
+
pattern: "/api/v1/projects/{key}/proven-sessions",
|
|
30960
|
+
commands: ["project-update"]
|
|
30961
|
+
},
|
|
30962
|
+
{
|
|
30963
|
+
method: "DELETE",
|
|
30964
|
+
pattern: "/api/v1/projects/{key}/proven-sessions",
|
|
30965
|
+
commands: ["project-update"]
|
|
30966
|
+
},
|
|
30967
|
+
{ method: "POST", pattern: "/api/v1/projects/{key}/related", commands: ["related"] },
|
|
30968
|
+
{
|
|
30969
|
+
method: "POST",
|
|
30970
|
+
pattern: "/api/v1/projects/{key}/architecture-sources/sync",
|
|
30971
|
+
commands: ["architecture-sync"]
|
|
30972
|
+
},
|
|
30973
|
+
{
|
|
30974
|
+
method: "POST",
|
|
30975
|
+
pattern: "/api/v1/projects/{key}/architecture-source/sync",
|
|
30976
|
+
commands: ["architecture-sync"]
|
|
30977
|
+
},
|
|
30978
|
+
{
|
|
30979
|
+
method: "POST",
|
|
30980
|
+
pattern: "/api/v1/projects/{key}/architecture-sources/{owner}/{repo}/sync",
|
|
30981
|
+
commands: ["architecture-sync"]
|
|
30982
|
+
}
|
|
30983
|
+
];
|
|
30984
|
+
function writeRoute(method, path) {
|
|
30985
|
+
const segments = path.split("/");
|
|
30986
|
+
return WRITE_ROUTES.find((row) => {
|
|
30987
|
+
const pattern = row.pattern.split("/");
|
|
30988
|
+
return row.method === method && pattern.length === segments.length && pattern.every((part, at) => part.startsWith("{") ? segments[at] !== "" : part === segments[at]);
|
|
30989
|
+
});
|
|
30990
|
+
}
|
|
30991
|
+
|
|
30992
|
+
class ApiGateRefusal extends Error {
|
|
30993
|
+
askgate;
|
|
30994
|
+
name = "ApiGateRefusal";
|
|
30995
|
+
constructor(message, askgate) {
|
|
30996
|
+
super(message);
|
|
30997
|
+
this.askgate = askgate;
|
|
30998
|
+
}
|
|
30999
|
+
}
|
|
31000
|
+
function gatedWriteRefusal(method, path, env) {
|
|
31001
|
+
const gates = [];
|
|
31002
|
+
for (const variable of SESSION_VARIABLES) {
|
|
31003
|
+
const id = env[variable];
|
|
31004
|
+
if (id === undefined || id === "")
|
|
31005
|
+
continue;
|
|
31006
|
+
let dir;
|
|
31007
|
+
try {
|
|
31008
|
+
dir = sessionDirectory(env, id);
|
|
31009
|
+
} catch {
|
|
31010
|
+
return new ToolInputError("dispatch_api", [
|
|
31011
|
+
`${variable} holds no session id whose AskGate record can be read: an id is 1-128 letters, digits, ".", "_" or "-". Unset ${variable}, or set it to the id of your own session.`
|
|
31012
|
+
], { aboutAFlag: false });
|
|
31013
|
+
}
|
|
31014
|
+
gates.push(askgateState(dir));
|
|
31015
|
+
}
|
|
31016
|
+
const broken = gates.find((gate) => gate.kind === "broken");
|
|
31017
|
+
const gated = gates.some((gate) => gate.kind === "on");
|
|
31018
|
+
if (!gated && broken === undefined)
|
|
31019
|
+
return;
|
|
31020
|
+
const row = writeRoute(method, path.split("?", 1)[0] ?? "");
|
|
31021
|
+
const command = row === undefined ? NO_ROW : row.commands.map((name) => `dispatch ${name}`).join(" or ");
|
|
31022
|
+
return gated || broken === undefined ? new ApiGateRefusal(`dispatch api was not called: ${GATED_WHY}; make this write with ${command}`, { mode: "block", why: GATED_WHY }) : new ApiGateRefusal(`dispatch api was not called: AskGate's record for this session, ${broken.problem}, so whether AskGate reviews this session's Dispatch writes cannot be told, and dispatch api sends only GET here; make this write with ${command}`, { mode: "block", why: broken.problem });
|
|
31023
|
+
}
|
|
31024
|
+
async function sendApiCall(args, env, client) {
|
|
31025
|
+
const path = stringArg(args, "path");
|
|
31026
|
+
const method = optionalString(args, "method");
|
|
31027
|
+
if (method !== undefined) {
|
|
31028
|
+
const refusal = gatedWriteRefusal(method, path, env);
|
|
31029
|
+
if (refusal !== undefined)
|
|
31030
|
+
throw refusal;
|
|
31031
|
+
}
|
|
31032
|
+
const answer = method === undefined ? await client.readRoute(path) : await client.sendRoute(method, path, optionalString(args, "body"));
|
|
31033
|
+
const details = { path, ...defined("method", method) };
|
|
31034
|
+
if ("body" in answer) {
|
|
31035
|
+
const { body, type } = answer;
|
|
31036
|
+
return {
|
|
31037
|
+
text: `the answer's body as it came: ${body.byteLength} bytes (${type ?? "no Content-Type"})`,
|
|
31038
|
+
body,
|
|
31039
|
+
details: { ...details, ...defined("content_type", type), bytes: body.byteLength }
|
|
31040
|
+
};
|
|
31041
|
+
}
|
|
31042
|
+
return { text: JSON.stringify(answer.payload, null, 2), details };
|
|
31043
|
+
}
|
|
31044
|
+
async function apiCommand(context) {
|
|
31045
|
+
return await sendApiCall(context.args, context.input.env ?? {}, context.newClient());
|
|
31046
|
+
}
|
|
31047
|
+
|
|
30430
31048
|
// ../envoy-client/src/dispatch-commands/render-item.ts
|
|
30431
31049
|
function askSummary({ ask, replies, answers }, askRef, pictures, incoming, outgoing) {
|
|
30432
31050
|
const answer = ask.answer;
|
|
@@ -32690,38 +33308,8 @@ async function reviewCommand(context) {
|
|
|
32690
33308
|
}
|
|
32691
33309
|
|
|
32692
33310
|
// ../envoy-client/src/session-answers.ts
|
|
32693
|
-
import { mkdirSync, readFileSync } from "fs";
|
|
32694
|
-
import { join as
|
|
32695
|
-
|
|
32696
|
-
// ../envoy-client/src/dispatch-session-directory.ts
|
|
32697
|
-
import { randomUUID } from "crypto";
|
|
32698
|
-
import { renameSync, rmSync, writeFileSync } from "fs";
|
|
32699
|
-
import { homedir as homedir2 } from "os";
|
|
32700
|
-
import { join as join3 } from "path";
|
|
32701
|
-
var SESSION_ID_PATH_SEGMENT = /^[A-Za-z0-9._-]{1,128}$/;
|
|
32702
|
-
var PRIVATE_FILE = 384;
|
|
32703
|
-
function sessionStateRoot(env) {
|
|
32704
|
-
const stateDir = env["DISPATCH_STATE_DIR"];
|
|
32705
|
-
if (stateDir)
|
|
32706
|
-
return stateDir;
|
|
32707
|
-
return join3(env["XDG_STATE_HOME"] || join3(homedir2(), ".local", "state"), "dispatch");
|
|
32708
|
-
}
|
|
32709
|
-
function sessionDirectory(env, sessionId) {
|
|
32710
|
-
if (!SESSION_ID_PATH_SEGMENT.test(sessionId) || sessionId === "." || sessionId === "..") {
|
|
32711
|
-
throw new Error(`dispatch: the session id ${JSON.stringify(sessionId)} is not 1-128 letters, digits, ".", "_" or "-"`);
|
|
32712
|
-
}
|
|
32713
|
-
return join3(sessionStateRoot(env), "sessions", sessionId);
|
|
32714
|
-
}
|
|
32715
|
-
function writeAtomically(path, text) {
|
|
32716
|
-
const temporary = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
32717
|
-
try {
|
|
32718
|
-
writeFileSync(temporary, text, { mode: PRIVATE_FILE });
|
|
32719
|
-
renameSync(temporary, path);
|
|
32720
|
-
} catch (error) {
|
|
32721
|
-
rmSync(temporary, { force: true });
|
|
32722
|
-
throw error;
|
|
32723
|
-
}
|
|
32724
|
-
}
|
|
33311
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync2 } from "fs";
|
|
33312
|
+
import { join as join5 } from "path";
|
|
32725
33313
|
|
|
32726
33314
|
// ../envoy-client/src/json-line-record.ts
|
|
32727
33315
|
import { appendFileSync } from "fs";
|
|
@@ -32861,7 +33449,7 @@ processGlobal2[STATE_KEY] = state;
|
|
|
32861
33449
|
var CACHE_FILE = "answers.json";
|
|
32862
33450
|
function readCacheFile(directory, url) {
|
|
32863
33451
|
try {
|
|
32864
|
-
const value = JSON.parse(
|
|
33452
|
+
const value = JSON.parse(readFileSync2(join5(directory, CACHE_FILE), "utf-8"));
|
|
32865
33453
|
if (typeof value === "object" && value !== null && "dispatch_url" in value && typeof value.dispatch_url === "string" && value.dispatch_url === url && "read_at" in value && typeof value.read_at === "string" && "total" in value && typeof value.total === "number" && "items" in value && Array.isArray(value.items)) {
|
|
32866
33454
|
return {
|
|
32867
33455
|
dispatch_url: value.dispatch_url,
|
|
@@ -32981,8 +33569,8 @@ async function readSessionAnswers(options) {
|
|
|
32981
33569
|
current.noRouteReported = false;
|
|
32982
33570
|
if (directory !== undefined) {
|
|
32983
33571
|
try {
|
|
32984
|
-
|
|
32985
|
-
writeAtomically(
|
|
33572
|
+
mkdirSync2(directory, { recursive: true, mode: 448 });
|
|
33573
|
+
writeAtomically(join5(directory, CACHE_FILE), JSON.stringify(cache));
|
|
32986
33574
|
} catch {}
|
|
32987
33575
|
}
|
|
32988
33576
|
return { cache };
|
|
@@ -33062,12 +33650,15 @@ async function whoamiCommand(context) {
|
|
|
33062
33650
|
const service = identity.kind === "agent" ? identity.service ?? null : null;
|
|
33063
33651
|
const proved = identity.kind === "agent" ? identity.session ?? null : null;
|
|
33064
33652
|
const project = identity.kind === "agent" ? identity.project ?? null : null;
|
|
33653
|
+
const projectScope = identity.kind === "agent" ? identity.project_scope ?? null : null;
|
|
33065
33654
|
const readOnly = identity.scopes?.includes("read") === true;
|
|
33066
33655
|
const cut = identity.kind === "agent" ? identity.cut ?? null : null;
|
|
33067
33656
|
const session = proved ?? sessionId;
|
|
33068
33657
|
const unowned = "no owner, so an issue you create without an assignee takes its parent's assignee when the parent is in the same project, else the project's default assignee, which every project names, so the issue always has one.";
|
|
33658
|
+
const brokerService = owner === null && service !== null && proved !== null;
|
|
33069
33659
|
const answered = identity.kind === "agent" ? identity.sign_in_required : undefined;
|
|
33070
|
-
const signInNeeded = answered ??
|
|
33660
|
+
const signInNeeded = answered ?? brokerService;
|
|
33661
|
+
const writesAsService = brokerService && !signInNeeded;
|
|
33071
33662
|
const who = owner !== null ? `acts for ${owner}` : service !== null ? `runs as service ${serviceSubjectLabel(service)}` : "runs";
|
|
33072
33663
|
const named = proved !== null && proved !== sessionId ? ` Your credential names session ${proved}, which is not this host session (${sessionId})` : "";
|
|
33073
33664
|
let acting;
|
|
@@ -33079,15 +33670,17 @@ async function whoamiCommand(context) {
|
|
|
33079
33670
|
acting = `Session ${session} ${who}: issues you create without an assignee are assigned to ${owner}.`;
|
|
33080
33671
|
} else if (signInNeeded) {
|
|
33081
33672
|
acting = `Session ${session} ${who} and no person, so it may read but Dispatch refuses every write it makes: run \`agent-secrets login\` on this machine to write as yourself.`;
|
|
33673
|
+
} else if (writesAsService) {
|
|
33674
|
+
acting = `Session ${session} ${who} and no person, and writes as that service with ${unowned}`;
|
|
33082
33675
|
} else if (service !== null) {
|
|
33083
33676
|
acting = `Session ${session} ${who}: ${unowned}`;
|
|
33084
33677
|
} else {
|
|
33085
33678
|
acting = `Session ${session} runs under the shared token: ${unowned}`;
|
|
33086
33679
|
}
|
|
33087
|
-
const writable = project !== null
|
|
33680
|
+
const writable = readOnly ? "" : project !== null ? ` It may write project ${project} and no other.` : writesAsService ? " It names no one project, so it may write wherever a token that names no project may." : "";
|
|
33088
33681
|
return {
|
|
33089
33682
|
text: acting + elsewhere + writable,
|
|
33090
|
-
details: { session, owner, service, project }
|
|
33683
|
+
details: { session, owner, service, project, project_scope: projectScope }
|
|
33091
33684
|
};
|
|
33092
33685
|
}
|
|
33093
33686
|
async function limitsCommand(context) {
|
|
@@ -33363,7 +33956,7 @@ async function executeDispatchTool(input) {
|
|
|
33363
33956
|
|
|
33364
33957
|
// ../cipher-client/src/environment.ts
|
|
33365
33958
|
import { readFile as readFile6 } from "fs/promises";
|
|
33366
|
-
import { join as
|
|
33959
|
+
import { join as join8 } from "path";
|
|
33367
33960
|
|
|
33368
33961
|
// ../cipher-client/src/device-login.ts
|
|
33369
33962
|
import { generateKeyPairSync as generateKeyPairSync2, randomUUID as randomUUID5 } from "crypto";
|
|
@@ -33723,7 +34316,7 @@ async function sendWithProof(proved, url, options, signal) {
|
|
|
33723
34316
|
|
|
33724
34317
|
// ../cipher-client/src/device-login-file.ts
|
|
33725
34318
|
import { createPrivateKey, randomUUID as randomUUID3 } from "crypto";
|
|
33726
|
-
import { constants as
|
|
34319
|
+
import { constants as constants3 } from "fs";
|
|
33727
34320
|
import {
|
|
33728
34321
|
access as access2,
|
|
33729
34322
|
lstat as lstat3,
|
|
@@ -33736,11 +34329,11 @@ import {
|
|
|
33736
34329
|
stat as stat3
|
|
33737
34330
|
} from "fs/promises";
|
|
33738
34331
|
import { userInfo } from "os";
|
|
33739
|
-
import { dirname as dirname3, isAbsolute as
|
|
34332
|
+
import { dirname as dirname3, isAbsolute as isAbsolute3, join as join7 } from "path";
|
|
33740
34333
|
|
|
33741
34334
|
// ../cipher-client/src/device-login-lock.ts
|
|
33742
34335
|
import { randomUUID as randomUUID2 } from "crypto";
|
|
33743
|
-
import { closeSync, constants, fstatSync, openSync, readSync } from "fs";
|
|
34336
|
+
import { closeSync as closeSync2, constants as constants2, fstatSync, openSync as openSync2, readSync } from "fs";
|
|
33744
34337
|
import {
|
|
33745
34338
|
link,
|
|
33746
34339
|
lstat as lstat2,
|
|
@@ -33751,7 +34344,7 @@ import {
|
|
|
33751
34344
|
stat as stat2,
|
|
33752
34345
|
utimes
|
|
33753
34346
|
} from "fs/promises";
|
|
33754
|
-
import { basename as basename2, dirname as dirname2, join as
|
|
34347
|
+
import { basename as basename2, dirname as dirname2, join as join6 } from "path";
|
|
33755
34348
|
|
|
33756
34349
|
// ../cipher-client/src/token-client.ts
|
|
33757
34350
|
var DEFAULT_RENEW_BEFORE_MS = 5 * 60000;
|
|
@@ -34161,7 +34754,7 @@ function heldUntil(token) {
|
|
|
34161
34754
|
var DEVICE_LOGIN_STALE_LOCK_MS = TRADE_ATTEMPT_MS / 2;
|
|
34162
34755
|
var LOCK_HEARTBEAT_MS = DEVICE_LOGIN_STALE_LOCK_MS / 3;
|
|
34163
34756
|
var DEVICE_LOGIN_MOST_BYTES = 64 * 1024;
|
|
34164
|
-
var LOCK_READ =
|
|
34757
|
+
var LOCK_READ = constants2.O_RDONLY | constants2.O_NONBLOCK | constants2.O_NOCTTY | constants2.O_NOFOLLOW;
|
|
34165
34758
|
var LOCK_RETRY_MS = 100;
|
|
34166
34759
|
var STEAL_STALE_MS = 5000;
|
|
34167
34760
|
async function statOrNone(path, what) {
|
|
@@ -34316,7 +34909,7 @@ async function withLoginLock(path, named, work, options = {}) {
|
|
|
34316
34909
|
function loginLockHeldBy(path, pid) {
|
|
34317
34910
|
let fd;
|
|
34318
34911
|
try {
|
|
34319
|
-
fd =
|
|
34912
|
+
fd = openSync2(`${path}.lock`, LOCK_READ);
|
|
34320
34913
|
} catch {
|
|
34321
34914
|
return false;
|
|
34322
34915
|
}
|
|
@@ -34330,7 +34923,7 @@ function loginLockHeldBy(path, pid) {
|
|
|
34330
34923
|
return false;
|
|
34331
34924
|
} finally {
|
|
34332
34925
|
try {
|
|
34333
|
-
|
|
34926
|
+
closeSync2(fd);
|
|
34334
34927
|
} catch {}
|
|
34335
34928
|
}
|
|
34336
34929
|
}
|
|
@@ -34340,7 +34933,7 @@ function namedWithUuid(name, prefix) {
|
|
|
34340
34933
|
async function removeUntouched(dir, left) {
|
|
34341
34934
|
const names = await readdir2(dir).catch(() => []);
|
|
34342
34935
|
for (const name of names.filter(left)) {
|
|
34343
|
-
const path =
|
|
34936
|
+
const path = join6(dir, name);
|
|
34344
34937
|
const info = await stat2(path).catch(() => null);
|
|
34345
34938
|
if (info !== null && Date.now() - info.mtimeMs > DEVICE_LOGIN_STALE_LOCK_MS) {
|
|
34346
34939
|
await rm2(path, { force: true }).catch(() => {});
|
|
@@ -34365,7 +34958,7 @@ function deviceLoginFile(env) {
|
|
|
34365
34958
|
if (home === "") {
|
|
34366
34959
|
throw new Error(`${DEVICE_LOGIN_FILE_VAR} names no file and HOME names no home directory, so this machine has nowhere to keep its device login`);
|
|
34367
34960
|
}
|
|
34368
|
-
return
|
|
34961
|
+
return join7(home, ".config", "legion", "device-login.json");
|
|
34369
34962
|
}
|
|
34370
34963
|
function member(json, name) {
|
|
34371
34964
|
const value = json[name];
|
|
@@ -34496,7 +35089,7 @@ async function lstatOnTheWay(path, what) {
|
|
|
34496
35089
|
}
|
|
34497
35090
|
}
|
|
34498
35091
|
async function walkTheWay(path, what, meet) {
|
|
34499
|
-
const names = (
|
|
35092
|
+
const names = (isAbsolute3(path) ? path : `${process.cwd()}/${path}`).split("/");
|
|
34500
35093
|
let reached = "/";
|
|
34501
35094
|
let info = await lstatOnTheWay(reached, what);
|
|
34502
35095
|
let followed = 0;
|
|
@@ -34534,7 +35127,7 @@ async function walkTheWay(path, what, meet) {
|
|
|
34534
35127
|
continue;
|
|
34535
35128
|
}
|
|
34536
35129
|
names.unshift(...target.split("/"));
|
|
34537
|
-
if (
|
|
35130
|
+
if (isAbsolute3(target)) {
|
|
34538
35131
|
reached = "/";
|
|
34539
35132
|
info = await lstatOnTheWay(reached, what);
|
|
34540
35133
|
}
|
|
@@ -34552,7 +35145,7 @@ async function homeReached() {
|
|
|
34552
35145
|
return null;
|
|
34553
35146
|
}
|
|
34554
35147
|
}
|
|
34555
|
-
if (!
|
|
35148
|
+
if (!isAbsolute3(home))
|
|
34556
35149
|
return null;
|
|
34557
35150
|
return await walkTheWay(home, "the home directory").catch(() => null);
|
|
34558
35151
|
}
|
|
@@ -34610,7 +35203,7 @@ async function deviceLoginPlace(path) {
|
|
|
34610
35203
|
await deviceLoginDirectory(path);
|
|
34611
35204
|
const dir = dirname3(path);
|
|
34612
35205
|
try {
|
|
34613
|
-
await access2(dir,
|
|
35206
|
+
await access2(dir, constants3.W_OK | constants3.X_OK);
|
|
34614
35207
|
} catch (error) {
|
|
34615
35208
|
const code = readErrorCode(error);
|
|
34616
35209
|
throw new Error(`this user cannot enter and write to the directory of ${DEVICE_LOGIN_FILE_NAMED}: ${code}${code === "EACCES" ? "; run chmod 700 on it" : ""}`);
|
|
@@ -34626,7 +35219,7 @@ async function deviceLoginPlace(path) {
|
|
|
34626
35219
|
if (!info.isFile())
|
|
34627
35220
|
throw notARegularFile(info);
|
|
34628
35221
|
}
|
|
34629
|
-
var LOGIN_READ =
|
|
35222
|
+
var LOGIN_READ = constants3.O_RDONLY | constants3.O_NONBLOCK | constants3.O_NOCTTY;
|
|
34630
35223
|
var LINKS_FOLLOWED_AT_MOST = 40;
|
|
34631
35224
|
async function checkLinkDirectories(path, absent) {
|
|
34632
35225
|
let checked = dirname3(path);
|
|
@@ -34657,7 +35250,7 @@ async function checkLinkDirectories(path, absent) {
|
|
|
34657
35250
|
return;
|
|
34658
35251
|
throw refusedBy(error, `read ${DEVICE_LOGIN_FILE_NAMED}`);
|
|
34659
35252
|
}
|
|
34660
|
-
at =
|
|
35253
|
+
at = isAbsolute3(target) ? target : `${dirname3(at)}/${target}`;
|
|
34661
35254
|
}
|
|
34662
35255
|
}
|
|
34663
35256
|
async function readDeviceLogin(path) {
|
|
@@ -34773,7 +35366,7 @@ async function saveDeviceLogin(path, login, held, keep = false) {
|
|
|
34773
35366
|
await deviceLoginPlace(path);
|
|
34774
35367
|
await removeUntouched(dir, isTemporaryCopy);
|
|
34775
35368
|
await removeLeftLocks(path);
|
|
34776
|
-
const temp =
|
|
35369
|
+
const temp = join7(dir, `.device-login-${randomUUID3()}.json.tmp`);
|
|
34777
35370
|
const saveFailed = (error) => new Error(`save ${DEVICE_LOGIN_FILE_NAMED}: ${readErrorCode(error)}`);
|
|
34778
35371
|
let whole = false;
|
|
34779
35372
|
try {
|
|
@@ -34871,7 +35464,7 @@ function unpollableAnswer(answered, intervalSeconds, lifetimeSeconds, left) {
|
|
|
34871
35464
|
|
|
34872
35465
|
// ../cipher-client/src/dpop.ts
|
|
34873
35466
|
import {
|
|
34874
|
-
createHash,
|
|
35467
|
+
createHash as createHash2,
|
|
34875
35468
|
createPrivateKey as createPrivateKey2,
|
|
34876
35469
|
createPublicKey,
|
|
34877
35470
|
generateKeyPairSync,
|
|
@@ -34899,7 +35492,7 @@ function dpopKey(privateKey, publicJwk) {
|
|
|
34899
35492
|
if (nonce !== undefined && nonce !== "")
|
|
34900
35493
|
claims["nonce"] = nonce;
|
|
34901
35494
|
if (accessToken !== undefined && accessToken !== "") {
|
|
34902
|
-
claims["ath"] =
|
|
35495
|
+
claims["ath"] = createHash2("sha256").update(accessToken).digest("base64url");
|
|
34903
35496
|
}
|
|
34904
35497
|
const signingInput = `${header}.${Buffer.from(JSON.stringify(claims)).toString("base64url")}`;
|
|
34905
35498
|
const signature = sign2("sha256", Buffer.from(signingInput), {
|
|
@@ -34946,6 +35539,9 @@ async function importDpopKey(pkcs8Pem, name) {
|
|
|
34946
35539
|
return dpopKey(privateKey, publicJwkOf(privateKey, name));
|
|
34947
35540
|
}
|
|
34948
35541
|
|
|
35542
|
+
// ../cipher-client/src/loopback.ts
|
|
35543
|
+
var LOOPBACK_HOST = /^(localhost|127(\.\d{1,3}){3}|\[::1\])$/i;
|
|
35544
|
+
|
|
34949
35545
|
// ../cipher-client/src/device-login.ts
|
|
34950
35546
|
var BROKER_SETTING = "AGENT_SECRETS_URL";
|
|
34951
35547
|
var ACCESS_MARGIN_MS = 2 * TRADE_ATTEMPT_MS;
|
|
@@ -34955,7 +35551,6 @@ function leastAccessLifeMs(leastLifeMs, lifeSeconds) {
|
|
|
34955
35551
|
return Math.max(ACCESS_MARGIN_MS, Math.min(leastLifeMs, lifeSeconds * 1000 / 2));
|
|
34956
35552
|
}
|
|
34957
35553
|
var BROKER_REQUEST_MS = TRADE_ATTEMPT_MS / 2;
|
|
34958
|
-
var LOOPBACK_HOST = /^(localhost|127(\.\d{1,3}){3}|\[::1\])$/i;
|
|
34959
35554
|
var UNPRINTABLE = /[\p{Cc}\p{Cf}]/u;
|
|
34960
35555
|
function deviceLoginSessionName(thumbprint) {
|
|
34961
35556
|
return `device-login-${thumbprint.slice(0, 12)}`;
|
|
@@ -35261,13 +35856,107 @@ async function refreshDeviceLogin(path, login, options) {
|
|
|
35261
35856
|
return next;
|
|
35262
35857
|
}
|
|
35263
35858
|
|
|
35859
|
+
// ../cipher-client/src/answer-text.ts
|
|
35860
|
+
var MAX_ANSWER_BYTES = 1 << 20;
|
|
35861
|
+
async function answerText(answer) {
|
|
35862
|
+
const reader = answer.body?.getReader();
|
|
35863
|
+
if (reader === undefined)
|
|
35864
|
+
return "";
|
|
35865
|
+
const chunks = [];
|
|
35866
|
+
let read = 0;
|
|
35867
|
+
while (read < MAX_ANSWER_BYTES) {
|
|
35868
|
+
const { done, value } = await reader.read();
|
|
35869
|
+
if (done)
|
|
35870
|
+
break;
|
|
35871
|
+
const kept = value.subarray(0, MAX_ANSWER_BYTES - read);
|
|
35872
|
+
chunks.push(kept);
|
|
35873
|
+
read += kept.length;
|
|
35874
|
+
}
|
|
35875
|
+
await reader.cancel().catch(() => {
|
|
35876
|
+
return;
|
|
35877
|
+
});
|
|
35878
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
35879
|
+
}
|
|
35880
|
+
|
|
35881
|
+
// ../cipher-client/src/machine-audience.ts
|
|
35882
|
+
var MACHINE_AUDIENCE_VAR = "AGENT_SECRETS_MACHINE_AUDIENCE";
|
|
35883
|
+
|
|
35884
|
+
// ../cipher-client/src/github-actions-identity.ts
|
|
35885
|
+
var ACTIONS_ID_TOKEN_REQUEST_URL_VAR = "ACTIONS_ID_TOKEN_REQUEST_URL";
|
|
35886
|
+
var ACTIONS_ID_TOKEN_REQUEST_TOKEN_VAR = "ACTIONS_ID_TOKEN_REQUEST_TOKEN";
|
|
35887
|
+
var TIMEOUT_MS = 20000;
|
|
35888
|
+
var ASKING = `ask GitHub for this run's OIDC token at the URL ${ACTIONS_ID_TOKEN_REQUEST_URL_VAR} names`;
|
|
35889
|
+
var NOT_HTTPS = `${ACTIONS_ID_TOKEN_REQUEST_URL_VAR} is not an https URL: the request token is sent there, so it takes https, or http to 127.0.0.1, ::1 or localhost`;
|
|
35890
|
+
function requestEndpoint(requestUrl, audience) {
|
|
35891
|
+
let endpoint;
|
|
35892
|
+
try {
|
|
35893
|
+
endpoint = new URL(requestUrl);
|
|
35894
|
+
} catch {
|
|
35895
|
+
throw new Error(NOT_HTTPS);
|
|
35896
|
+
}
|
|
35897
|
+
const loopback = endpoint.protocol === "http:" && LOOPBACK_HOST.test(endpoint.hostname);
|
|
35898
|
+
if (endpoint.protocol !== "https:" && !loopback)
|
|
35899
|
+
throw new Error(NOT_HTTPS);
|
|
35900
|
+
endpoint.searchParams.set("audience", audience);
|
|
35901
|
+
return endpoint.href;
|
|
35902
|
+
}
|
|
35903
|
+
function githubActionsIdentity(options) {
|
|
35904
|
+
holdSecret(options.requestToken);
|
|
35905
|
+
holdUrlUserinfo(options.requestUrl);
|
|
35906
|
+
const send = options.fetch ?? fetch;
|
|
35907
|
+
return {
|
|
35908
|
+
async read(signal) {
|
|
35909
|
+
const endpoint = requestEndpoint(options.requestUrl, options.audience);
|
|
35910
|
+
const signals = [AbortSignal.timeout(TIMEOUT_MS), signal].filter((one) => one !== undefined);
|
|
35911
|
+
let answer;
|
|
35912
|
+
try {
|
|
35913
|
+
answer = await send(endpoint, {
|
|
35914
|
+
method: "GET",
|
|
35915
|
+
headers: { Authorization: `bearer ${options.requestToken}` },
|
|
35916
|
+
signal: AbortSignal.any(signals),
|
|
35917
|
+
redirect: "manual"
|
|
35918
|
+
});
|
|
35919
|
+
} catch (error) {
|
|
35920
|
+
throw new Error(`${ASKING}: no answer: ${errorClass(error)}`);
|
|
35921
|
+
}
|
|
35922
|
+
if (answer.status < 200 || answer.status > 299) {
|
|
35923
|
+
await answer.body?.cancel().catch(() => {
|
|
35924
|
+
return;
|
|
35925
|
+
});
|
|
35926
|
+
throw new Error(`${ASKING}: HTTP ${answer.status}`);
|
|
35927
|
+
}
|
|
35928
|
+
let text;
|
|
35929
|
+
try {
|
|
35930
|
+
text = await answerText(answer);
|
|
35931
|
+
} catch (error) {
|
|
35932
|
+
throw new Error(`${ASKING}: no answer: ${errorClass(error)}`);
|
|
35933
|
+
}
|
|
35934
|
+
let parsed;
|
|
35935
|
+
try {
|
|
35936
|
+
parsed = JSON.parse(text);
|
|
35937
|
+
} catch {
|
|
35938
|
+
throw new Error(`${ASKING}: HTTP ${answer.status}: the answer is not JSON`);
|
|
35939
|
+
}
|
|
35940
|
+
const value = typeof parsed === "object" && parsed !== null && "value" in parsed ? parsed.value : undefined;
|
|
35941
|
+
if (typeof value !== "string" || value === "") {
|
|
35942
|
+
throw new Error(`${ASKING}: the answer holds no value`);
|
|
35943
|
+
}
|
|
35944
|
+
return { token: value, tokenType: SUBJECT_TOKEN_TYPE_GITHUB };
|
|
35945
|
+
},
|
|
35946
|
+
describe: () => {
|
|
35947
|
+
const audience = isCleanUrl(options.audience) ? `${options.audience} (${MACHINE_AUDIENCE_VAR})` : `the audience ${MACHINE_AUDIENCE_VAR} names`;
|
|
35948
|
+
return `this GitHub Actions run's OIDC token for ${audience}, asked for at the URL ${ACTIONS_ID_TOKEN_REQUEST_URL_VAR} names with the token ${ACTIONS_ID_TOKEN_REQUEST_TOKEN_VAR} names`;
|
|
35949
|
+
}
|
|
35950
|
+
};
|
|
35951
|
+
}
|
|
35952
|
+
|
|
35264
35953
|
// ../cipher-client/src/metadata-identity.ts
|
|
35265
35954
|
import { Agent, request } from "http";
|
|
35266
35955
|
var METADATA_HOST_VAR = "GCE_METADATA_HOST";
|
|
35267
35956
|
var DEFAULT_METADATA_HOST = "metadata.google.internal";
|
|
35268
35957
|
var IDENTITY_PATH = "/computeMetadata/v1/instance/service-accounts/default/identity";
|
|
35269
|
-
var
|
|
35270
|
-
var
|
|
35958
|
+
var TIMEOUT_MS2 = 1e4;
|
|
35959
|
+
var MAX_ANSWER_BYTES2 = 1 << 20;
|
|
35271
35960
|
var agent = new Agent({ keepAlive: false });
|
|
35272
35961
|
function tokenUrl(audience, env) {
|
|
35273
35962
|
const host = (env[METADATA_HOST_VAR] ?? "").trim() || DEFAULT_METADATA_HOST;
|
|
@@ -35282,7 +35971,7 @@ function get(url, signal, lookup) {
|
|
|
35282
35971
|
agent,
|
|
35283
35972
|
method: "GET",
|
|
35284
35973
|
headers: { "Metadata-Flavor": "Google" },
|
|
35285
|
-
timeout:
|
|
35974
|
+
timeout: TIMEOUT_MS2,
|
|
35286
35975
|
...signal === undefined ? {} : { signal },
|
|
35287
35976
|
...lookup === undefined ? {} : { lookup }
|
|
35288
35977
|
}, (answer) => {
|
|
@@ -35290,13 +35979,13 @@ function get(url, signal, lookup) {
|
|
|
35290
35979
|
let read = 0;
|
|
35291
35980
|
answer.on("data", (chunk) => {
|
|
35292
35981
|
read += chunk.length;
|
|
35293
|
-
if (read <=
|
|
35982
|
+
if (read <= MAX_ANSWER_BYTES2)
|
|
35294
35983
|
chunks.push(chunk);
|
|
35295
35984
|
});
|
|
35296
35985
|
answer.on("end", () => resolve({ status: answer.statusCode ?? 0, body: Buffer.concat(chunks).toString("utf8") }));
|
|
35297
35986
|
answer.on("error", reject);
|
|
35298
35987
|
});
|
|
35299
|
-
call.on("timeout", () => call.destroy(Object.assign(new Error(`no answer within ${
|
|
35988
|
+
call.on("timeout", () => call.destroy(Object.assign(new Error(`no answer within ${TIMEOUT_MS2} ms`), { code: "ETIMEDOUT" })));
|
|
35300
35989
|
call.on("error", reject);
|
|
35301
35990
|
call.end();
|
|
35302
35991
|
return promise;
|
|
@@ -35409,7 +36098,6 @@ var REQUESTED_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token";
|
|
|
35409
36098
|
var SCOPE = "https://www.googleapis.com/auth/cloud-platform";
|
|
35410
36099
|
var SUBJECT_TOKEN_TYPE_JWT = "urn:ietf:params:oauth:token-type:jwt";
|
|
35411
36100
|
var POOL_TIMEOUT_MS = 20000;
|
|
35412
|
-
var MAX_ANSWER_BYTES2 = 1 << 20;
|
|
35413
36101
|
var STS_SETTING = "securityTokenServiceUrl";
|
|
35414
36102
|
var IAM_SETTING = "iamCredentialsUrl";
|
|
35415
36103
|
function poolSubjectTokenType(brokerType) {
|
|
@@ -35418,25 +36106,6 @@ function poolSubjectTokenType(brokerType) {
|
|
|
35418
36106
|
function pathEscape(segment) {
|
|
35419
36107
|
return encodeURIComponent(segment).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`).replace(/%(?:24|26|2B|3A|3D|40)/g, decodeURIComponent);
|
|
35420
36108
|
}
|
|
35421
|
-
async function answerText(answer) {
|
|
35422
|
-
const reader = answer.body?.getReader();
|
|
35423
|
-
if (reader === undefined)
|
|
35424
|
-
return "";
|
|
35425
|
-
const chunks = [];
|
|
35426
|
-
let read = 0;
|
|
35427
|
-
while (read < MAX_ANSWER_BYTES2) {
|
|
35428
|
-
const { done, value } = await reader.read();
|
|
35429
|
-
if (done)
|
|
35430
|
-
break;
|
|
35431
|
-
const kept = value.subarray(0, MAX_ANSWER_BYTES2 - read);
|
|
35432
|
-
chunks.push(kept);
|
|
35433
|
-
read += kept.length;
|
|
35434
|
-
}
|
|
35435
|
-
await reader.cancel().catch(() => {
|
|
35436
|
-
return;
|
|
35437
|
-
});
|
|
35438
|
-
return Buffer.concat(chunks).toString("utf8");
|
|
35439
|
-
}
|
|
35440
36109
|
var GOOGLE_ERROR_CODE = /^[A-Za-z_]{1,64}$/;
|
|
35441
36110
|
function googleErrorCode(text) {
|
|
35442
36111
|
let parsed;
|
|
@@ -35565,7 +36234,6 @@ function projectedToken(path, variable) {
|
|
|
35565
36234
|
var BROKER_URL_VAR = "AGENT_SECRETS_URL";
|
|
35566
36235
|
var IDENTITY_VAR = "AGENT_SECRETS_MACHINE_IDENTITY";
|
|
35567
36236
|
var MACHINE_TOKEN_FILE_VAR = "AGENT_SECRETS_MACHINE_TOKEN_FILE";
|
|
35568
|
-
var MACHINE_AUDIENCE_VAR = "AGENT_SECRETS_MACHINE_AUDIENCE";
|
|
35569
36237
|
var KEY_DIR_VAR = "AGENT_SECRETS_KEY_DIR";
|
|
35570
36238
|
var ROLE_KEY_FILE = "key.pem";
|
|
35571
36239
|
var SESSION_VAR = "AGENT_SECRETS_SESSION";
|
|
@@ -35576,6 +36244,7 @@ var POOL_ACCOUNT_VAR = "AGENT_SECRETS_POOL_ACCOUNT";
|
|
|
35576
36244
|
var IDENTITY_KUBERNETES = "kubernetes";
|
|
35577
36245
|
var IDENTITY_METADATA = "metadata";
|
|
35578
36246
|
var IDENTITY_POOL = "pool";
|
|
36247
|
+
var IDENTITY_GITHUB_ACTIONS = "github-actions";
|
|
35579
36248
|
var IDENTITY_DEVICE_LOGIN = "device-login";
|
|
35580
36249
|
var BROKER_VARIABLES = [
|
|
35581
36250
|
BROKER_URL_VAR,
|
|
@@ -35587,6 +36256,8 @@ var BROKER_VARIABLES = [
|
|
|
35587
36256
|
PROJECTS_VAR,
|
|
35588
36257
|
POOL_AUDIENCE_VAR,
|
|
35589
36258
|
POOL_ACCOUNT_VAR,
|
|
36259
|
+
ACTIONS_ID_TOKEN_REQUEST_URL_VAR,
|
|
36260
|
+
ACTIONS_ID_TOKEN_REQUEST_TOKEN_VAR,
|
|
35590
36261
|
DEVICE_LOGIN_FILE_VAR,
|
|
35591
36262
|
SIGNIN_DIR_VAR,
|
|
35592
36263
|
"XDG_STATE_HOME",
|
|
@@ -35627,12 +36298,18 @@ function machineIdentity(env) {
|
|
|
35627
36298
|
tokenAudience
|
|
35628
36299
|
});
|
|
35629
36300
|
}
|
|
36301
|
+
case IDENTITY_GITHUB_ACTIONS: {
|
|
36302
|
+
const requestUrl = required2(env, ACTIONS_ID_TOKEN_REQUEST_URL_VAR, kind);
|
|
36303
|
+
const requestToken = required2(env, ACTIONS_ID_TOKEN_REQUEST_TOKEN_VAR, kind);
|
|
36304
|
+
const audience = required2(env, MACHINE_AUDIENCE_VAR, kind);
|
|
36305
|
+
return githubActionsIdentity({ requestUrl, requestToken, audience });
|
|
36306
|
+
}
|
|
35630
36307
|
case IDENTITY_DEVICE_LOGIN: {
|
|
35631
36308
|
const brokerUrl = required2(env, BROKER_URL_VAR, kind);
|
|
35632
36309
|
return new DeviceLoginIdentity({ path: deviceLoginFile(env), brokerUrl });
|
|
35633
36310
|
}
|
|
35634
36311
|
default:
|
|
35635
|
-
throw new Error(`${IDENTITY_VAR} names no identity this machine can prove; it takes ${IDENTITY_KUBERNETES}, ${IDENTITY_METADATA}, ${IDENTITY_POOL} or ${IDENTITY_DEVICE_LOGIN}`);
|
|
36312
|
+
throw new Error(`${IDENTITY_VAR} names no identity this machine can prove; it takes ${IDENTITY_KUBERNETES}, ${IDENTITY_METADATA}, ${IDENTITY_POOL}, ${IDENTITY_GITHUB_ACTIONS} or ${IDENTITY_DEVICE_LOGIN}`);
|
|
35636
36313
|
}
|
|
35637
36314
|
}
|
|
35638
36315
|
function tradeSession(env, own = "") {
|
|
@@ -35661,7 +36338,7 @@ async function machineKey(env) {
|
|
|
35661
36338
|
const dir = value(env, KEY_DIR_VAR);
|
|
35662
36339
|
if (value(env, IDENTITY_VAR) !== IDENTITY_KUBERNETES || dir === "")
|
|
35663
36340
|
return await newDpopKey();
|
|
35664
|
-
const path =
|
|
36341
|
+
const path = join8(dir, ROLE_KEY_FILE);
|
|
35665
36342
|
let pem;
|
|
35666
36343
|
try {
|
|
35667
36344
|
pem = await readFile6(path, "utf8");
|
|
@@ -35881,14 +36558,14 @@ function brokerCredential(env, session, read) {
|
|
|
35881
36558
|
}
|
|
35882
36559
|
|
|
35883
36560
|
// ../envoy-client/src/file-token-credential.ts
|
|
35884
|
-
import { readFileSync as
|
|
36561
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
35885
36562
|
var TOKEN_FILE_VAR = "DISPATCH_BROKER_TOKEN_FILE";
|
|
35886
36563
|
var DPOP_KEY_FILE_VAR = "DISPATCH_BROKER_DPOP_KEY_FILE";
|
|
35887
36564
|
var PKCS8_LABEL = "PRIVATE KEY";
|
|
35888
36565
|
var PKCS8_PEM = new RegExp(`^-----BEGIN ${PKCS8_LABEL}-----\\r?\\n(?:[A-Za-z0-9+/=]+\\r?\\n)+-----END ${PKCS8_LABEL}-----$`);
|
|
35889
36566
|
function readNamed(variable, filePath) {
|
|
35890
36567
|
try {
|
|
35891
|
-
return
|
|
36568
|
+
return readFileSync3(filePath, "utf8");
|
|
35892
36569
|
} catch (error) {
|
|
35893
36570
|
throw new Error(`${variable} names ${filePath}, which could not be read (${readErrorCode(error)})`);
|
|
35894
36571
|
}
|
|
@@ -36893,19 +37570,19 @@ function claudeProjectDirectory(environment, fallback) {
|
|
|
36893
37570
|
|
|
36894
37571
|
// src/codex/codex-session-record.ts
|
|
36895
37572
|
import { spawnSync } from "child_process";
|
|
36896
|
-
import { readFileSync as
|
|
37573
|
+
import { readFileSync as readFileSync5, readlinkSync as readlinkSync2, unwatchFile, watchFile } from "fs";
|
|
36897
37574
|
import { mkdir as mkdir3, readFile as readFile8, rename as rename4, utimes as utimes3, writeFile as writeFile2 } from "fs/promises";
|
|
36898
37575
|
import { homedir as homedir3 } from "os";
|
|
36899
|
-
import { basename as basename4, dirname as dirname5, join as
|
|
37576
|
+
import { basename as basename4, dirname as dirname5, join as join11 } from "path";
|
|
36900
37577
|
|
|
36901
37578
|
// src/session-identity.ts
|
|
36902
37579
|
import { readFile as readFile7 } from "fs/promises";
|
|
36903
|
-
import { basename as basename3, join as
|
|
37580
|
+
import { basename as basename3, join as join10 } from "path";
|
|
36904
37581
|
|
|
36905
37582
|
// src/session-state.ts
|
|
36906
|
-
import { readFileSync as
|
|
37583
|
+
import { readFileSync as readFileSync4 } from "fs";
|
|
36907
37584
|
import { mkdir as mkdir2, readdir as readdir3, rename as rename3, rm as rm4, stat as stat4, utimes as utimes2, writeFile } from "fs/promises";
|
|
36908
|
-
import { dirname as dirname4, join as
|
|
37585
|
+
import { dirname as dirname4, join as join9 } from "path";
|
|
36909
37586
|
async function writeAtomicStateFile(file, contents) {
|
|
36910
37587
|
await mkdir2(dirname4(file), { recursive: true, mode: 448 });
|
|
36911
37588
|
const temporary = `${file}.${process.pid}.${crypto.randomUUID()}.tmp`;
|
|
@@ -36923,7 +37600,7 @@ async function pruneStateDirectory(directory, isStale) {
|
|
|
36923
37600
|
throw error;
|
|
36924
37601
|
});
|
|
36925
37602
|
for (const entry of entries) {
|
|
36926
|
-
const file =
|
|
37603
|
+
const file = join9(directory, entry);
|
|
36927
37604
|
if (!await isStale(file))
|
|
36928
37605
|
continue;
|
|
36929
37606
|
await rm4(file, { recursive: true, force: true });
|
|
@@ -36938,7 +37615,7 @@ async function writtenLongAgo(file) {
|
|
|
36938
37615
|
}
|
|
36939
37616
|
var TITLES_DIRECTORY = "titles";
|
|
36940
37617
|
function sessionTitleFile(stateDirectory, sessionId) {
|
|
36941
|
-
return
|
|
37618
|
+
return join9(stateDirectory, TITLES_DIRECTORY, `${encodeURIComponent(sessionId)}.txt`);
|
|
36942
37619
|
}
|
|
36943
37620
|
async function writeChosenTitle(file, title) {
|
|
36944
37621
|
await writeAtomicStateFile(file, `${title}
|
|
@@ -36949,7 +37626,7 @@ async function removeChosenTitle(file) {
|
|
|
36949
37626
|
}
|
|
36950
37627
|
function readChosenTitleSync(file) {
|
|
36951
37628
|
try {
|
|
36952
|
-
return
|
|
37629
|
+
return readFileSync4(file, "utf8");
|
|
36953
37630
|
} catch {
|
|
36954
37631
|
return;
|
|
36955
37632
|
}
|
|
@@ -36966,7 +37643,7 @@ async function recordChosenTitle(sessionId, title) {
|
|
|
36966
37643
|
await writeChosenTitle(file, chosen);
|
|
36967
37644
|
}
|
|
36968
37645
|
async function pruneStaleSessionTitles(stateDirectory) {
|
|
36969
|
-
await pruneStateDirectory(
|
|
37646
|
+
await pruneStateDirectory(join9(stateDirectory, TITLES_DIRECTORY), writtenLongAgo);
|
|
36970
37647
|
}
|
|
36971
37648
|
|
|
36972
37649
|
// src/session-identity.ts
|
|
@@ -36991,14 +37668,14 @@ class SessionIdentity {
|
|
|
36991
37668
|
}
|
|
36992
37669
|
}
|
|
36993
37670
|
function sessionHandoffDirectory(stateDirectory, claudePid) {
|
|
36994
|
-
return
|
|
37671
|
+
return join10(stateDirectory, "sessions", String(claudePid));
|
|
36995
37672
|
}
|
|
36996
37673
|
function sessionHandoffFile(stateDirectory, claudePid) {
|
|
36997
|
-
return
|
|
37674
|
+
return join10(sessionHandoffDirectory(stateDirectory, claudePid), "session-id");
|
|
36998
37675
|
}
|
|
36999
37676
|
var ROLES_DIRECTORY = "roles";
|
|
37000
37677
|
function roleStateFile(stateDirectory, sessionId) {
|
|
37001
|
-
return
|
|
37678
|
+
return join10(stateDirectory, ROLES_DIRECTORY, `${encodeURIComponent(sessionId)}.json`);
|
|
37002
37679
|
}
|
|
37003
37680
|
|
|
37004
37681
|
class UnparseableRoleRecordError extends Error {
|
|
@@ -37049,14 +37726,14 @@ function processExists(pid) {
|
|
|
37049
37726
|
}
|
|
37050
37727
|
}
|
|
37051
37728
|
async function pruneStaleSessionHandoffs(stateDirectory) {
|
|
37052
|
-
await pruneStateDirectory(
|
|
37729
|
+
await pruneStateDirectory(join10(stateDirectory, "sessions"), (file) => {
|
|
37053
37730
|
const pid = basename3(file);
|
|
37054
37731
|
return /^\d+$/.test(pid) && !processExists(Number(pid));
|
|
37055
37732
|
});
|
|
37056
37733
|
}
|
|
37057
37734
|
async function pruneStaleSessionRoles(stateDirectory, currentSessionId) {
|
|
37058
37735
|
const own = roleStateFile(stateDirectory, currentSessionId);
|
|
37059
|
-
await pruneStateDirectory(
|
|
37736
|
+
await pruneStateDirectory(join10(stateDirectory, ROLES_DIRECTORY), async (file) => file !== own && await writtenLongAgo(file));
|
|
37060
37737
|
}
|
|
37061
37738
|
|
|
37062
37739
|
// src/codex/codex-session-record.ts
|
|
@@ -37070,10 +37747,10 @@ var CodexSessionRecord = object({
|
|
|
37070
37747
|
codex_started: string2().nullable().default(null)
|
|
37071
37748
|
});
|
|
37072
37749
|
function codexSessionRecordFile(stateDirectory, pid) {
|
|
37073
|
-
return
|
|
37750
|
+
return join11(stateDirectory, "codex-sessions", `${pid}.json`);
|
|
37074
37751
|
}
|
|
37075
37752
|
function codexEndedFile(stateDirectory, pid, sessionId) {
|
|
37076
|
-
return
|
|
37753
|
+
return join11(stateDirectory, "codex-sessions", `${pid}.${encodeURIComponent(sessionId)}.ended`);
|
|
37077
37754
|
}
|
|
37078
37755
|
async function markSessionEnded(stateDirectory, pid, sessionId) {
|
|
37079
37756
|
const marker = codexEndedFile(stateDirectory, pid, sessionId);
|
|
@@ -37118,7 +37795,7 @@ async function readCodexSessionRecord(file) {
|
|
|
37118
37795
|
}
|
|
37119
37796
|
function codexHome(env) {
|
|
37120
37797
|
const set = env["CODEX_HOME"];
|
|
37121
|
-
return set !== undefined && set !== "" ? set :
|
|
37798
|
+
return set !== undefined && set !== "" ? set : join11(env["HOME"] || homedir3(), ".codex");
|
|
37122
37799
|
}
|
|
37123
37800
|
function codexPluginDataDirectory(pluginRoot) {
|
|
37124
37801
|
const version = pluginRoot;
|
|
@@ -37128,7 +37805,7 @@ function codexPluginDataDirectory(pluginRoot) {
|
|
|
37128
37805
|
const plugins = dirname5(cache);
|
|
37129
37806
|
if (basename4(cache) !== "cache" || basename4(plugins) !== "plugins")
|
|
37130
37807
|
return;
|
|
37131
|
-
return
|
|
37808
|
+
return join11(plugins, "data", `${basename4(plugin)}-${basename4(marketplace)}`);
|
|
37132
37809
|
}
|
|
37133
37810
|
var MAX_ANCESTORS = 16;
|
|
37134
37811
|
function codexProcessId(readComm = procComm, readParent = procParent) {
|
|
@@ -37142,14 +37819,14 @@ function codexProcessId(readComm = procComm, readParent = procParent) {
|
|
|
37142
37819
|
}
|
|
37143
37820
|
function procComm(pid) {
|
|
37144
37821
|
try {
|
|
37145
|
-
return
|
|
37822
|
+
return readFileSync5(`/proc/${pid}/comm`, "utf8").trim();
|
|
37146
37823
|
} catch {
|
|
37147
37824
|
return;
|
|
37148
37825
|
}
|
|
37149
37826
|
}
|
|
37150
37827
|
function procParent(pid) {
|
|
37151
37828
|
try {
|
|
37152
|
-
const stat =
|
|
37829
|
+
const stat = readFileSync5(`/proc/${pid}/stat`, "utf8");
|
|
37153
37830
|
const parent = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]);
|
|
37154
37831
|
return Number.isInteger(parent) ? parent : undefined;
|
|
37155
37832
|
} catch {
|
|
@@ -37163,7 +37840,7 @@ var PS_OPTIONS = {
|
|
|
37163
37840
|
};
|
|
37164
37841
|
function procStarted(pid) {
|
|
37165
37842
|
try {
|
|
37166
|
-
const stat =
|
|
37843
|
+
const stat = readFileSync5(`/proc/${pid}/stat`, "utf8");
|
|
37167
37844
|
return stat.slice(stat.lastIndexOf(")") + 2).split(" ")[19];
|
|
37168
37845
|
} catch {
|
|
37169
37846
|
return;
|
|
@@ -37184,7 +37861,7 @@ function recordOfProcess(record, pid) {
|
|
|
37184
37861
|
function sharedAppServer(pid) {
|
|
37185
37862
|
let argv;
|
|
37186
37863
|
try {
|
|
37187
|
-
argv =
|
|
37864
|
+
argv = readFileSync5(`/proc/${pid}/cmdline`, "utf8").split("\x00");
|
|
37188
37865
|
} catch {
|
|
37189
37866
|
const ps = spawnSync("ps", ["-o", "args=", "-p", String(pid)], PS_OPTIONS);
|
|
37190
37867
|
if (ps.status !== 0)
|
|
@@ -37230,18 +37907,18 @@ function codexSubcommand(args) {
|
|
|
37230
37907
|
}
|
|
37231
37908
|
var SHARED_APP_SERVER_NOTE = "Codex runs this session in its shared app server (`codex app-server`), which runs several sessions in one process, so the Envoy plugin does not register it with Envoy: it receives no Envoy messages and does not show on Dispatch's Agents page. Its `dispatch` tool works, acting as this session.";
|
|
37232
37909
|
async function pruneCodexSessionRecords(stateDirectory) {
|
|
37233
|
-
await pruneStateDirectory(
|
|
37910
|
+
await pruneStateDirectory(join11(stateDirectory, "codex-sessions"), (file) => {
|
|
37234
37911
|
const match = /^(\d+)\.(?:json|.+\.ended)$/.exec(basename4(file));
|
|
37235
37912
|
return match !== null && !processExists(Number(match[1]));
|
|
37236
37913
|
});
|
|
37237
|
-
await pruneStateDirectory(
|
|
37914
|
+
await pruneStateDirectory(join11(stateDirectory, "codex-servers"), (file) => {
|
|
37238
37915
|
const name = basename4(file);
|
|
37239
37916
|
return /^\d+$/.test(name) && !processExists(Number(name));
|
|
37240
37917
|
});
|
|
37241
37918
|
}
|
|
37242
37919
|
function codexExecutable(pid) {
|
|
37243
37920
|
try {
|
|
37244
|
-
return
|
|
37921
|
+
return readlinkSync2(`/proc/${pid}/exe`);
|
|
37245
37922
|
} catch {
|
|
37246
37923
|
return;
|
|
37247
37924
|
}
|