Skip to content
Open
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
29 changes: 20 additions & 9 deletions apps/server/scripts/acp-replay-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ function matchesExpected(expected: unknown, actual: unknown): boolean {
return Object.is(expected, actual);
}

function stopWithFailure(detail: string, actual?: unknown): void {
function stopWithFailure(
detail: string,
actual?: unknown,
failedRequestId?: JsonRpcMessage["id"],
): void {
if (stopped) return;
stopped = true;
const entry = transcript.entries[cursor];
Expand All @@ -158,6 +162,16 @@ function stopWithFailure(detail: string, actual?: unknown): void {
};
writeStatus(failure);
process.stderr.write(`ACP replay mismatch: ${JSON.stringify(failure)}\n`);
// Reject what the client still awaits, so a test fails with the mismatch instead of hanging.
const message = `ACP replay mismatch at ${entry?.label ?? "end of transcript"}: ${detail}${
actual === undefined ? "" : ` ${JSON.stringify(actual)}`
}`;
const ids = [...pendingClientRequestIds.values()];
if (failedRequestId !== undefined && failedRequestId !== null) ids.push(failedRequestId);
for (const id of ids) {
send({ jsonrpc: "2.0", id, error: { code: -32603, message } });
}
pendingClientRequestIds.clear();
process.exitCode = 1;
process.stdin.pause();
}
Expand Down Expand Up @@ -284,14 +298,11 @@ function handleMessage(message: JsonRpcMessage): void {
}
const entry = transcript.entries[cursor];
if (entry?.type !== "expect_outbound" || !matchesExpected(entry.frame, actual)) {
stopWithFailure("Unexpected outbound ACP frame", actual);
if (actual.kind === "request" && message.id !== undefined && message.id !== null) {
send({
jsonrpc: "2.0",
id: message.id,
error: { code: -32603, message: "ACP replay frame mismatch" },
});
}
stopWithFailure(
"Unexpected outbound ACP frame",
actual,
actual.kind === "request" ? message.id : undefined,
);
return;
}
if (actual.kind === "request" && message.id !== undefined && message.id !== null) {
Expand Down
186 changes: 168 additions & 18 deletions apps/server/src/orchestration-v2/Adapters/KiroAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
import {
CheckpointId,
MessageId,
type ModelSelection,
NodeId,
Expand Down Expand Up @@ -293,13 +294,54 @@ const openSessionFrames = (input: {
}),
];

/** Kiro keys an MCP server's OAuth tokens by a hash of the server; the value is a JSON string. */
const MCP_TOKENS_KEY = "kiro.mcp.4f1c2a9e.tokens";
const MCP_TOKENS = JSON.stringify({ access_token: "stored-token", token_type: "Bearer" });

const kiroSecretRequest = (method: string, params: unknown): Frame => ({
type: "emit_inbound",
label: method,
frame: { kind: "request", method, params },
});
const kiroSecretResponse = (method: string, result: unknown): Frame => ({
type: "expect_outbound",
label: `${method}.result`,
frame: { kind: "response", method, result },
});

const kiroSecretRejection = (method: string): Frame => ({
type: "expect_outbound",
label: `${method}.error`,
frame: { kind: "response", method, error: "<any>" },
});

/**
* A temp KIRO_HOME whose `secrets.json` holds `secrets`, the flat map kiro-cli
* keeps there. HOME points elsewhere so only KIRO_HOME can find the file.
*/
const makeKiroHome = Effect.fn("makeKiroHome")(function* (secrets: Record<string, string>) {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-kiro-home-" });
const kiroHome = path.join(root, "kiro-home");
const home = path.join(root, "home");
yield* fileSystem.makeDirectory(kiroHome);
yield* fileSystem.makeDirectory(home);
const secretsPath = path.join(kiroHome, "secrets.json");
yield* fileSystem.writeFileString(secretsPath, JSON.stringify(secrets, null, 2));
return { secretsPath, environment: { HOME: home, KIRO_HOME: kiroHome } };
});

const runKiroScript = Effect.fn("runKiroScript")(function* (input: {
readonly scenario: string;
readonly frames: ReadonlyArray<Frame>;
/** Frames for the runtime a rollback starts, replayed by a second agent process. */
readonly replacementFrames?: ReadonlyArray<Frame>;
readonly model?: string;
/** Option selections (e.g. reasoningEffort) for the session and `startTurn`. */
readonly options?: ModelSelection["options"];
readonly runtimeMode?: RuntimeMode;
readonly environment?: NodeJS.ProcessEnv;
/** Holds the script's inbound frames at labels the test releases. */
readonly replayGate?: ProviderReplayGate;
readonly testHooks?: Parameters<typeof makeKiroAdapterV2>[0]["testHooks"];
Expand All @@ -323,6 +365,7 @@ const runKiroScript = Effect.fn("runKiroScript")(function* (input: {
requestId: RuntimeRequestId,
decision: ProviderApprovalDecision,
) => Effect.Effect<void, ProviderAdapterV2Error>;
readonly rollbackToStart: Effect.Effect<void, ProviderAdapterV2Error>;
}) => Effect.Effect<void, ProviderAdapterV2Error>;
}) {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
Expand All @@ -331,30 +374,48 @@ const runKiroScript = Effect.fn("runKiroScript")(function* (input: {
const replayDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: `t3-kiro-${input.scenario}-`,
});
const statusPath = path.join(replayDir, "status.json");
const transcript = yield* decodeAcpReplayTranscript(
{
provider: KIRO_PROVIDER,
protocol: "acp.ndjson-jsonrpc",
version: "1",
scenario: input.scenario,
entries: input.frames as never,
},
KIRO_PROVIDER,
const scripts = yield* Effect.forEach(
[input.frames, ...(input.replacementFrames === undefined ? [] : [input.replacementFrames])],
(frames, index) =>
decodeAcpReplayTranscript(
{
provider: KIRO_PROVIDER,
protocol: "acp.ndjson-jsonrpc",
version: "1",
scenario: input.scenario,
entries: frames as never,
},
KIRO_PROVIDER,
).pipe(
Effect.map((transcript) => ({
transcript,
statusPath: path.join(replayDir, `status-${index}.json`),
})),
),
);
let runtimesMade = 0;
const instanceId = ProviderInstanceId.make(`kiro-${input.scenario}`);
const scriptPath = yield* path.fromFileUrl(
new URL("../../../scripts/acp-replay-agent.ts", import.meta.url),
);
const adapter = yield* makeKiroAdapterV2({
instanceId,
settings: ENABLED_KIRO_SETTINGS,
environment: {},
environment: input.environment ?? {},
selfInvocation: yield* resolveSelfInvocation(),
makeRuntime: (runtimeInput) =>
makeAcpReplayRuntime({
transcript,
statusPath,
makeRuntime: (runtimeInput) => {
const script = scripts[runtimesMade++];
if (script === undefined) {
// An unscripted runtime is a retry after the last one failed; report why it failed.
const last = scripts[scripts.length - 1]!;
return makeAcpReplayCompletenessAssertion(
fileSystem,
last.statusPath,
last.transcript,
).pipe(Effect.andThen(Effect.die(`No replay script for Kiro runtime ${runtimesMade}`)));
}
return makeAcpReplayRuntime({
...script,
scriptPath,
childProcessSpawner,
fileSystem,
Expand All @@ -365,7 +426,8 @@ const runKiroScript = Effect.fn("runKiroScript")(function* (input: {
? runtime
: { ...runtime, cancel: input.wrapCancel(runtime.cancel) },
),
),
);
},
...(input.testHooks === undefined ? {} : { testHooks: input.testHooks }),
});
// Held frames must not outlive a failed drive and wedge teardown.
Expand Down Expand Up @@ -441,10 +503,26 @@ const runKiroScript = Effect.fn("runKiroScript")(function* (input: {
startTurnOn,
interrupt: (providerTurnId) => session.interruptTurn({ providerThread, providerTurnId }),
respond: (requestId, decision) => session.respondToRuntimeRequest({ requestId, decision }),
rollbackToStart: session
.rollbackThread({
providerThread,
providerThreadTurns: [],
target: {
type: "thread_start",
checkpointId: CheckpointId.make(`checkpoint-kiro-${input.scenario}`),
appRunOrdinal: 0,
},
})
.pipe(Effect.asVoid),
});
}).pipe(Effect.scoped);
// Closing the session stops the replay agent; every scripted frame must be used.
yield* makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript);
// Closing the session stops the replay agents; every scripted frame must be used.
yield* Effect.forEach(
scripts,
({ statusPath, transcript }) =>
makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript),
{ discard: true },
);
});

const collectTurn = (events: Stream.Stream<ProviderAdapterV2Event, ProviderAdapterV2Error>) =>
Expand Down Expand Up @@ -780,6 +858,78 @@ describe("KiroAdapterV2", () => {
}).pipe(Effect.provide(testLayer), Effect.scoped),
);

it.effect("advertises secret storage, reads Kiro's secrets file and never writes it", () =>
Effect.gen(function* () {
const kiroHome = yield* makeKiroHome({ [MCP_TOKENS_KEY]: MCP_TOKENS });
const refreshed = JSON.stringify({ access_token: "refreshed", token_type: "Bearer" });
const otherKey = "kiro.mcp.other.tokens";
yield* runKiroScript({
scenario: "secret-storage",
environment: kiroHome.environment,
frames: [
// Kiro reads stored MCP OAuth tokens from the client only when it opts in.
outbound("initialize", {
protocolVersion: "<any>",
info: "<any>",
capabilities: "<any>",
clientCapabilities: {
fs: "<any>",
terminal: "<any>",
session: "<any>",
elicitation: "<any>",
_meta: { kiro: { hooks: { enabled: true, v2: true }, secretStorage: true } },
},
clientInfo: "<any>",
}),
answer("initialize", KIRO_V3_INITIALIZE),
outbound("session/new", { cwd: "<workspace>", mcpServers: "<any>" }),
kiroSecretRequest("_kiro/secret/get", { key: MCP_TOKENS_KEY }),
kiroSecretResponse("_kiro/secret/get", { value: MCP_TOKENS }),
kiroSecretRequest("_kiro/secret/get", { key: "kiro.mcp.missing.tokens" }),
kiroSecretResponse("_kiro/secret/get", { value: null }),
// kiro-cli rewrites this file unlocked, so T3 never writes it.
kiroSecretRequest("_kiro/secret/store", { key: otherKey, value: refreshed }),
kiroSecretRejection("_kiro/secret/store"),
kiroSecretRequest("_kiro/secret/delete", { key: MCP_TOKENS_KEY }),
kiroSecretRejection("_kiro/secret/delete"),
answer("session/new", sessionSetup([modeOption])),
...closeSession,
],
drive: () => Effect.void,
});
const fileSystem = yield* FileSystem.FileSystem;
assert.deepStrictEqual(JSON.parse(yield* fileSystem.readFileString(kiroHome.secretsPath)), {
[MCP_TOKENS_KEY]: MCP_TOKENS,
});
}).pipe(Effect.provide(testLayer), Effect.scoped),
);

it.effect("answers Kiro's secret reads on the runtime a rollback starts", () =>
Effect.gen(function* () {
const kiroHome = yield* makeKiroHome({ [MCP_TOKENS_KEY]: MCP_TOKENS });
const openFrames = [
outbound("initialize"),
answer("initialize", KIRO_V3_INITIALIZE),
outbound("session/new", { cwd: "<workspace>", mcpServers: "<any>" }),
];
yield* runKiroScript({
scenario: "secret-get-after-rollback",
environment: kiroHome.environment,
// Rollback stops this runtime's process without `session/close`.
frames: [...openFrames, answer("session/new", sessionSetup([modeOption]))],
// The replacement `session/new` reconnects MCP servers, which reads their tokens.
replacementFrames: [
...openFrames,
kiroSecretRequest("_kiro/secret/get", { key: MCP_TOKENS_KEY }),
kiroSecretResponse("_kiro/secret/get", { value: MCP_TOKENS }),
answer("session/new", sessionSetup([modeOption])),
...closeSession,
],
drive: ({ rollbackToStart }) => rollbackToStart,
});
}).pipe(Effect.provide(testLayer), Effect.scoped),
);

it.effect("sends a model Kiro does not list, and fails with Kiro's own message", () =>
runKiroScript({
scenario: "unlisted-model",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{"type":"transcript_start","provider":"kiro","protocol":"acp.ndjson-jsonrpc","version":"1","scenario":"kiro_cloud_config","metadata":{"generatedBy":"live-kiro-recorder","kiroVersion":"2.32.0","normalization":"Session ids are fixed sess_ UUIDs and other UUIDs (message and request ids) are numbered in first-seen order, the workspace is <workspace>, HOME is /home/kiro-replay and the recording user is kiro-replay. T3-owned prompt text, MCP servers and initialize params other than clientCapabilities and _meta are <any>. Kiro's log directory, user-scoped commands and agents, and the _kiro/* broadcasts T3 ignores (governance, steering documents, powers, tools, hooks, MCP status, session roster) are removed. Timestamps are kept as recorded.","droppedFrames":13,"editedFrames":"Copied from turn_interrupt/kiro_transcript.ndjson. Added by hand: the background `fetch_cloud_config` tool call and its completion, as @hstern recorded them from kiro-cli 2.31.0 with an Identity Center login, in an order that failed there: the call before `session/prompt`, its completion after the `user_message_id_assigned` echo. The tool call id is invented; the reported frames omitted it."}}
{"type":"expect_outbound","label":"request:initialize","frame":{"kind":"request","method":"initialize","params":{"protocolVersion":2,"info":"<any>","capabilities":"<any>","clientCapabilities":{"fs":{"readTextFile":false,"writeTextFile":false},"terminal":false,"session":{"notices":{}},"elicitation":{"form":{}},"_meta":{"kiro":{"hooks":{"enabled":true,"v2":true}}}},"clientInfo":"<any>"}}}
{"type":"expect_outbound","label":"request:initialize","frame":{"kind":"request","method":"initialize","params":{"protocolVersion":2,"info":"<any>","capabilities":"<any>","clientCapabilities":{"fs":{"readTextFile":false,"writeTextFile":false},"terminal":false,"session":{"notices":{}},"elicitation":{"form":{}},"_meta":{"kiro":{"hooks":{"enabled":true,"v2":true},"secretStorage":true}}},"clientInfo":"<any>"}}}
{"type":"emit_inbound","label":"response:initialize","frame":{"kind":"response","method":"initialize","result":{"protocolVersion":2,"agentCapabilities":{"loadSession":true,"promptCapabilities":{"image":true,"embeddedContext":true},"mcpCapabilities":{"http":true,"sse":true},"sessionCapabilities":{"list":{},"close":{},"delete":{},"fork":{"_meta":{"kiro":{"messageId":true}}}},"_meta":{"kiro":{"checkpoints":true,"sessionList":true,"policyNotifications":true,"extensionMethods":["_kiro/knowledge","_kiro/memory/list","_kiro/memory/get","_kiro/memory/create","_kiro/memory/update","_kiro/memory/delete","_kiro/codeIntelligence","_kiro/session/context","_kiro/session/compact","_kiro/session/export","_kiro/session/history","_kiro/config/template","_kiro/workflow/new","_kiro/workflow/invoke","_kiro/workflow/load","_kiro/workflow/inspect","_kiro/workflow/list","_kiro/workflow/pause","_kiro/workflow/cancel","_kiro/workflow/delete","_kiro/workflow/listRecipes","_kiro/workflow/listWatchHandlers","_kiro/workflow/resumeAll","_kiro/workflow/retry","_kiro/workflow/update","_kiro/workflow/resume","_kiro/sourceProviders/list","_kiro/sourceProviders/listResources"],"sessionSources":["local","remote"],"sessionListScopes":["workspace","user"],"executionTargets":["local","cloud-sandbox"],"sourceProviders":true,"replayMarking":true}}},"authMethods":[{"id":"aws-builder-id","name":"AWS Builder ID"},{"id":"aws-iam-identity-center","name":"AWS IAM Identity Center"}]}}}
{"type":"expect_outbound","label":"request:session/new","frame":{"kind":"request","method":"session/new","params":{"cwd":"<workspace>","mcpServers":"<any>"}}}
{"type":"emit_inbound","label":"notification:session/update:available_commands_update","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"sess_00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"available_commands_update","availableCommands":[{"name":"architecture-selection","description":"Steering: architecture-selection","input":{"hint":"optional context"},"_meta":{"kiro":{"type":"steering","contextQuery":"global:architecture-selection","commandId":"architecture-selection","resource":{"resourceType":"steering","source":{"origin":"bundled"}},"originalName":"architecture-selection"}}},{"name":"quick-spec","description":"Steering: quick-spec","input":{"hint":"optional context"},"_meta":{"kiro":{"type":"steering","contextQuery":"global:quick-spec","commandId":"quick-spec","resource":{"resourceType":"steering","source":{"origin":"bundled"}},"originalName":"quick-spec"}}},{"name":"bug-fix","description":"Steering: bug-fix","input":{"hint":"optional context"},"_meta":{"kiro":{"type":"steering","contextQuery":"global:bug-fix","commandId":"bug-fix","resource":{"resourceType":"steering","source":{"origin":"bundled"}},"originalName":"bug-fix"}}},{"name":"general-task-execution","description":"General-purpose sub-agent with access to all tools for executing arbitrary tasks","input":{"hint":"task to delegate"},"_meta":{"kiro":{"type":"custom-agent","commandId":"general-task-execution","resource":{"resourceType":"agent","source":{"origin":"bundled"}},"originalName":"general-task-execution"}}},{"name":"context-gatherer","description":"Investigates the codebase to answer a specific question about it. Deeply analyzes existing code by tracing execution paths, mapping architecture and abstraction layers, understanding patterns, and documenting dependencies, returning a thorough, direct written analysis that explains how the relevant code works and points to the specific files and lines involved. Use it to understand code before you change it: before designing or implementing a change to code you don't already fully understand, start with context-gatherer so your edits are grounded in how the code actually works. When a task requires understanding code you don't already know, dispatch context-gatherer to do that exploration before running your own searches or reads. Rules: 1. Provide a clear, specific prompt that includes your intent: a detailed task description of the context you need and what you're trying to accomplish. 2. The subagent explores the codebase and returns an analysis of your query 3. Trust the returned results. You may still inspect additional code yourself when you need context beyond what was returned. 4. Reuse existing context-gatherer output for related follow-up questions; do not re-invoke on the same question with different phrasing. 5. For multiple distinct questions, dispatch several context-gatherers in parallel, each targeting a different aspect of the codebase.","input":{"hint":"task to delegate"},"_meta":{"kiro":{"type":"custom-agent","commandId":"context-gatherer","resource":{"resourceType":"agent","source":{"origin":"bundled"}},"originalName":"context-gatherer"}}}]}}}}
Expand Down
Loading
Loading