@polymorphism-tech/morph-spec 10.12.0 → 11.0.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/CLAUDE.md +27 -57
- package/README.md +51 -340
- package/bin/morph-spec.js +22 -373
- package/docs/CHEATSHEET.md +37 -166
- package/docs/QUICKSTART.md +14 -74
- package/framework/CLAUDE.md +15 -26
- package/framework/MORPH.md +34 -101
- package/framework/agents.json +8 -8
- package/framework/commands/morph-apply.md +5 -5
- package/framework/commands/morph-infra.md +1 -1
- package/framework/docs/CLI.md +74 -886
- package/framework/evals/review-checklist.md +14 -14
- package/framework/hooks/README.md +21 -189
- package/framework/hooks/claude-code/core/README.md +2 -5
- package/framework/hooks/claude-code/core/conventional-commit.js +2 -2
- package/framework/hooks/claude-code/core/denylist.json +3 -3
- package/framework/hooks/claude-code/core/graph-refresh-kick.js +2 -2
- package/framework/hooks/claude-code/core/masked-exit.js +6 -6
- package/framework/hooks/claude-code/core/pre-bash-denylist.js +2 -2
- package/framework/hooks/claude-code/core/protect-spec-files.js +17 -61
- package/framework/hooks/claude-code/core/stack-changed.js +35 -0
- package/framework/hooks/claude-code/core/validate-completion.js +31 -86
- package/framework/hooks/shared/{heavy-command.js → command-kind.js} +6 -79
- package/framework/hooks/shared/command-tokens.js +0 -139
- package/framework/hooks/shared/contract.js +14 -0
- package/framework/hooks/shared/feature-slug.js +21 -0
- package/framework/hooks/shared/frontend-locations.js +0 -1
- package/framework/hooks/shared/git-paths.js +0 -15
- package/framework/hooks/shared/graph-freshness.js +0 -19
- package/framework/hooks/shared/hook-response.js +0 -7
- package/framework/hooks/shared/morph-cli.js +2 -81
- package/framework/hooks/shared/owner-words.js +2 -120
- package/framework/hooks/shared/payload-utils.js +0 -39
- package/framework/hooks/shared/report-progress.js +7 -101
- package/framework/hooks/statusline.js +16 -60
- package/framework/mcp/registry.json +2 -2
- package/framework/rules/ai-agents-standards.md +1 -1
- package/framework/rules/asaas-standards.md +1 -1
- package/framework/rules/ef-standards.md +1 -1
- package/framework/rules/gsap-standards.md +1 -1
- package/framework/rules/hangfire-standards.md +1 -1
- package/framework/rules/implement-report.md +3 -3
- package/framework/rules/neon-auth-standards.md +1 -1
- package/framework/rules/nextjs-standards.md +1 -1
- package/framework/rules/plan-grammar.md +13 -11
- package/framework/rules/resend-standards.md +1 -1
- package/framework/rules/ui-design.md +4 -0
- package/framework/schemas/config.schema.json +95 -310
- package/framework/skills/README.md +12 -32
- package/framework/skills/core/create-umbraco-lp/SKILL.md +1 -1
- package/framework/skills/core/migrate-lp-to-umbraco/SKILL.md +1 -1
- package/framework/skills/core/morph-apply/SKILL.md +21 -46
- package/framework/skills/core/morph-plan/SKILL.md +28 -63
- package/framework/skills/core/morph-uiux/SKILL.md +23 -25
- package/framework/{templates/docs/ui-mockups.md → skills/core/morph-uiux/references/mockups.md} +4 -4
- package/framework/standards/STANDARDS.json +10 -11
- package/framework/standards/STANDARDS.meta.json +1 -1
- package/framework/standards/ai-agents/evals-with-cache.md +18 -32
- package/framework/standards/ai-agents/observability-patterns.md +2 -2
- package/framework/standards/architecture/code-comments.md +2 -5
- package/framework/standards/architecture/engineering-lens.md +1 -1
- package/framework/standards/backend/dotnet/testing.md +4 -4
- package/framework/standards/frontend/nextjs/lint.md +24 -10
- package/framework/standards/frontend/nextjs/testing.md +2 -2
- package/framework/standards/infrastructure/docker/local-compose-isolation.md +14 -30
- package/framework/templates/CLAUDE.md.core.guide.md +2 -2
- package/framework/templates/README.md +1 -1
- package/framework/templates/REGISTRY.json +3 -33
- package/framework/templates/docs/plan.md +3 -2
- package/framework/templates/dotnet/ai-kit/README.md +2 -4
- package/framework/templates/dotnet/ai-kit/src/Morph.AiKit/Evals/EvalCacheMissException.cs +1 -1
- package/framework/templates/dotnet/ai-kit/src/Morph.AiKit/Evals/EvalFixture.cs +1 -1
- package/package.json +1 -2
- package/src/commands/graph/explain.js +2 -1
- package/src/commands/graph/status.js +2 -1
- package/src/commands/mcp/mcp-setup.js +1 -2
- package/src/commands/project/doctor.js +5 -341
- package/src/commands/project/env.js +21 -52
- package/src/commands/project/init-contract.js +47 -0
- package/src/commands/project/init.js +36 -66
- package/src/commands/project/stack-scan.js +1 -1
- package/src/commands/project/update.js +15 -50
- package/src/commands/validation/check.js +128 -0
- package/src/commands/validation/plan-check.js +41 -64
- package/src/commands/validation/prove-red.js +1 -1
- package/src/core/paths/output-schema.js +0 -22
- package/src/core/templates/template-renderer.js +0 -14
- package/src/lib/ai/ai-project-scan.js +1 -6
- package/src/lib/check/attempts.js +38 -0
- package/src/lib/check/signals.js +145 -0
- package/src/lib/config/project-config.js +13 -0
- package/src/lib/contract/apply.js +62 -0
- package/src/lib/contract/defaults.js +96 -0
- package/src/lib/contract/merge.js +79 -0
- package/src/lib/contract/ports.js +43 -0
- package/src/lib/contract/scan.js +102 -0
- package/src/lib/detectors/claude-config-detector.js +7 -0
- package/src/lib/{procs → detectors}/mcp-config.js +1 -37
- package/src/lib/diff/diff-lines.js +64 -0
- package/src/lib/generators/architecture-scanner.js +1 -1
- package/src/lib/generators/scaffold-generator.js +0 -7
- package/src/lib/git/default-branch.js +0 -122
- package/src/lib/graph/diff-tree.js +2 -1
- package/src/lib/graph/god-nodes.js +2 -1
- package/src/lib/graph/graphify-runtime.js +0 -1
- package/src/lib/graph/graphifyignore.js +1 -1
- package/src/lib/graph/sanitize.js +2 -8
- package/src/lib/installers/memory-installer.js +15 -30
- package/src/lib/installers/stack-sync.js +16 -9
- package/src/lib/plan/checks.js +20 -64
- package/src/lib/plan/media.js +1 -1
- package/src/lib/plan/plan-parser.js +5 -5
- package/src/lib/review-reports.js +3 -77
- package/src/lib/scan/worktree-dirs.js +3 -0
- package/src/lib/specs/adr-index.js +2 -2
- package/src/lib/stack/dotnet-build-ignores.js +10 -2
- package/src/lib/stack/stack-scan.js +2 -2
- package/src/lib/stack-filter.js +3 -2
- package/src/lib/tests-audit/audit.js +1 -1
- package/src/scripts/setup-infra.js +6 -59
- package/src/utils/agents-installer.js +1 -34
- package/src/utils/atomic-json.js +27 -0
- package/src/utils/file-copier.js +4 -12
- package/src/utils/hooks-installer.js +9 -107
- package/claude-plugin.json +0 -14
- package/framework/commands/morph-archive.md +0 -46
- package/framework/commands/morph-hotfix.md +0 -17
- package/framework/commands/morph-preflight.md +0 -180
- package/framework/commands/morph-review.md +0 -15
- package/framework/commands/morph-status.md +0 -37
- package/framework/hooks/claude-code/core/block-destructive-worktree-removal.js +0 -80
- package/framework/hooks/claude-code/core/block-raw-worktree-add.js +0 -72
- package/framework/hooks/claude-code/core/enforce-phase-writes.js +0 -100
- package/framework/hooks/claude-code/core/gate-guard.js +0 -148
- package/framework/hooks/claude-code/core/heavy-gate.js +0 -78
- package/framework/hooks/claude-code/core/heavy-release.js +0 -23
- package/framework/hooks/claude-code/core/loop-detect.js +0 -79
- package/framework/hooks/claude-code/core/state-sync.js +0 -214
- package/framework/hooks/claude-code/core/telemetry-log.js +0 -259
- package/framework/hooks/claude-code/core/test-budget.js +0 -29
- package/framework/hooks/claude-code/core/worktree-context.js +0 -249
- package/framework/hooks/claude-code/core/worktree-create.js +0 -119
- package/framework/hooks/claude-code/core/worktree-env-inject.js +0 -68
- package/framework/hooks/claude-code/core/worktree-remove.js +0 -55
- package/framework/hooks/shared/build-check.js +0 -120
- package/framework/hooks/shared/claim-registry.js +0 -125
- package/framework/hooks/shared/e2e-check.js +0 -1022
- package/framework/hooks/shared/feature-locator.js +0 -82
- package/framework/hooks/shared/gate-event.js +0 -18
- package/framework/hooks/shared/gate-question.js +0 -20
- package/framework/hooks/shared/heavy-config.js +0 -113
- package/framework/hooks/shared/heavy-holders.js +0 -85
- package/framework/hooks/shared/heavy-slots.js +0 -280
- package/framework/hooks/shared/host-services.js +0 -440
- package/framework/hooks/shared/next-step.js +0 -181
- package/framework/hooks/shared/redact-secrets.js +0 -44
- package/framework/hooks/shared/reparse-scan.js +0 -40
- package/framework/hooks/shared/state-reader.js +0 -220
- package/framework/hooks/shared/state-writer.js +0 -112
- package/framework/hooks/shared/telemetry-logger.js +0 -101
- package/framework/hooks/shared/transcript-usage.js +0 -58
- package/framework/hooks/shared/worktree-root.js +0 -71
- package/framework/phases.json +0 -92
- package/framework/schemas/feature.schema.json +0 -242
- package/framework/skills/core/morph-apply/references/hotfix.md +0 -46
- package/framework/skills/core/morph-apply/references/machine.md +0 -30
- package/framework/skills/core/morph-apply/references/review.md +0 -77
- package/framework/skills/core/morph-plan/references/critique.md +0 -51
- package/framework/skills/core/morph-plan/references/interview.md +0 -37
- package/framework/templates/state.template.json +0 -17
- package/src/commands/dashboard/dashboard.js +0 -74
- package/src/commands/e2e/e2e.js +0 -400
- package/src/commands/observability/cost.js +0 -283
- package/src/commands/project/branch.js +0 -129
- package/src/commands/project/claim.js +0 -132
- package/src/commands/project/doctor-procs.js +0 -162
- package/src/commands/project/fleet.js +0 -66
- package/src/commands/project/heavy.js +0 -81
- package/src/commands/project/install-plugin-cmd.js +0 -27
- package/src/commands/project/status.js +0 -61
- package/src/commands/project/worktree.js +0 -1234
- package/src/commands/state/advance.js +0 -248
- package/src/commands/state/approve.js +0 -303
- package/src/commands/state/archive.js +0 -231
- package/src/commands/state/auto-approval.js +0 -67
- package/src/commands/state/create.js +0 -274
- package/src/commands/state/delete.js +0 -192
- package/src/commands/state/finish.js +0 -1247
- package/src/commands/state/gate-check.js +0 -244
- package/src/commands/state/gate-decision.js +0 -122
- package/src/commands/state/list.js +0 -244
- package/src/commands/state/next.js +0 -24
- package/src/commands/state/recap.js +0 -80
- package/src/commands/state/report-check.js +0 -244
- package/src/commands/state/retype.js +0 -110
- package/src/commands/validation/verify.js +0 -421
- package/src/core/state/feature-identity.js +0 -10
- package/src/core/state/gate-order.js +0 -23
- package/src/core/state/lifecycle-audit.js +0 -171
- package/src/core/state/state-manager.js +0 -523
- package/src/core/templates/template-seed.js +0 -21
- package/src/core/work-types.js +0 -44
- package/src/dashboard/app/App.jsx +0 -84
- package/src/dashboard/app/components/ActivityFeed.jsx +0 -166
- package/src/dashboard/app/components/CommandPalette.jsx +0 -144
- package/src/dashboard/app/components/DetailPanel.jsx +0 -43
- package/src/dashboard/app/components/EntityLink.jsx +0 -42
- package/src/dashboard/app/components/ErrorBoundary.jsx +0 -47
- package/src/dashboard/app/components/FlowGraph.jsx +0 -19
- package/src/dashboard/app/components/GatePauses.jsx +0 -116
- package/src/dashboard/app/components/KnowledgeMarkdown.jsx +0 -42
- package/src/dashboard/app/components/Markdown.jsx +0 -38
- package/src/dashboard/app/components/PhaseRail.jsx +0 -100
- package/src/dashboard/app/components/SearchFilter.jsx +0 -36
- package/src/dashboard/app/components/SessionMap.jsx +0 -77
- package/src/dashboard/app/components/Sidebar.jsx +0 -147
- package/src/dashboard/app/hooks/useMorphQuery.js +0 -44
- package/src/dashboard/app/hooks/usePref.js +0 -34
- package/src/dashboard/app/lib/format.js +0 -69
- package/src/dashboard/app/lib/phases.js +0 -75
- package/src/dashboard/app/lib/project.js +0 -38
- package/src/dashboard/app/main.jsx +0 -13
- package/src/dashboard/app/pages/Activity.jsx +0 -28
- package/src/dashboard/app/pages/AgentMap.jsx +0 -363
- package/src/dashboard/app/pages/FeatureDetail.jsx +0 -579
- package/src/dashboard/app/pages/Features.jsx +0 -393
- package/src/dashboard/app/pages/Guide.jsx +0 -145
- package/src/dashboard/app/pages/HookInspector.jsx +0 -444
- package/src/dashboard/app/pages/PhasePipeline.jsx +0 -273
- package/src/dashboard/app/pages/Portfolio.jsx +0 -190
- package/src/dashboard/app/pages/RulesMap.jsx +0 -285
- package/src/dashboard/app/pages/SkillsLibrary.jsx +0 -468
- package/src/dashboard/app/pages/Standards.jsx +0 -392
- package/src/dashboard/app/styles/index.css +0 -187
- package/src/dashboard/index.html +0 -14
- package/src/dashboard/package.json +0 -32
- package/src/dashboard/scanner/activity-scanner.js +0 -321
- package/src/dashboard/scanner/agents-scanner.js +0 -67
- package/src/dashboard/scanner/docs-scanner.js +0 -34
- package/src/dashboard/scanner/feature-status.js +0 -7
- package/src/dashboard/scanner/features-scanner.js +0 -397
- package/src/dashboard/scanner/hooks-scanner.js +0 -96
- package/src/dashboard/scanner/index.js +0 -21
- package/src/dashboard/scanner/phases-scanner.js +0 -14
- package/src/dashboard/scanner/portfolio.js +0 -129
- package/src/dashboard/scanner/project-api.js +0 -160
- package/src/dashboard/scanner/rules-scanner.js +0 -46
- package/src/dashboard/scanner/skills-scanner.js +0 -104
- package/src/dashboard/scanner/standards-scanner.js +0 -38
- package/src/dashboard/vite-plugin-morph-data.js +0 -120
- package/src/dashboard/vite.config.js +0 -26
- package/src/lib/ci/ci-verify.js +0 -30
- package/src/lib/ci/github-ci.js +0 -52
- package/src/lib/ci/pr-checks.js +0 -76
- package/src/lib/e2e/applicability.js +0 -39
- package/src/lib/e2e/compose-env.js +0 -80
- package/src/lib/e2e/stack-teardown.js +0 -96
- package/src/lib/git/branch-classifier.js +0 -104
- package/src/lib/git/evidence-staleness.js +0 -40
- package/src/lib/git/gh.js +0 -15
- package/src/lib/git/pending-prs.js +0 -58
- package/src/lib/git/pr-files.js +0 -31
- package/src/lib/git/pr-index.js +0 -39
- package/src/lib/git/tree-fingerprint.js +0 -58
- package/src/lib/git/tree-state.js +0 -125
- package/src/lib/heavy/cleanup.js +0 -72
- package/src/lib/heavy/gate.js +0 -168
- package/src/lib/heavy/live-stacks.js +0 -64
- package/src/lib/heavy/reclaim.js +0 -107
- package/src/lib/heavy/watchdog.js +0 -46
- package/src/lib/next-step.js +0 -12
- package/src/lib/plan/critique.js +0 -67
- package/src/lib/plan/plan-template.js +0 -18
- package/src/lib/procs/classify.js +0 -91
- package/src/lib/procs/list.js +0 -124
- package/src/lib/procs/prune.js +0 -47
- package/src/lib/procs/wslconfig.js +0 -70
- package/src/lib/projects/registry.js +0 -121
- package/src/lib/recap/piper.js +0 -36
- package/src/lib/recap/recap.js +0 -259
- package/src/lib/review-confirmation.js +0 -66
- package/src/lib/risk/diff-risk.js +0 -137
- package/src/lib/risk-detector.js +0 -638
- package/src/lib/specs/design-seed.js +0 -18
- package/src/lib/specs/screen-checklist.js +0 -315
- package/src/lib/stack/stack-profile.js +0 -69
- package/src/lib/tasks/eval-node.js +0 -293
- package/src/lib/tasks/linux-runner.js +0 -200
- package/src/lib/tasks/test-nodes.js +0 -414
- package/src/lib/tasks/test-runner.js +0 -569
- package/src/lib/telemetry/metrics.js +0 -384
- package/src/lib/telemetry/telemetry-reader.js +0 -276
- package/src/lib/telemetry/verify-event.js +0 -57
- package/src/lib/validators/code-budget/code-budget-validator.js +0 -170
- package/src/lib/validators/contracts/contract-compliance-validator.js +0 -219
- package/src/lib/validators/nextjs/next-component-validator.js +0 -142
- package/src/lib/validators/packages/package-validator.js +0 -310
- package/src/lib/validators/reuse/reuse-validator.js +0 -202
- package/src/lib/validators/reuse/symbol-scanner.js +0 -64
- package/src/lib/validators/validation-runner.js +0 -188
- package/src/lib/validators/verify-cache.js +0 -63
- package/src/lib/validators/verify-runner.js +0 -971
- package/src/lib/verify/affected.js +0 -143
- package/src/lib/verify/detached.js +0 -159
- package/src/lib/view/feature-html.js +0 -480
- package/src/lib/view/feature-page.client.js +0 -109
- package/src/lib/view/feature-page.css +0 -278
- package/src/lib/view/feature-view.js +0 -113
- package/src/lib/view/flow-layers.js +0 -79
- package/src/lib/view/flow-svg.js +0 -63
- package/src/lib/view/tokens.css +0 -18
- package/src/lib/work-type-detector.js +0 -81
- package/src/lib/worktree/dirs.js +0 -3
- package/src/lib/worktree/docker-audit.js +0 -175
- package/src/lib/worktree/env-allocator.js +0 -177
- package/src/lib/worktree/feature-truth.js +0 -145
- package/src/lib/worktree/finish-backup.js +0 -529
- package/src/lib/worktree/fleet-scanner.js +0 -223
- package/src/lib/worktree/ignored-triage.js +0 -136
- package/src/lib/worktree/vscode-settings.js +0 -112
- package/src/lib/worktree/worktree-audit.js +0 -323
- package/src/lib/worktree/worktree-include.js +0 -44
- package/src/scripts/install-plugin.js +0 -114
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { existsSync } from 'fs';
|
|
3
|
-
import { join, resolve } from 'path';
|
|
4
|
-
import { blockResult, runStandalone } from '../../shared/hook-response.js';
|
|
5
|
-
import { stateExists } from '../../shared/state-reader.js';
|
|
6
|
-
// From shared/, never from the sibling hook: importing a hook module fires its
|
|
7
|
-
// standalone tail (stdin read + exit) inside THIS process.
|
|
8
|
-
import { gitInvocations, simpleCommands } from '../../shared/command-tokens.js';
|
|
9
|
-
import { WORKTREE_DIR_NAMES } from '../../shared/worktree-root.js';
|
|
10
|
-
|
|
11
|
-
const GIT_EXECUTABLE = /(^|[\\/])git(\.exe)?$/i;
|
|
12
|
-
const TREE_IN_CONTAINER = new RegExp(`^(.*)/(${WORKTREE_DIR_NAMES.map((n) => n.replace('.', '\\.')).join('|')})/([^/]+)(?:/|$)`);
|
|
13
|
-
|
|
14
|
-
export function isRawWorktreeAdd(command) {
|
|
15
|
-
return gitInvocations(command).some(({ sub, args }) => sub === 'worktree' && args[0]?.toLowerCase() === 'add');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// A folder under worktrees/ whose `.git` file is gone is no worktree: git walks up and acts on the ROOT.
|
|
19
|
-
function orphanTreeOf(dir) {
|
|
20
|
-
const m = resolve(dir).replace(/\\/g, '/').match(TREE_IN_CONTAINER);
|
|
21
|
-
if (!m) return null;
|
|
22
|
-
const tree = `${m[1]}/${m[2]}/${m[3]}`;
|
|
23
|
-
return existsSync(tree) && !existsSync(join(tree, '.git')) && existsSync(join(m[1], '.git')) ? tree : null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** The orphan folder some `git` call of the command would run in (cwd, `cd`, `-C`), or null. */
|
|
27
|
-
export function gitInOrphanTree(command, cwd) {
|
|
28
|
-
let dir = cwd;
|
|
29
|
-
for (const tokens of simpleCommands(command)) {
|
|
30
|
-
const texts = tokens.map((t) => t.text);
|
|
31
|
-
if (texts[0] === 'cd' && texts[1]) { dir = resolve(dir, texts[1]); continue; }
|
|
32
|
-
const at = texts.findIndex((t) => GIT_EXECUTABLE.test(t));
|
|
33
|
-
if (at < 0) continue;
|
|
34
|
-
const c = texts.indexOf('-C', at);
|
|
35
|
-
const orphan = orphanTreeOf(c > at && texts[c + 1] ? resolve(dir, texts[c + 1]) : dir);
|
|
36
|
-
if (orphan) return orphan;
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export async function run(payload) {
|
|
42
|
-
try {
|
|
43
|
-
const command = payload?.tool_input?.command || '';
|
|
44
|
-
if (!command) return null;
|
|
45
|
-
|
|
46
|
-
// Before stateExists: the orphan folder has no `.morph/` of its own, and that is the case to stop.
|
|
47
|
-
const orphan = gitInOrphanTree(command, payload?.cwd || process.cwd());
|
|
48
|
-
if (orphan) {
|
|
49
|
-
return blockResult(
|
|
50
|
-
`Bloqueado: \`${orphan}\` não é mais uma worktree (o \`.git\` dela sumiu), então o git subiria até a raiz e rodaria na branch default.\n\n`
|
|
51
|
-
+ `Limpe a pasta com \`morph-spec worktree remove --target "${orphan}"\` a partir da raiz (desmonta as junctions e apaga só o que está vazio). `
|
|
52
|
-
+ 'Para trabalhar na feature, recrie a árvore com `morph-spec worktree setup <feature>`.',
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (!stateExists() || !isRawWorktreeAdd(command)) return null;
|
|
57
|
-
|
|
58
|
-
return blockResult(
|
|
59
|
-
'Bloqueado: `git worktree add` cru cria um worktree que o morph-spec NÃO enxerga — sem as junctions de infra ' +
|
|
60
|
-
'(`.claude`, `node_modules`, `.morph/framework` → nenhum hook roda lá dentro), sem bloco de portas (o dev server ' +
|
|
61
|
-
'colide com o da raiz), sem identidade (invisível para `fleet`, `doctor --worktrees` e `worktree list`) e fora de `<raiz>/worktrees/`.\n\n' +
|
|
62
|
-
'Use o comando que constrói a árvore governada:\n' +
|
|
63
|
-
' - feature: `morph-spec worktree setup <feature>`\n' +
|
|
64
|
-
' - tarefa isolada (sem feature): `morph-spec worktree provision <nome> --base <ref>`\n\n' +
|
|
65
|
-
'Os dois deixam a árvore em `<raiz>/worktrees/<nome>` com a infra completa; `list`/`prune`/`remove` do git não são afetados por este guard.'
|
|
66
|
-
);
|
|
67
|
-
} catch {
|
|
68
|
-
return null;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
await runStandalone(run);
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { existsSync } from 'fs';
|
|
4
|
-
import { stateExists, getFeaturePhase, isGateApproved } from '../../shared/state-reader.js';
|
|
5
|
-
import {
|
|
6
|
-
isFeaturePath,
|
|
7
|
-
extractFeatureName,
|
|
8
|
-
extractPhaseDir,
|
|
9
|
-
PHASE_DIRS
|
|
10
|
-
} from '../../shared/phase-utils.js';
|
|
11
|
-
|
|
12
|
-
// Approving this gate lets the next phase's first file be created: the phase only advances once that file exists.
|
|
13
|
-
const PHASE_DIR_ENTRY_GATE = {
|
|
14
|
-
'1-design': 'plan',
|
|
15
|
-
'3-implement': 'plan',
|
|
16
|
-
'4-review': 'plan',
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const ALWAYS_WRITABLE_DIR = '2-plan';
|
|
20
|
-
import { blockResult, runStandalone } from '../../shared/hook-response.js';
|
|
21
|
-
import { getFilePath, resolveMorphRoot, resolveTargetPath } from '../../shared/payload-utils.js';
|
|
22
|
-
import { GATE_NAME, PHASE_NAME } from '../../shared/owner-words.js';
|
|
23
|
-
import { SESSION_MARK } from '../../shared/next-step.js';
|
|
24
|
-
|
|
25
|
-
export async function run(payload) {
|
|
26
|
-
try {
|
|
27
|
-
if (process.env.MORPH_AMEND_PHASE) {
|
|
28
|
-
console.error(`[morph-spec] modo emenda (MORPH_AMEND_PHASE=${process.env.MORPH_AMEND_PHASE}): proteção de passo desligada`);
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (!payload) return null;
|
|
33
|
-
|
|
34
|
-
const filePath = getFilePath(payload);
|
|
35
|
-
if (!filePath) return null;
|
|
36
|
-
|
|
37
|
-
if (!isFeaturePath(filePath)) return null;
|
|
38
|
-
|
|
39
|
-
// The tree that OWNS the file, not the session's: a root session may write into a worktree's feature.
|
|
40
|
-
const root = resolveMorphRoot(filePath, process.cwd());
|
|
41
|
-
if (!stateExists(root)) return null;
|
|
42
|
-
|
|
43
|
-
const featureName = extractFeatureName(filePath);
|
|
44
|
-
if (!featureName) return null;
|
|
45
|
-
|
|
46
|
-
const phase = getFeaturePhase(featureName, root);
|
|
47
|
-
if (!phase) return null;
|
|
48
|
-
|
|
49
|
-
if (phase === 'implement') return null;
|
|
50
|
-
|
|
51
|
-
const targetDir = extractPhaseDir(filePath);
|
|
52
|
-
if (!targetDir) return null;
|
|
53
|
-
if (targetDir === ALWAYS_WRITABLE_DIR) return null;
|
|
54
|
-
|
|
55
|
-
const allowedDir = PHASE_DIRS[phase];
|
|
56
|
-
if (!allowedDir) return null;
|
|
57
|
-
|
|
58
|
-
if (targetDir !== allowedDir) {
|
|
59
|
-
const absolutePath = resolveTargetPath(filePath, root);
|
|
60
|
-
if (existsSync(absolutePath)) {
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const entryGate = PHASE_DIR_ENTRY_GATE[targetDir];
|
|
65
|
-
if (entryGate && isGateApproved(featureName, entryGate, root)) {
|
|
66
|
-
// uiux is optional, but once 1-design/ exists its gate is not
|
|
67
|
-
// decorative: implementation waits for it too.
|
|
68
|
-
const tookDesignBranch = targetDir !== '1-design'
|
|
69
|
-
&& existsSync(resolveTargetPath(`.morph/features/${featureName}/1-design`, root));
|
|
70
|
-
|
|
71
|
-
if (!tookDesignBranch || isGateApproved(featureName, 'uiux', root)) {
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
return blockResult(
|
|
75
|
-
`MORPH-SPEC: ainda não dá para escrever em '${targetDir}/'. Esta funcionalidade tem telas (1-design/ existe) ` +
|
|
76
|
-
`e a ${GATE_NAME.uiux} ainda não saiu: a construção espera por ela.\n\n` +
|
|
77
|
-
`${SESSION_MARK} aprove o gate uiux e depois avance:\n` +
|
|
78
|
-
` morph-spec approve ${featureName} uiux\n` +
|
|
79
|
-
` morph-spec advance ${featureName}`
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const phaseLabel = PHASE_NAME[phase] ?? phase;
|
|
84
|
-
const treeNote = root !== process.cwd().replace(/\\/g, '/')
|
|
85
|
-
? `\n(passo lido da árvore dona do arquivo: ${root})`
|
|
86
|
-
: '';
|
|
87
|
-
return blockResult(
|
|
88
|
-
`MORPH-SPEC: '${targetDir}/' não pode ser escrito durante o passo ${phaseLabel} (${phase}); ` +
|
|
89
|
-
`agora só '${allowedDir}/' aceita escrita.${treeNote}\n\n` +
|
|
90
|
-
`${SESSION_MARK} para seguir, aprove o gate atual e prepare o próximo passo num comando só: ` +
|
|
91
|
-
`morph-spec advance ${featureName} (rode naquela árvore: o gate fica no feature.json dela).`
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
} catch {
|
|
96
|
-
return null;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
await runStandalone(run);
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Detect-and-warn, never block: compares the observed gate action with the persisted gateDecisions.
|
|
3
|
-
|
|
4
|
-
import { loadState, stateExists, getActiveFeature } from '../../shared/state-reader.js';
|
|
5
|
-
import { contextResult, runStandalone } from '../../shared/hook-response.js';
|
|
6
|
-
import { gateFromQuestion } from '../../shared/gate-question.js';
|
|
7
|
-
import { resolveMorphRoot, resolveTargetPath } from '../../shared/payload-utils.js';
|
|
8
|
-
import { locateFeatureForQuestion } from '../../shared/feature-locator.js';
|
|
9
|
-
import { GATE_NAME, gateQuestion } from '../../shared/owner-words.js';
|
|
10
|
-
import { SESSION_MARK } from '../../shared/next-step.js';
|
|
11
|
-
import { ownerRef } from '../../shared/report-progress.js';
|
|
12
|
-
|
|
13
|
-
const gateName = (g) => GATE_NAME[g] ?? g;
|
|
14
|
-
const ref = (state, feature) => ownerRef(projectDir(), feature, state.features?.[feature]);
|
|
15
|
-
|
|
16
|
-
function projectDir() {
|
|
17
|
-
return process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function featureFromStatePath(filePath) {
|
|
21
|
-
const fp = String(filePath ?? '').replace(/\\/g, '/');
|
|
22
|
-
return (fp.match(/\.morph\/features\/([^/]+)\/feature\.json$/) ?? [])[1] ?? null;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// The legacy `.morph/state.json` has no feature segment to anchor on, so strip its own suffix.
|
|
26
|
-
function rootOfGateStateFile(filePath, fallback) {
|
|
27
|
-
const abs = resolveTargetPath(filePath, fallback);
|
|
28
|
-
const suffix = '/.morph/state.json';
|
|
29
|
-
if (abs.endsWith(suffix)) return abs.slice(0, -suffix.length) || fallback;
|
|
30
|
-
return resolveMorphRoot(abs, fallback);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function askedTexts(input) {
|
|
34
|
-
const texts = [];
|
|
35
|
-
const questions = input.tool_input?.questions;
|
|
36
|
-
if (Array.isArray(questions)) {
|
|
37
|
-
for (const q of questions) {
|
|
38
|
-
if (typeof q?.question === 'string') texts.push(q.question);
|
|
39
|
-
if (typeof q?.header === 'string') texts.push(q.header);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
// Fallback: some payloads only surface answered question keys.
|
|
43
|
-
const answers = input.tool_response?.answers ?? input.tool_response?.responses;
|
|
44
|
-
if (answers && typeof answers === 'object') texts.push(...Object.keys(answers));
|
|
45
|
-
return texts;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function getDecision(state, feature, gate) {
|
|
49
|
-
const d = state.features?.[feature]?.gateDecisions?.[gate];
|
|
50
|
-
if (!d || typeof d.pause !== 'boolean') return null;
|
|
51
|
-
return d;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function checkAskUserQuestion(input, sessionRoot) {
|
|
55
|
-
for (const text of askedTexts(input)) {
|
|
56
|
-
const gate = gateFromQuestion(text);
|
|
57
|
-
if (!gate) continue;
|
|
58
|
-
|
|
59
|
-
// Only the feature the question NAMES: a warning about the wrong feature is worse than silence.
|
|
60
|
-
const located = locateFeatureForQuestion(text, sessionRoot);
|
|
61
|
-
if (!located) continue;
|
|
62
|
-
const { name: feature, root: cwd } = located;
|
|
63
|
-
if (!stateExists(cwd)) continue;
|
|
64
|
-
|
|
65
|
-
const state = loadState(cwd);
|
|
66
|
-
const decision = getDecision(state, feature, gate);
|
|
67
|
-
if (!decision) continue;
|
|
68
|
-
if (decision.pause === false) {
|
|
69
|
-
return contextResult(
|
|
70
|
-
`⚠️ Esta pergunta não precisava ser feita: a ${gateName(gate)} de ${ref(state, feature)} estava liberada ` +
|
|
71
|
-
`para seguir sem parar você (nenhum risco encontrado).\n` +
|
|
72
|
-
`${SESSION_MARK} gateDecisions.${gate} era pause:false (trust:${decision.trust ?? 'auto'}): o gate passa sozinho ` +
|
|
73
|
-
`com um resumo curto. Se a pausa foi por um risco que o registro não capturou, atualize ` +
|
|
74
|
-
`gateDecisions.${gate}.signals antes de seguir; senão, não pause gate que passa sozinho.`,
|
|
75
|
-
'PostToolUse'
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
if (decision.resolver === 'session') {
|
|
79
|
-
return contextResult(
|
|
80
|
-
`⚠️ Pergunta feita cedo demais: a ${gateName(gate)} de ${ref(state, feature)} parou por algo que a própria ` +
|
|
81
|
-
`sessão resolve (${decision.reason || 'uma checagem automática'}), não você.\n` +
|
|
82
|
-
`${SESSION_MARK} resolver:"session": faça o que o todo diz (${decision.todo || 'a razão'}) e rode \`morph-spec gate-check ${feature} ${gate}\` de novo; ` +
|
|
83
|
-
`a pergunta só vem quando o gate-check devolver resolver:"human".`,
|
|
84
|
-
'PostToolUse'
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function isGateStateFile(filePath) {
|
|
93
|
-
const fp = String(filePath ?? '').replace(/\\/g, '/');
|
|
94
|
-
if (fp.endsWith('.morph/state.json')) return true;
|
|
95
|
-
return fp.includes('.morph/features/') && fp.endsWith('/feature.json');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function checkStateEdit(input, sessionRoot) {
|
|
99
|
-
const filePath = input.tool_input?.file_path;
|
|
100
|
-
if (!isGateStateFile(filePath)) return null;
|
|
101
|
-
|
|
102
|
-
const cwd = rootOfGateStateFile(filePath, sessionRoot);
|
|
103
|
-
if (!stateExists(cwd)) return null;
|
|
104
|
-
const state = loadState(cwd);
|
|
105
|
-
if (!state) return null;
|
|
106
|
-
const feature = featureFromStatePath(filePath) || getActiveFeature(cwd)?.name;
|
|
107
|
-
if (!feature) return null;
|
|
108
|
-
const gates = state.features?.[feature]?.approvalGates ?? {};
|
|
109
|
-
for (const gate of ['plan', 'uiux', 'review']) {
|
|
110
|
-
const g = gates[gate];
|
|
111
|
-
const autoApproved = g?.approved === true && g?.approvedBy === 'auto';
|
|
112
|
-
if (!autoApproved) continue;
|
|
113
|
-
const decision = getDecision(state, feature, gate);
|
|
114
|
-
if (!decision) continue;
|
|
115
|
-
if (decision.pause === true) {
|
|
116
|
-
return contextResult(
|
|
117
|
-
`⚠️ A ${gateName(gate)} de ${ref(state, feature)} foi dada automaticamente, mas havia um risco que pedia ` +
|
|
118
|
-
`a sua decisão (${decision.reason || 'risco detectado'}).\n` +
|
|
119
|
-
`${SESSION_MARK} approvedBy:"auto" com gateDecisions.${gate}.pause:true ignora o trust mode; reabra com ` +
|
|
120
|
-
`AskUserQuestion "${gateQuestion(gate, feature, state.features?.[feature])}" (opção "aprovado, prosseguir") ` +
|
|
121
|
-
`ou corrija o sinal de risco antes de avançar.`,
|
|
122
|
-
'PostToolUse'
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export async function run(payload) {
|
|
130
|
-
const sessionRoot = projectDir();
|
|
131
|
-
|
|
132
|
-
const input = payload || {};
|
|
133
|
-
const toolName = input.tool_name;
|
|
134
|
-
|
|
135
|
-
if (toolName === 'Edit' || toolName === 'Write') {
|
|
136
|
-
if (!isGateStateFile(input.tool_input?.file_path)) return null;
|
|
137
|
-
} else if (toolName !== 'AskUserQuestion') {
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
try {
|
|
142
|
-
if (toolName === 'AskUserQuestion') return checkAskUserQuestion(input, sessionRoot);
|
|
143
|
-
return checkStateEdit(input, sessionRoot);
|
|
144
|
-
} catch {}
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
await runStandalone(run);
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Never rewrites the command: a rewrite would be judged against the user's permission rules in its rewritten form.
|
|
3
|
-
// The lease has no live pid (the hook exits at once), so it lapses by TTL; a call that fires no Post hook holds it until then.
|
|
4
|
-
// The wait stays under the 600 s hook timeout because a timed-out PreToolUse hook lets the call run.
|
|
5
|
-
|
|
6
|
-
import { spawnSync } from 'node:child_process';
|
|
7
|
-
import { freemem } from 'node:os';
|
|
8
|
-
import { stateExists } from '../../shared/state-reader.js';
|
|
9
|
-
import { blockResult, runStandalone } from '../../shared/hook-response.js';
|
|
10
|
-
import { heavyClass, heavyKind, leaseTtlMs } from '../../shared/heavy-command.js';
|
|
11
|
-
|
|
12
|
-
const HOOK_BUDGET_SEC = 540;
|
|
13
|
-
const RECLAIM_TIMEOUT_MS = 45_000;
|
|
14
|
-
|
|
15
|
-
const defaultFreeGb = () => freemem() / 1024 ** 3;
|
|
16
|
-
|
|
17
|
-
// Through the CLI: an installed hook cannot reach the package's src/.
|
|
18
|
-
function runReclaim(cwd, env) {
|
|
19
|
-
if (env.MORPH_HEAVY_RECLAIM === 'off') return;
|
|
20
|
-
spawnSync('morph-spec heavy reclaim --json', {
|
|
21
|
-
cwd, env, shell: true, windowsHide: true, stdio: 'ignore', timeout: RECLAIM_TIMEOUT_MS,
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export async function run(payload, deps = {}) {
|
|
26
|
-
try {
|
|
27
|
-
if (payload?.tool_name && payload.tool_name !== 'Bash') return null;
|
|
28
|
-
const input = payload?.tool_input ?? {};
|
|
29
|
-
if (input.run_in_background) return null;
|
|
30
|
-
const kind = heavyKind(input.command);
|
|
31
|
-
if (!kind || !payload?.tool_use_id) return null;
|
|
32
|
-
|
|
33
|
-
// Loaded only here: this link runs on every Bash call and almost none is heavy.
|
|
34
|
-
const { minFreeMemFor, readHeavyConfig } = await import('../../shared/heavy-config.js');
|
|
35
|
-
const { acquireHeavy, describeBusy, heavyDir } = await import('../../shared/heavy-slots.js');
|
|
36
|
-
const env = deps.env ?? process.env;
|
|
37
|
-
const dir = env.CLAUDE_PROJECT_DIR || payload?.cwd || process.cwd();
|
|
38
|
-
if (!stateExists(dir)) return null;
|
|
39
|
-
const config = readHeavyConfig(dir, env);
|
|
40
|
-
if (!config.enabled || !config.gateBash) return null;
|
|
41
|
-
|
|
42
|
-
const memClass = heavyClass(kind);
|
|
43
|
-
const floor = minFreeMemFor(config, memClass);
|
|
44
|
-
let spentSec = 0;
|
|
45
|
-
if (config.reclaimIdleMinutes && (deps.freeGb ?? defaultFreeGb)() < floor) {
|
|
46
|
-
(deps.reclaim ?? runReclaim)(dir, env);
|
|
47
|
-
spentSec = RECLAIM_TIMEOUT_MS / 1000;
|
|
48
|
-
}
|
|
49
|
-
const result = (deps.acquire ?? acquireHeavy)({
|
|
50
|
-
dir: heavyDir(env),
|
|
51
|
-
slots: config.slots,
|
|
52
|
-
label: kind,
|
|
53
|
-
feature: null,
|
|
54
|
-
node: null,
|
|
55
|
-
cwd: dir,
|
|
56
|
-
kind: memClass,
|
|
57
|
-
minFreeMemGb: floor,
|
|
58
|
-
waitTimeoutMs: Math.max(0, Math.min(config.waitTimeoutSec, HOOK_BUDGET_SEC) - spentSec) * 1000,
|
|
59
|
-
maxHoldMs: config.maxHoldMinutes * 60_000,
|
|
60
|
-
lease: { id: payload.tool_use_id, ttlMs: leaseTtlMs(input, env) },
|
|
61
|
-
env,
|
|
62
|
-
log: () => {},
|
|
63
|
-
});
|
|
64
|
-
if (result.status !== 'busy') return null;
|
|
65
|
-
|
|
66
|
-
const { SESSION_MARK } = await import('../../shared/next-step.js');
|
|
67
|
-
return blockResult(
|
|
68
|
-
`⏳ \`${kind}\` não rodou: ${describeBusy(result, config)}. Não é falha do código: a máquina está ocupada com outros `
|
|
69
|
-
+ 'testes ou compilações e a fila já esperou o prazo dela.\n'
|
|
70
|
-
+ `${SESSION_MARK} não repita em laço; siga sem esta execução e avise o usuário. `
|
|
71
|
-
+ '`morph-spec heavy status` mostra quem segura os slots; `heavy.gateBash: false` desliga esta espera.',
|
|
72
|
-
);
|
|
73
|
-
} catch {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
await runStandalone(run);
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// A call that fires neither Post event leaves its slot to the TTL. Releasing is not deciding, so this link is safe async.
|
|
3
|
-
|
|
4
|
-
import { existsSync } from 'node:fs';
|
|
5
|
-
import { stateExists } from '../../shared/state-reader.js';
|
|
6
|
-
import { runStandalone } from '../../shared/hook-response.js';
|
|
7
|
-
|
|
8
|
-
export async function run(payload, deps = {}) {
|
|
9
|
-
try {
|
|
10
|
-
if (payload?.tool_name !== 'Bash' || !payload.tool_use_id) return null;
|
|
11
|
-
const env = deps.env ?? process.env;
|
|
12
|
-
if (!stateExists(env.CLAUDE_PROJECT_DIR || payload?.cwd || process.cwd())) return null;
|
|
13
|
-
// Runs after every tool call: pay for the slot module only when a semaphore directory exists.
|
|
14
|
-
const { heavyDir } = await import('../../shared/heavy-holders.js');
|
|
15
|
-
const dir = heavyDir(env);
|
|
16
|
-
if (!existsSync(dir)) return null;
|
|
17
|
-
const { releaseLeaseById } = await import('../../shared/heavy-slots.js');
|
|
18
|
-
releaseLeaseById(dir, payload.tool_use_id);
|
|
19
|
-
} catch {}
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
await runStandalone(run);
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs';
|
|
4
|
-
import { join } from 'path';
|
|
5
|
-
import { stateExists } from '../../shared/state-reader.js';
|
|
6
|
-
import { contextResult, runStandalone } from '../../shared/hook-response.js';
|
|
7
|
-
import { getCommandSignature } from '../../shared/payload-utils.js';
|
|
8
|
-
import { SESSION_MARK } from '../../shared/next-step.js';
|
|
9
|
-
|
|
10
|
-
const MAX_BUFFER = 15;
|
|
11
|
-
const LOOP_WINDOW = 8;
|
|
12
|
-
const MAX_UNIQUE_SIGS = 2;
|
|
13
|
-
const COOLDOWN_MS = 5 * 60 * 1000;
|
|
14
|
-
|
|
15
|
-
export async function run(payload) {
|
|
16
|
-
try {
|
|
17
|
-
if (!stateExists()) return null;
|
|
18
|
-
if (!payload) return null;
|
|
19
|
-
|
|
20
|
-
const cwd = process.cwd();
|
|
21
|
-
const memoryDir = join(cwd, '.morph', 'memory');
|
|
22
|
-
const bufferPath = join(memoryDir, 'action-buffer.json');
|
|
23
|
-
const cooldownPath = join(memoryDir, 'loop-cooldown.json');
|
|
24
|
-
|
|
25
|
-
mkdirSync(memoryDir, { recursive: true });
|
|
26
|
-
|
|
27
|
-
let buffer = [];
|
|
28
|
-
try {
|
|
29
|
-
if (existsSync(bufferPath)) {
|
|
30
|
-
buffer = JSON.parse(readFileSync(bufferPath, 'utf-8'));
|
|
31
|
-
if (!Array.isArray(buffer)) buffer = [];
|
|
32
|
-
}
|
|
33
|
-
} catch {
|
|
34
|
-
buffer = [];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const sig = getCommandSignature(payload);
|
|
38
|
-
buffer.push({ sig, ts: new Date().toISOString() });
|
|
39
|
-
|
|
40
|
-
if (buffer.length > MAX_BUFFER) {
|
|
41
|
-
buffer = buffer.slice(-MAX_BUFFER);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
writeFileSync(bufferPath, JSON.stringify(buffer), 'utf-8');
|
|
45
|
-
|
|
46
|
-
if (buffer.length < LOOP_WINDOW) return null;
|
|
47
|
-
|
|
48
|
-
const recent = buffer.slice(-LOOP_WINDOW);
|
|
49
|
-
const uniqueSigs = new Set(recent.map(a => a.sig));
|
|
50
|
-
|
|
51
|
-
if (uniqueSigs.size > MAX_UNIQUE_SIGS) return null;
|
|
52
|
-
|
|
53
|
-
try {
|
|
54
|
-
if (existsSync(cooldownPath)) {
|
|
55
|
-
const cooldown = JSON.parse(readFileSync(cooldownPath, 'utf-8'));
|
|
56
|
-
const elapsed = Date.now() - new Date(cooldown.lastDetected).getTime();
|
|
57
|
-
if (elapsed < COOLDOWN_MS) return null;
|
|
58
|
-
}
|
|
59
|
-
} catch {}
|
|
60
|
-
|
|
61
|
-
writeFileSync(cooldownPath, JSON.stringify({ lastDetected: new Date().toISOString() }), 'utf-8');
|
|
62
|
-
|
|
63
|
-
const sigList = [...uniqueSigs].join(', ');
|
|
64
|
-
return contextResult(
|
|
65
|
-
`⚠️ A sessão está andando em círculos (LOOP): as últimas ${LOOP_WINDOW} ações repetem só ${uniqueSigs.size} padrão(ões).\n` +
|
|
66
|
-
`${SESSION_MARK} padrões ${sigList}. Pare e mude de abordagem:\n` +
|
|
67
|
-
` - releia a mensagem de erro com calma: o que ela diz de fato?\n` +
|
|
68
|
-
` - tente uma correção completamente diferente\n` +
|
|
69
|
-
` - se o teste segue falhando, confira se o próprio teste está errado\n` +
|
|
70
|
-
` - se travou, pergunte ao usuário\n` +
|
|
71
|
-
`Não repita o mesmo padrão.`,
|
|
72
|
-
'PostToolUse'
|
|
73
|
-
);
|
|
74
|
-
} catch {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
await runStandalone(run);
|