diff --git a/api/cli.api.txt b/api/cli.api.txt index 3bc3ff4d2..cb69b7a01 100644 --- a/api/cli.api.txt +++ b/api/cli.api.txt @@ -23,6 +23,7 @@ AccountRemoveOptions · interface · export interface AccountRemoveOptions { AccountRemoveOptions · interface · provider?: Provider; AccountRemoveOptions · interface · resolveSettings?: (flags: GlobalFlags, options?: ResolveOptions) => Settings; AccountRemoveOptions · interface · ownerHome?: string; +AccountRemoveOptions · interface · effects?: Effects; AccountRemoveOptions · interface · } AccountSecrets · interface · export interface AccountSecrets { AccountSecrets · interface · credentials: CredentialsOptions; diff --git a/changes/feat-mark-shared-session-commands.md b/changes/feat-mark-shared-session-commands.md new file mode 100644 index 000000000..e3c6c8f95 --- /dev/null +++ b/changes/feat-mark-shared-session-commands.md @@ -0,0 +1,10 @@ +### Added + +- **`tokenSessionCommand`, `oauthSessionCommand` and `fileMirrorsCommand` are marked as cli-core's own commands**, + as `appDoctorCommand` is. `commands --json` gives them `origin: "cli-core"`, so the `reserved-shared-command` lint + rule (WC-CMD-34) stops warning on the CLIs that mount them. A CLI changes them only through their options: an + option, a description or an annotation added to the returned command afterwards stops the run with + `contract.unacknowledged-override`. No CLI does that today. +- **`accountRemoveCommand` takes `effects`.** A CLI whose removal reaches no service passes `LOCAL_WRITE` here, + instead of annotating the returned command. asterisk, beeline, notion and yandex-disk annotate it today, so + `accountRemoveCommand` stays unmarked until they pass `effects`. diff --git a/src/cli/account-remove.ts b/src/cli/account-remove.ts index c6ede5c12..bb627764f 100644 --- a/src/cli/account-remove.ts +++ b/src/cli/account-remove.ts @@ -11,6 +11,7 @@ import { saveConfigFile, } from "@wirecat/cli-base" import { annotate } from "@wirecat/cli-base/commands" +import type { Effects } from "@wirecat/cli-base/standards" import { Command } from "commander" import { lockPath, readLock, servingProfiles } from "../background/lock.js" import { alive } from "../background/processes.js" @@ -105,6 +106,8 @@ export interface AccountRemoveOptions { resolveSettings?: (flags: GlobalFlags, options?: ResolveOptions) => Settings /** The owner's own home; unset, the account's from the user database. A test names its sandbox. */ ownerHome?: string + /** A CLI whose removal reaches no service says so here (`LOCAL_WRITE`), not by annotating the command after. */ + effects?: Effects } type Kind = "sign_in" | "keyring" | "config" | "file" | "store" | "unit" | "schedule" @@ -243,7 +246,7 @@ export const accountRemoveCommand = ( ): Command => logsInOrOut( annotate(new Command("remove"), { - effects: REMOTE_WRITE, + effects: options.effects ?? REMOTE_WRITE, mcp: false, output: { entity: "account_removal" }, examples: [ diff --git a/src/cli/documents/file-mirrors.test.ts b/src/cli/documents/file-mirrors.test.ts index ad236e4ad..9d3b09ec0 100644 --- a/src/cli/documents/file-mirrors.test.ts +++ b/src/cli/documents/file-mirrors.test.ts @@ -9,7 +9,9 @@ import type { Expect, FileTarget, RemoteFile } from "../../documents.js" import type { KeyedGuard } from "../../sends/keyed-guard.js" import { outputFor, provide } from "../context.js" import { type Body, renderAnswer } from "../envelope.js" -import { type FileMirrorWrite, fileMirrorsCommand } from "./file-mirrors.js" +import { hasSharedContract } from "../shared-contracts.js" +import type { FileMirrorWrite } from "./file-mirrors.js" +import { fileMirrorsCommand } from "./index.js" const FOLDER = "/Notes" const sha = (text: string | Buffer) => createHash("sha256").update(text).digest("hex") @@ -132,10 +134,16 @@ const setup = (rewrite?: (text: string) => string, env?: NodeJS.ProcessEnv) => { return bodies.at(-1) as Body & { item?: Record; items?: Record[] } } const add = async () => (await run("add", FOLDER, local)).item?.id as string - return { home, state, local, remote, writes, runs, run, add, streams } + return { home, state, local, remote, writes, runs, run, add, streams, program } } describe("fileMirrorsCommand", () => { + it("is cli-core's own mirrors group", () => { + const { program } = setup() + const mirrors = program.commands.find((command) => command.name() === "mirrors") + expect(mirrors && hasSharedContract(mirrors)).toBe(true) + }) + it("marks the files a preview names for a scheduled agent, and leaves the mirror's own folder as it is", async () => { const preview = async (env: NodeJS.ProcessEnv) => { const { local, remote, run, add, streams } = setup(undefined, env) diff --git a/src/cli/documents/index.ts b/src/cli/documents/index.ts index c8cc5c7d8..ff69bdede 100644 --- a/src/cli/documents/index.ts +++ b/src/cli/documents/index.ts @@ -1,6 +1,9 @@ import type { Command } from "commander" +import { honoursDryRun } from "../dry-run-guard.js" +import { sharedCommand } from "../shared-contracts.js" import { addDocumentCatalogCommands, type DocumentCatalogCommands } from "./catalog.js" import { addLocalDocumentCommands, type LocalDocumentCommands } from "./documents.js" +import { fileMirrorsCommand as fileMirrorsCommandDefinition } from "./file-mirrors.js" import { addDocumentFolderCommands, type DocumentFolderCommands } from "./folders.js" import { addDocumentHistoryCommands, type DocumentHistoryCommands } from "./history.js" import { addDocumentKnowledgeCommands, type DocumentKnowledgeCommands } from "./knowledge.js" @@ -33,12 +36,7 @@ export { export { addLocalDocumentCommands, type ImportedDocument, type LocalDocumentCommands } from "./documents.js" export { type FileConflictCommands, type FileConflictConnection, fileConflictsCommand } from "./file-conflicts.js" export { addFileEditCommands, type FileEditCommands } from "./file-edit.js" -export { - type FileMirrorCommands, - type FileMirrorRemote, - type FileMirrorWrite, - fileMirrorsCommand, -} from "./file-mirrors.js" +export type { FileMirrorCommands, FileMirrorRemote, FileMirrorWrite } from "./file-mirrors.js" export { addFileUpdateCommand, type FileUpdateCommands, type FileUpdatePort } from "./file-update.js" export { addDocumentFolderCommands, type DocumentFolderCommands } from "./folders.js" export { @@ -133,3 +131,5 @@ export { documentSourceFromManifest, type ManifestDocumentSource, } from "./manifest/source.js" + +export const fileMirrorsCommand = honoursDryRun(sharedCommand(fileMirrorsCommandDefinition)) diff --git a/src/cli/index.ts b/src/cli/index.ts index 50664fc90..c7e8a1850 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -615,7 +615,6 @@ export { type OAuthSignedIn, oauthFlowOf, oauthPreset, - oauthSessionCommand, } from "./oauth-session.js" export { pagingOf, positiveCount, window, withPaging } from "./paging.js" export { notLocalPaths } from "./path-options.js" @@ -778,6 +777,7 @@ export { metadataCommand, modelCatalogueCommand, modelsCommand, + oauthSessionCommand, pinCommand, pollsCommand, projectsCommand, diff --git a/src/cli/oauth-session.test.ts b/src/cli/oauth-session.test.ts index e94d53f6e..61c35b21c 100644 --- a/src/cli/oauth-session.test.ts +++ b/src/cli/oauth-session.test.ts @@ -7,16 +7,12 @@ import { memoryKeyring } from "@wirecat/cli-base/testing" import { type Command, Option } from "commander" import { afterEach, describe, expect, it, vi } from "vitest" import { driveSessionCodec, zoomTokenCodec } from "../oauth/storage.js" -import { - type OAuthSessionOptions, - type OAuthSignedIn, - oauthFlowOf, - oauthPreset, - oauthSessionCommand, -} from "./oauth-session.js" +import { type OAuthSessionOptions, type OAuthSignedIn, oauthFlowOf, oauthPreset } from "./oauth-session.js" import { run } from "./program.js" import type { ServiceApiManifest } from "./service-api-manifest.js" import { settingsFor } from "./settings.js" +import { oauthSessionCommand } from "./shared-command-exports.js" +import { assertSharedContracts, hasSharedContract } from "./shared-contracts.js" const app = { command: "crm", appName: "crm-cli", envPrefix: "CRM", description: "synthetic", version: "1.0.0" } const manifest: ServiceApiManifest = { @@ -404,3 +400,22 @@ describe("oauthSessionCommand for a CLI that kept its own sign-ins", () => { ]) }) }) + +it("is cli-core's own session group, which a CLI changes only through its options", () => { + const session = oauthSessionCommand({ + app, + resolveSettings: settingsFor(app).resolveSettings, + providerId: "crm", + service: "Crm", + startOptions: [new Option("--grant ", "more to ask")], + preset: () => { + throw new Error("not reached") + }, + verify: async () => ({ id: "w", account: {} }), + }) + const start = session.commands.find((command) => command.name() === "start") as Command + expect(hasSharedContract(session) && hasSharedContract(start)).toBe(true) + expect(() => assertSharedContracts(session)).not.toThrow() + start.option("--later ", "added after the factory") + expect(() => assertSharedContracts(session)).toThrow("shared contract changed") +}) diff --git a/src/cli/shared-command-exports.ts b/src/cli/shared-command-exports.ts index c08db6012..6e506a978 100644 --- a/src/cli/shared-command-exports.ts +++ b/src/cli/shared-command-exports.ts @@ -73,6 +73,7 @@ import { } from "./messenger/tasks-command.js" import { topicsCommand as topicsCommandDefinition } from "./messenger/topics-command.js" import { watchCommand as watchCommandDefinition } from "./messenger/watch-command.js" +import { oauthSessionCommand as oauthSessionCommandDefinition } from "./oauth-session.js" import { runsCommand as runsCommandDefinition } from "./runs/command.js" import { scheduleCommandFor as scheduleCommandForDefinition } from "./schedule/schedule-command.js" import { serviceApiCommand as serviceApiCommandDefinition } from "./service-api.js" @@ -101,6 +102,8 @@ export const botMcpCommand = sharedCommand(botMcpCommandDefinition) /** @experimental */ export const botMessagesCommand = sharedCommand(botMessagesCommandDefinition) export const commandsCommand = sharedCommand(commandsCommandDefinition) +/** @experimental */ +export const oauthSessionCommand = sharedCommand(oauthSessionCommandDefinition) export const configCommand = sharedCommand(configCommandDefinition) export const setupCommand = sharedCommand(setupCommandDefinition) export const guideCommand = sharedCommand(guideCommandDefinition) diff --git a/src/cli/token-session.test.ts b/src/cli/token-session.test.ts index c5a77dde6..9a519df43 100644 --- a/src/cli/token-session.test.ts +++ b/src/cli/token-session.test.ts @@ -11,6 +11,8 @@ import { run } from "./program.js" import { hostsFetch } from "./service-api-call.js" import type { ServiceApiManifest } from "./service-api-manifest.js" import { settingsFor } from "./settings.js" + +import { hasSharedContract } from "./shared-contracts.js" import { manifestVerify, type TokenSessionOptions, tokenSessionCommand } from "./token-session.js" const app = { command: "pages", appName: "pages-cli", envPrefix: "PAGES", description: "synthetic", version: "1.0.0" } @@ -286,3 +288,13 @@ it("shows its guide before the question at a terminal and in the help, never bef expect((await signIn(["personal", "session", "start", "--json"], false)).err).not.toContain("pages.example") expect((await signIn(["personal", "session", "start", "--help"], false)).out).toContain(plain) }) + +it("is cli-core's own session group", () => { + const session = tokenSessionCommand({ + app, + service: "Pages", + resolveSettings: settingsFor(app).resolveSettings, + verify: async () => ({ id: "w", account: {} }), + }) + expect(session.commands.every(hasSharedContract) && hasSharedContract(session)).toBe(true) +}) diff --git a/src/cli/token-session.ts b/src/cli/token-session.ts index 8c1f5862b..7b4a780f1 100644 --- a/src/cli/token-session.ts +++ b/src/cli/token-session.ts @@ -10,6 +10,7 @@ import { plainSetupText } from "../setup/plain.js" import { drawsHyperlinks, hyperlink } from "../terminal/hyperlink.js" import { readSecret } from "../terminal/prompt.js" import { baseContext, environmentOf, outputFor, secretInputOf } from "./context.js" +import { honoursDryRun } from "./dry-run-guard.js" import { refuseOffline } from "./early-refusal.js" import { FETCH, LOCAL_READ, LOCAL_WRITE, logsInOrOut, REMOTE_READ, refuseUnderReadOnly } from "./effects.js" import { itemOf, listOf, renderAnswer } from "./envelope.js" @@ -19,6 +20,7 @@ import { ProfileTokenStore, type TokenStorage } from "./profile-token.js" import { serviceApiCall } from "./service-api-call.js" import { type ServiceApiManifest, siteValue } from "./service-api-manifest.js" import type { GlobalFlags, ResolveOptions, Settings } from "./settings.js" +import { sharedCommand } from "./shared-contracts.js" export interface TokenSessionOptions { app: AppIdentity @@ -140,7 +142,7 @@ export const storageOf = (raw: string): TokenStorage => { * `session start | status | end | list` for a service signed in with one token (WC-ACCT-12): the token is asked hidden * or read from stdin, checked with the service, and only then kept in the keyring or a private file. */ -export const tokenSessionCommand = (options: TokenSessionOptions): Command => { +const tokenSessionCommandDefinition = (options: TokenSessionOptions): Command => { const { app, service } = options const word = options.token ?? (options.login ? "password" : "API token") const contextOf = (command: Command) => { @@ -313,6 +315,11 @@ export const tokenSessionCommand = (options: TokenSessionOptions): Command => { .addCommand(list) } +/** cli-core's own command (WC-CMD-34): a CLI changes it only through its options. */ +export const tokenSessionCommand: (options: TokenSessionOptions) => Command = honoursDryRun( + sharedCommand(tokenSessionCommandDefinition), +) + const SITE = "site" /**