Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

ci: reject protocol submodule rollback (#1751)

Chris Zhang7fcd81e24c5e
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 29e6e19fb6..96164b68c4 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -63,6 +63,12 @@ jobs:
git submodule sync -- cindy-protocol
git submodule update --init --force --recursive -- cindy-protocol
+ - name: Reject cindy-protocol rollback or divergence
+ if: ${{ github.event_name == 'pull_request' }}
+ env:
+ CINDY_PROTOCOL_BASE_REF: ${{ github.event.pull_request.base.sha }}
+ run: node scripts/check-submodule-forward.mjs
+
- name: Setup pnpm
uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6
with:
diff --git a/package.json b/package.json
index d11b862bbf..da373f2a6d 100644
--- a/package.json
+++ b/package.json
@@ -111,11 +111,12 @@
"check:endpoints": "node scripts/check-endpoint-literals.mjs",
"check:dev-docs": "node --test scripts/__tests__/dev-docs-contract.test.mjs",
"check:dco": "node scripts/check-dco.mjs",
+ "check:submodule-forward": "node scripts/check-submodule-forward.mjs",
"dco:install-hook": "node scripts/install-dco-hook.mjs",
"licenses:generate": "node scripts/generate-third-party-notices.mjs",
"lint": "pnpm -r lint",
"format": "pnpm -r format",
- "test:runner": "node --test scripts/__tests__/test-workspaces.test.mjs scripts/__tests__/desktop-worker-benchmark.test.mjs scripts/__tests__/dev-docs-contract.test.mjs scripts/__tests__/restart-desktop-remote.test.mjs scripts/__tests__/dev-migration-policy.test.mjs scripts/__tests__/ensure-deps.test.mjs scripts/__tests__/client-endpoint-build-env.test.mjs scripts/__tests__/desktop-dev-region.test.mjs scripts/__tests__/endpoint-consistency.test.mjs scripts/__tests__/endpoint-local-file.test.mjs scripts/__tests__/ensure-agent-binaries.test.mjs scripts/__tests__/ensure-android-platform-tools.test.mjs scripts/__tests__/fetch-with-timeout.test.mjs scripts/__tests__/agent-binary-cdn-fallback.test.mjs scripts/__tests__/ensure-binary-fallback.test.mjs scripts/__tests__/verify-sha256.test.mjs scripts/__tests__/pi-update-layout.test.mjs scripts/__tests__/migration-freeze.test.mjs scripts/__tests__/third-party-notices.test.mjs scripts/__tests__/brand-identity-sync.test.mjs scripts/__tests__/check-dco.test.mjs scripts/__tests__/package-args.test.mjs scripts/__tests__/notarize-mac-app.test.mjs scripts/__tests__/glossary-rules.test.mjs",
+ "test:runner": "node --test scripts/__tests__/test-workspaces.test.mjs scripts/__tests__/desktop-worker-benchmark.test.mjs scripts/__tests__/dev-docs-contract.test.mjs scripts/__tests__/restart-desktop-remote.test.mjs scripts/__tests__/dev-migration-policy.test.mjs scripts/__tests__/ensure-deps.test.mjs scripts/__tests__/client-endpoint-build-env.test.mjs scripts/__tests__/desktop-dev-region.test.mjs scripts/__tests__/endpoint-consistency.test.mjs scripts/__tests__/endpoint-local-file.test.mjs scripts/__tests__/ensure-agent-binaries.test.mjs scripts/__tests__/ensure-android-platform-tools.test.mjs scripts/__tests__/fetch-with-timeout.test.mjs scripts/__tests__/agent-binary-cdn-fallback.test.mjs scripts/__tests__/ensure-binary-fallback.test.mjs scripts/__tests__/verify-sha256.test.mjs scripts/__tests__/pi-update-layout.test.mjs scripts/__tests__/migration-freeze.test.mjs scripts/__tests__/check-submodule-forward.test.mjs scripts/__tests__/third-party-notices.test.mjs scripts/__tests__/brand-identity-sync.test.mjs scripts/__tests__/check-dco.test.mjs scripts/__tests__/package-args.test.mjs scripts/__tests__/notarize-mac-app.test.mjs scripts/__tests__/glossary-rules.test.mjs",
"test:unit": "pnpm test:runner && node scripts/test-workspaces.mjs --tier unit",
"test:all": "pnpm test:runner && node scripts/test-workspaces.mjs --all",
"test:git-integration": "pnpm test:runner && node scripts/test-workspaces.mjs --tier git-integration",
diff --git a/scripts/__tests__/check-submodule-forward.test.mjs b/scripts/__tests__/check-submodule-forward.test.mjs
new file mode 100644
index 0000000000..1be7546768
--- /dev/null
+++ b/scripts/__tests__/check-submodule-forward.test.mjs
@@ -0,0 +1,44 @@
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { execFileSync } from 'node:child_process';
+import test from 'node:test';
+
+import { classifyProtocolRelation } from '../check-submodule-forward.mjs';
+
+function git(repo, ...args) {
+ return execFileSync('git', args, { cwd: repo, encoding: 'utf-8' }).trim();
+}
+
+function fixture() {
+ const repo = fs.mkdtempSync(path.join(os.tmpdir(), 'cindy-protocol-forward-'));
+ git(repo, 'init', '-b', 'main');
+ git(repo, 'config', 'user.name', 'Protocol Guard Test');
+ git(repo, 'config', 'user.email', '[email protected]');
+ fs.writeFileSync(path.join(repo, 'protocol.txt'), 'one\n');
+ git(repo, 'add', '.');
+ git(repo, 'commit', '-m', 'one');
+ const one = git(repo, 'rev-parse', 'HEAD');
+ fs.writeFileSync(path.join(repo, 'protocol.txt'), 'two\n');
+ git(repo, 'commit', '-am', 'two');
+ const two = git(repo, 'rev-parse', 'HEAD');
+ git(repo, 'checkout', '-b', 'fork', one);
+ fs.writeFileSync(path.join(repo, 'fork.txt'), 'fork\n');
+ git(repo, 'add', '.');
+ git(repo, 'commit', '-m', 'fork');
+ const fork = git(repo, 'rev-parse', 'HEAD');
+ return { repo, one, two, fork };
+}
+
+test('classifies unchanged, forward, rollback and diverged protocol gitlinks', () => {
+ const f = fixture();
+ try {
+ assert.equal(classifyProtocolRelation(f.repo, f.one, f.one), 'unchanged');
+ assert.equal(classifyProtocolRelation(f.repo, f.one, f.two), 'forward');
+ assert.equal(classifyProtocolRelation(f.repo, f.two, f.one), 'rollback');
+ assert.equal(classifyProtocolRelation(f.repo, f.two, f.fork), 'diverged');
+ } finally {
+ fs.rmSync(f.repo, { recursive: true, force: true });
+ }
+});
diff --git a/scripts/check-submodule-forward.mjs b/scripts/check-submodule-forward.mjs
new file mode 100644
index 0000000000..9c3b345e39
--- /dev/null
+++ b/scripts/check-submodule-forward.mjs
@@ -0,0 +1,85 @@
+#!/usr/bin/env node
+
+import path from 'node:path';
+import { spawnSync } from 'node:child_process';
+import { fileURLToPath } from 'node:url';
+
+const SUBMODULE_PATH = 'cindy-protocol';
+
+function git(cwd, args, { allowFailure = false } = {}) {
+ const result = spawnSync('git', args, {
+ cwd,
+ encoding: 'utf-8',
+ stdio: ['ignore', 'pipe', 'pipe'],
+ });
+ if (!allowFailure && result.status !== 0) {
+ const detail = (result.stderr || result.stdout || '').trim();
+ throw new Error(`git ${args.join(' ')} failed${detail ? `: ${detail}` : ''}`);
+ }
+ return result;
+}
+
+export function readGitlink(repoRoot, ref, submodulePath = SUBMODULE_PATH) {
+ const result = git(repoRoot, ['ls-tree', ref, '--', submodulePath]);
+ const match = result.stdout.trim().match(/^160000 commit ([0-9a-f]{40})\t(.+)$/i);
+ if (!match || match[2] !== submodulePath) {
+ throw new Error(`${ref} 中缺少合法的 ${submodulePath} gitlink`);
+ }
+ return match[1].toLowerCase();
+}
+
+function isAncestor(repoRoot, older, newer) {
+ const result = git(repoRoot, ['merge-base', '--is-ancestor', older, newer], {
+ allowFailure: true,
+ });
+ if (result.status === 0) return true;
+ if (result.status === 1) return false;
+ const detail = (result.stderr || result.stdout || '').trim();
+ throw new Error(`无法比较协议历史 ${older.slice(0, 10)}..${newer.slice(0, 10)}${detail ? `: ${detail}` : ''}`);
+}
+
+export function classifyProtocolRelation(protocolRepo, baseOid, headOid) {
+ if (baseOid === headOid) return 'unchanged';
+ if (isAncestor(protocolRepo, baseOid, headOid)) return 'forward';
+ if (isAncestor(protocolRepo, headOid, baseOid)) return 'rollback';
+ return 'diverged';
+}
+
+function ensureCommit(protocolRepo, oid) {
+ if (git(protocolRepo, ['cat-file', '-e', `${oid}^{commit}`], { allowFailure: true }).status === 0) {
+ return;
+ }
+ git(protocolRepo, ['fetch', '--no-tags', 'origin', oid]);
+}
+
+export function validateSubmoduleForward(repoRoot, baseRef, headRef = 'HEAD') {
+ const baseOid = readGitlink(repoRoot, baseRef);
+ const headOid = readGitlink(repoRoot, headRef);
+ const protocolRepo = path.join(repoRoot, SUBMODULE_PATH);
+ ensureCommit(protocolRepo, baseOid);
+ ensureCommit(protocolRepo, headOid);
+ const relation = classifyProtocolRelation(protocolRepo, baseOid, headOid);
+ return { baseRef, headRef, baseOid, headOid, relation };
+}
+
+function main() {
+ const repoRoot = process.cwd();
+ const baseRef = process.env.CINDY_PROTOCOL_BASE_REF || 'origin/main';
+ const result = validateSubmoduleForward(repoRoot, baseRef);
+ const summary = `${result.baseOid.slice(0, 10)} -> ${result.headOid.slice(0, 10)}`;
+ if (result.relation === 'rollback') {
+ console.error(`::error file=cindy-protocol::cindy-protocol gitlink 回退: ${summary}`);
+ process.exitCode = 1;
+ return;
+ }
+ if (result.relation === 'diverged') {
+ console.error(`::error file=cindy-protocol::cindy-protocol gitlink 与 base 分叉: ${summary}`);
+ process.exitCode = 1;
+ return;
+ }
+ console.log(`cindy-protocol gitlink ${result.relation === 'forward' ? '前进' : '未变化'}: ${summary}`);
+}
+
+if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
+ main();
+}