Files
msd-core/tests/declarative-reference-copilot.test.cjs
Tom Boucher d1e9491fef feat(#2099): drive GitHub Copilot through the EoS descriptor + multi-event hook bus (ADR-1239)
Fold Copilot's residual runtime-literal branches onto descriptor-driven
hostBehaviors. Several issue premises were inaccurate (verified via research)
and deliberately NOT followed: writesSharedSettings/"legacy exclusion list"
(per-runtime descriptor data, copilot's false is correct); RUNTIME_CONTENT_DISPATCH.copilot
+ installSurface==='copilot-instructions' (already descriptor-driven);
extendedHookEvents (closed Claude/Gemini enum — hooks extended in code instead);
reapply ternary (already folded, kimi #2095).

Real folds (all byte-parity — golden byte-identical for every runtime):
- src/install-engine.cts + src/surface.cts: the two `.agent.md` filename cutovers
  (_copyStaged + _syncGsdDir) unified onto hostBehaviors.agentFileExtension via a
  new exported agentFileExtensionFor() accessor (kills the two-mechanism divergence).
- src/runtime-artifact-conversion.cts: applyAgentPathRewrites' copilot skip →
  hostBehaviors.noPathRewrite:true (antigravity #2096 precedent).
- bin/install.js uninstall: the two isCopilot cleanup branches → installSurface===
  'copilot-instructions' gate (symmetric with install-time).
- bin/install.js: `!isCopilot` in the two skipSharedHooksInstall checks →
  hostBehaviors.skipSharedHooksInstall:true (copilot has no shared gsd-*.js hooks).
- bin/install.js: three dead legacy inline-agent-loop isCopilot refs removed
  (copilot ∈ _DESCRIPTOR_AGENTS_RUNTIMES → unreachable; byte-parity proven by clean
  golden + real reachable-runtime install diffs). isCopilot dropped from 4 destructures.
Zero live `runtime==='copilot'`/`isCopilot` branches remain in bin/install.js,
install-engine.cts, surface.cts, or runtime-artifact-conversion.cts (AC2 guard scans all four).

UPGRADE 1 (multi-event hook bus): buildCopilotHookConfig() now emits preToolUse/
postToolUse/userPromptSubmitted/sessionEnd advisory handlers alongside sessionStart
(static inline bash/powershell — deterministic, golden-trackable). Only copilot.json's
gsd-session.json hash changes.

UPGRADE 2 (background dispatch): surfaced via the negotiated contract only —
dispatch.background:true exceeds the declarative-cli baseline and survives negotiation
with no downgrade warning. NO .agent.md frontmatter field (copilot has none). MCP
companion out of scope (AC4 names only 2 upgrades).

Tests: declarative-reference-copilot (adapter/axes/fail-closed + AC2 4-file source-grep
guard) + copilot-upgrades (live 5-event hook wiring; dispatch.background negotiation).
Matrix EoS note + how-to; changeset (Changed). capability-registry regenerated.

Incidental flaky-test RE-ARCHITECTURE (no-defer, maintainer-directed):
tests/opencode-review-reconstruction.property.test.cjs spawned ~600 synchronous
execFileSync('jq') subprocesses (numRuns:200 × 3 fast-check properties, one jq per
generated stream); a single jq freezing on a contended macos-22 CI runner hung the whole
unit-test chunk to its 600s kill (this PR's CI). --test-force-exit can't interrupt a
synchronous execFileSync, so the cure is to stop spawning per case, not just time-bound
it. Re-architected to run the SHIPPED jq program over the whole fast-check corpus in ONE
jq process: each generated stream is one compact-JSON array per line in a temp file,
`jq -c <PROGRAM>` (no -s) applies PROGRAM to each array (`.` == the array, exactly what
production's `jq -rs <file>` sees after slurping) and emits one result per line —
empirically byte-identical to the per-stream form across embedded-newline/empty/quote/
unicode/null-drop cases, and file-input (like production) so there's no stdin pipe to
deadlock on large I/O. ~600 spawns → 6; coverage unchanged (200-case corpus per property,
deterministic seeds) plus explicit boundary/diagnostic example batches. Still property-
tests the real shipped jq (no JS reimplementation). Per-call jq timeout retained as a
belt-and-suspenders bound.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 13:10:31 -04:00

165 lines
8.7 KiB
JavaScript

// allow-test-rule: structural-regression-guard — AC2: assert no `runtime === 'copilot'` string-equality branch, no live `isCopilot` read remains in bin/install.js, src/install-engine.cts, src/surface.cts, or src/runtime-artifact-conversion.cts — a source-text property, so source-grep is the faithful check (#2099, expanded #2103)
'use strict';
/**
* Declarative reference host — GitHub Copilot (#2099 / ADR-1239 EoS).
*
* Copilot already installs through the descriptor-driven artifactLayout
* (skills/agents, each with a named `converter`), and its capability.json
* already declares `hostIntegration` + `dispatch` axes. Issue #2099's
* literal premises about a "legacy exclusion list" / writesSharedSettings
* mismatch and a hardcoded RUNTIME_CONTENT_DISPATCH branch were WRONG (see
* the deep-research writeup on the issue) — this migration instead folds
* the real residual `isCopilot` branches:
* 1. src/install-engine.cts's `.agent.md` destination-suffix rename —
* folded onto `hostBehaviors.agentFileExtension`.
* 2. bin/install.js uninstall's two Copilot side-effect branches
* (repo-root AGENTS.md cleanup + copilot-instructions.md/hook
* cleanup) — folded onto `resolveInstallPlan(runtime).installSurface
* === 'copilot-instructions'` (unique to copilot, so byte-parity).
* 3. bin/install.js's two `skipSharedHooksInstall` checks — folded onto
* `hostBehaviors.skipSharedHooksInstall:true` (copilot's golden has
* only hooks/gsd-session.json, no shared gsd-*.js scripts).
* 4. bin/install.js's dead legacy agent-converter dispatch arm in the
* inline agent-copy loop — unreachable because copilot is a member of
* `_DESCRIPTOR_AGENTS_RUNTIMES` (installRuntimeArtifacts already wrote
* the agents before that loop runs) — deleted outright.
*
* This test is the reference-host dogfood mirroring
* tests/declarative-reference-codebuddy.test.cjs: it (1) classifies
* Copilot's profile via profileOf, (2) confirms the public declarative
* adapter classifies it as declarative, (3) round-trips a real install
* proving a gsd agent/skill surface is emitted, (4) proves negotiation
* fails CLOSED on a corrupted descriptor, (5) proves the validator accepts
* the descriptor, and (6) source-greps the folded modules for the retired
* `isCopilot` branches (AC2).
*
* UPGRADE 1 (multi-event hook bus) + UPGRADE 2 (dispatch.background
* negotiation) live-install coverage is in tests/copilot-upgrades.test.cjs
* — not duplicated here.
*/
const { test, before } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { execFileSync } = require('node:child_process');
const {
profileOf,
negotiateHostCapabilities,
PROFILE_BASELINES,
UNDOCUMENTED,
} = require('../gsd-core/bin/lib/host-integration.cjs');
const { validateCapability } = require('../gsd-core/bin/lib/capability-validator.cjs');
const { createDeclarativeAdapter } = require('../gsd-core/bin/lib/adapter-declarative.cjs');
const { cleanup } = require('./helpers.cjs');
const { walk, runMinimalInstall, BUILD_SCRIPT } = require('./helpers/install-shared.cjs');
const DESC = path.join(__dirname, '..', 'capabilities', 'copilot', 'capability.json');
const COPILOT_CAP = JSON.parse(fs.readFileSync(DESC, 'utf8'));
const COPILOT_AXES = COPILOT_CAP.runtime.hostIntegration;
// hooks/dist is gitignored and built (mirrors golden-install-parity harness).
before(() => {
execFileSync(process.execPath, [BUILD_SCRIPT], { encoding: 'utf-8', stdio: 'pipe' });
});
test('Copilot classifies as the declarative-cli reference profile (profileOf)', () => {
const desc = JSON.parse(fs.readFileSync(DESC, 'utf8'));
const axes = desc.runtime.hostIntegration;
assert.ok(axes && axes.embeddingMode, 'copilot descriptor declares hostIntegration axes');
assert.equal(profileOf(axes), 'declarative-cli',
'Copilot is a Declarative-CLI host');
});
test('the public declarative adapter classifies Copilot as a declarative host', () => {
const adapter = createDeclarativeAdapter({ runtime: 'copilot' });
assert.equal(adapter.kind, 'declarative');
assert.equal(adapter.runtime, 'copilot');
assert.equal(typeof adapter.install, 'function');
assert.equal(typeof adapter.uninstall, 'function');
});
test('a real Copilot install emits a gsd agent/skill surface (invocable)', () => {
const { configDir, root } = runMinimalInstall({ runtime: 'copilot', scope: 'global' });
try {
const files = walk(configDir);
assert.ok(files.length > 0, 'install must emit artifacts');
const gsdSurface = files.filter((f) => /gsd/i.test(path.relative(configDir, f)));
assert.ok(gsdSurface.length > 0,
'install must emit a gsd agent/skill surface (declarative reference)');
const agentsDir = path.join(configDir, 'agents');
assert.ok(fs.existsSync(agentsDir), 'agents/ directory must exist');
const agentFiles = fs.readdirSync(agentsDir)
.filter((f) => f.startsWith('gsd-') && f.endsWith('.agent.md'));
assert.ok(agentFiles.length > 0, 'agents/ must contain gsd-*.agent.md files');
const skillsDir = path.join(configDir, 'skills');
assert.ok(fs.existsSync(skillsDir), 'skills/ directory must exist');
} finally {
cleanup(root);
}
});
// ---------------------------------------------------------------------------
// #2099 EoS/copilot — fail-closed negotiation + validator acceptance +
// the folded descriptor (mirrors codebuddy/antigravity/qwen reference tests).
// ---------------------------------------------------------------------------
test('negotiateHostCapabilities never throws for copilot, even fully corrupted', () => {
assert.doesNotThrow(() => negotiateHostCapabilities({}));
assert.doesNotThrow(() => negotiateHostCapabilities({ ...COPILOT_AXES, embeddingMode: UNDOCUMENTED }));
assert.doesNotThrow(() => negotiateHostCapabilities({ ...COPILOT_AXES, embeddingMode: 'future-unknown' }));
assert.doesNotThrow(() => negotiateHostCapabilities({ ...COPILOT_AXES, dispatch: 'corrupted-not-an-object' }));
assert.doesNotThrow(() => negotiateHostCapabilities({ ...COPILOT_AXES, dispatch: { ...COPILOT_AXES.dispatch, maxDepth: 'not-a-number' } }));
});
test('a partial/empty copilot descriptor degrades to the safe floor, not the declarative-cli baseline', () => {
const result = negotiateHostCapabilities({});
assert.equal(result.effective.embeddingMode, 'declarative', 'omitted embeddingMode degrades closed');
assert.equal(result.effective.hookBus, 'none');
assert.notDeepEqual(result.effective, PROFILE_BASELINES['declarative-cli']);
assert.ok(result.warnings.length > 0);
});
test('capabilities/copilot/capability.json validates — no errors', () => {
const errors = validateCapability(COPILOT_CAP, 'copilot');
assert.deepEqual(errors, [], `validateCapability must return no errors, got: ${JSON.stringify(errors)}`);
});
// -- AC2: the hardcoded branches are retired across all folded modules ------
test('no `runtime === "copilot"` string-equality branch (nor live `isCopilot` read) remains in bin/install.js, src/install-engine.cts, src/surface.cts, or src/runtime-artifact-conversion.cts (AC2)', () => {
const strip = (src) => src
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/\/\/[^\r\n]*/g, '')
.replace(/`[^`]*`/g, '');
const repoRoot = path.join(__dirname, '..');
const files = [
path.join(repoRoot, 'bin', 'install.js'),
path.join(repoRoot, 'src', 'install-engine.cts'),
path.join(repoRoot, 'src', 'surface.cts'),
path.join(repoRoot, 'src', 'runtime-artifact-conversion.cts'),
];
for (const file of files) {
const src = fs.readFileSync(file, 'utf8');
const stripped = strip(src);
// NIT-1: catch both quote styles (single- and double-quoted 'copilot').
const eqOffenders = stripped.match(/runtime\s*[!=]==\s*["']copilot["']/g) || [];
assert.deepEqual(eqOffenders, [],
`AC2: no hardcoded runtime==='copilot' branch may remain in ${path.relative(repoRoot, file)}; found: ${eqOffenders.join(', ')}`);
// Excludes legit enumeration sites: --copilot CLI flag parsing, the
// '7':'copilot' numbered-menu map, allRuntimes/_DESCRIPTOR_AGENTS_RUNTIMES
// set literals, config-dir lists, the copilot conversion FUNCTION names
// (convertClaudeCommandToCopilotSkill / convertClaudeAgentToCopilotAgent /
// convertCopilotToolName), and installSurface==='copilot-instructions'
// (a descriptor-field string, not a runtime literal) — none of those
// contain the token `isCopilot`, so a literal-word match is precise here.
const isCopilotHits = stripped.match(/\bisCopilot\b/g) || [];
assert.deepEqual(isCopilotHits, [],
`AC2: no live isCopilot read may remain in ${path.relative(repoRoot, file)}; found ${isCopilotHits.length} occurrence(s)`);
}
});