@sjawhar/claude-envoy 5.4.1 → 5.6.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 +386 -38
- package/dist/dispatch.js +2029 -1783
- package/dist/envoy-channel.js +388 -40
- package/dist/session-hook.js +612 -64
- package/hooks/session-hook.ts +73 -27
- package/legion/agents/plan-reviewer.md +1 -1
- package/package.json +1 -1
- package/src/codex/codex-session-record.ts +18 -10
package/dist/session-hook.js
CHANGED
|
@@ -980,8 +980,8 @@ __export(exports_coerce, {
|
|
|
980
980
|
});
|
|
981
981
|
|
|
982
982
|
// hooks/session-hook.ts
|
|
983
|
-
import { appendFileSync, mkdirSync as mkdirSync2, readFileSync as
|
|
984
|
-
import { join as
|
|
983
|
+
import { appendFileSync, mkdirSync as mkdirSync2, readFileSync as readFileSync7, rmSync as rmSync3, statSync as statSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
984
|
+
import { join as join11 } from "path";
|
|
985
985
|
|
|
986
986
|
// ../contracts/src/held-secrets.ts
|
|
987
987
|
var WITHHELD = "<withheld>";
|
|
@@ -14272,7 +14272,7 @@ var DOCUMENT_IS_MARKDOWN = "a document holds Markdown, which rewrites other text
|
|
|
14272
14272
|
var PATH_KEEPS_BYTES = "path (--path <file>), which stores a file whose path does not end in .md or .markdown as its bytes, unchanged";
|
|
14273
14273
|
var ARTIFACT_CONTENT_NAME_RULE = `${CONTENT_IS_MARKDOWN}, so a document's name ends in .md or .markdown or has no extension, under content and under neither flag alike; upload any other file (.jsonl, .tsv, .json, .txt) with ${PATH_KEEPS_BYTES}.`;
|
|
14274
14274
|
var ARTIFACT_NAME_EXISTS_RULE = `An upload under the name of a document the owner already holds is refused: it writes that document's next version, over the text it holds. Change a document with dispatch doc-edit. To write the next version, send the upload again with force (--force) and the token of the version it replaces (--precondition-json '{"document":"sha256:\u2026"}', the token dispatch doc-read prints).`;
|
|
14275
|
-
var ARTIFACT_STORED_TEXT_RULE = `content and content-file hold text Dispatch stores as Markdown, and a text the store would write under another spelling is refused. The refusal prints the text Dispatch stores: send that text. A name that does not end in .md or .markdown has a second way out, which keeps the text as it was written: send it as a file with ${PATH_KEEPS_BYTES}.`;
|
|
14275
|
+
var ARTIFACT_STORED_TEXT_RULE = `content and content-file hold text Dispatch stores as Markdown, and a text the store would write under another spelling is refused. A typed block whose attributes differ from Dispatch's only in their order is not refused: the command uploads it with them in Dispatch's order and names each such block. A refusal names the first difference: a changed line by the attribute or the word that differs, a word of more than 200 characters by a window from its first character that differs, and a line the store adds or drops by a quote cut at 200 characters, each cut named. The refusal prints the text Dispatch stores: send that text. A name that does not end in .md or .markdown has a second way out, which keeps the text as it was written: send it as a file with ${PATH_KEEPS_BYTES}.`;
|
|
14276
14276
|
var NAME_EXTENSION = /\.([^\s.]+)$/u;
|
|
14277
14277
|
var MARKDOWN_EXTENSION = /^(md|markdown)$/i;
|
|
14278
14278
|
function preconditionArgument(z) {
|
|
@@ -14574,7 +14574,7 @@ var dispatchToolSpecs = [
|
|
|
14574
14574
|
{
|
|
14575
14575
|
name: "dispatch_issue",
|
|
14576
14576
|
example: { project: "DSP", title: "Native workspace" },
|
|
14577
|
-
description: "Create a native Dispatch issue for newly tracked work. Search first with dispatch search; if potentially duplicate issues exist, this returns 409 POSSIBLE_DUPLICATE unless --force is set after reading them, or, when the refusal cannot be recorded, creates the issue and names the near copy to read in the result. " + "
|
|
14577
|
+
description: "Create a native Dispatch issue for newly tracked work. Search first with dispatch search; if potentially duplicate issues exist, this returns 409 POSSIBLE_DUPLICATE unless --force is set after reading them, or, when the refusal cannot be recorded, creates the issue and names the near copy to read in the result. " + "Where the spec is large, or changed in a way you cannot check alone, a fresh read-only reader can find its gaps and check its factual claims; a Legion architect can use `plan-gap-analyst` and `plan-reviewer`. " + "A spec holding an ask block whose body breaks its content rule (one or more question paragraphs, then at most one bullet list of options, last) is refused with 400 INVALID_ASK_BLOCK. " + `${CI_CHECK_RULE} ` + "Create a project only when a person asks for one; work that spans projects goes in the project that owns most of it. " + `${RELATED_CHECK_RESULT} With --force, the result also names each issue whose title you forced past. ` + `Do not use it when an existing issue already covers the work; read or update that issue instead. ${ISSUE_REFERENCE}`,
|
|
14578
14578
|
arguments: (z) => ({
|
|
14579
14579
|
project: z.string().describe("Project key for the new issue."),
|
|
14580
14580
|
title: z.string({ max: ISSUE_TITLE_MAX }).describe("Concise issue title, at most 1,000 characters."),
|
|
@@ -14859,12 +14859,12 @@ var dispatchToolSpecs = [
|
|
|
14859
14859
|
{
|
|
14860
14860
|
name: "dispatch_request_approval",
|
|
14861
14861
|
example: { issue: "DSP-1", summary: "A live sync replaces the nightly export." },
|
|
14862
|
-
description: "Ask a human to approve a document at its current version.
|
|
14862
|
+
description: "Ask a human to approve a document at its current version. Where the spec is large, or changed in a way you cannot check alone, a fresh read-only reader can find its gaps and check its factual claims; a Legion architect can use `plan-gap-analyst` and `plan-reviewer`. Opens an approval ask (Approve / " + "Request changes) in the human's Inbox whose question names the document and version, " + "followed by the summary; the answer pins a review to that version and arrives as " + "artifact.approved or artifact.changes_requested. " + "The request reaches the person it was handed to, else the issue's assignee, else the " + "project's default assignee, so it always reaches a person, and this tool's answer names " + "that person. " + "An open request follows the document: a " + "later version moves it to that version and leaves it waiting on you, as a human's reply in " + "its thread does. Only the first move since the request was opened or handed back sends an " + "event, and never to the session whose version made it; dispatch doc-read shows whom it " + "waits on. Once the revision is complete and " + HUMAN_AGREED_TO_DOCUMENT + ", call this again to hand that same Inbox row back. The request carries nothing new. A " + "call while it already waits on the human hands nothing back: the same summary changes " + "nothing, and a different one is refused, since it would rewrite the card the human is " + "reading. Approve and Request changes each close the request, so the next call opens a new " + "one. Call it once per revision, when the revision is complete, never after each edit. An " + "approval goes stale when the document changes outside its decision blocks after it, until " + "a later version's text is exactly the approved version's again, as when an accidental " + "edit is undone exactly, and no decision block in it was decided since: that version " + "carries the approval and the document reads approved at it, so nothing is requested for " + "it. When the document does not read approved, request approval again once that revision " + "is complete and " + HUMAN_AGREED_TO_DOCUMENT + ", with a one-sentence summary naming what changed since the earlier approval: the card " + "names that approval (version, date, reviewer) and its Show diff control carries the detail. " + "A human's own answer to a decision block carries the approval to the version it writes when " + "that answer is the whole change since the approved version, so nothing is requested for " + "it. A new version of a Legion root spec closes its armed design gate until a human " + "approves it; a carried approval reopens it too. " + "Refused, with nothing sent, while the document holds an open decision block, even when a " + "human asked for approval: the refusal names each block; ask the human to answer or waive " + "it first. " + "Refused 409 PLAN_APPROVED_BY_REVIEW on an issue's plan, which its plan review approves " + "(dispatch plan-approve), never a person. " + "On an issue's spec the answer ends with a `Coverage of <name>:` line naming each requirement " + "the spec marks that no check serves, in any document of the issue or of its descendants, or saying " + "the spec marks no requirements; a spec every requirement of which has a check gets no such line. It never " + "names a missing plan task, and a gap refuses nothing. " + OWNER_REFERENCE,
|
|
14863
14863
|
arguments: (z) => ({
|
|
14864
14864
|
issue: z.string().describe(ISSUE_REFERENCE).optional(),
|
|
14865
14865
|
project: z.string().describe("Project key owning the document.").optional(),
|
|
14866
14866
|
artifact: z.string().describe("Project document artifact id, slug, or filename; primary document by default for an issue.").optional(),
|
|
14867
|
-
summary: z.string({ min: 1 }).describe(`What the human is approving, in one to three sentences, and nothing else: no commentary on itself or the conversation, and no question; after an earlier approval, one sentence naming what changed since it, since the card shows that approval and a diff.
|
|
14867
|
+
summary: z.string({ min: 1 }).describe(`What the human is approving, in one to three sentences, and nothing else: no commentary on itself or the conversation, and no question; after an earlier approval, one sentence naming what changed since it, since the card shows that approval and a diff. Request approval only once ${HUMAN_AGREED_TO_DOCUMENT}.`),
|
|
14868
14868
|
include_verification: z.boolean().describe("Ask for the issue's Verification document in the same request, beside this one: one Inbox card names both at their current versions, and the answer records the human's review on each. 409 NO_VERIFICATION_DOCUMENT when the issue has no verification.md. Without it the request names this document alone and records nothing on the Verification document.").optional()
|
|
14869
14869
|
}),
|
|
14870
14870
|
validation: documentOwnerValidation(true)
|
|
@@ -14946,7 +14946,7 @@ var dispatchToolSpecs = [
|
|
|
14946
14946
|
project: z.string().describe("Project key for an unlinked document.").optional(),
|
|
14947
14947
|
name: z.string().describe("Artifact filename shown in Dispatch."),
|
|
14948
14948
|
path: z.string().describe("Local path to the file to upload.").optional(),
|
|
14949
|
-
content: z.string().describe("Inline text to store as a Markdown document, under a name ending in .md or .markdown or with no extension. The command refuses a text the store would write under another spelling, a CR LF or a lone carriage return among them, which Dispatch stores as a line feed. A missing line feed at the end is not such a text: Dispatch adds one, and the upload goes on. The refusal prints the text Dispatch stores, which is the text to send.").optional(),
|
|
14949
|
+
content: z.string().describe("Inline text to store as a Markdown document, under a name ending in .md or .markdown or with no extension. The command refuses a text the store would write under another spelling, a CR LF or a lone carriage return among them, which Dispatch stores as a line feed. A missing line feed at the end is not such a text: Dispatch adds one, and the upload goes on. Nor is a typed block whose attributes differ only in their order: the upload puts them in Dispatch's order. The refusal names the first difference: a changed line by the attribute or the word that differs, a word of more than 200 characters by a window from its first character that differs, and a line the store adds or drops by a quote cut at 200 characters, each cut named. It prints the text Dispatch stores, which is the text to send.").optional(),
|
|
14950
14950
|
force: z.boolean().describe("Write the next version of a document the owner already holds under this name; without it the upload of a name that exists is refused.").optional(),
|
|
14951
14951
|
precondition: uploadPreconditionArgument(z),
|
|
14952
14952
|
summary: z.string().describe("Optional version summary.").optional()
|
|
@@ -19254,6 +19254,13 @@ function readSecretFile(variable, filePath) {
|
|
|
19254
19254
|
|
|
19255
19255
|
// ../envoy-client/src/envoy-config.ts
|
|
19256
19256
|
var COMMAND_TIMEOUT_MS = 1e4;
|
|
19257
|
+
var COMMAND_TIMEOUT_VARIABLE = "ENVOY_COMMAND_TIMEOUT_MS";
|
|
19258
|
+
var LONGEST_TIMEOUT_MS = 2147483647;
|
|
19259
|
+
function commandTimeoutMs(env) {
|
|
19260
|
+
const text = env[COMMAND_TIMEOUT_VARIABLE] ?? "";
|
|
19261
|
+
const ms = /^[1-9]\d*$/.test(text) ? Number(text) : Number.NaN;
|
|
19262
|
+
return ms <= LONGEST_TIMEOUT_MS ? ms : COMMAND_TIMEOUT_MS;
|
|
19263
|
+
}
|
|
19257
19264
|
function nonEmpty(value) {
|
|
19258
19265
|
return value === undefined || value === "" ? undefined : value;
|
|
19259
19266
|
}
|
|
@@ -19412,6 +19419,7 @@ var processGlobal = globalThis;
|
|
|
19412
19419
|
var commandOutcomes = processGlobal[COMMAND_OUTCOMES] ?? new Map;
|
|
19413
19420
|
processGlobal[COMMAND_OUTCOMES] = commandOutcomes;
|
|
19414
19421
|
function runCommand(command, env, cwd) {
|
|
19422
|
+
const timeoutMs = commandTimeoutMs(env);
|
|
19415
19423
|
const childEnv = {};
|
|
19416
19424
|
for (const [name, variable] of Object.entries(env)) {
|
|
19417
19425
|
if (variable !== undefined)
|
|
@@ -19430,7 +19438,7 @@ function runCommand(command, env, cwd) {
|
|
|
19430
19438
|
stdin: "ignore",
|
|
19431
19439
|
stdout: fd,
|
|
19432
19440
|
stderr: "ignore",
|
|
19433
|
-
timeout:
|
|
19441
|
+
timeout: timeoutMs,
|
|
19434
19442
|
detached: true
|
|
19435
19443
|
});
|
|
19436
19444
|
} catch (error) {
|
|
@@ -19443,7 +19451,7 @@ function runCommand(command, env, cwd) {
|
|
|
19443
19451
|
} catch {}
|
|
19444
19452
|
if (result.exitedDueToTimeout) {
|
|
19445
19453
|
return {
|
|
19446
|
-
failure: `did not finish within ${
|
|
19454
|
+
failure: `did not finish within ${timeoutMs / 1000} s, and does not run again in this process`,
|
|
19447
19455
|
timedOut: true
|
|
19448
19456
|
};
|
|
19449
19457
|
}
|
|
@@ -25452,27 +25460,327 @@ import { resolve as resolvePath2 } from "path";
|
|
|
25452
25460
|
// ../contracts/src/go-blank.ts
|
|
25453
25461
|
var goBlankPattern = /^[\t\n\v\f\r \u0085\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]*$/u;
|
|
25454
25462
|
|
|
25455
|
-
// ../envoy-client/src/
|
|
25456
|
-
function
|
|
25457
|
-
|
|
25458
|
-
|
|
25463
|
+
// ../envoy-client/src/typed-block-opener.ts
|
|
25464
|
+
function runOf(text, index, char) {
|
|
25465
|
+
let end = index;
|
|
25466
|
+
while (text.charAt(end) === char)
|
|
25467
|
+
end++;
|
|
25468
|
+
return end - index;
|
|
25469
|
+
}
|
|
25470
|
+
function directiveNameChar(char) {
|
|
25471
|
+
return /^[A-Za-z0-9_-]$/.test(char);
|
|
25472
|
+
}
|
|
25473
|
+
function typedFenceBrace(text, at) {
|
|
25474
|
+
const nameStart = at + runOf(text, at, ":");
|
|
25475
|
+
if (nameStart - at < 3)
|
|
25476
|
+
return -1;
|
|
25477
|
+
let brace = nameStart;
|
|
25478
|
+
while (directiveNameChar(text.charAt(brace)))
|
|
25479
|
+
brace++;
|
|
25480
|
+
return brace > nameStart && text.charAt(brace) === "{" ? brace : -1;
|
|
25481
|
+
}
|
|
25482
|
+
function typedOpenerAt(text, at) {
|
|
25483
|
+
const brace = typedFenceBrace(text, at);
|
|
25484
|
+
if (brace === -1 || !text.endsWith("}"))
|
|
25485
|
+
return;
|
|
25486
|
+
const attributes = directiveAttributes(text.slice(brace + 1, -1));
|
|
25487
|
+
if (attributes === undefined)
|
|
25488
|
+
return;
|
|
25489
|
+
const colons = runOf(text, at, ":");
|
|
25490
|
+
return { ...attributes, name: text.slice(at + colons, brace), colons };
|
|
25491
|
+
}
|
|
25492
|
+
function refusedDirective(text) {
|
|
25493
|
+
if (text.startsWith(":::"))
|
|
25494
|
+
return text !== ":::" && typedOpenerAt(text, 0) === undefined;
|
|
25495
|
+
if (text.startsWith("::"))
|
|
25496
|
+
return text.length > 2 && directiveNameChar(text.charAt(2));
|
|
25497
|
+
return text.length > 1 && directiveNameChar(text.charAt(1)) && text.includes("{");
|
|
25498
|
+
}
|
|
25499
|
+
function attributeSpace(char) {
|
|
25500
|
+
return char === " " || char >= "\t" && char <= "\r";
|
|
25501
|
+
}
|
|
25502
|
+
var BLOCK_ID_ATTRIBUTE = "blockId";
|
|
25503
|
+
function directiveAttributes(value) {
|
|
25504
|
+
const attributes = [];
|
|
25505
|
+
const gaps = [];
|
|
25506
|
+
const named = new Set;
|
|
25507
|
+
let hasId = false;
|
|
25508
|
+
let id;
|
|
25509
|
+
let offset = 0;
|
|
25510
|
+
for (;; ) {
|
|
25511
|
+
const gap = offset;
|
|
25512
|
+
while (offset < value.length && attributeSpace(value.charAt(offset)))
|
|
25513
|
+
offset++;
|
|
25514
|
+
gaps.push(value.slice(gap, offset));
|
|
25515
|
+
if (offset === value.length)
|
|
25516
|
+
break;
|
|
25517
|
+
const start = offset;
|
|
25518
|
+
const sigil = value.charAt(start);
|
|
25519
|
+
let name;
|
|
25520
|
+
if (sigil === "#") {
|
|
25521
|
+
offset = start + 1;
|
|
25522
|
+
while (offset < value.length && !TYPED_BLOCK_ID_SHORTCUT_EXCLUDED.includes(value.charAt(offset)))
|
|
25523
|
+
offset++;
|
|
25524
|
+
if (offset === start + 1 || hasId)
|
|
25525
|
+
return;
|
|
25526
|
+
hasId = true;
|
|
25527
|
+
id = value.slice(start + 1, offset);
|
|
25528
|
+
name = "id";
|
|
25529
|
+
} else if (sigil === ".") {
|
|
25530
|
+
offset = start + 1;
|
|
25531
|
+
while (offset < value.length && directiveNameChar(value.charAt(offset)))
|
|
25532
|
+
offset++;
|
|
25533
|
+
if (offset === start + 1)
|
|
25534
|
+
return;
|
|
25535
|
+
named.add("class");
|
|
25536
|
+
name = "class";
|
|
25537
|
+
} else {
|
|
25538
|
+
while (offset < value.length && directiveNameChar(value.charAt(offset)))
|
|
25539
|
+
offset++;
|
|
25540
|
+
name = value.slice(start, offset);
|
|
25541
|
+
if (name === "")
|
|
25542
|
+
return;
|
|
25543
|
+
if (name === "id") {
|
|
25544
|
+
const close = value.startsWith('="', offset) ? value.indexOf('"', offset + 2) : -1;
|
|
25545
|
+
if (close === -1 || close === offset + 2 || hasId)
|
|
25546
|
+
return;
|
|
25547
|
+
hasId = true;
|
|
25548
|
+
id = decodedId(value.slice(offset + 2, close));
|
|
25549
|
+
offset = close + 1;
|
|
25550
|
+
} else {
|
|
25551
|
+
if (name === BLOCK_ID_ATTRIBUTE || named.has(name))
|
|
25552
|
+
return;
|
|
25553
|
+
named.add(name);
|
|
25554
|
+
if (offset < value.length && value.charAt(offset) !== " ") {
|
|
25555
|
+
const close = value.startsWith('="', offset) ? value.indexOf('"', offset + 2) : -1;
|
|
25556
|
+
if (close === -1)
|
|
25557
|
+
return;
|
|
25558
|
+
offset = close + 1;
|
|
25559
|
+
}
|
|
25560
|
+
}
|
|
25561
|
+
}
|
|
25562
|
+
if (offset < value.length && !attributeSpace(value.charAt(offset)))
|
|
25563
|
+
return;
|
|
25564
|
+
attributes.push({ name, text: value.slice(start, offset) });
|
|
25565
|
+
}
|
|
25566
|
+
return { attributes, gaps, id };
|
|
25567
|
+
}
|
|
25568
|
+
var NAMED_REFERENCES = new Map([
|
|
25569
|
+
["amp;", "&"],
|
|
25570
|
+
["amp", "&"],
|
|
25571
|
+
["lt;", "<"],
|
|
25572
|
+
["lt", "<"],
|
|
25573
|
+
["gt;", ">"],
|
|
25574
|
+
["gt", ">"],
|
|
25575
|
+
["quot;", '"'],
|
|
25576
|
+
["quot", '"'],
|
|
25577
|
+
["apos;", "'"],
|
|
25578
|
+
["nbsp;", "\xA0"],
|
|
25579
|
+
["nbsp", "\xA0"]
|
|
25580
|
+
]);
|
|
25581
|
+
var WINDOWS_1252 = "\u20AC\x81\u201A\u0192\u201E\u2026\u2020\u2021\u02C6\u2030\u0160\u2039\u0152\x8D\u017D\x8F" + "\x90\u2018\u2019\u201C\u201D\u2022\u2013\u2014\u02DC\u2122\u0161\u203A\u0153\x9D\u017E\u0178";
|
|
25582
|
+
function decodedId(raw) {
|
|
25583
|
+
let id = "";
|
|
25584
|
+
let at = 0;
|
|
25585
|
+
for (let amp = raw.indexOf("&");amp !== -1; amp = raw.indexOf("&", at)) {
|
|
25586
|
+
id += raw.slice(at, amp);
|
|
25587
|
+
const s = raw.slice(amp);
|
|
25588
|
+
let i = 1;
|
|
25589
|
+
if (s.charAt(1) === "#") {
|
|
25590
|
+
const hex = s.charAt(2) === "x" || s.charAt(2) === "X";
|
|
25591
|
+
let x = 0;
|
|
25592
|
+
i = hex ? 3 : 2;
|
|
25593
|
+
while (s.length > 3 && i < s.length) {
|
|
25594
|
+
const c = s.charAt(i++);
|
|
25595
|
+
const digit = (hex ? /^[0-9A-Fa-f]$/ : /^[0-9]$/).test(c) ? Number.parseInt(c, 16) : -1;
|
|
25596
|
+
if (digit >= 0) {
|
|
25597
|
+
x = Math.imul(x, hex ? 16 : 10) + digit | 0;
|
|
25598
|
+
continue;
|
|
25599
|
+
}
|
|
25600
|
+
if (c !== ";")
|
|
25601
|
+
i--;
|
|
25602
|
+
break;
|
|
25603
|
+
}
|
|
25604
|
+
if (s.length <= 3 || i <= 3) {
|
|
25605
|
+
id += "&";
|
|
25606
|
+
i = 1;
|
|
25607
|
+
} else if (x >= 128 && x <= 159) {
|
|
25608
|
+
id += WINDOWS_1252.charAt(x - 128);
|
|
25609
|
+
} else if (x <= 0 || x >= 55296 && x <= 57343 || x > 1114111) {
|
|
25610
|
+
id += "\uFFFD";
|
|
25611
|
+
} else {
|
|
25612
|
+
id += String.fromCodePoint(x);
|
|
25613
|
+
}
|
|
25614
|
+
} else {
|
|
25615
|
+
while (i < s.length && /^[A-Za-z0-9]$/.test(s.charAt(i)))
|
|
25616
|
+
i++;
|
|
25617
|
+
if (s.charAt(i) === ";")
|
|
25618
|
+
i++;
|
|
25619
|
+
const name = s.slice(1, i);
|
|
25620
|
+
const decoded = NAMED_REFERENCES.get(name);
|
|
25621
|
+
if (decoded !== undefined)
|
|
25622
|
+
id += decoded;
|
|
25623
|
+
else if (name.replace(/;$/, "").length > 1 && /^[A-Za-z]/.test(name))
|
|
25624
|
+
return;
|
|
25625
|
+
else
|
|
25626
|
+
id += s.slice(0, i);
|
|
25627
|
+
}
|
|
25628
|
+
at = amp + i;
|
|
25629
|
+
}
|
|
25630
|
+
return id + raw.slice(at);
|
|
25459
25631
|
}
|
|
25460
|
-
|
|
25461
|
-
|
|
25462
|
-
|
|
25463
|
-
|
|
25464
|
-
|
|
25632
|
+
|
|
25633
|
+
// ../envoy-client/src/dispatch-commands/stored-spelling.ts
|
|
25634
|
+
function headedOpener(line) {
|
|
25635
|
+
for (let at = 0;at < line.length; at += Math.max(1, runOf(line, at, ":"))) {
|
|
25636
|
+
const brace = typedFenceBrace(line, at);
|
|
25637
|
+
if (brace === -1)
|
|
25638
|
+
continue;
|
|
25639
|
+
const opener = typedOpenerAt(line, at);
|
|
25640
|
+
return opener === undefined ? undefined : { ...opener, head: line.slice(0, brace + 1) };
|
|
25465
25641
|
}
|
|
25466
|
-
|
|
25467
|
-
|
|
25468
|
-
|
|
25642
|
+
return;
|
|
25643
|
+
}
|
|
25644
|
+
function attributesByName(attributes) {
|
|
25645
|
+
const byName = new Map(attributes.map((attribute) => [attribute.name, attribute.text]));
|
|
25646
|
+
return byName.size === attributes.length ? byName : undefined;
|
|
25647
|
+
}
|
|
25648
|
+
function inStoredOrder(given, stored) {
|
|
25649
|
+
const written = attributesByName(given.attributes);
|
|
25650
|
+
if (written === undefined || attributesByName(stored.attributes) === undefined || stored.attributes.length !== given.attributes.length) {
|
|
25651
|
+
return;
|
|
25652
|
+
}
|
|
25653
|
+
let line = `${given.head}${given.gaps[0] ?? ""}`;
|
|
25654
|
+
for (const [index, attribute] of stored.attributes.entries()) {
|
|
25655
|
+
const text = written.get(attribute.name);
|
|
25656
|
+
if (text === undefined)
|
|
25657
|
+
return;
|
|
25658
|
+
line += `${text}${given.gaps[index + 1] ?? ""}`;
|
|
25659
|
+
}
|
|
25660
|
+
return `${line}}`;
|
|
25661
|
+
}
|
|
25662
|
+
function onlyReordered(given, held, stored) {
|
|
25663
|
+
return given !== undefined && stored !== undefined && inStoredOrder(given, stored) === held;
|
|
25664
|
+
}
|
|
25665
|
+
function reorderedBlockLine(line, opener) {
|
|
25666
|
+
const named = `${opener.name} block${opener.id === undefined ? "" : ` ${opener.id}`}`;
|
|
25667
|
+
const order = opener.attributes.map((attribute) => attribute.name).join(", ");
|
|
25668
|
+
return `This upload put the attributes of ${named} (line ${line}) in the order Dispatch stores: ${order}.`;
|
|
25669
|
+
}
|
|
25670
|
+
function attributesReordered(given, stored) {
|
|
25671
|
+
const written = withoutFinalLineFeed(given).split(`
|
|
25672
|
+
`);
|
|
25673
|
+
const holds = withoutFinalLineFeed(stored).split(`
|
|
25674
|
+
`);
|
|
25675
|
+
if (written.length !== holds.length)
|
|
25676
|
+
return;
|
|
25677
|
+
const lines = [];
|
|
25678
|
+
for (const [index, held] of holds.entries()) {
|
|
25679
|
+
const wrote = written[index];
|
|
25680
|
+
if (wrote === held)
|
|
25681
|
+
continue;
|
|
25682
|
+
const opener = headedOpener(held);
|
|
25683
|
+
if (wrote === undefined || opener === undefined || !onlyReordered(headedOpener(wrote), held, opener)) {
|
|
25684
|
+
return;
|
|
25685
|
+
}
|
|
25686
|
+
lines.push(reorderedBlockLine(index + 1, opener));
|
|
25687
|
+
}
|
|
25688
|
+
return lines;
|
|
25469
25689
|
}
|
|
25470
25690
|
var STORED_LINE_QUOTED = 200;
|
|
25471
|
-
function quotedStoredLine(
|
|
25472
|
-
|
|
25473
|
-
|
|
25474
|
-
const
|
|
25475
|
-
|
|
25691
|
+
function quotedStoredLine(characters, from) {
|
|
25692
|
+
const shown = characters.slice(from, from + STORED_LINE_QUOTED);
|
|
25693
|
+
const quote = JSON.stringify(shown.join(""));
|
|
25694
|
+
const cutAfter = from + shown.length < characters.length;
|
|
25695
|
+
if (from === 0 && !cutAfter)
|
|
25696
|
+
return quote;
|
|
25697
|
+
const cut = from === 0 ? "after" : cutAfter ? "before and after" : "before the last";
|
|
25698
|
+
return `${quote} (cut ${cut} ${shown.length} of its ${characters.length} characters)`;
|
|
25699
|
+
}
|
|
25700
|
+
function lineWords(line, opener) {
|
|
25701
|
+
const plain = (text) => (text.match(/\s+|\S+/g) ?? []).map((word) => ({ text: word, attribute: false }));
|
|
25702
|
+
if (opener === undefined)
|
|
25703
|
+
return plain(line);
|
|
25704
|
+
const words = plain(opener.head);
|
|
25705
|
+
for (const [index, gap] of opener.gaps.entries()) {
|
|
25706
|
+
if (gap !== "")
|
|
25707
|
+
words.push({ text: gap, attribute: false });
|
|
25708
|
+
const attribute = opener.attributes[index];
|
|
25709
|
+
if (attribute !== undefined)
|
|
25710
|
+
words.push({ text: attribute.text, attribute: true });
|
|
25711
|
+
}
|
|
25712
|
+
words.push({ text: "}", attribute: false });
|
|
25713
|
+
return words;
|
|
25714
|
+
}
|
|
25715
|
+
function wordText(word, from = 0, characters = Array.from(word.text)) {
|
|
25716
|
+
if (word.attribute)
|
|
25717
|
+
return `the attribute ${word.text}`;
|
|
25718
|
+
return `${/\S/.test(word.text) ? "the word" : "the space"} ${quotedStoredLine(characters, from)}`;
|
|
25719
|
+
}
|
|
25720
|
+
function wordDifference(written, holds) {
|
|
25721
|
+
let before = 0;
|
|
25722
|
+
for (let index = 0;index < Math.max(written.length, holds.length); index += 1) {
|
|
25723
|
+
const given = written[index];
|
|
25724
|
+
const stored = holds[index];
|
|
25725
|
+
if (given !== undefined && stored !== undefined && given.text === stored.text) {
|
|
25726
|
+
before += Array.from(given.text).length;
|
|
25727
|
+
continue;
|
|
25728
|
+
}
|
|
25729
|
+
if (given === undefined && stored !== undefined) {
|
|
25730
|
+
return `ends after character ${before}, where Dispatch goes on with ${wordText(stored)}`;
|
|
25731
|
+
}
|
|
25732
|
+
if (given !== undefined && stored === undefined) {
|
|
25733
|
+
return `reads ${wordText(given)} at character ${before + 1}, where Dispatch ends the line`;
|
|
25734
|
+
}
|
|
25735
|
+
if (given !== undefined && stored !== undefined) {
|
|
25736
|
+
const wrote = Array.from(given.text);
|
|
25737
|
+
const held = Array.from(stored.text);
|
|
25738
|
+
let from = 0;
|
|
25739
|
+
if (!given.attribute && !stored.attribute && Math.max(wrote.length, held.length) > STORED_LINE_QUOTED) {
|
|
25740
|
+
while (from < wrote.length && wrote[from] === held[from])
|
|
25741
|
+
from += 1;
|
|
25742
|
+
const tail = Math.max(0, from - STORED_LINE_QUOTED);
|
|
25743
|
+
if (from === wrote.length) {
|
|
25744
|
+
return `ends ${wordText(given, tail, wrote)} after character ${before + from}, where Dispatch goes on with ${wordText(stored, from, held)}`;
|
|
25745
|
+
}
|
|
25746
|
+
if (from === held.length) {
|
|
25747
|
+
return `reads ${wordText(given, from, wrote)} at character ${before + from + 1}, where Dispatch ends ${wordText(stored, tail, held)}`;
|
|
25748
|
+
}
|
|
25749
|
+
}
|
|
25750
|
+
return `reads ${wordText(given, from, wrote)} at character ${before + from + 1}, where Dispatch stores ${wordText(stored, from, held)}`;
|
|
25751
|
+
}
|
|
25752
|
+
}
|
|
25753
|
+
return "differs from the line Dispatch stores";
|
|
25754
|
+
}
|
|
25755
|
+
function attributeDifference(line, given, stored) {
|
|
25756
|
+
const written = attributesByName(given.attributes);
|
|
25757
|
+
if (written === undefined)
|
|
25758
|
+
return;
|
|
25759
|
+
for (const attribute of stored.attributes) {
|
|
25760
|
+
const text = written.get(attribute.name);
|
|
25761
|
+
if (text === undefined)
|
|
25762
|
+
return `Dispatch adds the attribute ${attribute.text} to line ${line}`;
|
|
25763
|
+
if (text !== attribute.text) {
|
|
25764
|
+
return `line ${line} holds the attribute ${text}, which Dispatch stores as ${attribute.text}`;
|
|
25765
|
+
}
|
|
25766
|
+
}
|
|
25767
|
+
const held = new Set(stored.attributes.map((attribute) => attribute.name));
|
|
25768
|
+
const dropped = given.attributes.find((attribute) => !held.has(attribute.name));
|
|
25769
|
+
return dropped === undefined ? undefined : `line ${line} holds the attribute ${dropped.text}, which Dispatch drops`;
|
|
25770
|
+
}
|
|
25771
|
+
function lineDifference(line, wrote, given, held, stored) {
|
|
25772
|
+
if (given === undefined || stored === undefined) {
|
|
25773
|
+
return `line ${line} ${wordDifference(lineWords(wrote, undefined), lineWords(held, undefined))}`;
|
|
25774
|
+
}
|
|
25775
|
+
const attribute = attributeDifference(line, given, stored);
|
|
25776
|
+
if (attribute !== undefined)
|
|
25777
|
+
return attribute;
|
|
25778
|
+
const reordered = inStoredOrder(given, stored);
|
|
25779
|
+
if (reordered !== undefined && reordered !== wrote) {
|
|
25780
|
+
const words = lineWords(reordered, headedOpener(reordered));
|
|
25781
|
+
return `line ${line}, with its attributes put in the order Dispatch stores, ${wordDifference(words, lineWords(held, stored))}`;
|
|
25782
|
+
}
|
|
25783
|
+
return `line ${line} ${wordDifference(lineWords(wrote, given), lineWords(held, stored))}`;
|
|
25476
25784
|
}
|
|
25477
25785
|
function firstStoredDifference(given, stored) {
|
|
25478
25786
|
const written = given.split(`
|
|
@@ -25485,13 +25793,17 @@ function firstStoredDifference(given, stored) {
|
|
|
25485
25793
|
if (wrote === held)
|
|
25486
25794
|
continue;
|
|
25487
25795
|
if (wrote === undefined && held !== undefined) {
|
|
25488
|
-
return `the store writes a line ${line + 1}, ${quotedStoredLine(held)}`;
|
|
25796
|
+
return `the store writes a line ${line + 1}, ${quotedStoredLine(Array.from(held), 0)}`;
|
|
25489
25797
|
}
|
|
25490
25798
|
if (held === undefined && wrote !== undefined) {
|
|
25491
|
-
return `the store drops line ${line + 1}, ${quotedStoredLine(wrote)}`;
|
|
25799
|
+
return `the store drops line ${line + 1}, ${quotedStoredLine(Array.from(wrote), 0)}`;
|
|
25492
25800
|
}
|
|
25493
25801
|
if (wrote !== undefined && held !== undefined) {
|
|
25494
|
-
|
|
25802
|
+
const givenOpener = headedOpener(wrote);
|
|
25803
|
+
const storedOpener = headedOpener(held);
|
|
25804
|
+
if (onlyReordered(givenOpener, held, storedOpener))
|
|
25805
|
+
continue;
|
|
25806
|
+
return lineDifference(line + 1, wrote, givenOpener, held, storedOpener);
|
|
25495
25807
|
}
|
|
25496
25808
|
}
|
|
25497
25809
|
return "the stored text differs from the text given";
|
|
@@ -25521,6 +25833,33 @@ ${printed.text}`,
|
|
|
25521
25833
|
"--- end of the text Dispatch stores ---"
|
|
25522
25834
|
].join("");
|
|
25523
25835
|
}
|
|
25836
|
+
function withoutFinalLineFeed(text) {
|
|
25837
|
+
return text.endsWith(`
|
|
25838
|
+
`) ? text.slice(0, -1) : text;
|
|
25839
|
+
}
|
|
25840
|
+
function storedUpload(given, stored, name) {
|
|
25841
|
+
if (withoutFinalLineFeed(stored) === withoutFinalLineFeed(given)) {
|
|
25842
|
+
return { sent: given, reordered: [] };
|
|
25843
|
+
}
|
|
25844
|
+
const reordered = attributesReordered(given, stored);
|
|
25845
|
+
return reordered === undefined ? { refusal: storedTextProblem(given, stored, name) } : { sent: stored, reordered };
|
|
25846
|
+
}
|
|
25847
|
+
|
|
25848
|
+
// ../envoy-client/src/dispatch-commands/upload-checks.ts
|
|
25849
|
+
function artifactUnderName(artifacts, name) {
|
|
25850
|
+
const written = name.trim();
|
|
25851
|
+
return artifacts.find((held) => held.slug === written) ?? artifacts.find((held) => held.name === written);
|
|
25852
|
+
}
|
|
25853
|
+
function artifactNameExistsProblem(held, owner) {
|
|
25854
|
+
const versions = held.versions ?? [];
|
|
25855
|
+
const newest = versions[versions.length - 1];
|
|
25856
|
+
if (newest === undefined) {
|
|
25857
|
+
return `${owner} already holds ${held.name} (slug ${held.slug}, kind ${held.kind}), which has no version yet and which ${actorText(held.created_by)} created. ${ARTIFACT_NAME_EXISTS_RULE}`;
|
|
25858
|
+
}
|
|
25859
|
+
const authors = (newest.authors ?? []).map(actorText);
|
|
25860
|
+
const wroteIt = authors.length === 0 ? actorText(held.created_by) : authors.join(" and ");
|
|
25861
|
+
return `${owner} already holds ${held.name} (slug ${held.slug}, kind ${held.kind}) at version ${newest.number}, which ${wroteIt} wrote. ${ARTIFACT_NAME_EXISTS_RULE}`;
|
|
25862
|
+
}
|
|
25524
25863
|
function storedTextCheckFailed(error, name) {
|
|
25525
25864
|
const failure = `Dispatch could not say how it would store this text, so nothing was uploaded: POST /api/v1/markdown/round-trip failed with ${messageFor(error)}.`;
|
|
25526
25865
|
if (!dispatchAnswered(error, 413)) {
|
|
@@ -25532,10 +25871,6 @@ function storedTextCheckFailed(error, name) {
|
|
|
25532
25871
|
function roundTripRouteAbsent(error) {
|
|
25533
25872
|
return dispatchAnswered(error, 404) || dispatchAnswered(error, 405);
|
|
25534
25873
|
}
|
|
25535
|
-
function withoutFinalLineFeed(text) {
|
|
25536
|
-
return text.endsWith(`
|
|
25537
|
-
`) ? text.slice(0, -1) : text;
|
|
25538
|
-
}
|
|
25539
25874
|
|
|
25540
25875
|
// ../envoy-client/src/dispatch-commands/documents.ts
|
|
25541
25876
|
async function blockAsks(client, resolved, state) {
|
|
@@ -25919,6 +26254,8 @@ async function artifactCommand(context) {
|
|
|
25919
26254
|
]);
|
|
25920
26255
|
}
|
|
25921
26256
|
}
|
|
26257
|
+
let sent = content;
|
|
26258
|
+
let reordered = [];
|
|
25922
26259
|
if (!blank) {
|
|
25923
26260
|
let stored;
|
|
25924
26261
|
try {
|
|
@@ -25928,8 +26265,12 @@ async function artifactCommand(context) {
|
|
|
25928
26265
|
throw new Error(storedTextCheckFailed(error, name), { cause: error });
|
|
25929
26266
|
}
|
|
25930
26267
|
}
|
|
25931
|
-
if (stored !== undefined
|
|
25932
|
-
|
|
26268
|
+
if (stored !== undefined) {
|
|
26269
|
+
const upload = storedUpload(content, stored, name);
|
|
26270
|
+
if ("refusal" in upload)
|
|
26271
|
+
throw new ToolInputError(input.tool, [upload.refusal]);
|
|
26272
|
+
sent = upload.sent;
|
|
26273
|
+
reordered = upload.reordered;
|
|
25933
26274
|
}
|
|
25934
26275
|
}
|
|
25935
26276
|
const artifactInput = path !== undefined ? {
|
|
@@ -25937,7 +26278,7 @@ async function artifactCommand(context) {
|
|
|
25937
26278
|
file: input.uploadBytes === undefined ? Bun.file(resolvePath2(input.cwd, path)) : new Blob([new Uint8Array(input.uploadBytes)], {
|
|
25938
26279
|
type: Bun.file(resolvePath2(input.cwd, path)).type
|
|
25939
26280
|
})
|
|
25940
|
-
} :
|
|
26281
|
+
} : sent !== undefined ? { ...options, content: sent } : options;
|
|
25941
26282
|
const result = artifactOwner.kind === "project" ? await client.projectArtifact(artifactOwner.project, artifactInput) : await client.artifact(issue(), artifactInput);
|
|
25942
26283
|
const artifactRef = dispatchDocumentRef(artifactOwner.kind === "project" ? artifactOwner.project : issue(), result.artifact.slug);
|
|
25943
26284
|
const uploadOwner = artifactOwner.kind === "project" ? documentTopic(result.artifact) : issueTopic(issue());
|
|
@@ -25948,6 +26289,7 @@ async function artifactCommand(context) {
|
|
|
25948
26289
|
return {
|
|
25949
26290
|
text: [
|
|
25950
26291
|
path === undefined && content === undefined ? `Made ${result.artifact.name} as an empty document (artifact slug ${result.artifact.slug}; ${artifactRef}). Write in it with dispatch doc-edit.${await notSubscribed(uploadOwner, input.subscriptions)}` : `Uploaded ${result.artifact.name} as version ${result.version.number} (artifact slug ${result.artifact.slug}; ${artifactRef})${await notSubscribed(uploadOwner, input.subscriptions)}`,
|
|
26292
|
+
...reordered,
|
|
25951
26293
|
...adviceLines
|
|
25952
26294
|
].join(`
|
|
25953
26295
|
`),
|
|
@@ -32138,6 +32480,13 @@ function claudeProjectDirectory(environment, fallback) {
|
|
|
32138
32480
|
return configuredValue(environment.CLAUDE_PROJECT_DIR) ?? fallback;
|
|
32139
32481
|
}
|
|
32140
32482
|
|
|
32483
|
+
// src/codex/codex-session-record.ts
|
|
32484
|
+
import { spawnSync } from "child_process";
|
|
32485
|
+
import { readFileSync as readFileSync6, readlinkSync as readlinkSync2, unwatchFile, watchFile } from "fs";
|
|
32486
|
+
import { mkdir as mkdir4, readFile as readFile5, rename as rename5, utimes as utimes3, writeFile as writeFile3 } from "fs/promises";
|
|
32487
|
+
import { homedir as homedir3 } from "os";
|
|
32488
|
+
import { basename as basename4, dirname as dirname5, join as join10 } from "path";
|
|
32489
|
+
|
|
32141
32490
|
// src/session-identity.ts
|
|
32142
32491
|
import { readFile as readFile4 } from "fs/promises";
|
|
32143
32492
|
import { basename as basename3, join as join9 } from "path";
|
|
@@ -32299,6 +32648,194 @@ async function pruneStaleSessionRoles(stateDirectory, currentSessionId) {
|
|
|
32299
32648
|
await pruneStateDirectory(join9(stateDirectory, ROLES_DIRECTORY), async (file) => file !== own && await writtenLongAgo(file));
|
|
32300
32649
|
}
|
|
32301
32650
|
|
|
32651
|
+
// src/codex/codex-session-record.ts
|
|
32652
|
+
var CodexSessionRecord = object({
|
|
32653
|
+
session_id: string2().min(1),
|
|
32654
|
+
transcript_path: string2().nullable(),
|
|
32655
|
+
codex_home: string2().min(1),
|
|
32656
|
+
cwd: string2().min(1),
|
|
32657
|
+
source: string2().nullable(),
|
|
32658
|
+
codex_executable: string2().nullable(),
|
|
32659
|
+
codex_started: string2().nullable().default(null)
|
|
32660
|
+
});
|
|
32661
|
+
function codexSessionRecordFile(stateDirectory, pid) {
|
|
32662
|
+
return join10(stateDirectory, "codex-sessions", `${pid}.json`);
|
|
32663
|
+
}
|
|
32664
|
+
function codexEndedFile(stateDirectory, pid, sessionId) {
|
|
32665
|
+
return join10(stateDirectory, "codex-sessions", `${pid}.${encodeURIComponent(sessionId)}.ended`);
|
|
32666
|
+
}
|
|
32667
|
+
async function markSessionEnded(stateDirectory, pid, sessionId) {
|
|
32668
|
+
const marker = codexEndedFile(stateDirectory, pid, sessionId);
|
|
32669
|
+
await mkdir4(dirname5(marker), { mode: 448, recursive: true });
|
|
32670
|
+
await writeFile3(marker, "", { mode: 384 });
|
|
32671
|
+
const now = new Date;
|
|
32672
|
+
await utimes3(codexSessionRecordFile(stateDirectory, pid), now, now);
|
|
32673
|
+
}
|
|
32674
|
+
function watchRecord(file, pid, ended, changed, intervalMs) {
|
|
32675
|
+
const look = () => {
|
|
32676
|
+
readCodexSessionRecord(file).then(async (record) => {
|
|
32677
|
+
if (record === undefined || !recordOfProcess(record, pid))
|
|
32678
|
+
return;
|
|
32679
|
+
changed(record, await ended(record.session_id));
|
|
32680
|
+
});
|
|
32681
|
+
};
|
|
32682
|
+
watchFile(file, { interval: intervalMs }, look);
|
|
32683
|
+
look();
|
|
32684
|
+
return () => unwatchFile(file, look);
|
|
32685
|
+
}
|
|
32686
|
+
async function sessionEnded(file) {
|
|
32687
|
+
try {
|
|
32688
|
+
await readFile5(file);
|
|
32689
|
+
return true;
|
|
32690
|
+
} catch {
|
|
32691
|
+
return false;
|
|
32692
|
+
}
|
|
32693
|
+
}
|
|
32694
|
+
async function writeCodexSessionRecord(file, value) {
|
|
32695
|
+
await mkdir4(dirname5(file), { recursive: true, mode: 448 });
|
|
32696
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
32697
|
+
await writeFile3(temporary, JSON.stringify(value), { mode: 384 });
|
|
32698
|
+
await rename5(temporary, file);
|
|
32699
|
+
}
|
|
32700
|
+
async function readCodexSessionRecord(file) {
|
|
32701
|
+
try {
|
|
32702
|
+
const parsed = CodexSessionRecord.safeParse(JSON.parse(await readFile5(file, "utf8")));
|
|
32703
|
+
return parsed.success ? parsed.data : undefined;
|
|
32704
|
+
} catch {
|
|
32705
|
+
return;
|
|
32706
|
+
}
|
|
32707
|
+
}
|
|
32708
|
+
function codexHome(env) {
|
|
32709
|
+
const set = env["CODEX_HOME"];
|
|
32710
|
+
return set !== undefined && set !== "" ? set : join10(env["HOME"] || homedir3(), ".codex");
|
|
32711
|
+
}
|
|
32712
|
+
function codexPluginDataDirectory(pluginRoot) {
|
|
32713
|
+
const version = pluginRoot;
|
|
32714
|
+
const plugin = dirname5(version);
|
|
32715
|
+
const marketplace = dirname5(plugin);
|
|
32716
|
+
const cache = dirname5(marketplace);
|
|
32717
|
+
const plugins = dirname5(cache);
|
|
32718
|
+
if (basename4(cache) !== "cache" || basename4(plugins) !== "plugins")
|
|
32719
|
+
return;
|
|
32720
|
+
return join10(plugins, "data", `${basename4(plugin)}-${basename4(marketplace)}`);
|
|
32721
|
+
}
|
|
32722
|
+
var MAX_ANCESTORS = 16;
|
|
32723
|
+
function codexProcessId(readComm = procComm, readParent = procParent) {
|
|
32724
|
+
let pid = process.ppid;
|
|
32725
|
+
for (let step = 0;pid !== undefined && pid > 1 && step < MAX_ANCESTORS; step++) {
|
|
32726
|
+
if (readComm(pid) === "codex")
|
|
32727
|
+
return pid;
|
|
32728
|
+
pid = readParent(pid);
|
|
32729
|
+
}
|
|
32730
|
+
return process.ppid;
|
|
32731
|
+
}
|
|
32732
|
+
function procComm(pid) {
|
|
32733
|
+
try {
|
|
32734
|
+
return readFileSync6(`/proc/${pid}/comm`, "utf8").trim();
|
|
32735
|
+
} catch {
|
|
32736
|
+
return;
|
|
32737
|
+
}
|
|
32738
|
+
}
|
|
32739
|
+
function procParent(pid) {
|
|
32740
|
+
try {
|
|
32741
|
+
const stat = readFileSync6(`/proc/${pid}/stat`, "utf8");
|
|
32742
|
+
const parent = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]);
|
|
32743
|
+
return Number.isInteger(parent) ? parent : undefined;
|
|
32744
|
+
} catch {
|
|
32745
|
+
return;
|
|
32746
|
+
}
|
|
32747
|
+
}
|
|
32748
|
+
var PS_OPTIONS = {
|
|
32749
|
+
encoding: "utf8",
|
|
32750
|
+
env: { LC_ALL: "C", PATH: process.env["PATH"] ?? "/usr/bin:/bin", TZ: "UTC" },
|
|
32751
|
+
timeout: 1000
|
|
32752
|
+
};
|
|
32753
|
+
function procStarted(pid) {
|
|
32754
|
+
try {
|
|
32755
|
+
const stat = readFileSync6(`/proc/${pid}/stat`, "utf8");
|
|
32756
|
+
return stat.slice(stat.lastIndexOf(")") + 2).split(" ")[19];
|
|
32757
|
+
} catch {
|
|
32758
|
+
return;
|
|
32759
|
+
}
|
|
32760
|
+
}
|
|
32761
|
+
function processStarted(pid) {
|
|
32762
|
+
const started = procStarted(pid);
|
|
32763
|
+
if (started !== undefined)
|
|
32764
|
+
return started;
|
|
32765
|
+
const ps = spawnSync("ps", ["-o", "lstart=", "-p", String(pid)], PS_OPTIONS);
|
|
32766
|
+
const fromPs = ps.status === 0 ? ps.stdout.trim() : "";
|
|
32767
|
+
return fromPs === "" ? undefined : `ps:${fromPs}`;
|
|
32768
|
+
}
|
|
32769
|
+
function recordOfProcess(record, pid) {
|
|
32770
|
+
const started = processStarted(pid);
|
|
32771
|
+
return record.codex_started !== null && started !== undefined && record.codex_started === started;
|
|
32772
|
+
}
|
|
32773
|
+
function sharedAppServer(pid) {
|
|
32774
|
+
let argv;
|
|
32775
|
+
try {
|
|
32776
|
+
argv = readFileSync6(`/proc/${pid}/cmdline`, "utf8").split("\x00");
|
|
32777
|
+
} catch {
|
|
32778
|
+
const ps = spawnSync("ps", ["-o", "args=", "-p", String(pid)], PS_OPTIONS);
|
|
32779
|
+
if (ps.status !== 0)
|
|
32780
|
+
return false;
|
|
32781
|
+
argv = ps.stdout.trim().split(/\s+/);
|
|
32782
|
+
}
|
|
32783
|
+
return SHARED_SUBCOMMANDS.has(codexSubcommand(argv.slice(1)) ?? "");
|
|
32784
|
+
}
|
|
32785
|
+
var SHARED_SUBCOMMANDS = new Set(["agents", "app-server"]);
|
|
32786
|
+
var VALUE_OPTIONS = new Set([
|
|
32787
|
+
"-c",
|
|
32788
|
+
"--config",
|
|
32789
|
+
"--enable",
|
|
32790
|
+
"--disable",
|
|
32791
|
+
"--remote",
|
|
32792
|
+
"--remote-auth-token-env",
|
|
32793
|
+
"-i",
|
|
32794
|
+
"--image",
|
|
32795
|
+
"-m",
|
|
32796
|
+
"--model",
|
|
32797
|
+
"--local-provider",
|
|
32798
|
+
"-p",
|
|
32799
|
+
"--profile",
|
|
32800
|
+
"-s",
|
|
32801
|
+
"--sandbox",
|
|
32802
|
+
"-a",
|
|
32803
|
+
"--ask-for-approval",
|
|
32804
|
+
"-C",
|
|
32805
|
+
"--cd",
|
|
32806
|
+
"--add-dir"
|
|
32807
|
+
]);
|
|
32808
|
+
function codexSubcommand(args) {
|
|
32809
|
+
for (let index = 0;index < args.length; index++) {
|
|
32810
|
+
const arg = args[index] ?? "";
|
|
32811
|
+
if (arg === "--")
|
|
32812
|
+
return args[index + 1];
|
|
32813
|
+
if (!arg.startsWith("-"))
|
|
32814
|
+
return arg;
|
|
32815
|
+
if (VALUE_OPTIONS.has(arg))
|
|
32816
|
+
index++;
|
|
32817
|
+
}
|
|
32818
|
+
return;
|
|
32819
|
+
}
|
|
32820
|
+
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.";
|
|
32821
|
+
async function pruneCodexSessionRecords(stateDirectory) {
|
|
32822
|
+
await pruneStateDirectory(join10(stateDirectory, "codex-sessions"), (file) => {
|
|
32823
|
+
const match = /^(\d+)\.(?:json|.+\.ended)$/.exec(basename4(file));
|
|
32824
|
+
return match !== null && !processExists(Number(match[1]));
|
|
32825
|
+
});
|
|
32826
|
+
await pruneStateDirectory(join10(stateDirectory, "codex-servers"), (file) => {
|
|
32827
|
+
const name = basename4(file);
|
|
32828
|
+
return /^\d+$/.test(name) && !processExists(Number(name));
|
|
32829
|
+
});
|
|
32830
|
+
}
|
|
32831
|
+
function codexExecutable(pid) {
|
|
32832
|
+
try {
|
|
32833
|
+
return readlinkSync2(`/proc/${pid}/exe`);
|
|
32834
|
+
} catch {
|
|
32835
|
+
return;
|
|
32836
|
+
}
|
|
32837
|
+
}
|
|
32838
|
+
|
|
32302
32839
|
// hooks/hook-arguments.ts
|
|
32303
32840
|
function parseHookArguments(argv) {
|
|
32304
32841
|
const at = argv.indexOf("--host");
|
|
@@ -32345,6 +32882,28 @@ var RefreshOutcome = object({
|
|
|
32345
32882
|
notice: string2().nullable().optional()
|
|
32346
32883
|
});
|
|
32347
32884
|
var ANSWERS_TIMING_FILE = "ENVOY_ANSWERS_TIMING_FILE";
|
|
32885
|
+
var startedRefresher = null;
|
|
32886
|
+
var armedWaits = [];
|
|
32887
|
+
function armedWait(kind, limit, { capMs = Number.POSITIVE_INFINITY, leastMs = 0, at = performance.now() } = {}) {
|
|
32888
|
+
const ms = Math.max(leastMs, Math.min(capMs, Math.max(0, Math.round(limit - at))));
|
|
32889
|
+
if (process.env[ANSWERS_TIMING_FILE] !== undefined)
|
|
32890
|
+
armedWaits.push({ kind, at, ms, limit });
|
|
32891
|
+
return ms;
|
|
32892
|
+
}
|
|
32893
|
+
function writeAnswersTiming(timing) {
|
|
32894
|
+
const timingFile = process.env[ANSWERS_TIMING_FILE];
|
|
32895
|
+
if (timingFile === undefined)
|
|
32896
|
+
return;
|
|
32897
|
+
try {
|
|
32898
|
+
writeAtomically(timingFile, JSON.stringify({ ...timing, waits: armedWaits, refresher: startedRefresher }));
|
|
32899
|
+
} catch {}
|
|
32900
|
+
}
|
|
32901
|
+
function nameRefresher(pid) {
|
|
32902
|
+
if (process.env[ANSWERS_TIMING_FILE] === undefined)
|
|
32903
|
+
return;
|
|
32904
|
+
startedRefresher = { pid, started: procStarted(pid) ?? null };
|
|
32905
|
+
writeAnswersTiming({});
|
|
32906
|
+
}
|
|
32348
32907
|
var DISPATCH_HOST_LINE = "export DISPATCH_HOST=claude";
|
|
32349
32908
|
var JJ_CONFIG_EXPORT = "export JJ_CONFIG=";
|
|
32350
32909
|
var JJ_BASE_CHAIN_TIMEOUT_MS = 3000;
|
|
@@ -32353,7 +32912,7 @@ function singleQuoted(value) {
|
|
|
32353
32912
|
}
|
|
32354
32913
|
function envFileText(envFile) {
|
|
32355
32914
|
try {
|
|
32356
|
-
return
|
|
32915
|
+
return readFileSync7(envFile, "utf8");
|
|
32357
32916
|
} catch (error) {
|
|
32358
32917
|
if (!hasErrnoCode(error, "ENOENT"))
|
|
32359
32918
|
throw error;
|
|
@@ -32470,19 +33029,16 @@ function dispatchFirst(raw) {
|
|
|
32470
33029
|
process.stdout.write(JSON.stringify({
|
|
32471
33030
|
hookSpecificOutput: {
|
|
32472
33031
|
hookEventName: input.hook_event_name,
|
|
32473
|
-
additionalContext: readDispatchFirstContext(dispatchFirstSkillFile(
|
|
33032
|
+
additionalContext: readDispatchFirstContext(dispatchFirstSkillFile(join11(pluginRoot, "skills")))
|
|
32474
33033
|
}
|
|
32475
33034
|
}));
|
|
32476
33035
|
}
|
|
32477
|
-
function remainingUntil(at) {
|
|
32478
|
-
return Math.max(0, Math.round(at - performance.now()));
|
|
32479
|
-
}
|
|
32480
33036
|
function refreshLockHeld(lock) {
|
|
32481
33037
|
let age;
|
|
32482
33038
|
let text;
|
|
32483
33039
|
try {
|
|
32484
33040
|
age = Date.now() - statSync2(lock).mtimeMs;
|
|
32485
|
-
text =
|
|
33041
|
+
text = readFileSync7(lock, "utf8");
|
|
32486
33042
|
} catch (error) {
|
|
32487
33043
|
if (hasErrnoCode(error, "ENOENT"))
|
|
32488
33044
|
return false;
|
|
@@ -32518,7 +33074,7 @@ function takeRefreshLock(lock) {
|
|
|
32518
33074
|
function refreshOutcome(file, since) {
|
|
32519
33075
|
let value;
|
|
32520
33076
|
try {
|
|
32521
|
-
value = JSON.parse(
|
|
33077
|
+
value = JSON.parse(readFileSync7(file, "utf8"));
|
|
32522
33078
|
} catch {
|
|
32523
33079
|
return;
|
|
32524
33080
|
}
|
|
@@ -32532,16 +33088,13 @@ async function refreshedAnswers(session, cwd, dispatchUrl, startedAt, by) {
|
|
|
32532
33088
|
} catch (error) {
|
|
32533
33089
|
return { reason: messageFor(error) };
|
|
32534
33090
|
}
|
|
32535
|
-
const lock =
|
|
33091
|
+
const lock = join11(directory, REFRESH_LOCK);
|
|
32536
33092
|
if (!refreshLockHeld(lock)) {
|
|
32537
|
-
Bun.spawn([process.execPath, Bun.main, "answers-refresh", session, cwd, "--host", HOST], {
|
|
32538
|
-
|
|
32539
|
-
|
|
32540
|
-
stderr: "ignore",
|
|
32541
|
-
detached: true
|
|
32542
|
-
}).unref();
|
|
33093
|
+
const refresher = Bun.spawn([process.execPath, Bun.main, "answers-refresh", session, cwd, "--host", HOST], { stdin: "ignore", stdout: "ignore", stderr: "ignore", detached: true });
|
|
33094
|
+
refresher.unref();
|
|
33095
|
+
nameRefresher(refresher.pid);
|
|
32543
33096
|
}
|
|
32544
|
-
const file =
|
|
33097
|
+
const file = join11(directory, REFRESH_OUTCOME);
|
|
32545
33098
|
for (;; ) {
|
|
32546
33099
|
const outcome = refreshOutcome(file, startedAt);
|
|
32547
33100
|
if (outcome !== undefined) {
|
|
@@ -32553,13 +33106,13 @@ async function refreshedAnswers(session, cwd, dispatchUrl, startedAt, by) {
|
|
|
32553
33106
|
reason: `the refresher read ${read}, not the Dispatch envoy.json names now`
|
|
32554
33107
|
};
|
|
32555
33108
|
}
|
|
32556
|
-
const
|
|
32557
|
-
if (
|
|
33109
|
+
const now = performance.now();
|
|
33110
|
+
if (Math.round(by - now) <= 0) {
|
|
32558
33111
|
return {
|
|
32559
33112
|
reason: `a "!" command in envoy.json had not finished ${Math.round(by)} ms after the hook started`
|
|
32560
33113
|
};
|
|
32561
33114
|
}
|
|
32562
|
-
await Bun.sleep(
|
|
33115
|
+
await Bun.sleep(armedWait("refresher", by, { capMs: REFRESH_POLL_MS, at: now }));
|
|
32563
33116
|
}
|
|
32564
33117
|
}
|
|
32565
33118
|
async function answersMessage(session, cwd, startedAt, by) {
|
|
@@ -32584,7 +33137,7 @@ async function answersMessage(session, cwd, startedAt, by) {
|
|
|
32584
33137
|
dispatch,
|
|
32585
33138
|
dispatchUrl,
|
|
32586
33139
|
env: process.env,
|
|
32587
|
-
deadlineMs:
|
|
33140
|
+
deadlineMs: armedWait("read", by, { leastMs: ANSWERS_LEAST_WAIT_MS })
|
|
32588
33141
|
});
|
|
32589
33142
|
}
|
|
32590
33143
|
async function answers(text) {
|
|
@@ -32610,24 +33163,19 @@ async function answers(text) {
|
|
|
32610
33163
|
printed = performance.now();
|
|
32611
33164
|
await Promise.race([
|
|
32612
33165
|
sessionAnswersSettled(input.session_id),
|
|
32613
|
-
Bun.sleep(
|
|
33166
|
+
Bun.sleep(armedWait("settle", by + ANSWERS_SETTLE_MS))
|
|
32614
33167
|
]);
|
|
32615
33168
|
} catch (error) {
|
|
32616
33169
|
process.stderr.write(`envoy: people's answers were not added \u2014 ${messageFor(error)}
|
|
32617
33170
|
`);
|
|
32618
33171
|
}
|
|
32619
|
-
|
|
32620
|
-
if (timingFile !== undefined) {
|
|
32621
|
-
try {
|
|
32622
|
-
writeFileSync2(timingFile, JSON.stringify({ began, printed, ended: performance.now() }));
|
|
32623
|
-
} catch {}
|
|
32624
|
-
}
|
|
33172
|
+
writeAnswersTiming({ began, printed, ended: performance.now(), deadline: by });
|
|
32625
33173
|
process.exit(0);
|
|
32626
33174
|
}
|
|
32627
33175
|
async function answersRefresh(session, cwd) {
|
|
32628
33176
|
const directory = sessionDirectory(process.env, session);
|
|
32629
33177
|
mkdirSync2(directory, { recursive: true, mode: 448 });
|
|
32630
|
-
const lock =
|
|
33178
|
+
const lock = join11(directory, REFRESH_LOCK);
|
|
32631
33179
|
if (takeRefreshLock(lock)) {
|
|
32632
33180
|
try {
|
|
32633
33181
|
const file = sessionConfigFile(HOST, process.env);
|
|
@@ -32645,7 +33193,7 @@ async function answersRefresh(session, cwd) {
|
|
|
32645
33193
|
text: read.text ?? null,
|
|
32646
33194
|
notice: read.notice ?? null
|
|
32647
33195
|
};
|
|
32648
|
-
writeAtomically(
|
|
33196
|
+
writeAtomically(join11(directory, REFRESH_OUTCOME), JSON.stringify(outcome));
|
|
32649
33197
|
} finally {
|
|
32650
33198
|
rmSync3(lock, { force: true });
|
|
32651
33199
|
}
|