gsd-pi 2.74.0-dev.2b524c3 → 2.74.0-dev.6e23363
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +85 -0
- package/dist/headless-query.js +4 -1
- package/dist/help-text.js +23 -0
- package/dist/resources/extensions/gsd/activity-log.js +16 -0
- package/dist/resources/extensions/gsd/auto/detect-stuck.js +11 -4
- package/dist/resources/extensions/gsd/auto/loop.js +147 -10
- package/dist/resources/extensions/gsd/auto/phases.js +158 -4
- package/dist/resources/extensions/gsd/auto/session.js +10 -0
- package/dist/resources/extensions/gsd/auto-dispatch.js +11 -1
- package/dist/resources/extensions/gsd/auto-model-selection.js +51 -5
- package/dist/resources/extensions/gsd/auto-post-unit.js +213 -14
- package/dist/resources/extensions/gsd/auto-prompts.js +12 -0
- package/dist/resources/extensions/gsd/auto-unit-closeout.js +18 -0
- package/dist/resources/extensions/gsd/auto-verification.js +100 -2
- package/dist/resources/extensions/gsd/auto.js +36 -4
- package/dist/resources/extensions/gsd/bootstrap/register-extension.js +21 -8
- package/dist/resources/extensions/gsd/commands/catalog.js +26 -1
- package/dist/resources/extensions/gsd/commands/handlers/ops.js +20 -0
- package/dist/resources/extensions/gsd/commands/handlers/workflow.js +68 -9
- package/dist/resources/extensions/gsd/commands-add-tests.js +111 -0
- package/dist/resources/extensions/gsd/commands-backlog.js +140 -0
- package/dist/resources/extensions/gsd/commands-do.js +79 -0
- package/dist/resources/extensions/gsd/commands-maintenance.js +6 -6
- package/dist/resources/extensions/gsd/commands-pr-branch.js +180 -0
- package/dist/resources/extensions/gsd/commands-prefs-wizard.js +1 -1
- package/dist/resources/extensions/gsd/commands-session-report.js +82 -0
- package/dist/resources/extensions/gsd/commands-ship.js +187 -0
- package/dist/resources/extensions/gsd/db-writer.js +3 -5
- package/dist/resources/extensions/gsd/docs/preferences-reference.md +14 -1
- package/dist/resources/extensions/gsd/git-service.js +49 -1
- package/dist/resources/extensions/gsd/graph-context.js +157 -0
- package/dist/resources/extensions/gsd/gsd-db.js +581 -2
- package/dist/resources/extensions/gsd/guided-flow.js +23 -0
- package/dist/resources/extensions/gsd/index.js +15 -2
- package/dist/resources/extensions/gsd/init-wizard.js +1 -0
- package/dist/resources/extensions/gsd/journal.js +27 -0
- package/dist/resources/extensions/gsd/md-importer.js +3 -4
- package/dist/resources/extensions/gsd/memory-store.js +19 -51
- package/dist/resources/extensions/gsd/metrics.js +19 -0
- package/dist/resources/extensions/gsd/milestone-validation-gates.js +13 -12
- package/dist/resources/extensions/gsd/native-git-bridge.js +7 -4
- package/dist/resources/extensions/gsd/parallel-orchestrator.js +33 -1
- package/dist/resources/extensions/gsd/preferences-models.js +20 -3
- package/dist/resources/extensions/gsd/preferences-types.js +1 -0
- package/dist/resources/extensions/gsd/preferences-validation.js +108 -2
- package/dist/resources/extensions/gsd/preferences.js +26 -0
- package/dist/resources/extensions/gsd/prompts/add-tests.md +35 -0
- package/dist/resources/extensions/gsd/slice-parallel-orchestrator.js +12 -2
- package/dist/resources/extensions/gsd/state.js +5 -1
- package/dist/resources/extensions/gsd/templates/PREFERENCES.md +18 -0
- package/dist/resources/extensions/gsd/tools/complete-slice.js +20 -0
- package/dist/resources/extensions/gsd/tools/validate-milestone.js +39 -4
- package/dist/resources/extensions/gsd/tools/workflow-tool-executors.js +3 -14
- package/dist/resources/extensions/gsd/triage-resolution.js +2 -5
- package/dist/resources/extensions/gsd/unit-ownership.js +1 -1
- package/dist/resources/extensions/gsd/uok/audit-toggle.js +7 -0
- package/dist/resources/extensions/gsd/uok/audit.js +40 -0
- package/dist/resources/extensions/gsd/uok/contracts.js +1 -0
- package/dist/resources/extensions/gsd/uok/execution-graph.js +179 -0
- package/dist/resources/extensions/gsd/uok/flags.js +29 -0
- package/dist/resources/extensions/gsd/uok/gate-runner.js +109 -0
- package/dist/resources/extensions/gsd/uok/gitops.js +53 -0
- package/dist/resources/extensions/gsd/uok/kernel.js +80 -0
- package/dist/resources/extensions/gsd/uok/loop-adapter.js +133 -0
- package/dist/resources/extensions/gsd/uok/model-policy.js +66 -0
- package/dist/resources/extensions/gsd/uok/plan-v2.js +132 -0
- package/dist/resources/extensions/gsd/workflow-logger.js +22 -0
- package/dist/resources/extensions/gsd/workflow-manifest.js +8 -69
- package/dist/resources/extensions/gsd/workflow-migration.js +21 -22
- package/dist/resources/extensions/gsd/workflow-projections.js +4 -1
- package/dist/resources/extensions/gsd/workflow-reconcile.js +14 -11
- package/dist/resources/extensions/ttsr/ttsr-manager.js +3 -1
- package/dist/tsconfig.extensions.tsbuildinfo +1 -0
- package/dist/web/standalone/.next/BUILD_ID +1 -1
- package/dist/web/standalone/.next/app-path-routes-manifest.json +9 -9
- package/dist/web/standalone/.next/build-manifest.json +2 -2
- package/dist/web/standalone/.next/prerender-manifest.json +3 -3
- package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.html +1 -1
- package/dist/web/standalone/.next/server/app/index.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app-paths-manifest.json +9 -9
- package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
- package/dist/web/standalone/.next/server/middleware-manifest.json +5 -5
- package/dist/web/standalone/.next/server/pages/404.html +1 -1
- package/dist/web/standalone/.next/server/pages/500.html +1 -1
- package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
- package/package.json +3 -2
- package/packages/daemon/package.json +2 -2
- package/packages/mcp-server/dist/index.d.ts +3 -0
- package/packages/mcp-server/dist/index.d.ts.map +1 -1
- package/packages/mcp-server/dist/index.js +3 -0
- package/packages/mcp-server/dist/index.js.map +1 -1
- package/packages/mcp-server/dist/readers/graph.d.ts +87 -0
- package/packages/mcp-server/dist/readers/graph.d.ts.map +1 -0
- package/packages/mcp-server/dist/readers/graph.js +548 -0
- package/packages/mcp-server/dist/readers/graph.js.map +1 -0
- package/packages/mcp-server/dist/readers/index.d.ts +2 -0
- package/packages/mcp-server/dist/readers/index.d.ts.map +1 -1
- package/packages/mcp-server/dist/readers/index.js +1 -0
- package/packages/mcp-server/dist/readers/index.js.map +1 -1
- package/packages/mcp-server/dist/server.d.ts.map +1 -1
- package/packages/mcp-server/dist/server.js +65 -0
- package/packages/mcp-server/dist/server.js.map +1 -1
- package/packages/mcp-server/package.json +2 -2
- package/packages/mcp-server/src/index.ts +15 -0
- package/packages/mcp-server/src/readers/graph.test.ts +426 -0
- package/packages/mcp-server/src/readers/graph.ts +708 -0
- package/packages/mcp-server/src/readers/index.ts +12 -0
- package/packages/mcp-server/src/server.ts +83 -0
- package/packages/mcp-server/tsconfig.json +1 -0
- package/packages/mcp-server/tsconfig.tsbuildinfo +1 -0
- package/packages/native/package.json +2 -2
- package/packages/native/tsconfig.tsbuildinfo +1 -0
- package/packages/pi-agent-core/package.json +1 -1
- package/packages/pi-agent-core/tsconfig.json +1 -0
- package/packages/pi-agent-core/tsconfig.tsbuildinfo +1 -0
- package/packages/pi-ai/package.json +1 -1
- package/packages/pi-ai/tsconfig.json +1 -0
- package/packages/pi-ai/tsconfig.tsbuildinfo +1 -0
- package/packages/pi-coding-agent/dist/core/chat-controller-ordering.test.js +120 -0
- package/packages/pi-coding-agent/dist/core/chat-controller-ordering.test.js.map +1 -1
- package/packages/pi-coding-agent/dist/core/model-registry-env-fallback.test.d.ts +2 -0
- package/packages/pi-coding-agent/dist/core/model-registry-env-fallback.test.d.ts.map +1 -0
- package/packages/pi-coding-agent/dist/core/model-registry-env-fallback.test.js +52 -0
- package/packages/pi-coding-agent/dist/core/model-registry-env-fallback.test.js.map +1 -0
- package/packages/pi-coding-agent/dist/core/model-registry.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/model-registry.js +2 -2
- package/packages/pi-coding-agent/dist/core/model-registry.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.js +48 -4
- package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.js.map +1 -1
- package/packages/pi-coding-agent/src/core/chat-controller-ordering.test.ts +166 -0
- package/packages/pi-coding-agent/src/core/model-registry-env-fallback.test.ts +59 -0
- package/packages/pi-coding-agent/src/core/model-registry.ts +2 -1
- package/packages/pi-coding-agent/src/modes/interactive/controllers/chat-controller.ts +53 -4
- package/packages/pi-coding-agent/src/types/ambient-modules.d.ts +69 -0
- package/packages/pi-coding-agent/tsconfig.json +3 -2
- package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -0
- package/packages/pi-tui/package.json +1 -1
- package/packages/pi-tui/tsconfig.json +1 -0
- package/packages/pi-tui/tsconfig.tsbuildinfo +1 -0
- package/packages/rpc-client/package.json +1 -1
- package/packages/rpc-client/tsconfig.json +1 -0
- package/packages/rpc-client/tsconfig.tsbuildinfo +1 -0
- package/src/resources/extensions/gsd/activity-log.ts +21 -0
- package/src/resources/extensions/gsd/auto/detect-stuck.ts +12 -4
- package/src/resources/extensions/gsd/auto/loop-deps.ts +10 -0
- package/src/resources/extensions/gsd/auto/loop.ts +159 -10
- package/src/resources/extensions/gsd/auto/phases.ts +191 -4
- package/src/resources/extensions/gsd/auto/session.ts +10 -0
- package/src/resources/extensions/gsd/auto-dispatch.ts +16 -6
- package/src/resources/extensions/gsd/auto-model-selection.ts +66 -5
- package/src/resources/extensions/gsd/auto-post-unit.ts +231 -15
- package/src/resources/extensions/gsd/auto-prompts.ts +13 -0
- package/src/resources/extensions/gsd/auto-unit-closeout.ts +25 -1
- package/src/resources/extensions/gsd/auto-verification.ts +129 -2
- package/src/resources/extensions/gsd/auto.ts +41 -2
- package/src/resources/extensions/gsd/bootstrap/register-extension.ts +24 -8
- package/src/resources/extensions/gsd/commands/catalog.ts +26 -1
- package/src/resources/extensions/gsd/commands/handlers/ops.ts +20 -0
- package/src/resources/extensions/gsd/commands/handlers/workflow.ts +74 -9
- package/src/resources/extensions/gsd/commands-add-tests.ts +137 -0
- package/src/resources/extensions/gsd/commands-backlog.ts +182 -0
- package/src/resources/extensions/gsd/commands-do.ts +109 -0
- package/src/resources/extensions/gsd/commands-maintenance.ts +6 -6
- package/src/resources/extensions/gsd/commands-pr-branch.ts +234 -0
- package/src/resources/extensions/gsd/commands-prefs-wizard.ts +1 -1
- package/src/resources/extensions/gsd/commands-session-report.ts +101 -0
- package/src/resources/extensions/gsd/commands-ship.ts +219 -0
- package/src/resources/extensions/gsd/db-writer.ts +3 -5
- package/src/resources/extensions/gsd/docs/preferences-reference.md +14 -1
- package/src/resources/extensions/gsd/git-service.ts +68 -0
- package/src/resources/extensions/gsd/graph-context.ts +212 -0
- package/src/resources/extensions/gsd/gsd-db.ts +788 -3
- package/src/resources/extensions/gsd/guided-flow.ts +32 -0
- package/src/resources/extensions/gsd/index.ts +18 -2
- package/src/resources/extensions/gsd/init-wizard.ts +3 -2
- package/src/resources/extensions/gsd/journal.ts +30 -0
- package/src/resources/extensions/gsd/md-importer.ts +3 -5
- package/src/resources/extensions/gsd/memory-store.ts +31 -62
- package/src/resources/extensions/gsd/metrics.ts +26 -0
- package/src/resources/extensions/gsd/milestone-validation-gates.ts +13 -14
- package/src/resources/extensions/gsd/native-git-bridge.ts +11 -12
- package/src/resources/extensions/gsd/parallel-orchestrator.ts +40 -1
- package/src/resources/extensions/gsd/preferences-models.ts +20 -3
- package/src/resources/extensions/gsd/preferences-types.ts +32 -0
- package/src/resources/extensions/gsd/preferences-validation.ts +107 -2
- package/src/resources/extensions/gsd/preferences.ts +28 -0
- package/src/resources/extensions/gsd/prompts/add-tests.md +35 -0
- package/src/resources/extensions/gsd/session-lock.ts +14 -2
- package/src/resources/extensions/gsd/slice-parallel-orchestrator.ts +20 -1
- package/src/resources/extensions/gsd/state.ts +9 -2
- package/src/resources/extensions/gsd/templates/PREFERENCES.md +18 -0
- package/src/resources/extensions/gsd/tests/auto-loop.test.ts +7 -3
- package/src/resources/extensions/gsd/tests/auto-model-selection.test.ts +20 -0
- package/src/resources/extensions/gsd/tests/auto-project-root-env.test.ts +7 -3
- package/src/resources/extensions/gsd/tests/cold-resume-db-reopen.test.ts +6 -2
- package/src/resources/extensions/gsd/tests/commands-backlog.test.ts +158 -0
- package/src/resources/extensions/gsd/tests/commands-do.test.ts +127 -0
- package/src/resources/extensions/gsd/tests/commands-pr-branch.test.ts +68 -0
- package/src/resources/extensions/gsd/tests/commands-session-report.test.ts +82 -0
- package/src/resources/extensions/gsd/tests/commands-ship.test.ts +71 -0
- package/src/resources/extensions/gsd/tests/commands-workflow-custom.test.ts +14 -0
- package/src/resources/extensions/gsd/tests/complete-slice.test.ts +2 -2
- package/src/resources/extensions/gsd/tests/complete-task.test.ts +2 -2
- package/src/resources/extensions/gsd/tests/extension-bootstrap-isolation.test.ts +154 -0
- package/src/resources/extensions/gsd/tests/finalize-timeout-guard.test.ts +10 -7
- package/src/resources/extensions/gsd/tests/graph-context.test.ts +337 -0
- package/src/resources/extensions/gsd/tests/gsd-db.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/journal-integration.test.ts +68 -1
- package/src/resources/extensions/gsd/tests/md-importer.test.ts +1 -2
- package/src/resources/extensions/gsd/tests/memory-store.test.ts +2 -3
- package/src/resources/extensions/gsd/tests/native-git-bridge-exec-fallback.test.ts +140 -0
- package/src/resources/extensions/gsd/tests/post-exec-retry-bypass.test.ts +79 -1
- package/src/resources/extensions/gsd/tests/post-unit-state-rebuild.test.ts +2 -1
- package/src/resources/extensions/gsd/tests/pre-execution-pause-wiring.test.ts +40 -1
- package/src/resources/extensions/gsd/tests/single-writer-invariant.test.ts +180 -0
- package/src/resources/extensions/gsd/tests/token-profile.test.ts +8 -5
- package/src/resources/extensions/gsd/tests/uok-audit-unified.test.ts +101 -0
- package/src/resources/extensions/gsd/tests/uok-contracts.test.ts +85 -0
- package/src/resources/extensions/gsd/tests/uok-execution-graph.test.ts +69 -0
- package/src/resources/extensions/gsd/tests/uok-flags.test.ts +39 -0
- package/src/resources/extensions/gsd/tests/uok-gate-runner.test.ts +70 -0
- package/src/resources/extensions/gsd/tests/uok-gitops-turn-action.test.ts +85 -0
- package/src/resources/extensions/gsd/tests/uok-gitops-wiring.test.ts +35 -0
- package/src/resources/extensions/gsd/tests/uok-model-policy.test.ts +89 -0
- package/src/resources/extensions/gsd/tests/uok-plan-v2-wiring.test.ts +167 -0
- package/src/resources/extensions/gsd/tests/uok-preferences.test.ts +42 -0
- package/src/resources/extensions/gsd/tests/validate-milestone-write-order.test.ts +39 -0
- package/src/resources/extensions/gsd/tests/workflow-logger-wiring.test.ts +223 -0
- package/src/resources/extensions/gsd/tools/complete-slice.ts +26 -0
- package/src/resources/extensions/gsd/tools/validate-milestone.ts +48 -3
- package/src/resources/extensions/gsd/tools/workflow-tool-executors.ts +3 -11
- package/src/resources/extensions/gsd/triage-resolution.ts +2 -7
- package/src/resources/extensions/gsd/types.ts +1 -1
- package/src/resources/extensions/gsd/unit-ownership.ts +2 -2
- package/src/resources/extensions/gsd/uok/audit-toggle.ts +9 -0
- package/src/resources/extensions/gsd/uok/audit.ts +51 -0
- package/src/resources/extensions/gsd/uok/contracts.ts +135 -0
- package/src/resources/extensions/gsd/uok/execution-graph.ts +241 -0
- package/src/resources/extensions/gsd/uok/flags.ts +45 -0
- package/src/resources/extensions/gsd/uok/gate-runner.ts +146 -0
- package/src/resources/extensions/gsd/uok/gitops.ts +75 -0
- package/src/resources/extensions/gsd/uok/kernel.ts +105 -0
- package/src/resources/extensions/gsd/uok/loop-adapter.ts +162 -0
- package/src/resources/extensions/gsd/uok/model-policy.ts +112 -0
- package/src/resources/extensions/gsd/uok/plan-v2.ts +156 -0
- package/src/resources/extensions/gsd/workflow-logger.ts +25 -0
- package/src/resources/extensions/gsd/workflow-manifest.ts +9 -104
- package/src/resources/extensions/gsd/workflow-migration.ts +21 -29
- package/src/resources/extensions/gsd/workflow-projections.ts +8 -1
- package/src/resources/extensions/gsd/workflow-reconcile.ts +15 -15
- package/src/resources/extensions/ttsr/ttsr-manager.ts +10 -5
- /package/dist/web/standalone/.next/static/{YzIEI9sxJy4t5xgClF08g → bc2gRVFTgD7j--BsJE7vP}/_buildManifest.js +0 -0
- /package/dist/web/standalone/.next/static/{YzIEI9sxJy4t5xgClF08g → bc2gRVFTgD7j--BsJE7vP}/_ssgManifest.js +0 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GSD Command — /gsd ship
|
|
3
|
+
*
|
|
4
|
+
* Creates a PR from milestone artifacts: generates title + body from
|
|
5
|
+
* roadmap, slice summaries, and metrics, then opens via `gh pr create`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent";
|
|
9
|
+
|
|
10
|
+
import { execFileSync } from "node:child_process";
|
|
11
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
12
|
+
|
|
13
|
+
import { deriveState } from "./state.js";
|
|
14
|
+
import { resolveMilestoneFile, resolveSlicePath, resolveSliceFile } from "./paths.js";
|
|
15
|
+
import { getLedger, getProjectTotals, aggregateByModel, formatCost, formatTokenCount, loadLedgerFromDisk } from "./metrics.js";
|
|
16
|
+
import { nativeGetCurrentBranch, nativeDetectMainBranch } from "./native-git-bridge.js";
|
|
17
|
+
import { formatDuration } from "../shared/format-utils.js";
|
|
18
|
+
|
|
19
|
+
function git(basePath: string, args: readonly string[]): string {
|
|
20
|
+
return execFileSync("git", args, { cwd: basePath, encoding: "utf-8" }).trim();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isValidRefName(name: string): boolean {
|
|
24
|
+
try {
|
|
25
|
+
execFileSync("git", ["check-ref-format", "--branch", name], { stdio: "pipe" });
|
|
26
|
+
return true;
|
|
27
|
+
} catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface PRContent {
|
|
33
|
+
title: string;
|
|
34
|
+
body: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function listSliceIds(basePath: string, milestoneId: string): string[] {
|
|
38
|
+
// Slices live at <milestoneDir>/slices/<sliceId>/ with canonical S\d+ IDs.
|
|
39
|
+
// Use resolveSlicePath with a probe to find the real slices directory root.
|
|
40
|
+
const probe = resolveSlicePath(basePath, milestoneId, "S01");
|
|
41
|
+
let slicesDir: string | null = null;
|
|
42
|
+
if (probe) {
|
|
43
|
+
// probe looks like <milestoneDir>/slices/S01 — parent is slices dir.
|
|
44
|
+
slicesDir = probe.replace(/[\\/][^\\/]+$/, "");
|
|
45
|
+
} else {
|
|
46
|
+
// Fall back to scanning the milestones roadmap file's sibling slices dir.
|
|
47
|
+
const roadmap = resolveMilestoneFile(basePath, milestoneId, "ROADMAP");
|
|
48
|
+
if (roadmap) {
|
|
49
|
+
slicesDir = roadmap.replace(/[\\/][^\\/]+$/, "") + "/slices";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (!slicesDir || !existsSync(slicesDir)) return [];
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
return readdirSync(slicesDir, { withFileTypes: true })
|
|
56
|
+
.filter((e) => e.isDirectory() && /^S\d+$/.test(e.name))
|
|
57
|
+
.map((e) => e.name)
|
|
58
|
+
.sort();
|
|
59
|
+
} catch {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function collectSliceSummaries(basePath: string, milestoneId: string): string[] {
|
|
65
|
+
const summaries: string[] = [];
|
|
66
|
+
for (const sliceId of listSliceIds(basePath, milestoneId)) {
|
|
67
|
+
const summaryPath = resolveSliceFile(basePath, milestoneId, sliceId, "SUMMARY");
|
|
68
|
+
if (!summaryPath || !existsSync(summaryPath)) continue;
|
|
69
|
+
try {
|
|
70
|
+
const content = readFileSync(summaryPath, "utf-8").trim();
|
|
71
|
+
if (content) summaries.push(`### ${sliceId}\n${content}`);
|
|
72
|
+
} catch {
|
|
73
|
+
// non-fatal
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return summaries;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function generatePRContent(basePath: string, milestoneId: string, milestoneTitle: string): PRContent {
|
|
80
|
+
const title = `feat: ${milestoneTitle || milestoneId}`;
|
|
81
|
+
|
|
82
|
+
const sections: string[] = [];
|
|
83
|
+
|
|
84
|
+
// TL;DR
|
|
85
|
+
sections.push("## TL;DR\n");
|
|
86
|
+
sections.push(`**What:** Ship milestone ${milestoneId} — ${milestoneTitle || "(untitled)"}`);
|
|
87
|
+
sections.push(`**Why:** Milestone work complete, ready for review.`);
|
|
88
|
+
sections.push(`**How:** See slice summaries below.\n`);
|
|
89
|
+
|
|
90
|
+
// What — slice summaries
|
|
91
|
+
const summaries = collectSliceSummaries(basePath, milestoneId);
|
|
92
|
+
if (summaries.length > 0) {
|
|
93
|
+
sections.push("## What\n");
|
|
94
|
+
sections.push(summaries.join("\n\n"));
|
|
95
|
+
sections.push("");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Roadmap status
|
|
99
|
+
const roadmapPath = resolveMilestoneFile(basePath, milestoneId, "ROADMAP");
|
|
100
|
+
if (roadmapPath && existsSync(roadmapPath)) {
|
|
101
|
+
try {
|
|
102
|
+
const roadmap = readFileSync(roadmapPath, "utf-8");
|
|
103
|
+
const checkboxLines = roadmap.split("\n").filter((l) => /^\s*-\s*\[[ x]\]/.test(l));
|
|
104
|
+
if (checkboxLines.length > 0) {
|
|
105
|
+
sections.push("## Roadmap\n");
|
|
106
|
+
sections.push(checkboxLines.join("\n"));
|
|
107
|
+
sections.push("");
|
|
108
|
+
}
|
|
109
|
+
} catch {
|
|
110
|
+
// non-fatal
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Metrics
|
|
115
|
+
const ledger = getLedger();
|
|
116
|
+
const units = ledger?.units ?? loadLedgerFromDisk(basePath)?.units ?? [];
|
|
117
|
+
if (units.length > 0) {
|
|
118
|
+
const totals = getProjectTotals(units);
|
|
119
|
+
const byModel = aggregateByModel(units);
|
|
120
|
+
sections.push("## Metrics\n");
|
|
121
|
+
sections.push(`- **Units executed:** ${units.length}`);
|
|
122
|
+
sections.push(`- **Total cost:** ${formatCost(totals.cost)}`);
|
|
123
|
+
sections.push(`- **Tokens:** ${formatTokenCount(totals.tokens.input)} input / ${formatTokenCount(totals.tokens.output)} output`);
|
|
124
|
+
if (totals.duration > 0) {
|
|
125
|
+
sections.push(`- **Duration:** ${formatDuration(totals.duration)}`);
|
|
126
|
+
}
|
|
127
|
+
if (byModel.length > 0) {
|
|
128
|
+
sections.push(`- **Models:** ${byModel.map((m) => `${m.model} (${m.units} units)`).join(", ")}`);
|
|
129
|
+
}
|
|
130
|
+
sections.push("");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Change type checklist
|
|
134
|
+
sections.push("## Change type\n");
|
|
135
|
+
sections.push("- [x] `feat` — New feature or capability");
|
|
136
|
+
sections.push("- [ ] `fix` — Bug fix");
|
|
137
|
+
sections.push("- [ ] `refactor` — Code restructuring");
|
|
138
|
+
sections.push("- [ ] `test` — Adding or updating tests");
|
|
139
|
+
sections.push("- [ ] `docs` — Documentation only");
|
|
140
|
+
sections.push("- [ ] `chore` — Build, CI, or tooling changes\n");
|
|
141
|
+
|
|
142
|
+
// AI disclosure
|
|
143
|
+
sections.push("---\n");
|
|
144
|
+
sections.push("*This PR was prepared with AI assistance (GSD auto-mode).*");
|
|
145
|
+
|
|
146
|
+
return { title, body: sections.join("\n") };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function handleShip(
|
|
150
|
+
args: string,
|
|
151
|
+
ctx: ExtensionCommandContext,
|
|
152
|
+
_pi: ExtensionAPI,
|
|
153
|
+
): Promise<void> {
|
|
154
|
+
const basePath = process.cwd();
|
|
155
|
+
const dryRun = args.includes("--dry-run");
|
|
156
|
+
const draft = args.includes("--draft");
|
|
157
|
+
const force = args.includes("--force");
|
|
158
|
+
const baseMatch = args.match(/--base\s+(\S+)/);
|
|
159
|
+
const base = baseMatch?.[1] ?? nativeDetectMainBranch(basePath);
|
|
160
|
+
|
|
161
|
+
if (!isValidRefName(base)) {
|
|
162
|
+
ctx.ui.notify(`Invalid base branch name: ${base}`, "error");
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 1. Validate milestone state
|
|
167
|
+
const state = await deriveState(basePath);
|
|
168
|
+
if (!state.activeMilestone) {
|
|
169
|
+
ctx.ui.notify("No active milestone to ship. Complete milestone work first.", "warning");
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const milestoneId = state.activeMilestone.id;
|
|
174
|
+
const milestoneTitle = state.activeMilestone.title ?? "";
|
|
175
|
+
|
|
176
|
+
// 2. Check for incomplete work (use GSD phase as proxy — no phase field on ActiveRef)
|
|
177
|
+
if (state.phase !== "complete" && !force) {
|
|
178
|
+
ctx.ui.notify(
|
|
179
|
+
`Milestone ${milestoneId} may not be complete (phase: ${state.phase}). Use --force to ship anyway.`,
|
|
180
|
+
"warning",
|
|
181
|
+
);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// 3. Generate PR content
|
|
186
|
+
const { title, body } = generatePRContent(basePath, milestoneId, milestoneTitle);
|
|
187
|
+
|
|
188
|
+
// 4. Dry-run — just show the PR content
|
|
189
|
+
if (dryRun) {
|
|
190
|
+
ctx.ui.notify(`--- PR Preview ---\n\nTitle: ${title}\n\n${body}`, "info");
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// 5. Check git state
|
|
195
|
+
const currentBranch = nativeGetCurrentBranch(basePath);
|
|
196
|
+
if (!isValidRefName(currentBranch)) {
|
|
197
|
+
ctx.ui.notify(`Current branch name is invalid for git: ${currentBranch}`, "error");
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (currentBranch === base) {
|
|
201
|
+
ctx.ui.notify(`You're on ${base} — create a feature branch first.`, "warning");
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// 6. Push and create PR (all argv-safe, no shell interpolation)
|
|
206
|
+
try {
|
|
207
|
+
git(basePath, ["push", "-u", "origin", currentBranch]);
|
|
208
|
+
|
|
209
|
+
const ghArgs = ["pr", "create", "--base", base, "--title", title, "--body", body];
|
|
210
|
+
if (draft) ghArgs.push("--draft");
|
|
211
|
+
|
|
212
|
+
const prUrl = execFileSync("gh", ghArgs, { cwd: basePath, encoding: "utf-8" }).trim();
|
|
213
|
+
|
|
214
|
+
ctx.ui.notify(`PR created: ${prUrl}`, "success");
|
|
215
|
+
} catch (err) {
|
|
216
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
217
|
+
ctx.ui.notify(`Failed to create PR: ${msg}`, "error");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -346,8 +346,7 @@ export async function saveRequirementToDb(
|
|
|
346
346
|
} catch (diskErr) {
|
|
347
347
|
logError('manifest', 'disk write failed, rolling back DB row', { fn: 'saveRequirementToDb', error: String((diskErr as Error).message) });
|
|
348
348
|
try {
|
|
349
|
-
|
|
350
|
-
rollbackAdapter?.prepare('DELETE FROM requirements WHERE id = :id').run({ ':id': id });
|
|
349
|
+
db.deleteRequirementById(id);
|
|
351
350
|
} catch (rollbackErr) {
|
|
352
351
|
logError('manifest', 'SPLIT BRAIN: disk write failed AND DB rollback failed — DB has orphaned row', { fn: 'saveRequirementToDb', id, error: String((rollbackErr as Error).message) });
|
|
353
352
|
}
|
|
@@ -471,7 +470,7 @@ export async function saveDecisionToDb(
|
|
|
471
470
|
} catch (diskErr) {
|
|
472
471
|
logError('manifest', 'disk write failed, rolling back DB row', { fn: 'saveDecisionToDb', error: String((diskErr as Error).message) });
|
|
473
472
|
try {
|
|
474
|
-
|
|
473
|
+
db.deleteDecisionById(id);
|
|
475
474
|
} catch (rollbackErr) {
|
|
476
475
|
logError('manifest', 'SPLIT BRAIN: disk write failed AND DB rollback failed — DB has orphaned row', { fn: 'saveDecisionToDb', id, error: String((rollbackErr as Error).message) });
|
|
477
476
|
}
|
|
@@ -714,8 +713,7 @@ export async function saveArtifactToDb(
|
|
|
714
713
|
await saveFile(fullPath, opts.content);
|
|
715
714
|
} catch (diskErr) {
|
|
716
715
|
logError('manifest', 'disk write failed, rolling back DB row', { fn: 'saveArtifactToDb', error: String((diskErr as Error).message) });
|
|
717
|
-
|
|
718
|
-
rollbackAdapter?.prepare('DELETE FROM artifacts WHERE path = :path').run({ ':path': opts.path });
|
|
716
|
+
db.deleteArtifactByPath(opts.path);
|
|
719
717
|
throw diskErr;
|
|
720
718
|
}
|
|
721
719
|
}
|
|
@@ -153,7 +153,7 @@ Setting `prefer_skills: []` does **not** disable skill discovery — it just mea
|
|
|
153
153
|
|
|
154
154
|
- `context_pause_threshold`: number (0-100) — context window usage percentage at which auto-mode should pause to suggest checkpointing. Set to `0` to disable. Default: `0` (disabled).
|
|
155
155
|
|
|
156
|
-
- `token_profile`: `"budget"`, `"balanced"`, or `"
|
|
156
|
+
- `token_profile`: `"budget"`, `"balanced"`, `"quality"`, or `"burn-max"` — coordinates model selection, phase skipping, and context compression. `budget` skips research/reassessment and uses cheaper models; `balanced` (default) skips research/reassessment to reduce token burn; `quality` prefers higher-quality models; `burn-max` keeps full-context defaults, disables downgrade routing, and keeps phase skips off.
|
|
157
157
|
|
|
158
158
|
- `phases`: fine-grained control over which phases run. Usually set by `token_profile`, but can be overridden. Keys:
|
|
159
159
|
- `skip_research`: boolean — skip milestone-level research. Default: `false`.
|
|
@@ -191,6 +191,19 @@ Setting `prefer_skills: []` does **not** disable skill discovery — it just mea
|
|
|
191
191
|
- `hooks`: boolean — enable routing hooks. Default: `true`.
|
|
192
192
|
- `capability_routing`: boolean — enable capability-profile scoring for model selection within a tier. Requires `enabled: true`. Default: `false`.
|
|
193
193
|
|
|
194
|
+
- `uok`: Unified Orchestration Kernel controls. Keys:
|
|
195
|
+
- `enabled`: boolean — enable kernel wrappers and contract observers. Default: `true`.
|
|
196
|
+
- `legacy_fallback.enabled`: boolean — emergency release fallback that forces legacy orchestration behavior even when `uok.enabled` is `true`. Default: `false`.
|
|
197
|
+
- Runtime override: set `GSD_UOK_FORCE_LEGACY=1` (or `GSD_UOK_LEGACY_FALLBACK=1`) to force legacy behavior for the current process.
|
|
198
|
+
- `gates.enabled`: boolean — route checks through the unified gate runner and persist `gate_runs`.
|
|
199
|
+
- `model_policy.enabled`: boolean — enforce policy filtering before model capability scoring.
|
|
200
|
+
- `execution_graph.enabled`: boolean — enable DAG scheduler facade/adapters for execution.
|
|
201
|
+
- `gitops.enabled`: boolean — persist turn-level git transaction records.
|
|
202
|
+
- `gitops.turn_action`: `"commit"` | `"snapshot"` | `"status-only"` — turn transaction mode.
|
|
203
|
+
- `gitops.turn_push`: boolean — whether turn transactions should include push intent metadata.
|
|
204
|
+
- `audit_unified.enabled`: boolean — dual-write unified audit envelope events.
|
|
205
|
+
- `plan_v2.enabled`: boolean — enable bounded clarify/research/draft/compile planning flow.
|
|
206
|
+
|
|
194
207
|
- `context_management`: configures context hygiene for auto-mode sessions. Keys:
|
|
195
208
|
- `observation_masking`: boolean — mask old tool results to reduce context bloat. Default: `true`.
|
|
196
209
|
- `observation_mask_turns`: number — keep this many recent turns verbatim (1-50). Default: `8`.
|
|
@@ -34,6 +34,7 @@ import {
|
|
|
34
34
|
nativeAddPaths,
|
|
35
35
|
nativeResetSoft,
|
|
36
36
|
nativeCommitSubject,
|
|
37
|
+
_resetHasChangesCache,
|
|
37
38
|
} from "./native-git-bridge.js";
|
|
38
39
|
import { GSDError, GSD_MERGE_CONFLICT, GSD_GIT_ERROR } from "./errors.js";
|
|
39
40
|
import { getErrorMessage } from "./error-utils.js";
|
|
@@ -93,6 +94,17 @@ export interface CommitOptions {
|
|
|
93
94
|
allowEmpty?: boolean;
|
|
94
95
|
}
|
|
95
96
|
|
|
97
|
+
export type TurnGitActionMode = "commit" | "snapshot" | "status-only";
|
|
98
|
+
|
|
99
|
+
export interface TurnGitActionResult {
|
|
100
|
+
action: TurnGitActionMode;
|
|
101
|
+
status: "ok" | "failed";
|
|
102
|
+
commitMessage?: string;
|
|
103
|
+
snapshotLabel?: string;
|
|
104
|
+
dirty?: boolean;
|
|
105
|
+
error?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
96
108
|
// ─── Meaningful Commit Message Generation ───────────────────────────────────
|
|
97
109
|
|
|
98
110
|
/** Context for generating a meaningful commit message from task execution results. */
|
|
@@ -822,6 +834,62 @@ export function createGitService(basePath: string): GitServiceImpl {
|
|
|
822
834
|
return new GitServiceImpl(basePath, gitPrefs);
|
|
823
835
|
}
|
|
824
836
|
|
|
837
|
+
function buildTurnSnapshotLabel(unitType: string, unitId: string): string {
|
|
838
|
+
const raw = `${unitType}/${unitId}`.trim();
|
|
839
|
+
if (!raw) return "turn";
|
|
840
|
+
return raw
|
|
841
|
+
.replace(/[^a-zA-Z0-9._/-]/g, "-")
|
|
842
|
+
.replace(/\/{2,}/g, "/")
|
|
843
|
+
.replace(/-{2,}/g, "-")
|
|
844
|
+
.replace(/^[-/]+|[-/]+$/g, "") || "turn";
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
export function runTurnGitAction(args: {
|
|
848
|
+
basePath: string;
|
|
849
|
+
action: TurnGitActionMode;
|
|
850
|
+
unitType: string;
|
|
851
|
+
unitId: string;
|
|
852
|
+
taskContext?: TaskCommitContext;
|
|
853
|
+
}): TurnGitActionResult {
|
|
854
|
+
try {
|
|
855
|
+
// Force fresh working-tree status per turn; nativeHasChanges caches briefly.
|
|
856
|
+
_resetHasChangesCache();
|
|
857
|
+
if (args.action === "status-only") {
|
|
858
|
+
return {
|
|
859
|
+
action: args.action,
|
|
860
|
+
status: "ok",
|
|
861
|
+
dirty: nativeHasChanges(args.basePath),
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
const git = createGitService(args.basePath);
|
|
866
|
+
if (args.action === "snapshot") {
|
|
867
|
+
const label = buildTurnSnapshotLabel(args.unitType, args.unitId);
|
|
868
|
+
git.createSnapshot(label);
|
|
869
|
+
return {
|
|
870
|
+
action: args.action,
|
|
871
|
+
status: "ok",
|
|
872
|
+
snapshotLabel: label,
|
|
873
|
+
dirty: nativeHasChanges(args.basePath),
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
const commitMessage = git.autoCommit(args.unitType, args.unitId, [], args.taskContext) ?? undefined;
|
|
878
|
+
return {
|
|
879
|
+
action: args.action,
|
|
880
|
+
status: "ok",
|
|
881
|
+
commitMessage,
|
|
882
|
+
dirty: nativeHasChanges(args.basePath),
|
|
883
|
+
};
|
|
884
|
+
} catch (err) {
|
|
885
|
+
return {
|
|
886
|
+
action: args.action,
|
|
887
|
+
status: "failed",
|
|
888
|
+
error: getErrorMessage(err),
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
825
893
|
// ─── Commit Type Inference ─────────────────────────────────────────────────
|
|
826
894
|
|
|
827
895
|
/**
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph-aware context injection for dispatch prompt builders.
|
|
3
|
+
*
|
|
4
|
+
* Reads the pre-built graph.json and returns a formatted context block
|
|
5
|
+
* for injection into prompts. Gracefully returns null when no graph exists
|
|
6
|
+
* or the query yields no results — callers must handle null.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { logWarning } from "./workflow-logger.js";
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
|
|
13
|
+
interface GraphNode {
|
|
14
|
+
id: string;
|
|
15
|
+
label: string;
|
|
16
|
+
type: string;
|
|
17
|
+
confidence: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface GraphEdge {
|
|
22
|
+
from: string;
|
|
23
|
+
to: string;
|
|
24
|
+
type: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface GraphQueryResult {
|
|
28
|
+
nodes: GraphNode[];
|
|
29
|
+
edges: GraphEdge[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface GraphStatusResult {
|
|
33
|
+
exists: boolean;
|
|
34
|
+
stale: boolean;
|
|
35
|
+
ageHours?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface GraphApi {
|
|
39
|
+
graphQuery: (projectDir: string, term: string, budget?: number) => Promise<GraphQueryResult>;
|
|
40
|
+
graphStatus: (projectDir: string) => Promise<GraphStatusResult>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface GraphFileShape {
|
|
44
|
+
nodes: GraphNode[];
|
|
45
|
+
edges: GraphEdge[];
|
|
46
|
+
builtAt?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let cachedGraphApi: GraphApi | null = null;
|
|
50
|
+
let resolvedGraphApi = false;
|
|
51
|
+
|
|
52
|
+
export interface GraphSubgraphOptions {
|
|
53
|
+
/** Budget in tokens passed to graphQuery (1 node ≈ 20 tokens, 1 edge ≈ 10 tokens) */
|
|
54
|
+
budget: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function readGraphFile(projectDir: string): GraphFileShape | null {
|
|
58
|
+
try {
|
|
59
|
+
const graphPath = join(projectDir, ".gsd", "graphs", "graph.json");
|
|
60
|
+
const raw = readFileSync(graphPath, "utf-8");
|
|
61
|
+
const parsed = JSON.parse(raw) as Partial<GraphFileShape>;
|
|
62
|
+
const nodes = Array.isArray(parsed.nodes) ? parsed.nodes : [];
|
|
63
|
+
const edges = Array.isArray(parsed.edges) ? parsed.edges : [];
|
|
64
|
+
return { nodes, edges, builtAt: typeof parsed.builtAt === "string" ? parsed.builtAt : undefined };
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function fallbackGraphQuery(projectDir: string, term: string, budget = 3000): Promise<GraphQueryResult> {
|
|
71
|
+
const graph = readGraphFile(projectDir);
|
|
72
|
+
if (!graph) return { nodes: [], edges: [] };
|
|
73
|
+
|
|
74
|
+
const needle = term.trim().toLowerCase();
|
|
75
|
+
const matches = graph.nodes.filter((node) => {
|
|
76
|
+
const hay = [node.id, node.label, node.description].filter(Boolean).join(" ").toLowerCase();
|
|
77
|
+
return hay.includes(needle);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const maxNodes = Math.max(1, Math.floor(Math.max(1, budget) / 20));
|
|
81
|
+
const selectedIds = new Set(matches.slice(0, maxNodes).map((node) => node.id));
|
|
82
|
+
const nodeById = new Map(graph.nodes.map((node) => [node.id, node] as const));
|
|
83
|
+
|
|
84
|
+
// Pull one-hop neighbors so relation context survives even when the term
|
|
85
|
+
// matches only one side of an edge.
|
|
86
|
+
for (const edge of graph.edges) {
|
|
87
|
+
if (selectedIds.size >= maxNodes) break;
|
|
88
|
+
const touchesSelection = selectedIds.has(edge.from) || selectedIds.has(edge.to);
|
|
89
|
+
if (!touchesSelection) continue;
|
|
90
|
+
if (selectedIds.has(edge.from) && !selectedIds.has(edge.to) && nodeById.has(edge.to)) {
|
|
91
|
+
selectedIds.add(edge.to);
|
|
92
|
+
} else if (selectedIds.has(edge.to) && !selectedIds.has(edge.from) && nodeById.has(edge.from)) {
|
|
93
|
+
selectedIds.add(edge.from);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const nodes = graph.nodes.filter((node) => selectedIds.has(node.id));
|
|
98
|
+
|
|
99
|
+
const remainingBudget = Math.max(0, budget - nodes.length * 20);
|
|
100
|
+
const maxEdges = Math.floor(remainingBudget / 10);
|
|
101
|
+
const edges = graph.edges
|
|
102
|
+
.filter((edge) => selectedIds.has(edge.from) && selectedIds.has(edge.to))
|
|
103
|
+
.slice(0, maxEdges);
|
|
104
|
+
|
|
105
|
+
return { nodes, edges };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function fallbackGraphStatus(projectDir: string): Promise<GraphStatusResult> {
|
|
109
|
+
const graph = readGraphFile(projectDir);
|
|
110
|
+
if (!graph) return { exists: false, stale: false };
|
|
111
|
+
if (!graph.builtAt) return { exists: true, stale: false };
|
|
112
|
+
|
|
113
|
+
const builtAtMs = Date.parse(graph.builtAt);
|
|
114
|
+
if (!Number.isFinite(builtAtMs)) return { exists: true, stale: false };
|
|
115
|
+
|
|
116
|
+
const ageHours = (Date.now() - builtAtMs) / (1000 * 60 * 60);
|
|
117
|
+
return { exists: true, stale: ageHours > 24, ageHours };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function isGraphApi(mod: unknown): mod is GraphApi {
|
|
121
|
+
if (!mod || typeof mod !== "object") return false;
|
|
122
|
+
const candidate = mod as Record<string, unknown>;
|
|
123
|
+
return typeof candidate.graphQuery === "function" && typeof candidate.graphStatus === "function";
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function resolveGraphApi(): Promise<GraphApi> {
|
|
127
|
+
if (resolvedGraphApi && cachedGraphApi) return cachedGraphApi;
|
|
128
|
+
|
|
129
|
+
resolvedGraphApi = true;
|
|
130
|
+
try {
|
|
131
|
+
const imported = await import("@gsd-build/mcp-server");
|
|
132
|
+
if (isGraphApi(imported)) {
|
|
133
|
+
cachedGraphApi = imported;
|
|
134
|
+
return cachedGraphApi;
|
|
135
|
+
}
|
|
136
|
+
logWarning("prompt", "@gsd-build/mcp-server graph exports unavailable; using local graph fallback");
|
|
137
|
+
} catch {
|
|
138
|
+
// Fall back to local reader implementation.
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
cachedGraphApi = {
|
|
142
|
+
graphQuery: fallbackGraphQuery,
|
|
143
|
+
graphStatus: fallbackGraphStatus,
|
|
144
|
+
};
|
|
145
|
+
return cachedGraphApi;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Query the knowledge graph for nodes related to the given term and format
|
|
150
|
+
* the result as an inlined context block.
|
|
151
|
+
*
|
|
152
|
+
* Returns null when:
|
|
153
|
+
* - @gsd-build/mcp-server fails to import
|
|
154
|
+
* - graph.json does not exist (graphQuery already handles this gracefully)
|
|
155
|
+
* - query returns zero nodes
|
|
156
|
+
*
|
|
157
|
+
* Annotates the block header when the graph is stale (> 24 hours old).
|
|
158
|
+
*/
|
|
159
|
+
export async function inlineGraphSubgraph(
|
|
160
|
+
projectDir: string,
|
|
161
|
+
term: string,
|
|
162
|
+
opts: GraphSubgraphOptions,
|
|
163
|
+
): Promise<string | null> {
|
|
164
|
+
if (!term || !term.trim()) return null;
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
const graphApi = await resolveGraphApi();
|
|
168
|
+
const result = await graphApi.graphQuery(projectDir, term, opts.budget);
|
|
169
|
+
if (result.nodes.length === 0) return null;
|
|
170
|
+
|
|
171
|
+
// Check staleness for annotation
|
|
172
|
+
let staleAnnotation = "";
|
|
173
|
+
try {
|
|
174
|
+
const status = await graphApi.graphStatus(projectDir);
|
|
175
|
+
if (status.exists && status.stale && status.ageHours !== undefined) {
|
|
176
|
+
const hours = Math.round(status.ageHours);
|
|
177
|
+
staleAnnotation = `\n> ⚠ Graph last built ${hours}h ago — context may be outdated`;
|
|
178
|
+
}
|
|
179
|
+
} catch {
|
|
180
|
+
// Non-fatal — skip annotation on error
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Format nodes as a compact list
|
|
184
|
+
const nodeLines = result.nodes.map((node) => {
|
|
185
|
+
const desc = node.description ? ` — ${node.description}` : "";
|
|
186
|
+
return `- **${node.label}** (\`${node.type}\`, ${node.confidence})${desc}`;
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// Format edges as relations (only if present)
|
|
190
|
+
const edgeLines = result.edges.length > 0
|
|
191
|
+
? result.edges.map((edge) => `- \`${edge.from}\` →[${edge.type}]→ \`${edge.to}\``)
|
|
192
|
+
: [];
|
|
193
|
+
|
|
194
|
+
const sections: string[] = [
|
|
195
|
+
`### Knowledge Graph Context (term: "${term}")`,
|
|
196
|
+
`Source: \`.gsd/graphs/graph.json\``,
|
|
197
|
+
staleAnnotation,
|
|
198
|
+
"",
|
|
199
|
+
`**Nodes (${result.nodes.length}):**`,
|
|
200
|
+
...nodeLines,
|
|
201
|
+
];
|
|
202
|
+
|
|
203
|
+
if (edgeLines.length > 0) {
|
|
204
|
+
sections.push("", `**Relations (${result.edges.length}):**`, ...edgeLines);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return sections.filter((l) => l !== undefined).join("\n");
|
|
208
|
+
} catch (err) {
|
|
209
|
+
logWarning("prompt", `inlineGraphSubgraph failed (non-fatal): ${err instanceof Error ? err.message : String(err)}`);
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
}
|