@mcoda/codali 0.1.133 → 0.1.135-codali.10
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/CHANGELOG.md +53 -0
- package/ORCHESTRATION.md +462 -0
- package/README.md +112 -1
- package/config/alternate-local.example.json +49 -0
- package/config/docdex-runtime.json +21 -0
- package/config/orchestration.schema.json +548 -0
- package/config/raijin-suku.example.json +172 -0
- package/config/terminal-long-scenarios.v1.json +71 -0
- package/dist/agents/RoleResolution.d.ts.map +1 -1
- package/dist/agents/RoleResolution.js +4 -4
- package/dist/api/CodaliApi.d.ts +16 -0
- package/dist/api/CodaliApi.d.ts.map +1 -1
- package/dist/api/CodaliApi.js +39 -10
- package/dist/cli/AskCommand.d.ts +3 -0
- package/dist/cli/AskCommand.d.ts.map +1 -1
- package/dist/cli/AskCommand.js +76 -13
- package/dist/cli/RunCommand.d.ts +4 -0
- package/dist/cli/RunCommand.d.ts.map +1 -1
- package/dist/cli/RunCommand.js +110 -24
- package/dist/cli/TerminalCommand.d.ts +35 -0
- package/dist/cli/TerminalCommand.d.ts.map +1 -0
- package/dist/cli/TerminalCommand.js +618 -0
- package/dist/cli/TerminalCredentials.d.ts +3 -0
- package/dist/cli/TerminalCredentials.d.ts.map +1 -0
- package/dist/cli/TerminalCredentials.js +89 -0
- package/dist/cli/TerminalInput.d.ts +24 -0
- package/dist/cli/TerminalInput.d.ts.map +1 -0
- package/dist/cli/TerminalInput.js +98 -0
- package/dist/cli/TerminalText.d.ts +10 -0
- package/dist/cli/TerminalText.d.ts.map +1 -0
- package/dist/cli/TerminalText.js +53 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +33 -4
- package/dist/cognitive/ConfiguredPipeline.d.ts +34 -0
- package/dist/cognitive/ConfiguredPipeline.d.ts.map +1 -0
- package/dist/cognitive/ConfiguredPipeline.js +50 -0
- package/dist/cognitive/ValidationRunner.d.ts +4 -0
- package/dist/cognitive/ValidationRunner.d.ts.map +1 -1
- package/dist/cognitive/ValidationRunner.js +18 -41
- package/dist/config/NodeOrchestration.d.ts +21 -0
- package/dist/config/NodeOrchestration.d.ts.map +1 -0
- package/dist/config/NodeOrchestration.js +54 -0
- package/dist/config/OrchestrationConfig.d.ts +100 -0
- package/dist/config/OrchestrationConfig.d.ts.map +1 -0
- package/dist/config/OrchestrationConfig.js +245 -0
- package/dist/connectors/mcp/McpClient.d.ts +3 -1
- package/dist/connectors/mcp/McpClient.d.ts.map +1 -1
- package/dist/connectors/mcp/McpClient.js +3 -2
- package/dist/connectors/mcp/McpServerRegistry.d.ts +2 -2
- package/dist/connectors/mcp/McpServerRegistry.d.ts.map +1 -1
- package/dist/connectors/mcp/McpServerRegistry.js +39 -16
- package/dist/connectors/mcp/McpToolAdapter.js +2 -2
- package/dist/connectors/media/ConfiguredMediaTools.d.ts +6 -0
- package/dist/connectors/media/ConfiguredMediaTools.d.ts.map +1 -0
- package/dist/connectors/media/ConfiguredMediaTools.js +41 -0
- package/dist/connectors/media/ImageGenerationTool.js +3 -3
- package/dist/connectors/media/MediaArtifactReceipt.d.ts +5 -0
- package/dist/connectors/media/MediaArtifactReceipt.d.ts.map +1 -0
- package/dist/connectors/media/MediaArtifactReceipt.js +65 -0
- package/dist/connectors/media/MediaJobStore.d.ts +68 -0
- package/dist/connectors/media/MediaJobStore.d.ts.map +1 -0
- package/dist/connectors/media/MediaJobStore.js +199 -0
- package/dist/connectors/media/MediaJobWorker.d.ts +2 -0
- package/dist/connectors/media/MediaJobWorker.d.ts.map +1 -0
- package/dist/connectors/media/MediaJobWorker.js +65 -0
- package/dist/connectors/media/RemoteMediaJob.d.ts +6 -0
- package/dist/connectors/media/RemoteMediaJob.d.ts.map +1 -0
- package/dist/connectors/media/RemoteMediaJob.js +80 -0
- package/dist/docdex/DocdexClient.d.ts +18 -1
- package/dist/docdex/DocdexClient.d.ts.map +1 -1
- package/dist/docdex/DocdexClient.js +135 -19
- package/dist/docdex/ManagedDocdex.d.ts +29 -0
- package/dist/docdex/ManagedDocdex.d.ts.map +1 -0
- package/dist/docdex/ManagedDocdex.js +215 -0
- package/dist/docdex/ManagedRuntime.d.ts +28 -0
- package/dist/docdex/ManagedRuntime.d.ts.map +1 -0
- package/dist/docdex/ManagedRuntime.js +212 -0
- package/dist/eval/EvaluationProcess.d.ts +13 -0
- package/dist/eval/EvaluationProcess.d.ts.map +1 -0
- package/dist/eval/EvaluationProcess.js +76 -0
- package/dist/eval/LongWorkflowJourney.d.ts +34 -0
- package/dist/eval/LongWorkflowJourney.d.ts.map +1 -0
- package/dist/eval/LongWorkflowJourney.js +243 -0
- package/dist/eval/PlanningCorpus.d.ts +45 -0
- package/dist/eval/PlanningCorpus.d.ts.map +1 -0
- package/dist/eval/PlanningCorpus.js +189 -0
- package/dist/eval/PlanningCorpusRunner.d.ts +13 -0
- package/dist/eval/PlanningCorpusRunner.d.ts.map +1 -0
- package/dist/eval/PlanningCorpusRunner.js +89 -0
- package/dist/eval/TerminalCorpusRunner.d.ts +36 -0
- package/dist/eval/TerminalCorpusRunner.d.ts.map +1 -0
- package/dist/eval/TerminalCorpusRunner.js +222 -0
- package/dist/eval/TerminalTaskCorpus.d.ts +26 -0
- package/dist/eval/TerminalTaskCorpus.d.ts.map +1 -0
- package/dist/eval/TerminalTaskCorpus.js +105 -0
- package/dist/eval/TerminalWorkflowCorpus.d.ts +11 -0
- package/dist/eval/TerminalWorkflowCorpus.d.ts.map +1 -0
- package/dist/eval/TerminalWorkflowCorpus.js +119 -0
- package/dist/eval/fixtures/ContractReviewProvider.d.ts +4 -0
- package/dist/eval/fixtures/ContractReviewProvider.d.ts.map +1 -0
- package/dist/eval/fixtures/ContractReviewProvider.js +10 -0
- package/dist/eval/fixtures/MixedMediaRecoveryJourney.d.ts +6 -0
- package/dist/eval/fixtures/MixedMediaRecoveryJourney.d.ts.map +1 -0
- package/dist/eval/fixtures/MixedMediaRecoveryJourney.js +145 -0
- package/dist/eval/fixtures/TerminalRecoveryJourney.d.ts +4 -0
- package/dist/eval/fixtures/TerminalRecoveryJourney.d.ts.map +1 -0
- package/dist/eval/fixtures/TerminalRecoveryJourney.js +136 -0
- package/dist/gateway/CodaliGateway.d.ts +2 -0
- package/dist/gateway/CodaliGateway.d.ts.map +1 -1
- package/dist/gateway/CodaliGateway.js +16 -4
- package/dist/gateway/ConfiguredGateway.d.ts +23 -0
- package/dist/gateway/ConfiguredGateway.d.ts.map +1 -0
- package/dist/gateway/ConfiguredGateway.js +44 -0
- package/dist/gateway/GatewayTracer.d.ts +1 -1
- package/dist/gateway/GatewayTracer.d.ts.map +1 -1
- package/dist/gateway/GatewayTracer.js +2 -27
- package/dist/gateway/LocalGatewayTaskRunner.d.ts.map +1 -1
- package/dist/gateway/LocalGatewayTaskRunner.js +13 -1
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -0
- package/dist/permissions/PermissionGate.d.ts +44 -0
- package/dist/permissions/PermissionGate.d.ts.map +1 -0
- package/dist/permissions/PermissionGate.js +97 -0
- package/dist/providers/ConfiguredEmbeddings.d.ts +43 -0
- package/dist/providers/ConfiguredEmbeddings.d.ts.map +1 -0
- package/dist/providers/ConfiguredEmbeddings.js +87 -0
- package/dist/providers/ConfiguredModelScope.d.ts +70 -0
- package/dist/providers/ConfiguredModelScope.d.ts.map +1 -0
- package/dist/providers/ConfiguredModelScope.js +117 -0
- package/dist/providers/ConfiguredProvider.d.ts +84 -0
- package/dist/providers/ConfiguredProvider.d.ts.map +1 -0
- package/dist/providers/ConfiguredProvider.js +271 -0
- package/dist/providers/ModelServiceError.d.ts +14 -0
- package/dist/providers/ModelServiceError.d.ts.map +1 -0
- package/dist/providers/ModelServiceError.js +67 -0
- package/dist/providers/MswarmWorkerProvider.d.ts.map +1 -1
- package/dist/providers/MswarmWorkerProvider.js +4 -0
- package/dist/providers/OpenAiCompatibleProvider.d.ts.map +1 -1
- package/dist/providers/OpenAiCompatibleProvider.js +140 -52
- package/dist/providers/ProviderTypes.d.ts +16 -0
- package/dist/providers/ProviderTypes.d.ts.map +1 -1
- package/dist/providers/RequestUsage.d.ts +11 -0
- package/dist/providers/RequestUsage.d.ts.map +1 -0
- package/dist/providers/RequestUsage.js +33 -0
- package/dist/runtime/CodaliJobRuntime.d.ts +6 -0
- package/dist/runtime/CodaliJobRuntime.d.ts.map +1 -1
- package/dist/runtime/CodaliJobRuntime.js +36 -1
- package/dist/runtime/CodaliRuntime.d.ts +31 -0
- package/dist/runtime/CodaliRuntime.d.ts.map +1 -1
- package/dist/runtime/CodaliRuntime.js +72 -1
- package/dist/runtime/CompletionReport.d.ts +5 -0
- package/dist/runtime/CompletionReport.d.ts.map +1 -0
- package/dist/runtime/CompletionReport.js +12 -0
- package/dist/runtime/ProgressTracker.d.ts +17 -0
- package/dist/runtime/ProgressTracker.d.ts.map +1 -0
- package/dist/runtime/ProgressTracker.js +27 -0
- package/dist/runtime/RequestedTestEvidence.d.ts +12 -0
- package/dist/runtime/RequestedTestEvidence.d.ts.map +1 -0
- package/dist/runtime/RequestedTestEvidence.js +16 -0
- package/dist/runtime/RequiredRetrieval.d.ts +13 -0
- package/dist/runtime/RequiredRetrieval.d.ts.map +1 -0
- package/dist/runtime/RequiredRetrieval.js +25 -0
- package/dist/runtime/ReviewContract.d.ts +35 -0
- package/dist/runtime/ReviewContract.d.ts.map +1 -0
- package/dist/runtime/ReviewContract.js +131 -0
- package/dist/runtime/ReviewCoordinator.d.ts +41 -0
- package/dist/runtime/ReviewCoordinator.d.ts.map +1 -0
- package/dist/runtime/ReviewCoordinator.js +81 -0
- package/dist/runtime/ReviewSource.d.ts +9 -0
- package/dist/runtime/ReviewSource.d.ts.map +1 -0
- package/dist/runtime/ReviewSource.js +54 -0
- package/dist/runtime/Runner.d.ts +13 -0
- package/dist/runtime/Runner.d.ts.map +1 -1
- package/dist/runtime/Runner.js +225 -158
- package/dist/runtime/SecretRedaction.d.ts +11 -0
- package/dist/runtime/SecretRedaction.d.ts.map +1 -0
- package/dist/runtime/SecretRedaction.js +85 -0
- package/dist/runtime/SessionController.d.ts +150 -0
- package/dist/runtime/SessionController.d.ts.map +1 -0
- package/dist/runtime/SessionController.js +1487 -0
- package/dist/runtime/StructuralEvidence.d.ts +22 -0
- package/dist/runtime/StructuralEvidence.d.ts.map +1 -0
- package/dist/runtime/StructuralEvidence.js +87 -0
- package/dist/runtime/TaskReview.d.ts +25 -0
- package/dist/runtime/TaskReview.d.ts.map +1 -0
- package/dist/runtime/TaskReview.js +84 -0
- package/dist/runtime/TaskReviewContext.d.ts +71 -0
- package/dist/runtime/TaskReviewContext.d.ts.map +1 -0
- package/dist/runtime/TaskReviewContext.js +50 -0
- package/dist/runtime/UsageReconciliation.d.ts +28 -0
- package/dist/runtime/UsageReconciliation.d.ts.map +1 -0
- package/dist/runtime/UsageReconciliation.js +68 -0
- package/dist/runtime/ValidationOutput.d.ts +5 -0
- package/dist/runtime/ValidationOutput.d.ts.map +1 -0
- package/dist/runtime/ValidationOutput.js +32 -0
- package/dist/runtime/VerificationCommand.d.ts +5 -0
- package/dist/runtime/VerificationCommand.d.ts.map +1 -0
- package/dist/runtime/VerificationCommand.js +76 -0
- package/dist/runtime/VerificationReceiptSelection.d.ts +15 -0
- package/dist/runtime/VerificationReceiptSelection.d.ts.map +1 -0
- package/dist/runtime/VerificationReceiptSelection.js +30 -0
- package/dist/runtime/WorkStatusPage.d.ts +15 -0
- package/dist/runtime/WorkStatusPage.d.ts.map +1 -0
- package/dist/runtime/WorkStatusPage.js +58 -0
- package/dist/runtime/WorkspaceLedger.d.ts +13 -0
- package/dist/runtime/WorkspaceLedger.d.ts.map +1 -0
- package/dist/runtime/WorkspaceLedger.js +145 -0
- package/dist/runtime/WorkspaceLock.d.ts.map +1 -1
- package/dist/runtime/WorkspaceLock.js +20 -17
- package/dist/runtime/WorkspaceSnapshot.d.ts +13 -0
- package/dist/runtime/WorkspaceSnapshot.d.ts.map +1 -0
- package/dist/runtime/WorkspaceSnapshot.js +88 -0
- package/dist/session/AtomicState.d.ts +13 -0
- package/dist/session/AtomicState.d.ts.map +1 -0
- package/dist/session/AtomicState.js +123 -0
- package/dist/session/SessionStoragePath.d.ts +15 -0
- package/dist/session/SessionStoragePath.d.ts.map +1 -0
- package/dist/session/SessionStoragePath.js +101 -0
- package/dist/session/SessionStore.d.ts +9 -0
- package/dist/session/SessionStore.d.ts.map +1 -1
- package/dist/session/SessionStore.js +181 -47
- package/dist/session/WorkState.d.ts +32 -0
- package/dist/session/WorkState.d.ts.map +1 -0
- package/dist/session/WorkState.js +399 -0
- package/dist/session/WorkTypes.d.ts +102 -0
- package/dist/session/WorkTypes.d.ts.map +1 -0
- package/dist/session/WorkTypes.js +1 -0
- package/dist/tools/ToolRegistry.d.ts +2 -0
- package/dist/tools/ToolRegistry.d.ts.map +1 -1
- package/dist/tools/ToolRegistry.js +12 -2
- package/dist/tools/ToolTypes.d.ts +16 -0
- package/dist/tools/ToolTypes.d.ts.map +1 -1
- package/dist/tools/docdex/DocdexTools.d.ts +2 -0
- package/dist/tools/docdex/DocdexTools.d.ts.map +1 -1
- package/dist/tools/docdex/DocdexTools.js +17 -2
- package/dist/tools/filesystem/FileTools.d.ts.map +1 -1
- package/dist/tools/filesystem/FileTools.js +30 -10
- package/dist/tools/shell/AsyncProcess.d.ts +19 -0
- package/dist/tools/shell/AsyncProcess.d.ts.map +1 -0
- package/dist/tools/shell/AsyncProcess.js +94 -0
- package/dist/tools/shell/DarwinRuntimeLibraries.d.ts +13 -0
- package/dist/tools/shell/DarwinRuntimeLibraries.d.ts.map +1 -0
- package/dist/tools/shell/DarwinRuntimeLibraries.js +130 -0
- package/dist/tools/shell/HostToolchain.d.ts +19 -0
- package/dist/tools/shell/HostToolchain.d.ts.map +1 -0
- package/dist/tools/shell/HostToolchain.js +266 -0
- package/dist/tools/shell/IsolatedExecutor.d.ts +15 -0
- package/dist/tools/shell/IsolatedExecutor.d.ts.map +1 -0
- package/dist/tools/shell/IsolatedExecutor.js +138 -0
- package/dist/tools/shell/ShellTool.d.ts.map +1 -1
- package/dist/tools/shell/ShellTool.js +9 -29
- package/dist/tools/shell/ShellWorkingDirectory.d.ts +3 -0
- package/dist/tools/shell/ShellWorkingDirectory.d.ts.map +1 -0
- package/dist/tools/shell/ShellWorkingDirectory.js +30 -0
- package/package.json +27 -24
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ReviewSourcePage } from './TaskReview.js';
|
|
2
|
+
/** Bounded review evidence; binary assets remain checksums, never prompt data. */
|
|
3
|
+
export declare const readReviewSource: (root: string, relative: string, expectedHash: string, maxChars: number) => Promise<{
|
|
4
|
+
content: string;
|
|
5
|
+
truncated: boolean;
|
|
6
|
+
}>;
|
|
7
|
+
/** Page offsets refer to redacted text, so repeated reads cannot reconstruct a secret. */
|
|
8
|
+
export declare function readReviewSourcePage(root: string, relative: string, expectedHash: string, offset: number): Promise<ReviewSourcePage>;
|
|
9
|
+
//# sourceMappingURL=ReviewSource.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReviewSource.d.ts","sourceRoot":"","sources":["../../src/runtime/ReviewSource.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,kFAAkF;AAClF,eAAO,MAAM,gBAAgB,GAAU,MAAM,MAAM,EAAE,UAAU,MAAM,EAAE,cAAc,MAAM,EAAE,UAAU,MAAM,KAAG,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAE,CAyB9J,CAAC;AAEF,0FAA0F;AAC1F,wBAAsB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAQ1I"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { constants, promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { redactSecretValues } from './SecretRedaction.js';
|
|
5
|
+
/** Bounded review evidence; binary assets remain checksums, never prompt data. */
|
|
6
|
+
export const readReviewSource = async (root, relative, expectedHash, maxChars) => {
|
|
7
|
+
if (!/^[a-f0-9]{64}$/.test(expectedHash))
|
|
8
|
+
return { content: '[missing or not a regular source file]', truncated: false };
|
|
9
|
+
const canonicalRoot = await fs.realpath(root);
|
|
10
|
+
const file = await fs.realpath(path.join(canonicalRoot, relative));
|
|
11
|
+
if (!file.startsWith(canonicalRoot + path.sep))
|
|
12
|
+
throw new Error(`Review source escapes the workspace: ${relative}`);
|
|
13
|
+
const handle = await fs.open(file, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
14
|
+
try {
|
|
15
|
+
const metadata = await handle.stat();
|
|
16
|
+
if (!metadata.isFile())
|
|
17
|
+
throw new Error(`Review source is not a regular file: ${relative}`);
|
|
18
|
+
if (metadata.size > 128 * 1024)
|
|
19
|
+
return { content: '[large source omitted; use bounded source/diff receipts for review]', truncated: true };
|
|
20
|
+
const bytes = Buffer.alloc(metadata.size + 1);
|
|
21
|
+
let length = 0;
|
|
22
|
+
while (length < bytes.length) {
|
|
23
|
+
const { bytesRead } = await handle.read(bytes, length, bytes.length - length, length);
|
|
24
|
+
if (!bytesRead)
|
|
25
|
+
break;
|
|
26
|
+
length += bytesRead;
|
|
27
|
+
}
|
|
28
|
+
const content = bytes.subarray(0, length);
|
|
29
|
+
if (createHash('sha256').update(content).digest('hex') !== expectedHash)
|
|
30
|
+
throw new Error(`Review source changed before inspection: ${relative}`);
|
|
31
|
+
if (content.includes(0) || /\.(?:png|jpe?g|gif|webp|ico|pdf|zip|tgz|gz|mp[34]|mov|webm|wav|flac|gguf|bin|exe)$/i.test(relative))
|
|
32
|
+
return { content: '[binary artifact omitted; checksum and tool receipt identify its content]', truncated: false };
|
|
33
|
+
// Redact before slicing: a credential spanning a page boundary must never
|
|
34
|
+
// leave a raw prefix or suffix in either prompt.
|
|
35
|
+
const text = redactSecretValues(content.toString('utf8'));
|
|
36
|
+
return { content: text.slice(0, maxChars), truncated: text.length > maxChars };
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
await handle.close();
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
/** Page offsets refer to redacted text, so repeated reads cannot reconstruct a secret. */
|
|
43
|
+
export async function readReviewSourcePage(root, relative, expectedHash, offset) {
|
|
44
|
+
if (!Number.isSafeInteger(offset) || offset < 0)
|
|
45
|
+
throw new Error('Invalid review source offset');
|
|
46
|
+
const source = await readReviewSource(root, relative, expectedHash, 128 * 1024);
|
|
47
|
+
if (source.truncated)
|
|
48
|
+
return { path: relative, offset, content: source.content, truncated: true, unavailable: true };
|
|
49
|
+
if (offset > source.content.length)
|
|
50
|
+
throw new Error(`Review source offset exceeds the file length: ${relative}`);
|
|
51
|
+
const content = source.content.slice(offset, offset + 8000);
|
|
52
|
+
const truncated = offset + content.length < source.content.length;
|
|
53
|
+
return { path: relative, offset, content, truncated, ...(truncated ? { nextOffset: offset + content.length } : {}) };
|
|
54
|
+
}
|
package/dist/runtime/Runner.d.ts
CHANGED
|
@@ -3,11 +3,19 @@ import { ToolRegistry } from "../tools/ToolRegistry.js";
|
|
|
3
3
|
import type { ToolContext } from "../tools/ToolTypes.js";
|
|
4
4
|
import type { RunLogger } from "./RunLogger.js";
|
|
5
5
|
export interface RunnerOptions {
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
/** A controller may reject a premature final answer within this same bounded loop. */
|
|
8
|
+
continueAfterFinal?: (message: ProviderMessage) => string | undefined | Promise<string | undefined>;
|
|
9
|
+
onMessages?: (messages: ProviderMessage[]) => Promise<void>;
|
|
6
10
|
provider: Provider;
|
|
7
11
|
tools: ToolRegistry;
|
|
8
12
|
context: ToolContext;
|
|
9
13
|
maxSteps: number;
|
|
10
14
|
maxToolCalls: number;
|
|
15
|
+
/** Reserve the last bounded worker step for summarizing gathered evidence. */
|
|
16
|
+
finalStepWithoutTools?: boolean;
|
|
17
|
+
/** Combined tool-result character budget per model turn, when bounded. */
|
|
18
|
+
maxToolResultChars?: number;
|
|
11
19
|
maxTokens?: number;
|
|
12
20
|
temperature?: number;
|
|
13
21
|
responseFormat?: ProviderResponseFormat;
|
|
@@ -45,11 +53,15 @@ export declare class RunnerBudgetError extends Error {
|
|
|
45
53
|
});
|
|
46
54
|
}
|
|
47
55
|
export declare class Runner {
|
|
56
|
+
private continueAfterFinal?;
|
|
57
|
+
private onMessages?;
|
|
48
58
|
private provider;
|
|
49
59
|
private tools;
|
|
50
60
|
private context;
|
|
51
61
|
private maxSteps;
|
|
52
62
|
private maxToolCalls;
|
|
63
|
+
private finalStepWithoutTools;
|
|
64
|
+
private maxToolResultChars?;
|
|
53
65
|
private maxTokens?;
|
|
54
66
|
private temperature?;
|
|
55
67
|
private responseFormat?;
|
|
@@ -60,6 +72,7 @@ export declare class Runner {
|
|
|
60
72
|
private streamFlushMs?;
|
|
61
73
|
private timeoutMs?;
|
|
62
74
|
private logger?;
|
|
75
|
+
private signal?;
|
|
63
76
|
constructor(options: RunnerOptions);
|
|
64
77
|
run(initialMessages: ProviderMessage[]): Promise<RunnerResult>;
|
|
65
78
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Runner.d.ts","sourceRoot":"","sources":["../../src/runtime/Runner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EAEV,QAAQ,EACR,eAAe,EACf,eAAe,EACf,sBAAsB,EAEtB,aAAa,EACd,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"Runner.d.ts","sourceRoot":"","sources":["../../src/runtime/Runner.ts"],"names":[],"mappings":"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"}
|
package/dist/runtime/Runner.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { truncateToolResult } from "../tools/TruncateResult.js";
|
|
2
|
+
import { ModelServiceError } from "../providers/ModelServiceError.js";
|
|
1
3
|
export class RunnerBudgetError extends Error {
|
|
2
4
|
constructor(code, metadata) {
|
|
3
5
|
super(code === "runner_timeout_exceeded"
|
|
@@ -26,6 +28,8 @@ export class Runner {
|
|
|
26
28
|
this.context = options.context;
|
|
27
29
|
this.maxSteps = options.maxSteps;
|
|
28
30
|
this.maxToolCalls = options.maxToolCalls;
|
|
31
|
+
this.finalStepWithoutTools = options.finalStepWithoutTools === true;
|
|
32
|
+
this.maxToolResultChars = options.maxToolResultChars;
|
|
29
33
|
this.maxTokens = options.maxTokens;
|
|
30
34
|
this.temperature = options.temperature;
|
|
31
35
|
this.responseFormat = options.responseFormat;
|
|
@@ -35,181 +39,244 @@ export class Runner {
|
|
|
35
39
|
this.onToken = options.onToken;
|
|
36
40
|
this.streamFlushMs = options.streamFlushMs;
|
|
37
41
|
this.timeoutMs = options.timeoutMs;
|
|
42
|
+
this.continueAfterFinal = options.continueAfterFinal;
|
|
43
|
+
this.onMessages = options.onMessages;
|
|
38
44
|
this.logger = options.logger;
|
|
45
|
+
this.signal = options.signal ?? options.context.signal;
|
|
39
46
|
}
|
|
40
47
|
async run(initialMessages) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
let timeoutId;
|
|
98
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
99
|
-
timeoutId = setTimeout(() => reject(new Error("Runner timeout exceeded")), remaining);
|
|
100
|
-
});
|
|
101
|
-
try {
|
|
102
|
-
return await Promise.race([promise, timeoutPromise]);
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
if (error instanceof Error && /Runner timeout exceeded/i.test(error.message)) {
|
|
106
|
-
await throwBudgetError("runner_timeout_exceeded", -1);
|
|
48
|
+
this.signal?.throwIfAborted();
|
|
49
|
+
const abortController = new AbortController();
|
|
50
|
+
const onAbort = () => abortController.abort(this.signal?.reason);
|
|
51
|
+
this.signal?.addEventListener("abort", onAbort, { once: true });
|
|
52
|
+
const timeout = this.timeoutMs ? setTimeout(() => abortController.abort(new Error("Runner timeout exceeded")), this.timeoutMs) : undefined;
|
|
53
|
+
const signal = abortController.signal;
|
|
54
|
+
try {
|
|
55
|
+
const messages = [...initialMessages];
|
|
56
|
+
let toolCallsExecuted = 0;
|
|
57
|
+
let usageTotals;
|
|
58
|
+
const deadline = this.timeoutMs ? Date.now() + this.timeoutMs : undefined;
|
|
59
|
+
const emitEvent = (event) => {
|
|
60
|
+
if (signal.aborted)
|
|
61
|
+
return;
|
|
62
|
+
if (this.onEvent) {
|
|
63
|
+
this.onEvent(event);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (event.type === "token") {
|
|
67
|
+
this.onToken?.(event.content);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const emitStatus = (phase, message) => {
|
|
71
|
+
this.onEvent?.({ type: "status", phase, message });
|
|
72
|
+
};
|
|
73
|
+
const recordUsage = (usage) => {
|
|
74
|
+
if (!usage)
|
|
75
|
+
return;
|
|
76
|
+
if (!usageTotals)
|
|
77
|
+
usageTotals = {};
|
|
78
|
+
if (usage.inputTokens !== undefined) {
|
|
79
|
+
usageTotals.inputTokens = (usageTotals.inputTokens ?? 0) + usage.inputTokens;
|
|
80
|
+
}
|
|
81
|
+
if (usage.outputTokens !== undefined) {
|
|
82
|
+
usageTotals.outputTokens = (usageTotals.outputTokens ?? 0) + usage.outputTokens;
|
|
83
|
+
}
|
|
84
|
+
if (usage.totalTokens !== undefined) {
|
|
85
|
+
usageTotals.totalTokens = (usageTotals.totalTokens ?? 0) + usage.totalTokens;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const timeRemaining = () => {
|
|
89
|
+
if (!deadline)
|
|
90
|
+
return undefined;
|
|
91
|
+
return deadline - Date.now();
|
|
92
|
+
};
|
|
93
|
+
const throwBudgetError = async (code, step) => {
|
|
94
|
+
const error = new RunnerBudgetError(code, {
|
|
95
|
+
step,
|
|
96
|
+
tool_calls_executed: toolCallsExecuted,
|
|
97
|
+
max_steps: this.maxSteps,
|
|
98
|
+
max_tool_calls: this.maxToolCalls,
|
|
99
|
+
timeout_ms: this.timeoutMs,
|
|
100
|
+
});
|
|
101
|
+
if (this.logger) {
|
|
102
|
+
await this.logger.log("runner_budget_failure", error.metadata);
|
|
107
103
|
}
|
|
108
104
|
throw error;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const toolSpecs = this.tools.describe().map((tool) => ({
|
|
120
|
-
name: tool.name,
|
|
121
|
-
description: tool.description,
|
|
122
|
-
inputSchema: tool.inputSchema,
|
|
123
|
-
}));
|
|
124
|
-
const allowTools = toolSpecs.length > 0 && this.toolChoice !== "none";
|
|
125
|
-
emitStatus("thinking");
|
|
126
|
-
if (this.logger) {
|
|
127
|
-
await this.logger.log("provider_request", {
|
|
128
|
-
provider: this.provider.name,
|
|
129
|
-
messages,
|
|
130
|
-
tools: allowTools ? toolSpecs : [],
|
|
131
|
-
toolChoice: allowTools ? this.toolChoice ?? "auto" : "none",
|
|
132
|
-
responseFormat: this.responseFormat,
|
|
133
|
-
temperature: this.temperature,
|
|
134
|
-
maxTokens: this.maxTokens,
|
|
135
|
-
stream: this.stream ?? false,
|
|
105
|
+
};
|
|
106
|
+
const withTimeout = async (promise, label) => {
|
|
107
|
+
const remaining = timeRemaining();
|
|
108
|
+
if (remaining !== undefined && remaining <= 0) {
|
|
109
|
+
await throwBudgetError("runner_timeout_exceeded", -1);
|
|
110
|
+
}
|
|
111
|
+
let timeoutId;
|
|
112
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
113
|
+
if (remaining !== undefined)
|
|
114
|
+
timeoutId = setTimeout(() => reject(new Error("Runner timeout exceeded")), remaining);
|
|
136
115
|
});
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
onToken: (token) => emitEvent({ type: "token", content: token }),
|
|
148
|
-
streamFlushMs: this.streamFlushMs,
|
|
149
|
-
}), "provider");
|
|
150
|
-
recordUsage(response.usage);
|
|
151
|
-
messages.push(response.message);
|
|
152
|
-
if (this.logger) {
|
|
153
|
-
await this.logger.log("provider_response", {
|
|
154
|
-
message: response.message,
|
|
155
|
-
toolCalls: response.toolCalls,
|
|
156
|
-
usage: response.usage,
|
|
116
|
+
let onCancel;
|
|
117
|
+
const cancelled = new Promise((_, reject) => {
|
|
118
|
+
// Provider streams may not honor AbortSignal; never accept their late reply.
|
|
119
|
+
// Tools are allowed to finish their cancellation cleanup before unlocking.
|
|
120
|
+
if (label === "provider") {
|
|
121
|
+
onCancel = () => reject(signal.reason ?? new Error("Cancelled"));
|
|
122
|
+
signal.addEventListener("abort", onCancel, { once: true });
|
|
123
|
+
if (signal.aborted)
|
|
124
|
+
onCancel();
|
|
125
|
+
}
|
|
157
126
|
});
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
127
|
+
try {
|
|
128
|
+
return await Promise.race([promise, timeoutPromise, cancelled]);
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
if (error instanceof Error && /Runner timeout exceeded/i.test(error.message)) {
|
|
132
|
+
await throwBudgetError("runner_timeout_exceeded", -1);
|
|
133
|
+
}
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
if (timeoutId)
|
|
138
|
+
clearTimeout(timeoutId);
|
|
139
|
+
if (onCancel)
|
|
140
|
+
signal.removeEventListener("abort", onCancel);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
for (let step = 0; step < this.maxSteps; step += 1) {
|
|
144
|
+
signal.throwIfAborted();
|
|
145
|
+
if (deadline && timeRemaining() <= 0) {
|
|
146
|
+
await throwBudgetError("runner_timeout_exceeded", step);
|
|
147
|
+
}
|
|
148
|
+
const toolSpecs = this.tools.describe().map((tool) => ({
|
|
149
|
+
name: tool.name,
|
|
150
|
+
description: tool.description,
|
|
151
|
+
inputSchema: tool.inputSchema,
|
|
152
|
+
}));
|
|
153
|
+
const finalSummaryStep = this.finalStepWithoutTools && step > 0 && step === this.maxSteps - 1;
|
|
154
|
+
const allowTools = toolSpecs.length > 0 && this.toolChoice !== "none" && !finalSummaryStep;
|
|
155
|
+
emitStatus("thinking");
|
|
156
|
+
if (this.logger) {
|
|
157
|
+
await this.logger.log("provider_request", {
|
|
158
|
+
provider: this.provider.name,
|
|
159
|
+
messages,
|
|
160
|
+
tools: allowTools ? toolSpecs : [],
|
|
161
|
+
toolChoice: allowTools ? this.toolChoice ?? "auto" : "none",
|
|
162
|
+
responseFormat: this.responseFormat,
|
|
163
|
+
temperature: this.temperature,
|
|
164
|
+
maxTokens: this.maxTokens,
|
|
165
|
+
stream: this.stream ?? false,
|
|
166
|
+
});
|
|
170
167
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
:
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
type: "
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
168
|
+
const response = await withTimeout(this.provider.generate({
|
|
169
|
+
signal,
|
|
170
|
+
messages,
|
|
171
|
+
tools: allowTools ? toolSpecs : undefined,
|
|
172
|
+
toolChoice: allowTools ? this.toolChoice ?? "auto" : finalSummaryStep ? "none" : undefined,
|
|
173
|
+
maxTokens: this.maxTokens,
|
|
174
|
+
temperature: this.temperature,
|
|
175
|
+
responseFormat: this.responseFormat,
|
|
176
|
+
stream: this.stream,
|
|
177
|
+
onEvent: emitEvent,
|
|
178
|
+
onToken: (token) => emitEvent({ type: "token", content: token }),
|
|
179
|
+
streamFlushMs: this.streamFlushMs,
|
|
180
|
+
}), "provider").catch((error) => {
|
|
181
|
+
if (error instanceof ModelServiceError)
|
|
182
|
+
error.completedModelCalls = step;
|
|
183
|
+
throw error;
|
|
184
|
+
});
|
|
185
|
+
recordUsage(response.usage);
|
|
186
|
+
signal.throwIfAborted();
|
|
187
|
+
messages.push({
|
|
188
|
+
...response.message,
|
|
189
|
+
...(response.toolCalls?.length ? { toolCalls: response.toolCalls } : {}),
|
|
187
190
|
});
|
|
191
|
+
await this.onMessages?.([...messages]);
|
|
188
192
|
if (this.logger) {
|
|
189
|
-
await this.logger.log("
|
|
193
|
+
await this.logger.log("provider_response", {
|
|
194
|
+
message: response.message,
|
|
195
|
+
toolCalls: response.toolCalls,
|
|
196
|
+
usage: response.usage,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
if (!response.toolCalls || response.toolCalls.length === 0) {
|
|
200
|
+
const continuation = await this.continueAfterFinal?.(response.message);
|
|
201
|
+
if (continuation) {
|
|
202
|
+
messages.push({ role: "user", content: continuation });
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
finalMessage: response.message,
|
|
207
|
+
messages,
|
|
208
|
+
toolCallsExecuted,
|
|
209
|
+
usage: usageTotals,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
for (const call of response.toolCalls) {
|
|
213
|
+
signal.throwIfAborted();
|
|
214
|
+
if (toolCallsExecuted >= this.maxToolCalls) {
|
|
215
|
+
await throwBudgetError("runner_tool_call_limit_exceeded", step);
|
|
216
|
+
}
|
|
217
|
+
toolCallsExecuted += 1;
|
|
218
|
+
emitStatus("executing", call.name);
|
|
219
|
+
this.onEvent?.({ type: "tool_call", name: call.name, args: call.args });
|
|
220
|
+
const result = await withTimeout(this.tools.execute(call.name, call.args, { ...this.context, signal }), "tool");
|
|
221
|
+
const toolError = result.error;
|
|
222
|
+
let content = result.ok
|
|
223
|
+
? result.output
|
|
224
|
+
: `ERROR[${toolError?.code ?? "tool_execution_failed"}]: ${toolError?.message ?? "tool failed"}`;
|
|
225
|
+
if (result.ok && this.maxToolResultChars && this.maxToolResultChars > 0) {
|
|
226
|
+
const limit = Math.max(1, Math.floor(this.maxToolResultChars / response.toolCalls.length));
|
|
227
|
+
let structured = result.data;
|
|
228
|
+
if (structured === undefined) {
|
|
229
|
+
try {
|
|
230
|
+
structured = JSON.parse(content);
|
|
231
|
+
}
|
|
232
|
+
catch { /* Plain-text tool output. */ }
|
|
233
|
+
}
|
|
234
|
+
content = truncateToolResult(structured, content, limit).text;
|
|
235
|
+
}
|
|
236
|
+
if (result.invocationReceipt) {
|
|
237
|
+
content += `\n\n[Tool metadata: invocation receipt ID ${result.invocationReceipt.id}; receiptRef=${result.invocationReceipt.reference}]`;
|
|
238
|
+
}
|
|
239
|
+
messages.push(buildToolMessage(call, content));
|
|
240
|
+
await this.onMessages?.([...messages]);
|
|
241
|
+
this.onEvent?.({
|
|
242
|
+
type: "tool_result",
|
|
190
243
|
name: call.name,
|
|
244
|
+
output: content,
|
|
191
245
|
ok: result.ok,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
error_category: toolError?.category,
|
|
195
|
-
error_retryable: toolError?.retryable,
|
|
196
|
-
error_details: toolError?.details,
|
|
246
|
+
errorCode: toolError?.code,
|
|
247
|
+
retryable: toolError?.retryable,
|
|
197
248
|
});
|
|
198
|
-
if (
|
|
199
|
-
await this.logger.
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
249
|
+
if (this.logger) {
|
|
250
|
+
await this.logger.log("tool_call", {
|
|
251
|
+
name: call.name,
|
|
252
|
+
ok: result.ok,
|
|
253
|
+
error: toolError?.message,
|
|
254
|
+
error_code: toolError?.code,
|
|
255
|
+
error_category: toolError?.category,
|
|
256
|
+
error_retryable: toolError?.retryable,
|
|
257
|
+
error_details: toolError?.details,
|
|
207
258
|
});
|
|
259
|
+
if (toolError) {
|
|
260
|
+
await this.logger.logSafetyEvent({
|
|
261
|
+
phase: "act",
|
|
262
|
+
category: "tool",
|
|
263
|
+
code: toolError.code,
|
|
264
|
+
disposition: toolError.retryable ? "retryable" : "non_retryable",
|
|
265
|
+
tool: call.name,
|
|
266
|
+
message: toolError.message,
|
|
267
|
+
details: toolError.details,
|
|
268
|
+
});
|
|
269
|
+
}
|
|
208
270
|
}
|
|
209
271
|
}
|
|
210
272
|
}
|
|
273
|
+
await throwBudgetError("runner_step_limit_exceeded", this.maxSteps);
|
|
274
|
+
throw new Error("unreachable_runner_budget_path");
|
|
275
|
+
}
|
|
276
|
+
finally {
|
|
277
|
+
if (timeout)
|
|
278
|
+
clearTimeout(timeout);
|
|
279
|
+
this.signal?.removeEventListener("abort", onAbort);
|
|
211
280
|
}
|
|
212
|
-
await throwBudgetError("runner_step_limit_exceeded", this.maxSteps);
|
|
213
|
-
throw new Error("unreachable_runner_budget_path");
|
|
214
281
|
}
|
|
215
282
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Register credentials when resolved; do not scan or expose unrelated environment values. */
|
|
2
|
+
export declare const registerSecretValue: (value: string) => void;
|
|
3
|
+
export declare const redactSecretValues: (value: string) => string;
|
|
4
|
+
/** Hold incomplete credential-shaped words so split chunks cannot expose a prefix or tail. */
|
|
5
|
+
export declare class StreamingSecretRedactor {
|
|
6
|
+
private pending;
|
|
7
|
+
private droppingWord;
|
|
8
|
+
write(chunk: string): string;
|
|
9
|
+
finish(): string;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=SecretRedaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SecretRedaction.d.ts","sourceRoot":"","sources":["../../src/runtime/SecretRedaction.ts"],"names":[],"mappings":"AAaA,8FAA8F;AAC9F,eAAO,MAAM,mBAAmB,GAAI,OAAO,MAAM,KAAG,IAEnD,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,KAAG,MAKlD,CAAC;AAEF,8FAA8F;AAC9F,qBAAa,uBAAuB;IAClC,OAAO,CAAC,OAAO,CAAM;IACrB,OAAO,CAAC,YAAY,CAAS;IAC7B,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IA4B5B,MAAM,IAAI,MAAM;CAUjB"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const knownSecrets = new Set();
|
|
2
|
+
const patterns = [
|
|
3
|
+
/\bBearer\s+[A-Za-z0-9._~+/=-]{12,}/gi,
|
|
4
|
+
/\bsk-[A-Za-z0-9_-]{16,}\b/g,
|
|
5
|
+
/\bsk_[A-Za-z0-9]+_[A-Za-z0-9_]{8,}\b/g,
|
|
6
|
+
/\bgh[pousr]_[A-Za-z0-9_]{20,}\b/g,
|
|
7
|
+
/\bglpat-[A-Za-z0-9_-]{16,}\b/g,
|
|
8
|
+
/\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g,
|
|
9
|
+
/\bAKIA[0-9A-Z]{16}\b/g,
|
|
10
|
+
/\bAIza[0-9A-Za-z_-]{30,}\b/g,
|
|
11
|
+
/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g,
|
|
12
|
+
];
|
|
13
|
+
/** Register credentials when resolved; do not scan or expose unrelated environment values. */
|
|
14
|
+
export const registerSecretValue = (value) => {
|
|
15
|
+
if (value.length >= 4) {
|
|
16
|
+
knownSecrets.add(value);
|
|
17
|
+
knownSecrets.add(JSON.stringify(value).slice(1, -1));
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
export const redactSecretValues = (value) => {
|
|
21
|
+
let output = value;
|
|
22
|
+
for (const secret of [...knownSecrets].sort((a, b) => b.length - a.length))
|
|
23
|
+
output = output.split(secret).join("[redacted]");
|
|
24
|
+
for (const pattern of patterns)
|
|
25
|
+
output = output.replace(pattern, "[redacted]");
|
|
26
|
+
return output;
|
|
27
|
+
};
|
|
28
|
+
/** Hold incomplete credential-shaped words so split chunks cannot expose a prefix or tail. */
|
|
29
|
+
export class StreamingSecretRedactor {
|
|
30
|
+
constructor() {
|
|
31
|
+
this.pending = "";
|
|
32
|
+
this.droppingWord = false;
|
|
33
|
+
}
|
|
34
|
+
write(chunk) {
|
|
35
|
+
if (this.droppingWord) {
|
|
36
|
+
const boundary = chunk.search(/[^A-Za-z0-9._~+/=-]/);
|
|
37
|
+
if (boundary < 0)
|
|
38
|
+
return "";
|
|
39
|
+
chunk = chunk.slice(boundary);
|
|
40
|
+
this.droppingWord = false;
|
|
41
|
+
}
|
|
42
|
+
this.pending += chunk;
|
|
43
|
+
let cutoff = this.pending.search(/[A-Za-z0-9._~+/=-]*$/);
|
|
44
|
+
const bearer = /\bBearer\s+[A-Za-z0-9._~+/=-]*$/i.exec(this.pending);
|
|
45
|
+
if (bearer)
|
|
46
|
+
cutoff = Math.min(cutoff, bearer.index);
|
|
47
|
+
for (const secret of knownSecrets) {
|
|
48
|
+
// An exact credential may contain spaces/punctuation, unlike a bearer word.
|
|
49
|
+
for (let at = this.pending.indexOf(secret); at >= 0; at = this.pending.indexOf(secret, at + 1)) {
|
|
50
|
+
if (at < cutoff && at + secret.length > cutoff)
|
|
51
|
+
cutoff = at;
|
|
52
|
+
}
|
|
53
|
+
for (let size = Math.min(secret.length - 1, this.pending.length); size > 0; size--) {
|
|
54
|
+
if (this.pending.endsWith(secret.slice(0, size))) {
|
|
55
|
+
cutoff = Math.min(cutoff, this.pending.length - size);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
let output = redactSecretValues(this.pending.slice(0, cutoff));
|
|
61
|
+
this.pending = this.pending.slice(cutoff);
|
|
62
|
+
// Never release a truncated prefix; discard an oversized unbroken token.
|
|
63
|
+
const longestCredential = Math.max(0, ...[...knownSecrets].map(secret => secret.length));
|
|
64
|
+
if (this.pending.length > Math.max(65536, longestCredential)) {
|
|
65
|
+
output += "[redacted oversized token]";
|
|
66
|
+
this.pending = "";
|
|
67
|
+
this.droppingWord = true;
|
|
68
|
+
}
|
|
69
|
+
return output;
|
|
70
|
+
}
|
|
71
|
+
finish() {
|
|
72
|
+
let output = redactSecretValues(this.pending);
|
|
73
|
+
for (const secret of knownSecrets) {
|
|
74
|
+
for (let size = Math.min(secret.length - 1, output.length); size >= 4; size--) {
|
|
75
|
+
if (output.endsWith(secret.slice(0, size))) {
|
|
76
|
+
output = `${output.slice(0, -size)}[redacted]`;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
this.pending = "";
|
|
82
|
+
this.droppingWord = false;
|
|
83
|
+
return output;
|
|
84
|
+
}
|
|
85
|
+
}
|