@leitwerk-dev/server 0.3.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/dist/{app-WfwKzrVk.d.ts → app-CsuxFZT5.d.ts} +332 -111
- package/dist/app-JM4CBTND.js +8 -0
- package/dist/app.d.ts +9 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/auth/fastify-auth.d.ts +9 -0
- package/dist/auth/fastify-auth.d.ts.map +1 -1
- package/dist/{chunk-LK3RO6C7.js → chunk-FPMDWQF7.js} +3050 -645
- package/dist/chunk-FPMDWQF7.js.map +1 -0
- package/dist/{chunk-KIOQNNG6.js → chunk-MBHRCQFE.js} +2781 -1822
- package/dist/chunk-MBHRCQFE.js.map +1 -0
- package/dist/config/config-loader.d.ts +1 -1
- package/dist/config/config-loader.d.ts.map +1 -1
- package/dist/config/config-types.d.ts +14 -1
- package/dist/config/config-types.d.ts.map +1 -1
- package/dist/config/kubernetes-docker-isolation.d.ts +7 -0
- package/dist/config/kubernetes-docker-isolation.d.ts.map +1 -0
- package/dist/db/database.d.ts.map +1 -1
- package/dist/db/execution-inspection-repo.d.ts +15 -0
- package/dist/db/execution-inspection-repo.d.ts.map +1 -0
- package/dist/db/history-window.d.ts +11 -0
- package/dist/db/history-window.d.ts.map +1 -0
- package/dist/db/mapped-llm-run-repo.d.ts +56 -0
- package/dist/db/mapped-llm-run-repo.d.ts.map +1 -0
- package/dist/db/process-event-repo.d.ts +2 -0
- package/dist/db/process-event-repo.d.ts.map +1 -1
- package/dist/db/process-instance-repo.d.ts +4 -1
- package/dist/db/process-instance-repo.d.ts.map +1 -1
- package/dist/db/process-leaf-outcome-snapshot-repo.d.ts +2 -1
- package/dist/db/process-leaf-outcome-snapshot-repo.d.ts.map +1 -1
- package/dist/db/process-project-repo.d.ts +2 -0
- package/dist/db/process-project-repo.d.ts.map +1 -1
- package/dist/db/process-skill-repo.d.ts.map +1 -1
- package/dist/db/process-turn-annotation-repo.d.ts +4 -1
- package/dist/db/process-turn-annotation-repo.d.ts.map +1 -1
- package/dist/db/process-turn-record-repo.d.ts +7 -1
- package/dist/db/process-turn-record-repo.d.ts.map +1 -1
- package/dist/db/publication-repo.d.ts +5 -0
- package/dist/db/publication-repo.d.ts.map +1 -0
- package/dist/db/repo-helpers.d.ts +6 -0
- package/dist/db/repo-helpers.d.ts.map +1 -1
- package/dist/db/repositories.d.ts +16 -0
- package/dist/db/repositories.d.ts.map +1 -1
- package/dist/db/schema.d.ts +1177 -71
- package/dist/db/schema.d.ts.map +1 -1
- package/dist/db/scoped-settings-repo.d.ts +37 -0
- package/dist/db/scoped-settings-repo.d.ts.map +1 -0
- package/dist/db/skill-repo.d.ts +3 -0
- package/dist/db/skill-repo.d.ts.map +1 -1
- package/dist/db/turn-start-record-repo.d.ts +2 -1
- package/dist/db/turn-start-record-repo.d.ts.map +1 -1
- package/dist/db/turn-summary-repo.d.ts +1 -1
- package/dist/db/turn-summary-repo.d.ts.map +1 -1
- package/dist/db/worker-lease-repo.d.ts +2 -1
- package/dist/db/worker-lease-repo.d.ts.map +1 -1
- package/dist/domain-logic/outcome-tools.d.ts.map +1 -1
- package/dist/external-source-service.d.ts.map +1 -1
- package/dist/future-execution/execution.d.ts +4 -1
- package/dist/future-execution/execution.d.ts.map +1 -1
- package/dist/future-execution/launch-lifecycle.d.ts +9 -25
- package/dist/future-execution/launch-lifecycle.d.ts.map +1 -1
- package/dist/future-execution/lifecycle.d.ts +3 -28
- package/dist/future-execution/lifecycle.d.ts.map +1 -1
- package/dist/future-execution/model-projection.d.ts.map +1 -1
- package/dist/future-execution/reconciliation.d.ts +1 -0
- package/dist/future-execution/reconciliation.d.ts.map +1 -1
- package/dist/future-execution-presenter.d.ts +4 -14
- package/dist/future-execution-presenter.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/integration-tool-registry.d.ts +11 -2
- package/dist/integration-tool-registry.d.ts.map +1 -1
- package/dist/launch-coordinator-test-harness.d.ts.map +1 -1
- package/dist/launch-coordinator.d.ts.map +1 -1
- package/dist/launcher-model-config-service.d.ts +7 -1
- package/dist/launcher-model-config-service.d.ts.map +1 -1
- package/dist/leaf-outcome-snapshot-capture.d.ts +1 -0
- package/dist/leaf-outcome-snapshot-capture.d.ts.map +1 -1
- package/dist/main.js +3 -3
- package/dist/primary-path-snapshot.d.ts.map +1 -1
- package/dist/process-action-presenter.d.ts +11 -0
- package/dist/process-action-presenter.d.ts.map +1 -0
- package/dist/process-action-registry.d.ts.map +1 -1
- package/dist/process-engine/decision.d.ts +2 -0
- package/dist/process-engine/decision.d.ts.map +1 -1
- package/dist/process-engine/engine.d.ts.map +1 -1
- package/dist/process-engine/mapped-turns.d.ts +45 -0
- package/dist/process-engine/mapped-turns.d.ts.map +1 -0
- package/dist/process-engine/operation.d.ts +1 -1
- package/dist/process-engine/operation.d.ts.map +1 -1
- package/dist/process-engine/ops/accept-worker-turn-start.d.ts +2 -0
- package/dist/process-engine/ops/accept-worker-turn-start.d.ts.map +1 -1
- package/dist/process-engine/ops/continue-failed-turn.d.ts.map +1 -1
- package/dist/process-engine/ops/process-observation.d.ts +6 -0
- package/dist/process-engine/ops/process-observation.d.ts.map +1 -0
- package/dist/process-engine/ops/retry-failed-turn.d.ts.map +1 -1
- package/dist/process-engine/ops/retry-startup.d.ts.map +1 -1
- package/dist/process-engine/ops/start-process.d.ts +2 -0
- package/dist/process-engine/ops/start-process.d.ts.map +1 -1
- package/dist/process-engine/ops/turn-outcome.d.ts.map +1 -1
- package/dist/process-engine/ops/turn-wait.d.ts +30 -0
- package/dist/process-engine/ops/turn-wait.d.ts.map +1 -0
- package/dist/process-engine/ops/update-model-config.d.ts +9 -0
- package/dist/process-engine/ops/update-model-config.d.ts.map +1 -0
- package/dist/process-engine/recorder.d.ts.map +1 -1
- package/dist/process-engine/turn-start-preflight.d.ts +6 -0
- package/dist/process-engine/turn-start-preflight.d.ts.map +1 -1
- package/dist/process-engine/turn-wait-state.d.ts +24 -0
- package/dist/process-engine/turn-wait-state.d.ts.map +1 -0
- package/dist/process-engine/turn-worker-requirement.d.ts +2 -0
- package/dist/process-engine/turn-worker-requirement.d.ts.map +1 -1
- package/dist/process-engine/types.d.ts +10 -1
- package/dist/process-engine/types.d.ts.map +1 -1
- package/dist/process-engine/writes/build-process-abort-writes.d.ts +3 -1
- package/dist/process-engine/writes/build-process-abort-writes.d.ts.map +1 -1
- package/dist/process-engine/writes/build-recovery-start-writes.d.ts.map +1 -1
- package/dist/process-engine/writes/build-turn-outcome-writes.d.ts +10 -1
- package/dist/process-engine/writes/build-turn-outcome-writes.d.ts.map +1 -1
- package/dist/process-engine/writes/build-turn-selection-writes.d.ts.map +1 -1
- package/dist/process-engine/writes/commit-writes.d.ts +2 -0
- package/dist/process-engine/writes/commit-writes.d.ts.map +1 -1
- package/dist/process-engine/writes/reserve-selected-turn-start.d.ts +5 -0
- package/dist/process-engine/writes/reserve-selected-turn-start.d.ts.map +1 -0
- package/dist/process-engine/writes/writes.d.ts +35 -49
- package/dist/process-engine/writes/writes.d.ts.map +1 -1
- package/dist/process-graph.d.ts +1 -1
- package/dist/process-graph.d.ts.map +1 -1
- package/dist/process-inspection-reader.d.ts +21 -0
- package/dist/process-inspection-reader.d.ts.map +1 -0
- package/dist/process-inspection-trace.d.ts +13 -0
- package/dist/process-inspection-trace.d.ts.map +1 -0
- package/dist/process-inspection.d.ts +22 -0
- package/dist/process-inspection.d.ts.map +1 -0
- package/dist/process-instance-tree-presenter.d.ts +4 -3
- package/dist/process-instance-tree-presenter.d.ts.map +1 -1
- package/dist/process-launch-executor.d.ts +2 -0
- package/dist/process-launch-executor.d.ts.map +1 -1
- package/dist/process-launch-plan.d.ts +1 -0
- package/dist/process-launch-plan.d.ts.map +1 -1
- package/dist/process-launcher-registry.d.ts.map +1 -1
- package/dist/process-model-config.d.ts +19 -0
- package/dist/process-model-config.d.ts.map +1 -0
- package/dist/process-model-policy/construct.d.ts.map +1 -1
- package/dist/process-model-policy/evaluate.d.ts.map +1 -1
- package/dist/process-model-policy/index.d.ts +4 -1
- package/dist/process-model-policy/index.d.ts.map +1 -1
- package/dist/process-model-policy/project.d.ts.map +1 -1
- package/dist/process-model-policy/types.d.ts +18 -0
- package/dist/process-model-policy/types.d.ts.map +1 -1
- package/dist/process-model-policy-presenter.d.ts.map +1 -1
- package/dist/process-runtime-availability.d.ts +2 -1
- package/dist/process-runtime-availability.d.ts.map +1 -1
- package/dist/process-turn-navigation.d.ts +12 -2
- package/dist/process-turn-navigation.d.ts.map +1 -1
- package/dist/process-turn-trace.d.ts.map +1 -1
- package/dist/process-ui-snapshot-presenter.d.ts +15 -3
- package/dist/process-ui-snapshot-presenter.d.ts.map +1 -1
- package/dist/routes/future-executions.d.ts.map +1 -1
- package/dist/routes/launchers.d.ts.map +1 -1
- package/dist/routes/process-actions.d.ts.map +1 -1
- package/dist/routes/process-detail.d.ts.map +1 -1
- package/dist/routes/process-diagnostics-assembler.d.ts +3 -1
- package/dist/routes/process-diagnostics-assembler.d.ts.map +1 -1
- package/dist/routes/process-model-config.d.ts +4 -0
- package/dist/routes/process-model-config.d.ts.map +1 -0
- package/dist/routes/process-primary-path-assembler.d.ts +4 -12
- package/dist/routes/process-primary-path-assembler.d.ts.map +1 -1
- package/dist/routes/process-route-helpers.d.ts +15 -41
- package/dist/routes/process-route-helpers.d.ts.map +1 -1
- package/dist/routes/processes.d.ts.map +1 -1
- package/dist/routes/settings.d.ts +11 -0
- package/dist/routes/settings.d.ts.map +1 -0
- package/dist/routes/ticket-creation.d.ts.map +1 -1
- package/dist/scoped-settings-service.d.ts +83 -0
- package/dist/scoped-settings-service.d.ts.map +1 -0
- package/dist/server-bootstrap/build-host-capabilities.d.ts +2 -0
- package/dist/server-bootstrap/build-host-capabilities.d.ts.map +1 -1
- package/dist/server-bootstrap/register-http.d.ts.map +1 -1
- package/dist/session-summary-projection.d.ts.map +1 -1
- package/dist/skills/bundled-skills.d.ts +5 -0
- package/dist/skills/bundled-skills.d.ts.map +1 -0
- package/dist/skills/catalog-service.d.ts +1 -0
- package/dist/skills/catalog-service.d.ts.map +1 -1
- package/dist/skills/extension-importer.d.ts +11 -0
- package/dist/skills/extension-importer.d.ts.map +1 -0
- package/dist/skills/launch-skills.d.ts +11 -0
- package/dist/skills/launch-skills.d.ts.map +1 -0
- package/dist/skills/skill-frontmatter.d.ts +5 -0
- package/dist/skills/skill-frontmatter.d.ts.map +1 -1
- package/dist/skills/source-importer.d.ts +3 -0
- package/dist/skills/source-importer.d.ts.map +1 -1
- package/dist/startup-evidence.d.ts +3 -0
- package/dist/startup-evidence.d.ts.map +1 -1
- package/dist/supervisor/execution-inspection-ingestor.d.ts +9 -0
- package/dist/supervisor/execution-inspection-ingestor.d.ts.map +1 -0
- package/dist/supervisor/ipc-handler.d.ts +2 -0
- package/dist/supervisor/ipc-handler.d.ts.map +1 -1
- package/dist/supervisor/worker-event-ingestor.d.ts +1 -0
- package/dist/supervisor/worker-event-ingestor.d.ts.map +1 -1
- package/dist/supervisor/worker-start-payload-builder.d.ts +2 -0
- package/dist/supervisor/worker-start-payload-builder.d.ts.map +1 -1
- package/dist/supervisor/worker-supervisor.d.ts +4 -0
- package/dist/supervisor/worker-supervisor.d.ts.map +1 -1
- package/dist/supervisor/worker-turn-ipc-recorder.d.ts +2 -2
- package/dist/supervisor/worker-turn-ipc-recorder.d.ts.map +1 -1
- package/dist/test-helpers/accepted-turn-start.d.ts +18 -4
- package/dist/test-helpers/accepted-turn-start.d.ts.map +1 -1
- package/dist/test-helpers/owned-test-deps.d.ts +18 -0
- package/dist/test-helpers/owned-test-deps.d.ts.map +1 -0
- package/dist/test-helpers/prepared-fixture-start.d.ts.map +1 -1
- package/dist/test-helpers/process-model-fixtures.d.ts +1 -0
- package/dist/test-helpers/process-model-fixtures.d.ts.map +1 -1
- package/dist/test-helpers/scoped-settings-fixtures.d.ts +16 -0
- package/dist/test-helpers/scoped-settings-fixtures.d.ts.map +1 -0
- package/dist/test-helpers/skill-fixtures.d.ts +4 -0
- package/dist/test-helpers/skill-fixtures.d.ts.map +1 -0
- package/dist/testing.d.ts +1 -1
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +14 -10
- package/dist/testing.js.map +1 -1
- package/dist/tool-approval-gate.d.ts +3 -0
- package/dist/tool-approval-gate.d.ts.map +1 -1
- package/dist/turn-progress.d.ts.map +1 -1
- package/dist/turn-wait-service.d.ts +28 -0
- package/dist/turn-wait-service.d.ts.map +1 -0
- package/dist/worker-runtime-profile-selection.d.ts +2 -0
- package/dist/worker-runtime-profile-selection.d.ts.map +1 -1
- package/package.json +20 -19
- package/src/app.ts +117 -5
- package/src/auth/fastify-auth.ts +13 -0
- package/src/config/config-loader.ts +40 -2
- package/src/config/config-types.ts +14 -1
- package/src/config/kubernetes-docker-isolation.ts +21 -0
- package/src/db/database.ts +159 -40
- package/src/db/execution-inspection-repo.ts +163 -0
- package/src/db/history-window.ts +18 -0
- package/src/db/mapped-llm-run-repo.ts +177 -0
- package/src/db/process-event-repo.ts +88 -10
- package/src/db/process-instance-repo.ts +9 -2
- package/src/db/process-leaf-outcome-snapshot-repo.ts +8 -2
- package/src/db/process-project-repo.ts +4 -0
- package/src/db/process-skill-repo.ts +9 -2
- package/src/db/process-turn-annotation-repo.ts +27 -9
- package/src/db/process-turn-record-repo.ts +53 -13
- package/src/db/publication-repo.ts +63 -0
- package/src/db/repo-helpers.ts +11 -1
- package/src/db/repositories.ts +22 -0
- package/src/db/schema.ts +173 -2
- package/src/db/scoped-settings-repo.ts +234 -0
- package/src/db/skill-repo.ts +89 -27
- package/src/db/turn-start-record-repo.ts +13 -3
- package/src/db/turn-summary-repo.ts +12 -3
- package/src/db/worker-lease-repo.ts +8 -2
- package/src/domain-logic/outcome-tools.ts +4 -149
- package/src/external-source-service.ts +33 -7
- package/src/future-execution/execution.ts +9 -2
- package/src/future-execution/launch-lifecycle.ts +87 -78
- package/src/future-execution/lifecycle.ts +5 -34
- package/src/future-execution/model-projection.ts +2 -0
- package/src/future-execution/reconciliation.ts +7 -0
- package/src/future-execution-presenter.ts +14 -22
- package/src/integration-tool-registry.ts +59 -26
- package/src/launch-coordinator-test-harness.ts +8 -4
- package/src/launch-coordinator.ts +10 -1
- package/src/launcher-model-config-service.ts +24 -3
- package/src/leaf-outcome-snapshot-capture.ts +2 -0
- package/src/primary-path-snapshot.ts +3 -4
- package/src/process-action-presenter.ts +98 -0
- package/src/process-action-registry.ts +31 -39
- package/src/process-engine/decision.ts +10 -0
- package/src/process-engine/engine.ts +13 -4
- package/src/process-engine/mapped-turns.ts +328 -0
- package/src/process-engine/operation.ts +1 -1
- package/src/process-engine/ops/accept-worker-turn-start.ts +6 -0
- package/src/process-engine/ops/continue-failed-turn.ts +2 -13
- package/src/process-engine/ops/process-observation.ts +119 -0
- package/src/process-engine/ops/retry-failed-turn.ts +2 -12
- package/src/process-engine/ops/retry-startup.ts +10 -13
- package/src/process-engine/ops/start-process.ts +34 -2
- package/src/process-engine/ops/turn-outcome.ts +65 -62
- package/src/process-engine/ops/turn-wait.ts +225 -0
- package/src/process-engine/ops/update-model-config.ts +66 -0
- package/src/process-engine/recorder.ts +33 -5
- package/src/process-engine/runner.ts +8 -8
- package/src/process-engine/turn-start-preflight.ts +168 -105
- package/src/process-engine/turn-wait-state.ts +126 -0
- package/src/process-engine/turn-worker-requirement.ts +4 -0
- package/src/process-engine/types.ts +15 -0
- package/src/process-engine/writes/build-process-abort-writes.ts +30 -19
- package/src/process-engine/writes/build-recovery-start-writes.ts +1 -0
- package/src/process-engine/writes/build-turn-outcome-writes.ts +118 -21
- package/src/process-engine/writes/build-turn-selection-writes.ts +2 -31
- package/src/process-engine/writes/commit-writes.ts +20 -2
- package/src/process-engine/writes/reserve-selected-turn-start.ts +44 -0
- package/src/process-engine/writes/writes.ts +73 -61
- package/src/process-graph.ts +0 -1
- package/src/process-inspection-reader.ts +305 -0
- package/src/process-inspection-trace.ts +176 -0
- package/src/process-inspection.ts +169 -0
- package/src/process-instance-tree-presenter.ts +56 -216
- package/src/process-launch-executor.ts +5 -2
- package/src/process-launch-plan.ts +17 -2
- package/src/process-launcher-registry.ts +28 -16
- package/src/process-model-config.ts +100 -0
- package/src/process-model-policy/construct.ts +1 -0
- package/src/process-model-policy/evaluate.ts +34 -32
- package/src/process-model-policy/index.ts +45 -3
- package/src/process-model-policy/project.ts +62 -76
- package/src/process-model-policy/types.ts +26 -0
- package/src/process-model-policy-presenter.ts +7 -3
- package/src/process-runtime-availability.ts +9 -4
- package/src/process-turn-navigation.ts +86 -8
- package/src/process-turn-trace.ts +12 -24
- package/src/process-ui-snapshot-presenter.ts +249 -298
- package/src/routes/future-executions.ts +5 -5
- package/src/routes/launchers.ts +51 -1
- package/src/routes/process-actions.ts +10 -10
- package/src/routes/process-detail.ts +25 -2
- package/src/routes/process-diagnostics-assembler.ts +32 -6
- package/src/routes/process-model-config.ts +70 -0
- package/src/routes/process-primary-path-assembler.ts +17 -33
- package/src/routes/process-route-helpers.ts +161 -327
- package/src/routes/processes.ts +2 -0
- package/src/routes/settings.ts +173 -0
- package/src/routes/ticket-creation.ts +28 -5
- package/src/scoped-settings-service.ts +736 -0
- package/src/server-bootstrap/build-host-capabilities.ts +42 -0
- package/src/server-bootstrap/register-http.ts +7 -1
- package/src/session-summary-projection.ts +1 -2
- package/src/skills/bundled-skills.ts +64 -0
- package/src/skills/catalog-service.ts +35 -6
- package/src/skills/extension-importer.ts +97 -0
- package/src/skills/launch-skills.ts +19 -0
- package/src/skills/skill-frontmatter.ts +13 -0
- package/src/skills/source-importer.ts +3 -0
- package/src/startup-evidence.ts +20 -0
- package/src/supervisor/execution-inspection-ingestor.ts +55 -0
- package/src/supervisor/ipc-handler.ts +29 -18
- package/src/supervisor/worker-event-ingestor.ts +30 -14
- package/src/supervisor/worker-start-payload-builder.ts +42 -0
- package/src/supervisor/worker-supervisor.ts +5 -0
- package/src/supervisor/worker-turn-ipc-recorder.ts +13 -1
- package/src/test-helpers/accepted-turn-start.ts +51 -27
- package/src/test-helpers/owned-test-deps.ts +45 -0
- package/src/test-helpers/prepared-fixture-start.ts +1 -0
- package/src/test-helpers/process-model-fixtures.ts +2 -0
- package/src/test-helpers/scoped-settings-fixtures.ts +126 -0
- package/src/test-helpers/skill-fixtures.ts +28 -0
- package/src/testing.ts +4 -1
- package/src/tool-approval-gate.ts +16 -0
- package/src/turn-progress.ts +3 -2
- package/src/turn-wait-service.ts +228 -0
- package/src/worker-runtime-profile-selection.ts +8 -1
- package/dist/app-4QSV4FUQ.js +0 -8
- package/dist/chunk-KIOQNNG6.js.map +0 -1
- package/dist/chunk-LK3RO6C7.js.map +0 -1
- /package/dist/{app-4QSV4FUQ.js.map → app-JM4CBTND.js.map} +0 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
InspectionEvidence,
|
|
3
|
+
InspectionModelInput,
|
|
4
|
+
ProcessTurnRecord,
|
|
5
|
+
} from "@leitwerk-dev/domain";
|
|
6
|
+
import type {
|
|
7
|
+
ExecutionInspectionConfiguration,
|
|
8
|
+
ExecutionInspectionContext,
|
|
9
|
+
ExecutionInspectionSummary,
|
|
10
|
+
ExecutionInspectionTrace,
|
|
11
|
+
InspectionTargetState,
|
|
12
|
+
} from "@leitwerk-dev/protocol";
|
|
13
|
+
import { createInspectionLineage } from "./process-inspection.js";
|
|
14
|
+
import { buildInspectionTraceMessages } from "./process-inspection-trace.js";
|
|
15
|
+
import { ProcessUiSnapshotAssembler } from "./process-ui-snapshot-presenter.js";
|
|
16
|
+
|
|
17
|
+
/** One server-owned adapter for compact facts and on-demand inspection evidence. @internal */
|
|
18
|
+
export class ProcessInspectionReader {
|
|
19
|
+
private readonly history: ProcessUiSnapshotAssembler;
|
|
20
|
+
constructor(private readonly deps: ConstructorParameters<typeof ProcessUiSnapshotAssembler>[0]) {
|
|
21
|
+
this.history = new ProcessUiSnapshotAssembler(deps);
|
|
22
|
+
}
|
|
23
|
+
private selected(instanceId: string, turnRecordId: string) {
|
|
24
|
+
if (!this.deps.processes.getById(instanceId)) return null;
|
|
25
|
+
const record = this.deps.turnRecords.getById(turnRecordId);
|
|
26
|
+
return record?.instanceId === instanceId ? record : null;
|
|
27
|
+
}
|
|
28
|
+
private lineageSnapshot(instanceId: string) {
|
|
29
|
+
return {
|
|
30
|
+
records: this.deps.turnRecords.listByInstance(instanceId),
|
|
31
|
+
leases: this.deps.leases.listByInstance(instanceId),
|
|
32
|
+
observations: this.deps.executionInspections.listContextFacts(instanceId),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** No session or immutable-content reads are needed to open the shell. */
|
|
36
|
+
summary(instanceId: string, turnRecordId: string): ExecutionInspectionSummary | null {
|
|
37
|
+
const record = this.selected(instanceId, turnRecordId);
|
|
38
|
+
if (!record) return null;
|
|
39
|
+
const records = this.deps.turnRecords
|
|
40
|
+
.listByInstance(instanceId)
|
|
41
|
+
.sort((a, b) => a.startedAt.localeCompare(b.startedAt) || a.id.localeCompare(b.id));
|
|
42
|
+
const index = records.findIndex((candidate) => candidate.id === turnRecordId);
|
|
43
|
+
const start = record.turnStartRecordId
|
|
44
|
+
? this.deps.turnStarts.getById(record.turnStartRecordId)
|
|
45
|
+
: null;
|
|
46
|
+
const inputs = this.deps.executionInspections.modelInputs(instanceId, turnRecordId);
|
|
47
|
+
const accepted =
|
|
48
|
+
start && "start" in start.state && start.state.start?.kind === "llm"
|
|
49
|
+
? start.state.start.model
|
|
50
|
+
: null;
|
|
51
|
+
let model: InspectionEvidence<InspectionModelInput["model"]> =
|
|
52
|
+
record.turnType === "llm"
|
|
53
|
+
? { state: "not_recorded", reason: "The historical model was not recorded" }
|
|
54
|
+
: {
|
|
55
|
+
state: "not_applicable",
|
|
56
|
+
reason: `This ${record.turnType} execution does not invoke a model`,
|
|
57
|
+
};
|
|
58
|
+
const effective = inputs.at(-1)?.model;
|
|
59
|
+
if (effective) model = { state: "recorded", value: effective };
|
|
60
|
+
else if (accepted)
|
|
61
|
+
model = {
|
|
62
|
+
state: "recorded",
|
|
63
|
+
value: {
|
|
64
|
+
provider: accepted.providerId,
|
|
65
|
+
id: accepted.modelId,
|
|
66
|
+
thinkingLevel: accepted.thinkingLevel,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
const { turnResultMarkdown: _output, ...execution } = record;
|
|
70
|
+
return {
|
|
71
|
+
instanceId,
|
|
72
|
+
execution,
|
|
73
|
+
origin: createInspectionLineage(this.lineageSnapshot(instanceId)).origin(record),
|
|
74
|
+
startKind: start
|
|
75
|
+
? { state: "recorded", value: start.startKind }
|
|
76
|
+
: { state: "not_recorded", reason: "Execution start kind was not recorded" },
|
|
77
|
+
model,
|
|
78
|
+
modelInputCount: inputs.length,
|
|
79
|
+
usage: this.deps.turnSummaries.get(turnRecordId)?.usage ?? null,
|
|
80
|
+
previousTurnRecordId: records[index - 1]?.id ?? null,
|
|
81
|
+
nextTurnRecordId: records[index + 1]?.id ?? null,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
private missingModelEvidence<T>(record: ProcessTurnRecord): InspectionEvidence<T> {
|
|
85
|
+
return record.turnType === "llm"
|
|
86
|
+
? {
|
|
87
|
+
state: "not_recorded",
|
|
88
|
+
reason:
|
|
89
|
+
"No model input was recorded for this execution. Historical instructions cannot be reconstructed from current settings.",
|
|
90
|
+
}
|
|
91
|
+
: {
|
|
92
|
+
state: "not_applicable",
|
|
93
|
+
reason: `This ${record.turnType} execution does not invoke a model`,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
configuration(instanceId: string, turnRecordId: string): ExecutionInspectionConfiguration | null {
|
|
97
|
+
const record = this.selected(instanceId, turnRecordId);
|
|
98
|
+
if (!record) return null;
|
|
99
|
+
const revisions = this.deps.executionInspections
|
|
100
|
+
.list(instanceId, turnRecordId)
|
|
101
|
+
.flatMap((capture) => {
|
|
102
|
+
if (capture.fact.kind !== "model_input") return [];
|
|
103
|
+
const { messages: _messages, kind: _kind, ...configuration } = capture.fact;
|
|
104
|
+
return [{ id: capture.id, timestamp: capture.timestamp, ...configuration }];
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
instanceId,
|
|
108
|
+
turnRecordId,
|
|
109
|
+
revisions: revisions.length
|
|
110
|
+
? { state: "recorded", value: revisions }
|
|
111
|
+
: this.missingModelEvidence(record),
|
|
112
|
+
currentWorkflowTurnId: record.turnId,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
async context(
|
|
116
|
+
instanceId: string,
|
|
117
|
+
turnRecordId: string,
|
|
118
|
+
): Promise<ExecutionInspectionContext | null> {
|
|
119
|
+
const record = this.selected(instanceId, turnRecordId);
|
|
120
|
+
if (!record) return null;
|
|
121
|
+
const lineageSnapshot = this.lineageSnapshot(instanceId);
|
|
122
|
+
const captures = this.deps.executionInspections.list(instanceId, turnRecordId);
|
|
123
|
+
const session = await this.deps.sessionReader.readSessionTree(instanceId);
|
|
124
|
+
const lineage = createInspectionLineage({ ...lineageSnapshot, tree: session.piTree });
|
|
125
|
+
const supplies = captures.filter((capture) => capture.fact.kind === "supplied_context");
|
|
126
|
+
const products = supplies.flatMap((capture) =>
|
|
127
|
+
capture.fact.kind === "supplied_context"
|
|
128
|
+
? capture.fact.products.map((product) => ({
|
|
129
|
+
...product,
|
|
130
|
+
supplyId: capture.id,
|
|
131
|
+
consumed: captures.some(
|
|
132
|
+
(candidate) =>
|
|
133
|
+
candidate.fact.kind === "product_consumed" &&
|
|
134
|
+
`${turnRecordId}:${candidate.fact.supplyId}` === capture.id &&
|
|
135
|
+
candidate.fact.name === product.name,
|
|
136
|
+
),
|
|
137
|
+
}))
|
|
138
|
+
: [],
|
|
139
|
+
);
|
|
140
|
+
const modelInputs = captures.flatMap((capture) =>
|
|
141
|
+
capture.fact.kind === "model_input"
|
|
142
|
+
? [
|
|
143
|
+
{
|
|
144
|
+
id: capture.id,
|
|
145
|
+
timestamp: capture.timestamp,
|
|
146
|
+
boundaryEntryId: capture.fact.boundaryEntryId,
|
|
147
|
+
model: capture.fact.model,
|
|
148
|
+
messages: capture.fact.messages.map((message) => {
|
|
149
|
+
const sourceTurnRecordId = message.entryId ? lineage.owner(message.entryId) : null;
|
|
150
|
+
return {
|
|
151
|
+
entryId: message.entryId,
|
|
152
|
+
sourceTurnRecordId,
|
|
153
|
+
role: message.role,
|
|
154
|
+
content:
|
|
155
|
+
sourceTurnRecordId && sourceTurnRecordId !== turnRecordId
|
|
156
|
+
? null
|
|
157
|
+
: message.content,
|
|
158
|
+
};
|
|
159
|
+
}),
|
|
160
|
+
},
|
|
161
|
+
]
|
|
162
|
+
: [],
|
|
163
|
+
);
|
|
164
|
+
const boundaries = new Set(
|
|
165
|
+
modelInputs
|
|
166
|
+
.map((revision) => revision.boundaryEntryId)
|
|
167
|
+
.filter((id): id is string => id !== null),
|
|
168
|
+
);
|
|
169
|
+
if (record.resultPiEntryId) boundaries.add(record.resultPiEntryId);
|
|
170
|
+
const contextEntries = new Map(
|
|
171
|
+
[...boundaries].flatMap((boundary) =>
|
|
172
|
+
session.piTree.getBranch(boundary).map((entry) => [entry.id, entry] as const),
|
|
173
|
+
),
|
|
174
|
+
);
|
|
175
|
+
const compactions = [...contextEntries.values()].flatMap((entry) =>
|
|
176
|
+
entry.type === "compaction"
|
|
177
|
+
? [{ entryId: entry.id, firstKeptEntryId: entry.firstKeptEntryId, summary: entry.summary }]
|
|
178
|
+
: [],
|
|
179
|
+
);
|
|
180
|
+
const inputMessages = buildInspectionTraceMessages({
|
|
181
|
+
tree: session.piTree,
|
|
182
|
+
record,
|
|
183
|
+
captures,
|
|
184
|
+
events: [],
|
|
185
|
+
owner: lineage.owner,
|
|
186
|
+
}).filter((message) => message.role === "user" || message.role === "system");
|
|
187
|
+
return {
|
|
188
|
+
instanceId,
|
|
189
|
+
turnRecordId,
|
|
190
|
+
origin: lineage.origin(record),
|
|
191
|
+
ancestry: lineage.ancestry(record),
|
|
192
|
+
products: supplies.length
|
|
193
|
+
? { state: "recorded", value: products }
|
|
194
|
+
: { state: "not_recorded", reason: "Supplied product versions were not recorded" },
|
|
195
|
+
modelInputs: modelInputs.length
|
|
196
|
+
? { state: "recorded", value: modelInputs }
|
|
197
|
+
: this.missingModelEvidence(record),
|
|
198
|
+
compactions,
|
|
199
|
+
inputMessages,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
async trace(
|
|
203
|
+
instanceId: string,
|
|
204
|
+
turnRecordId: string,
|
|
205
|
+
target: { entryId?: string; itemId?: string; boundaryFor?: string } = {},
|
|
206
|
+
): Promise<ExecutionInspectionTrace | null> {
|
|
207
|
+
// Keep every durable fact at the same boundary as the reasoning history.
|
|
208
|
+
// Session storage is asynchronous and can advance independently.
|
|
209
|
+
const lineageSnapshot = this.lineageSnapshot(instanceId);
|
|
210
|
+
const captures = this.deps.executionInspections.list(instanceId, turnRecordId);
|
|
211
|
+
const annotations = this.deps.turnAnnotations
|
|
212
|
+
.listByInstance(instanceId)
|
|
213
|
+
.filter((annotation) =>
|
|
214
|
+
annotation.references.some(
|
|
215
|
+
(reference) =>
|
|
216
|
+
reference.kind === "turn_record" && reference.turnRecordId === turnRecordId,
|
|
217
|
+
),
|
|
218
|
+
);
|
|
219
|
+
const evidence = await this.history.readReasoningEvidence({
|
|
220
|
+
instanceId,
|
|
221
|
+
turnRecordId,
|
|
222
|
+
includeSession: true,
|
|
223
|
+
});
|
|
224
|
+
if (!evidence?.session) return null;
|
|
225
|
+
const { detail: history, turnRecord: record, events, session } = evidence;
|
|
226
|
+
const lineage = createInspectionLineage({ ...lineageSnapshot, tree: session.piTree });
|
|
227
|
+
const messages = buildInspectionTraceMessages({
|
|
228
|
+
tree: session.piTree,
|
|
229
|
+
record,
|
|
230
|
+
captures,
|
|
231
|
+
events,
|
|
232
|
+
owner: lineage.owner,
|
|
233
|
+
});
|
|
234
|
+
let targetState: InspectionTargetState | null = null;
|
|
235
|
+
let inheritedBoundary: ExecutionInspectionTrace["inheritedBoundary"] = null;
|
|
236
|
+
const unavailable = {
|
|
237
|
+
state: "unavailable",
|
|
238
|
+
reason: "The requested item is unavailable or does not belong to this execution",
|
|
239
|
+
} as const;
|
|
240
|
+
let itemId = target.entryId ? `entry:${target.entryId}` : target.itemId;
|
|
241
|
+
if (itemId === "input")
|
|
242
|
+
itemId =
|
|
243
|
+
messages.find((message) => message.role === "user" || message.role === "system")?.id ??
|
|
244
|
+
itemId;
|
|
245
|
+
if (itemId === "reasoning")
|
|
246
|
+
itemId =
|
|
247
|
+
messages
|
|
248
|
+
.flatMap((message) => message.blocks)
|
|
249
|
+
.find((block) => block.content.type === "thinking")?.id ??
|
|
250
|
+
events
|
|
251
|
+
.filter(
|
|
252
|
+
(event) =>
|
|
253
|
+
event.eventType === "pi.stream.delta" && event.data.streamType === "thinking",
|
|
254
|
+
)
|
|
255
|
+
.map((event) => `event:${event.eventSequence}`)[0] ??
|
|
256
|
+
itemId;
|
|
257
|
+
if (itemId) {
|
|
258
|
+
const message = messages.find(
|
|
259
|
+
(message) =>
|
|
260
|
+
message.id === itemId ||
|
|
261
|
+
message.aliases.includes(itemId) ||
|
|
262
|
+
message.blocks.some((block) => block.id === itemId),
|
|
263
|
+
);
|
|
264
|
+
const event = events.find((event) => `event:${event.eventSequence}` === itemId);
|
|
265
|
+
targetState =
|
|
266
|
+
message || event
|
|
267
|
+
? { state: "available", itemId: message?.aliases.includes(itemId) ? message.id : itemId }
|
|
268
|
+
: unavailable;
|
|
269
|
+
}
|
|
270
|
+
if (target.boundaryFor) {
|
|
271
|
+
const child = lineageSnapshot.records.find((record) => record.id === target.boundaryFor);
|
|
272
|
+
const context = child ? lineage.origin(child).conversation : null;
|
|
273
|
+
const boundary = context?.state === "recorded" ? context.value : null;
|
|
274
|
+
if (
|
|
275
|
+
boundary?.turnRecordId === turnRecordId &&
|
|
276
|
+
(!target.entryId || boundary.entryId === target.entryId)
|
|
277
|
+
) {
|
|
278
|
+
inheritedBoundary = boundary;
|
|
279
|
+
targetState = messages.some((message) => message.entryId === boundary.entryId)
|
|
280
|
+
? { state: "available", itemId: `entry:${boundary.entryId}` }
|
|
281
|
+
: {
|
|
282
|
+
state: "unavailable",
|
|
283
|
+
reason: "The inheritance boundary is recorded, but its source entry is unavailable",
|
|
284
|
+
};
|
|
285
|
+
} else targetState = unavailable;
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
...history,
|
|
289
|
+
messages,
|
|
290
|
+
unassignedMessages: buildInspectionTraceMessages({
|
|
291
|
+
tree: session.piTree,
|
|
292
|
+
record,
|
|
293
|
+
scope: "unassigned_branch",
|
|
294
|
+
captures: [],
|
|
295
|
+
events: [],
|
|
296
|
+
owner: lineage.owner,
|
|
297
|
+
}),
|
|
298
|
+
events,
|
|
299
|
+
annotations,
|
|
300
|
+
output: record.turnResultMarkdown,
|
|
301
|
+
target: targetState,
|
|
302
|
+
inheritedBoundary,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
asUnknownRecord,
|
|
4
|
+
type ExecutionInspectionRecord,
|
|
5
|
+
type ProcessEvent,
|
|
6
|
+
type ProcessTurnRecord,
|
|
7
|
+
} from "@leitwerk-dev/domain";
|
|
8
|
+
import type {
|
|
9
|
+
InspectionTraceBlock,
|
|
10
|
+
InspectionTraceMessage,
|
|
11
|
+
PiSessionContentBlock,
|
|
12
|
+
} from "@leitwerk-dev/protocol";
|
|
13
|
+
import type { ReadonlyPiSessionTree } from "./pi-session-tree.js";
|
|
14
|
+
|
|
15
|
+
function blocks(messageId: string, content: unknown): InspectionTraceBlock[] {
|
|
16
|
+
const raw =
|
|
17
|
+
typeof content === "string"
|
|
18
|
+
? [{ type: "text", text: content }]
|
|
19
|
+
: Array.isArray(content)
|
|
20
|
+
? content
|
|
21
|
+
: [];
|
|
22
|
+
const occurrences = new Map<string, number>();
|
|
23
|
+
return raw.flatMap((value) => {
|
|
24
|
+
const block = asUnknownRecord(value);
|
|
25
|
+
if (!block) return [];
|
|
26
|
+
let safe: PiSessionContentBlock;
|
|
27
|
+
if (block.type === "text" && typeof block.text === "string")
|
|
28
|
+
safe = { type: "text", text: block.text };
|
|
29
|
+
else if (block.type === "thinking" && typeof block.thinking === "string")
|
|
30
|
+
safe = {
|
|
31
|
+
type: "thinking",
|
|
32
|
+
thinking: block.thinking,
|
|
33
|
+
...(block.redacted === true ? { redacted: true } : {}),
|
|
34
|
+
};
|
|
35
|
+
else if (
|
|
36
|
+
block.type === "toolCall" &&
|
|
37
|
+
typeof block.id === "string" &&
|
|
38
|
+
typeof block.name === "string"
|
|
39
|
+
)
|
|
40
|
+
safe = {
|
|
41
|
+
type: "toolCall",
|
|
42
|
+
id: block.id,
|
|
43
|
+
name: block.name,
|
|
44
|
+
arguments: asUnknownRecord(block.arguments) ?? {},
|
|
45
|
+
};
|
|
46
|
+
else if (
|
|
47
|
+
block.type === "image" &&
|
|
48
|
+
typeof block.data === "string" &&
|
|
49
|
+
typeof block.mimeType === "string"
|
|
50
|
+
)
|
|
51
|
+
safe = { type: "image", data: block.data, mimeType: block.mimeType };
|
|
52
|
+
else
|
|
53
|
+
safe = {
|
|
54
|
+
type: "text",
|
|
55
|
+
text: `[Unsupported recorded ${String(block.type ?? "content")} block]`,
|
|
56
|
+
};
|
|
57
|
+
const fingerprint = createHash("sha256").update(JSON.stringify(safe)).digest("hex");
|
|
58
|
+
const occurrence = occurrences.get(fingerprint) ?? 0;
|
|
59
|
+
occurrences.set(fingerprint, occurrence + 1);
|
|
60
|
+
const id = createHash("sha256")
|
|
61
|
+
.update(`${messageId}\0${fingerprint}\0${occurrence}`)
|
|
62
|
+
.digest("hex")
|
|
63
|
+
.slice(0, 24);
|
|
64
|
+
return [{ id: `block:${id}`, content: safe }];
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Preserve session roles, message order and model-facing blocks. @internal */
|
|
69
|
+
export function buildInspectionTraceMessages(input: {
|
|
70
|
+
tree: ReadonlyPiSessionTree;
|
|
71
|
+
record: ProcessTurnRecord;
|
|
72
|
+
scope?: "execution" | "unassigned_branch";
|
|
73
|
+
owner(entryId: string): string | null;
|
|
74
|
+
captures: readonly ExecutionInspectionRecord[];
|
|
75
|
+
events: readonly ProcessEvent[];
|
|
76
|
+
}): InspectionTraceMessage[] {
|
|
77
|
+
const messages: InspectionTraceMessage[] = [];
|
|
78
|
+
const included = new Set<string>();
|
|
79
|
+
const unassigned = input.scope === "unassigned_branch";
|
|
80
|
+
const entries = unassigned
|
|
81
|
+
? input.record.resultPiEntryId
|
|
82
|
+
? input.tree.getBranch(input.record.resultPiEntryId)
|
|
83
|
+
: []
|
|
84
|
+
: input.tree.entries;
|
|
85
|
+
for (const entry of entries) {
|
|
86
|
+
if (unassigned) {
|
|
87
|
+
if (input.owner(entry.id) !== null || !["message", "custom_message"].includes(entry.type))
|
|
88
|
+
continue;
|
|
89
|
+
} else if (input.owner(entry.id) !== input.record.id) continue;
|
|
90
|
+
const message =
|
|
91
|
+
entry.type === "message"
|
|
92
|
+
? entry.message
|
|
93
|
+
: entry.type === "custom_message"
|
|
94
|
+
? { role: "user", content: entry.content }
|
|
95
|
+
: {
|
|
96
|
+
role: entry.type,
|
|
97
|
+
content:
|
|
98
|
+
entry.type === "compaction"
|
|
99
|
+
? entry.summary
|
|
100
|
+
: entry.type === "model_change"
|
|
101
|
+
? `Model changed to ${entry.provider}/${entry.modelId}`
|
|
102
|
+
: `Recorded ${entry.type.replaceAll("_", " ")} session entry`,
|
|
103
|
+
};
|
|
104
|
+
const record = message as unknown as Record<string, unknown>;
|
|
105
|
+
const id = `entry:${entry.id}`;
|
|
106
|
+
const link = input.captures.find(
|
|
107
|
+
(item) => item.fact.kind === "entry_link" && item.fact.entryId === entry.id,
|
|
108
|
+
);
|
|
109
|
+
const piTurnId = link?.fact.kind === "entry_link" ? link.fact.piTurnId : null;
|
|
110
|
+
const aliases =
|
|
111
|
+
link && piTurnId
|
|
112
|
+
? input.events
|
|
113
|
+
.filter((event) => {
|
|
114
|
+
if (event.data.workerLeaseId !== link.workerLeaseId || event.data.turnId !== piTurnId)
|
|
115
|
+
return false;
|
|
116
|
+
if (message.role === "assistant")
|
|
117
|
+
return (
|
|
118
|
+
event.eventType === "pi.stream.delta" ||
|
|
119
|
+
(["pi.tool.call", "pi.tool.started"].includes(event.eventType) &&
|
|
120
|
+
Array.isArray(record.content) &&
|
|
121
|
+
record.content.some(
|
|
122
|
+
(block) => block.type === "toolCall" && block.id === event.data.toolCallId,
|
|
123
|
+
))
|
|
124
|
+
);
|
|
125
|
+
return (
|
|
126
|
+
message.role === "toolResult" &&
|
|
127
|
+
["pi.tool.result", "pi.tool.completed"].includes(event.eventType) &&
|
|
128
|
+
event.data.toolCallId === record.toolCallId
|
|
129
|
+
);
|
|
130
|
+
})
|
|
131
|
+
.map((event) => `event:${event.eventSequence}`)
|
|
132
|
+
: [];
|
|
133
|
+
messages.push({
|
|
134
|
+
id,
|
|
135
|
+
entryId: entry.id,
|
|
136
|
+
aliases,
|
|
137
|
+
role: message.role,
|
|
138
|
+
timestamp: entry.timestamp,
|
|
139
|
+
blocks: blocks(id, record.content),
|
|
140
|
+
toolCallId: typeof record.toolCallId === "string" ? record.toolCallId : null,
|
|
141
|
+
toolName: typeof record.toolName === "string" ? record.toolName : null,
|
|
142
|
+
isError: record.isError === true,
|
|
143
|
+
});
|
|
144
|
+
included.add(entry.id);
|
|
145
|
+
}
|
|
146
|
+
if (unassigned) return messages;
|
|
147
|
+
// Prompt evidence is available before the next session upload. Do not copy
|
|
148
|
+
// inherited messages out of a model input into this execution's activity.
|
|
149
|
+
for (const capture of input.captures) {
|
|
150
|
+
if (capture.fact.kind !== "model_input") continue;
|
|
151
|
+
for (const message of capture.fact.messages) {
|
|
152
|
+
if (
|
|
153
|
+
message.role !== "user" ||
|
|
154
|
+
!message.entryId ||
|
|
155
|
+
included.has(message.entryId) ||
|
|
156
|
+
input.owner(message.entryId) !== input.record.id
|
|
157
|
+
)
|
|
158
|
+
continue;
|
|
159
|
+
if (message.content.state !== "recorded" && message.content.state !== "redacted") continue;
|
|
160
|
+
const id = `entry:${message.entryId}`;
|
|
161
|
+
messages.push({
|
|
162
|
+
id,
|
|
163
|
+
entryId: message.entryId,
|
|
164
|
+
aliases: [],
|
|
165
|
+
role: message.role,
|
|
166
|
+
timestamp: capture.timestamp,
|
|
167
|
+
blocks: blocks(id, message.content.value),
|
|
168
|
+
toolCallId: null,
|
|
169
|
+
toolName: null,
|
|
170
|
+
isError: false,
|
|
171
|
+
});
|
|
172
|
+
included.add(message.entryId);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return messages.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
|
|
176
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
InspectionContextObservation,
|
|
3
|
+
InspectionEvidence,
|
|
4
|
+
PreparedTurnStart,
|
|
5
|
+
ProcessTurnRecord,
|
|
6
|
+
WorkerLease,
|
|
7
|
+
} from "@leitwerk-dev/domain";
|
|
8
|
+
import type { ExecutionContextOrigin, InspectionBoundary } from "@leitwerk-dev/protocol";
|
|
9
|
+
import type { ReadonlyPiSessionTree } from "./pi-session-tree.js";
|
|
10
|
+
|
|
11
|
+
interface LineageInput {
|
|
12
|
+
records: readonly ProcessTurnRecord[];
|
|
13
|
+
leases: readonly WorkerLease[];
|
|
14
|
+
observations: readonly InspectionContextObservation[];
|
|
15
|
+
tree?: ReadonlyPiSessionTree;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Recorded start receipts remain usable for executions predating inspection capture. */
|
|
19
|
+
function preparedStart(input: LineageInput, record: ProcessTurnRecord): PreparedTurnStart | null {
|
|
20
|
+
const supplied = input.observations.find(
|
|
21
|
+
(item) => item.turnRecordId === record.id && item.fact.kind === "supplied_context",
|
|
22
|
+
);
|
|
23
|
+
if (supplied?.fact.kind === "supplied_context" && supplied.fact.origin)
|
|
24
|
+
return supplied.fact.origin;
|
|
25
|
+
const receipt = input.leases.find(
|
|
26
|
+
(lease) => lease.id === record.acceptedWorkerLeaseId,
|
|
27
|
+
)?.bootstrapReceipt;
|
|
28
|
+
return receipt?.kind === "llm" && receipt.startRecordId === record.turnStartRecordId
|
|
29
|
+
? receipt.preparedStart
|
|
30
|
+
: null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Pure ownership and ancestry projection; current workflow definitions are not an input. @internal */
|
|
34
|
+
export function createInspectionLineage(input: LineageInput) {
|
|
35
|
+
const records = new Map(input.records.map((record) => [record.id, record]));
|
|
36
|
+
const starts = new Map(
|
|
37
|
+
input.records
|
|
38
|
+
.filter((record) => record.turnStartRecordId)
|
|
39
|
+
.map((record) => [record.turnStartRecordId, record.id]),
|
|
40
|
+
);
|
|
41
|
+
const candidates = new Map<string, Set<string>>();
|
|
42
|
+
const boundaryFor = (record: ProcessTurnRecord): string | null => {
|
|
43
|
+
const prepared = preparedStart(input, record);
|
|
44
|
+
return prepared?.forkPiEntryId ?? record.forkPiEntryId;
|
|
45
|
+
};
|
|
46
|
+
const claim = (entryId: string, recordId: string) => {
|
|
47
|
+
if (!records.has(recordId)) return;
|
|
48
|
+
const owners = candidates.get(entryId) ?? new Set<string>();
|
|
49
|
+
owners.add(recordId);
|
|
50
|
+
candidates.set(entryId, owners);
|
|
51
|
+
};
|
|
52
|
+
for (const record of input.records) {
|
|
53
|
+
// A failure before writing an entry can retain the inherited leaf as its result.
|
|
54
|
+
// That reference does not transfer ownership of the parent's entry.
|
|
55
|
+
if (record.resultPiEntryId && record.resultPiEntryId !== boundaryFor(record))
|
|
56
|
+
claim(record.resultPiEntryId, record.id);
|
|
57
|
+
}
|
|
58
|
+
for (const item of input.observations)
|
|
59
|
+
if (item.fact.kind === "entry_link") claim(item.fact.entryId, item.turnRecordId);
|
|
60
|
+
for (const entry of input.tree?.entries ?? []) {
|
|
61
|
+
if (entry.type !== "custom_message" && entry.type !== "compaction") continue;
|
|
62
|
+
const details = entry.details as Record<string, unknown> | undefined;
|
|
63
|
+
if (
|
|
64
|
+
details &&
|
|
65
|
+
(details.kind === "turn_prompt" || details.kind === "turn_compaction") &&
|
|
66
|
+
typeof details.startRecordId === "string"
|
|
67
|
+
) {
|
|
68
|
+
const owner = starts.get(details.startRecordId);
|
|
69
|
+
if (owner) claim(entry.id, owner);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const explicit = new Set(candidates.keys());
|
|
73
|
+
// Bound legacy ownership by recorded start/result entries. Extending the owner
|
|
74
|
+
// of an earlier result to arbitrary later descendants would invent lineage.
|
|
75
|
+
for (const record of input.records) {
|
|
76
|
+
if (!record.resultPiEntryId || !input.tree?.getEntry(record.resultPiEntryId)) continue;
|
|
77
|
+
const branch = input.tree.getBranch(record.resultPiEntryId);
|
|
78
|
+
const prepared = preparedStart(input, record);
|
|
79
|
+
const boundary = prepared?.forkPiEntryId ?? record.forkPiEntryId;
|
|
80
|
+
const boundaryIndex = boundary ? branch.findIndex((entry) => entry.id === boundary) : -1;
|
|
81
|
+
const promptIndex = branch.findIndex((entry) => {
|
|
82
|
+
if (entry.type !== "custom_message") return false;
|
|
83
|
+
const details = entry.details as Record<string, unknown> | undefined;
|
|
84
|
+
return details?.kind === "turn_prompt" && details.startRecordId === record.turnStartRecordId;
|
|
85
|
+
});
|
|
86
|
+
const first =
|
|
87
|
+
promptIndex >= 0
|
|
88
|
+
? promptIndex
|
|
89
|
+
: boundaryIndex >= 0
|
|
90
|
+
? boundaryIndex + 1
|
|
91
|
+
: prepared?.startTarget.kind === "root"
|
|
92
|
+
? 0
|
|
93
|
+
: -1;
|
|
94
|
+
if (first < 0) continue;
|
|
95
|
+
for (const entry of branch.slice(first))
|
|
96
|
+
if (!explicit.has(entry.id)) claim(entry.id, record.id);
|
|
97
|
+
}
|
|
98
|
+
const inheritedOwner = (entryId: string): string | null => {
|
|
99
|
+
const owners = candidates.get(entryId);
|
|
100
|
+
return owners?.size === 1 ? [...owners][0] : null;
|
|
101
|
+
};
|
|
102
|
+
const origin = (record: ProcessTurnRecord): ExecutionContextOrigin => {
|
|
103
|
+
const unknown = {
|
|
104
|
+
state: "not_recorded",
|
|
105
|
+
reason: "Historical context origin was not recorded",
|
|
106
|
+
} as const;
|
|
107
|
+
if (record.turnType !== "llm") {
|
|
108
|
+
const none = {
|
|
109
|
+
state: "not_applicable",
|
|
110
|
+
reason: `This ${record.turnType} execution does not invoke a model`,
|
|
111
|
+
} as const;
|
|
112
|
+
return {
|
|
113
|
+
authoredMode: none,
|
|
114
|
+
startTarget: none,
|
|
115
|
+
conversation: none,
|
|
116
|
+
structuralPath: record.pathType,
|
|
117
|
+
summary: none.reason,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const prepared = preparedStart(input, record);
|
|
121
|
+
const boundaryId = boundaryFor(record);
|
|
122
|
+
let conversation: InspectionEvidence<InspectionBoundary | null> = unknown;
|
|
123
|
+
if (boundaryId)
|
|
124
|
+
conversation = {
|
|
125
|
+
state: "recorded",
|
|
126
|
+
value: { entryId: boundaryId, turnRecordId: inheritedOwner(boundaryId) },
|
|
127
|
+
};
|
|
128
|
+
else if (
|
|
129
|
+
prepared &&
|
|
130
|
+
(prepared.startTarget.kind === "root" || prepared.startTarget.kind === "current_leaf")
|
|
131
|
+
)
|
|
132
|
+
conversation = { state: "recorded", value: null };
|
|
133
|
+
const source = conversation.state === "recorded" ? conversation.value : undefined;
|
|
134
|
+
const summary =
|
|
135
|
+
source === null
|
|
136
|
+
? "No inherited conversation · supplied inputs may still apply"
|
|
137
|
+
: source
|
|
138
|
+
? `${prepared?.contextMode === "compacted" ? "Compacted conversation" : "Conversation inherited"} through ${source.entryId}${source.turnRecordId ? "" : " · source execution unknown"}`
|
|
139
|
+
: "Conversation origin not recorded";
|
|
140
|
+
return {
|
|
141
|
+
authoredMode: prepared ? { state: "recorded", value: prepared.contextMode } : unknown,
|
|
142
|
+
startTarget: prepared ? { state: "recorded", value: prepared.startTarget } : unknown,
|
|
143
|
+
conversation,
|
|
144
|
+
structuralPath: record.pathType,
|
|
145
|
+
summary,
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
const ancestry = (record: ProcessTurnRecord) => {
|
|
149
|
+
const result: Array<{ turnRecordId: string; turnId: string; boundaryEntryId: string }> = [];
|
|
150
|
+
const visited = new Set([record.id]);
|
|
151
|
+
let current = record;
|
|
152
|
+
while (true) {
|
|
153
|
+
const context = origin(current).conversation;
|
|
154
|
+
const source = context.state === "recorded" ? context.value : null;
|
|
155
|
+
if (!source?.turnRecordId || visited.has(source.turnRecordId)) break;
|
|
156
|
+
const parent = records.get(source.turnRecordId);
|
|
157
|
+
if (!parent) break;
|
|
158
|
+
visited.add(parent.id);
|
|
159
|
+
result.push({
|
|
160
|
+
turnRecordId: parent.id,
|
|
161
|
+
turnId: parent.turnId,
|
|
162
|
+
boundaryEntryId: source.entryId,
|
|
163
|
+
});
|
|
164
|
+
current = parent;
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
};
|
|
168
|
+
return { owner: inheritedOwner, origin, ancestry, boundaryFor };
|
|
169
|
+
}
|