@tangle-network/agent-app 0.46.37 → 0.46.41
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 +3 -2
- package/dist/assistant/index.js +1 -1
- package/dist/chat-react/index.js +1 -1
- package/dist/chat-routes/index.js +1 -1
- package/dist/{chunk-BDMDL4AB.js → chunk-35TMEOFA.js} +16 -9
- package/dist/chunk-35TMEOFA.js.map +1 -0
- package/dist/{chunk-64EVYEGI.js → chunk-PZOTRGGQ.js} +162 -8
- package/dist/chunk-PZOTRGGQ.js.map +1 -0
- package/dist/{chunk-7ZPEJWDI.js → chunk-QK4IFP3E.js} +2 -2
- package/dist/{chunk-7ZPEJWDI.js.map → chunk-QK4IFP3E.js.map} +1 -1
- package/dist/sandbox/foreground-single-flight.d.ts +62 -0
- package/dist/sandbox/index.d.ts +1 -0
- package/dist/sandbox/index.js +13 -1
- package/dist/sandbox/prewarm-claim-d1.d.ts +15 -8
- package/dist/sandbox/prewarm.d.ts +44 -0
- package/dist/signoff/cli.js +1 -1
- package/dist/signoff/index.js +1 -1
- package/dist/signoff/workflow-pin.d.ts +3 -2
- package/dist/trace/index.d.ts +1 -0
- package/dist/trace/index.js +240 -0
- package/dist/trace/index.js.map +1 -1
- package/dist/trace/stage-timing.d.ts +61 -0
- package/dist/web-react/index.js +1 -1
- package/package.json +3 -3
- package/dist/chunk-64EVYEGI.js.map +0 -1
- package/dist/chunk-BDMDL4AB.js.map +0 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serialize request-time sandbox provisioning across Worker isolates.
|
|
3
|
+
*
|
|
4
|
+
* Background prewarming can lose a race without delaying a request. A real
|
|
5
|
+
* user turn cannot: its losing request must wait for the owner, adopt the box
|
|
6
|
+
* that became ready, or report why the owner did not leave a usable box.
|
|
7
|
+
*/
|
|
8
|
+
import type { Harness } from '../harness/index';
|
|
9
|
+
import type { PeekWorkspaceSandboxOutcome } from './index';
|
|
10
|
+
import { type FencedPrewarmClaimStore } from './prewarm';
|
|
11
|
+
export declare const DEFAULT_FOREGROUND_PROVISION_CLAIM_TTL_SECONDS = 180;
|
|
12
|
+
export declare const DEFAULT_FOREGROUND_PROVISION_POLL_INTERVAL_MS = 5000;
|
|
13
|
+
export type ReadyRunningSandboxOutcome = Extract<PeekWorkspaceSandboxOutcome, {
|
|
14
|
+
status: 'running';
|
|
15
|
+
}> & {
|
|
16
|
+
box: Extract<PeekWorkspaceSandboxOutcome, {
|
|
17
|
+
status: 'running';
|
|
18
|
+
}>['box'] & {
|
|
19
|
+
filesystemIncarnationReadiness: 'ready';
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export declare class SandboxProvisioningFailedElsewhereError extends Error {
|
|
23
|
+
readonly workspaceId: string;
|
|
24
|
+
readonly state: string;
|
|
25
|
+
readonly code = "sandbox.provisioning_failed_elsewhere";
|
|
26
|
+
constructor(workspaceId: string, state: string);
|
|
27
|
+
}
|
|
28
|
+
export declare class SandboxFilesystemNotReadyError extends Error {
|
|
29
|
+
readonly workspaceId: string;
|
|
30
|
+
readonly readiness: 'transitioning' | 'missing';
|
|
31
|
+
readonly code = "sandbox.filesystem_not_ready";
|
|
32
|
+
readonly retryable = true;
|
|
33
|
+
constructor(workspaceId: string, readiness: 'transitioning' | 'missing');
|
|
34
|
+
}
|
|
35
|
+
export type ForegroundSandboxSingleFlightEvent = {
|
|
36
|
+
type: 'waiting';
|
|
37
|
+
key: string;
|
|
38
|
+
workspaceId: string;
|
|
39
|
+
} | {
|
|
40
|
+
type: 'release-failed';
|
|
41
|
+
key: string;
|
|
42
|
+
workspaceId: string;
|
|
43
|
+
error: string;
|
|
44
|
+
};
|
|
45
|
+
export interface ForegroundSandboxSingleFlightOptions<T> {
|
|
46
|
+
claim: FencedPrewarmClaimStore;
|
|
47
|
+
workspaceId: string;
|
|
48
|
+
harness: Harness;
|
|
49
|
+
provision(): Promise<T>;
|
|
50
|
+
peek(): Promise<PeekWorkspaceSandboxOutcome>;
|
|
51
|
+
adopt(box: ReadyRunningSandboxOutcome): T | Promise<T>;
|
|
52
|
+
onEvent?(event: ForegroundSandboxSingleFlightEvent): void;
|
|
53
|
+
wait?(ms: number): Promise<void>;
|
|
54
|
+
claimTtlSeconds?: number;
|
|
55
|
+
pollIntervalMs?: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Run one foreground provision or adopt the ready result from its current
|
|
59
|
+
* owner. An expired retained claim permits takeover. A released claim without
|
|
60
|
+
* a usable box reports the prior attempt instead of starting a retry storm.
|
|
61
|
+
*/
|
|
62
|
+
export declare function runForegroundSandboxSingleFlight<T>(options: ForegroundSandboxSingleFlightOptions<T>): Promise<T>;
|
package/dist/sandbox/index.d.ts
CHANGED
package/dist/sandbox/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
+
DEFAULT_FOREGROUND_PROVISION_CLAIM_TTL_SECONDS,
|
|
3
|
+
DEFAULT_FOREGROUND_PROVISION_POLL_INTERVAL_MS,
|
|
2
4
|
DEFAULT_PREWARM_CLAIM_TABLE,
|
|
3
5
|
DEFAULT_PROVISION_TIMEOUT_MS,
|
|
4
6
|
DEFAULT_SANDBOX_RESOURCES,
|
|
@@ -12,7 +14,9 @@ import {
|
|
|
12
14
|
PYPI_EGRESS_DOMAINS,
|
|
13
15
|
SANDBOX_BACKING_CONTAINER_MISSING_CODE,
|
|
14
16
|
SandboxEgressPolicyMismatchError,
|
|
17
|
+
SandboxFilesystemNotReadyError,
|
|
15
18
|
SandboxModelResolutionError,
|
|
19
|
+
SandboxProvisioningFailedElsewhereError,
|
|
16
20
|
SandboxRecoveryFailedError,
|
|
17
21
|
SandboxRuntimeAuthRefreshError,
|
|
18
22
|
WORKSPACE_SANDBOX_HOST_EXHAUSTED,
|
|
@@ -71,8 +75,10 @@ import {
|
|
|
71
75
|
resolveModel,
|
|
72
76
|
resolveModelSelection,
|
|
73
77
|
resolveSandboxClientCredentials,
|
|
78
|
+
runForegroundSandboxSingleFlight,
|
|
74
79
|
runSandboxPrompt,
|
|
75
80
|
runSandboxToolPathSetup,
|
|
81
|
+
sandboxPrewarmClaimKey,
|
|
76
82
|
sandboxToolBinDir,
|
|
77
83
|
sandboxToolPath,
|
|
78
84
|
sandboxToolRootDir,
|
|
@@ -92,7 +98,7 @@ import {
|
|
|
92
98
|
workspaceSandboxRecoveryMessage,
|
|
93
99
|
workspaceSandboxRecoveryRecommendedActions,
|
|
94
100
|
writeProfileFilesToBox
|
|
95
|
-
} from "../chunk-
|
|
101
|
+
} from "../chunk-PZOTRGGQ.js";
|
|
96
102
|
import "../chunk-TH7L265V.js";
|
|
97
103
|
import "../chunk-LWSJK546.js";
|
|
98
104
|
import "../chunk-P5PIAQVS.js";
|
|
@@ -102,6 +108,8 @@ import "../chunk-UGWQLQDS.js";
|
|
|
102
108
|
import "../chunk-S5SRJJQG.js";
|
|
103
109
|
import "../chunk-JML7WKWU.js";
|
|
104
110
|
export {
|
|
111
|
+
DEFAULT_FOREGROUND_PROVISION_CLAIM_TTL_SECONDS,
|
|
112
|
+
DEFAULT_FOREGROUND_PROVISION_POLL_INTERVAL_MS,
|
|
105
113
|
DEFAULT_PREWARM_CLAIM_TABLE,
|
|
106
114
|
DEFAULT_PROVISION_TIMEOUT_MS,
|
|
107
115
|
DEFAULT_SANDBOX_RESOURCES,
|
|
@@ -115,7 +123,9 @@ export {
|
|
|
115
123
|
PYPI_EGRESS_DOMAINS,
|
|
116
124
|
SANDBOX_BACKING_CONTAINER_MISSING_CODE,
|
|
117
125
|
SandboxEgressPolicyMismatchError,
|
|
126
|
+
SandboxFilesystemNotReadyError,
|
|
118
127
|
SandboxModelResolutionError,
|
|
128
|
+
SandboxProvisioningFailedElsewhereError,
|
|
119
129
|
SandboxRecoveryFailedError,
|
|
120
130
|
SandboxRuntimeAuthRefreshError,
|
|
121
131
|
WORKSPACE_SANDBOX_HOST_EXHAUSTED,
|
|
@@ -174,8 +184,10 @@ export {
|
|
|
174
184
|
resolveModel,
|
|
175
185
|
resolveModelSelection,
|
|
176
186
|
resolveSandboxClientCredentials,
|
|
187
|
+
runForegroundSandboxSingleFlight,
|
|
177
188
|
runSandboxPrompt,
|
|
178
189
|
runSandboxToolPathSetup,
|
|
190
|
+
sandboxPrewarmClaimKey,
|
|
179
191
|
sandboxToolBinDir,
|
|
180
192
|
sandboxToolPath,
|
|
181
193
|
sandboxToolRootDir,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { FencedPrewarmClaimStore } from './prewarm';
|
|
1
2
|
/**
|
|
2
3
|
* `createD1PrewarmClaimStore` — the cross-isolate half of
|
|
3
4
|
* `createSandboxPrewarmer`'s single-flight, implemented once.
|
|
@@ -27,6 +28,15 @@
|
|
|
27
28
|
* reporting `changes: 0` is a SQLite detail, whereas "no row came back" is the
|
|
28
29
|
* statement telling you directly.
|
|
29
30
|
*
|
|
31
|
+
* Foreground provisioning uses `acquireLease`/`releaseLease`. The lease's
|
|
32
|
+
* returned `expiresAt` is a fencing value: takeover writes a value strictly
|
|
33
|
+
* greater than the expired value, and release changes only the matching value
|
|
34
|
+
* into a signed tombstone. A later acquire advances the absolute value, so a
|
|
35
|
+
* stale release cannot collide even after a same-clock release and reacquire.
|
|
36
|
+
* `inspect` hides the tombstone as `absent`. This uses the published two-column
|
|
37
|
+
* table without changing the legacy `PrewarmClaimStore` methods or requiring a
|
|
38
|
+
* migration.
|
|
39
|
+
*
|
|
30
40
|
* ── STRUCTURAL, NOT A DEPENDENCY ───────────────────────────────────────────
|
|
31
41
|
* The binding is taken as the narrow shape actually used (`prepare().bind()`,
|
|
32
42
|
* `.first()`, `.run()`), per the package's structural-over-hard-dep rule. No
|
|
@@ -60,11 +70,12 @@ export interface D1PrewarmClaimStoreOptions {
|
|
|
60
70
|
now?(): number;
|
|
61
71
|
}
|
|
62
72
|
export declare const DEFAULT_PREWARM_CLAIM_TABLE = "sandbox_prewarm_claims";
|
|
63
|
-
/** Paste into a migration. `expires_at`
|
|
64
|
-
*
|
|
73
|
+
/** Paste into a migration. Positive `expires_at` values are epoch MILLISECONDS,
|
|
74
|
+
* matching `Date.now()`. A released fenced claim keeps a negative tombstone so
|
|
75
|
+
* a later lease can advance its fencing value without adding a column. */
|
|
65
76
|
export declare const PREWARM_CLAIM_TABLE_DDL = "CREATE TABLE IF NOT EXISTS sandbox_prewarm_claims (\n key TEXT PRIMARY KEY,\n expires_at INTEGER NOT NULL\n)";
|
|
66
77
|
/**
|
|
67
|
-
* A `PrewarmClaimStore` backed by one D1 table.
|
|
78
|
+
* A fenced `PrewarmClaimStore` backed by one D1 table.
|
|
68
79
|
*
|
|
69
80
|
* ```ts
|
|
70
81
|
* const prewarmer = createSandboxPrewarmer(shell, {
|
|
@@ -73,8 +84,4 @@ export declare const PREWARM_CLAIM_TABLE_DDL = "CREATE TABLE IF NOT EXISTS sandb
|
|
|
73
84
|
* })
|
|
74
85
|
* ```
|
|
75
86
|
*/
|
|
76
|
-
export declare function createD1PrewarmClaimStore(db: PrewarmClaimD1Like, options?: D1PrewarmClaimStoreOptions):
|
|
77
|
-
acquire(key: string, ttlSeconds: number): Promise<boolean>;
|
|
78
|
-
release(key: string): Promise<void>;
|
|
79
|
-
isHeld(key: string): Promise<boolean>;
|
|
80
|
-
};
|
|
87
|
+
export declare function createD1PrewarmClaimStore(db: PrewarmClaimD1Like, options?: D1PrewarmClaimStoreOptions): FencedPrewarmClaimStore;
|
|
@@ -103,6 +103,47 @@ export interface PrewarmClaimStore {
|
|
|
103
103
|
* that started it, and every other one reports `absent`. */
|
|
104
104
|
isHeld?(key: string): Promise<boolean>;
|
|
105
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* The ownership proof for one foreground provisioning attempt.
|
|
108
|
+
*
|
|
109
|
+
* `expiresAt` is both the lease deadline and its fencing value. The D1 store
|
|
110
|
+
* advances it when a takeover succeeds, so an expired owner cannot release a
|
|
111
|
+
* successor's lease. Keep this object and pass it back unchanged to
|
|
112
|
+
* `FencedPrewarmClaimStore.releaseLease`.
|
|
113
|
+
*/
|
|
114
|
+
export interface PrewarmClaimLease {
|
|
115
|
+
readonly key: string;
|
|
116
|
+
readonly expiresAt: number;
|
|
117
|
+
}
|
|
118
|
+
/** State needed by a foreground caller that waits for another isolate. */
|
|
119
|
+
export type PrewarmClaimState = {
|
|
120
|
+
status: 'held';
|
|
121
|
+
expiresAt: number;
|
|
122
|
+
} | {
|
|
123
|
+
status: 'expired';
|
|
124
|
+
expiresAt: number;
|
|
125
|
+
} | {
|
|
126
|
+
status: 'absent';
|
|
127
|
+
};
|
|
128
|
+
/** A claim store that can distinguish a completed owner from a crashed owner. */
|
|
129
|
+
export interface InspectablePrewarmClaimStore extends PrewarmClaimStore {
|
|
130
|
+
isHeld(key: string): Promise<boolean>;
|
|
131
|
+
inspect(key: string): Promise<PrewarmClaimState>;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Fenced claim operations for foreground provisioning.
|
|
135
|
+
*
|
|
136
|
+
* This is separate from `PrewarmClaimStore` so the published boolean
|
|
137
|
+
* `acquire` and key-only `release` methods remain source-compatible. A
|
|
138
|
+
* foreground caller must use the lease returned here and release that lease,
|
|
139
|
+
* not the legacy key-only method.
|
|
140
|
+
*/
|
|
141
|
+
export interface FencedPrewarmClaimStore extends InspectablePrewarmClaimStore {
|
|
142
|
+
/** Atomically acquire a lease or return null when another live lease wins. */
|
|
143
|
+
acquireLease(key: string, ttlSeconds: number): Promise<PrewarmClaimLease | null>;
|
|
144
|
+
/** Conditionally release only this lease. A stale lease is a no-op. */
|
|
145
|
+
releaseLease(lease: PrewarmClaimLease): Promise<void>;
|
|
146
|
+
}
|
|
106
147
|
/** What `prewarm()` decided. Every value except `started` means no box was
|
|
107
148
|
* created and nothing was spent on this call. */
|
|
108
149
|
export type PrewarmOutcome = 'started' | 'already-running' | 'already-warming' | 'warming-elsewhere' | 'declined-by-policy' | 'cooling-down' | 'absent-and-resume-only';
|
|
@@ -190,4 +231,7 @@ export interface SandboxPrewarmer {
|
|
|
190
231
|
/** Clear a recorded failure so the next `prewarm` retries immediately. */
|
|
191
232
|
clearFailure(scope: SandboxPrewarmScope): void;
|
|
192
233
|
}
|
|
234
|
+
/** Identity of a warm. Harness is part of it because `ensureWorkspaceSandbox`
|
|
235
|
+
* destroys and rebuilds a box whose harness does not match. */
|
|
236
|
+
export declare function sandboxPrewarmClaimKey(scope: Pick<SandboxPrewarmScope, 'workspaceId' | 'harness'>): string;
|
|
193
237
|
export declare function createSandboxPrewarmer(shell: SandboxRuntimeConfig, options: SandboxPrewarmerOptions): SandboxPrewarmer;
|
package/dist/signoff/cli.js
CHANGED
package/dist/signoff/index.js
CHANGED
|
@@ -11,12 +11,13 @@
|
|
|
11
11
|
* At measurement time, none of tax-agent, legal-agent, or agent-app had an
|
|
12
12
|
* `.nvmrc`.
|
|
13
13
|
* All three pinned Node 22 in the merge workflow.
|
|
14
|
-
* Agent App now
|
|
14
|
+
* Agent App now pins Node 24.18.0 in `.nvmrc`.
|
|
15
15
|
*
|
|
16
16
|
* The workflow fallback reads only `pull_request` checks because those checks
|
|
17
17
|
* can block a merge.
|
|
18
18
|
* Agent App uses `.nvmrc` instead of this fallback.
|
|
19
|
-
* Its publish workflow uses Node
|
|
19
|
+
* Its publish workflow uses Node 24.18.0 for source checks and npm, with a
|
|
20
|
+
* separate clean-runner Node 22 compatibility job.
|
|
20
21
|
*
|
|
21
22
|
* Nothing here guesses. An expression (`${{ matrix.node }}`) is not a version,
|
|
22
23
|
* a `node-version-file` that does not exist is not a pin, and two merge-gate
|
package/dist/trace/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
export * from './mission-trace';
|
|
17
17
|
export * from './flow-types';
|
|
18
18
|
export * from './mission-flow';
|
|
19
|
+
export * from './stage-timing';
|
|
19
20
|
import type { FlowTrace } from './flow-types';
|
|
20
21
|
/** Represent a timed event with a timestamp and associated event data */
|
|
21
22
|
export interface TimedEvent {
|
package/dist/trace/index.js
CHANGED
|
@@ -46,6 +46,242 @@ function randomHex(byteLength) {
|
|
|
46
46
|
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
// src/trace/stage-timing.ts
|
|
50
|
+
var STAGE_TIMING_EVENT = "stage_timing";
|
|
51
|
+
var STAGE_TIMING_VERSION = 1;
|
|
52
|
+
var MAX_STAGE_LENGTH = 80;
|
|
53
|
+
var MAX_IDENTIFIER_LENGTH = 128;
|
|
54
|
+
var MAX_DETAIL_KEYS = 8;
|
|
55
|
+
var MAX_DETAIL_KEY_LENGTH = 64;
|
|
56
|
+
var MAX_DETAIL_STRING_LENGTH = 64;
|
|
57
|
+
var SENSITIVE_KEY = /(?:api|auth|access|refresh|session|private|secret|password|credential|cookie|bearer|token|ssn|ein|email|phone)[_-]?(?:key|token|secret)?/iu;
|
|
58
|
+
var CREDENTIAL_VALUE = /\b(?:bearer|basic|digest|token)\s+\S+|\bapi[-_ ]?key\s*[:=]\s*\S+/iu;
|
|
59
|
+
function boundedIdentifier(value) {
|
|
60
|
+
if (typeof value !== "string") return void 0;
|
|
61
|
+
const trimmed = value.trim();
|
|
62
|
+
return trimmed ? trimmed.slice(0, MAX_IDENTIFIER_LENGTH) : void 0;
|
|
63
|
+
}
|
|
64
|
+
function safeProperty(value, key) {
|
|
65
|
+
try {
|
|
66
|
+
return Reflect.get(value, key);
|
|
67
|
+
} catch {
|
|
68
|
+
return void 0;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function isObject(value) {
|
|
72
|
+
return value !== null && typeof value === "object";
|
|
73
|
+
}
|
|
74
|
+
function sanitizeDetail(detail) {
|
|
75
|
+
try {
|
|
76
|
+
if (!isObject(detail) || Array.isArray(detail)) return void 0;
|
|
77
|
+
const entries = [];
|
|
78
|
+
for (const [rawKey, value] of Object.entries(detail)) {
|
|
79
|
+
if (entries.length >= MAX_DETAIL_KEYS) break;
|
|
80
|
+
const normalizedKey = rawKey.trim();
|
|
81
|
+
if (!normalizedKey || SENSITIVE_KEY.test(normalizedKey) || value === void 0) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const key = normalizedKey.slice(0, MAX_DETAIL_KEY_LENGTH);
|
|
85
|
+
if (typeof value === "string") {
|
|
86
|
+
if (CREDENTIAL_VALUE.test(value)) continue;
|
|
87
|
+
entries.push([key, value.slice(0, MAX_DETAIL_STRING_LENGTH)]);
|
|
88
|
+
} else if (typeof value === "number") {
|
|
89
|
+
entries.push([key, Number.isFinite(value) ? value : null]);
|
|
90
|
+
} else if (typeof value === "boolean" || value === null) {
|
|
91
|
+
entries.push([key, value]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return entries.length > 0 ? Object.fromEntries(entries) : void 0;
|
|
95
|
+
} catch {
|
|
96
|
+
return void 0;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function isStageTimingKind(value) {
|
|
100
|
+
return value === "leaf" || value === "span";
|
|
101
|
+
}
|
|
102
|
+
function isStageTimingOutcome(value) {
|
|
103
|
+
return value === "ok" || value === "error" || value === "timeout" || value === "skipped";
|
|
104
|
+
}
|
|
105
|
+
function buildStageTimingRecord(context, stage, startedAt, durationMs, input = {}) {
|
|
106
|
+
try {
|
|
107
|
+
const name = typeof stage === "string" ? stage.trim() : "";
|
|
108
|
+
if (!name || !Number.isFinite(startedAt) || startedAt < 0 || !Number.isFinite(durationMs) || durationMs < 0 || !isObject(context) || !isObject(input)) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const rawKind = safeProperty(input, "kind");
|
|
112
|
+
const rawOutcome = safeProperty(input, "outcome");
|
|
113
|
+
const kind = rawKind === void 0 ? "leaf" : rawKind;
|
|
114
|
+
const outcome = rawOutcome === void 0 ? "ok" : rawOutcome;
|
|
115
|
+
if (!isStageTimingKind(kind) || !isStageTimingOutcome(outcome)) return null;
|
|
116
|
+
const runId = boundedIdentifier(safeProperty(context, "runId"));
|
|
117
|
+
if (!runId) return null;
|
|
118
|
+
const record = {
|
|
119
|
+
evt: STAGE_TIMING_EVENT,
|
|
120
|
+
v: STAGE_TIMING_VERSION,
|
|
121
|
+
stage: name.slice(0, MAX_STAGE_LENGTH),
|
|
122
|
+
kind,
|
|
123
|
+
startedAt: Math.round(startedAt),
|
|
124
|
+
durationMs: Math.round(durationMs),
|
|
125
|
+
outcome,
|
|
126
|
+
runId
|
|
127
|
+
};
|
|
128
|
+
for (const key of [
|
|
129
|
+
"workspaceId",
|
|
130
|
+
"threadId",
|
|
131
|
+
"sandboxId",
|
|
132
|
+
"model",
|
|
133
|
+
"harness",
|
|
134
|
+
"path"
|
|
135
|
+
]) {
|
|
136
|
+
const value = boundedIdentifier(safeProperty(context, key));
|
|
137
|
+
if (value) record[key] = value;
|
|
138
|
+
}
|
|
139
|
+
const attempt = safeProperty(input, "attempt");
|
|
140
|
+
if (typeof attempt === "number" && Number.isSafeInteger(attempt) && attempt >= 0) {
|
|
141
|
+
record.attempt = attempt;
|
|
142
|
+
}
|
|
143
|
+
const detail = sanitizeDetail(safeProperty(input, "detail"));
|
|
144
|
+
if (detail) record.detail = detail;
|
|
145
|
+
return record;
|
|
146
|
+
} catch {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function safePrimitiveText(value) {
|
|
151
|
+
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean" && typeof value !== "bigint" && typeof value !== "symbol") {
|
|
152
|
+
return "";
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
return String(value);
|
|
156
|
+
} catch {
|
|
157
|
+
return "";
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function outcomeForError(error) {
|
|
161
|
+
try {
|
|
162
|
+
const objectLike = isObject(error) || typeof error === "function";
|
|
163
|
+
const code = objectLike ? safePrimitiveText(safeProperty(error, "code")) : "";
|
|
164
|
+
const message = objectLike ? safePrimitiveText(safeProperty(error, "message")) : safePrimitiveText(error);
|
|
165
|
+
return /timeout|timed ?out|deadline/iu.test(`${code} ${message}`) ? "timeout" : "error";
|
|
166
|
+
} catch {
|
|
167
|
+
return "error";
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function safeClock(now) {
|
|
171
|
+
try {
|
|
172
|
+
const value = now();
|
|
173
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
174
|
+
} catch {
|
|
175
|
+
return void 0;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function safeObject(value) {
|
|
179
|
+
try {
|
|
180
|
+
if (!isObject(value) || Array.isArray(value)) return {};
|
|
181
|
+
return { ...value };
|
|
182
|
+
} catch {
|
|
183
|
+
return {};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function mergeInputs(base, extra) {
|
|
187
|
+
const baseObject = safeObject(base);
|
|
188
|
+
const extraObject = safeObject(extra);
|
|
189
|
+
const detail = {
|
|
190
|
+
...safeObject(baseObject.detail),
|
|
191
|
+
...safeObject(extraObject.detail)
|
|
192
|
+
};
|
|
193
|
+
return {
|
|
194
|
+
...baseObject,
|
|
195
|
+
...extraObject,
|
|
196
|
+
detail
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function withOutcome(extra, outcome) {
|
|
200
|
+
return { ...safeObject(extra), outcome };
|
|
201
|
+
}
|
|
202
|
+
function createStageTiming(options) {
|
|
203
|
+
const now = options.now ?? (() => Date.now());
|
|
204
|
+
const generatedRunId = () => {
|
|
205
|
+
try {
|
|
206
|
+
const value = (options.createRunId ?? (() => crypto.randomUUID()))();
|
|
207
|
+
return boundedIdentifier(value) ?? crypto.randomUUID();
|
|
208
|
+
} catch {
|
|
209
|
+
try {
|
|
210
|
+
return crypto.randomUUID();
|
|
211
|
+
} catch {
|
|
212
|
+
return "run-unknown";
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
const context = {
|
|
217
|
+
...options.context,
|
|
218
|
+
runId: boundedIdentifier(options.context?.runId) ?? generatedRunId()
|
|
219
|
+
};
|
|
220
|
+
const emit = (stage, startedAt, durationMs, input = {}) => {
|
|
221
|
+
try {
|
|
222
|
+
const record = buildStageTimingRecord(context, stage, startedAt, durationMs, input);
|
|
223
|
+
if (!record) return;
|
|
224
|
+
const result = options.emit(record);
|
|
225
|
+
if (result && typeof result.then === "function") {
|
|
226
|
+
void Promise.resolve(result).catch(() => void 0);
|
|
227
|
+
}
|
|
228
|
+
} catch {
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
const start = (stage, input = {}) => {
|
|
232
|
+
const observedStart = safeClock(now);
|
|
233
|
+
const startedAt = observedStart ?? 0;
|
|
234
|
+
let emitted = false;
|
|
235
|
+
const finish = (extra = {}) => {
|
|
236
|
+
if (emitted) return;
|
|
237
|
+
emitted = true;
|
|
238
|
+
if (observedStart === void 0) return;
|
|
239
|
+
const observedEnd = safeClock(now);
|
|
240
|
+
if (observedEnd === void 0) return;
|
|
241
|
+
try {
|
|
242
|
+
emit(stage, observedStart, observedEnd - observedStart, mergeInputs(input, extra));
|
|
243
|
+
} catch {
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
return {
|
|
247
|
+
startedAt,
|
|
248
|
+
done: finish,
|
|
249
|
+
fail(error, extra = {}) {
|
|
250
|
+
let outcome = "error";
|
|
251
|
+
try {
|
|
252
|
+
outcome = outcomeForError(error);
|
|
253
|
+
} catch {
|
|
254
|
+
}
|
|
255
|
+
finish(withOutcome(extra, outcome));
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
return {
|
|
260
|
+
context,
|
|
261
|
+
setContext(input) {
|
|
262
|
+
try {
|
|
263
|
+
Object.assign(context, input);
|
|
264
|
+
} catch {
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
recordDuration(stage, startedAt, durationMs, input = {}) {
|
|
268
|
+
emit(stage, startedAt, durationMs, input);
|
|
269
|
+
},
|
|
270
|
+
start,
|
|
271
|
+
async measure(stage, input, operation) {
|
|
272
|
+
const handle = start(stage, input);
|
|
273
|
+
try {
|
|
274
|
+
const value = await operation();
|
|
275
|
+
handle.done();
|
|
276
|
+
return value;
|
|
277
|
+
} catch (error) {
|
|
278
|
+
handle.fail(error);
|
|
279
|
+
throw error;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
49
285
|
// src/trace/index.ts
|
|
50
286
|
function timedEventsFromLines(lines) {
|
|
51
287
|
const out = [];
|
|
@@ -184,10 +420,14 @@ function renderHistogram(values, opts) {
|
|
|
184
420
|
return lines.join("\n");
|
|
185
421
|
}
|
|
186
422
|
export {
|
|
423
|
+
STAGE_TIMING_EVENT,
|
|
424
|
+
STAGE_TIMING_VERSION,
|
|
187
425
|
buildFlowTrace,
|
|
426
|
+
buildStageTimingRecord,
|
|
188
427
|
childSpanContext,
|
|
189
428
|
composeMissionFlowTrace,
|
|
190
429
|
createMissionTraceContext,
|
|
430
|
+
createStageTiming,
|
|
191
431
|
delegationActivityToFlowSpans,
|
|
192
432
|
loopTraceEventsToFlowSpans,
|
|
193
433
|
renderHistogram,
|