@uselemma/tracing 7.14.0 → 7.15.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/README.md +73 -17
- package/dist/client.d.ts +49 -7
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +204 -73
- package/dist/client.js.map +1 -1
- package/dist/coding-agent-backlog.d.ts +81 -0
- package/dist/coding-agent-backlog.d.ts.map +1 -0
- package/dist/coding-agent-backlog.js +239 -0
- package/dist/coding-agent-backlog.js.map +1 -0
- package/dist/coding-agent-delivery.d.ts +31 -0
- package/dist/coding-agent-delivery.d.ts.map +1 -0
- package/dist/coding-agent-delivery.js +97 -0
- package/dist/coding-agent-delivery.js.map +1 -0
- package/dist/coding-agent-queue.d.ts +74 -0
- package/dist/coding-agent-queue.d.ts.map +1 -0
- package/dist/coding-agent-queue.js +230 -0
- package/dist/coding-agent-queue.js.map +1 -0
- package/dist/coding-agent.d.ts +14 -0
- package/dist/coding-agent.d.ts.map +1 -1
- package/dist/coding-agent.js +52 -15
- package/dist/coding-agent.js.map +1 -1
- package/dist/debug-mode.d.ts.map +1 -1
- package/dist/debug-mode.js +4 -4
- package/dist/debug-mode.js.map +1 -1
- package/dist/delivery.d.ts +50 -7
- package/dist/delivery.d.ts.map +1 -1
- package/dist/delivery.js +117 -21
- package/dist/delivery.js.map +1 -1
- package/dist/env.d.ts +8 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +18 -0
- package/dist/env.js.map +1 -0
- package/dist/error-message.d.ts +10 -0
- package/dist/error-message.d.ts.map +1 -1
- package/dist/error-message.js +14 -0
- package/dist/error-message.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/integration-client.d.ts +20 -0
- package/dist/integration-client.d.ts.map +1 -0
- package/dist/integration-client.js +47 -0
- package/dist/integration-client.js.map +1 -0
- package/dist/langchain-context.d.ts +7 -0
- package/dist/langchain-context.d.ts.map +1 -0
- package/dist/langchain-context.js +28 -0
- package/dist/langchain-context.js.map +1 -0
- package/dist/langchain-eviction.d.ts +42 -0
- package/dist/langchain-eviction.d.ts.map +1 -0
- package/dist/langchain-eviction.js +56 -0
- package/dist/langchain-eviction.js.map +1 -0
- package/dist/langchain.d.ts +31 -13
- package/dist/langchain.d.ts.map +1 -1
- package/dist/langchain.js +74 -109
- package/dist/langchain.js.map +1 -1
- package/dist/mastra.d.ts +9 -11
- package/dist/mastra.d.ts.map +1 -1
- package/dist/mastra.js +40 -25
- package/dist/mastra.js.map +1 -1
- package/dist/openai-agents.d.ts +8 -0
- package/dist/openai-agents.d.ts.map +1 -1
- package/dist/openai-agents.js +17 -14
- package/dist/openai-agents.js.map +1 -1
- package/dist/serialize.d.ts +17 -0
- package/dist/serialize.d.ts.map +1 -0
- package/dist/serialize.js +143 -0
- package/dist/serialize.js.map +1 -0
- package/dist/stream-result.d.ts +33 -0
- package/dist/stream-result.d.ts.map +1 -0
- package/dist/stream-result.js +135 -0
- package/dist/stream-result.js.map +1 -0
- package/dist/vercel-ai-events.d.ts +308 -0
- package/dist/vercel-ai-events.d.ts.map +1 -0
- package/dist/vercel-ai-events.js +340 -0
- package/dist/vercel-ai-events.js.map +1 -0
- package/dist/vercel-ai-run.d.ts +171 -0
- package/dist/vercel-ai-run.d.ts.map +1 -0
- package/dist/vercel-ai-run.js +650 -0
- package/dist/vercel-ai-run.js.map +1 -0
- package/dist/vercel-ai.d.ts +36 -108
- package/dist/vercel-ai.d.ts.map +1 -1
- package/dist/vercel-ai.js +185 -882
- package/dist/vercel-ai.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/examples/vercel-ai-v6.ts +2 -2
- package/examples/vercel-ai-v7.ts +14 -16
- package/package.json +1 -1
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state behind a coding-agent plugin's backlog: a credential Lemma
|
|
3
|
+
* rejected, the cap on pending traces, the traces the cap dropped, and the
|
|
4
|
+
* notice a plugin shows for them when a session starts. Plugins import this
|
|
5
|
+
* module by path; it is not part of the package's public API.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
8
|
+
/** The names of the queued turn files, oldest first. */
|
|
9
|
+
export declare function pendingEntryNames(dataDir: string): Promise<string[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Pending traces a plugin keeps. Nothing is sent while Lemma rejects the
|
|
12
|
+
* credential or cannot be reached, so without a cap every new turn would grow
|
|
13
|
+
* the backlog. Two hundred covers several days of prompts, so a key fixed or
|
|
14
|
+
* an outage over within that time loses none.
|
|
15
|
+
*/
|
|
16
|
+
export declare const MAX_PENDING_TRACES = 200;
|
|
17
|
+
/**
|
|
18
|
+
* After a failed send: when Lemma refused the credential, record it so
|
|
19
|
+
* delivery stops until setup stores another, and return true. Every later
|
|
20
|
+
* send with the same credential would fail the same way.
|
|
21
|
+
*/
|
|
22
|
+
export declare function stopOnRejectedCredential(dataDir: string, credentialId: string, error: unknown): Promise<boolean>;
|
|
23
|
+
/**
|
|
24
|
+
* Remove the oldest pending files beyond `limit`. Each parsed file goes to
|
|
25
|
+
* `onDrop` first, so a plugin can remove its state for that turn; a file
|
|
26
|
+
* whose `onDrop` fails stays for the next flush. `counted` receives each
|
|
27
|
+
* removal as it happens, so a later failure loses no count.
|
|
28
|
+
*/
|
|
29
|
+
export declare function dropOldestPendingEntries(dataDir: string, limit: number, options?: {
|
|
30
|
+
onDrop?: (pending: unknown) => Promise<void>;
|
|
31
|
+
counted?: () => void;
|
|
32
|
+
}): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Prepare a flush with the stored credential. A rejection of an earlier
|
|
35
|
+
* credential is forgotten, since setup has stored a new one, and the backlog
|
|
36
|
+
* is capped; each dropped trace is counted for the next session-start
|
|
37
|
+
* notice, whatever kept it from being sent. While Lemma rejects this
|
|
38
|
+
* credential the flush sends nothing.
|
|
39
|
+
*/
|
|
40
|
+
export declare function prepareFlush(dataDir: string, credentialId: string, options?: {
|
|
41
|
+
onDrop?: (pending: unknown) => Promise<void>;
|
|
42
|
+
}): Promise<"ready" | "rejected">;
|
|
43
|
+
export type SessionStartNotice = {
|
|
44
|
+
/**
|
|
45
|
+
* A rejected key is shown at every session start until setup replaces it;
|
|
46
|
+
* dropped traces are shown once, by whoever claims their records.
|
|
47
|
+
*/
|
|
48
|
+
kind: "rejected" | "dropped";
|
|
49
|
+
/** Identifies what the notice reports, for showing it once. */
|
|
50
|
+
id: string;
|
|
51
|
+
message: string;
|
|
52
|
+
/**
|
|
53
|
+
* The drop records this notice reports, as `dropped/<name>.json` relative
|
|
54
|
+
* to the data dir. Claiming the notice removes exactly these.
|
|
55
|
+
*/
|
|
56
|
+
dropRecords: string[];
|
|
57
|
+
};
|
|
58
|
+
export type SessionStartNoticeOptions = {
|
|
59
|
+
harness: string;
|
|
60
|
+
setupCommand: string;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* The one-line warning for the next session start: Lemma rejects the stored
|
|
64
|
+
* credential, or the cap dropped traces since the last notice. Reading it
|
|
65
|
+
* changes nothing; claimSessionStartNotice marks it shown.
|
|
66
|
+
*/
|
|
67
|
+
export declare function readSessionStartNotice(dataDir: string, credentialId: string, options: SessionStartNoticeOptions): Promise<SessionStartNotice | null>;
|
|
68
|
+
/**
|
|
69
|
+
* Claim a notice before showing it: remove the drop records it reports and
|
|
70
|
+
* return whether it should be shown. A dropped-traces notice is shown only
|
|
71
|
+
* by the caller that removed at least one of its records, so two sessions
|
|
72
|
+
* starting together report the same drops once.
|
|
73
|
+
*/
|
|
74
|
+
export declare function claimSessionStartNotice(dataDir: string, notice: SessionStartNotice): Promise<boolean>;
|
|
75
|
+
/**
|
|
76
|
+
* Show the session-start notice for a hook event: claim it and return its
|
|
77
|
+
* message, or return null when `event` is not one of `startEvents`, no
|
|
78
|
+
* credential is stored, or another session already reported it.
|
|
79
|
+
*/
|
|
80
|
+
export declare function hookSessionStartNotice(event: unknown, startEvents: readonly string[], dataDir: string, credentialId: () => Promise<string | undefined>, options: SessionStartNoticeOptions): Promise<string | null>;
|
|
81
|
+
//# sourceMappingURL=coding-agent-backlog.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coding-agent-backlog.d.ts","sourceRoot":"","sources":["../src/coding-agent-backlog.ts"],"names":[],"mappings":"AAeA;;;;;GAKG;AAEH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEzE;AAaD,wDAAwD;AACxD,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAO1E;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,MAAM,CAAC;AA6EtC;;;;GAIG;AACH,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,KAAK,EAAE,OAAO,GACb,OAAO,CAAC,OAAO,CAAC,CAUlB;AAED;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;IACP,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACjB,GACL,OAAO,CAAC,IAAI,CAAC,CAgCf;AAED;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE;IAAE,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAAO,GAC7D,OAAO,CAAC,OAAO,GAAG,UAAU,CAAC,CA4B/B;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;OAGG;IACH,IAAI,EAAE,UAAU,GAAG,SAAS,CAAC;IAC7B,+DAA+D;IAC/D,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,WAAW,EAAE,MAAM,EAAE,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACtC,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAyBF;;;;GAIG;AACH,wBAAsB,sBAAsB,CAC1C,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,yBAAyB,GACjC,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAwBpC;AAED;;;;;GAKG;AACH,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,kBAAkB,GACzB,OAAO,CAAC,OAAO,CAAC,CAOlB;AAED;;;;GAIG;AACH,wBAAsB,sBAAsB,CAC1C,KAAK,EAAE,OAAO,EACd,WAAW,EAAE,SAAS,MAAM,EAAE,EAC9B,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,EAC/C,OAAO,EAAE,yBAAyB,GACjC,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAOxB"}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MAX_PENDING_TRACES = void 0;
|
|
4
|
+
exports.isRecord = isRecord;
|
|
5
|
+
exports.pendingEntryNames = pendingEntryNames;
|
|
6
|
+
exports.stopOnRejectedCredential = stopOnRejectedCredential;
|
|
7
|
+
exports.dropOldestPendingEntries = dropOldestPendingEntries;
|
|
8
|
+
exports.prepareFlush = prepareFlush;
|
|
9
|
+
exports.readSessionStartNotice = readSessionStartNotice;
|
|
10
|
+
exports.claimSessionStartNotice = claimSessionStartNotice;
|
|
11
|
+
exports.hookSessionStartNotice = hookSessionStartNotice;
|
|
12
|
+
const node_crypto_1 = require("node:crypto");
|
|
13
|
+
const promises_1 = require("node:fs/promises");
|
|
14
|
+
const node_path_1 = require("node:path");
|
|
15
|
+
const coding_agent_delivery_1 = require("./coding-agent-delivery");
|
|
16
|
+
/**
|
|
17
|
+
* The state behind a coding-agent plugin's backlog: a credential Lemma
|
|
18
|
+
* rejected, the cap on pending traces, the traces the cap dropped, and the
|
|
19
|
+
* notice a plugin shows for them when a session starts. Plugins import this
|
|
20
|
+
* module by path; it is not part of the package's public API.
|
|
21
|
+
*/
|
|
22
|
+
function isRecord(value) {
|
|
23
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
24
|
+
}
|
|
25
|
+
/** Remove a file; false when another process removed it first. */
|
|
26
|
+
async function removeIfPresent(path) {
|
|
27
|
+
return (0, promises_1.unlink)(path).then(() => true, (error) => {
|
|
28
|
+
if (error.code === "ENOENT")
|
|
29
|
+
return false;
|
|
30
|
+
throw error;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/** The names of the queued turn files, oldest first. */
|
|
34
|
+
async function pendingEntryNames(dataDir) {
|
|
35
|
+
return (0, promises_1.readdir)((0, node_path_1.join)(dataDir, "pending"))
|
|
36
|
+
.then((entries) => entries.filter((name) => name.endsWith(".json")).sort())
|
|
37
|
+
.catch((error) => {
|
|
38
|
+
if (error.code === "ENOENT")
|
|
39
|
+
return [];
|
|
40
|
+
throw error;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Pending traces a plugin keeps. Nothing is sent while Lemma rejects the
|
|
45
|
+
* credential or cannot be reached, so without a cap every new turn would grow
|
|
46
|
+
* the backlog. Two hundred covers several days of prompts, so a key fixed or
|
|
47
|
+
* an outage over within that time loses none.
|
|
48
|
+
*/
|
|
49
|
+
exports.MAX_PENDING_TRACES = 200;
|
|
50
|
+
const REJECTED_CREDENTIAL_FILE = "last-error.json";
|
|
51
|
+
const DROPPED_TRACES_DIR = "dropped";
|
|
52
|
+
const DROP_RECORD = /^dropped\/[0-9]+-[0-9a-f-]+\.json$/;
|
|
53
|
+
function isRejectedCredential(value) {
|
|
54
|
+
return (isRecord(value) &&
|
|
55
|
+
value.version === 1 &&
|
|
56
|
+
typeof value.credentialId === "string" &&
|
|
57
|
+
(value.status === 401 || value.status === 403) &&
|
|
58
|
+
typeof value.rejectedAt === "string");
|
|
59
|
+
}
|
|
60
|
+
function isDroppedTraces(value) {
|
|
61
|
+
return (isRecord(value) && value.version === 1 && typeof value.count === "number");
|
|
62
|
+
}
|
|
63
|
+
async function writeState(dataDir, name, value) {
|
|
64
|
+
const path = (0, node_path_1.join)(dataDir, name);
|
|
65
|
+
await (0, promises_1.mkdir)((0, node_path_1.dirname)(path), { recursive: true, mode: 0o700 });
|
|
66
|
+
const temporaryPath = `${path}.${process.pid}.${(0, node_crypto_1.randomUUID)()}.tmp`;
|
|
67
|
+
await (0, promises_1.writeFile)(temporaryPath, `${JSON.stringify(value)}\n`, {
|
|
68
|
+
encoding: "utf8",
|
|
69
|
+
mode: 0o600,
|
|
70
|
+
flag: "wx",
|
|
71
|
+
});
|
|
72
|
+
await (0, promises_1.rename)(temporaryPath, path);
|
|
73
|
+
}
|
|
74
|
+
async function readState(dataDir, name, isState) {
|
|
75
|
+
try {
|
|
76
|
+
const value = JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(dataDir, name), "utf8"));
|
|
77
|
+
return isState(value) ? value : null;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
if (error.code === "ENOENT" ||
|
|
81
|
+
error instanceof SyntaxError) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* After a failed send: when Lemma refused the credential, record it so
|
|
89
|
+
* delivery stops until setup stores another, and return true. Every later
|
|
90
|
+
* send with the same credential would fail the same way.
|
|
91
|
+
*/
|
|
92
|
+
async function stopOnRejectedCredential(dataDir, credentialId, error) {
|
|
93
|
+
const status = (0, coding_agent_delivery_1.rejectedCredentialStatus)(error);
|
|
94
|
+
if (!status)
|
|
95
|
+
return false;
|
|
96
|
+
await writeState(dataDir, REJECTED_CREDENTIAL_FILE, {
|
|
97
|
+
version: 1,
|
|
98
|
+
credentialId,
|
|
99
|
+
status,
|
|
100
|
+
rejectedAt: new Date().toISOString(),
|
|
101
|
+
});
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Remove the oldest pending files beyond `limit`. Each parsed file goes to
|
|
106
|
+
* `onDrop` first, so a plugin can remove its state for that turn; a file
|
|
107
|
+
* whose `onDrop` fails stays for the next flush. `counted` receives each
|
|
108
|
+
* removal as it happens, so a later failure loses no count.
|
|
109
|
+
*/
|
|
110
|
+
async function dropOldestPendingEntries(dataDir, limit, options = {}) {
|
|
111
|
+
const pendingDir = (0, node_path_1.join)(dataDir, "pending");
|
|
112
|
+
const names = await pendingEntryNames(dataDir);
|
|
113
|
+
if (names.length <= limit)
|
|
114
|
+
return;
|
|
115
|
+
const entries = await Promise.all(names.map(async (name) => ({
|
|
116
|
+
name,
|
|
117
|
+
modified: await (0, promises_1.stat)((0, node_path_1.join)(pendingDir, name)).then((entry) => entry.mtimeMs, () => Number.POSITIVE_INFINITY),
|
|
118
|
+
})));
|
|
119
|
+
entries.sort((a, b) => a.modified - b.modified || a.name.localeCompare(b.name));
|
|
120
|
+
for (const { name } of entries.slice(0, entries.length - limit)) {
|
|
121
|
+
const path = (0, node_path_1.join)(pendingDir, name);
|
|
122
|
+
if (options.onDrop) {
|
|
123
|
+
const value = await (0, promises_1.readFile)(path, "utf8")
|
|
124
|
+
.then((text) => JSON.parse(text))
|
|
125
|
+
.catch(() => null);
|
|
126
|
+
const handled = value === null ||
|
|
127
|
+
(await options.onDrop(value).then(() => true, () => false));
|
|
128
|
+
if (!handled)
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (await removeIfPresent(path))
|
|
132
|
+
options.counted?.();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Prepare a flush with the stored credential. A rejection of an earlier
|
|
137
|
+
* credential is forgotten, since setup has stored a new one, and the backlog
|
|
138
|
+
* is capped; each dropped trace is counted for the next session-start
|
|
139
|
+
* notice, whatever kept it from being sent. While Lemma rejects this
|
|
140
|
+
* credential the flush sends nothing.
|
|
141
|
+
*/
|
|
142
|
+
async function prepareFlush(dataDir, credentialId, options = {}) {
|
|
143
|
+
const recorded = await readState(dataDir, REJECTED_CREDENTIAL_FILE, isRejectedCredential);
|
|
144
|
+
const rejected = recorded?.credentialId === credentialId;
|
|
145
|
+
if (recorded && !rejected) {
|
|
146
|
+
await (0, promises_1.rm)((0, node_path_1.join)(dataDir, REJECTED_CREDENTIAL_FILE), { force: true });
|
|
147
|
+
}
|
|
148
|
+
let dropped = 0;
|
|
149
|
+
try {
|
|
150
|
+
await dropOldestPendingEntries(dataDir, exports.MAX_PENDING_TRACES, {
|
|
151
|
+
onDrop: options.onDrop,
|
|
152
|
+
counted: () => {
|
|
153
|
+
dropped += 1;
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
if (dropped > 0) {
|
|
159
|
+
await writeState(dataDir, `${DROPPED_TRACES_DIR}/${Date.now()}-${(0, node_crypto_1.randomUUID)()}.json`, { version: 1, count: dropped });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return rejected ? "rejected" : "ready";
|
|
163
|
+
}
|
|
164
|
+
async function readDropRecords(dataDir) {
|
|
165
|
+
const records = await (0, promises_1.readdir)((0, node_path_1.join)(dataDir, DROPPED_TRACES_DIR))
|
|
166
|
+
.then((entries) => entries
|
|
167
|
+
.map((name) => `${DROPPED_TRACES_DIR}/${name}`)
|
|
168
|
+
.filter((record) => DROP_RECORD.test(record))
|
|
169
|
+
.sort())
|
|
170
|
+
.catch((error) => {
|
|
171
|
+
if (error.code === "ENOENT")
|
|
172
|
+
return [];
|
|
173
|
+
throw error;
|
|
174
|
+
});
|
|
175
|
+
const values = await Promise.all(records.map((record) => readState(dataDir, record, isDroppedTraces)));
|
|
176
|
+
return {
|
|
177
|
+
count: values.reduce((sum, value) => sum + (value?.count ?? 0), 0),
|
|
178
|
+
records: records.filter((_record, index) => values[index] !== null),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* The one-line warning for the next session start: Lemma rejects the stored
|
|
183
|
+
* credential, or the cap dropped traces since the last notice. Reading it
|
|
184
|
+
* changes nothing; claimSessionStartNotice marks it shown.
|
|
185
|
+
*/
|
|
186
|
+
async function readSessionStartNotice(dataDir, credentialId, options) {
|
|
187
|
+
const [rejection, dropped] = await Promise.all([
|
|
188
|
+
readState(dataDir, REJECTED_CREDENTIAL_FILE, isRejectedCredential),
|
|
189
|
+
readDropRecords(dataDir),
|
|
190
|
+
]);
|
|
191
|
+
const droppedNote = dropped.count > 0
|
|
192
|
+
? `${dropped.count} older ${dropped.count === 1 ? "trace was" : "traces were"} dropped past the ${exports.MAX_PENDING_TRACES}-trace backlog limit.`
|
|
193
|
+
: "";
|
|
194
|
+
if (rejection?.credentialId === credentialId) {
|
|
195
|
+
return {
|
|
196
|
+
kind: "rejected",
|
|
197
|
+
id: `rejected:${rejection.rejectedAt}`,
|
|
198
|
+
message: `Lemma stopped sending ${options.harness} traces: it rejected this plugin's key (HTTP ${rejection.status}), which happens when the key is revoked or the person who approved it leaves the organization. Run \`${options.setupCommand}\` to create a new key; traces queued until then are sent with it.${droppedNote ? ` ${droppedNote}` : ""}`,
|
|
199
|
+
dropRecords: dropped.records,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
if (dropped.count === 0)
|
|
203
|
+
return null;
|
|
204
|
+
return {
|
|
205
|
+
kind: "dropped",
|
|
206
|
+
id: `dropped:${(0, node_crypto_1.createHash)("sha256").update(dropped.records.join("\n")).digest("hex").slice(0, 16)}`,
|
|
207
|
+
message: `Lemma could not send ${options.harness} traces for a while: ${droppedNote}`,
|
|
208
|
+
dropRecords: dropped.records,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Claim a notice before showing it: remove the drop records it reports and
|
|
213
|
+
* return whether it should be shown. A dropped-traces notice is shown only
|
|
214
|
+
* by the caller that removed at least one of its records, so two sessions
|
|
215
|
+
* starting together report the same drops once.
|
|
216
|
+
*/
|
|
217
|
+
async function claimSessionStartNotice(dataDir, notice) {
|
|
218
|
+
const removed = await Promise.all(notice.dropRecords
|
|
219
|
+
.filter((record) => DROP_RECORD.test(record))
|
|
220
|
+
.map((record) => removeIfPresent((0, node_path_1.join)(dataDir, record))));
|
|
221
|
+
return notice.kind === "rejected" || removed.some(Boolean);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Show the session-start notice for a hook event: claim it and return its
|
|
225
|
+
* message, or return null when `event` is not one of `startEvents`, no
|
|
226
|
+
* credential is stored, or another session already reported it.
|
|
227
|
+
*/
|
|
228
|
+
async function hookSessionStartNotice(event, startEvents, dataDir, credentialId, options) {
|
|
229
|
+
if (typeof event !== "string" || !startEvents.includes(event))
|
|
230
|
+
return null;
|
|
231
|
+
const id = await credentialId();
|
|
232
|
+
if (!id)
|
|
233
|
+
return null;
|
|
234
|
+
const notice = await readSessionStartNotice(dataDir, id, options);
|
|
235
|
+
if (!notice || !(await claimSessionStartNotice(dataDir, notice)))
|
|
236
|
+
return null;
|
|
237
|
+
return notice.message;
|
|
238
|
+
}
|
|
239
|
+
//# sourceMappingURL=coding-agent-backlog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coding-agent-backlog.js","sourceRoot":"","sources":["../src/coding-agent-backlog.ts"],"names":[],"mappings":";;;AAsBA,4BAEC;AAcD,8CAOC;AA0FD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D,CAAC;IACF,OAAO,MAAM,CAAC,IAAI,KAAK,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAC7D,CAAC;AAED;;;;GAIG;AACI,KAAK,UAAU,sBAAsB,CAC1C,KAAc,EACd,WAA8B,EAC9B,OAAe,EACf,YAA+C,EAC/C,OAAkC;IAElC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAC3E,MAAM,EAAE,GAAG,MAAM,YAAY,EAAE,CAAC;IAChC,IAAI,CAAC,EAAE;QAAE,OAAO,IAAI,CAAC;IACrB,MAAM,MAAM,GAAG,MAAM,sBAAsB,CAAC,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;IAClE,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,uBAAuB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IAC9E,OAAO,MAAM,CAAC,OAAO,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delivery helpers for the coding-agent plugins: a deadline-bounded fetch,
|
|
3
|
+
* and a failed delivery described by HTTP status without the response body,
|
|
4
|
+
* which can echo request content. Plugins import this module by path; it is
|
|
5
|
+
* not part of the package's public API.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Bounds one delivery, including reading an error response's body, by the
|
|
9
|
+
* time left before the flush deadline, which the SDK's per-attempt cap does
|
|
10
|
+
* not know.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createDeliveryFetch(fetchImplementation: typeof fetch, remainingMilliseconds: number): typeof fetch;
|
|
13
|
+
/**
|
|
14
|
+
* A failed delivery, described without the response body, which can echo
|
|
15
|
+
* request content.
|
|
16
|
+
*/
|
|
17
|
+
export declare class TraceDeliveryError extends Error {
|
|
18
|
+
readonly status: number | undefined;
|
|
19
|
+
constructor(status: number | undefined, cause: unknown);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The status of a delivery Lemma refused because of the credential: it was
|
|
23
|
+
* revoked, or the person who approved it left the organization.
|
|
24
|
+
*/
|
|
25
|
+
export declare function rejectedCredentialStatus(error: unknown): 401 | 403 | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Run one delivery through `send`, handing it a fetch that records the
|
|
28
|
+
* response status, and rethrow a failure as a TraceDeliveryError.
|
|
29
|
+
*/
|
|
30
|
+
export declare function deliverTrace(fetchImplementation: typeof fetch, send: (deliveryFetch: typeof fetch) => Promise<void>): Promise<void>;
|
|
31
|
+
//# sourceMappingURL=coding-agent-delivery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coding-agent-delivery.d.ts","sourceRoot":"","sources":["../src/coding-agent-delivery.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,mBAAmB,EAAE,OAAO,KAAK,EACjC,qBAAqB,EAAE,MAAM,GAC5B,OAAO,KAAK,CA+Bd;AAED;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;gBAExB,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,EAAE,OAAO;CAYvD;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,KAAK,EAAE,OAAO,GACb,GAAG,GAAG,GAAG,GAAG,SAAS,CAIvB;AAED;;;GAGG;AACH,wBAAsB,YAAY,CAChC,mBAAmB,EAAE,OAAO,KAAK,EACjC,IAAI,EAAE,CAAC,aAAa,EAAE,OAAO,KAAK,KAAK,OAAO,CAAC,IAAI,CAAC,GACnD,OAAO,CAAC,IAAI,CAAC,CAWf"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Delivery helpers for the coding-agent plugins: a deadline-bounded fetch,
|
|
4
|
+
* and a failed delivery described by HTTP status without the response body,
|
|
5
|
+
* which can echo request content. Plugins import this module by path; it is
|
|
6
|
+
* not part of the package's public API.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.TraceDeliveryError = void 0;
|
|
10
|
+
exports.createDeliveryFetch = createDeliveryFetch;
|
|
11
|
+
exports.rejectedCredentialStatus = rejectedCredentialStatus;
|
|
12
|
+
exports.deliverTrace = deliverTrace;
|
|
13
|
+
/**
|
|
14
|
+
* Bounds one delivery, including reading an error response's body, by the
|
|
15
|
+
* time left before the flush deadline, which the SDK's per-attempt cap does
|
|
16
|
+
* not know.
|
|
17
|
+
*/
|
|
18
|
+
function createDeliveryFetch(fetchImplementation, remainingMilliseconds) {
|
|
19
|
+
return async (request, init) => {
|
|
20
|
+
const controller = new AbortController();
|
|
21
|
+
const signal = init?.signal
|
|
22
|
+
? AbortSignal.any([init.signal, controller.signal])
|
|
23
|
+
: controller.signal;
|
|
24
|
+
let timeout;
|
|
25
|
+
const timedOut = new Promise((_, reject) => {
|
|
26
|
+
timeout = setTimeout(() => {
|
|
27
|
+
controller.abort();
|
|
28
|
+
const error = new Error("Lemma trace delivery timed out");
|
|
29
|
+
error.name = "TimeoutError";
|
|
30
|
+
reject(error);
|
|
31
|
+
}, remainingMilliseconds);
|
|
32
|
+
});
|
|
33
|
+
const requestAndBody = (async () => {
|
|
34
|
+
const response = await fetchImplementation(request, { ...init, signal });
|
|
35
|
+
if (response.ok || !response.body)
|
|
36
|
+
return response;
|
|
37
|
+
const body = await response.arrayBuffer();
|
|
38
|
+
return new Response(body, {
|
|
39
|
+
status: response.status,
|
|
40
|
+
statusText: response.statusText,
|
|
41
|
+
headers: response.headers,
|
|
42
|
+
});
|
|
43
|
+
})();
|
|
44
|
+
try {
|
|
45
|
+
return await Promise.race([requestAndBody, timedOut]);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
if (timeout)
|
|
49
|
+
clearTimeout(timeout);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A failed delivery, described without the response body, which can echo
|
|
55
|
+
* request content.
|
|
56
|
+
*/
|
|
57
|
+
class TraceDeliveryError extends Error {
|
|
58
|
+
status;
|
|
59
|
+
constructor(status, cause) {
|
|
60
|
+
super(status !== undefined
|
|
61
|
+
? `Lemma answered HTTP ${status}`
|
|
62
|
+
: cause instanceof Error && cause.name === "TimeoutError"
|
|
63
|
+
? "the request to Lemma timed out"
|
|
64
|
+
: "Lemma could not be reached", { cause });
|
|
65
|
+
this.name = "TraceDeliveryError";
|
|
66
|
+
this.status = status;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.TraceDeliveryError = TraceDeliveryError;
|
|
70
|
+
/**
|
|
71
|
+
* The status of a delivery Lemma refused because of the credential: it was
|
|
72
|
+
* revoked, or the person who approved it left the organization.
|
|
73
|
+
*/
|
|
74
|
+
function rejectedCredentialStatus(error) {
|
|
75
|
+
if (!(error instanceof TraceDeliveryError))
|
|
76
|
+
return undefined;
|
|
77
|
+
const { status } = error;
|
|
78
|
+
return status === 401 || status === 403 ? status : undefined;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Run one delivery through `send`, handing it a fetch that records the
|
|
82
|
+
* response status, and rethrow a failure as a TraceDeliveryError.
|
|
83
|
+
*/
|
|
84
|
+
async function deliverTrace(fetchImplementation, send) {
|
|
85
|
+
let status;
|
|
86
|
+
try {
|
|
87
|
+
await send(async (request, init) => {
|
|
88
|
+
const response = await fetchImplementation(request, init);
|
|
89
|
+
status = response.status;
|
|
90
|
+
return response;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
throw new TraceDeliveryError(status, error);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=coding-ag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ACjC,IAAoD;IAEpD,IAAI,MAA0B,CAAC;IAC/B,IAAI,CAAC;QACH,MAAM,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;YACjC,MAAM,QAAQ,GAAG,MAAM,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;YAC1D,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;YACzB,OAAO,QAAQ,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,kBAAkB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC9C,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { CodingAgentHarnessId, CompletedCodingAgentTurn } from "./coding-agent";
|
|
2
|
+
/**
|
|
3
|
+
* The pending-turn queue of the coding-agent plugins that run inside the
|
|
4
|
+
* agent's process. Plugins import this module by path; it is not part of the
|
|
5
|
+
* package's public API, and it imports nothing at runtime from the package
|
|
6
|
+
* entry point, so a plugin that loads the package itself bundles no second
|
|
7
|
+
* copy of it.
|
|
8
|
+
*
|
|
9
|
+
* Each completed turn is one JSON file under `<dataDir>/pending/`, named so
|
|
10
|
+
* that older turns sort first. A flush sends the files in name order under a
|
|
11
|
+
* directory lock that one process owns at a time, removes each file once its
|
|
12
|
+
* trace is delivered, and leaves a failed one for the next flush.
|
|
13
|
+
*/
|
|
14
|
+
/** Total time one flush spends delivering, so a backlog never holds the agent. */
|
|
15
|
+
export declare const DEFAULT_FLUSH_TIMEOUT_MS = 10000;
|
|
16
|
+
/**
|
|
17
|
+
* Take the flush lock, or return null while another live process holds it.
|
|
18
|
+
* A lock whose owner exited is reclaimed once it is stale, by exactly one
|
|
19
|
+
* contender.
|
|
20
|
+
*/
|
|
21
|
+
export declare function acquireFlushLock(dataDir: string): Promise<(() => Promise<void>) | null>;
|
|
22
|
+
/** The API, project and credential a turn was captured under. */
|
|
23
|
+
export type CredentialScope = {
|
|
24
|
+
apiUrl: string;
|
|
25
|
+
projectId: string;
|
|
26
|
+
credentialId: string;
|
|
27
|
+
};
|
|
28
|
+
/** Queue a completed turn, atomically and readable only by the user. */
|
|
29
|
+
export declare function writePendingTurn(dataDir: string, turn: CompletedCodingAgentTurn, scope: CredentialScope): Promise<void>;
|
|
30
|
+
/** Why a flush kept a queued turn for a later flush. */
|
|
31
|
+
export type RetainedTurn =
|
|
32
|
+
/** The file is unreadable, or not a completed turn of this harness. */
|
|
33
|
+
{
|
|
34
|
+
kind: "invalid";
|
|
35
|
+
}
|
|
36
|
+
/** It was queued for another API or project than the stored credential's. */
|
|
37
|
+
| {
|
|
38
|
+
kind: "otherProject";
|
|
39
|
+
}
|
|
40
|
+
/** Sending or dequeuing it failed. */
|
|
41
|
+
| {
|
|
42
|
+
kind: "failed";
|
|
43
|
+
error: unknown;
|
|
44
|
+
};
|
|
45
|
+
export type FlushPendingTurnsOptions<Credentials extends CredentialScope> = {
|
|
46
|
+
dataDir: string;
|
|
47
|
+
harness: CodingAgentHarnessId;
|
|
48
|
+
/** Epoch milliseconds after which no further delivery starts. */
|
|
49
|
+
deadline: number;
|
|
50
|
+
now?: () => number;
|
|
51
|
+
/**
|
|
52
|
+
* The stored credential, read each time the flush takes the lock. Null
|
|
53
|
+
* ends the flush without sending.
|
|
54
|
+
*/
|
|
55
|
+
credentials: () => Promise<Credentials | null>;
|
|
56
|
+
send: (turn: CompletedCodingAgentTurn, credentials: Credentials, remainingMilliseconds: number) => Promise<void>;
|
|
57
|
+
/** A turn that was not sent stays queued for a later flush. */
|
|
58
|
+
retained: (name: string, reason: RetainedTurn) => void;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Send every queued turn once, oldest first, until the deadline. Turns
|
|
62
|
+
* queued while the flush runs are sent before it returns. A turn queued for
|
|
63
|
+
* another API or project waits until setup points back at it. Returns how
|
|
64
|
+
* many were sent; 0 when another process holds the lock.
|
|
65
|
+
*/
|
|
66
|
+
export declare function flushPendingTurns<Credentials extends CredentialScope>(options: FlushPendingTurnsOptions<Credentials>): Promise<number>;
|
|
67
|
+
/**
|
|
68
|
+
* Why a queued trace was kept, for a retry warning. A file that fails to
|
|
69
|
+
* parse is not quoted, since the parser's message can echo its content.
|
|
70
|
+
*/
|
|
71
|
+
export declare function retainedReason(error: unknown): string;
|
|
72
|
+
/** Why the shared flush kept a queued turn, for a retry warning. */
|
|
73
|
+
export declare function describeRetained(reason: RetainedTurn): string;
|
|
74
|
+
//# sourceMappingURL=coding-agent-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coding-agent-queue.d.ts","sourceRoot":"","sources":["../src/coding-agent-queue.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EACV,oBAAoB,EACpB,wBAAwB,EACzB,MAAM,gBAAgB,CAAC;AAQxB;;;;;;;;;;;GAWG;AAEH,kFAAkF;AAClF,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAwC/C;;;;GAIG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CA4CvC;AAED,iEAAiE;AACjE,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAuBF,wEAAwE;AACxE,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,wBAAwB,EAC9B,KAAK,EAAE,eAAe,GACrB,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED,wDAAwD;AACxD,MAAM,MAAM,YAAY;AACtB,uEAAuE;AACrE;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE;AACrB,6EAA6E;GAC3E;IAAE,IAAI,EAAE,cAAc,CAAA;CAAE;AAC1B,sCAAsC;GACp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