Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-1396-export-external-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bradygaster/squad-cli": patch
---

Fix #1396: `squad export` now resolves externalized state. After `squad externalize`, export read the local `.squad/` directory directly, so it either failed with a misleading "No squad found — run init first" or silently exported stale/scaffolded local files instead of the real team state in the external directory. Export now routes through the same `effectiveSquadDir()` resolution used by `build`, `loop`, `plugin`, `watch`, and `doctor`, reading `team.md`, `decisions.md`, `routing.md`, `casting/`, `agents/`, and `.squad`-local skills from the effective state directory.
11 changes: 6 additions & 5 deletions packages/squad-cli/src/cli/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import path from 'node:path';
import { FSStorageProvider, exportToRepo, parseRepoString } from '@bradygaster/squad-sdk';
import type { RepoSpec } from '@bradygaster/squad-sdk';
import { detectSquadDir } from '../core/detect-squad-dir.js';
import { effectiveSquadDir } from '../core/effective-squad-dir.js';
import { success, warn, info } from '../core/output.js';
import { fatal } from '../core/errors.js';
import { getPackageVersion } from '../core/version.js';
Expand All @@ -32,7 +32,8 @@ export interface ExportRepoOptions {
}

/**
* Build the export manifest from a local squad directory.
* Build the export manifest from the effective squad state directory
* (the external state dir when state is externalized, otherwise local .squad/).
*/
function buildManifest(dest: string, storage: FSStorageProvider, squadInfo: { path: string }): ExportManifest {
const manifest: ExportManifest = {
Expand Down Expand Up @@ -129,14 +130,14 @@ function buildManifest(dest: string, storage: FSStorageProvider, squadInfo: { pa
*/
export async function runExport(dest: string, outPath?: string, repoOptions?: ExportRepoOptions): Promise<void> {
const storage = new FSStorageProvider();
const squadInfo = detectSquadDir(dest);
const teamMd = path.join(squadInfo.path, 'team.md');
const { stateDir } = effectiveSquadDir(dest);
const teamMd = path.join(stateDir, 'team.md');

if (!storage.existsSync(teamMd)) {
fatal('No squad found — run init first');
}

const manifest = buildManifest(dest, storage, squadInfo);
const manifest = buildManifest(dest, storage, { path: stateDir });
const bundleJson = JSON.stringify(manifest, null, 2) + '\n';

// Export to GitHub repo if --repo is specified
Expand Down
75 changes: 75 additions & 0 deletions test/cli/export-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { runInit } from '@bradygaster/squad-cli/core/init';
import { runExport } from '@bradygaster/squad-cli/commands/export';
import { runImport } from '@bradygaster/squad-cli/commands/import';

const EXT_ROOT = join(tmpdir(), `.test-cli-export-ext-${randomBytes(4).toString('hex')}`);
const EXT_GLOBAL = join(tmpdir(), `.test-cli-export-ext-global-${randomBytes(4).toString('hex')}`);
const EXT_PROJECT_KEY = `test-export-ext-${randomBytes(4).toString('hex')}`;

const TEST_ROOT = join(tmpdir(), `.test-cli-export-import-${randomBytes(4).toString('hex')}`);
const IMPORT_ROOT = join(tmpdir(), `.test-cli-import-target-${randomBytes(4).toString('hex')}`);

Expand Down Expand Up @@ -347,3 +351,74 @@ describe('CLI: export/import commands', () => {
expect(existsSync(join(IMPORT_ROOT, '.squad', 'routing.md'))).toBe(false);
});
});

describe('CLI: export with externalized state (#1396)', () => {
const origAppData = process.env['APPDATA'];
const origXdgConfig = process.env['XDG_CONFIG_HOME'];
const externalStateDir = join(EXT_GLOBAL, 'squad', 'projects', EXT_PROJECT_KEY);

beforeEach(async () => {
if (existsSync(EXT_ROOT)) {
await rm(EXT_ROOT, { recursive: true, force: true });
}
if (existsSync(EXT_GLOBAL)) {
await rm(EXT_GLOBAL, { recursive: true, force: true });
}

// Point resolveGlobalSquadPath() inside EXT_GLOBAL (not the real user dir)
if (process.platform === 'win32') {
process.env['APPDATA'] = EXT_GLOBAL;
} else {
process.env['XDG_CONFIG_HOME'] = EXT_GLOBAL;
}

// Local repo: thin .squad/ holding only the marker `squad externalize` leaves behind
await mkdir(join(EXT_ROOT, '.squad'), { recursive: true });
await writeFile(
join(EXT_ROOT, '.squad', 'config.json'),
JSON.stringify({ version: 1, teamRoot: '.', projectKey: EXT_PROJECT_KEY, stateLocation: 'external' }, null, 2)
);

// External state dir: the real team state
await mkdir(join(externalStateDir, 'agents', 'alice'), { recursive: true });
await writeFile(join(externalStateDir, 'team.md'), '# External Team\n');
await writeFile(join(externalStateDir, 'decisions.md'), '# External Decisions\n');
await writeFile(join(externalStateDir, 'agents', 'alice', 'charter.md'), '# Alice Charter\n');
});

afterEach(async () => {
if (origAppData === undefined) delete process.env['APPDATA'];
else process.env['APPDATA'] = origAppData;
if (origXdgConfig === undefined) delete process.env['XDG_CONFIG_HOME'];
else process.env['XDG_CONFIG_HOME'] = origXdgConfig;

if (existsSync(EXT_ROOT)) {
await rm(EXT_ROOT, { recursive: true, force: true });
}
if (existsSync(EXT_GLOBAL)) {
await rm(EXT_GLOBAL, { recursive: true, force: true });
}
});

it('exports team state from the external state dir', async () => {
await runExport(EXT_ROOT);

const exportPath = join(EXT_ROOT, 'squad-export.json');
expect(existsSync(exportPath)).toBe(true);

const manifest = JSON.parse(await readFile(exportPath, 'utf-8'));
expect(manifest.team_md).toBe('# External Team\n');
expect(manifest.decisions_md).toBe('# External Decisions\n');
expect(manifest.agents['alice']?.charter).toBe('# Alice Charter\n');
});

it('prefers external state over stale local files when the marker is set', async () => {
// Simulate local scaffolding re-created after externalize (e.g. a re-run init)
await writeFile(join(EXT_ROOT, '.squad', 'team.md'), '# Stale Local Scaffold\n');

await runExport(EXT_ROOT);

const manifest = JSON.parse(await readFile(join(EXT_ROOT, 'squad-export.json'), 'utf-8'));
expect(manifest.team_md).toBe('# External Team\n');
});
});