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
3 changes: 1 addition & 2 deletions docs/cli-reference.md

Large diffs are not rendered by default.

32 changes: 4 additions & 28 deletions src/commands/setupConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import { timedWait } from '../utils/timingLog';
*/
export const DEFAULT_WAIT_SECONDS = 280;
export const MAX_WAIT_SECONDS = 300;
/** CAP-983 (spec 3o rule 7): how long a wait gives Keep's pairing tab to open the conversation before the link is shown. */
export const KEEP_OPEN_SECONDS = 45;

export interface ConversationIdentity {
readonly userId: string;
Expand Down Expand Up @@ -271,14 +269,14 @@ export async function openSetupConversation(question: Refusal, deps: Conversatio
/** The conversation's link, as the agent shows it (CONVERSATION_OPEN, exit 3). */
function linkRefusal(state: ConversationState, goals: readonly Goal[], deps: ConversationSetupDeps, bin: string, resumed: boolean): ConversationOutcome {
const fingerprint = conversationFingerprint(state.k);
// CAP-983 (spec 3o): the tab that approved pairing is waiting and opens this conversation itself; a resumed one keeps its link.
// CAP-983 (spec 3o): the tab that approved pairing is waiting and opens this conversation itself. The link is still shown (Vincent, 2026-12-02).
const opensInKeep = !resumed && state.opensInKeep === true;
return refusal({
code: ERROR_CODES.CONVERSATION_OPEN,
message: resumed
? 'Setup for this folder is already open in Keep, so it carries on there. Show the human the link again, then run `answer.wait` at once and again whenever it exits 3.' // COPY-FLAG
: opensInKeep
? 'Keep is opening setup by itself in the tab where the human approved pairing. Do not post the link. Tell the human to continue in Keep, then run `answer.wait` at once and again whenever it exits 3.' // COPY-FLAG
? 'Keep is opening setup by itself in the tab where the human approved pairing. Still show the human the link, in case that tab is closed, then run `answer.wait` at once and again whenever it exits 3.' // COPY-FLAG
: 'Setup continues in Keep. Show the human the link, then run `answer.wait` at once and again whenever it exits 3.', // COPY-FLAG
exitCode: 3,
extra: {
Expand All @@ -292,7 +290,7 @@ function linkRefusal(state: ConversationState, goals: readonly Goal[], deps: Con
expires_at: state.openBy,
opens_in_keep: opensInKeep,
instructions: opensInKeep
? 'Keep opens this by itself in the tab where the human approved pairing. Show the link only if a wait refuses with CONVERSATION_NOT_OPENED_IN_KEEP.' // COPY-FLAG
? 'Keep opens this by itself in the tab where the human approved pairing. If it does not, open this link to answer the setup questions in Keep. Keep shows the same fingerprint.' // COPY-FLAG
: 'Open this link to answer the setup questions in Keep. Keep shows the same fingerprint.', // COPY-FLAG
answer: { wait: waitCommand(bin, state.id, state.folder) },
},
Expand Down Expand Up @@ -522,31 +520,9 @@ async function steps(run: ConversationRun, options: WaitOptions, deps: Conversat
extra: { flow_id: id, step: last.step, answered_in: 'keep', answers: last.answers, steps: answeredHere, goals: goalList(run.state.goals), answer: { wait: waitCommand(bin, id, run.state.folder) }, ...echo },
});
};
// CAP-983 (spec 3o rule 7): Keep was to open it in the pairing tab. Give it 45 seconds, then hand the agent the link.
const keepOpens = !run.helloDone && run.state.opensInKeep === true;
const helloBy = keepOpens ? Math.min(deadline, now() + KEEP_OPEN_SECONDS * 1000) : deadline;
for (;;) {
if (!run.helloDone) {
const event = await timedWait('wait.conversation', () => run.next(helloBy));
if (event.kind === 'timeout' && keepOpens) {
// From here on the agent holds the link, so this conversation is like any other.
run.update((s) => ({ ...s, opensInKeep: false }));
const fingerprint = conversationFingerprint(run.state.k);
return refusal({
code: ERROR_CODES.CONVERSATION_NOT_OPENED_IN_KEEP,
message: 'Keep has not opened setup yet. Show the human this link now, then run `answer.wait` at once and again whenever it exits 3.', // COPY-FLAG
exitCode: 3,
extra: {
flow_id: id,
elicitation: {
kind: 'link', url: conversationUrl(deps.keepOrigin, run.state), code: fingerprint, fingerprint, purpose: 'conversation', expires_at: run.state.openBy,
opens_in_keep: false, instructions: 'Open this link to answer the setup questions in Keep. Keep shows the same fingerprint.', // COPY-FLAG
answer: { wait: waitCommand(bin, id, run.state.folder) },
},
goals: goalList(run.state.goals),
},
});
}
const event = await timedWait('wait.conversation', () => run.next(deadline));
if (event.kind === 'timeout') return waiting({ opened: false, ...(note !== undefined ? { note_sent: false } : {}) });
if (event.kind === 'ended') return refusal(stopRefusal(new ConversationStop(endedCode(event, event.outcome === 'expired'), true), bin, id, run.state.folder, run.state.about, run.state.answers));
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/conversation/recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export interface ConversationState {
/** CAP-957: the last stop shown in Keep (code and message), so a rerun that hits the same stop doesn't repeat it. */
readonly shownStop?: string;
/**
* CAP-983 (spec 3o): the service said the pairing tab is waiting, so Keep opens this conversation itself and the agent
* does not post the link. Cleared once the CLI has handed the link out after all (CONVERSATION_NOT_OPENED_IN_KEEP).
* CAP-983 (spec 3o): the service said the pairing tab is waiting, so Keep opens this conversation itself. The agent
* still shows the link.
*/
readonly opensInKeep?: boolean;
/** CAP-977: the fingerprint of the question the human last answered, until a different question is asked or it is asked again on purpose. */
Expand Down
2 changes: 1 addition & 1 deletion src/core/agentsBlockPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const AGENTS_BLOCK = [
'- A result can succeed and still report a step that did not happen: an object in it with `"ok": false`, a `code`, a `message` and a `next_command` (such as `services_recorded`, `keep_not_told`, `scan_incomplete` or `scripts_not_wired`). Tell the human its `message`, then run its `next_command` when it has one. Never treat it as done.', // COPY-FLAG (CAP-980)
'- To pair this machine, run `capy pair --title "<what you are doing and why>" --description "<what you will do once approved>" --json`, in your own words (`capy setup` takes the same two flags). Keep shows them to the human on the unlock request, so say plainly what you want access for. Do not ask the human anything first, and add no other flags. It gives one approval link: Keep signs the human in or up and decides whether this machine gets their organization keys or a new organization. When the keys are already on this machine, the pairing only signs in (`"keys": "already_present"`).', // COPY-FLAG
'- If an elicitation has `kind` `"link"`, show the human its `url`, `code`, `fingerprint` and `expires_at`, and its QR. Every time you show a link (the first one and each PAIR_LINK_RENEWED), write the `url` as a clickable link, with the `code`, `fingerprint` and `expires_at`, as plain text in your reply. The QR is an addition, never the only way: not every agent or client can show images. Show the QR as an image the human can scan right in the conversation: if your client can show images or HTML widgets, show the PNG inline with `qr.image.data_uri` as the image source (a wait response has no data URI, so read `qr.image.file` and show it inline the same way). An attached file, a download link or a file path is not showing the QR. If you cannot show an image, show `qr.text` only in a real terminal with a monospace grid and no extra line spacing, else say plainly that the QR cannot be shown here. Never use an external QR service. Then run its `answer.wait` command at once in the foreground. It can take up to 9 minutes, because it returns only when something changes, so give it a tool timeout of at least 10 minutes. Run it again whenever it exits 3 with `PAIR_STILL_WAITING`, or if your tool stopped it early. If it exits 3 with `PAIR_LINK_RENEWED`, the old link expired and Capy made a new one: show the human the new `url`, `code`, `fingerprint`, `expires_at` and QR the same way, then run `answer.wait` again. Keep showing each new link and running its `answer.wait` without stopping to ask the human, until the human gives you the one-time code or says stop, the pairing is denied, or the CLI answers PAIR_EXPIRED. The CLI renews a link at most 4 times; when the last one expires it exits 3 with `PAIR_EXPIRED` and a confirm: only then ask the human whether to start again, and run `answer.yes` if they say yes. Never start a new `capy pair` for an expired link any other way. After the human approves, Keep shows them a one-time code and the wait exits 3 with `PAIR_CODE_REQUIRED`: ask the human for that code and run `capy pair --wait --code <code> --json`. Never fill in the code yourself: only the person who approved can see it, which is what stops a phished approval.', // COPY-FLAG
'- When a link elicitation has `opens_in_keep: true`, Keep is opening it by itself in the tab where the human approved pairing: do not post the link, tell the human to continue in Keep, and run `answer.wait` at once. If the wait exits 3 with `CONVERSATION_NOT_OPENED_IN_KEEP`, show the link it returns then.', // COPY-FLAG
'- When a link elicitation has `opens_in_keep: true`, Keep is opening it by itself in the tab where the human approved pairing: still show the link as usual, say Keep should open it by itself, and run `answer.wait` at once.', // COPY-FLAG
`- ${SETUP_COPY.run.agentsLine}`, // COPY-FLAG (CAP-960)
'- To refresh what this project has hooked up, run `capy summary --json` and follow its `discover.instructions`: discovery is yours, and its `suggestions` are only a starting point.', // COPY-FLAG (CAP-971)
'- To send a secret to GitHub or any other platform, run `capy deploy --json`. If no deploy target is set up, ask the human whether to set one up, and do not set one up yourself. Never push the value any other way, such as `gh secret set` or `vercel env add`. Never add `--yes` on your own.', // COPY-FLAG
Expand Down
1 change: 0 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,6 @@ export const ERROR_CODES = {
CONVERSATION_OPEN: 'CONVERSATION_OPEN',
CONVERSATION_WAITING: 'CONVERSATION_WAITING',
/** CAP-983 (spec 3o): Keep was to open the conversation by itself in the pairing tab and has not within 45 seconds. Show the link now. Exit 3. */
CONVERSATION_NOT_OPENED_IN_KEEP: 'CONVERSATION_NOT_OPENED_IN_KEEP',
CONVERSATION_STEP_DONE: 'CONVERSATION_STEP_DONE',
/** CAP-977: setup asked again the exact form the human just answered in Keep. The CLI does not post it a second time; the step is failed in Keep with one note. Exit 1. */
CONVERSATION_STEP_REPEATED: 'CONVERSATION_STEP_REPEATED',
Expand Down
2 changes: 1 addition & 1 deletion src/ui/elicitation.ts

Large diffs are not rendered by default.

26 changes: 5 additions & 21 deletions tests/conversation/setupConversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ describe('CAP-930: each run\'s output goes to Keep, sealed, scrubbed, and before
describe('CAP-983: the pairing tab opens the conversation itself (spec 3o)', () => {
beforeEach(() => forgetHandoff());

test('with this run\'s handoff secret, the conversation is tagged for the tab and the agent is told not to post the link', async () => {
test('with this run\'s handoff secret, the conversation is tagged for the tab, and the agent still shows the link', async () => {
const secret = randomBytes(32);
rememberHandoff(ORG, USER, secret.toString('base64url'));
service.tabWaiting(true);
Expand All @@ -1022,8 +1022,8 @@ describe('CAP-983: the pairing tab opens the conversation itself (spec 3o)', ()
// The tag the tab checks: HMAC under its secret over the id and this CLI's key.
expect(service.flows.get(id)!.handoffTag).toBe(w.b64u(w.handoffTag(secret, id, w.unb64u(state.cliPub))));
expect(elicitation.opens_in_keep).toBe(true);
expect(outcome.refusal.message).toContain('Do not post the link');
// The link is still there for the fallback, and the secret is used once: the next conversation has none.
expect(outcome.refusal.message).toContain('Still show the human the link');
// The link is always shown (Vincent, 2026-12-02), and the secret is used once: the next conversation has none.
expect(elicitation.url).toBe(`https://keep.example/flow/conversation?f=${id}#k=${state.k}`);
expect(JSON.stringify(store.load(USER, id))).not.toContain(secret.toString('base64url'));
await waitSetupConversation({ answers: {}, cancel: true }, deps);
Expand All @@ -1046,28 +1046,12 @@ describe('CAP-983: the pairing tab opens the conversation itself (spec 3o)', ()
}
});

test('if Keep does not open it within 45 seconds, the wait hands the agent the link (no silent failure)', async () => {
test('a wait does not cut short when Keep opens it: it runs its whole time like any other', async () => {
rememberHandoff(ORG, USER, randomBytes(32).toString('base64url'));
service.tabWaiting(true);
const state = await open();
const started = now;
const outcome = await waitSetupConversation({ answers: {} }, deps);
if (outcome.kind !== 'refusal') throw new Error('expected the fallback');
expect(outcome.refusal.code).toBe('CONVERSATION_NOT_OPENED_IN_KEEP');
expect(now - started).toBeLessThan(60_000);
const { elicitation } = outcome.refusal.extra as { elicitation: { url: string; fingerprint: string; answer: { wait: string } } };
expect(elicitation.url).toBe(`https://keep.example/flow/conversation?f=${state.id}#k=${state.k}`);
expect(elicitation.answer.wait).toContain(`--flow ${state.id}`);
// Once the agent holds the link, the next wait is an ordinary one.
await open();
expect(codeOf(await wait())).toBe('CONVERSATION_WAITING');
});

test('when Keep opens it in time, the wait goes on as usual', async () => {
rememberHandoff(ORG, USER, randomBytes(32).toString('base64url'));
service.tabWaiting(true);
const state = await open();
fakePage(state, kLocal, service).hello();
expect(codeOf(await wait())).not.toBe('CONVERSATION_NOT_OPENED_IN_KEEP');
});
});

Loading