@hassangameryt/freeflow 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/plugins/marketplace.json +1 -1
- package/.claude-plugin/marketplace.json +4 -4
- package/{plugins/freeflow/.claude-plugin → .claude-plugin}/plugin.json +5 -2
- package/{plugins/freeflow/.codex-plugin → .codex-plugin}/plugin.json +8 -9
- package/README.md +194 -127
- package/assets/feedback-loop.png +0 -0
- package/assets/freeflow-plugin-icon.png +0 -0
- package/assets/freeflow_workflow_map.png +0 -0
- package/assets/workflow.png +0 -0
- package/command-surface.json +129 -0
- package/hooks/freeflow-runtime-context.mjs +419 -0
- package/hooks/tests/check-runtime-context-hook.sh +223 -0
- package/package.json +41 -22
- package/pi-extension/dist/index.js +269 -0
- package/pi-extension/dist/integrations/mcp-config.js +29 -0
- package/pi-extension/dist/output-router/host-producer-identification.js +232 -0
- package/pi-extension/dist/output-router/native-safety-net.js +216 -0
- package/pi-extension/dist/output-router/observed-tool-routing.js +122 -0
- package/pi-extension/dist/output-router/renderers.js +1023 -0
- package/pi-extension/dist/output-router/router-tools.js +554 -0
- package/pi-extension/dist/output-router/schemas.js +383 -0
- package/pi-extension/dist/output-router/status.js +744 -0
- package/pi-extension/dist/output-router/utils.js +502 -0
- package/pi-extension/dist/runtime/runtime-context.js +798 -0
- package/pi-extension/dist/settings/settings-tui.js +314 -0
- package/pi-extension/dist/settings/settings-ui.js +1487 -0
- package/pi-extension/freeflow/index.js +1 -0
- package/pi-extension/src/index.ts +295 -0
- package/pi-extension/src/integrations/mcp-config.ts +32 -0
- package/pi-extension/src/output-router/host-producer-identification.ts +256 -0
- package/pi-extension/src/output-router/native-safety-net.ts +248 -0
- package/pi-extension/src/output-router/observed-tool-routing.ts +134 -0
- package/pi-extension/src/output-router/renderers.ts +1108 -0
- package/pi-extension/src/output-router/router-tools.ts +579 -0
- package/pi-extension/src/output-router/schemas.ts +387 -0
- package/pi-extension/src/output-router/status.ts +791 -0
- package/pi-extension/src/output-router/utils.ts +554 -0
- package/pi-extension/src/runtime/runtime-context.ts +895 -0
- package/pi-extension/src/settings/settings-tui.ts +408 -0
- package/pi-extension/src/settings/settings-ui.ts +1730 -0
- package/pi-extension/tests/integration/pi-extension.test.js +4018 -0
- package/pi-extension/tests/output-router/observed-producers.test.js +132 -0
- package/pi-extension/tests/output-router/search.test.js +722 -0
- package/pi-extension/tests/settings/settings-tui.test.js +243 -0
- package/pi-extension/tsconfig.json +16 -0
- package/{plugins/freeflow/router/dist → router/dist/benchmarks}/benchmark-harness.d.ts +16 -13
- package/router/dist/benchmarks/benchmark-harness.js +92 -0
- package/router/dist/benchmarks/benchmarks.d.ts +109 -0
- package/router/dist/benchmarks/benchmarks.js +979 -0
- package/router/dist/benchmarks/codex-qa-benchmarks.d.ts +95 -0
- package/router/dist/benchmarks/codex-qa-benchmarks.js +500 -0
- package/router/dist/benchmarks/command-benchmarks.d.ts +143 -0
- package/router/dist/benchmarks/command-benchmarks.js +698 -0
- package/router/dist/benchmarks/context-mode-normalized-benchmarks.d.ts +82 -0
- package/router/dist/benchmarks/context-mode-normalized-benchmarks.js +773 -0
- package/router/dist/benchmarks/context-mode-real-deep-benchmark.d.ts +132 -0
- package/router/dist/benchmarks/context-mode-real-deep-benchmark.js +1380 -0
- package/router/dist/benchmarks/context-saving-baseline.d.ts +60 -0
- package/router/dist/benchmarks/context-saving-baseline.js +470 -0
- package/router/dist/benchmarks/index-benchmarks.d.ts +103 -0
- package/router/dist/benchmarks/index-benchmarks.js +862 -0
- package/router/dist/benchmarks/storage-policy-benchmarks.d.ts +101 -0
- package/router/dist/benchmarks/storage-policy-benchmarks.js +618 -0
- package/router/dist/benchmarks/vault-index-benchmarks.d.ts +56 -0
- package/router/dist/benchmarks/vault-index-benchmarks.js +322 -0
- package/router/dist/config/config.d.ts +103 -0
- package/router/dist/config/config.js +632 -0
- package/{plugins/freeflow/router/dist → router/dist/config}/router-contract.d.ts +2 -2
- package/router/dist/config/router-contract.js +54 -0
- package/{plugins/freeflow/router/dist → router/dist/config}/schema.d.ts +19 -10
- package/router/dist/config/schema.js +648 -0
- package/router/dist/config/types.d.ts +573 -0
- package/router/dist/config/types.js +41 -0
- package/router/dist/evidence/bounded-evidence.d.ts +40 -0
- package/router/dist/evidence/bounded-evidence.js +323 -0
- package/router/dist/evidence/evidence-range-selector.d.ts +24 -0
- package/router/dist/evidence/evidence-range-selector.js +232 -0
- package/router/dist/evidence/evidence-search.d.ts +27 -0
- package/router/dist/evidence/evidence-search.js +490 -0
- package/{plugins/freeflow/router/dist → router/dist/evidence}/evidence.d.ts +18 -18
- package/router/dist/evidence/evidence.js +200 -0
- package/router/dist/evidence/failure-contracts.d.ts +37 -0
- package/router/dist/evidence/failure-contracts.js +166 -0
- package/router/dist/evidence/line-ranges.d.ts +24 -0
- package/router/dist/evidence/line-ranges.js +22 -0
- package/router/dist/experiments/local-index.d.ts +64 -0
- package/router/dist/experiments/local-index.js +581 -0
- package/router/dist/index.d.ts +30 -0
- package/router/dist/index.js +32 -0
- package/router/dist/local/local-traversal.d.ts +18 -0
- package/router/dist/local/local-traversal.js +334 -0
- package/router/dist/processing/engine.d.ts +132 -0
- package/router/dist/processing/engine.js +533 -0
- package/router/dist/processing/reducers.d.ts +280 -0
- package/router/dist/processing/reducers.js +1673 -0
- package/router/dist/processing/renderers.d.ts +31 -0
- package/router/dist/processing/renderers.js +195 -0
- package/router/dist/processing/scripts.d.ts +91 -0
- package/router/dist/processing/scripts.js +386 -0
- package/{plugins/freeflow/router/dist → router/dist/repo}/repo-traversal.d.ts +9 -9
- package/router/dist/repo/repo-traversal.js +245 -0
- package/router/dist/routing/observed-reducers.d.ts +41 -0
- package/router/dist/routing/observed-reducers.js +661 -0
- package/router/dist/routing/observed-routing.d.ts +26 -0
- package/router/dist/routing/observed-routing.js +378 -0
- package/router/dist/routing/parsers.d.ts +16 -0
- package/router/dist/routing/parsers.js +521 -0
- package/router/dist/routing/run-filters.d.ts +60 -0
- package/router/dist/routing/run-filters.js +238 -0
- package/router/dist/routing/run-reducers.d.ts +49 -0
- package/router/dist/routing/run-reducers.js +122 -0
- package/router/dist/sandbox/adapter-roots.d.ts +46 -0
- package/router/dist/sandbox/adapter-roots.js +66 -0
- package/router/dist/sandbox/eryx-python-adapter.d.ts +13 -0
- package/router/dist/sandbox/eryx-python-adapter.js +868 -0
- package/router/dist/sandbox/jq-wasm-adapter.d.ts +9 -0
- package/router/dist/sandbox/jq-wasm-adapter.js +429 -0
- package/router/dist/sandbox/quickjs-wasi-adapter.d.ts +13 -0
- package/router/dist/sandbox/quickjs-wasi-adapter.js +384 -0
- package/router/dist/sandbox/script-sandbox.d.ts +134 -0
- package/router/dist/sandbox/script-sandbox.js +337 -0
- package/router/dist/setup/script-transform-adapters.d.ts +32 -0
- package/router/dist/setup/script-transform-adapters.js +278 -0
- package/router/dist/tools/batch.d.ts +33 -0
- package/router/dist/tools/batch.js +586 -0
- package/router/dist/tools/run.d.ts +62 -0
- package/router/dist/tools/run.js +1304 -0
- package/router/dist/tools/search.d.ts +66 -0
- package/router/dist/tools/search.js +1364 -0
- package/router/dist/transform/engine.d.ts +168 -0
- package/router/dist/transform/engine.js +2072 -0
- package/router/dist/vault/vault-index.d.ts +125 -0
- package/router/dist/vault/vault-index.js +416 -0
- package/router/dist/vault/vault.d.ts +138 -0
- package/router/dist/vault/vault.js +630 -0
- package/runtime/interaction-contract.md +16 -0
- package/skills/bypass/SKILL.md +82 -0
- package/skills/commit-work/SKILL.md +106 -0
- package/skills/commit-work/references/staging-decisions.md +73 -0
- package/skills/decision-gate/SKILL.md +48 -0
- package/skills/design-for-depth/SKILL.md +106 -0
- package/skills/design-for-depth/references/design-pressure-signals.md +314 -0
- package/skills/design-for-depth/references/interface-design-loop.md +156 -0
- package/skills/design-for-depth/references/software-design-philosophy.md +248 -0
- package/skills/diagnose-failure/SKILL.md +105 -0
- package/skills/diagnose-failure/references/diagnostic-loop-catalog.md +36 -0
- package/skills/diagnose-failure/references/flaky-and-performance.md +43 -0
- package/skills/discuss/SKILL.md +121 -0
- package/skills/discuss/references/checkpoints.md +66 -0
- package/skills/evaluate-skill/SKILL.md +105 -0
- package/skills/evaluate-skill/references/definition-schema.md +345 -0
- package/skills/evaluate-skill/references/evaluation-design.md +86 -0
- package/skills/evaluate-skill/references/execution-and-evidence.md +85 -0
- package/skills/evaluate-skill/references/review-and-revision.md +51 -0
- package/skills/evaluate-skill/scripts/lib/definitions.mjs +296 -0
- package/skills/evaluate-skill/scripts/lib/evidence.mjs +46 -0
- package/skills/evaluate-skill/scripts/lib/grade-comparison.mjs +60 -0
- package/skills/evaluate-skill/scripts/lib/grade-interaction.mjs +148 -0
- package/skills/evaluate-skill/scripts/lib/grade-support.mjs +126 -0
- package/skills/evaluate-skill/scripts/lib/grade-workspace.mjs +192 -0
- package/skills/evaluate-skill/scripts/lib/grade.mjs +56 -0
- package/skills/evaluate-skill/scripts/lib/pi.mjs +489 -0
- package/skills/evaluate-skill/scripts/lib/process.mjs +79 -0
- package/skills/evaluate-skill/scripts/lib/rpc.mjs +444 -0
- package/skills/evaluate-skill/scripts/lib/runner.mjs +346 -0
- package/skills/evaluate-skill/scripts/lib/sandbox.mjs +409 -0
- package/skills/evaluate-skill/scripts/lib/view.mjs +337 -0
- package/skills/evaluate-skill/scripts/pi-guard.mjs +164 -0
- package/skills/evaluate-skill/scripts/skill-eval.mjs +101 -0
- package/skills/execute-work/SKILL.md +99 -0
- package/skills/execute-work/references/code-practices.md +78 -0
- package/skills/execute-work/references/execution-loop.md +77 -0
- package/skills/finish-branch/SKILL.md +117 -0
- package/skills/finish-branch/references/integration-options.md +80 -0
- package/skills/handoff/SKILL.md +92 -0
- package/skills/handoff/references/templates.md +141 -0
- package/skills/launch-work/SKILL.md +116 -0
- package/skills/launch-work/references/launch-readiness.md +85 -0
- package/skills/migration-work/SKILL.md +115 -0
- package/skills/migration-work/references/migration-lifecycle.md +82 -0
- package/skills/mode-contract/SKILL.md +82 -0
- package/skills/output-router/SKILL.md +206 -0
- package/skills/output-router/references/safety-policy.md +40 -0
- package/skills/release-work/SKILL.md +118 -0
- package/skills/release-work/references/release-evidence.md +78 -0
- package/skills/review-artifact/SKILL.md +126 -0
- package/skills/review-artifact/references/reviewer-prompt.md +148 -0
- package/skills/review-work/SKILL.md +126 -0
- package/skills/review-work/references/reviewer-prompt.md +134 -0
- package/skills/review-work/references/security-risk-lens.md +67 -0
- package/skills/setup-freeflow/SKILL.md +98 -0
- package/skills/setup-freeflow/references/activation-contract.md +120 -0
- package/skills/setup-freeflow/references/host-setup.md +72 -0
- package/skills/setup-freeflow/references/output-router-setup.md +110 -0
- package/skills/simplify-code/SKILL.md +80 -0
- package/skills/simplify-code/references/simplification-patterns.md +70 -0
- package/skills/tdd/SKILL.md +96 -0
- package/skills/tdd/references/test-design.md +96 -0
- package/skills/track-work/SKILL.md +84 -0
- package/skills/track-work/references/working-record-schema.md +378 -0
- package/skills/verify-work/SKILL.md +85 -0
- package/skills/verify-work/references/browser-runtime-evidence.md +55 -0
- package/skills/verify-work/references/integration-evidence.md +51 -0
- package/skills/verify-work/references/performance-evidence.md +57 -0
- package/skills/workflow/SKILL.md +91 -0
- package/skills/workflow/references/domain-skill-composition.md +57 -0
- package/skills/workflow/references/workflow-loop.md +92 -0
- package/skills/write-plan/SKILL.md +123 -0
- package/skills/write-plan/references/plan-shapes.md +156 -0
- package/skills/write-skill/SKILL.md +119 -0
- package/skills/write-skill/references/activation-boundaries.md +62 -0
- package/skills/write-skill/references/agent-first-instructions.md +87 -0
- package/skills/write-skill/references/progressive-disclosure.md +63 -0
- package/skills/write-skill/scripts/lib/skill-author.mjs +653 -0
- package/skills/write-skill/scripts/skill-author.mjs +104 -0
- package/skills/write-spec/SKILL.md +104 -0
- package/skills/write-spec/references/artifact-standards.md +86 -0
- package/skills/write-spec/references/decision-records.md +77 -0
- package/skills/write-spec/references/spec-shapes.md +128 -0
- package/plugins/freeflow/command-surface.json +0 -77
- package/plugins/freeflow/docs/README.md +0 -40
- package/plugins/freeflow/docs/adr/0001-three-modes.md +0 -27
- package/plugins/freeflow/docs/adr/0002-evals-before-hooks.md +0 -17
- package/plugins/freeflow/docs/adr/0003-release-boundary.md +0 -21
- package/plugins/freeflow/docs/adr/0004-research-replaces-shallow-discovery-skills.md +0 -26
- package/plugins/freeflow/docs/adr/README.md +0 -8
- package/plugins/freeflow/docs/architecture.md +0 -86
- package/plugins/freeflow/docs/output-router.md +0 -212
- package/plugins/freeflow/docs/release-evidence.md +0 -69
- package/plugins/freeflow/docs/skills.md +0 -43
- package/plugins/freeflow/docs/workflow.md +0 -57
- package/plugins/freeflow/hooks/freeflow-runtime-context.mjs +0 -257
- package/plugins/freeflow/pi-extension/index.js +0 -1176
- package/plugins/freeflow/router/dist/benchmark-harness.js +0 -92
- package/plugins/freeflow/router/dist/benchmarks.d.ts +0 -102
- package/plugins/freeflow/router/dist/benchmarks.js +0 -880
- package/plugins/freeflow/router/dist/bounded-evidence.d.ts +0 -40
- package/plugins/freeflow/router/dist/bounded-evidence.js +0 -310
- package/plugins/freeflow/router/dist/codex-qa-benchmarks.d.ts +0 -91
- package/plugins/freeflow/router/dist/codex-qa-benchmarks.js +0 -461
- package/plugins/freeflow/router/dist/command-benchmarks.d.ts +0 -128
- package/plugins/freeflow/router/dist/command-benchmarks.js +0 -652
- package/plugins/freeflow/router/dist/config.d.ts +0 -22
- package/plugins/freeflow/router/dist/config.js +0 -114
- package/plugins/freeflow/router/dist/evidence-range-selector.d.ts +0 -22
- package/plugins/freeflow/router/dist/evidence-range-selector.js +0 -180
- package/plugins/freeflow/router/dist/evidence-search.d.ts +0 -25
- package/plugins/freeflow/router/dist/evidence-search.js +0 -450
- package/plugins/freeflow/router/dist/evidence.js +0 -196
- package/plugins/freeflow/router/dist/experimental-local-index.d.ts +0 -1
- package/plugins/freeflow/router/dist/experimental-local-index.js +0 -1
- package/plugins/freeflow/router/dist/experiments/local-index.d.ts +0 -58
- package/plugins/freeflow/router/dist/experiments/local-index.js +0 -562
- package/plugins/freeflow/router/dist/index-benchmarks.d.ts +0 -84
- package/plugins/freeflow/router/dist/index-benchmarks.js +0 -458
- package/plugins/freeflow/router/dist/index.d.ts +0 -12
- package/plugins/freeflow/router/dist/index.js +0 -12
- package/plugins/freeflow/router/dist/line-ranges.d.ts +0 -22
- package/plugins/freeflow/router/dist/line-ranges.js +0 -22
- package/plugins/freeflow/router/dist/parsers.d.ts +0 -16
- package/plugins/freeflow/router/dist/parsers.js +0 -473
- package/plugins/freeflow/router/dist/repo-traversal.js +0 -242
- package/plugins/freeflow/router/dist/retrieve.d.ts +0 -33
- package/plugins/freeflow/router/dist/retrieve.js +0 -855
- package/plugins/freeflow/router/dist/router-contract.js +0 -54
- package/plugins/freeflow/router/dist/run.d.ts +0 -26
- package/plugins/freeflow/router/dist/run.js +0 -356
- package/plugins/freeflow/router/dist/schema.js +0 -354
- package/plugins/freeflow/router/dist/types.d.ts +0 -234
- package/plugins/freeflow/router/dist/types.js +0 -9
- package/plugins/freeflow/router/dist/vault.d.ts +0 -80
- package/plugins/freeflow/router/dist/vault.js +0 -484
- package/plugins/freeflow/skills/bypass/SKILL.md +0 -68
- package/plugins/freeflow/skills/commit-work/SKILL.md +0 -98
- package/plugins/freeflow/skills/commit-work/references/staging-decisions.md +0 -51
- package/plugins/freeflow/skills/diagnose-failure/SKILL.md +0 -95
- package/plugins/freeflow/skills/diagnose-failure/references/feedback-loop-catalog.md +0 -25
- package/plugins/freeflow/skills/diagnose-failure/references/flaky-and-performance.md +0 -31
- package/plugins/freeflow/skills/evaluate-skill/SKILL.md +0 -96
- package/plugins/freeflow/skills/evaluate-skill/references/eval-patterns.md +0 -41
- package/plugins/freeflow/skills/evaluate-skill/references/grading-priority.md +0 -33
- package/plugins/freeflow/skills/execute-plan/SKILL.md +0 -132
- package/plugins/freeflow/skills/execute-plan/references/execution-map.md +0 -122
- package/plugins/freeflow/skills/handoff/SKILL.md +0 -66
- package/plugins/freeflow/skills/handoff/references/templates.md +0 -64
- package/plugins/freeflow/skills/interview-gate/SKILL.md +0 -90
- package/plugins/freeflow/skills/mode-contract/SKILL.md +0 -79
- package/plugins/freeflow/skills/output-router/SKILL.md +0 -69
- package/plugins/freeflow/skills/output-router/references/safety-policy.md +0 -33
- package/plugins/freeflow/skills/research/ARTIFACT-DESTINATIONS.md +0 -58
- package/plugins/freeflow/skills/research/CHECKPOINTS.md +0 -74
- package/plugins/freeflow/skills/research/LANGUAGE.md +0 -61
- package/plugins/freeflow/skills/research/SKILL.md +0 -128
- package/plugins/freeflow/skills/review-artifact/SKILL.md +0 -115
- package/plugins/freeflow/skills/review-artifact/references/reviewer-prompt.md +0 -56
- package/plugins/freeflow/skills/review-work/SKILL.md +0 -96
- package/plugins/freeflow/skills/review-work/references/reviewer-prompt.md +0 -97
- package/plugins/freeflow/skills/setup-freeflow/SKILL.md +0 -94
- package/plugins/freeflow/skills/setup-freeflow/references/activation-contract.md +0 -99
- package/plugins/freeflow/skills/setup-freeflow/references/host-setup.md +0 -35
- package/plugins/freeflow/skills/setup-freeflow/references/output-router-setup.md +0 -52
- package/plugins/freeflow/skills/verify-work/SKILL.md +0 -62
- package/plugins/freeflow/skills/workflow/SKILL.md +0 -95
- package/plugins/freeflow/skills/workflow/references/workflow-map.md +0 -102
- package/plugins/freeflow/skills/write-plan/SKILL.md +0 -88
- package/plugins/freeflow/skills/write-plan/references/plan-shapes.md +0 -70
- package/plugins/freeflow/skills/write-skill/SKILL.md +0 -88
- package/plugins/freeflow/skills/write-spec/SKILL.md +0 -90
- package/plugins/freeflow/skills/write-spec/references/artifact-standards.md +0 -67
- package/plugins/freeflow/skills/write-spec/references/spec-shapes.md +0 -69
- /package/{plugins/freeflow/assets → assets}/freeflow-plugin-icon-transparent.png +0 -0
- /package/{plugins/freeflow/hooks → hooks}/hooks.json +0 -0
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { freeflowSearch } from "./search.js";
|
|
3
|
+
import { freeflowRun } from "./run.js";
|
|
4
|
+
const DEFAULT_BATCH_CONCURRENCY = 4;
|
|
5
|
+
const MAX_BATCH_CONCURRENCY = 16;
|
|
6
|
+
const MAX_BATCH_STEPS = 50;
|
|
7
|
+
const BATCH_STEP_KINDS = new Set(["run", "search"]);
|
|
8
|
+
const MAX_BATCH_QUERIES = 10;
|
|
9
|
+
const MAX_BATCH_QUERY_LENGTH = 500;
|
|
10
|
+
const MAX_QUERY_MATCHES = 3;
|
|
11
|
+
const QUERY_MATCH_EXCERPT_MAX_BYTES = 1_500;
|
|
12
|
+
export async function freeflowBatch(options, runner) {
|
|
13
|
+
const validation = validateBatchInput(options);
|
|
14
|
+
if (!validation.ok) {
|
|
15
|
+
return batchValidationFailure(options.preserve ?? "important", validation.issues);
|
|
16
|
+
}
|
|
17
|
+
const startedAt = Date.now();
|
|
18
|
+
const steps = await mapWithConcurrency(validation.value.steps, validation.value.concurrency, (step, index) =>
|
|
19
|
+
executeBatchStep({
|
|
20
|
+
step,
|
|
21
|
+
index,
|
|
22
|
+
options: validation.value,
|
|
23
|
+
runner,
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
const failedCount = steps.filter((step) => step.status === "failed").length;
|
|
27
|
+
const okCount = steps.length - failedCount;
|
|
28
|
+
const queryAnswers = await answerBatchQueries(validation.value, steps);
|
|
29
|
+
const routingStatus = failedCount === 0 ? "routed" : okCount === 0 ? "failed" : "partial";
|
|
30
|
+
const durationMs = Date.now() - startedAt;
|
|
31
|
+
const queryReason =
|
|
32
|
+
queryAnswers.length > 0
|
|
33
|
+
? ` Aggregated ${queryAnswers.filter((answer) => answer.status === "answered").length}/${queryAnswers.length} query answer(s) from child evidence handles.`
|
|
34
|
+
: "";
|
|
35
|
+
return {
|
|
36
|
+
toolStatus: failedCount === 0 ? "ok" : "error",
|
|
37
|
+
decisionId: decisionId(
|
|
38
|
+
"batch",
|
|
39
|
+
validation.value.sessionId,
|
|
40
|
+
String(steps.length),
|
|
41
|
+
String(failedCount),
|
|
42
|
+
stepDecisionSeed(steps),
|
|
43
|
+
queryDecisionSeed(queryAnswers),
|
|
44
|
+
),
|
|
45
|
+
preserve: validation.value.preserve,
|
|
46
|
+
producer: { kind: "other", name: "batch" },
|
|
47
|
+
persistence: { status: "not_persisted", recoverability: "none" },
|
|
48
|
+
routing: {
|
|
49
|
+
status: routingStatus,
|
|
50
|
+
route: "batch",
|
|
51
|
+
reason:
|
|
52
|
+
failedCount === 0
|
|
53
|
+
? `Ran ${steps.length} independent Freeflow-owned step(s) with concurrency=${validation.value.concurrency}; child results are available in details.result.steps.${queryReason}`
|
|
54
|
+
: `Ran ${steps.length} independent Freeflow-owned step(s) with concurrency=${validation.value.concurrency}; ${failedCount} step(s) failed and ${okCount} step(s) completed. Child results are available in details.result.steps.${queryReason}`,
|
|
55
|
+
},
|
|
56
|
+
summary: renderBatchSummary({
|
|
57
|
+
okCount,
|
|
58
|
+
stepCount: steps.length,
|
|
59
|
+
durationMs,
|
|
60
|
+
concurrency: validation.value.concurrency,
|
|
61
|
+
queryAnswers,
|
|
62
|
+
}),
|
|
63
|
+
concurrency: validation.value.concurrency,
|
|
64
|
+
stepCount: steps.length,
|
|
65
|
+
okCount,
|
|
66
|
+
failedCount,
|
|
67
|
+
steps,
|
|
68
|
+
...(queryAnswers.length > 0 ? { queries: queryAnswers } : {}),
|
|
69
|
+
recovery: {
|
|
70
|
+
how: "Inspect details.result.steps for each child result. Child run outputs remain recoverable by their own outputId; child search results keep exact path/outputId and line-range recovery hints. Query answers cite matching child evidence handles when present.",
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function validateBatchInput(value) {
|
|
75
|
+
const issues = [];
|
|
76
|
+
if (!isRecord(value)) {
|
|
77
|
+
return { ok: false, issues: [{ path: "$", message: "Expected batch input object." }] };
|
|
78
|
+
}
|
|
79
|
+
if (typeof value.sessionId !== "string" || value.sessionId.length === 0) {
|
|
80
|
+
issues.push({ path: "$.sessionId", message: "Expected non-empty sessionId." });
|
|
81
|
+
}
|
|
82
|
+
if (!Array.isArray(value.steps) || value.steps.length === 0) {
|
|
83
|
+
issues.push({ path: "$.steps", message: "Expected at least one batch step." });
|
|
84
|
+
} else if (value.steps.length > MAX_BATCH_STEPS) {
|
|
85
|
+
issues.push({ path: "$.steps", message: `Expected at most ${MAX_BATCH_STEPS} batch steps.` });
|
|
86
|
+
}
|
|
87
|
+
const concurrency = normalizeConcurrency(value.concurrency);
|
|
88
|
+
if (!concurrency.ok) {
|
|
89
|
+
issues.push({ path: "$.concurrency", message: concurrency.message });
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
value.preserve !== undefined &&
|
|
93
|
+
value.preserve !== "summary" &&
|
|
94
|
+
value.preserve !== "important" &&
|
|
95
|
+
value.preserve !== "full"
|
|
96
|
+
) {
|
|
97
|
+
issues.push({ path: "$.preserve", message: "Expected preserve mode summary, important, or full." });
|
|
98
|
+
}
|
|
99
|
+
const normalizedQueries = normalizeBatchQueries(value.queries, issues);
|
|
100
|
+
const normalizedSteps = [];
|
|
101
|
+
if (Array.isArray(value.steps)) {
|
|
102
|
+
value.steps.forEach((step, index) => {
|
|
103
|
+
if (!isRecord(step)) {
|
|
104
|
+
issues.push({ path: `$.steps[${index}]`, message: "Expected batch step object." });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (typeof step.kind !== "string" || !BATCH_STEP_KINDS.has(step.kind)) {
|
|
108
|
+
issues.push({ path: `$.steps[${index}].kind`, message: "Expected step kind run or search." });
|
|
109
|
+
}
|
|
110
|
+
if (step.id !== undefined && (typeof step.id !== "string" || step.id.length === 0)) {
|
|
111
|
+
issues.push({ path: `$.steps[${index}].id`, message: "Expected non-empty string id when present." });
|
|
112
|
+
}
|
|
113
|
+
if (!isRecord(step.input)) {
|
|
114
|
+
issues.push({ path: `$.steps[${index}].input`, message: "Expected step input object." });
|
|
115
|
+
}
|
|
116
|
+
if (typeof step.kind === "string" && BATCH_STEP_KINDS.has(step.kind) && isRecord(step.input)) {
|
|
117
|
+
normalizedSteps.push({
|
|
118
|
+
id: typeof step.id === "string" && step.id.length > 0 ? step.id : `${step.kind}-${index + 1}`,
|
|
119
|
+
kind: step.kind,
|
|
120
|
+
input: step.input,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
if (issues.length > 0 || !concurrency.ok) {
|
|
126
|
+
return { ok: false, issues };
|
|
127
|
+
}
|
|
128
|
+
const normalized = {
|
|
129
|
+
...value,
|
|
130
|
+
steps: normalizedSteps,
|
|
131
|
+
concurrency: concurrency.value,
|
|
132
|
+
preserve: value.preserve ?? "important",
|
|
133
|
+
queries: normalizedQueries,
|
|
134
|
+
};
|
|
135
|
+
return { ok: true, value: normalized };
|
|
136
|
+
}
|
|
137
|
+
function normalizeConcurrency(value) {
|
|
138
|
+
if (value === undefined) {
|
|
139
|
+
return { ok: true, value: DEFAULT_BATCH_CONCURRENCY };
|
|
140
|
+
}
|
|
141
|
+
if (!Number.isInteger(value) || value < 1 || value > MAX_BATCH_CONCURRENCY) {
|
|
142
|
+
return { ok: false, message: `Expected integer concurrency from 1 to ${MAX_BATCH_CONCURRENCY}.` };
|
|
143
|
+
}
|
|
144
|
+
return { ok: true, value };
|
|
145
|
+
}
|
|
146
|
+
function normalizeBatchQueries(value, issues) {
|
|
147
|
+
if (value === undefined) {
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
if (!Array.isArray(value)) {
|
|
151
|
+
issues.push({ path: "$.queries", message: "Expected queries to be an array of strings when present." });
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
if (value.length > MAX_BATCH_QUERIES) {
|
|
155
|
+
issues.push({ path: "$.queries", message: `Expected at most ${MAX_BATCH_QUERIES} queries.` });
|
|
156
|
+
}
|
|
157
|
+
const normalized = [];
|
|
158
|
+
value.forEach((query, index) => {
|
|
159
|
+
if (typeof query !== "string" || query.trim().length === 0) {
|
|
160
|
+
issues.push({ path: `$.queries[${index}]`, message: "Expected non-empty query string." });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (query.length > MAX_BATCH_QUERY_LENGTH) {
|
|
164
|
+
issues.push({
|
|
165
|
+
path: `$.queries[${index}]`,
|
|
166
|
+
message: `Expected query length at most ${MAX_BATCH_QUERY_LENGTH} characters.`,
|
|
167
|
+
});
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
normalized.push(query.trim());
|
|
171
|
+
});
|
|
172
|
+
return normalized;
|
|
173
|
+
}
|
|
174
|
+
async function executeBatchStep(options) {
|
|
175
|
+
const startedAt = Date.now();
|
|
176
|
+
try {
|
|
177
|
+
const result = await executeStepResult(options.step, options.index, options.options, options.runner);
|
|
178
|
+
return {
|
|
179
|
+
id: options.step.id,
|
|
180
|
+
index: options.index,
|
|
181
|
+
kind: options.step.kind,
|
|
182
|
+
status: isFailedChildResult(result, options.step.kind) ? "failed" : "ok",
|
|
183
|
+
toolStatus: result.toolStatus,
|
|
184
|
+
durationMs: Date.now() - startedAt,
|
|
185
|
+
result,
|
|
186
|
+
};
|
|
187
|
+
} catch (error) {
|
|
188
|
+
return {
|
|
189
|
+
id: options.step.id,
|
|
190
|
+
index: options.index,
|
|
191
|
+
kind: options.step.kind,
|
|
192
|
+
status: "failed",
|
|
193
|
+
durationMs: Date.now() - startedAt,
|
|
194
|
+
error: errorMessage(error),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async function executeStepResult(step, index, options, runner) {
|
|
199
|
+
if (step.kind === "run") {
|
|
200
|
+
return freeflowRun(
|
|
201
|
+
{
|
|
202
|
+
...step.input,
|
|
203
|
+
preserve: step.input.preserve ?? options.preserve,
|
|
204
|
+
sessionId: options.sessionId,
|
|
205
|
+
...(options.vaultRoot !== undefined ? { vaultRoot: options.vaultRoot } : {}),
|
|
206
|
+
...(options.vaultRetention !== undefined ? { vaultRetention: options.vaultRetention } : {}),
|
|
207
|
+
...(options.thresholds !== undefined ? { thresholds: options.thresholds } : {}),
|
|
208
|
+
...(options.scriptTransform !== undefined ? { scriptTransform: options.scriptTransform } : {}),
|
|
209
|
+
...(options.scriptSandboxAdapters !== undefined
|
|
210
|
+
? { scriptSandboxAdapters: options.scriptSandboxAdapters }
|
|
211
|
+
: {}),
|
|
212
|
+
...(options.storagePolicy !== undefined ? { storagePolicy: options.storagePolicy } : {}),
|
|
213
|
+
},
|
|
214
|
+
runner,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
return freeflowSearch({
|
|
218
|
+
...step.input,
|
|
219
|
+
preserve: step.input.preserve ?? options.preserve,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
function isFailedChildResult(result, kind) {
|
|
223
|
+
if (result.toolStatus === "error" || result.routing?.status === "failed") {
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
if (kind === "run" && "execution" in result && result.execution?.status !== "success") {
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
async function answerBatchQueries(options, steps) {
|
|
232
|
+
if (options.queries.length === 0) {
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
const answers = [];
|
|
236
|
+
for (const query of options.queries) {
|
|
237
|
+
const matches = await collectBatchQueryMatches(options, steps, query);
|
|
238
|
+
const selected = matches.sort((a, b) => b.score - a.score).slice(0, MAX_QUERY_MATCHES);
|
|
239
|
+
answers.push({
|
|
240
|
+
query,
|
|
241
|
+
status: selected.length > 0 ? "answered" : "no_match",
|
|
242
|
+
summary: renderBatchQueryAnswer(query, selected),
|
|
243
|
+
matches: selected,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
return answers;
|
|
247
|
+
}
|
|
248
|
+
async function collectBatchQueryMatches(options, steps, query) {
|
|
249
|
+
const matches = [];
|
|
250
|
+
const seenVaultQueries = new Set();
|
|
251
|
+
const tokens = tokenizeQuery(query);
|
|
252
|
+
for (const step of steps) {
|
|
253
|
+
const result = step.result;
|
|
254
|
+
if (!result) {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
collectStructuredQueryMatches(matches, step, result, query, tokens);
|
|
258
|
+
const refs = vaultQueryRefsForResult(result);
|
|
259
|
+
if (!options.vaultRoot) {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
for (const ref of refs) {
|
|
263
|
+
const key = `${ref.outputId}:${ref.stream}`;
|
|
264
|
+
if (seenVaultQueries.has(`${query}:${key}`)) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
seenVaultQueries.add(`${query}:${key}`);
|
|
268
|
+
const routed = await freeflowSearch({
|
|
269
|
+
action: "query",
|
|
270
|
+
source: {
|
|
271
|
+
kind: "vault",
|
|
272
|
+
root: options.vaultRoot,
|
|
273
|
+
sessionId: options.sessionId,
|
|
274
|
+
outputId: ref.outputId,
|
|
275
|
+
stream: ref.stream,
|
|
276
|
+
},
|
|
277
|
+
query,
|
|
278
|
+
topK: 1,
|
|
279
|
+
preserve: "summary",
|
|
280
|
+
});
|
|
281
|
+
for (const packet of routed.evidence ?? []) {
|
|
282
|
+
addEvidenceQueryMatch(matches, step, packet, query, tokens, `vault ${ref.outputId}:${ref.stream}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return dedupeQueryMatches(matches);
|
|
287
|
+
}
|
|
288
|
+
function collectStructuredQueryMatches(matches, step, result, query, tokens) {
|
|
289
|
+
for (const packet of result.evidence ?? []) {
|
|
290
|
+
addEvidenceQueryMatch(matches, step, packet, query, tokens, "child evidence");
|
|
291
|
+
}
|
|
292
|
+
if (isCommandLikeResult(result) && Array.isArray(result.importantLines)) {
|
|
293
|
+
for (const line of result.importantLines) {
|
|
294
|
+
addImportantLineQueryMatch(matches, step, result.outputId, line, query, tokens);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function addEvidenceQueryMatch(matches, step, packet, query, tokens, sourceLabel) {
|
|
299
|
+
const score = scoreQueryText(packet.excerpt, query, tokens);
|
|
300
|
+
if (score <= 0) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
matches.push({
|
|
304
|
+
stepId: step.id,
|
|
305
|
+
stepIndex: step.index,
|
|
306
|
+
stepKind: step.kind,
|
|
307
|
+
source: packet.source,
|
|
308
|
+
excerpt: selectQueryExcerpt(packet.excerpt, tokens),
|
|
309
|
+
why: `Matched ${sourceLabel}${packet.lines ? ` lines ${packet.lines}` : ""}.`,
|
|
310
|
+
score,
|
|
311
|
+
...(packet.source.kind === "vault" ? { outputId: packet.source.outputId } : {}),
|
|
312
|
+
...(packet.id ? { evidenceId: packet.id } : {}),
|
|
313
|
+
...(packet.lines ? { lines: packet.lines } : {}),
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
function addImportantLineQueryMatch(matches, step, outputId, line, query, tokens) {
|
|
317
|
+
const score = scoreQueryText(line.excerpt, query, tokens);
|
|
318
|
+
if (score <= 0) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
matches.push({
|
|
322
|
+
stepId: step.id,
|
|
323
|
+
stepIndex: step.index,
|
|
324
|
+
stepKind: step.kind,
|
|
325
|
+
source: { kind: "vault", outputId, stream: line.stream },
|
|
326
|
+
excerpt: selectQueryExcerpt(line.excerpt, tokens),
|
|
327
|
+
why: `Matched selected child command evidence lines ${line.lines}.`,
|
|
328
|
+
score,
|
|
329
|
+
outputId,
|
|
330
|
+
lines: line.lines,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
function vaultQueryRefsForResult(result) {
|
|
334
|
+
const refs = [];
|
|
335
|
+
if (isCommandLikeResult(result)) {
|
|
336
|
+
if (result.persistence?.recoverability === "exact") {
|
|
337
|
+
refs.push({ outputId: result.outputId, stream: "combined" });
|
|
338
|
+
}
|
|
339
|
+
if (result.recovery?.outputId && result.recovery.outputId !== result.outputId) {
|
|
340
|
+
refs.push({ outputId: result.recovery.outputId, stream: "combined" });
|
|
341
|
+
}
|
|
342
|
+
if (result.reducer?.outputId) {
|
|
343
|
+
refs.push({ outputId: result.reducer.outputId, stream: "raw" });
|
|
344
|
+
}
|
|
345
|
+
if (result.scriptFilter?.outputId) {
|
|
346
|
+
refs.push({ outputId: result.scriptFilter.outputId, stream: "raw" });
|
|
347
|
+
}
|
|
348
|
+
return refs;
|
|
349
|
+
}
|
|
350
|
+
if (hasOutputId(result)) {
|
|
351
|
+
refs.push({ outputId: result.outputId, stream: "raw" });
|
|
352
|
+
}
|
|
353
|
+
const recoveryOutputId = recoveryOutputIdFor(result.recovery);
|
|
354
|
+
if (recoveryOutputId && !refs.some((ref) => ref.outputId === recoveryOutputId)) {
|
|
355
|
+
refs.push({ outputId: recoveryOutputId, stream: "raw" });
|
|
356
|
+
}
|
|
357
|
+
return refs;
|
|
358
|
+
}
|
|
359
|
+
function renderBatchSummary(options) {
|
|
360
|
+
const lines = [
|
|
361
|
+
`Batch completed ${options.okCount}/${options.stepCount} step(s) successfully in ${options.durationMs}ms with concurrency=${options.concurrency}.`,
|
|
362
|
+
];
|
|
363
|
+
if (options.queryAnswers.length > 0) {
|
|
364
|
+
lines.push(renderBatchQueryAnswers(options.queryAnswers));
|
|
365
|
+
}
|
|
366
|
+
return lines.join("\n");
|
|
367
|
+
}
|
|
368
|
+
function renderBatchQueryAnswers(answers) {
|
|
369
|
+
const lines = ["query answers:"];
|
|
370
|
+
for (const answer of answers) {
|
|
371
|
+
lines.push(answer.summary);
|
|
372
|
+
}
|
|
373
|
+
return lines.join("\n");
|
|
374
|
+
}
|
|
375
|
+
function renderBatchQueryAnswer(query, matches) {
|
|
376
|
+
if (matches.length === 0) {
|
|
377
|
+
return `- ${query}: no deterministic match in child evidence handles`;
|
|
378
|
+
}
|
|
379
|
+
const renderedMatches = matches.map((match) => `${match.stepId}: ${oneLine(match.excerpt, 500)}`);
|
|
380
|
+
return `- ${query}: ${renderedMatches.join(" | ")}`;
|
|
381
|
+
}
|
|
382
|
+
function dedupeQueryMatches(matches) {
|
|
383
|
+
const deduped = [];
|
|
384
|
+
const seen = new Set();
|
|
385
|
+
for (const match of matches) {
|
|
386
|
+
const key = `${match.stepId}:${sourceKey(match.source)}:${match.lines ?? ""}:${match.excerpt}`;
|
|
387
|
+
if (seen.has(key)) {
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
seen.add(key);
|
|
391
|
+
deduped.push(match);
|
|
392
|
+
}
|
|
393
|
+
return deduped;
|
|
394
|
+
}
|
|
395
|
+
function sourceKey(source) {
|
|
396
|
+
if (source.kind === "repo") {
|
|
397
|
+
return `repo:${source.path}`;
|
|
398
|
+
}
|
|
399
|
+
if (source.kind === "vault") {
|
|
400
|
+
return `vault:${source.outputId}:${source.stream ?? ""}`;
|
|
401
|
+
}
|
|
402
|
+
if (source.kind === "local") {
|
|
403
|
+
return `local:${source.root}:${source.path}`;
|
|
404
|
+
}
|
|
405
|
+
return `native:${source.tool}:${source.outputId}`;
|
|
406
|
+
}
|
|
407
|
+
function tokenizeQuery(query) {
|
|
408
|
+
return Array.from(
|
|
409
|
+
new Set(
|
|
410
|
+
query
|
|
411
|
+
.toLowerCase()
|
|
412
|
+
.split(/[^a-z0-9_./:-]+/)
|
|
413
|
+
.filter((token) => token.length >= 2 && !BATCH_QUERY_STOPWORDS.has(token)),
|
|
414
|
+
),
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
const BATCH_QUERY_STOPWORDS = new Set([
|
|
418
|
+
"a",
|
|
419
|
+
"an",
|
|
420
|
+
"and",
|
|
421
|
+
"are",
|
|
422
|
+
"as",
|
|
423
|
+
"at",
|
|
424
|
+
"be",
|
|
425
|
+
"by",
|
|
426
|
+
"for",
|
|
427
|
+
"from",
|
|
428
|
+
"in",
|
|
429
|
+
"is",
|
|
430
|
+
"it",
|
|
431
|
+
"of",
|
|
432
|
+
"on",
|
|
433
|
+
"or",
|
|
434
|
+
"that",
|
|
435
|
+
"the",
|
|
436
|
+
"this",
|
|
437
|
+
"to",
|
|
438
|
+
"with",
|
|
439
|
+
]);
|
|
440
|
+
function scoreQueryText(text, query, tokens) {
|
|
441
|
+
const lowerText = text.toLowerCase();
|
|
442
|
+
const normalizedQuery = query.toLowerCase();
|
|
443
|
+
let score = lowerText.includes(normalizedQuery) ? 1_000 : 0;
|
|
444
|
+
for (const token of tokens) {
|
|
445
|
+
if (lowerText.includes(token)) {
|
|
446
|
+
score += 100 + countOccurrences(lowerText, token);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return score;
|
|
450
|
+
}
|
|
451
|
+
function selectQueryExcerpt(text, tokens) {
|
|
452
|
+
const lines = splitLines(text);
|
|
453
|
+
if (lines.length <= 8) {
|
|
454
|
+
return truncateBytes(text, QUERY_MATCH_EXCERPT_MAX_BYTES);
|
|
455
|
+
}
|
|
456
|
+
const matchingIndexes = lines
|
|
457
|
+
.map((line, index) => ({ line: line.toLowerCase(), index }))
|
|
458
|
+
.filter(({ line }) => tokens.some((token) => line.includes(token)))
|
|
459
|
+
.map(({ index }) => index);
|
|
460
|
+
if (matchingIndexes.length === 0) {
|
|
461
|
+
return truncateBytes(lines.slice(0, 8).join("\n"), QUERY_MATCH_EXCERPT_MAX_BYTES);
|
|
462
|
+
}
|
|
463
|
+
const selected = new Set();
|
|
464
|
+
for (const index of matchingIndexes) {
|
|
465
|
+
selected.add(Math.max(0, index - 1));
|
|
466
|
+
selected.add(index);
|
|
467
|
+
selected.add(Math.min(lines.length - 1, index + 1));
|
|
468
|
+
}
|
|
469
|
+
const sorted = Array.from(selected).sort((a, b) => a - b);
|
|
470
|
+
const capped = sorted.length > 24 ? [...sorted.slice(0, 12), ...sorted.slice(-12)] : sorted;
|
|
471
|
+
const excerptLines = [];
|
|
472
|
+
let previous = -1;
|
|
473
|
+
for (const index of capped) {
|
|
474
|
+
if (previous >= 0 && index > previous + 1) {
|
|
475
|
+
excerptLines.push("…");
|
|
476
|
+
}
|
|
477
|
+
excerptLines.push(lines[index] ?? "");
|
|
478
|
+
previous = index;
|
|
479
|
+
}
|
|
480
|
+
return truncateBytes(excerptLines.join("\n"), QUERY_MATCH_EXCERPT_MAX_BYTES);
|
|
481
|
+
}
|
|
482
|
+
function isCommandLikeResult(result) {
|
|
483
|
+
return typeof result.outputId === "string" && "execution" in result;
|
|
484
|
+
}
|
|
485
|
+
function hasOutputId(result) {
|
|
486
|
+
return typeof result.outputId === "string";
|
|
487
|
+
}
|
|
488
|
+
function recoveryOutputIdFor(recovery) {
|
|
489
|
+
return typeof recovery?.outputId === "string" && recovery.outputId.length > 0 ? recovery.outputId : undefined;
|
|
490
|
+
}
|
|
491
|
+
function splitLines(text) {
|
|
492
|
+
return text.split(/\r?\n/);
|
|
493
|
+
}
|
|
494
|
+
function countOccurrences(text, token) {
|
|
495
|
+
let count = 0;
|
|
496
|
+
let index = text.indexOf(token);
|
|
497
|
+
while (index !== -1) {
|
|
498
|
+
count += 1;
|
|
499
|
+
index = text.indexOf(token, index + token.length);
|
|
500
|
+
}
|
|
501
|
+
return count;
|
|
502
|
+
}
|
|
503
|
+
function oneLine(text, maxBytes) {
|
|
504
|
+
return truncateBytes(text.replace(/\s+/g, " ").trim(), maxBytes);
|
|
505
|
+
}
|
|
506
|
+
function truncateBytes(text, maxBytes) {
|
|
507
|
+
if (Buffer.byteLength(text, "utf8") <= maxBytes) {
|
|
508
|
+
return text;
|
|
509
|
+
}
|
|
510
|
+
let result = "";
|
|
511
|
+
for (const char of text) {
|
|
512
|
+
const candidate = `${result}${char}`;
|
|
513
|
+
if (Buffer.byteLength(`${candidate}…`, "utf8") > maxBytes) {
|
|
514
|
+
return `${result}…`;
|
|
515
|
+
}
|
|
516
|
+
result = candidate;
|
|
517
|
+
}
|
|
518
|
+
return result;
|
|
519
|
+
}
|
|
520
|
+
function queryDecisionSeed(answers) {
|
|
521
|
+
return answers
|
|
522
|
+
.map(
|
|
523
|
+
(answer) =>
|
|
524
|
+
`${answer.query}:${answer.status}:${answer.matches.map((match) => `${match.stepId}:${match.score}:${match.lines ?? ""}`).join(",")}`,
|
|
525
|
+
)
|
|
526
|
+
.join("|");
|
|
527
|
+
}
|
|
528
|
+
async function mapWithConcurrency(items, concurrency, worker) {
|
|
529
|
+
const results = new Array(items.length);
|
|
530
|
+
let nextIndex = 0;
|
|
531
|
+
const workers = Array.from({ length: Math.min(concurrency, items.length) }, async () => {
|
|
532
|
+
while (nextIndex < items.length) {
|
|
533
|
+
const index = nextIndex;
|
|
534
|
+
nextIndex += 1;
|
|
535
|
+
const item = items[index];
|
|
536
|
+
if (item !== undefined) {
|
|
537
|
+
results[index] = await worker(item, index);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
await Promise.all(workers);
|
|
542
|
+
return results;
|
|
543
|
+
}
|
|
544
|
+
function batchValidationFailure(preserve, issues) {
|
|
545
|
+
return {
|
|
546
|
+
toolStatus: "error",
|
|
547
|
+
decisionId: decisionId("batch-validation", validationMessage(issues)),
|
|
548
|
+
preserve,
|
|
549
|
+
producer: { kind: "other", name: "batch" },
|
|
550
|
+
persistence: { status: "not_persisted", recoverability: "none" },
|
|
551
|
+
routing: {
|
|
552
|
+
status: "failed",
|
|
553
|
+
route: "batch",
|
|
554
|
+
reason: `Invalid freeflow_batch input: ${validationMessage(issues)}`,
|
|
555
|
+
},
|
|
556
|
+
summary: "Batch was not executed because the input was invalid.",
|
|
557
|
+
concurrency: 0,
|
|
558
|
+
stepCount: 0,
|
|
559
|
+
okCount: 0,
|
|
560
|
+
failedCount: 0,
|
|
561
|
+
steps: [],
|
|
562
|
+
recovery: {
|
|
563
|
+
how: "No batch steps were executed. Fix the batch schema and rerun.",
|
|
564
|
+
},
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
function stepDecisionSeed(steps) {
|
|
568
|
+
return steps
|
|
569
|
+
.map((step) => `${step.index}:${step.kind}:${step.status}:${step.result?.decisionId ?? step.error ?? "none"}`)
|
|
570
|
+
.join("|");
|
|
571
|
+
}
|
|
572
|
+
function validationMessage(issues) {
|
|
573
|
+
return issues.map((issue) => `${issue.path}: ${issue.message}`).join("; ");
|
|
574
|
+
}
|
|
575
|
+
function isRecord(value) {
|
|
576
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
577
|
+
}
|
|
578
|
+
function decisionId(...parts) {
|
|
579
|
+
return `ffdec_${hash(parts.join("\0")).slice(0, 16)}`;
|
|
580
|
+
}
|
|
581
|
+
function hash(value) {
|
|
582
|
+
return createHash("sha256").update(value).digest("hex");
|
|
583
|
+
}
|
|
584
|
+
function errorMessage(error) {
|
|
585
|
+
return error instanceof Error ? error.message : String(error);
|
|
586
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type ScriptTransformLimitsInput } from "../transform/engine.js";
|
|
2
|
+
import { type RunOutputFiltersInput } from "../routing/run-filters.js";
|
|
3
|
+
import type {
|
|
4
|
+
CommandRoutedResult,
|
|
5
|
+
ExecutionStatus,
|
|
6
|
+
PreserveMode,
|
|
7
|
+
RouterThresholds,
|
|
8
|
+
ScriptTransformConfig,
|
|
9
|
+
ScriptTransformLanguage,
|
|
10
|
+
StoragePolicyMode,
|
|
11
|
+
VaultRetentionPolicy,
|
|
12
|
+
} from "../config/types.js";
|
|
13
|
+
import type { ScriptSandboxAdapter } from "../sandbox/script-sandbox.js";
|
|
14
|
+
export interface HostCommandRunRequest {
|
|
15
|
+
command: string | readonly string[];
|
|
16
|
+
cwd?: string;
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface HostCommandRunResult {
|
|
20
|
+
stdout: string;
|
|
21
|
+
stderr: string;
|
|
22
|
+
executionStatus: ExecutionStatus;
|
|
23
|
+
exitCode: number | null;
|
|
24
|
+
combined?: string;
|
|
25
|
+
durationMs?: number;
|
|
26
|
+
}
|
|
27
|
+
export interface HostCommandRunner {
|
|
28
|
+
run(request: HostCommandRunRequest): Promise<HostCommandRunResult>;
|
|
29
|
+
}
|
|
30
|
+
export interface RunScriptProducerInput {
|
|
31
|
+
language: ScriptTransformLanguage;
|
|
32
|
+
code: string;
|
|
33
|
+
label?: string;
|
|
34
|
+
limits?: ScriptTransformLimitsInput;
|
|
35
|
+
}
|
|
36
|
+
export interface RunScriptFilterInput {
|
|
37
|
+
language: ScriptTransformLanguage;
|
|
38
|
+
code: string;
|
|
39
|
+
label?: string;
|
|
40
|
+
limits?: ScriptTransformLimitsInput;
|
|
41
|
+
}
|
|
42
|
+
export interface FreeflowRunOptions {
|
|
43
|
+
command?: string | readonly string[];
|
|
44
|
+
script?: RunScriptProducerInput;
|
|
45
|
+
cwd?: string;
|
|
46
|
+
timeoutMs?: number;
|
|
47
|
+
sessionId: string;
|
|
48
|
+
vaultRoot?: string;
|
|
49
|
+
vaultRetention?: VaultRetentionPolicy;
|
|
50
|
+
preserve?: PreserveMode;
|
|
51
|
+
goal?: string;
|
|
52
|
+
thresholds?: Partial<RouterThresholds>;
|
|
53
|
+
filters?: RunOutputFiltersInput;
|
|
54
|
+
scriptFilter?: RunScriptFilterInput;
|
|
55
|
+
scriptTransform?: ScriptTransformConfig;
|
|
56
|
+
scriptSandboxAdapters?: readonly ScriptSandboxAdapter[];
|
|
57
|
+
storagePolicy?: StoragePolicyMode;
|
|
58
|
+
}
|
|
59
|
+
export declare function freeflowRun(
|
|
60
|
+
options: FreeflowRunOptions,
|
|
61
|
+
runner: HostCommandRunner,
|
|
62
|
+
): Promise<CommandRoutedResult>;
|