@citeark/agent 0.3.20
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/LICENSE +202 -0
- package/README.md +128 -0
- package/data/dataset-source-registry.v1.json +300 -0
- package/dist/arkgraph/boot.js +6 -0
- package/dist/arkgraph/index.html +1 -0
- package/dist/arkgraph/viewer.css +1 -0
- package/dist/arkgraph/viewer.en.css +1 -0
- package/dist/arkgraph/viewer.en.js +49 -0
- package/dist/arkgraph/viewer.en.js.LEGAL.txt +56 -0
- package/dist/arkgraph/viewer.js +49 -0
- package/dist/arkgraph/viewer.js.LEGAL.txt +56 -0
- package/docker/claude-code/Dockerfile +97 -0
- package/docker/claude-code/codex-pro-relay.mjs +466 -0
- package/docker/claude-code/runtime-contract-check.mjs +79 -0
- package/docs/arkgraph-reading.md +79 -0
- package/docs/configuration.md +100 -0
- package/docs/integration.md +92 -0
- package/docs/maturity-plan.md +27 -0
- package/docs/npm-release.md +44 -0
- package/docs/paper-reading.md +40 -0
- package/docs/research-plan-granularity.md +27 -0
- package/docs/terminal.md +49 -0
- package/examples/toy-evaluation/compile-task.json +27 -0
- package/examples/toy-evaluation/paper.md +5 -0
- package/examples/toy-evaluation/repository/README.md +9 -0
- package/examples/toy-evaluation/repository/checkpoint.json +4 -0
- package/examples/toy-evaluation/repository/evaluate.py +17 -0
- package/examples/toy-evaluation/task.json +81 -0
- package/package.json +59 -0
- package/prompts/compile-research.md +58 -0
- package/prompts/execute-contract.md +72 -0
- package/prompts/execute-workspace-simple.md +51 -0
- package/prompts/execute-workspace.md +34 -0
- package/prompts/prepare-reproduction.md +82 -0
- package/prompts/repair-research.md +45 -0
- package/protocol/CAP.md +129 -0
- package/protocol/LICENSE +12 -0
- package/protocol/MAPPINGS.md +72 -0
- package/protocol/README.md +38 -0
- package/protocol/conformance-v2.0-alpha.1.json +36 -0
- package/protocol/examples/arkgraph/checkpoint-evaluation.json +309 -0
- package/protocol/examples/arkgraph/fixtures.mjs +49 -0
- package/protocol/examples/arkgraph/paper-free.json +291 -0
- package/protocol/examples/arkgraph/partial-failure.json +344 -0
- package/protocol/examples/arkgraph/training-evaluation.json +443 -0
- package/protocol/profiles/agent-trace.md +16 -0
- package/protocol/profiles/computational-run.md +16 -0
- package/protocol/profiles/core.md +15 -0
- package/protocol/profiles/public-bundle.md +18 -0
- package/protocol/profiles/reproduction.md +29 -0
- package/protocol/profiles/research-compilation.md +44 -0
- package/protocol/profiles/research-plan.md +39 -0
- package/protocol/profiles/restricted-evidence.md +15 -0
- package/runtime/bootstrap-autodl-runtime.sh +314 -0
- package/runtime/create-runtime-venv.sh +41 -0
- package/runtime/install-local-cpu-runtime.sh +23 -0
- package/runtime/install-scientific-runtime.sh +153 -0
- package/runtime/mineru/parse.py +62 -0
- package/runtime/mineru/requirements.txt +4 -0
- package/runtime/requirements-baseline.txt +38 -0
- package/schemas/cap/v2/activity.schema.json +47 -0
- package/schemas/cap/v2/agent.schema.json +32 -0
- package/schemas/cap/v2/assertion.schema.json +110 -0
- package/schemas/cap/v2/descriptor.schema.json +243 -0
- package/schemas/cap/v2/entity.schema.json +64 -0
- package/schemas/cap/v2/manifest.schema.json +67 -0
- package/schemas/cap/v2/relation.schema.json +82 -0
- package/schemas/compute-catalog.schema.json +63 -0
- package/schemas/compute-decision.schema.json +27 -0
- package/schemas/execution-contract.schema.json +1024 -0
- package/schemas/research-card.schema.json +30 -0
- package/schemas/research-inventory-draft.schema.json +366 -0
- package/schemas/research.schema.json +1044 -0
- package/schemas/result.schema.json +173 -0
- package/schemas/verification-policy.schema.json +47 -0
- package/schemas/verified-conclusion.schema.json +58 -0
- package/schemas/workspace-summary.schema.json +24 -0
- package/scripts/build-arkgraph-view.mjs +12 -0
- package/scripts/check-execution-feasibility.mjs +24 -0
- package/scripts/check-syntax.mjs +15 -0
- package/scripts/deterministic-asset-preparation.py +438 -0
- package/scripts/package-cap.mjs +23 -0
- package/scripts/package-local-agent.mjs +23 -0
- package/scripts/preview-arkgraph.mjs +25 -0
- package/scripts/replay-research-compiler-candidate.mjs +134 -0
- package/scripts/review-compiler-sources.mjs +44 -0
- package/scripts/run-asset-preparation.sh +17 -0
- package/scripts/run-research-plan.mjs +98 -0
- package/scripts/validate-asset-preparation.py +290 -0
- package/scripts/verify-local-runtime.mjs +57 -0
- package/scripts/verify-npm-package.mjs +57 -0
- package/src/adapters/paper2agent.mjs +107 -0
- package/src/assets/cache.mjs +159 -0
- package/src/assets/compute.mjs +98 -0
- package/src/assets/executor.mjs +145 -0
- package/src/assets/lifecycle.mjs +213 -0
- package/src/assets/manifest.mjs +242 -0
- package/src/assets/opportunistic-preparation.mjs +81 -0
- package/src/assets/plan.mjs +411 -0
- package/src/assets/prompts.mjs +29 -0
- package/src/assets/public-asset-probe.mjs +525 -0
- package/src/assets/qualification.mjs +119 -0
- package/src/assets/readiness.mjs +130 -0
- package/src/assets/reproduction-admission.mjs +355 -0
- package/src/assets/requirements.mjs +152 -0
- package/src/assets/source-grounding.mjs +341 -0
- package/src/assets/source-policy.mjs +118 -0
- package/src/autodl/client.mjs +260 -0
- package/src/autodl/ssh.mjs +380 -0
- package/src/autodl/tools.mjs +129 -0
- package/src/cap/redaction.mjs +38 -0
- package/src/cap/v2/archive.mjs +152 -0
- package/src/cap/v2/attestation.mjs +204 -0
- package/src/cap/v2/canonical-json.mjs +114 -0
- package/src/cap/v2/compilation-artifact.mjs +240 -0
- package/src/cap/v2/core.mjs +282 -0
- package/src/cap/v2/measurement-assessment-records.mjs +23 -0
- package/src/cap/v2/pipeline-artifact.mjs +922 -0
- package/src/cap/v2/read.mjs +41 -0
- package/src/cap/v2/reassessment-artifact.mjs +383 -0
- package/src/cap/v2/research-artifact.mjs +231 -0
- package/src/cap/v2/research-map-records.mjs +46 -0
- package/src/cap/v2/research-object-records.mjs +163 -0
- package/src/cap/v2/research-records.mjs +187 -0
- package/src/cap/v2/verify.mjs +642 -0
- package/src/cli.mjs +1146 -0
- package/src/compute/autodl-pro-compiler.mjs +347 -0
- package/src/compute/autodl-pro-executor.mjs +459 -0
- package/src/compute/autodl-pro-job.mjs +843 -0
- package/src/compute/autodl-pro-network.mjs +295 -0
- package/src/compute/autodl-pro-remote.mjs +810 -0
- package/src/compute/autodl-pro-staging.mjs +117 -0
- package/src/compute/campaign.mjs +110 -0
- package/src/compute/catalog.mjs +123 -0
- package/src/compute/checkpoint-protocol.mjs +154 -0
- package/src/compute/codex-account-lock.mjs +111 -0
- package/src/compute/codex-account-session.mjs +107 -0
- package/src/compute/compiler-profile.mjs +38 -0
- package/src/compute/compiler-router.mjs +23 -0
- package/src/compute/coordinator-recovery.mjs +210 -0
- package/src/compute/executor-router.mjs +29 -0
- package/src/compute/gcp-batch-compiler.mjs +685 -0
- package/src/compute/gcp-batch-executor.mjs +1215 -0
- package/src/compute/gcp-batch-failure.mjs +92 -0
- package/src/compute/gcp-batch-job.mjs +527 -0
- package/src/compute/gcp-batch-lifecycle.mjs +81 -0
- package/src/compute/gcp-checkpoint-worker.mjs +1633 -0
- package/src/compute/local-codex-compiler.mjs +52 -0
- package/src/compute/measurement-hardware.mjs +128 -0
- package/src/compute/remote-attempt.mjs +226 -0
- package/src/compute/requirements.mjs +124 -0
- package/src/compute/research-phases.mjs +48 -0
- package/src/compute/scheduler.mjs +452 -0
- package/src/compute/shared-workloads.mjs +26 -0
- package/src/compute/stage-archive.mjs +79 -0
- package/src/contracts/campaign-contract.mjs +52 -0
- package/src/contracts/execution-contract.mjs +819 -0
- package/src/contracts/execution-mode.mjs +19 -0
- package/src/contracts/execution-timeouts.mjs +45 -0
- package/src/contracts/execution-workload.mjs +68 -0
- package/src/contracts/preflight-schema.mjs +25 -0
- package/src/contracts/public-contract.mjs +63 -0
- package/src/contracts/subject-tags.mjs +31 -0
- package/src/dashboard/data.mjs +898 -0
- package/src/dashboard/server.mjs +79 -0
- package/src/dashboard/static/dashboard.css +366 -0
- package/src/dashboard/static/dashboard.js +560 -0
- package/src/dashboard/static/index.html +85 -0
- package/src/deployment/community-policy.mjs +9 -0
- package/src/deployment/environment.mjs +112 -0
- package/src/deployment/guided.mjs +98 -0
- package/src/deployment/handoff.mjs +102 -0
- package/src/deployment/local-contract.mjs +31 -0
- package/src/deployment/local.mjs +100 -0
- package/src/deployment/prepare.mjs +46 -0
- package/src/deployment/recipe.mjs +108 -0
- package/src/deployment/supplement.mjs +51 -0
- package/src/deployment/terminal.mjs +43 -0
- package/src/diagnosis/renderer.mjs +75 -0
- package/src/diagnosis/target-failure.mjs +46 -0
- package/src/evidence/parser-registry.mjs +54 -0
- package/src/evidence/parsers/fasttext-classification.mjs +82 -0
- package/src/evidence/parsers/json-scalar.mjs +96 -0
- package/src/evidence/parsers/simcse-senteval.mjs +104 -0
- package/src/evidence/parsers/starspace-classification.mjs +78 -0
- package/src/evidence/registry.mjs +147 -0
- package/src/execution/runner-audit.mjs +473 -0
- package/src/gcp/auth.mjs +106 -0
- package/src/gcp/batch-client.mjs +120 -0
- package/src/gcp/resource-discovery.mjs +177 -0
- package/src/gcp/rest.mjs +82 -0
- package/src/gcp/secret-manager.mjs +34 -0
- package/src/gcp/signed-url.mjs +133 -0
- package/src/gcp/storage.mjs +220 -0
- package/src/graph/command.mjs +41 -0
- package/src/graph/execution.mjs +97 -0
- package/src/graph/model.mjs +37 -0
- package/src/graph/presentation.mjs +110 -0
- package/src/graph/query.mjs +159 -0
- package/src/graph/research-relations.mjs +69 -0
- package/src/graph/source-page.mjs +12 -0
- package/src/graph/source-preview.mjs +34 -0
- package/src/graph/validate.mjs +76 -0
- package/src/job.mjs +496 -0
- package/src/network/autodl-routing-proxy.mjs +462 -0
- package/src/network/egress-proxy.mjs +158 -0
- package/src/observability/event-contract.mjs +230 -0
- package/src/observability/pipeline-monitor.mjs +166 -0
- package/src/pipeline/orchestrator.mjs +1281 -0
- package/src/pipeline/recovery-error.mjs +11 -0
- package/src/pipeline/replay.mjs +304 -0
- package/src/pipeline/shared-execution.mjs +115 -0
- package/src/pipeline/stage-checkpoint.mjs +86 -0
- package/src/pipeline/stage-recovery.mjs +101 -0
- package/src/pipeline/targets.mjs +110 -0
- package/src/process.mjs +143 -0
- package/src/protocol.mjs +312 -0
- package/src/provider/codex-account.mjs +44 -0
- package/src/provider/codex-completion.mjs +49 -0
- package/src/provider/completion.mjs +292 -0
- package/src/provider/model-client.mjs +44 -0
- package/src/provider/model-route.mjs +29 -0
- package/src/provider/openrouter-readiness.mjs +189 -0
- package/src/provider/reader-bridge.mjs +35 -0
- package/src/provider/relay.mjs +263 -0
- package/src/provider/runtime-auth.mjs +40 -0
- package/src/public/cap.d.mts +90 -0
- package/src/public/cap.mjs +12 -0
- package/src/public/contracts.d.mts +2 -0
- package/src/public/host.mjs +171 -0
- package/src/public/operations.d.mts +11 -0
- package/src/public/presentation.d.mts +4 -0
- package/src/records/views.mjs +26 -0
- package/src/remote/command.mjs +178 -0
- package/src/remote/ssh.mjs +59 -0
- package/src/repository-origin.mjs +81 -0
- package/src/reproduction/evidence-feedback.mjs +96 -0
- package/src/reproduction/incomplete-initialization.mjs +25 -0
- package/src/reproduction/lifecycle.mjs +253 -0
- package/src/reproduction/plan.mjs +132 -0
- package/src/reproduction/prompts.mjs +70 -0
- package/src/reproduction/runner.mjs +188 -0
- package/src/reproduction/summary.mjs +130 -0
- package/src/reproduction/workspace-mode.mjs +7 -0
- package/src/research/automatic-admission.mjs +156 -0
- package/src/research/compiler-coverage.mjs +85 -0
- package/src/research/compiler-failure.mjs +24 -0
- package/src/research/compiler-normalization-guards.mjs +112 -0
- package/src/research/compiler-repair.mjs +3 -0
- package/src/research/compiler.mjs +853 -0
- package/src/research/continuation-selection.mjs +26 -0
- package/src/research/execution-graph-context.mjs +43 -0
- package/src/research/experiment-importance.mjs +15 -0
- package/src/research/inventory-handoff.mjs +104 -0
- package/src/research/inventory-revisions.mjs +32 -0
- package/src/research/mineru-local.mjs +73 -0
- package/src/research/paper-command.mjs +19 -0
- package/src/research/paper-markdown.mjs +180 -0
- package/src/research/paper-source-map.mjs +69 -0
- package/src/research/planning-policy.mjs +88 -0
- package/src/research/reference-materials.mjs +11 -0
- package/src/research/reproduction-scope.mjs +30 -0
- package/src/research/research-map.mjs +94 -0
- package/src/research/research-objects.mjs +88 -0
- package/src/research/source-discovery.mjs +646 -0
- package/src/research/source-observations.mjs +75 -0
- package/src/research/source-review-cli-mcp.mjs +26 -0
- package/src/research/source-review-input.mjs +209 -0
- package/src/research/source-review-local-codex.mjs +36 -0
- package/src/research/source-review-model.mjs +70 -0
- package/src/research/source-review.mjs +173 -0
- package/src/research/structure.mjs +3163 -0
- package/src/research-card/renderer.mjs +277 -0
- package/src/research-card/verified-conclusion.mjs +143 -0
- package/src/results/output-registry.mjs +183 -0
- package/src/runtime/claude-code.mjs +52 -0
- package/src/runtime/codex-capacity-retry.mjs +87 -0
- package/src/runtime/codex.mjs +64 -0
- package/src/runtime/config.mjs +157 -0
- package/src/runtime/final-output.mjs +40 -0
- package/src/runtime/index.mjs +21 -0
- package/src/runtime/local-codex.mjs +74 -0
- package/src/runtime/opencode.mjs +95 -0
- package/src/runtime/prompt.mjs +13 -0
- package/src/sandbox/docker.mjs +363 -0
- package/src/settings/command.mjs +297 -0
- package/src/settings/store.mjs +119 -0
- package/src/telemetry/pricing.mjs +68 -0
- package/src/telemetry/usage.mjs +265 -0
- package/src/terminal/events.mjs +97 -0
- package/src/terminal/input.mjs +40 -0
- package/src/terminal/plain.mjs +40 -0
- package/src/terminal/remote-stream.mjs +22 -0
- package/src/terminal/screen.mjs +214 -0
- package/src/terminal/transcript.mjs +69 -0
- package/src/util.mjs +107 -0
- package/src/verification/ai-assessor.mjs +534 -0
- package/src/verification/claim-evaluator.mjs +242 -0
- package/src/verification/evidence-context.mjs +165 -0
- package/src/verification/evidence-reader.mjs +95 -0
- package/src/verification/integrity.mjs +570 -0
- package/src/verification/tolerance.mjs +32 -0
- package/src/workloads/cpu-research-preparation.mjs +56 -0
- package/src/workloads/definition.mjs +74 -0
- package/src/workloads/phase-aware-reproduction.mjs +46 -0
- package/src/workloads/reproduction.mjs +85 -0
- package/src/workspace/command.mjs +242 -0
- package/src/workspace/control.mjs +49 -0
- package/src/workspace/entry.mjs +28 -0
- package/src/workspace/input.mjs +93 -0
- package/src/workspace/interactive.mjs +94 -0
- package/src/workspace/jobs.mjs +418 -0
- package/src/workspace/session.mjs +97 -0
- package/src/workspace/worker.mjs +137 -0
- package/ui/arkgraph/ambient-motion.mjs +10 -0
- package/ui/arkgraph/app.jsx +153 -0
- package/ui/arkgraph/boot.js +6 -0
- package/ui/arkgraph/camera-motion.mjs +20 -0
- package/ui/arkgraph/context-reveal.mjs +39 -0
- package/ui/arkgraph/details.css +3 -0
- package/ui/arkgraph/entry.jsx +28 -0
- package/ui/arkgraph/experiment-curves.mjs +17 -0
- package/ui/arkgraph/experiment-selection.mjs +15 -0
- package/ui/arkgraph/experiment-style.css +26 -0
- package/ui/arkgraph/experiment-ui.jsx +32 -0
- package/ui/arkgraph/frame.html +1 -0
- package/ui/arkgraph/graph-gestures.mjs +62 -0
- package/ui/arkgraph/label-layout.mjs +57 -0
- package/ui/arkgraph/locales/en.json +229 -0
- package/ui/arkgraph/locales/source-types.json +15 -0
- package/ui/arkgraph/localization-build.mjs +27 -0
- package/ui/arkgraph/material-build.mjs +23 -0
- package/ui/arkgraph/material-colors.mjs +39 -0
- package/ui/arkgraph/material-style.css +15 -0
- package/ui/arkgraph/open-graph.jsx +326 -0
- package/ui/arkgraph/outline.jsx +49 -0
- package/ui/arkgraph/package-lock.json +888 -0
- package/ui/arkgraph/package.json +17 -0
- package/ui/arkgraph/reading-layout.mjs +130 -0
- package/ui/arkgraph/reading-presentation.mjs +73 -0
- package/ui/arkgraph/record-detail.css +51 -0
- package/ui/arkgraph/record-details.jsx +29 -0
- package/ui/arkgraph/research-types.mjs +31 -0
- package/ui/arkgraph/selection-mark.jsx +6 -0
- package/ui/arkgraph/soft-spine.mjs +26 -0
- package/ui/arkgraph/steering-style.css +187 -0
- package/ui/arkgraph/style.css +272 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { hasSessionEvents, sessionEvent } from '../terminal/events.mjs';
|
|
2
|
+
const SENSITIVE_KEY = /^(?:authorization|cookie|set-cookie|password|secret|token|access[_-]?token|refresh[_-]?token|api[_-]?key|private[_-]?key)$/i;
|
|
3
|
+
const SENSITIVE_QUERY_KEY = /(?:authorization|password|secret|token|api[_-]?key|signature|credential)/i;
|
|
4
|
+
const BEARER_VALUE = /\b(?:bearer|basic)\s+[a-z0-9._~+/=-]+/gi;
|
|
5
|
+
const SECRET_ASSIGNMENT = /\b(token|secret|password|api[_-]?key|authorization)=([^&\s]+)/gi;
|
|
6
|
+
|
|
7
|
+
export const OBSERVABILITY_SCHEMA_VERSION = "1.0";
|
|
8
|
+
|
|
9
|
+
export function sanitizeObservation(value, depth = 0) {
|
|
10
|
+
if (depth > 8) return "[depth-limited]";
|
|
11
|
+
if (value === null || value === undefined) return value;
|
|
12
|
+
if (typeof value === "string") return redactText(value).slice(0, 4_000);
|
|
13
|
+
if (typeof value === "number" || typeof value === "boolean") return value;
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
return value.slice(0, 100).map((item) => sanitizeObservation(item, depth + 1));
|
|
16
|
+
}
|
|
17
|
+
if (typeof value !== "object") return String(value).slice(0, 500);
|
|
18
|
+
const output = {};
|
|
19
|
+
for (const [key, item] of Object.entries(value).slice(0, 200)) {
|
|
20
|
+
output[key] = SENSITIVE_KEY.test(key)
|
|
21
|
+
? "[REDACTED]"
|
|
22
|
+
: sanitizeObservation(item, depth + 1);
|
|
23
|
+
}
|
|
24
|
+
return output;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function externalRequestObservation({
|
|
28
|
+
component,
|
|
29
|
+
service,
|
|
30
|
+
operation,
|
|
31
|
+
method = "GET",
|
|
32
|
+
url,
|
|
33
|
+
attempt,
|
|
34
|
+
maxAttempts,
|
|
35
|
+
startedAt,
|
|
36
|
+
finishedAt,
|
|
37
|
+
response,
|
|
38
|
+
error,
|
|
39
|
+
retryable = false,
|
|
40
|
+
correlation,
|
|
41
|
+
}) {
|
|
42
|
+
const start = iso(startedAt) ?? new Date().toISOString();
|
|
43
|
+
const finish = iso(finishedAt) ?? start;
|
|
44
|
+
const status = Number.isInteger(response?.status) ? response.status : undefined;
|
|
45
|
+
return sanitizeObservation({
|
|
46
|
+
schemaVersion: OBSERVABILITY_SCHEMA_VERSION,
|
|
47
|
+
kind: "external_request",
|
|
48
|
+
component,
|
|
49
|
+
service,
|
|
50
|
+
operation,
|
|
51
|
+
correlation,
|
|
52
|
+
request: {
|
|
53
|
+
method: String(method).toUpperCase(),
|
|
54
|
+
url: observableUrl(url),
|
|
55
|
+
attempt,
|
|
56
|
+
maxAttempts,
|
|
57
|
+
},
|
|
58
|
+
response: status === undefined
|
|
59
|
+
? undefined
|
|
60
|
+
: {
|
|
61
|
+
status,
|
|
62
|
+
requestId: firstHeader(response.headers, [
|
|
63
|
+
"x-github-request-id",
|
|
64
|
+
"x-request-id",
|
|
65
|
+
"x-goog-request-id",
|
|
66
|
+
"cf-ray",
|
|
67
|
+
]),
|
|
68
|
+
retryAfter: header(response.headers, "retry-after"),
|
|
69
|
+
rateLimit: compact({
|
|
70
|
+
limit: header(response.headers, "x-ratelimit-limit"),
|
|
71
|
+
remaining: header(response.headers, "x-ratelimit-remaining"),
|
|
72
|
+
reset: header(response.headers, "x-ratelimit-reset"),
|
|
73
|
+
resource: header(response.headers, "x-ratelimit-resource"),
|
|
74
|
+
}),
|
|
75
|
+
},
|
|
76
|
+
outcome: {
|
|
77
|
+
status: response?.ok ? "succeeded" : "failed",
|
|
78
|
+
retryable: Boolean(retryable),
|
|
79
|
+
error: error
|
|
80
|
+
? compact({
|
|
81
|
+
name: error instanceof Error ? error.name : "Error",
|
|
82
|
+
code: typeof error?.code === "string" ? error.code : undefined,
|
|
83
|
+
message: error instanceof Error ? error.message : String(error),
|
|
84
|
+
})
|
|
85
|
+
: undefined,
|
|
86
|
+
},
|
|
87
|
+
timing: {
|
|
88
|
+
startedAt: start,
|
|
89
|
+
finishedAt: finish,
|
|
90
|
+
durationMs: Math.max(0, Date.parse(finish) - Date.parse(start)),
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function executeObservedExternalRequest({
|
|
96
|
+
request,
|
|
97
|
+
observe = emitStructuredObservation,
|
|
98
|
+
component,
|
|
99
|
+
service,
|
|
100
|
+
operation,
|
|
101
|
+
method = "GET",
|
|
102
|
+
url,
|
|
103
|
+
attempt = 1,
|
|
104
|
+
maxAttempts = 1,
|
|
105
|
+
retryableStatus = defaultRetryableStatus,
|
|
106
|
+
correlation,
|
|
107
|
+
now = () => new Date(),
|
|
108
|
+
}) {
|
|
109
|
+
const startedAt = now().toISOString();
|
|
110
|
+
let response;
|
|
111
|
+
try {
|
|
112
|
+
response = await request();
|
|
113
|
+
safeObserve(observe, externalRequestObservation({
|
|
114
|
+
component,
|
|
115
|
+
service,
|
|
116
|
+
operation,
|
|
117
|
+
method,
|
|
118
|
+
url,
|
|
119
|
+
attempt,
|
|
120
|
+
maxAttempts,
|
|
121
|
+
startedAt,
|
|
122
|
+
finishedAt: now().toISOString(),
|
|
123
|
+
response,
|
|
124
|
+
error: response.ok ? undefined : new Error(`HTTP ${response.status}`),
|
|
125
|
+
retryable: retryableStatus(response.status),
|
|
126
|
+
correlation,
|
|
127
|
+
}));
|
|
128
|
+
return response;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
safeObserve(observe, externalRequestObservation({
|
|
131
|
+
component,
|
|
132
|
+
service,
|
|
133
|
+
operation,
|
|
134
|
+
method,
|
|
135
|
+
url,
|
|
136
|
+
attempt,
|
|
137
|
+
maxAttempts,
|
|
138
|
+
startedAt,
|
|
139
|
+
finishedAt: now().toISOString(),
|
|
140
|
+
response,
|
|
141
|
+
error,
|
|
142
|
+
retryable: true,
|
|
143
|
+
correlation,
|
|
144
|
+
}));
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function emitStructuredObservation(observation, {
|
|
150
|
+
severity,
|
|
151
|
+
output = console,
|
|
152
|
+
} = {}) {
|
|
153
|
+
const safe = sanitizeObservation(observation);
|
|
154
|
+
const level = severity
|
|
155
|
+
?? (safe?.outcome?.status === "failed" ? "WARNING" : "INFO");
|
|
156
|
+
const record = {
|
|
157
|
+
...safe,
|
|
158
|
+
severity: level,
|
|
159
|
+
event: "citeark.observation",
|
|
160
|
+
};
|
|
161
|
+
if (output === console && hasSessionEvents()) {
|
|
162
|
+
sessionEvent({ kind: 'diagnostic', observation: record });
|
|
163
|
+
return record;
|
|
164
|
+
}
|
|
165
|
+
const target = level === "ERROR"
|
|
166
|
+
? output.error
|
|
167
|
+
: level === "WARNING"
|
|
168
|
+
? output.warn
|
|
169
|
+
: output.log;
|
|
170
|
+
target.call(output, JSON.stringify(record));
|
|
171
|
+
return record;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function observableUrl(value) {
|
|
175
|
+
try {
|
|
176
|
+
const url = new URL(String(value));
|
|
177
|
+
url.username = "";
|
|
178
|
+
url.password = "";
|
|
179
|
+
for (const key of [...url.searchParams.keys()]) {
|
|
180
|
+
if (SENSITIVE_QUERY_KEY.test(key)) url.searchParams.set(key, "[REDACTED]");
|
|
181
|
+
}
|
|
182
|
+
return url.toString();
|
|
183
|
+
} catch {
|
|
184
|
+
return redactText(String(value ?? ""));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function redactText(value) {
|
|
189
|
+
return String(value)
|
|
190
|
+
.replace(BEARER_VALUE, "[REDACTED_AUTHORIZATION]")
|
|
191
|
+
.replace(SECRET_ASSIGNMENT, "$1=[REDACTED]");
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function header(headers, name) {
|
|
195
|
+
const value = headers?.get?.(name);
|
|
196
|
+
return typeof value === "string" && value ? value.slice(0, 500) : undefined;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function firstHeader(headers, names) {
|
|
200
|
+
for (const name of names) {
|
|
201
|
+
const value = header(headers, name);
|
|
202
|
+
if (value) return value;
|
|
203
|
+
}
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function compact(value) {
|
|
208
|
+
const output = Object.fromEntries(
|
|
209
|
+
Object.entries(value).filter(([, item]) => item !== undefined && item !== null && item !== ""),
|
|
210
|
+
);
|
|
211
|
+
return Object.keys(output).length ? output : undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function safeObserve(observe, observation) {
|
|
215
|
+
try {
|
|
216
|
+
observe(observation);
|
|
217
|
+
} catch {
|
|
218
|
+
// 日志接收器故障不能改变被观测请求的结果。
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function defaultRetryableStatus(status) {
|
|
223
|
+
return new Set([408, 425, 429, 500, 502, 503, 504]).has(status);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function iso(value) {
|
|
227
|
+
if (value instanceof Date && Number.isFinite(value.getTime())) return value.toISOString();
|
|
228
|
+
if (typeof value !== "string" || !Number.isFinite(Date.parse(value))) return undefined;
|
|
229
|
+
return new Date(value).toISOString();
|
|
230
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { sessionEvent } from '../terminal/events.mjs';
|
|
2
|
+
import { appendFile, mkdir, rename, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
OBSERVABILITY_SCHEMA_VERSION,
|
|
7
|
+
sanitizeObservation,
|
|
8
|
+
} from "./event-contract.mjs";
|
|
9
|
+
|
|
10
|
+
const DEFAULT_HEARTBEAT_INTERVAL_MS = 15_000;
|
|
11
|
+
|
|
12
|
+
export function createPipelineMonitor({
|
|
13
|
+
directory,
|
|
14
|
+
pipelineId,
|
|
15
|
+
heartbeatIntervalMs = DEFAULT_HEARTBEAT_INTERVAL_MS,
|
|
16
|
+
now = () => new Date(),
|
|
17
|
+
correlation = {},
|
|
18
|
+
onEvent = () => {},
|
|
19
|
+
} = {}) {
|
|
20
|
+
const root = path.join(path.resolve(directory), "observability");
|
|
21
|
+
const progressPath = path.join(root, "progress.json");
|
|
22
|
+
const eventsPath = path.join(root, "events.jsonl");
|
|
23
|
+
const startedAt = now().toISOString();
|
|
24
|
+
const safeCorrelation = sanitizeObservation(correlation);
|
|
25
|
+
const state = {
|
|
26
|
+
schemaVersion: OBSERVABILITY_SCHEMA_VERSION,
|
|
27
|
+
pipelineId,
|
|
28
|
+
correlation: safeCorrelation,
|
|
29
|
+
status: "running",
|
|
30
|
+
currentStage: null,
|
|
31
|
+
currentAction: "Initializing research",
|
|
32
|
+
startedAt,
|
|
33
|
+
updatedAt: startedAt,
|
|
34
|
+
lastHeartbeatAt: startedAt,
|
|
35
|
+
lastActivityAt: startedAt,
|
|
36
|
+
lastOutputAt: null,
|
|
37
|
+
sequence: 0,
|
|
38
|
+
};
|
|
39
|
+
let queue = Promise.resolve();
|
|
40
|
+
let closed = false;
|
|
41
|
+
let lastWriteError = null;
|
|
42
|
+
|
|
43
|
+
const enqueue = (operation) => {
|
|
44
|
+
queue = queue.then(operation, operation).catch((error) => {
|
|
45
|
+
lastWriteError = error;
|
|
46
|
+
});
|
|
47
|
+
return queue;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const persist = () => {
|
|
51
|
+
const snapshot = structuredClone(state);
|
|
52
|
+
enqueue(async () => {
|
|
53
|
+
await mkdir(root, { recursive: true });
|
|
54
|
+
await atomicJson(progressPath, snapshot);
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const record = (message, {
|
|
59
|
+
kind = "activity",
|
|
60
|
+
level = "info",
|
|
61
|
+
stage = state.currentStage,
|
|
62
|
+
details = null,
|
|
63
|
+
output = false,
|
|
64
|
+
} = {}) => {
|
|
65
|
+
if (closed || typeof message !== "string" || !message.trim()) return null;
|
|
66
|
+
const at = now().toISOString();
|
|
67
|
+
const safeMessage = sanitizeObservation(message.trim());
|
|
68
|
+
const safeDetails = details && typeof details === "object"
|
|
69
|
+
? sanitizeObservation(details)
|
|
70
|
+
: null;
|
|
71
|
+
state.sequence += 1;
|
|
72
|
+
state.currentStage = stage ?? state.currentStage;
|
|
73
|
+
state.currentAction = safeMessage;
|
|
74
|
+
state.updatedAt = at;
|
|
75
|
+
state.lastActivityAt = at;
|
|
76
|
+
if (output) state.lastOutputAt = at;
|
|
77
|
+
const event = {
|
|
78
|
+
schemaVersion: OBSERVABILITY_SCHEMA_VERSION,
|
|
79
|
+
pipelineId,
|
|
80
|
+
correlation: safeCorrelation,
|
|
81
|
+
sequence: state.sequence,
|
|
82
|
+
at,
|
|
83
|
+
kind,
|
|
84
|
+
level,
|
|
85
|
+
stage: state.currentStage,
|
|
86
|
+
message: safeMessage,
|
|
87
|
+
...(safeDetails ? { details: safeDetails } : {}),
|
|
88
|
+
};
|
|
89
|
+
try {
|
|
90
|
+
onEvent(structuredClone(event));
|
|
91
|
+
} catch {
|
|
92
|
+
// 外部日志接收器故障不能改变流水线结果;本地事件仍会继续落盘。
|
|
93
|
+
}
|
|
94
|
+
const snapshot = structuredClone(state);
|
|
95
|
+
enqueue(async () => {
|
|
96
|
+
await mkdir(root, { recursive: true });
|
|
97
|
+
await appendFile(eventsPath, `${JSON.stringify(event)}\n`, "utf8");
|
|
98
|
+
await atomicJson(progressPath, snapshot);
|
|
99
|
+
});
|
|
100
|
+
return event;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const heartbeat = () => {
|
|
104
|
+
if (closed) return;
|
|
105
|
+
const at = now().toISOString();
|
|
106
|
+
state.updatedAt = at;
|
|
107
|
+
state.lastHeartbeatAt = at;
|
|
108
|
+
persist();
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const timer = setInterval(heartbeat, Math.max(1_000, Number(heartbeatIntervalMs) || DEFAULT_HEARTBEAT_INTERVAL_MS));
|
|
112
|
+
timer.unref?.();
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
paths: { root, progress: progressPath, events: eventsPath },
|
|
116
|
+
state,
|
|
117
|
+
async start({ stage = null, message = "Research started" } = {}) {
|
|
118
|
+
state.currentStage = stage;
|
|
119
|
+
record(message, { kind: "lifecycle", stage });
|
|
120
|
+
await queue;
|
|
121
|
+
},
|
|
122
|
+
record,
|
|
123
|
+
stage(stage, status, message, details = {}) {
|
|
124
|
+
sessionEvent({ kind: 'stage', stage, status });
|
|
125
|
+
state.currentStage = stage;
|
|
126
|
+
return record(message, {
|
|
127
|
+
kind: "stage",
|
|
128
|
+
level: status === "failed" ? "error" : "info",
|
|
129
|
+
stage,
|
|
130
|
+
details: { ...details, status },
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
output(message, options = {}) {
|
|
134
|
+
return record(message, { ...options, kind: options.kind ?? "agent-output", output: true });
|
|
135
|
+
},
|
|
136
|
+
heartbeat,
|
|
137
|
+
async flush() {
|
|
138
|
+
await queue;
|
|
139
|
+
return { error: lastWriteError };
|
|
140
|
+
},
|
|
141
|
+
async close({ status = "completed", stage = state.currentStage, message } = {}) {
|
|
142
|
+
clearInterval(timer);
|
|
143
|
+
if (closed) return { error: lastWriteError };
|
|
144
|
+
if (message) record(message, {
|
|
145
|
+
kind: "lifecycle",
|
|
146
|
+
level: status === "failed" ? "error" : "info",
|
|
147
|
+
stage,
|
|
148
|
+
details: { status },
|
|
149
|
+
});
|
|
150
|
+
state.status = status;
|
|
151
|
+
state.currentStage = stage;
|
|
152
|
+
state.updatedAt = now().toISOString();
|
|
153
|
+
state.lastHeartbeatAt = state.updatedAt;
|
|
154
|
+
persist();
|
|
155
|
+
closed = true;
|
|
156
|
+
await queue;
|
|
157
|
+
return { error: lastWriteError };
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function atomicJson(target, value) {
|
|
163
|
+
const temporary = `${target}.${process.pid}.tmp`;
|
|
164
|
+
await writeFile(temporary, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
165
|
+
await rename(temporary, target);
|
|
166
|
+
}
|