@cbartens/bkit 6.1.0 → 6.2.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/.bkit/README.md +14 -12
- package/.bkit/core/.agents/docs.md +1 -11
- package/.bkit/core/.agents/plugins/bkit/hooks/bkit-gateguard.cjs +30 -9
- package/.bkit/core/.agents/plugins/bkit/hooks/bkit-guardrails.cjs +25 -27
- package/.bkit/core/.agents/rules.md +22 -9
- package/.bkit/core/.bkit/README.md +13 -11
- package/.bkit/core/.bkit/authoring.md +58 -13
- package/.bkit/core/.bkit/cli.md +43 -33
- package/.bkit/core/.bkit/cloud.md +93 -30
- package/.bkit/core/.bkit/install.md +55 -16
- package/.bkit/core/.bkit/jsonl.md +24 -7
- package/.bkit/core/.bkit/pipeline.md +8 -8
- package/.bkit/core/.bkit/prompts.md +160 -43
- package/.bkit/core/.bkit/providers.md +99 -36
- package/.bkit/core/.bkit/templates.md +167 -37
- package/.bkit/core/.bkit/trackers.md +39 -6
- package/.bkit/core/.bkit/workflows.md +344 -136
- package/.bkit/core/.claude/hooks/bkit-guardrails.cjs +50 -27
- package/.bkit/core/.claude/hooks/bkit-preflight.sh +11 -11
- package/.bkit/core/.codex/hooks/bkit-gateguard.cjs +13 -8
- package/.bkit/core/.codex/hooks/bkit-guardrails.cjs +14 -25
- package/.bkit/core/.cursor/hooks/bkit-guardrails.cjs +20 -9
- package/.bkit/core/.cursor/hooks/bkit-preflight.sh +8 -8
- package/.bkit/core/.opencode/plugin/bkit-gateguard.js +14 -4
- package/.bkit/core/.opencode/plugin/bkit-guardrails.js +114 -0
- package/.bkit/core/docs/templates/bkit-audit-accessibility.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-adoption.md +30 -6
- package/.bkit/core/docs/templates/bkit-audit-agents.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-analytics.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-annotations.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-app-performance.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-app-store.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-architecture.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +2 -2
- package/.bkit/core/docs/templates/bkit-audit-config.md +3 -2
- package/.bkit/core/docs/templates/bkit-audit-cost.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-coverage.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-database.md +3 -3
- package/.bkit/core/docs/templates/bkit-audit-dead-code.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-deferrals.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-dependencies.md +3 -3
- package/.bkit/core/docs/templates/bkit-audit-documentation.md +32 -2
- package/.bkit/core/docs/templates/bkit-audit-duplication.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-failures.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-flakes.md +2 -2
- package/.bkit/core/docs/templates/bkit-audit-gates.md +5 -3
- package/.bkit/core/docs/templates/bkit-audit-issues.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-llm.md +2 -2
- package/.bkit/core/docs/templates/bkit-audit-logs.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-models.md +2 -2
- package/.bkit/core/docs/templates/bkit-audit-privacy.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-prompts.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-red-team.md +2 -2
- package/.bkit/core/docs/templates/bkit-audit-reliability.md +3 -2
- package/.bkit/core/docs/templates/bkit-audit-repo.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-security.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-slop.md +1 -1
- package/.bkit/core/docs/templates/bkit-audit-testing.md +3 -2
- package/.bkit/core/docs/templates/bkit-audit-usability.md +1 -1
- package/.bkit/core/docs/templates/bkit-plan.md +38 -8
- package/.bkit/core/engine/checks/after/auth.ts +118 -47
- package/.bkit/core/engine/checks/after/github.ts +161 -37
- package/.bkit/core/engine/checks/after/index.ts +18 -3
- package/.bkit/core/engine/checks/after/tools.ts +245 -1
- package/.bkit/core/engine/checks/before/files.ts +59 -1
- package/.bkit/core/engine/checks/before/index.ts +8 -0
- package/.bkit/core/engine/checks/status-holes.ts +20 -26
- package/.bkit/core/engine/checks/tips/catalogue.ts +107 -10
- package/.bkit/core/engine/checks/tips/detect.ts +36 -3
- package/.bkit/core/engine/checks/tips/index.ts +1 -1
- package/.bkit/core/engine/cli.ts +13 -13
- package/.bkit/core/engine/commands/ci/ci.ts +124 -19
- package/.bkit/core/engine/commands/ci/push-gate.ts +101 -43
- package/.bkit/core/engine/commands/install/doctor.ts +5 -5
- package/.bkit/core/engine/commands/install/install.ts +28 -25
- package/.bkit/core/engine/commands/install/uninstall.ts +42 -30
- package/.bkit/core/engine/commands/install/upgrade.ts +14 -2
- package/.bkit/core/engine/commands/land.ts +2 -3
- package/.bkit/core/engine/commands/lib.ts +394 -39
- package/.bkit/core/engine/commands/registry.ts +1 -1
- package/.bkit/core/engine/commands/workflow/branches.ts +351 -78
- package/.bkit/core/engine/commands/workflow/challenge.ts +63 -14
- package/.bkit/core/engine/commands/workflow/context.ts +71 -10
- package/.bkit/core/engine/commands/workflow/counters.ts +298 -23
- package/.bkit/core/engine/commands/workflow/decisions.ts +260 -125
- package/.bkit/core/engine/commands/workflow/disk.ts +2 -1
- package/.bkit/core/engine/commands/workflow/feature-base.ts +66 -0
- package/.bkit/core/engine/commands/workflow/issues.ts +312 -17
- package/.bkit/core/engine/commands/workflow/junit.ts +204 -0
- package/.bkit/core/engine/commands/workflow/ledgers.ts +904 -99
- package/.bkit/core/engine/commands/workflow/locks.ts +186 -29
- package/.bkit/core/engine/commands/workflow/loops.ts +87 -0
- package/.bkit/core/engine/commands/workflow/open-decision.ts +251 -0
- package/.bkit/core/engine/commands/workflow/plan-blocks.ts +8 -0
- package/.bkit/core/engine/commands/workflow/plans.ts +1 -1
- package/.bkit/core/engine/commands/workflow/prs.ts +311 -16
- package/.bkit/core/engine/commands/workflow/repo.ts +1 -1
- package/.bkit/core/engine/commands/workflow/run-facts.ts +6 -0
- package/.bkit/core/engine/commands/workflow/runs.ts +877 -174
- package/.bkit/core/engine/commands/workflow/runtime.ts +12 -4
- package/.bkit/core/engine/commands/workflow/status.ts +324 -11
- package/.bkit/core/engine/commands/workflow/step-table.ts +41 -15
- package/.bkit/core/engine/commands/workflow/steps.ts +604 -29
- package/.bkit/core/engine/commands/workflow/summary-findings.ts +1 -1
- package/.bkit/core/engine/commands/workflow/summary.ts +314 -8
- package/.bkit/core/engine/commands/workflow/tasks.ts +400 -27
- package/.bkit/core/engine/commands/workflow/tests-base.ts +319 -0
- package/.bkit/core/engine/commands/workflow/tests-changed.ts +138 -74
- package/.bkit/core/engine/commands/workflow/tests-jump.ts +93 -0
- package/.bkit/core/engine/commands/workflow/tests.ts +815 -182
- package/.bkit/core/engine/commands/workflow/tracker.ts +1134 -137
- package/.bkit/core/engine/commands/workflow/workflows.ts +100 -72
- package/.bkit/core/engine/commands/workflow/worktrees.ts +421 -57
- package/.bkit/core/engine/install/cloud-env.ts +38 -10
- package/.bkit/core/engine/install/managed-block.ts +105 -0
- package/.bkit/core/engine/install/manifest.ts +7 -1
- package/.bkit/core/engine/install/modules/claude.ts +6 -2
- package/.bkit/core/engine/install/modules/cloud.ts +84 -95
- package/.bkit/core/engine/install/modules/codex.ts +10 -2
- package/.bkit/core/engine/install/modules/core.ts +202 -26
- package/.bkit/core/engine/install/modules/opencode.ts +18 -23
- package/.bkit/core/engine/install/network-allowlist.ts +8 -3
- package/.bkit/core/engine/install/project-docs.ts +650 -0
- package/.bkit/core/engine/install/template-copies.ts +58 -5
- package/.bkit/core/engine/install/write.ts +128 -7
- package/.bkit/core/engine/ledgers/journal.ts +18 -10
- package/.bkit/core/engine/ledgers/migrate.ts +20 -8
- package/.bkit/core/engine/ledgers/status.ts +604 -8
- package/.bkit/core/engine/ledgers/telemetry.ts +115 -13
- package/.bkit/core/engine/render/trackers.ts +46 -11
- package/.bkit/core/engine/shared/package-manager.ts +7 -3
- package/.bkit/core/engine/shared/settings.ts +12 -1
- package/.bkit/core/engine/shared/text.ts +14 -0
- package/.bkit/core/engine/tools/instructions-sources.ts +1 -0
- package/.bkit/core/engine/tools/instructions.ts +116 -20
- package/.bkit/core/hooks/antigravity-hook.js +20 -4
- package/.bkit/core/hooks/gateguard-edit-payload.js +8 -5
- package/.bkit/core/hooks/gateguard-engine.js +249 -23
- package/.bkit/core/hooks/guardrails-deny.js +167 -0
- package/.bkit/core/hooks/guardrails-read.js +3 -3
- package/.bkit/core/hooks/guardrails-shell.js +1122 -167
- package/.bkit/core/hooks/guardrails-tiers.js +247 -176
- package/.bkit/core/hooks/preflight-tracker.sh +219 -0
- package/.bkit/core/prompts/bkit-audit-generate.md +23 -6
- package/.bkit/core/prompts/bkit-build-challenge-1.md +5 -4
- package/.bkit/core/prompts/bkit-build-challenge-2.md +6 -5
- package/.bkit/core/prompts/bkit-build-install-skills.md +2 -2
- package/.bkit/core/prompts/bkit-build-plan-convert.md +21 -7
- package/.bkit/core/prompts/bkit-build-plan-current.md +5 -4
- package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +22 -17
- package/.bkit/core/prompts/bkit-build-plan-tasks.md +16 -12
- package/.bkit/core/prompts/bkit-build-plan-tests.md +14 -10
- package/.bkit/core/prompts/bkit-build-plan-verify.md +21 -8
- package/.bkit/core/prompts/bkit-debug-abort-handover.md +10 -5
- package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +22 -6
- package/.bkit/core/prompts/bkit-debug-failure-remediate.md +19 -3
- package/.bkit/core/prompts/bkit-debug-failure-reproduce.md +22 -6
- package/.bkit/core/prompts/bkit-debug-failure-soak.md +10 -7
- package/.bkit/core/prompts/bkit-design-brainstorm-ideate-1.md +3 -3
- package/.bkit/core/prompts/bkit-design-brainstorm-ideate-2.md +4 -4
- package/.bkit/core/prompts/bkit-design-brainstorm-ideate-3.md +3 -3
- package/.bkit/core/prompts/bkit-design-generate.md +6 -4
- package/.bkit/core/prompts/bkit-design-organise.md +4 -3
- package/.bkit/core/prompts/bkit-design-review-1.md +5 -4
- package/.bkit/core/prompts/bkit-design-review-2.md +6 -5
- package/.bkit/core/prompts/bkit-design-spike.md +4 -3
- package/.bkit/core/prompts/bkit-design-update.md +6 -4
- package/.bkit/core/prompts/bkit-plan-generate.md +35 -14
- package/.bkit/core/prompts/bkit-plan-review-1.md +9 -7
- package/.bkit/core/prompts/bkit-plan-review-2.md +9 -7
- package/.bkit/core/prompts/bkit-plan-scope-check.md +8 -6
- package/.bkit/core/prompts/bkit-plan-update.md +22 -8
- package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +11 -8
- package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +9 -5
- package/.bkit/core/prompts/bkit-ship-update-docs.md +26 -4
- package/.bkit/core/prompts/bkit-utility-compress-markdown.md +3 -3
- package/.bkit/core/prompts/bkit-utility-update-decisions.md +10 -7
- package/.bkit/core/prompts/bkit-utility-update-deferrals.md +8 -5
- package/.bkit/core/prompts/bkit-utility-update-documentation.md +31 -7
- package/.bkit/core/prompts/bkit-utility-update-failures.md +8 -5
- package/.bkit/core/prompts/bkit-utility-update-knowledge.md +10 -7
- package/.bkit/core/prompts/bkit-utility-update-roadmap.md +4 -4
- package/.bkit/core/prompts/bkit-utility-update-tracker.md +4 -4
- package/.bkit/core/workflows/bkit-auto.md +23 -11
- package/.bkit/core/workflows/bkit-build.md +331 -100
- package/.bkit/core/workflows/bkit-debug.md +135 -64
- package/.bkit/core/workflows/bkit-design.md +58 -32
- package/.bkit/core/workflows/bkit-plan.md +73 -45
- package/.bkit/core/workflows/bkit-ship.md +233 -167
- package/.bkit/shared/.agents/plugins/bkit/hooks.json +1 -1
- package/.bkit/shared/.bkit/.env.example +5 -0
- package/.bkit/shared/.claude/bkit-cloud.md +7 -7
- package/.bkit/shared/.claude/settings.json +5 -1
- package/.bkit/shared/.cursor/bkit-cloud.md +1 -1
- package/.bkit/shared/.cursor/hooks.json +1 -1
- package/.bkit/shared/.opencode/opencode.json +11 -2
- package/.bkit/templates/.claude/hooks/bkit-cloud-bootstrap.sh +27 -0
- package/.bkit/templates/.cursor/Dockerfile +7 -7
- package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-idea.yml +1 -1
- package/.bkit/templates/.github/dependabot.yml +5 -0
- package/.bkit/templates/.github/renovate.json5 +5 -3
- package/.bkit/templates/.github/workflows/bkit-ci.yml +11 -11
- package/.bkit/templates/.github/workflows/bkit-cloud.yml +525 -161
- package/.bkit/templates/.github/workflows/bkit-renovate.yml +2 -2
- package/.bkit/templates/.husky/pre-push +123 -6
- package/.bkit/templates/context/bkit-serena.md +1 -1
- package/.bkit/templates/context/bkit-vercel.md +103 -59
- package/.bkit/templates/docs/architecture.md +98 -0
- package/.bkit/templates/docs/capabilities.md +33 -37
- package/.bkit/templates/docs/components.md +142 -0
- package/.bkit/templates/docs/data-schema.md +87 -0
- package/.bkit/{shared → templates}/docs/deployments.md +128 -142
- package/.bkit/templates/docs/design.md +117 -0
- package/.bkit/templates/docs/noise.md +54 -0
- package/.bkit/templates/docs/observability.md +99 -0
- package/.bkit/templates/docs/security.md +137 -0
- package/.bkit/templates/docs/setup.md +93 -0
- package/.bkit/templates/docs/technology.md +70 -0
- package/.bkit/{shared → templates}/docs/testing.md +200 -87
- package/.bkit/templates/scripts/ci/bkit-check-accepted-divergences.mjs +1 -6
- package/.bkit/templates/scripts/ci/bkit-machine-lock.sh +511 -103
- package/.bkit/templates/scripts/ci/bkit-test-awake.mjs +314 -28
- package/.bkit/templates/scripts/ci/bkit-test-awake.test.mjs +500 -1
- package/.bkit/templates/scripts/graphify/bkit-graphify-graphml.py +1 -1
- package/.bkit/templates/scripts/graphify/bkit-graphify-rollup.py +91 -62
- package/.bkit/templates/scripts/graphify/bkit-graphify.sh +59 -11
- package/.bkit/templates/slop-scan.config.json +2 -0
- package/CHANGELOG.md +79 -0
- package/DETAILS.md +661 -0
- package/README.md +73 -604
- package/dist/checks/after/auth.js +114 -46
- package/dist/checks/after/github.js +138 -35
- package/dist/checks/after/index.js +16 -4
- package/dist/checks/after/tools.js +224 -1
- package/dist/checks/before/files.js +59 -1
- package/dist/checks/before/index.js +7 -1
- package/dist/checks/status-holes.js +20 -21
- package/dist/checks/tips/catalogue.js +99 -10
- package/dist/checks/tips/detect.js +35 -3
- package/dist/checks/tips/index.js +1 -1
- package/dist/cli.js +13 -13
- package/dist/commands/ci/ci.js +113 -20
- package/dist/commands/ci/push-gate.js +79 -44
- package/dist/commands/install/doctor.js +5 -5
- package/dist/commands/install/install.js +23 -22
- package/dist/commands/install/uninstall.js +36 -30
- package/dist/commands/install/upgrade.js +10 -2
- package/dist/commands/land.js +2 -3
- package/dist/commands/lib.js +323 -25
- package/dist/commands/workflow/branches.js +312 -72
- package/dist/commands/workflow/challenge.js +66 -13
- package/dist/commands/workflow/context.js +46 -10
- package/dist/commands/workflow/counters.js +231 -23
- package/dist/commands/workflow/decisions.js +221 -106
- package/dist/commands/workflow/disk.js +2 -1
- package/dist/commands/workflow/feature-base.js +60 -0
- package/dist/commands/workflow/issues.js +243 -15
- package/dist/commands/workflow/junit.js +137 -0
- package/dist/commands/workflow/ledgers.js +726 -112
- package/dist/commands/workflow/locks.js +150 -29
- package/dist/commands/workflow/loops.js +67 -0
- package/dist/commands/workflow/open-decision.js +199 -0
- package/dist/commands/workflow/plan-blocks.js +6 -0
- package/dist/commands/workflow/plans.js +1 -1
- package/dist/commands/workflow/prs.js +272 -16
- package/dist/commands/workflow/repo.js +1 -1
- package/dist/commands/workflow/run-facts.js +6 -0
- package/dist/commands/workflow/runs.js +747 -155
- package/dist/commands/workflow/runtime.js +8 -4
- package/dist/commands/workflow/status.js +259 -13
- package/dist/commands/workflow/step-table.js +41 -15
- package/dist/commands/workflow/steps.js +530 -26
- package/dist/commands/workflow/summary-findings.js +1 -1
- package/dist/commands/workflow/summary.js +289 -8
- package/dist/commands/workflow/tasks.js +294 -27
- package/dist/commands/workflow/tests-base.js +266 -0
- package/dist/commands/workflow/tests-changed.js +130 -56
- package/dist/commands/workflow/tests-jump.js +77 -0
- package/dist/commands/workflow/tests.js +608 -131
- package/dist/commands/workflow/tracker.js +920 -112
- package/dist/commands/workflow/workflows.js +80 -59
- package/dist/commands/workflow/worktrees.js +387 -58
- package/dist/install/cloud-env.js +32 -10
- package/dist/install/managed-block.js +101 -0
- package/dist/install/manifest.js +7 -1
- package/dist/install/modules/claude.js +6 -2
- package/dist/install/modules/cloud.js +73 -86
- package/dist/install/modules/codex.js +9 -2
- package/dist/install/modules/core.js +190 -27
- package/dist/install/modules/opencode.js +18 -23
- package/dist/install/network-allowlist.js +8 -3
- package/dist/install/project-docs.js +551 -0
- package/dist/install/template-copies.js +55 -5
- package/dist/install/write.js +117 -5
- package/dist/ledgers/journal.js +10 -8
- package/dist/ledgers/migrate.js +20 -9
- package/dist/ledgers/status.js +461 -8
- package/dist/ledgers/telemetry.js +71 -13
- package/dist/render/trackers.js +21 -12
- package/dist/shared/package-manager.js +7 -2
- package/dist/shared/settings.js +8 -0
- package/dist/shared/text.js +13 -0
- package/dist/tools/instructions-sources.js +1 -0
- package/dist/tools/instructions.js +97 -24
- package/package.json +23 -15
- package/.bkit/core/hooks/preflight-linear.sh +0 -73
- package/.bkit/shared/docs/architecture.md +0 -16
- package/.bkit/shared/docs/data-schema.md +0 -16
- package/.bkit/shared/docs/design.md +0 -38
- package/.bkit/shared/docs/noise.md +0 -20
- package/.bkit/shared/docs/observability.md +0 -22
- package/.bkit/shared/docs/security.md +0 -62
- package/.bkit/shared/docs/setup.md +0 -52
- package/.bkit/shared/docs/technology.md +0 -15
- /package/.bkit/templates/{.lintstagedrc.json → lintstagedrc.example.json} +0 -0
- /package/.bkit/templates/{.prettierignore → prettierignore.example} +0 -0
|
@@ -9,14 +9,27 @@
|
|
|
9
9
|
// thrown message, a log line or stdout.
|
|
10
10
|
import * as fs from "node:fs";
|
|
11
11
|
import * as path from "node:path";
|
|
12
|
+
import { appendFrictionRow, frictionRow } from "../../ledgers/journal.js";
|
|
12
13
|
import { actionHash, actionKey, alreadyDone, recordAction, runNumber, } from "../../ledgers/status.js";
|
|
13
14
|
import { resolveRepoSlug } from "../../ledgers/telemetry.js";
|
|
14
15
|
import { readBkitConfig } from "../../shared/settings.js";
|
|
15
16
|
import { git, parseJson, runGh } from "../lib.js";
|
|
16
17
|
import { issueIdOf, readIssueTracker } from "./fids.js";
|
|
17
|
-
import { readRunsLenient } from "./locks.js";
|
|
18
|
-
/**
|
|
18
|
+
import { acquirePublicationLock, readRunsLenient, releasePublicationLock } from "./locks.js";
|
|
19
|
+
/** Conservative full Markdown UTF-16 budget, including markers and part labels. */
|
|
19
20
|
export const JIRA_COMMENT_LIMIT = 32_767;
|
|
21
|
+
/**
|
|
22
|
+
* Live contract evidence v1 (2026-11-05): GitHub accepts 262,144 UTF-8 body bytes
|
|
23
|
+
* and rejects the next byte; Linear accepts 100,000 Unicode scalars and rejects
|
|
24
|
+
* the next scalar. Jira's Markdown conversion makes its raw boundary dependent
|
|
25
|
+
* on formatting; both ASCII and non-BMP payloads pass at this safe UTF-16 budget.
|
|
26
|
+
* Keep the selected budgets conservative, rather than promising provider maxima.
|
|
27
|
+
*/
|
|
28
|
+
const COMMENT_BODY_BUDGETS = {
|
|
29
|
+
github: 65_536,
|
|
30
|
+
linear: 100_000,
|
|
31
|
+
jira: JIRA_COMMENT_LIMIT,
|
|
32
|
+
};
|
|
20
33
|
export const JIRA_MCP_URL = "https://mcp.atlassian.com/v2/mcp";
|
|
21
34
|
export const LINEAR_API_URL = "https://api.linear.app/graphql";
|
|
22
35
|
/** Rovo MCP tools the engine calls; `.bkit/trackers.md` names each. */
|
|
@@ -33,6 +46,20 @@ export const JIRA_TOOLS = {
|
|
|
33
46
|
// Cloudflare in front of Atlassian answers error 1010 to a default library User-Agent.
|
|
34
47
|
const USER_AGENT = "bkit-engine (+https://github.com/cbartens/bkit)";
|
|
35
48
|
const TIMEOUT_MS = 30_000;
|
|
49
|
+
function timeLeft(budget) {
|
|
50
|
+
if (!budget)
|
|
51
|
+
return TIMEOUT_MS;
|
|
52
|
+
if (budget.deadline === 0)
|
|
53
|
+
budget.deadline = performance.now() + budget.duration;
|
|
54
|
+
const remaining = budget.deadline - performance.now();
|
|
55
|
+
if (remaining <= 0)
|
|
56
|
+
throw new TrackerError("network", "publication timeout");
|
|
57
|
+
return Math.max(1, Math.floor(Math.min(TIMEOUT_MS, remaining / (budget.reserve ? 2 : 1))));
|
|
58
|
+
}
|
|
59
|
+
function charge(budget, kind) {
|
|
60
|
+
if (budget && ++budget[kind] > (kind === "pages" ? 10 : 3))
|
|
61
|
+
throw new TrackerError("network", `publication ${kind} limit`);
|
|
62
|
+
}
|
|
36
63
|
const SECRET_KEYS = ["BKIT_GH_TOKEN", "BKIT_LINEAR_API_KEY", "BKIT_JIRA_API_KEY"];
|
|
37
64
|
export class TrackerError extends Error {
|
|
38
65
|
reason;
|
|
@@ -145,8 +172,8 @@ export function ghEnv() {
|
|
|
145
172
|
return t ? { GH_TOKEN: t } : {};
|
|
146
173
|
}
|
|
147
174
|
/** `gh` for a tracker call; a failure throws its reason. */
|
|
148
|
-
export function gh(t, args, input) {
|
|
149
|
-
const res = runGh(t.cwd, args, input, ghEnv());
|
|
175
|
+
export function gh(t, args, input, budget) {
|
|
176
|
+
const res = runGh(t.cwd, args, input, ghEnv(), budget ? { timeoutMs: timeLeft(budget) } : {});
|
|
150
177
|
if (!res.ok)
|
|
151
178
|
throw new TrackerError(ghReason(res), `gh ${args[0] ?? ""}: ${res.stderr}`);
|
|
152
179
|
return res.stdout;
|
|
@@ -165,7 +192,7 @@ async function post(url, headers, body, o = {}) {
|
|
|
165
192
|
method: "POST",
|
|
166
193
|
headers: { "content-type": "application/json", "user-agent": USER_AGENT, ...headers },
|
|
167
194
|
body: JSON.stringify(body),
|
|
168
|
-
signal: AbortSignal.timeout(
|
|
195
|
+
signal: AbortSignal.timeout(timeLeft(o.budget)),
|
|
169
196
|
});
|
|
170
197
|
}
|
|
171
198
|
catch (err) {
|
|
@@ -189,9 +216,9 @@ const LINEAR_CODES = {
|
|
|
189
216
|
* One Linear GraphQL call. GraphQL errors map to a reason by `extensions.code` — Linear
|
|
190
217
|
* answers rate limits and auth failures with HTTP 400 and the code in the body.
|
|
191
218
|
*/
|
|
192
|
-
export async function linearGql(query, variables) {
|
|
219
|
+
export async function linearGql(query, variables, budget) {
|
|
193
220
|
const key = secret("BKIT_LINEAR_API_KEY");
|
|
194
|
-
const res = await post(LINEAR_API_URL, { authorization: key }, { query, variables }, { keepErrors: true });
|
|
221
|
+
const res = await post(LINEAR_API_URL, { authorization: key }, { query, variables }, { keepErrors: true, budget });
|
|
195
222
|
const parsed = parseJson(res.text);
|
|
196
223
|
const doc = asRecord(parsed.ok ? parsed.value : null);
|
|
197
224
|
const errors = Array.isArray(doc.errors) ? doc.errors : [];
|
|
@@ -246,13 +273,15 @@ function jiraReason(text) {
|
|
|
246
273
|
}
|
|
247
274
|
export class JiraSession {
|
|
248
275
|
key;
|
|
276
|
+
budget;
|
|
249
277
|
id = 1;
|
|
250
278
|
session = null;
|
|
251
|
-
constructor(key) {
|
|
279
|
+
constructor(key, budget) {
|
|
252
280
|
this.key = key;
|
|
281
|
+
this.budget = budget;
|
|
253
282
|
}
|
|
254
|
-
static open() {
|
|
255
|
-
return new JiraSession(secret("BKIT_JIRA_API_KEY"));
|
|
283
|
+
static open(budget) {
|
|
284
|
+
return new JiraSession(secret("BKIT_JIRA_API_KEY"), budget);
|
|
256
285
|
}
|
|
257
286
|
headers() {
|
|
258
287
|
return {
|
|
@@ -273,13 +302,13 @@ export class JiraSession {
|
|
|
273
302
|
capabilities: {},
|
|
274
303
|
clientInfo: { name: "bkit", version: "engine" },
|
|
275
304
|
},
|
|
276
|
-
});
|
|
305
|
+
}, { budget: this.budget });
|
|
277
306
|
rpcMessage(res.text, res.headers.get("content-type") ?? "", this.id);
|
|
278
307
|
this.session = res.headers.get("mcp-session-id") ?? "";
|
|
279
308
|
await post(JIRA_MCP_URL, this.headers(), {
|
|
280
309
|
jsonrpc: "2.0",
|
|
281
310
|
method: "notifications/initialized",
|
|
282
|
-
});
|
|
311
|
+
}, { budget: this.budget });
|
|
283
312
|
}
|
|
284
313
|
/** `tools/call` one tool; returns its text content parsed as JSON when it is JSON. */
|
|
285
314
|
async call(name, args) {
|
|
@@ -290,7 +319,7 @@ export class JiraSession {
|
|
|
290
319
|
id,
|
|
291
320
|
method: "tools/call",
|
|
292
321
|
params: { name, arguments: args },
|
|
293
|
-
});
|
|
322
|
+
}, { budget: this.budget });
|
|
294
323
|
const msg = rpcMessage(res.text, res.headers.get("content-type") ?? "", id);
|
|
295
324
|
if (msg.error) {
|
|
296
325
|
const message = String(asRecord(msg.error).message ?? "");
|
|
@@ -353,114 +382,358 @@ export function writeBodyFile(cwd, kind, body) {
|
|
|
353
382
|
* a split body opens with the body's first line when it is the generated marker,
|
|
354
383
|
* then `_(part i/n)_`; a single line longer than a part is cut hard.
|
|
355
384
|
*/
|
|
356
|
-
export function splitAt(body, limit) {
|
|
385
|
+
export function splitAt(body, limit, opts = {}) {
|
|
386
|
+
if (opts.verifiedBudget === false)
|
|
387
|
+
return [body];
|
|
357
388
|
if (body.length <= limit)
|
|
358
389
|
return [body];
|
|
390
|
+
if (opts.compact) {
|
|
391
|
+
const marker = body.startsWith("###### ") ? body.split("\n", 1)[0] : "";
|
|
392
|
+
let rest = marker ? body.slice(marker.length + 1) : body;
|
|
393
|
+
const room = limit - marker.length - 64;
|
|
394
|
+
if (room <= 1)
|
|
395
|
+
throw new TrackerError("http-400", "comment limit cannot fit one code point");
|
|
396
|
+
const chunks = [];
|
|
397
|
+
while (rest.length) {
|
|
398
|
+
const cut = /[\uD800-\uDBFF]/.test(rest[room - 1] ?? "") ? room - 1 : room;
|
|
399
|
+
chunks.push(rest.slice(0, cut));
|
|
400
|
+
rest = rest.slice(cut);
|
|
401
|
+
}
|
|
402
|
+
return chunks.map((part, i) => `${marker ? `${marker}\n` : ""}_(part ${i + 1}/${chunks.length})_\n${part}`);
|
|
403
|
+
}
|
|
359
404
|
const lines = body.split("\n");
|
|
360
405
|
const marker = lines[0]?.startsWith("###### ") ? lines.shift() : null;
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
line
|
|
406
|
+
let digits = 2;
|
|
407
|
+
for (;;) {
|
|
408
|
+
const widestPart = "9".repeat(digits);
|
|
409
|
+
const headerRoom = (marker ? marker.length + 1 : 0) + `_(part ${widestPart}/${widestPart})_\n`.length;
|
|
410
|
+
const room = limit - headerRoom;
|
|
411
|
+
const chunks = [];
|
|
412
|
+
let cur = "";
|
|
413
|
+
const flush = () => {
|
|
414
|
+
if (cur !== "")
|
|
415
|
+
chunks.push(cur);
|
|
416
|
+
cur = "";
|
|
417
|
+
};
|
|
418
|
+
for (const raw of lines) {
|
|
419
|
+
let line = raw;
|
|
420
|
+
while (line.length > room) {
|
|
421
|
+
flush();
|
|
422
|
+
const cut = room > 0 && /[\uD800-\uDBFF]/.test(line[room - 1] ?? "") ? room - 1 : room;
|
|
423
|
+
if (cut <= 0)
|
|
424
|
+
throw new TrackerError("http-400", "comment limit cannot fit one code point");
|
|
425
|
+
chunks.push(line.slice(0, cut));
|
|
426
|
+
line = line.slice(cut);
|
|
427
|
+
}
|
|
428
|
+
const next = cur === "" ? line : `${cur}\n${line}`;
|
|
429
|
+
if (next.length > room) {
|
|
430
|
+
flush();
|
|
431
|
+
cur = line;
|
|
432
|
+
}
|
|
433
|
+
else
|
|
434
|
+
cur = next;
|
|
376
435
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
436
|
+
flush();
|
|
437
|
+
const requiredDigits = String(chunks.length).length;
|
|
438
|
+
if (requiredDigits > digits) {
|
|
439
|
+
digits = requiredDigits;
|
|
440
|
+
continue;
|
|
381
441
|
}
|
|
382
|
-
|
|
383
|
-
cur = next;
|
|
442
|
+
return chunks.map((c, i) => [...(marker ? [marker] : []), `_(part ${i + 1}/${chunks.length})_`, c].join("\n"));
|
|
384
443
|
}
|
|
385
|
-
flush();
|
|
386
|
-
return chunks.map((c, i) => [...(marker ? [marker] : []), `_(part ${i + 1}/${chunks.length})_`, c].join("\n"));
|
|
387
444
|
}
|
|
388
|
-
|
|
389
|
-
|
|
445
|
+
function commentEvidence(raw) {
|
|
446
|
+
const author = asRecord(raw.user ?? raw.author);
|
|
447
|
+
const id = raw.databaseId ?? raw.id;
|
|
448
|
+
const authorId = author.databaseId ?? author.accountId ?? author.id;
|
|
449
|
+
const createdAt = raw.created_at ?? raw.createdAt ?? raw.created;
|
|
450
|
+
const updatedAt = raw.updated_at ?? raw.updatedAt ?? raw.updated;
|
|
451
|
+
if ((typeof id !== "string" && typeof id !== "number") ||
|
|
452
|
+
String(id).trim() === "" ||
|
|
453
|
+
typeof createdAt !== "string" ||
|
|
454
|
+
typeof updatedAt !== "string" ||
|
|
455
|
+
!Number.isFinite(Date.parse(createdAt)) ||
|
|
456
|
+
!Number.isFinite(Date.parse(updatedAt)) ||
|
|
457
|
+
typeof raw.body !== "string")
|
|
458
|
+
return undefined;
|
|
459
|
+
return {
|
|
460
|
+
id: String(id),
|
|
461
|
+
authorId: (typeof authorId === "string" || typeof authorId === "number") &&
|
|
462
|
+
String(authorId).trim() !== ""
|
|
463
|
+
? String(authorId)
|
|
464
|
+
: null,
|
|
465
|
+
createdAt,
|
|
466
|
+
updatedAt,
|
|
467
|
+
fingerprint: actionHash(raw.body),
|
|
468
|
+
representation: "markdown",
|
|
469
|
+
sectionIds: [
|
|
470
|
+
...raw.body.matchAll(/(?:######[ \t]+|<h6\b[^>]*>\s*)bkit:section v1 ([a-f0-9]{64})\b/g),
|
|
471
|
+
].map((match) => match[1]),
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
/** Create a comment, or edit the supplied verified ID. Throws `TrackerError` on failure. */
|
|
475
|
+
export async function postComment(t, body, opts = {}) {
|
|
390
476
|
if (t.issue === null)
|
|
391
477
|
throw new TrackerError("not-found", "no issue");
|
|
478
|
+
const limit = t.tracker === "linear"
|
|
479
|
+
? COMMENT_BODY_BUDGETS.linear
|
|
480
|
+
: t.tracker === "github"
|
|
481
|
+
? COMMENT_BODY_BUDGETS.github
|
|
482
|
+
: COMMENT_BODY_BUDGETS.jira;
|
|
483
|
+
if (!opts.commentId && t.tracker !== "jira" && body.length > limit) {
|
|
484
|
+
const parts = splitAt(body, limit, { compact: true });
|
|
485
|
+
let last;
|
|
486
|
+
for (const part of parts)
|
|
487
|
+
last = await postComment(t, part, opts);
|
|
488
|
+
if (!last)
|
|
489
|
+
throw new TrackerError("network", "no comment parts");
|
|
490
|
+
return { url: last.url, parts: parts.length };
|
|
491
|
+
}
|
|
392
492
|
if (t.tracker === "github") {
|
|
493
|
+
charge(opts.budget, "writes");
|
|
393
494
|
const out = gh(t, [
|
|
394
495
|
"api",
|
|
395
|
-
|
|
496
|
+
opts.commentId
|
|
497
|
+
? `repos/${t.ownerRepo}/issues/comments/${encodeURIComponent(opts.commentId)}`
|
|
498
|
+
: `repos/${t.ownerRepo}/issues/${t.issue}/comments`,
|
|
396
499
|
"--method",
|
|
397
|
-
"POST",
|
|
500
|
+
opts.commentId ? "PATCH" : "POST",
|
|
398
501
|
"--input",
|
|
399
502
|
"-",
|
|
400
|
-
], JSON.stringify({ body }));
|
|
503
|
+
], JSON.stringify({ body }), opts.budget);
|
|
401
504
|
const parsed = parseJson(out);
|
|
402
|
-
const
|
|
403
|
-
|
|
505
|
+
const raw = asRecord(parsed.ok ? parsed.value : null);
|
|
506
|
+
const url = raw.html_url;
|
|
507
|
+
const evidence = commentEvidence(raw);
|
|
508
|
+
return {
|
|
509
|
+
url: typeof url === "string" ? url : null,
|
|
510
|
+
parts: 1,
|
|
511
|
+
...(evidence ? { evidence } : {}),
|
|
512
|
+
};
|
|
404
513
|
}
|
|
405
514
|
if (t.tracker === "linear") {
|
|
406
|
-
|
|
407
|
-
const
|
|
515
|
+
charge(opts.budget, "writes");
|
|
516
|
+
const data = await linearGql(opts.commentId
|
|
517
|
+
? "mutation($id: String!, $input: CommentUpdateInput!) { commentUpdate(id: $id, input: $input) { success comment { id body url createdAt updatedAt user { id } } } }"
|
|
518
|
+
: "mutation($input: CommentCreateInput!) { commentCreate(input: $input) { success comment { id body url createdAt updatedAt user { id } } } }", opts.commentId
|
|
519
|
+
? { id: opts.commentId, input: { body } }
|
|
520
|
+
: { input: { issueId: t.issue, body } }, opts.budget);
|
|
521
|
+
const created = asRecord(opts.commentId ? data.commentUpdate : data.commentCreate);
|
|
408
522
|
if (created.success !== true)
|
|
409
|
-
throw new TrackerError("http-400", "linear:
|
|
410
|
-
const
|
|
411
|
-
|
|
523
|
+
throw new TrackerError("http-400", "linear: comment mutation failed");
|
|
524
|
+
const raw = asRecord(created.comment);
|
|
525
|
+
const url = raw.url;
|
|
526
|
+
const evidence = commentEvidence(raw);
|
|
527
|
+
return {
|
|
528
|
+
url: typeof url === "string" ? url : null,
|
|
529
|
+
parts: 1,
|
|
530
|
+
...(evidence ? { evidence } : {}),
|
|
531
|
+
};
|
|
412
532
|
}
|
|
413
533
|
if (t.tracker === "jira") {
|
|
534
|
+
if (opts.commentId &&
|
|
535
|
+
(!opts.evidence ||
|
|
536
|
+
opts.evidence.id !== opts.commentId ||
|
|
537
|
+
!opts.evidence.authorId ||
|
|
538
|
+
opts.evidence.representation !== "markdown" ||
|
|
539
|
+
!/^[a-f0-9]{64}$/.test(opts.evidence.fingerprint) ||
|
|
540
|
+
!Number.isFinite(Date.parse(opts.evidence.createdAt)) ||
|
|
541
|
+
!Number.isFinite(Date.parse(opts.evidence.updatedAt))))
|
|
542
|
+
throw new TrackerError("unsupported", "jira: comment edit requires verified evidence");
|
|
414
543
|
const site = jiraSite(t);
|
|
415
|
-
const jira = JiraSession.open();
|
|
544
|
+
const jira = JiraSession.open(opts.budget);
|
|
416
545
|
const parts = splitAt(body, JIRA_COMMENT_LIMIT);
|
|
546
|
+
if (opts.commentId && parts.length !== 1)
|
|
547
|
+
throw new TrackerError("unsupported", "jira: split bodies require new comments");
|
|
417
548
|
let id;
|
|
549
|
+
let observed = false;
|
|
550
|
+
let returnedBody;
|
|
418
551
|
for (const part of parts) {
|
|
419
|
-
|
|
552
|
+
charge(opts.budget, "writes");
|
|
553
|
+
const res = asRecord(await jira.call(JIRA_TOOLS.comment, {
|
|
420
554
|
cloudId: site,
|
|
421
555
|
issueIdOrKey: t.issue,
|
|
422
556
|
commentBody: part,
|
|
423
|
-
|
|
424
|
-
|
|
557
|
+
...(opts.commentId
|
|
558
|
+
? {
|
|
559
|
+
commentId: opts.commentId,
|
|
560
|
+
contentFormat: "markdown",
|
|
561
|
+
responseContentFormat: "markdown",
|
|
562
|
+
}
|
|
563
|
+
: {}),
|
|
564
|
+
}));
|
|
565
|
+
const raw = res.data === undefined ? res : asRecord(res.data);
|
|
566
|
+
id = raw.commentId ?? raw.id;
|
|
567
|
+
observed = typeof raw.commentId === "string";
|
|
568
|
+
returnedBody =
|
|
569
|
+
raw.appliedContentFormat === "markdown" && typeof raw.body === "string"
|
|
570
|
+
? raw.body
|
|
571
|
+
: undefined;
|
|
425
572
|
}
|
|
426
573
|
const base = `${site}/browse/${t.issue}`;
|
|
427
|
-
|
|
574
|
+
const posted = {
|
|
428
575
|
url: id === undefined ? base : `${base}?focusedCommentId=${String(id)}`,
|
|
429
576
|
parts: parts.length,
|
|
430
577
|
};
|
|
578
|
+
if (observed && parts.length === 1) {
|
|
579
|
+
try {
|
|
580
|
+
const latest = await jiraComments(t, 1, jira, opts.budget);
|
|
581
|
+
const evidence = latest.find((comment) => comment.evidence !== undefined &&
|
|
582
|
+
comment.evidence.id === id &&
|
|
583
|
+
returnedBody !== undefined &&
|
|
584
|
+
comment.evidence.fingerprint === actionHash(returnedBody) &&
|
|
585
|
+
(!opts.evidence || comment.evidence.authorId === opts.evidence.authorId))?.evidence;
|
|
586
|
+
return { ...posted, ...(evidence ? { evidence } : {}) };
|
|
587
|
+
}
|
|
588
|
+
catch {
|
|
589
|
+
// An acknowledged mutation stays successful when optional evidence is unavailable.
|
|
590
|
+
return posted;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return posted;
|
|
431
594
|
}
|
|
432
595
|
throw new TrackerError("not-found", "issue tracker none");
|
|
433
596
|
}
|
|
434
597
|
/** The issue's latest `n` comments (body + url), newest last. */
|
|
435
|
-
export async function recentComments(t, n) {
|
|
436
|
-
if (t.issue === null)
|
|
598
|
+
export async function recentComments(t, n, budget) {
|
|
599
|
+
if (t.issue === null || !Number.isInteger(n) || n <= 0)
|
|
437
600
|
return [];
|
|
438
601
|
if (t.tracker === "github") {
|
|
439
602
|
const [owner, name] = t.ownerRepo.split("/");
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
603
|
+
const nodes = [];
|
|
604
|
+
const cursors = new Set();
|
|
605
|
+
let before;
|
|
606
|
+
for (let page = 0; page < 10; page++) {
|
|
607
|
+
charge(budget, "pages");
|
|
608
|
+
const out = gh(t, [
|
|
609
|
+
"api",
|
|
610
|
+
"graphql",
|
|
611
|
+
"-f",
|
|
612
|
+
`query=query($o:String!,$r:String!,$n:Int!,$before:String){repository(owner:$o,name:$r){issue(number:$n){comments(last:${Math.min(n, 100)},before:$before){nodes{id databaseId body url author{__typename ... on User{id databaseId} ... on Bot{id databaseId}} createdAt updatedAt} pageInfo{hasPreviousPage startCursor}}}}}`,
|
|
613
|
+
"-F",
|
|
614
|
+
`o=${owner ?? ""}`,
|
|
615
|
+
"-F",
|
|
616
|
+
`r=${name ?? ""}`,
|
|
617
|
+
"-F",
|
|
618
|
+
`n=${t.issue}`,
|
|
619
|
+
...(before ? ["-f", `before=${before}`] : []),
|
|
620
|
+
], undefined, budget);
|
|
621
|
+
const parsed = parseJson(out);
|
|
622
|
+
const comments = asRecord(asRecord(asRecord(asRecord(asRecord(parsed.ok ? parsed.value : null).data).repository).issue).comments);
|
|
623
|
+
if (!Array.isArray(comments.nodes))
|
|
624
|
+
throw new TrackerError("http-200", "github: comment page returned no nodes");
|
|
625
|
+
nodes.unshift(...comments.nodes);
|
|
626
|
+
const info = asRecord(comments.pageInfo);
|
|
627
|
+
if (info.hasPreviousPage !== true || nodes.length >= n)
|
|
628
|
+
break;
|
|
629
|
+
const cursor = info.startCursor;
|
|
630
|
+
if (typeof cursor !== "string" || cursor === "" || cursors.has(cursor) || page === 9)
|
|
631
|
+
throw new TrackerError("http-200", "github: comment pagination is incomplete");
|
|
632
|
+
cursors.add(cursor);
|
|
633
|
+
before = cursor;
|
|
634
|
+
}
|
|
635
|
+
return nodes.map((c) => {
|
|
456
636
|
const r = asRecord(c);
|
|
457
|
-
|
|
637
|
+
const evidence = commentEvidence(r);
|
|
638
|
+
return {
|
|
639
|
+
body: String(r.body ?? ""),
|
|
640
|
+
url: typeof r.url === "string" ? r.url : null,
|
|
641
|
+
...(evidence ? { evidence } : {}),
|
|
642
|
+
};
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
if (t.tracker === "linear") {
|
|
646
|
+
const nodes = [];
|
|
647
|
+
const cursors = new Set();
|
|
648
|
+
let after = null;
|
|
649
|
+
for (let page = 0; page < 10; page++) {
|
|
650
|
+
charge(budget, "pages");
|
|
651
|
+
const data = await linearGql("query($id: String!, $after: String) { issue(id: $id) { comments(first: 100, after: $after) { nodes { id body url createdAt updatedAt user { id } } pageInfo { hasNextPage endCursor } } } }", { id: t.issue, after }, budget);
|
|
652
|
+
const comments = asRecord(asRecord(data.issue).comments);
|
|
653
|
+
if (!Array.isArray(comments.nodes))
|
|
654
|
+
throw new TrackerError("http-200", "linear: comment page returned no nodes");
|
|
655
|
+
nodes.push(...comments.nodes);
|
|
656
|
+
const info = asRecord(comments.pageInfo);
|
|
657
|
+
if (info.hasNextPage !== true)
|
|
658
|
+
break;
|
|
659
|
+
const cursor = info.endCursor;
|
|
660
|
+
if (typeof cursor !== "string" || cursor === "" || cursors.has(cursor) || page === 9)
|
|
661
|
+
throw new TrackerError("http-200", "linear: comment pagination is incomplete");
|
|
662
|
+
cursors.add(cursor);
|
|
663
|
+
after = cursor;
|
|
664
|
+
}
|
|
665
|
+
const ordered = nodes.map((raw) => {
|
|
666
|
+
const comment = asRecord(raw);
|
|
667
|
+
const created = typeof comment.createdAt === "string" ? Date.parse(comment.createdAt) : NaN;
|
|
668
|
+
if (!Number.isFinite(created))
|
|
669
|
+
throw new TrackerError("http-200", "linear: comment creation ordering is incomplete");
|
|
670
|
+
return { comment, created };
|
|
671
|
+
});
|
|
672
|
+
return ordered
|
|
673
|
+
.sort((a, b) => a.created - b.created)
|
|
674
|
+
.slice(-n)
|
|
675
|
+
.map(({ comment: raw }) => {
|
|
676
|
+
const evidence = commentEvidence(raw);
|
|
677
|
+
return {
|
|
678
|
+
body: str(raw.body),
|
|
679
|
+
url: typeof raw.url === "string" ? raw.url : null,
|
|
680
|
+
...(evidence ? { evidence } : {}),
|
|
681
|
+
};
|
|
458
682
|
});
|
|
459
683
|
}
|
|
460
|
-
|
|
461
|
-
|
|
684
|
+
if (t.tracker === "jira")
|
|
685
|
+
return jiraComments(t, n, JiraSession.open(budget), budget);
|
|
686
|
+
return [];
|
|
687
|
+
}
|
|
688
|
+
async function jiraComments(t, n, jira, budget) {
|
|
689
|
+
const site = jiraSite(t);
|
|
690
|
+
const comments = [];
|
|
691
|
+
for (let page = 0; page < 10; page++) {
|
|
692
|
+
charge(budget, "pages");
|
|
693
|
+
const response = asRecord(await jira.call(JIRA_TOOLS.comments, {
|
|
694
|
+
cloudId: site,
|
|
695
|
+
issueIdOrKey: t.issue,
|
|
696
|
+
startAt: comments.length,
|
|
697
|
+
maxResults: Math.min(n - comments.length, 100),
|
|
698
|
+
orderBy: "-created",
|
|
699
|
+
responseContentFormat: "markdown",
|
|
700
|
+
view: "evidence",
|
|
701
|
+
}));
|
|
702
|
+
const data = response.data === undefined ? response : asRecord(response.data);
|
|
703
|
+
if (!Array.isArray(data.comments))
|
|
704
|
+
throw new TrackerError("http-200", "jira: comment page returned no comments");
|
|
705
|
+
for (const item of data.comments) {
|
|
706
|
+
const raw = asRecord(item);
|
|
707
|
+
const candidate = data.appliedContentFormat === "markdown" ? commentEvidence(raw) : undefined;
|
|
708
|
+
const evidence = candidate?.authorId ? candidate : undefined;
|
|
709
|
+
comments.push({
|
|
710
|
+
body: textOf(raw.body),
|
|
711
|
+
url: typeof raw.id === "string"
|
|
712
|
+
? `${site}/browse/${t.issue}?focusedCommentId=${raw.id}`
|
|
713
|
+
: null,
|
|
714
|
+
...(evidence ? { evidence } : {}),
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
if (data.isLast !== false || comments.length >= n)
|
|
718
|
+
return comments.slice(0, n).reverse();
|
|
719
|
+
if (data.comments.length === 0)
|
|
720
|
+
throw new TrackerError("http-200", "jira: comment pagination is incomplete");
|
|
721
|
+
}
|
|
722
|
+
throw new TrackerError("http-200", "jira: comment pagination is incomplete");
|
|
462
723
|
}
|
|
463
724
|
const str = (v) => (typeof v === "string" ? v : "");
|
|
725
|
+
/**
|
|
726
|
+
* One terminal judgement for every tracker, by state category: GitHub `closed`, Linear
|
|
727
|
+
* state type `completed` / `canceled`, Jira status category `done`. Renamed states
|
|
728
|
+
* stay judged right because the name is never read.
|
|
729
|
+
*/
|
|
730
|
+
function isTerminal(tracker, state, category) {
|
|
731
|
+
if (tracker === "github")
|
|
732
|
+
return state === "closed";
|
|
733
|
+
if (tracker === "linear")
|
|
734
|
+
return category === "completed" || category === "canceled";
|
|
735
|
+
return category === "done";
|
|
736
|
+
}
|
|
464
737
|
// ADF nodes whose children are inline text; every other node's children are blocks, one per line.
|
|
465
738
|
const ADF_INLINE = new Set(["paragraph", "heading", "codeBlock", "mediaSingle"]);
|
|
466
739
|
/** An Atlassian Document Format node as text: headings as `#` lines, blocks one per line. */
|
|
@@ -517,13 +790,14 @@ export async function fetchIssue(t) {
|
|
|
517
790
|
url: str(issue.html_url),
|
|
518
791
|
state: str(issue.state),
|
|
519
792
|
labels: (Array.isArray(issue.labels) ? issue.labels : []).map((l) => typeof l === "string" ? l : str(asRecord(l).name)),
|
|
793
|
+
terminal: isTerminal("github", str(issue.state), ""),
|
|
520
794
|
body: str(issue.body),
|
|
521
795
|
comments,
|
|
522
796
|
};
|
|
523
797
|
}
|
|
524
798
|
if (t.tracker === "linear") {
|
|
525
799
|
const page = "nodes { body createdAt } pageInfo { hasNextPage endCursor }";
|
|
526
|
-
const data = await linearGql(`query($id: String!) { issue(id: $id) { identifier title url description state { name } labels { nodes { name } } comments(first: 250) { ${page} } } }`, { id: t.issue });
|
|
800
|
+
const data = await linearGql(`query($id: String!) { issue(id: $id) { identifier title url description state { name type } labels { nodes { name } } comments(first: 250) { ${page} } } }`, { id: t.issue });
|
|
527
801
|
const issue = asRecord(data.issue);
|
|
528
802
|
if (!data.issue)
|
|
529
803
|
throw new TrackerError("not-found", `linear: ${t.issue} not found`);
|
|
@@ -544,6 +818,7 @@ export async function fetchIssue(t) {
|
|
|
544
818
|
url: str(issue.url),
|
|
545
819
|
state: str(asRecord(issue.state).name),
|
|
546
820
|
labels: nodes(issue.labels).map((l) => str(asRecord(l).name)),
|
|
821
|
+
terminal: isTerminal("linear", "", str(asRecord(issue.state).type)),
|
|
547
822
|
body: str(issue.description),
|
|
548
823
|
comments,
|
|
549
824
|
};
|
|
@@ -554,6 +829,7 @@ export async function fetchIssue(t) {
|
|
|
554
829
|
const raw = asRecord(await jira.call(JIRA_TOOLS.getIssue, {
|
|
555
830
|
cloudId: site,
|
|
556
831
|
issueIdOrKey: t.issue,
|
|
832
|
+
// The evidence view carries `fields.status` with its `statusCategory`, read below.
|
|
557
833
|
view: "evidence",
|
|
558
834
|
}));
|
|
559
835
|
const fields = asRecord(raw.fields);
|
|
@@ -590,6 +866,7 @@ export async function fetchIssue(t) {
|
|
|
590
866
|
url: `${site}/browse/${t.issue}`,
|
|
591
867
|
state: str(asRecord(fields.status).name),
|
|
592
868
|
labels: (Array.isArray(fields.labels) ? fields.labels : []).map((l) => String(l)),
|
|
869
|
+
terminal: isTerminal("jira", "", str(asRecord(asRecord(fields.status).statusCategory).key)),
|
|
593
870
|
body: textOf(description),
|
|
594
871
|
comments,
|
|
595
872
|
};
|
|
@@ -751,7 +1028,7 @@ export async function readBlockers(t) {
|
|
|
751
1028
|
const numbers = parseBlockedBy(str(asRecord(parsed.ok ? parsed.value : null).body));
|
|
752
1029
|
return numbers.map((n) => {
|
|
753
1030
|
const state = gh(t, ["api", `repos/${t.ownerRepo}/issues/${n}`, "--jq", ".state"]).trim();
|
|
754
|
-
return { id: `#${n}`, state, open: state
|
|
1031
|
+
return { id: `#${n}`, state, open: !isTerminal("github", state, "") };
|
|
755
1032
|
});
|
|
756
1033
|
}
|
|
757
1034
|
if (t.tracker === "linear") {
|
|
@@ -768,7 +1045,7 @@ export async function readBlockers(t) {
|
|
|
768
1045
|
return {
|
|
769
1046
|
id: str(issue.identifier),
|
|
770
1047
|
state: str(state.name),
|
|
771
|
-
open: !
|
|
1048
|
+
open: !isTerminal("linear", "", str(state.type)),
|
|
772
1049
|
};
|
|
773
1050
|
});
|
|
774
1051
|
}
|
|
@@ -786,7 +1063,7 @@ export async function readBlockers(t) {
|
|
|
786
1063
|
return {
|
|
787
1064
|
id: str(issue.key),
|
|
788
1065
|
state: str(status.name),
|
|
789
|
-
open: str(asRecord(status.statusCategory).key)
|
|
1066
|
+
open: !isTerminal("jira", "", str(asRecord(status.statusCategory).key)),
|
|
790
1067
|
};
|
|
791
1068
|
});
|
|
792
1069
|
}
|
|
@@ -864,6 +1141,212 @@ export function skipOf(t) {
|
|
|
864
1141
|
return { skipped: "no-issue" };
|
|
865
1142
|
return null;
|
|
866
1143
|
}
|
|
1144
|
+
function progressBinding(t, o) {
|
|
1145
|
+
const common = git(t.cwd, ["rev-parse", "--path-format=absolute", "--git-common-dir"]);
|
|
1146
|
+
if (!common)
|
|
1147
|
+
return null;
|
|
1148
|
+
const target = [t.tracker, t.ownerRepo || t.jiraSite || t.linearTeam || "", t.issue].join("/");
|
|
1149
|
+
if (o.publication && o.key) {
|
|
1150
|
+
const { invocation, workflow } = o.publication;
|
|
1151
|
+
return {
|
|
1152
|
+
target,
|
|
1153
|
+
invocation,
|
|
1154
|
+
workflow,
|
|
1155
|
+
file: path.join(common, "bkit-outbox", "progress", `${actionHash(`${target}\0${invocation}`)}.json`),
|
|
1156
|
+
priorDelivery: false,
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
if (!o.file || !o.key)
|
|
1160
|
+
return null;
|
|
1161
|
+
const [workflow, runText, step, group, attempt] = o.key.split("/");
|
|
1162
|
+
if (!workflow || !/^\d+$/.test(runText ?? ""))
|
|
1163
|
+
return null;
|
|
1164
|
+
if (step === "failure")
|
|
1165
|
+
return null;
|
|
1166
|
+
const runs = readRunsLenient(o.file).filter((run) => run.workflow === workflow);
|
|
1167
|
+
const run = runs[Number(runText) - 1];
|
|
1168
|
+
if (!run)
|
|
1169
|
+
return null;
|
|
1170
|
+
const fid = path.basename(path.dirname(o.file));
|
|
1171
|
+
const issue = issueIdOf(fid, t.tracker);
|
|
1172
|
+
if (issue !== null && issue !== t.issue)
|
|
1173
|
+
return "conflict";
|
|
1174
|
+
if (issue === null)
|
|
1175
|
+
return null;
|
|
1176
|
+
let invocation = actionHash([path.resolve(o.file), workflow, runText, run.startedAt].join("\0"));
|
|
1177
|
+
const association = runPublicationFile(common, o.file, workflow, run.startedAt);
|
|
1178
|
+
if (fs.existsSync(association)) {
|
|
1179
|
+
const parsed = parseJson(fs.readFileSync(association, "utf-8"));
|
|
1180
|
+
const saved = asRecord(parsed.ok ? parsed.value : null);
|
|
1181
|
+
if (saved.version === 1 && saved.workflow === workflow && typeof saved.invocation === "string")
|
|
1182
|
+
invocation = saved.invocation;
|
|
1183
|
+
}
|
|
1184
|
+
const at = run.steps.find((row) => row.step === step &&
|
|
1185
|
+
(group === "-" || row.group === Number(group)) &&
|
|
1186
|
+
(attempt === "-" || row.attempt === Number(attempt)))?.at;
|
|
1187
|
+
const prefix = `${workflow}/${runText}/`;
|
|
1188
|
+
const priorDelivery = fs
|
|
1189
|
+
.readFileSync(o.file, "utf-8")
|
|
1190
|
+
.split("\n")
|
|
1191
|
+
.some((line) => {
|
|
1192
|
+
const parsed = parseJson(line);
|
|
1193
|
+
const row = asRecord(parsed.ok ? parsed.value : null);
|
|
1194
|
+
return (row.type === "engine-action" && typeof row.key === "string" && row.key.startsWith(prefix));
|
|
1195
|
+
});
|
|
1196
|
+
return {
|
|
1197
|
+
target,
|
|
1198
|
+
invocation,
|
|
1199
|
+
workflow,
|
|
1200
|
+
file: path.join(common, "bkit-outbox", "progress", `${actionHash(`${target}\0${invocation}`)}.json`),
|
|
1201
|
+
...(at && Number.isFinite(Date.parse(at)) ? { at } : {}),
|
|
1202
|
+
priorDelivery,
|
|
1203
|
+
};
|
|
1204
|
+
}
|
|
1205
|
+
function readProgress(binding) {
|
|
1206
|
+
if (!fs.existsSync(binding.file))
|
|
1207
|
+
return null;
|
|
1208
|
+
const parsed = parseJson(fs.readFileSync(binding.file, "utf-8"));
|
|
1209
|
+
const raw = asRecord(parsed.ok ? parsed.value : null);
|
|
1210
|
+
const comment = asRecord(raw.comment);
|
|
1211
|
+
const evidence = asRecord(comment.evidence);
|
|
1212
|
+
if (raw.version !== 1 ||
|
|
1213
|
+
raw.target !== binding.target ||
|
|
1214
|
+
raw.invocation !== binding.invocation ||
|
|
1215
|
+
raw.workflow !== binding.workflow ||
|
|
1216
|
+
typeof raw.source !== "string" ||
|
|
1217
|
+
!Array.isArray(raw.sections) ||
|
|
1218
|
+
typeof evidence.id !== "string" ||
|
|
1219
|
+
typeof evidence.authorId !== "string" ||
|
|
1220
|
+
typeof evidence.fingerprint !== "string" ||
|
|
1221
|
+
typeof evidence.createdAt !== "string" ||
|
|
1222
|
+
typeof evidence.updatedAt !== "string" ||
|
|
1223
|
+
evidence.representation !== "markdown" ||
|
|
1224
|
+
!Array.isArray(evidence.sectionIds) ||
|
|
1225
|
+
!evidence.sectionIds.every((id) => typeof id === "string") ||
|
|
1226
|
+
(comment.url !== null && typeof comment.url !== "string"))
|
|
1227
|
+
return null;
|
|
1228
|
+
const sections = [];
|
|
1229
|
+
for (const value of raw.sections) {
|
|
1230
|
+
const section = asRecord(value);
|
|
1231
|
+
if (typeof section.id !== "string" ||
|
|
1232
|
+
typeof section.key !== "string" ||
|
|
1233
|
+
typeof section.hash !== "string" ||
|
|
1234
|
+
(section.at !== undefined && typeof section.at !== "string"))
|
|
1235
|
+
return null;
|
|
1236
|
+
sections.push({
|
|
1237
|
+
id: section.id,
|
|
1238
|
+
key: section.key,
|
|
1239
|
+
hash: section.hash,
|
|
1240
|
+
...(typeof section.at === "string" ? { at: section.at } : {}),
|
|
1241
|
+
});
|
|
1242
|
+
}
|
|
1243
|
+
return {
|
|
1244
|
+
version: 1,
|
|
1245
|
+
target: binding.target,
|
|
1246
|
+
invocation: binding.invocation,
|
|
1247
|
+
workflow: binding.workflow,
|
|
1248
|
+
source: raw.source,
|
|
1249
|
+
comment: {
|
|
1250
|
+
url: comment.url,
|
|
1251
|
+
evidence: {
|
|
1252
|
+
id: evidence.id,
|
|
1253
|
+
authorId: evidence.authorId,
|
|
1254
|
+
fingerprint: evidence.fingerprint,
|
|
1255
|
+
createdAt: evidence.createdAt,
|
|
1256
|
+
updatedAt: evidence.updatedAt,
|
|
1257
|
+
representation: "markdown",
|
|
1258
|
+
sectionIds: evidence.sectionIds,
|
|
1259
|
+
},
|
|
1260
|
+
},
|
|
1261
|
+
sections,
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
function progressSection(body, binding, key) {
|
|
1265
|
+
const hash = actionHash(body);
|
|
1266
|
+
const section = {
|
|
1267
|
+
id: actionHash(`${key}\0${hash}`),
|
|
1268
|
+
key,
|
|
1269
|
+
hash,
|
|
1270
|
+
...(binding.at ? { at: binding.at } : {}),
|
|
1271
|
+
};
|
|
1272
|
+
const rest = body.split("\n").slice(1).join("\n");
|
|
1273
|
+
const source = stampMarker([
|
|
1274
|
+
GENERATED_MARKER,
|
|
1275
|
+
`###### bkit:progress v1 ${binding.invocation} 1 1`,
|
|
1276
|
+
`###### bkit:section v1 ${section.id} 1/1`,
|
|
1277
|
+
...(binding.at ? [`Reported at ${binding.at}`, ""] : []),
|
|
1278
|
+
rest,
|
|
1279
|
+
].join("\n"));
|
|
1280
|
+
return { section, source };
|
|
1281
|
+
}
|
|
1282
|
+
function saveProgress(binding, source, section, posted, priorSections = []) {
|
|
1283
|
+
if (!posted.evidence?.authorId)
|
|
1284
|
+
return;
|
|
1285
|
+
const state = {
|
|
1286
|
+
version: 1,
|
|
1287
|
+
target: binding.target,
|
|
1288
|
+
invocation: binding.invocation,
|
|
1289
|
+
workflow: binding.workflow,
|
|
1290
|
+
source,
|
|
1291
|
+
comment: { url: posted.url, evidence: posted.evidence },
|
|
1292
|
+
sections: [...priorSections, section],
|
|
1293
|
+
};
|
|
1294
|
+
fs.mkdirSync(path.dirname(binding.file), { recursive: true });
|
|
1295
|
+
fs.writeFileSync(binding.file, JSON.stringify(state), { mode: 0o600 });
|
|
1296
|
+
}
|
|
1297
|
+
export function commentDiagnostic(o, text, warnings) {
|
|
1298
|
+
const detail = redact(text);
|
|
1299
|
+
try {
|
|
1300
|
+
o.log(detail);
|
|
1301
|
+
}
|
|
1302
|
+
catch {
|
|
1303
|
+
warnings.push(`${detail}; comment diagnostic unavailable`);
|
|
1304
|
+
}
|
|
1305
|
+
const workflow = o.publication?.workflow ?? o.workflow ?? o.key?.split("/")[0];
|
|
1306
|
+
if (!o.cwd ||
|
|
1307
|
+
!workflow ||
|
|
1308
|
+
!["design", "plan", "build", "debug", "ship", "auto"].includes(workflow))
|
|
1309
|
+
return;
|
|
1310
|
+
const stage = /receipt|engine-action/.test(text)
|
|
1311
|
+
? "receipt"
|
|
1312
|
+
: /lock/.test(text)
|
|
1313
|
+
? "lock"
|
|
1314
|
+
: /recognition|reconciliation|postflight/.test(text)
|
|
1315
|
+
? "reconciliation"
|
|
1316
|
+
: /source|cache/.test(text)
|
|
1317
|
+
? "source"
|
|
1318
|
+
: /association/.test(text)
|
|
1319
|
+
? "association"
|
|
1320
|
+
: "delivery";
|
|
1321
|
+
try {
|
|
1322
|
+
const root = git(o.cwd, ["rev-parse", "--show-toplevel"]) ?? o.cwd;
|
|
1323
|
+
const row = frictionRow({
|
|
1324
|
+
workflow,
|
|
1325
|
+
step: `comment-${stage}`,
|
|
1326
|
+
noun: "tracker",
|
|
1327
|
+
verb: "comment",
|
|
1328
|
+
code: null,
|
|
1329
|
+
bookRoot: root,
|
|
1330
|
+
});
|
|
1331
|
+
row.summary = `Workflow comment ${stage} unavailable`;
|
|
1332
|
+
row.description = `${workflow} comment reporting encountered an exhausted ${stage} problem; substantive workflow work continues.`;
|
|
1333
|
+
row.remediation =
|
|
1334
|
+
"Inspect tracker availability or local comment state; delivery may start a fresh comment.";
|
|
1335
|
+
if (o.knownUrl)
|
|
1336
|
+
row.references = [o.knownUrl];
|
|
1337
|
+
if (appendFrictionRow(root, row).ok)
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
catch {
|
|
1341
|
+
warnings.push("[bkit-tracker] comment friction unavailable");
|
|
1342
|
+
}
|
|
1343
|
+
try {
|
|
1344
|
+
o.log("[bkit-tracker] comment friction unavailable; continuing workflow");
|
|
1345
|
+
}
|
|
1346
|
+
catch {
|
|
1347
|
+
warnings.push("[bkit-tracker] friction diagnostic unavailable");
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
867
1350
|
/** The latest of `recent` that is `body` posted before: same marker hash, else same text. */
|
|
868
1351
|
function samePosted(recent, body) {
|
|
869
1352
|
const hash = markerHashOf(body);
|
|
@@ -878,41 +1361,237 @@ function samePosted(recent, body) {
|
|
|
878
1361
|
* written after a successful post is `done: true` with a `warning`.
|
|
879
1362
|
*/
|
|
880
1363
|
export async function postCommentOnce(t, body, o) {
|
|
1364
|
+
o = { ...o, cwd: t.cwd };
|
|
881
1365
|
const skip = skipOf(t);
|
|
882
1366
|
if (skip)
|
|
883
1367
|
return skip;
|
|
884
1368
|
const action = "comment";
|
|
885
1369
|
const hash = actionHash(body);
|
|
886
1370
|
let posted;
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
1371
|
+
let binding = null;
|
|
1372
|
+
let outgoing = body;
|
|
1373
|
+
let section;
|
|
1374
|
+
const warnings = [];
|
|
1375
|
+
let cached = null;
|
|
1376
|
+
let lock = null;
|
|
1377
|
+
const routine = o.purpose === "routine";
|
|
1378
|
+
let aggregate = false;
|
|
1379
|
+
const budget = {
|
|
1380
|
+
deadline: 0,
|
|
1381
|
+
duration: Math.max(1, Math.min(60_000, o.budgetMs ?? 60_000)),
|
|
1382
|
+
pages: 0,
|
|
1383
|
+
writes: 0,
|
|
1384
|
+
reserve: true,
|
|
1385
|
+
};
|
|
1386
|
+
const seal = () => {
|
|
1387
|
+
if (binding && binding !== "conflict") {
|
|
1388
|
+
try {
|
|
1389
|
+
fs.rmSync(binding.file, { force: true });
|
|
1390
|
+
}
|
|
1391
|
+
catch {
|
|
1392
|
+
commentDiagnostic(o, "[bkit-tracker] progress seal unavailable", warnings);
|
|
1393
|
+
}
|
|
897
1394
|
}
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
const reason = classifyFailure(err);
|
|
902
|
-
o.log(redact(`tracker ${action} failed (${reason}): ${err.message ?? String(err)}`));
|
|
903
|
-
return { done: false, action, reason };
|
|
904
|
-
}
|
|
905
|
-
if (o.file && o.key) {
|
|
1395
|
+
};
|
|
1396
|
+
let retain = true;
|
|
1397
|
+
try {
|
|
906
1398
|
try {
|
|
907
|
-
|
|
1399
|
+
try {
|
|
1400
|
+
binding = progressBinding(t, o);
|
|
1401
|
+
}
|
|
1402
|
+
catch {
|
|
1403
|
+
commentDiagnostic(o, "[bkit-tracker] progress association unavailable; creating standalone comment", warnings);
|
|
1404
|
+
}
|
|
1405
|
+
if (o.file && o.key && binding !== "conflict") {
|
|
1406
|
+
try {
|
|
1407
|
+
const prior = alreadyDone(o.file, o.key, hash);
|
|
1408
|
+
if (prior)
|
|
1409
|
+
return { done: true, action, url: prior.ref, deduped: true };
|
|
1410
|
+
}
|
|
1411
|
+
catch {
|
|
1412
|
+
commentDiagnostic(o, "[bkit-tracker] comment receipt unavailable; creating standalone comment", warnings);
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
if (o.purpose === "attention" || o.purpose === "terminal")
|
|
1416
|
+
seal();
|
|
1417
|
+
if (binding &&
|
|
1418
|
+
binding !== "conflict" &&
|
|
1419
|
+
o.key &&
|
|
1420
|
+
markerHashOf(body) &&
|
|
1421
|
+
o.purpose !== "attention" &&
|
|
1422
|
+
o.purpose !== "terminal") {
|
|
1423
|
+
try {
|
|
1424
|
+
cached = readProgress(binding);
|
|
1425
|
+
if (cached === null && fs.existsSync(binding.file))
|
|
1426
|
+
commentDiagnostic(o, "[bkit-tracker] progress source unavailable; creating standalone comment", warnings);
|
|
1427
|
+
if (cached)
|
|
1428
|
+
o.knownUrl = cached.comment.url;
|
|
1429
|
+
const rendered = progressSection(body, binding, o.key);
|
|
1430
|
+
section = rendered.section;
|
|
1431
|
+
if (routine) {
|
|
1432
|
+
lock = await acquirePublicationLock(t.cwd, {
|
|
1433
|
+
target: binding.target,
|
|
1434
|
+
invocation: binding.invocation,
|
|
1435
|
+
timeoutMs: Math.min(30_000, budget.duration / 2),
|
|
1436
|
+
});
|
|
1437
|
+
if (!lock) {
|
|
1438
|
+
cached = null;
|
|
1439
|
+
commentDiagnostic(o, "[bkit-tracker] aggregation unavailable: publication lock", warnings);
|
|
1440
|
+
}
|
|
1441
|
+
else {
|
|
1442
|
+
cached = readProgress(binding);
|
|
1443
|
+
if (cached && budget.duration < 1000) {
|
|
1444
|
+
// With a short caller allowance, reserve the transport for standalone delivery.
|
|
1445
|
+
outgoing = body;
|
|
1446
|
+
}
|
|
1447
|
+
else if (cached) {
|
|
1448
|
+
const newest = (await recentComments(t, 1, budget)).at(-1);
|
|
1449
|
+
const actual = newest?.evidence;
|
|
1450
|
+
const saved = cached.comment.evidence;
|
|
1451
|
+
if (actual?.authorId &&
|
|
1452
|
+
actual.id === saved.id &&
|
|
1453
|
+
actual.authorId === saved.authorId &&
|
|
1454
|
+
actual.fingerprint === saved.fingerprint &&
|
|
1455
|
+
actual.updatedAt === saved.updatedAt &&
|
|
1456
|
+
actual.createdAt === saved.createdAt &&
|
|
1457
|
+
actual.representation === saved.representation) {
|
|
1458
|
+
const known = cached.sections.find((s) => s.id === rendered.section.id);
|
|
1459
|
+
if (known && actual.sectionIds.includes(known.id))
|
|
1460
|
+
return {
|
|
1461
|
+
done: true,
|
|
1462
|
+
action,
|
|
1463
|
+
url: newest?.url ?? cached.comment.url,
|
|
1464
|
+
deduped: true,
|
|
1465
|
+
};
|
|
1466
|
+
outgoing = `${cached.source}\n\n${rendered.source.split("\n").slice(2).join("\n")}`;
|
|
1467
|
+
const limit = t.tracker === "linear"
|
|
1468
|
+
? COMMENT_BODY_BUDGETS.linear
|
|
1469
|
+
: t.tracker === "jira"
|
|
1470
|
+
? COMMENT_BODY_BUDGETS.jira
|
|
1471
|
+
: COMMENT_BODY_BUDGETS.github;
|
|
1472
|
+
if (outgoing.length <= limit)
|
|
1473
|
+
aggregate = true;
|
|
1474
|
+
else
|
|
1475
|
+
outgoing = body;
|
|
1476
|
+
}
|
|
1477
|
+
else
|
|
1478
|
+
outgoing = body;
|
|
1479
|
+
}
|
|
1480
|
+
else if (!binding.priorDelivery)
|
|
1481
|
+
outgoing = rendered.source;
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
else if (cached || !binding.priorDelivery)
|
|
1485
|
+
outgoing = rendered.source;
|
|
1486
|
+
}
|
|
1487
|
+
catch {
|
|
1488
|
+
outgoing = body;
|
|
1489
|
+
aggregate = false;
|
|
1490
|
+
commentDiagnostic(o, "[bkit-tracker] progress source unavailable; creating standalone comment", warnings);
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
if (routine && (!binding || binding === "conflict"))
|
|
1494
|
+
commentDiagnostic(o, "[bkit-tracker] aggregation unavailable: progress association unavailable", warnings);
|
|
1495
|
+
if (o.recent) {
|
|
1496
|
+
try {
|
|
1497
|
+
const same = samePosted(await recentComments(t, o.recent, budget), body);
|
|
1498
|
+
if (same)
|
|
1499
|
+
return { done: true, action, url: same.url, deduped: true };
|
|
1500
|
+
}
|
|
1501
|
+
catch {
|
|
1502
|
+
commentDiagnostic(o, "[bkit-tracker] comment lookup unavailable; creating standalone comment", warnings);
|
|
1503
|
+
outgoing = body;
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
if (aggregate && cached) {
|
|
1507
|
+
try {
|
|
1508
|
+
posted = await postComment(t, outgoing, {
|
|
1509
|
+
commentId: cached.comment.evidence.id,
|
|
1510
|
+
evidence: cached.comment.evidence,
|
|
1511
|
+
budget,
|
|
1512
|
+
});
|
|
1513
|
+
}
|
|
1514
|
+
catch {
|
|
1515
|
+
try {
|
|
1516
|
+
const latest = (await recentComments(t, 1, budget)).at(-1);
|
|
1517
|
+
if (latest?.evidence?.id === cached.comment.evidence.id &&
|
|
1518
|
+
latest.evidence.authorId === cached.comment.evidence.authorId &&
|
|
1519
|
+
section &&
|
|
1520
|
+
latest.evidence.sectionIds.includes(section.id)) {
|
|
1521
|
+
posted = { url: latest.url, parts: 1, evidence: latest.evidence };
|
|
1522
|
+
// A section proves delivery, not authority to overwrite a changed revision.
|
|
1523
|
+
retain = latest.evidence.fingerprint === actionHash(outgoing);
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
catch {
|
|
1527
|
+
commentDiagnostic(o, "[bkit-tracker] append recognition unavailable", warnings);
|
|
1528
|
+
}
|
|
1529
|
+
if (!posted) {
|
|
1530
|
+
outgoing = body;
|
|
1531
|
+
aggregate = false;
|
|
1532
|
+
seal();
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
if (!posted) {
|
|
1537
|
+
budget.reserve = false;
|
|
1538
|
+
posted = await postComment(t, outgoing, { budget });
|
|
1539
|
+
}
|
|
908
1540
|
}
|
|
909
1541
|
catch (err) {
|
|
910
|
-
const
|
|
911
|
-
o
|
|
912
|
-
|
|
1542
|
+
const reason = classifyFailure(err);
|
|
1543
|
+
commentDiagnostic(o, `tracker ${action} failed (${reason}); delivery unavailable`, warnings);
|
|
1544
|
+
seal();
|
|
1545
|
+
return { done: false, action, reason };
|
|
1546
|
+
}
|
|
1547
|
+
if (aggregate && posted.evidence) {
|
|
1548
|
+
try {
|
|
1549
|
+
const latest = (await recentComments(t, 1, budget)).at(-1)?.evidence;
|
|
1550
|
+
retain =
|
|
1551
|
+
retain &&
|
|
1552
|
+
latest?.id === posted.evidence.id &&
|
|
1553
|
+
latest.fingerprint === posted.evidence.fingerprint &&
|
|
1554
|
+
latest.authorId === posted.evidence.authorId &&
|
|
1555
|
+
latest.updatedAt === posted.evidence.updatedAt;
|
|
1556
|
+
}
|
|
1557
|
+
catch {
|
|
1558
|
+
retain = false;
|
|
1559
|
+
commentDiagnostic(o, "[bkit-tracker] postflight reconciliation unavailable", warnings);
|
|
1560
|
+
}
|
|
1561
|
+
if (!retain)
|
|
1562
|
+
seal();
|
|
1563
|
+
}
|
|
1564
|
+
if (retain && binding && binding !== "conflict" && section) {
|
|
1565
|
+
try {
|
|
1566
|
+
saveProgress(binding, outgoing, section, posted, aggregate ? cached?.sections : []);
|
|
1567
|
+
}
|
|
1568
|
+
catch {
|
|
1569
|
+
const warning = "[bkit-tracker] posted, but progress source could not be saved";
|
|
1570
|
+
commentDiagnostic(o, warning, warnings);
|
|
1571
|
+
warnings.push(warning);
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
if (o.file && o.key && binding !== "conflict") {
|
|
1575
|
+
try {
|
|
1576
|
+
recordAction(o.file, { action, key: o.key, hash, ref: posted.url });
|
|
1577
|
+
}
|
|
1578
|
+
catch (err) {
|
|
1579
|
+
const warning = redact(`posted, but the engine-action row was not written to ${o.file}: ${err.message ?? String(err)}`);
|
|
1580
|
+
commentDiagnostic(o, warning, warnings);
|
|
1581
|
+
warnings.push(warning);
|
|
1582
|
+
}
|
|
913
1583
|
}
|
|
1584
|
+
return {
|
|
1585
|
+
done: true,
|
|
1586
|
+
action,
|
|
1587
|
+
url: posted.url,
|
|
1588
|
+
...(warnings.length ? { warning: warnings.join("; ") } : {}),
|
|
1589
|
+
};
|
|
1590
|
+
}
|
|
1591
|
+
finally {
|
|
1592
|
+
if (lock && !(await releasePublicationLock(lock)))
|
|
1593
|
+
commentDiagnostic(o, "[bkit-tracker] publication lock release unavailable", warnings);
|
|
914
1594
|
}
|
|
915
|
-
return { done: true, action, url: posted.url };
|
|
916
1595
|
}
|
|
917
1596
|
/** Run one tracker write as `action`: `done: true`, or `done: false` with the reason (logged, redacted). */
|
|
918
1597
|
export async function attempt(action, log, fn) {
|
|
@@ -938,11 +1617,117 @@ export const POST_FLAG = {
|
|
|
938
1617
|
/** The `tracker` output key's meaning, shared by every `--post` verb. */
|
|
939
1618
|
export const TRACKER_OUTPUT = "with --post: {done: true, action, url, deduped?, warning?} | {done: false, action, reason} (post `commentFile` verbatim via the tracker MCP) | {skipped: no-issue | tracker-none}; absent without --post";
|
|
940
1619
|
/** The `commentFile` output key's meaning, shared by every verb that prints a comment. */
|
|
941
|
-
export const COMMENT_FILE_OUTPUT = "absolute path of a file holding `comment` byte-for-byte (the MCP fallback reads it); null when comment is null";
|
|
1620
|
+
export const COMMENT_FILE_OUTPUT = "absolute path of a file holding `comment` byte-for-byte (the MCP fallback reads it); null when comment is null or its source file is unavailable";
|
|
942
1621
|
/** How many of the issue's latest comments a re-run checks for the same marker hash. */
|
|
943
1622
|
export const RECENT_COMMENTS = 5;
|
|
944
1623
|
/** `comment` written to its body file, or null for no comment. */
|
|
945
|
-
export
|
|
1624
|
+
export function commentFileOf(cwd, comment, ctx) {
|
|
1625
|
+
if (comment === null)
|
|
1626
|
+
return null;
|
|
1627
|
+
try {
|
|
1628
|
+
return writeBodyFile(cwd, "comment", comment);
|
|
1629
|
+
}
|
|
1630
|
+
catch {
|
|
1631
|
+
commentDiagnostic({
|
|
1632
|
+
cwd,
|
|
1633
|
+
workflow: ctx?.publication?.workflow ?? str(ctx?.flags.workflow),
|
|
1634
|
+
log: (line) => ctx?.log(line),
|
|
1635
|
+
}, "[bkit-tracker] fallback source unavailable; continuing workflow", []);
|
|
1636
|
+
return null;
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
/** This repository's private outbox `sessions/` folder; null outside a Git repository. */
|
|
1640
|
+
export function sessionsRoot(cwd) {
|
|
1641
|
+
const common = git(cwd, ["rev-parse", "--path-format=absolute", "--git-common-dir"]);
|
|
1642
|
+
return common ? path.join(common, "bkit-outbox", "sessions") : null;
|
|
1643
|
+
}
|
|
1644
|
+
/** The real path of `p`, resolving its nearest existing ancestor when `p` itself is gone. */
|
|
1645
|
+
function canonicalPath(p) {
|
|
1646
|
+
const abs = path.resolve(p);
|
|
1647
|
+
const tail = [];
|
|
1648
|
+
let head = abs;
|
|
1649
|
+
while (!fs.existsSync(head)) {
|
|
1650
|
+
const parent = path.dirname(head);
|
|
1651
|
+
if (parent === head)
|
|
1652
|
+
return abs;
|
|
1653
|
+
tail.unshift(path.basename(head));
|
|
1654
|
+
head = parent;
|
|
1655
|
+
}
|
|
1656
|
+
return path.join(fs.realpathSync(head), ...tail);
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* Proof that a workflow or audit run is making a `bkit issues comment` call: a session
|
|
1660
|
+
* folder directly under the outbox `sessions/` folder (it may be deleted by now), a
|
|
1661
|
+
* status file holding a run, or an audit body file in its own audit outbox folder.
|
|
1662
|
+
* Null with no proof or on any lookup error.
|
|
1663
|
+
*/
|
|
1664
|
+
export function commentRunProof(ctx) {
|
|
1665
|
+
try {
|
|
1666
|
+
const session = str(ctx.flags.sessionDir);
|
|
1667
|
+
if (session !== undefined) {
|
|
1668
|
+
const root = sessionsRoot(ctx.cwd);
|
|
1669
|
+
if (root &&
|
|
1670
|
+
path.dirname(canonicalPath(path.resolve(ctx.cwd, session))) === canonicalPath(root))
|
|
1671
|
+
return "session";
|
|
1672
|
+
}
|
|
1673
|
+
const file = str(ctx.flags.file);
|
|
1674
|
+
if (file !== undefined) {
|
|
1675
|
+
const abs = path.resolve(ctx.cwd, file);
|
|
1676
|
+
if (fs.statSync(abs).isFile() &&
|
|
1677
|
+
endedRunActionKey(abs, str(ctx.flags.workflow) || undefined, "proof") !== undefined)
|
|
1678
|
+
return "status";
|
|
1679
|
+
}
|
|
1680
|
+
const slug = str(ctx.flags.auditSlug);
|
|
1681
|
+
const body = str(ctx.flags.bodyFile);
|
|
1682
|
+
if (str(ctx.flags.kind) === "audit" && slug !== undefined && body !== undefined) {
|
|
1683
|
+
const root = sessionsRoot(ctx.cwd);
|
|
1684
|
+
if (root &&
|
|
1685
|
+
path.dirname(canonicalPath(path.resolve(ctx.cwd, body))) ===
|
|
1686
|
+
path.join(canonicalPath(path.dirname(root)), `audit-${slug}`))
|
|
1687
|
+
return "audit";
|
|
1688
|
+
}
|
|
1689
|
+
return null;
|
|
1690
|
+
}
|
|
1691
|
+
catch {
|
|
1692
|
+
return null;
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
/** Optional session ownership, validated against this repository's private outbox. */
|
|
1696
|
+
export function sessionPublication(cwd, session, workflow) {
|
|
1697
|
+
if (!session || !workflow)
|
|
1698
|
+
return undefined;
|
|
1699
|
+
const sessions = sessionsRoot(cwd);
|
|
1700
|
+
if (!sessions)
|
|
1701
|
+
throw new Error("session association repository unavailable");
|
|
1702
|
+
const dir = fs.realpathSync(path.resolve(cwd, session));
|
|
1703
|
+
if (path.dirname(dir) !== fs.realpathSync(sessions) || !fs.statSync(dir).isDirectory())
|
|
1704
|
+
throw new Error("session association directory unavailable");
|
|
1705
|
+
return { invocation: actionHash(`${dir}\0${workflow}`), workflow };
|
|
1706
|
+
}
|
|
1707
|
+
function runPublicationFile(common, file, workflow, at) {
|
|
1708
|
+
return path.join(common, "bkit-outbox", "invocations", `${actionHash([path.resolve(file), workflow, at].join("\0"))}.json`);
|
|
1709
|
+
}
|
|
1710
|
+
/** Bind only the exact run opened by workflows start; loss of this cache is harmless. */
|
|
1711
|
+
export function bindRunPublication(ctx, file, workflow, run) {
|
|
1712
|
+
try {
|
|
1713
|
+
const publication = sessionPublication(ctx.cwd, str(ctx.flags.sessionDir), workflow);
|
|
1714
|
+
const current = readRunsLenient(file).filter((r) => r.workflow === workflow)[run - 1];
|
|
1715
|
+
const common = git(ctx.cwd, ["rev-parse", "--path-format=absolute", "--git-common-dir"]);
|
|
1716
|
+
if (!publication || !current || !common)
|
|
1717
|
+
return;
|
|
1718
|
+
const destination = runPublicationFile(common, file, workflow, current.startedAt);
|
|
1719
|
+
if (fs.existsSync(destination))
|
|
1720
|
+
return;
|
|
1721
|
+
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
|
1722
|
+
fs.writeFileSync(destination, JSON.stringify({ version: 1, ...publication }), {
|
|
1723
|
+
mode: 0o600,
|
|
1724
|
+
flag: "wx",
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
catch {
|
|
1728
|
+
commentDiagnostic({ cwd: ctx.cwd, workflow, log: (line) => ctx.log(line) }, "[bkit-tracker] invocation association unavailable; progress may start fresh", []);
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
946
1731
|
/**
|
|
947
1732
|
* The engine-action key of a comment in `file`'s current run of `workflow` (the
|
|
948
1733
|
* latest run's workflow when omitted); undefined with no status.jsonl or no run.
|
|
@@ -972,12 +1757,35 @@ function commentKey(file, workflow, numberOf, parts) {
|
|
|
972
1757
|
* Post `body` for a verb's `--post`: once per key in `file` when both are given,
|
|
973
1758
|
* else once per marker hash among the issue's latest comments.
|
|
974
1759
|
*/
|
|
975
|
-
export function postForVerb(ctx, body, o) {
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
1760
|
+
export async function postForVerb(ctx, body, o) {
|
|
1761
|
+
try {
|
|
1762
|
+
const t = trackerTarget(ctx.cwd, { fid: o.fid, issue: o.issue });
|
|
1763
|
+
let publication = ctx.publication;
|
|
1764
|
+
if (!o.file && !publication) {
|
|
1765
|
+
try {
|
|
1766
|
+
publication = sessionPublication(ctx.cwd, str(ctx.flags.sessionDir), str(ctx.flags.workflow));
|
|
1767
|
+
}
|
|
1768
|
+
catch {
|
|
1769
|
+
commentDiagnostic({ cwd: ctx.cwd, workflow: str(ctx.flags.workflow), log: (line) => ctx.log(line) }, "[bkit-tracker] invocation association unavailable; progress may start fresh", []);
|
|
1770
|
+
publication = undefined;
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
const key = o.key ??
|
|
1774
|
+
(publication
|
|
1775
|
+
? actionKey(publication.workflow, 1, "step", str(ctx.flags.stepNumber), str(ctx.flags.subStep), str(ctx.flags.attempt), "comment")
|
|
1776
|
+
: undefined);
|
|
1777
|
+
return await postCommentOnce(t, body, {
|
|
1778
|
+
file: o.file,
|
|
1779
|
+
key,
|
|
1780
|
+
publication,
|
|
1781
|
+
workflow: str(ctx.flags.workflow),
|
|
1782
|
+
purpose: o.purpose ?? (str(ctx.flags.workflow) ? "routine" : undefined),
|
|
1783
|
+
recent: key ? undefined : RECENT_COMMENTS,
|
|
1784
|
+
log: (line) => ctx.log(line),
|
|
1785
|
+
});
|
|
1786
|
+
}
|
|
1787
|
+
catch {
|
|
1788
|
+
commentDiagnostic({ cwd: ctx.cwd, workflow: str(ctx.flags.workflow), log: (line) => ctx.log(line) }, "[bkit-tracker] comment preparation unavailable; continuing workflow", []);
|
|
1789
|
+
return { done: false, action: "comment", reason: "preparation" };
|
|
1790
|
+
}
|
|
983
1791
|
}
|