diff --git a/README.md b/README.md index a1ebe1f..81476c9 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,12 @@ Run commands from this repository root: .\scripts\agent-harness.cmd compose --project kri-contact-front .\scripts\agent-harness.cmd compose --contract kri-common .\scripts\agent-harness.cmd register --project kri-contact-front --path _KriContact/contact-front +.\scripts\agent-harness.cmd register --project new-service --path ____Project/new-service --shared ``` The `.cmd` launcher invokes the PowerShell wrapper with a process-scoped execution-policy bypass. It does not change the user or machine execution policy. When local scripts are already allowed, `scripts\agent-harness.ps1` may be called directly. -`plan`, `verify`, and `list` are read-only. `compose` writes only generated `AGENTS.md` and `CLAUDE.md`. `register` writes only `workspace.local.yml`. No command performs Git, database, deployment, or network operations. +`plan`, `verify`, and `list` are read-only. `compose` writes only generated `AGENTS.md` and `CLAUDE.md`. `register` writes `workspace.local.yml` by default; `register --shared` writes `workspace.yml`. No command performs Git, database, deployment, or network operations. Use `--contract ` to select the registered provider first and then only registered consumers of that contract, ordered by project ID. Use `--all` only when every registered project is intentionally in scope. `--project`, `--contract`, and `--all` are mutually exclusive. diff --git a/dist/cli.cjs b/dist/cli.cjs index d479e3d..b677085 100644 --- a/dist/cli.cjs +++ b/dist/cli.cjs @@ -15108,7 +15108,7 @@ async function writeSingleFileAtomically(target, content) { fail(`Failed to update ${target}: ${detail}`); } } -async function registerProject(harnessRoot, projectId, projectPathInput) { +async function registerProject(harnessRoot, projectId, projectPathInput, scope = "local") { if (!SLUG_PATTERN.test(projectId)) { fail(`Invalid project id: ${projectId}`); } @@ -15125,7 +15125,12 @@ async function registerProject(harnessRoot, projectId, projectPathInput) { if (existing) { const existingPath = import_node_path.default.resolve(context.workspaceRoot, existing.path); if (comparablePath(existingPath) === comparablePath(candidate)) { - return { changed: false, path: projectPath }; + if (scope === "shared" && existing.source === "local") { + fail( + `Project ${projectId} is already registered locally. Remove it from workspace.local.yml before shared registration.` + ); + } + return { changed: false, path: projectPath, source: existing.source }; } fail(`Project id is already registered with another path: ${projectId}`); } @@ -15136,25 +15141,40 @@ async function registerProject(harnessRoot, projectId, projectPathInput) { if (profile.project.id !== projectId) { fail(`Profile project id does not match registration: ${profile.project.id} != ${projectId}`); } - const localPath = import_node_path.default.join(context.harnessRoot, "workspace.local.yml"); - let local = { schema_version: 1, projects: {} }; - if (await pathExists(localPath)) { - local = await loadWorkspaceDocument(localPath, context.validators); - if (local.workspace_root !== void 0) { - fail(`${localPath} must not override workspace_root.`); + const registryPath = import_node_path.default.join( + context.harnessRoot, + scope === "shared" ? "workspace.yml" : "workspace.local.yml" + ); + let registry = { schema_version: 1, projects: {} }; + if (await pathExists(registryPath)) { + registry = await loadWorkspaceDocument(registryPath, context.validators); + if (scope === "local" && registry.workspace_root !== void 0) { + fail(`${registryPath} must not override workspace_root.`); } } - if (local.projects[projectId]) { - fail(`Project id already exists in ${localPath}: ${projectId}`); + if (scope === "shared" && !registry.workspace_root) { + fail(`${registryPath} must declare workspace_root.`); } - local.projects[projectId] = { path: projectPath }; - const sortedProjects = Object.fromEntries( - Object.entries(local.projects).sort(([left], [right]) => left.localeCompare(right)) + if (registry.projects[projectId]) { + fail(`Project id already exists in ${registryPath}: ${projectId}`); + } + registry.projects[projectId] = { path: projectPath }; + const projects = scope === "local" ? Object.fromEntries( + Object.entries(registry.projects).sort(([left], [right]) => left.localeCompare(right)) + ) : registry.projects; + const document = scope === "shared" ? { + schema_version: 1, + workspace_root: registry.workspace_root, + projects + } : { schema_version: 1, projects }; + const normalized = normalizeNewlines((0, import_yaml.stringify)(document)); + const serialized = scope === "shared" ? normalized.replace("\nprojects:\n", "\n\nprojects:\n") : normalized; + await writeSingleFileAtomically( + registryPath, + serialized.endsWith("\n") ? serialized : `${serialized} +` ); - const serialized = normalizeNewlines((0, import_yaml.stringify)({ schema_version: 1, projects: sortedProjects })); - await writeSingleFileAtomically(localPath, serialized.endsWith("\n") ? serialized : `${serialized} -`); - return { changed: true, path: projectPath }; + return { changed: true, path: projectPath, source: scope }; } async function listProjects(harnessRoot) { const context = await loadHarnessContext(harnessRoot); @@ -15180,14 +15200,14 @@ Usage: agent-harness compose --project agent-harness compose --contract agent-harness compose --all - agent-harness register --project --path + agent-harness register --project --path [--shared] Commands: list List registered projects without modifying files. plan Resolve and validate inputs, then report proposed output state. verify Compare generated output byte for byte without modifying files. compose Write managed AGENTS.md and CLAUDE.md files. - register Add a project to workspace.local.yml. + register Add a project to workspace.local.yml, or workspace.yml with --shared. `; } function requireValue(args, index, option) { @@ -15199,11 +15219,12 @@ function requireValue(args, index, option) { } function parseArgs(args) { if (args[0] === "--help" || args[0] === "-h") { - return { command: void 0, all: false, help: true }; + return { command: void 0, all: false, shared: false, help: true }; } const options = { command: args[0], all: false, + shared: false, help: false }; for (let index = 1; index < args.length; index += 1) { @@ -15224,6 +15245,9 @@ function parseArgs(args) { case "--all": options.all = true; break; + case "--shared": + options.shared = true; + break; case "--help": case "-h": options.help = true; @@ -15282,6 +15306,9 @@ async function run() { return; } const harnessRoot = process.env.KRI_AGENT_HARNESS_ROOT ? import_node_path2.default.resolve(process.env.KRI_AGENT_HARNESS_ROOT) : import_node_path2.default.resolve(__dirname, ".."); + if (options.shared && options.command !== "register") { + throw new HarnessError("--shared is accepted only by register."); + } switch (options.command) { case "list": { if (options.project || options.contract || options.all || options.projectPath) { @@ -15353,9 +15380,14 @@ async function run() { if (!options.project || !options.projectPath) { throw new HarnessError("register requires --project and --path ."); } - const result = await registerProject(harnessRoot, options.project, options.projectPath); + const result = await registerProject( + harnessRoot, + options.project, + options.projectPath, + options.shared ? "shared" : "local" + ); console.log( - result.changed ? `registered: ${options.project} -> ${result.path}` : `registered: unchanged (${options.project} -> ${result.path})` + result.changed ? `registered (${result.source}): ${options.project} -> ${result.path}` : `registered: unchanged (${result.source}, ${options.project} -> ${result.path})` ); return; } diff --git a/dist/index.cjs b/dist/index.cjs index 3e0f648..ba938f1 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -15124,7 +15124,7 @@ async function writeSingleFileAtomically(target, content) { fail(`Failed to update ${target}: ${detail}`); } } -async function registerProject(harnessRoot, projectId, projectPathInput) { +async function registerProject(harnessRoot, projectId, projectPathInput, scope = "local") { if (!SLUG_PATTERN.test(projectId)) { fail(`Invalid project id: ${projectId}`); } @@ -15141,7 +15141,12 @@ async function registerProject(harnessRoot, projectId, projectPathInput) { if (existing) { const existingPath = import_node_path.default.resolve(context.workspaceRoot, existing.path); if (comparablePath(existingPath) === comparablePath(candidate)) { - return { changed: false, path: projectPath }; + if (scope === "shared" && existing.source === "local") { + fail( + `Project ${projectId} is already registered locally. Remove it from workspace.local.yml before shared registration.` + ); + } + return { changed: false, path: projectPath, source: existing.source }; } fail(`Project id is already registered with another path: ${projectId}`); } @@ -15152,25 +15157,40 @@ async function registerProject(harnessRoot, projectId, projectPathInput) { if (profile.project.id !== projectId) { fail(`Profile project id does not match registration: ${profile.project.id} != ${projectId}`); } - const localPath = import_node_path.default.join(context.harnessRoot, "workspace.local.yml"); - let local = { schema_version: 1, projects: {} }; - if (await pathExists(localPath)) { - local = await loadWorkspaceDocument(localPath, context.validators); - if (local.workspace_root !== void 0) { - fail(`${localPath} must not override workspace_root.`); + const registryPath = import_node_path.default.join( + context.harnessRoot, + scope === "shared" ? "workspace.yml" : "workspace.local.yml" + ); + let registry = { schema_version: 1, projects: {} }; + if (await pathExists(registryPath)) { + registry = await loadWorkspaceDocument(registryPath, context.validators); + if (scope === "local" && registry.workspace_root !== void 0) { + fail(`${registryPath} must not override workspace_root.`); } } - if (local.projects[projectId]) { - fail(`Project id already exists in ${localPath}: ${projectId}`); + if (scope === "shared" && !registry.workspace_root) { + fail(`${registryPath} must declare workspace_root.`); } - local.projects[projectId] = { path: projectPath }; - const sortedProjects = Object.fromEntries( - Object.entries(local.projects).sort(([left], [right]) => left.localeCompare(right)) + if (registry.projects[projectId]) { + fail(`Project id already exists in ${registryPath}: ${projectId}`); + } + registry.projects[projectId] = { path: projectPath }; + const projects = scope === "local" ? Object.fromEntries( + Object.entries(registry.projects).sort(([left], [right]) => left.localeCompare(right)) + ) : registry.projects; + const document = scope === "shared" ? { + schema_version: 1, + workspace_root: registry.workspace_root, + projects + } : { schema_version: 1, projects }; + const normalized = normalizeNewlines((0, import_yaml.stringify)(document)); + const serialized = scope === "shared" ? normalized.replace("\nprojects:\n", "\n\nprojects:\n") : normalized; + await writeSingleFileAtomically( + registryPath, + serialized.endsWith("\n") ? serialized : `${serialized} +` ); - const serialized = normalizeNewlines((0, import_yaml.stringify)({ schema_version: 1, projects: sortedProjects })); - await writeSingleFileAtomically(localPath, serialized.endsWith("\n") ? serialized : `${serialized} -`); - return { changed: true, path: projectPath }; + return { changed: true, path: projectPath, source: scope }; } async function listProjects(harnessRoot) { const context = await loadHarnessContext(harnessRoot); diff --git a/docs/composition.md b/docs/composition.md index e784869..2d0c297 100644 --- a/docs/composition.md +++ b/docs/composition.md @@ -130,7 +130,7 @@ Codex uses a 32 KiB default combined project-instruction limit. To leave room fo - `plan`: resolve, validate, compose in memory, and report fragments, size, warnings, and output state. - `verify`: compare expected and stored output byte for byte without modifying files; return a failure exit code for drift. - `compose`: atomically replace only managed or missing `AGENTS.md` and `CLAUDE.md` files, then verify them again. -- `register`: validate a relative project path and matching profile, then atomically update only `workspace.local.yml`. +- `register`: validate a relative project path and matching profile, then atomically update `workspace.local.yml` by default or `workspace.yml` when `--shared` is explicit. For `plan`, `verify`, and `compose`, select exactly one scope with `--project `, `--contract `, or `--all`. Contract scope processes the provider first and consumers in project-ID order. No command performs Git, database, deployment, paid API, or network operations. `--all` must be explicit; a single project is the normal scope. diff --git a/src/cli.ts b/src/cli.ts index 9bfc742..18e6e0d 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -16,6 +16,7 @@ interface CliOptions { contract?: string; projectPath?: string; all: boolean; + shared: boolean; help: boolean; } @@ -33,14 +34,14 @@ Usage: agent-harness compose --project agent-harness compose --contract agent-harness compose --all - agent-harness register --project --path + agent-harness register --project --path [--shared] Commands: list List registered projects without modifying files. plan Resolve and validate inputs, then report proposed output state. verify Compare generated output byte for byte without modifying files. compose Write managed AGENTS.md and CLAUDE.md files. - register Add a project to workspace.local.yml. + register Add a project to workspace.local.yml, or workspace.yml with --shared. `; } @@ -54,11 +55,12 @@ function requireValue(args: string[], index: number, option: string): string { function parseArgs(args: string[]): CliOptions { if (args[0] === "--help" || args[0] === "-h") { - return { command: undefined, all: false, help: true }; + return { command: undefined, all: false, shared: false, help: true }; } const options: CliOptions = { command: args[0], all: false, + shared: false, help: false, }; @@ -80,6 +82,9 @@ function parseArgs(args: string[]): CliOptions { case "--all": options.all = true; break; + case "--shared": + options.shared = true; + break; case "--help": case "-h": options.help = true; @@ -152,6 +157,10 @@ async function run(): Promise { ? path.resolve(process.env.KRI_AGENT_HARNESS_ROOT) : path.resolve(__dirname, ".."); + if (options.shared && options.command !== "register") { + throw new HarnessError("--shared is accepted only by register."); + } + switch (options.command) { case "list": { if (options.project || options.contract || options.all || options.projectPath) { @@ -229,11 +238,16 @@ async function run(): Promise { if (!options.project || !options.projectPath) { throw new HarnessError("register requires --project and --path ."); } - const result = await registerProject(harnessRoot, options.project, options.projectPath); + const result = await registerProject( + harnessRoot, + options.project, + options.projectPath, + options.shared ? "shared" : "local", + ); console.log( result.changed - ? `registered: ${options.project} -> ${result.path}` - : `registered: unchanged (${options.project} -> ${result.path})`, + ? `registered (${result.source}): ${options.project} -> ${result.path}` + : `registered: unchanged (${result.source}, ${options.project} -> ${result.path})`, ); return; } diff --git a/src/harness.ts b/src/harness.ts index 5d192e6..59a4513 100644 --- a/src/harness.ts +++ b/src/harness.ts @@ -42,6 +42,8 @@ export type ProjectType = export type ProjectLayer = "frontend" | "backend" | "fullstack" | "standalone"; +export type RegistrationScope = "local" | "shared"; + export interface ProjectProfile { schema_version: 1; project: { @@ -880,7 +882,8 @@ export async function registerProject( harnessRoot: string, projectId: string, projectPathInput: string, -): Promise<{ changed: boolean; path: string }> { + scope: RegistrationScope = "local", +): Promise<{ changed: boolean; path: string; source: RegistrationScope }> { if (!SLUG_PATTERN.test(projectId)) { fail(`Invalid project id: ${projectId}`); } @@ -898,7 +901,12 @@ export async function registerProject( if (existing) { const existingPath = path.resolve(context.workspaceRoot, existing.path); if (comparablePath(existingPath) === comparablePath(candidate)) { - return { changed: false, path: projectPath }; + if (scope === "shared" && existing.source === "local") { + fail( + `Project ${projectId} is already registered locally. Remove it from workspace.local.yml before shared registration.`, + ); + } + return { changed: false, path: projectPath, source: existing.source }; } fail(`Project id is already registered with another path: ${projectId}`); } @@ -911,24 +919,46 @@ export async function registerProject( fail(`Profile project id does not match registration: ${profile.project.id} != ${projectId}`); } - const localPath = path.join(context.harnessRoot, "workspace.local.yml"); - let local: WorkspaceDocument = { schema_version: 1, projects: {} }; - if (await pathExists(localPath)) { - local = await loadWorkspaceDocument(localPath, context.validators); - if (local.workspace_root !== undefined) { - fail(`${localPath} must not override workspace_root.`); + const registryPath = path.join( + context.harnessRoot, + scope === "shared" ? "workspace.yml" : "workspace.local.yml", + ); + let registry: WorkspaceDocument = { schema_version: 1, projects: {} }; + if (await pathExists(registryPath)) { + registry = await loadWorkspaceDocument(registryPath, context.validators); + if (scope === "local" && registry.workspace_root !== undefined) { + fail(`${registryPath} must not override workspace_root.`); } } - if (local.projects[projectId]) { - fail(`Project id already exists in ${localPath}: ${projectId}`); + if (scope === "shared" && !registry.workspace_root) { + fail(`${registryPath} must declare workspace_root.`); } - local.projects[projectId] = { path: projectPath }; - const sortedProjects = Object.fromEntries( - Object.entries(local.projects).sort(([left], [right]) => left.localeCompare(right)), + if (registry.projects[projectId]) { + fail(`Project id already exists in ${registryPath}: ${projectId}`); + } + + registry.projects[projectId] = { path: projectPath }; + const projects = scope === "local" + ? Object.fromEntries( + Object.entries(registry.projects).sort(([left], [right]) => left.localeCompare(right)), + ) + : registry.projects; + const document = scope === "shared" + ? { + schema_version: 1 as const, + workspace_root: registry.workspace_root!, + projects, + } + : { schema_version: 1 as const, projects }; + const normalized = normalizeNewlines(stringifyYaml(document)); + const serialized = scope === "shared" + ? normalized.replace("\nprojects:\n", "\n\nprojects:\n") + : normalized; + await writeSingleFileAtomically( + registryPath, + serialized.endsWith("\n") ? serialized : `${serialized}\n`, ); - const serialized = normalizeNewlines(stringifyYaml({ schema_version: 1, projects: sortedProjects })); - await writeSingleFileAtomically(localPath, serialized.endsWith("\n") ? serialized : `${serialized}\n`); - return { changed: true, path: projectPath }; + return { changed: true, path: projectPath, source: scope }; } export async function listProjects(harnessRoot: string): Promise> { diff --git a/tests/harness.test.mjs b/tests/harness.test.mjs index b0e8247..478f2e6 100644 --- a/tests/harness.test.mjs +++ b/tests/harness.test.mjs @@ -11,6 +11,7 @@ import { promisify } from "node:util"; const require = createRequire(import.meta.url); const { composeProject, + listProjects, planProject, registerProject, selectProjectIdsByContract, @@ -270,6 +271,49 @@ stack: ); }); +test("registers a project in the shared workspace only when explicitly requested", async (t) => { + const { temporaryRoot, harnessRoot } = await createFixture(t); + const newProjectRoot = path.join(temporaryRoot, "apps", "shared-tool"); + await writeText( + path.join(newProjectRoot, ".agent", "profile.yml"), + `schema_version: 1 +project: + id: shared-tool + name: Shared Tool + type: scaffold-cli + layer: standalone + legacy: false +stack: + - typescript +`, + ); + + const result = await registerProject( + harnessRoot, + "shared-tool", + "apps/shared-tool", + "shared", + ); + assert.deepEqual(result, { + changed: true, + path: "apps/shared-tool", + source: "shared", + }); + + const sharedWorkspace = await readFile(path.join(harnessRoot, "workspace.yml"), "utf8"); + assert.match(sharedWorkspace, /workspace_root: \.\.\n\nprojects:/); + assert.match(sharedWorkspace, /shared-tool:/); + assert.match(sharedWorkspace, /path: apps\/shared-tool/); + assert.equal( + await readFile(path.join(harnessRoot, "workspace.local.yml"), "utf8").catch(() => null), + null, + ); + assert.equal( + (await listProjects(harnessRoot)).find((project) => project.id === "shared-tool")?.source, + "shared", + ); +}); + test("registers, composes, and verifies a project path containing Korean text and spaces", async (t) => { const { temporaryRoot, harnessRoot } = await createFixture(t); const projectRoot = path.join(temporaryRoot, "___NEW 출구조사", "legacy-app");