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
1 change: 1 addition & 0 deletions api/cli.api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions changes/feat-mark-shared-session-commands.md
Original file line number Diff line number Diff line change
@@ -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`.
5 changes: 4 additions & 1 deletion src/cli/account-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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: [
Expand Down
12 changes: 10 additions & 2 deletions src/cli/documents/file-mirrors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -132,10 +134,16 @@ const setup = (rewrite?: (text: string) => string, env?: NodeJS.ProcessEnv) => {
return bodies.at(-1) as Body & { item?: Record<string, unknown>; items?: Record<string, unknown>[] }
}
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)
Expand Down
12 changes: 6 additions & 6 deletions src/cli/documents/index.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -133,3 +131,5 @@ export {
documentSourceFromManifest,
type ManifestDocumentSource,
} from "./manifest/source.js"

export const fileMirrorsCommand = honoursDryRun(sharedCommand(fileMirrorsCommandDefinition))
2 changes: 1 addition & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -778,6 +777,7 @@ export {
metadataCommand,
modelCatalogueCommand,
modelsCommand,
oauthSessionCommand,
pinCommand,
pollsCommand,
projectsCommand,
Expand Down
29 changes: 22 additions & 7 deletions src/cli/oauth-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 <scopes>", "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 <value>", "added after the factory")
expect(() => assertSharedContracts(session)).toThrow("shared contract changed")
})
3 changes: 3 additions & 0 deletions src/cli/shared-command-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/cli/token-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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)
})
9 changes: 8 additions & 1 deletion src/cli/token-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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"

/**
Expand Down
Loading