@halonicjs/testing 0.1.0 → 0.1.1
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/NOTICE +4 -0
- package/README.md +1 -1
- package/dist/adapters/http-kit/src/address-checking.d.ts +19 -0
- package/dist/adapters/http-kit/src/address-checking.js +1 -0
- package/dist/adapters/http-kit/src/index.d.ts +1 -0
- package/dist/adapters/integration-kit/src/auth-placement.d.ts +40 -0
- package/dist/adapters/integration-kit/src/auth-placement.js +102 -0
- package/dist/adapters/integration-kit/src/current-secret-forms.d.ts +28 -0
- package/dist/adapters/integration-kit/src/current-secret-forms.js +60 -0
- package/dist/adapters/integration-kit/src/declaration.d.ts +2 -1
- package/dist/adapters/integration-kit/src/declaration.js +4 -3
- package/dist/adapters/integration-kit/src/host.d.ts +7 -1
- package/dist/adapters/integration-kit/src/host.js +1 -0
- package/dist/adapters/integration-kit/src/index.d.ts +3 -1
- package/dist/adapters/integration-kit/src/index.js +1 -0
- package/dist/adapters/integration-kit/src/leak-attempt.d.ts +4 -2
- package/dist/adapters/integration-kit/src/leak-attempt.js +5 -2
- package/dist/adapters/integration-kit/src/limited-http.d.ts +7 -1
- package/dist/adapters/integration-kit/src/limited-http.js +53 -10
- package/dist/adapters/integration-kit/src/loader.d.ts +29 -2
- package/dist/adapters/integration-kit/src/loader.js +44 -12
- package/dist/adapters/integration-kit/src/own-request.d.ts +2 -2
- package/dist/adapters/integration-kit/src/own-request.js +1 -1
- package/dist/adapters/integration-kit/src/plugin-load.d.ts +3 -3
- package/dist/adapters/integration-kit/src/plugin-load.js +5 -5
- package/dist/adapters/integration-kit/src/policy.d.ts +28 -0
- package/dist/adapters/integration-kit/src/response-body.d.ts +6 -2
- package/dist/adapters/integration-kit/src/secret-filter.d.ts +3 -3
- package/dist/adapters/integration-kit/src/secret-filter.js +19 -4
- package/dist/adapters/integration-kit/src/secret-in-request.d.ts +3 -0
- package/dist/adapters/integration-kit/src/secret-in-request.js +8 -2
- package/dist/adapters/integration-kit/src/secret-placement.d.ts +35 -25
- package/dist/adapters/integration-kit/src/secret-placement.js +65 -95
- package/dist/adapters/integration-kit/src/sign-in-placement.d.ts +29 -0
- package/dist/adapters/integration-kit/src/sign-in-placement.js +92 -0
- package/dist/core/events/src/blocked-sites.d.ts +57 -0
- package/dist/core/events/src/blocked-sites.js +89 -0
- package/dist/core/events/src/index.d.ts +5 -1
- package/dist/core/events/src/index.js +3 -0
- package/dist/core/events/src/run-basics.d.ts +30 -7
- package/dist/core/events/src/run-event.d.ts +49 -5
- package/dist/core/events/src/run-reads.d.ts +25 -0
- package/dist/core/events/src/run-reads.js +44 -0
- package/dist/core/events/src/run-sends.d.ts +47 -0
- package/dist/core/events/src/run-sends.js +68 -0
- package/dist/core/events/src/summarize.js +5 -1
- package/dist/core/events/src/timeline-entry.js +1 -0
- package/dist/core/format/src/index.d.ts +7 -5
- package/dist/core/format/src/index.js +5 -3
- package/dist/core/format/src/integration/index.d.ts +3 -2
- package/dist/core/format/src/integration/index.js +2 -1
- package/dist/core/format/src/kinds/agent/sandbox-fields.d.ts +15 -5
- package/dist/core/format/src/kinds/agent/sandbox-fields.js +29 -10
- package/dist/core/format/src/kinds/agent/schema.js +2 -2
- package/dist/core/format/src/kinds/connector/is-local.js +1 -1
- package/dist/core/format/src/kinds/connector/kind.js +99 -0
- package/dist/core/format/src/kinds/connector/schema.d.ts +24 -0
- package/dist/core/format/src/kinds/connector/schema.js +53 -2
- package/dist/core/format/src/kinds/deployments/kind.js +25 -1
- package/dist/core/format/src/kinds/deployments/schema.d.ts +40 -0
- package/dist/core/format/src/kinds/deployments/schema.js +52 -0
- package/dist/core/format/src/kinds/integration/approved-sign-ins.d.ts +62 -0
- package/dist/core/format/src/kinds/integration/approved-sign-ins.js +125 -0
- package/dist/core/format/src/kinds/integration/host-key.d.ts +7 -0
- package/dist/core/format/src/kinds/integration/host-key.js +8 -0
- package/dist/core/format/src/kinds/integration/kind.d.ts +4 -2
- package/dist/core/format/src/kinds/integration/kind.js +49 -12
- package/dist/core/format/src/kinds/integration/policy.d.ts +47 -2
- package/dist/core/format/src/kinds/integration/policy.js +61 -2
- package/dist/core/format/src/kinds/integration/schema.d.ts +180 -3
- package/dist/core/format/src/kinds/integration/schema.js +132 -6
- package/dist/core/format/src/kinds/integration/sign-in-checks.d.ts +13 -0
- package/dist/core/format/src/kinds/integration/sign-in-checks.js +120 -0
- package/dist/core/format/src/kinds/integration/sign-in-urls.d.ts +19 -0
- package/dist/core/format/src/kinds/integration/sign-in-urls.js +47 -0
- package/dist/core/format/src/kinds/plugin/checks.js +7 -1
- package/dist/core/format/src/kinds/plugin/schema.d.ts +130 -0
- package/dist/core/format/src/kinds/plugin/schema.js +2 -2
- package/dist/core/format/src/kinds/plugin/ui-checks.d.ts +1 -1
- package/dist/core/format/src/kinds/plugin/ui-checks.js +1 -10
- package/dist/core/format/src/kinds/routine/kind.js +38 -1
- package/dist/core/format/src/kinds/routine/schema.d.ts +10 -0
- package/dist/core/format/src/kinds/routine/schema.js +23 -1
- package/dist/core/format/src/kinds/routine/webhook-headers.d.ts +9 -0
- package/dist/core/format/src/kinds/routine/webhook-headers.js +26 -0
- package/dist/core/format/src/kinds/workflow/schema.js +1 -1
- package/dist/core/format/src/kinds/workspace/kind.js +1 -1
- package/dist/core/format/src/kinds/workspace/schema.d.ts +2 -2
- package/dist/core/format/src/kinds/workspace/schema.js +1 -1
- package/dist/core/format/src/shared/fields.d.ts +3 -6
- package/dist/core/format/src/shared/fields.js +33 -13
- package/dist/core/ports/src/agent-runs-not-available.d.ts +21 -0
- package/dist/core/ports/src/agent-runs-not-available.js +25 -0
- package/dist/core/ports/src/blob-store.d.ts +12 -0
- package/dist/core/ports/src/blob-store.js +22 -0
- package/dist/core/ports/src/connector-client.d.ts +48 -4
- package/dist/core/ports/src/connector-client.js +17 -0
- package/dist/core/ports/src/definition-store.d.ts +23 -2
- package/dist/core/ports/src/harness-bridge-server.d.ts +12 -3
- package/dist/core/ports/src/harness-sign-in-command.d.ts +27 -0
- package/dist/core/ports/src/harness-sign-in-command.js +31 -0
- package/dist/core/ports/src/harness.d.ts +46 -13
- package/dist/core/ports/src/harness.js +1 -1
- package/dist/core/ports/src/host-settings-store.d.ts +116 -0
- package/dist/core/ports/src/host-settings-store.js +106 -0
- package/dist/core/ports/src/index.d.ts +18 -10
- package/dist/core/ports/src/index.js +15 -7
- package/dist/core/ports/src/instruction-trust-store.d.ts +81 -0
- package/dist/core/ports/src/instruction-trust-store.js +43 -0
- package/dist/core/ports/src/integration.d.ts +71 -7
- package/dist/core/ports/src/integration.js +22 -4
- package/dist/core/ports/src/llm-provider.d.ts +3 -1
- package/dist/core/ports/src/llm-provider.js +3 -0
- package/dist/core/ports/src/password-hasher.d.ts +36 -20
- package/dist/core/ports/src/port-versions.d.ts +7 -4
- package/dist/core/ports/src/port-versions.js +6 -0
- package/dist/core/ports/src/program-probe.d.ts +25 -2
- package/dist/core/ports/src/program-probe.js +1 -1
- package/dist/core/ports/src/provider-default-base-url.d.ts +10 -0
- package/dist/core/ports/src/provider-default-base-url.js +18 -0
- package/dist/core/ports/src/record-store.d.ts +17 -0
- package/dist/core/ports/src/record-store.js +70 -0
- package/dist/core/ports/src/run-executor.d.ts +5 -4
- package/dist/core/ports/src/run-target-not-found.d.ts +34 -0
- package/dist/core/ports/src/run-target-not-found.js +41 -0
- package/dist/core/ports/src/sandbox-setting-store.d.ts +68 -0
- package/dist/core/ports/src/sandbox-setting-store.js +33 -0
- package/dist/core/ports/src/sandbox.d.ts +130 -13
- package/dist/core/ports/src/sandbox.js +38 -2
- package/dist/core/ports/src/secrets.d.ts +73 -0
- package/dist/core/ports/src/secrets.js +56 -0
- package/dist/core/ports/src/tool-approval-store.d.ts +50 -8
- package/dist/core/ports/src/tool-approval-store.js +59 -0
- package/dist/core/ports/src/vcs-status.d.ts +8 -0
- package/dist/core/ports/src/vcs-status.js +90 -0
- package/dist/core/std/src/amount.d.ts +13 -0
- package/dist/core/std/src/amount.js +22 -0
- package/dist/core/std/src/hidden-characters.d.ts +19 -0
- package/dist/core/std/src/hidden-characters.js +54 -0
- package/dist/core/std/src/index.d.ts +5 -0
- package/dist/core/std/src/index.js +5 -0
- package/dist/core/std/src/public-suffixes.generated.d.ts +9 -0
- package/dist/core/std/src/public-suffixes.generated.js +10429 -0
- package/dist/core/std/src/registrable-domain.d.ts +10 -0
- package/dist/core/std/src/registrable-domain.js +62 -0
- package/dist/core/std/src/secret-variable-name.d.ts +12 -0
- package/dist/core/std/src/secret-variable-name.js +34 -0
- package/dist/core/std/src/site-name.d.ts +35 -0
- package/dist/core/std/src/site-name.js +111 -0
- package/dist/core/std/src/url-host.d.ts +35 -0
- package/dist/core/std/src/url-host.js +170 -0
- package/dist/testing/integration/src/load.js +3 -1
- package/package.json +2 -2
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type SignInPlacement, type SignInTokens } from "../../../core/ports/src/index.js";
|
|
2
|
+
import { type IntegrationPolicy } from "./policy.js";
|
|
3
|
+
/**
|
|
4
|
+
* Checks that the sign-in is declared and that the code places its token the
|
|
5
|
+
* way the sheet's `access` block says, with the same check as a secret's
|
|
6
|
+
* placement (`placement-not-in-sheet`). Runs before any token is asked for.
|
|
7
|
+
*/
|
|
8
|
+
export declare function checkSignInPlacement(policy: IntegrationPolicy, placement: SignInPlacement): void;
|
|
9
|
+
/**
|
|
10
|
+
* Checks that the token may go to the request's first host. Unlike a
|
|
11
|
+
* secret, a token never goes over plain `http`, not even to this machine.
|
|
12
|
+
*/
|
|
13
|
+
export declare function checkSignInPlacementHost(policy: IntegrationPolicy, placement: SignInPlacement, url: URL): void;
|
|
14
|
+
/**
|
|
15
|
+
* The access tokens this hop will carry, one per sign-in whose token may go
|
|
16
|
+
* to the hop's host. A sign-in that may not go there is not asked for, so a
|
|
17
|
+
* redirect to another declared host neither refreshes nor places its token.
|
|
18
|
+
*
|
|
19
|
+
* @throws {IntegrationRefusedError} `sign-in-needed` when the user has to sign in (again), or when no sign-ins are set up at all
|
|
20
|
+
*/
|
|
21
|
+
export declare function readTokens(policy: IntegrationPolicy, tokens: SignInTokens | undefined, placements: readonly SignInPlacement[], url: URL, signal: AbortSignal): Promise<Map<string, string>>;
|
|
22
|
+
/** Puts the token into the headers, when this hop was asked for it. */
|
|
23
|
+
export declare function placeToken(placement: SignInPlacement, value: string | undefined, headers: Headers): void;
|
|
24
|
+
/**
|
|
25
|
+
* Tells the sign-in the server answered 401 to the tokens this hop placed.
|
|
26
|
+
* It counts a value only while it is the sign-in's current token and decides
|
|
27
|
+
* about refreshing; the kit never resends the request.
|
|
28
|
+
*/
|
|
29
|
+
export declare function reportRejected(tokens: SignInTokens | undefined, placed: ReadonlyMap<string, string>): void;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { SignInNeededError } from "../../../core/ports/src/index.js";
|
|
2
|
+
import { IntegrationRefusedError } from "@halonicjs/adapters/integration-kit";
|
|
3
|
+
import { hostIn } from "./policy.js";
|
|
4
|
+
import { checkSecretLength, describe, matchesSheet, quoted } from "./secret-placement.js";
|
|
5
|
+
/** The sheet's entry for a sign-in, looked up by own key only, so `toString` is never a sign-in. */
|
|
6
|
+
function declaredSignIn(policy, signIn) {
|
|
7
|
+
return Object.hasOwn(policy.signIns, signIn) ? policy.signIns[signIn] : undefined;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Checks that the sign-in is declared and that the code places its token the
|
|
11
|
+
* way the sheet's `access` block says, with the same check as a secret's
|
|
12
|
+
* placement (`placement-not-in-sheet`). Runs before any token is asked for.
|
|
13
|
+
*/
|
|
14
|
+
export function checkSignInPlacement(policy, placement) {
|
|
15
|
+
const declared = declaredSignIn(policy, placement.signIn);
|
|
16
|
+
if (declared === undefined) {
|
|
17
|
+
throw new IntegrationRefusedError("sign-in-not-declared", `${policy.name}: the sign-in ${quoted(placement.signIn)} is not in its permission sheet`);
|
|
18
|
+
}
|
|
19
|
+
if (!matchesSheet(placement, declared)) {
|
|
20
|
+
throw new IntegrationRefusedError("placement-not-in-sheet", `${policy.name}: the token of the sign-in ${quoted(placement.signIn)} goes ${describe(declared)} in its permission sheet, not ${describe(placement)}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Checks that the token may go to the request's first host. Unlike a
|
|
25
|
+
* secret, a token never goes over plain `http`, not even to this machine.
|
|
26
|
+
*/
|
|
27
|
+
export function checkSignInPlacementHost(policy, placement, url) {
|
|
28
|
+
const declared = declaredSignIn(policy, placement.signIn);
|
|
29
|
+
if (declared === undefined || !hostIn(declared.hosts, url)) {
|
|
30
|
+
throw new IntegrationRefusedError("token-not-allowed-for-host", `${policy.name}: the token of the sign-in ${quoted(placement.signIn)} may not go to ${url.host}`);
|
|
31
|
+
}
|
|
32
|
+
if (!mayCarryToken(url)) {
|
|
33
|
+
throw new IntegrationRefusedError("token-over-plain-http", `${policy.name}: the token of the sign-in ${quoted(placement.signIn)} may not go over plain http (${url.host})`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** A token goes over `https` only. */
|
|
37
|
+
function mayCarryToken(url) {
|
|
38
|
+
return url.protocol === "https:";
|
|
39
|
+
}
|
|
40
|
+
/** Whether this hop may carry the token of the sign-in. */
|
|
41
|
+
function mayGoTo(policy, signIn, url) {
|
|
42
|
+
const declared = declaredSignIn(policy, signIn);
|
|
43
|
+
return declared !== undefined && hostIn(declared.hosts, url) && mayCarryToken(url);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The access tokens this hop will carry, one per sign-in whose token may go
|
|
47
|
+
* to the hop's host. A sign-in that may not go there is not asked for, so a
|
|
48
|
+
* redirect to another declared host neither refreshes nor places its token.
|
|
49
|
+
*
|
|
50
|
+
* @throws {IntegrationRefusedError} `sign-in-needed` when the user has to sign in (again), or when no sign-ins are set up at all
|
|
51
|
+
*/
|
|
52
|
+
export async function readTokens(policy, tokens, placements, url, signal) {
|
|
53
|
+
const values = new Map();
|
|
54
|
+
for (const { signIn } of placements) {
|
|
55
|
+
if (values.has(signIn) || !mayGoTo(policy, signIn, url))
|
|
56
|
+
continue;
|
|
57
|
+
const value = await accessToken(policy, tokens, signIn, signal);
|
|
58
|
+
checkSecretLength(policy.name, `sign-in ${signIn}`, value);
|
|
59
|
+
values.set(signIn, value);
|
|
60
|
+
}
|
|
61
|
+
return values;
|
|
62
|
+
}
|
|
63
|
+
async function accessToken(policy, tokens, signIn, signal) {
|
|
64
|
+
try {
|
|
65
|
+
if (tokens === undefined)
|
|
66
|
+
throw new SignInNeededError(signIn);
|
|
67
|
+
return await tokens.access(signIn, signal);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
if (!(error instanceof SignInNeededError))
|
|
71
|
+
throw error;
|
|
72
|
+
throw new IntegrationRefusedError("sign-in-needed", `${policy.name}: the sign-in ${quoted(signIn)} needs you to sign in again`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Puts the token into the headers, when this hop was asked for it. */
|
|
76
|
+
export function placeToken(placement, value, headers) {
|
|
77
|
+
if (value === undefined)
|
|
78
|
+
return;
|
|
79
|
+
if (placement.as === "bearer")
|
|
80
|
+
headers.set("authorization", `Bearer ${value}`);
|
|
81
|
+
else
|
|
82
|
+
headers.set(placement.header, `${placement.prefix ?? ""}${value}`);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Tells the sign-in the server answered 401 to the tokens this hop placed.
|
|
86
|
+
* It counts a value only while it is the sign-in's current token and decides
|
|
87
|
+
* about refreshing; the kit never resends the request.
|
|
88
|
+
*/
|
|
89
|
+
export function reportRejected(tokens, placed) {
|
|
90
|
+
for (const [signIn, value] of placed)
|
|
91
|
+
tokens?.rejected(signIn, value);
|
|
92
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { RunEvent } from "./run-event.js";
|
|
2
|
+
/** One site a step's sandbox refused, with how many times it was tried. */
|
|
3
|
+
export interface BlockedSite {
|
|
4
|
+
/** The host, `A`–`Z` lowercased and without a trailing dot. Show it with `siteName` from std. */
|
|
5
|
+
readonly host: string;
|
|
6
|
+
/** The refused connections to it in the step, all ports together. */
|
|
7
|
+
readonly tries: number;
|
|
8
|
+
}
|
|
9
|
+
/** The blocked sites of one visit of a step. */
|
|
10
|
+
export interface StepBlockedSites {
|
|
11
|
+
readonly state: string;
|
|
12
|
+
readonly agent: string;
|
|
13
|
+
/** Set for a step in one branch of a parallel state. */
|
|
14
|
+
readonly branch?: string;
|
|
15
|
+
/** Set for a step in one item of a for-each state. */
|
|
16
|
+
readonly item?: number;
|
|
17
|
+
/** The `seq` of the step's first blocked site: where the run log view shows the line. */
|
|
18
|
+
readonly firstSeq: number;
|
|
19
|
+
/** The sites to show, most tries first, then A to Z, at most the `limit` asked for. */
|
|
20
|
+
readonly sites: readonly BlockedSite[];
|
|
21
|
+
/** How many more sites were blocked than `sites` shows. */
|
|
22
|
+
readonly moreSites: number;
|
|
23
|
+
/** The tries of those further sites together. */
|
|
24
|
+
readonly moreTries: number;
|
|
25
|
+
/**
|
|
26
|
+
* Refused accesses past the targets the log names (`others`), files and
|
|
27
|
+
* sites together, which the log doesn't tell apart. 0 when there are none.
|
|
28
|
+
*/
|
|
29
|
+
readonly otherBlocks: number;
|
|
30
|
+
}
|
|
31
|
+
/** How many sites {@link blockedSites} lists per step. */
|
|
32
|
+
export interface BlockedSitesOptions {
|
|
33
|
+
/**
|
|
34
|
+
* 5 for a step's entry on the run page, 20 for the run log view
|
|
35
|
+
* (sandbox-screens 2.2). A whole number, 0 or more.
|
|
36
|
+
*/
|
|
37
|
+
readonly limit: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Names each site a step's sandbox refused once, with its tries
|
|
41
|
+
* (requirement `events.blocked-sites`). A step is one visit of a state: a
|
|
42
|
+
* new `state.entered` starts a new one. A `sandbox.blocked` event's `count`
|
|
43
|
+
* is a running total, so the latest one for a site and port counts, and
|
|
44
|
+
* the ports of one site add up. Blocked files aren't sites and are left
|
|
45
|
+
* out; a step with no blocked site isn't listed.
|
|
46
|
+
*
|
|
47
|
+
* @param events - one run's log, in `seq` order
|
|
48
|
+
* @param options - how many sites to list per step
|
|
49
|
+
* @returns the steps with blocked sites, in the order of their first one
|
|
50
|
+
* @throws RangeError when `limit` isn't a whole number, 0 or more
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* blockedSites(events, { limit: 5 });
|
|
54
|
+
* // => [{ state: "install", agent: "writer", firstSeq: 12, sites: [{ host: "registry.npmjs.org", tries: 12 }],
|
|
55
|
+
* // moreSites: 0, moreTries: 0, otherBlocks: 0 }]
|
|
56
|
+
*/
|
|
57
|
+
export declare function blockedSites(events: readonly RunEvent[], options: BlockedSitesOptions): StepBlockedSites[];
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// The sites each step's sandbox refused, worked out from a run's
|
|
2
|
+
// `sandbox.blocked` events, so the run page and the run log view can name
|
|
3
|
+
// each site once with its tries (HAL-1708), not once per retry. Data only:
|
|
4
|
+
// the words come from the message catalogue.
|
|
5
|
+
/**
|
|
6
|
+
* Names each site a step's sandbox refused once, with its tries
|
|
7
|
+
* (requirement `events.blocked-sites`). A step is one visit of a state: a
|
|
8
|
+
* new `state.entered` starts a new one. A `sandbox.blocked` event's `count`
|
|
9
|
+
* is a running total, so the latest one for a site and port counts, and
|
|
10
|
+
* the ports of one site add up. Blocked files aren't sites and are left
|
|
11
|
+
* out; a step with no blocked site isn't listed.
|
|
12
|
+
*
|
|
13
|
+
* @param events - one run's log, in `seq` order
|
|
14
|
+
* @param options - how many sites to list per step
|
|
15
|
+
* @returns the steps with blocked sites, in the order of their first one
|
|
16
|
+
* @throws RangeError when `limit` isn't a whole number, 0 or more
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* blockedSites(events, { limit: 5 });
|
|
20
|
+
* // => [{ state: "install", agent: "writer", firstSeq: 12, sites: [{ host: "registry.npmjs.org", tries: 12 }],
|
|
21
|
+
* // moreSites: 0, moreTries: 0, otherBlocks: 0 }]
|
|
22
|
+
*/
|
|
23
|
+
export function blockedSites(events, options) {
|
|
24
|
+
if (!Number.isInteger(options.limit) || options.limit < 0) {
|
|
25
|
+
throw new RangeError(`blockedSites: limit must be a whole number, 0 or more, not ${options.limit}`);
|
|
26
|
+
}
|
|
27
|
+
const visits = new Map();
|
|
28
|
+
const tallies = new Map();
|
|
29
|
+
const others = new Map();
|
|
30
|
+
for (const event of events) {
|
|
31
|
+
if (event.type === "state.entered")
|
|
32
|
+
visits.set(event.state, event.seq);
|
|
33
|
+
if (event.type !== "sandbox.blocked" || event.block.kind === "file")
|
|
34
|
+
continue;
|
|
35
|
+
const key = JSON.stringify([event.state, event.branch, event.item, visits.get(event.state)]);
|
|
36
|
+
if (event.block.kind === "others") {
|
|
37
|
+
others.set(key, event.count);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const tally = tallies.get(key) ?? newTally(event);
|
|
41
|
+
tallies.set(key, tally);
|
|
42
|
+
const host = plainHost(event.block.host);
|
|
43
|
+
tally.counts.set(JSON.stringify([host, event.block.port]), { host, count: event.count });
|
|
44
|
+
}
|
|
45
|
+
return [...tallies].map(([key, tally]) => summary(tally, others.get(key) ?? 0, options.limit));
|
|
46
|
+
}
|
|
47
|
+
function newTally(event) {
|
|
48
|
+
const step = {
|
|
49
|
+
state: event.state,
|
|
50
|
+
agent: event.agent,
|
|
51
|
+
...(event.branch === undefined ? {} : { branch: event.branch }),
|
|
52
|
+
...(event.item === undefined ? {} : { item: event.item }),
|
|
53
|
+
firstSeq: event.seq,
|
|
54
|
+
};
|
|
55
|
+
return { step, counts: new Map() };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* One host however the sandbox wrote it: `API.Example.com.` is
|
|
59
|
+
* `api.example.com`. Only `A`–`Z` are lowercased, as `siteName` does:
|
|
60
|
+
* Unicode lowercasing turns the Kelvin sign (U+212A) into a plain `k`, so a
|
|
61
|
+
* look-alike would pass for the real name.
|
|
62
|
+
*/
|
|
63
|
+
function plainHost(host) {
|
|
64
|
+
const lower = host.replace(/[A-Z]/g, (letter) => letter.toLowerCase());
|
|
65
|
+
return lower.endsWith(".") ? lower.slice(0, -1) : lower;
|
|
66
|
+
}
|
|
67
|
+
function summary(tally, otherBlocks, limit) {
|
|
68
|
+
const triesByHost = new Map();
|
|
69
|
+
for (const { host, count } of tally.counts.values()) {
|
|
70
|
+
triesByHost.set(host, (triesByHost.get(host) ?? 0) + count);
|
|
71
|
+
}
|
|
72
|
+
const sites = [...triesByHost].map(([host, tries]) => ({ host, tries })).sort(mostTriesFirst);
|
|
73
|
+
const more = sites.slice(limit);
|
|
74
|
+
return {
|
|
75
|
+
...tally.step,
|
|
76
|
+
sites: sites.slice(0, limit),
|
|
77
|
+
moreSites: more.length,
|
|
78
|
+
moreTries: more.reduce((total, site) => total + site.tries, 0),
|
|
79
|
+
otherBlocks,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/** Most tries first, then A to Z by code point, the same on every computer. */
|
|
83
|
+
function mostTriesFirst(first, second) {
|
|
84
|
+
if (first.tries !== second.tries)
|
|
85
|
+
return second.tries - first.tries;
|
|
86
|
+
if (first.host === second.host)
|
|
87
|
+
return 0;
|
|
88
|
+
return first.host < second.host ? -1 : 1;
|
|
89
|
+
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export { addUsage } from "./add-usage.js";
|
|
2
2
|
export type { AgentResume, LoggedToolCall } from "./agent-resume.js";
|
|
3
|
-
export
|
|
3
|
+
export { type BlockedSite, type BlockedSitesOptions, blockedSites, type StepBlockedSites, } from "./blocked-sites.js";
|
|
4
|
+
export type { ApprovalResolver, CantTryReason, CantTryStep, LeftOutReason, ModelSkipReason, ProviderText, RunCatchUp, RunFailureReason, RunKind, RunParent, RunStatus, RunTrigger, RunVersion, StoredBlob, ToolNoun, ToolOutcome, ToolRefusalReason, ToolRisk, ToolUnaskedReason, ToolVerb, Usage, } from "./run-basics.js";
|
|
4
5
|
export type { RunEvent, RunEventBase, RunEventBody, RunEventType } from "./run-event.js";
|
|
6
|
+
export { draftsOf, readsOf } from "./run-reads.js";
|
|
7
|
+
export type { RunSends, SendCount, SendGroup } from "./run-sends.js";
|
|
8
|
+
export { sendsOf } from "./run-sends.js";
|
|
5
9
|
export type { PendingApproval, RunSummary, SentSignal } from "./run-summary.js";
|
|
6
10
|
export type { LoggedSandboxBlock, SandboxBlock } from "./sandbox-blocked.js";
|
|
7
11
|
export { maxBlockedTargetsPerStep, sandboxBlockedView } from "./sandbox-blocked.js";
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export { addUsage } from "./add-usage.js";
|
|
2
|
+
export { blockedSites, } from "./blocked-sites.js";
|
|
3
|
+
export { draftsOf, readsOf } from "./run-reads.js";
|
|
4
|
+
export { sendsOf } from "./run-sends.js";
|
|
2
5
|
export { maxBlockedTargetsPerStep, sandboxBlockedView } from "./sandbox-blocked.js";
|
|
3
6
|
export { deniedPathGroupCodes, sandboxOpenedView } from "./sandbox-opened.js";
|
|
4
7
|
export { readSandboxOpened } from "./sandbox-opened-read.js";
|
|
@@ -73,7 +73,7 @@ export interface Usage {
|
|
|
73
73
|
}
|
|
74
74
|
/** What started a run. Absent for runs started by hand from a workflow. */
|
|
75
75
|
export interface RunTrigger {
|
|
76
|
-
type: "manual" | "schedule" | "every" | "mail" | "signal";
|
|
76
|
+
type: "manual" | "schedule" | "every" | "mail" | "signal" | "webhook";
|
|
77
77
|
/** Routine key: "<hubId>/<routineId>" or "@workspace/<routineId>". */
|
|
78
78
|
routineId?: string;
|
|
79
79
|
/** ISO time the run was due (the scheduled time, not when it actually started). */
|
|
@@ -82,6 +82,8 @@ export interface RunTrigger {
|
|
|
82
82
|
messageId?: string;
|
|
83
83
|
/** Signal triggers: "<runId>:<seq>" of the signal that started this run (one run per signal). */
|
|
84
84
|
signalId?: string;
|
|
85
|
+
/** Webhook triggers: the sender's delivery id of the request that started this run (one run per delivery). */
|
|
86
|
+
deliveryId?: string;
|
|
85
87
|
/**
|
|
86
88
|
* Routine runs that started later than the tick's grace after `at`, such
|
|
87
89
|
* as after the computer slept or Halonic was closed. Absent for a run that
|
|
@@ -133,6 +135,12 @@ export type ToolOutcome = "ok" | "error" | "unknown";
|
|
|
133
135
|
* `events.tool-refused`): `not-offered`, the step offered no tool of that name.
|
|
134
136
|
*/
|
|
135
137
|
export type ToolRefusalReason = "not-offered";
|
|
138
|
+
/**
|
|
139
|
+
* Why a connector's tool call ran without asking a person (requirement
|
|
140
|
+
* `events.tool-unasked`): `read-list`, the `read` list of the connector's
|
|
141
|
+
* file names the tool.
|
|
142
|
+
*/
|
|
143
|
+
export type ToolUnaskedReason = "read-list";
|
|
136
144
|
/**
|
|
137
145
|
* Where a payload too large for the run log is kept: the hash of its bytes
|
|
138
146
|
* in the run's blob store, their size and their media type. The same shape
|
|
@@ -152,14 +160,23 @@ export interface StoredBlob {
|
|
|
152
160
|
*/
|
|
153
161
|
export type ToolRisk = "read" | "write" | "send";
|
|
154
162
|
/**
|
|
155
|
-
* What one call of a
|
|
156
|
-
* its integration's sheet names it, such as
|
|
157
|
-
* Both are stored so a reader never has to
|
|
163
|
+
* What one call of a tool sends, reads or drafts, in the singular and the
|
|
164
|
+
* plural, as its integration's sheet names it, such as
|
|
165
|
+
* `{ one: "email", many: "emails" }`. Both are stored so a reader never has to
|
|
166
|
+
* make a plural itself.
|
|
158
167
|
*/
|
|
159
168
|
export interface ToolNoun {
|
|
160
169
|
one: string;
|
|
161
170
|
many: string;
|
|
162
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* What one call of a tool did to its noun, in the past tense, as its
|
|
174
|
+
* integration's sheet says: "saved" reads "Saved 1 file". A closed list, so a
|
|
175
|
+
* sheet can't word a send as something milder; absent means "sent". `drafted`
|
|
176
|
+
* is for a `write` tool that prepares something without sending it ("Drafted
|
|
177
|
+
* 1 email"), and a `send` tool may never use it.
|
|
178
|
+
*/
|
|
179
|
+
export type ToolVerb = "sent" | "saved" | "created" | "updated" | "moved" | "drafted";
|
|
163
180
|
/**
|
|
164
181
|
* Why a run failed. Each reason has its own requirement in `requirements/`.
|
|
165
182
|
*
|
|
@@ -185,8 +202,10 @@ export interface ToolNoun {
|
|
|
185
202
|
* options.
|
|
186
203
|
* - `harness-unavailable`: the step's agent runs in a harness program, and the engine that ran it
|
|
187
204
|
* has no harnesses, as in workflow tests, which never start programs.
|
|
205
|
+
* - `local-only`: the workspace is local only, and no model the step could use runs on this
|
|
206
|
+
* computer, so Halonic called none of them.
|
|
188
207
|
*/
|
|
189
|
-
export type RunFailureReason = "budget" | "cancelled" | "model-missing" | "model-no-tools" | "error" | "model-unreachable" | "model-out-of-memory" | "disk-full" | "interrupted" | "sandbox-not-ready" | "sandbox-start-failed" | "harness-start-failed" | "key-refused" | "out-of-credit" | "provider-busy" | "answer-not-an-option" | "harness-unavailable";
|
|
208
|
+
export type RunFailureReason = "budget" | "cancelled" | "model-missing" | "model-no-tools" | "error" | "model-unreachable" | "model-out-of-memory" | "disk-full" | "interrupted" | "sandbox-not-ready" | "sandbox-start-failed" | "harness-start-failed" | "key-refused" | "out-of-credit" | "provider-busy" | "answer-not-an-option" | "harness-unavailable" | "local-only";
|
|
190
209
|
/**
|
|
191
210
|
* The model provider's own error text, kept in the run log for `--debug` and
|
|
192
211
|
* Copy details only (requirement `events.provider-text`). Studio and the
|
|
@@ -199,7 +218,11 @@ export interface ProviderText {
|
|
|
199
218
|
status?: number;
|
|
200
219
|
text: string;
|
|
201
220
|
}
|
|
202
|
-
/**
|
|
203
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Why a model was skipped before it was called: its provider's key isn't set
|
|
223
|
+
* (`no-key`), or the workspace is local only and the provider isn't local
|
|
224
|
+
* (`local-only`).
|
|
225
|
+
*/
|
|
226
|
+
export type ModelSkipReason = "no-key" | "local-only";
|
|
204
227
|
/** Who resolved an approval: a person, or a rule that answers without asking anyone. */
|
|
205
228
|
export type ApprovalResolver = "person" | "rule";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AgentResume } from "./agent-resume.js";
|
|
2
|
-
import type { ApprovalResolver, CantTryStep, ModelSkipReason, ProviderText, RunFailureReason, RunKind, RunParent, RunTrigger, RunVersion, StoredBlob, ToolNoun, ToolOutcome, ToolRefusalReason, ToolRisk, Usage } from "./run-basics.js";
|
|
2
|
+
import type { ApprovalResolver, CantTryStep, ModelSkipReason, ProviderText, RunFailureReason, RunKind, RunParent, RunTrigger, RunVersion, StoredBlob, ToolNoun, ToolOutcome, ToolRefusalReason, ToolRisk, ToolUnaskedReason, ToolVerb, Usage } from "./run-basics.js";
|
|
3
3
|
import type { LoggedSandboxBlock } from "./sandbox-blocked.js";
|
|
4
4
|
import type { SandboxOpenedView } from "./sandbox-opened.js";
|
|
5
5
|
/** The fields every event in a run log has, whatever its type. */
|
|
@@ -154,6 +154,20 @@ export type RunEventBody = {
|
|
|
154
154
|
toolCallId: string;
|
|
155
155
|
reason: ToolRefusalReason;
|
|
156
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* A connector's tool call is about to run without asking a person,
|
|
159
|
+
* because the `read` list of the connector's file names the tool
|
|
160
|
+
* (requirement `events.tool-unasked`). `connector`: the connector's id.
|
|
161
|
+
* `tool`: the name the model called. Written before the call, so it shows
|
|
162
|
+
* even when the call fails.
|
|
163
|
+
*/
|
|
164
|
+
| {
|
|
165
|
+
type: "tool.unasked";
|
|
166
|
+
agent: string;
|
|
167
|
+
connector: string;
|
|
168
|
+
tool: string;
|
|
169
|
+
reason: ToolUnaskedReason;
|
|
170
|
+
}
|
|
157
171
|
/**
|
|
158
172
|
* A step's sandbox is being opened (requirement `events.started-events`).
|
|
159
173
|
* Written before the call, so a screen can say what the step waits for;
|
|
@@ -177,6 +191,24 @@ export type RunEventBody = {
|
|
|
177
191
|
harness: string;
|
|
178
192
|
timeoutMs: number;
|
|
179
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* The instructions a step's harness runs with, pinned by content hash
|
|
196
|
+
* (requirement `events.harness-instructions`). Written right before
|
|
197
|
+
* `harness.started`, so a review can tell which instructions a run had.
|
|
198
|
+
* `files` lists `AGENTS.md` and each skill file, with paths relative to the
|
|
199
|
+
* run's folder, never absolute ones; `sha256` is the hash of the whole set.
|
|
200
|
+
* It never holds file contents. Logs written before this event have none.
|
|
201
|
+
*/
|
|
202
|
+
| {
|
|
203
|
+
type: "harness.instructions";
|
|
204
|
+
state: string;
|
|
205
|
+
agent: string;
|
|
206
|
+
files: {
|
|
207
|
+
path: string;
|
|
208
|
+
sha256: string;
|
|
209
|
+
}[];
|
|
210
|
+
sha256: string;
|
|
211
|
+
}
|
|
180
212
|
/**
|
|
181
213
|
* A step's sandbox opened (requirement `events.sandbox-opened`). `sandbox`
|
|
182
214
|
* is a display copy of its profile, never its environment variables.
|
|
@@ -258,10 +290,11 @@ export type RunEventBody = {
|
|
|
258
290
|
}
|
|
259
291
|
/**
|
|
260
292
|
* `outcome`: how the call ended (requirement `events.tool-result-outcome`).
|
|
261
|
-
* `risk` and `
|
|
262
|
-
*
|
|
263
|
-
* `events.tool-result-risk`). Each is absent in older
|
|
264
|
-
* `noun` for a tool without a sheet.
|
|
293
|
+
* `risk`, `noun` and `verb`: what the tool can do, what one call sends,
|
|
294
|
+
* reads or drafts and what it does to it, copied from its integration's
|
|
295
|
+
* sheet when the call ends (requirement `events.tool-result-risk`). Each is absent in older
|
|
296
|
+
* logs, and `risk`, `noun` and `verb` for a tool without a sheet. A
|
|
297
|
+
* missing `verb` means "sent".
|
|
265
298
|
* `resultBlob`: set when the result was too large for the log; `result`
|
|
266
299
|
* then holds only its start, and the whole result is in the blob store
|
|
267
300
|
* (requirement `events.tool-result-blob`).
|
|
@@ -274,6 +307,7 @@ export type RunEventBody = {
|
|
|
274
307
|
outcome?: ToolOutcome;
|
|
275
308
|
risk?: ToolRisk;
|
|
276
309
|
noun?: ToolNoun;
|
|
310
|
+
verb?: ToolVerb;
|
|
277
311
|
resultBlob?: StoredBlob;
|
|
278
312
|
}
|
|
279
313
|
/** A connector tool call that needs a person's OK; the run waits for "approve" or "reject". */
|
|
@@ -360,6 +394,16 @@ export type RunEventBody = {
|
|
|
360
394
|
*/
|
|
361
395
|
context?: Record<string, unknown>;
|
|
362
396
|
}
|
|
397
|
+
/**
|
|
398
|
+
* The child run a waiting `workflow` state waits on runs again, so the run
|
|
399
|
+
* does too and takes no events until the child waits again
|
|
400
|
+
* (requirement `events.workflow-resumed`).
|
|
401
|
+
*/
|
|
402
|
+
| {
|
|
403
|
+
type: "workflow.resumed";
|
|
404
|
+
state: string;
|
|
405
|
+
childRunId: string;
|
|
406
|
+
}
|
|
363
407
|
/**
|
|
364
408
|
* `child`: the run is waiting because this child run waits; events go to it.
|
|
365
409
|
* `title` and `body`: the wait step's description, filled in against the
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { RunEvent } from "./run-event.js";
|
|
2
|
+
import { type SendCount } from "./run-sends.js";
|
|
3
|
+
/**
|
|
4
|
+
* What a run read, worked out from its log by the reader rules of requirement
|
|
5
|
+
* `events.tool-result-risk`: one count per noun in the order of each noun's
|
|
6
|
+
* first call, for every `tool.result` with `risk: "read"`, a `noun` and
|
|
7
|
+
* `outcome: "ok"`. Pure; the receipt says "Read 3 emails" from it.
|
|
8
|
+
*
|
|
9
|
+
* @param events - the run's events, oldest first
|
|
10
|
+
* @returns the reads, noun by noun; empty when the run read nothing it can name
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* readsOf(events);
|
|
14
|
+
* // gmail_read_message twice, drive_get_file once => [2 emails, 1 Drive file]
|
|
15
|
+
*/
|
|
16
|
+
export declare function readsOf(events: readonly RunEvent[]): SendCount[];
|
|
17
|
+
/**
|
|
18
|
+
* What a run drafted: the ok calls of a write tool whose `verb` is
|
|
19
|
+
* "drafted", counted per noun like {@link readsOf}. A draft is not a send.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* draftsOf(events);
|
|
23
|
+
* // gmail_create_draft once => [1 email]
|
|
24
|
+
*/
|
|
25
|
+
export declare function draftsOf(events: readonly RunEvent[]): SendCount[];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { countNoun } from "./run-sends.js";
|
|
2
|
+
/**
|
|
3
|
+
* What a run read, worked out from its log by the reader rules of requirement
|
|
4
|
+
* `events.tool-result-risk`: one count per noun in the order of each noun's
|
|
5
|
+
* first call, for every `tool.result` with `risk: "read"`, a `noun` and
|
|
6
|
+
* `outcome: "ok"`. Pure; the receipt says "Read 3 emails" from it.
|
|
7
|
+
*
|
|
8
|
+
* @param events - the run's events, oldest first
|
|
9
|
+
* @returns the reads, noun by noun; empty when the run read nothing it can name
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* readsOf(events);
|
|
13
|
+
* // gmail_read_message twice, drive_get_file once => [2 emails, 1 Drive file]
|
|
14
|
+
*/
|
|
15
|
+
export function readsOf(events) {
|
|
16
|
+
const counts = [];
|
|
17
|
+
for (const event of events) {
|
|
18
|
+
if (event.type !== "tool.result" || event.risk !== "read")
|
|
19
|
+
continue;
|
|
20
|
+
if (event.outcome !== "ok" || event.noun === undefined)
|
|
21
|
+
continue;
|
|
22
|
+
countNoun(counts, event.noun);
|
|
23
|
+
}
|
|
24
|
+
return counts;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* What a run drafted: the ok calls of a write tool whose `verb` is
|
|
28
|
+
* "drafted", counted per noun like {@link readsOf}. A draft is not a send.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* draftsOf(events);
|
|
32
|
+
* // gmail_create_draft once => [1 email]
|
|
33
|
+
*/
|
|
34
|
+
export function draftsOf(events) {
|
|
35
|
+
const counts = [];
|
|
36
|
+
for (const event of events) {
|
|
37
|
+
if (event.type !== "tool.result" || event.risk !== "write")
|
|
38
|
+
continue;
|
|
39
|
+
if (event.outcome !== "ok" || event.verb !== "drafted" || event.noun === undefined)
|
|
40
|
+
continue;
|
|
41
|
+
countNoun(counts, event.noun);
|
|
42
|
+
}
|
|
43
|
+
return counts;
|
|
44
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ToolNoun, ToolVerb } from "./run-basics.js";
|
|
2
|
+
import type { RunEvent } from "./run-event.js";
|
|
3
|
+
/** How many calls did one verb to one noun, such as 2 calls that sent an email. */
|
|
4
|
+
export interface SendCount {
|
|
5
|
+
noun: ToolNoun;
|
|
6
|
+
calls: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* What one verb did, noun by noun, in the order of each noun's first call:
|
|
10
|
+
* "sent" with 1 email and 2 invites reads "sent 1 email and 2 invites".
|
|
11
|
+
*/
|
|
12
|
+
export interface SendGroup {
|
|
13
|
+
verb: ToolVerb;
|
|
14
|
+
counts: SendCount[];
|
|
15
|
+
}
|
|
16
|
+
/** What a run sent, worked out from its log by {@link sendsOf}. */
|
|
17
|
+
export interface RunSends {
|
|
18
|
+
/** The calls that sent, one group per verb, in the order of each verb's first call. */
|
|
19
|
+
sent: SendGroup[];
|
|
20
|
+
/** The calls that broke off without an answer, so may have sent; grouped the same way. */
|
|
21
|
+
maySend: SendGroup[];
|
|
22
|
+
/**
|
|
23
|
+
* Whether the run made a tool call at all. A run "sent nothing" only when
|
|
24
|
+
* it made none: a call whose log doesn't say what it can do may have sent.
|
|
25
|
+
* Starting a child run counts as a call, because the child may have sent.
|
|
26
|
+
*/
|
|
27
|
+
madeToolCalls: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* What a run sent, by the reader rules of requirement
|
|
31
|
+
* `events.tool-result-risk`. Pure; the receipt and the runs list both use it,
|
|
32
|
+
* so they say the same thing about a run.
|
|
33
|
+
*
|
|
34
|
+
* A call counts as sent when its `tool.result` has `risk: "send"`, a `noun`
|
|
35
|
+
* and `outcome: "ok"`, and as "may have sent" with `outcome: "unknown"`. A
|
|
36
|
+
* missing `verb` is "sent". Groups and the counts in them keep call order.
|
|
37
|
+
*
|
|
38
|
+
* @param events - the run's events, oldest first
|
|
39
|
+
* @returns what it sent, what it may have sent, and whether it made a tool call
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const { sent } = sendsOf(await runStore.events(runId));
|
|
43
|
+
* // gmail_send, calendar_create_event twice, drive_create_file =>
|
|
44
|
+
* // [{ verb: "sent", counts: [1 email, 2 invites] }, { verb: "saved", counts: [1 file] }]
|
|
45
|
+
*/
|
|
46
|
+
export declare function sendsOf(events: readonly RunEvent[]): RunSends;
|
|
47
|
+
export declare function countNoun(counts: SendCount[], noun: ToolNoun): void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a run sent, by the reader rules of requirement
|
|
3
|
+
* `events.tool-result-risk`. Pure; the receipt and the runs list both use it,
|
|
4
|
+
* so they say the same thing about a run.
|
|
5
|
+
*
|
|
6
|
+
* A call counts as sent when its `tool.result` has `risk: "send"`, a `noun`
|
|
7
|
+
* and `outcome: "ok"`, and as "may have sent" with `outcome: "unknown"`. A
|
|
8
|
+
* missing `verb` is "sent". Groups and the counts in them keep call order.
|
|
9
|
+
*
|
|
10
|
+
* @param events - the run's events, oldest first
|
|
11
|
+
* @returns what it sent, what it may have sent, and whether it made a tool call
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const { sent } = sendsOf(await runStore.events(runId));
|
|
15
|
+
* // gmail_send, calendar_create_event twice, drive_create_file =>
|
|
16
|
+
* // [{ verb: "sent", counts: [1 email, 2 invites] }, { verb: "saved", counts: [1 file] }]
|
|
17
|
+
*/
|
|
18
|
+
export function sendsOf(events) {
|
|
19
|
+
const sends = { sent: [], maySend: [], madeToolCalls: false };
|
|
20
|
+
for (const event of events) {
|
|
21
|
+
if (!couldSend(event))
|
|
22
|
+
continue;
|
|
23
|
+
sends.madeToolCalls = true;
|
|
24
|
+
if (event.type !== "tool.result" || event.risk !== "send" || event.noun === undefined) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const verb = event.verb ?? "sent";
|
|
28
|
+
if (event.outcome === "ok")
|
|
29
|
+
addCall(sends.sent, verb, event.noun);
|
|
30
|
+
if (event.outcome === "unknown")
|
|
31
|
+
addCall(sends.maySend, verb, event.noun);
|
|
32
|
+
}
|
|
33
|
+
return sends;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Whether the event is a call that could have sent something: a tool call, or
|
|
37
|
+
* a child run started, whose own log holds what it sent.
|
|
38
|
+
*/
|
|
39
|
+
function couldSend(event) {
|
|
40
|
+
return (event.type === "tool.called" ||
|
|
41
|
+
event.type === "tool.result" ||
|
|
42
|
+
event.type === "workflow.started");
|
|
43
|
+
}
|
|
44
|
+
/** Count one call in its verb's group, adding the group or the noun at the end when new. */
|
|
45
|
+
function addCall(groups, verb, noun) {
|
|
46
|
+
let group = groups.find((candidate) => candidate.verb === verb);
|
|
47
|
+
if (group === undefined) {
|
|
48
|
+
group = { verb, counts: [] };
|
|
49
|
+
groups.push(group);
|
|
50
|
+
}
|
|
51
|
+
const count = group.counts.find((candidate) => sameNoun(candidate.noun, noun));
|
|
52
|
+
if (count === undefined) {
|
|
53
|
+
group.counts.push({ noun: { one: noun.one, many: noun.many }, calls: 1 });
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
count.calls += 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function countNoun(counts, noun) {
|
|
60
|
+
const count = counts.find((candidate) => sameNoun(candidate.noun, noun));
|
|
61
|
+
if (count === undefined)
|
|
62
|
+
counts.push({ noun: { one: noun.one, many: noun.many }, calls: 1 });
|
|
63
|
+
else
|
|
64
|
+
count.calls += 1;
|
|
65
|
+
}
|
|
66
|
+
function sameNoun(a, b) {
|
|
67
|
+
return a.one === b.one && a.many === b.many;
|
|
68
|
+
}
|