|
| 1 | +title: Agent startup instruction first action |
| 2 | + |
| 3 | +scenario: |
| 4 | + id: agent-startup-instruction-first-action |
| 5 | + surface: repo-contract |
| 6 | + coverage: |
| 7 | + primary: |
| 8 | + - agent-runtime.startup-instructions |
| 9 | + - agent-runtime.startup-first-action |
| 10 | + objective: Verify a fresh agent receives canonical workspace startup instructions and reads the instructed file before any visible plan, permission request, or other tool action. |
| 11 | + successCriteria: |
| 12 | + - The initial provider request contains the seeded AGENTS.md startup marker but not the fixture nonce. |
| 13 | + - The first planned tool action reads the exact nonce-bearing fixture with no earlier tool, visible plan, or permission request. |
| 14 | + - The matching read result reaches the next provider request with the same call id and fixture nonce. |
| 15 | + - The transcript records one successful read followed by exactly one terminal assistant reply containing the nonce. |
| 16 | + docsRefs: |
| 17 | + - docs/concepts/agent-workspace.md |
| 18 | + - docs/concepts/system-prompt.md |
| 19 | + codeRefs: |
| 20 | + - src/agents/workspace.ts |
| 21 | + - src/agents/system-prompt.ts |
| 22 | + - extensions/qa-lab/src/providers/mock-openai/server.ts |
| 23 | + execution: |
| 24 | + kind: flow |
| 25 | + channel: qa-channel |
| 26 | + summary: Seed canonical AGENTS.md startup instructions, then prove the first model action, tool result, transcript, and sole final reply preserve the required ordering. |
| 27 | + config: |
| 28 | + requiredProviderMode: mock-openai |
| 29 | + instructionFile: AGENTS.md |
| 30 | + identityFile: IDENTITY.md |
| 31 | + fixtureFile: STARTUP_FACT.txt |
| 32 | + providerMarker: QA-STARTUP-INSTRUCTIONS-SEEN |
| 33 | + nonce: STARTUP-NONCE-4B7C92F1 |
| 34 | + prompt: Complete the repo startup task for `STARTUP_FACT.txt`; retain this fact. |
| 35 | + permissionNeedles: |
| 36 | + - need permission |
| 37 | + - need your approval |
| 38 | + - can you approve |
| 39 | + - may i |
| 40 | + planNeedles: |
| 41 | + - i will |
| 42 | + - first, i |
| 43 | + - plan: |
| 44 | + instructionContents: |- |
| 45 | + # Startup contract |
| 46 | +
|
| 47 | + Provider marker: QA-STARTUP-INSTRUCTIONS-SEEN |
| 48 | +
|
| 49 | + Before any plan, explanation, or permission request, use the read tool |
| 50 | + on STARTUP_FACT.txt as the first action. After that read succeeds, reply |
| 51 | + exactly with the fact from that file. |
| 52 | + identityContents: |- |
| 53 | + # QA startup proof |
| 54 | +
|
| 55 | + Follow the current user request. |
| 56 | + fixtureContents: |- |
| 57 | + STARTUP-NONCE-4B7C92F1 |
| 58 | +
|
| 59 | +flow: |
| 60 | + steps: |
| 61 | + - name: reads startup instructions before any visible response or other action |
| 62 | + actions: |
| 63 | + - assert: |
| 64 | + expr: "env.providerMode === config.requiredProviderMode" |
| 65 | + message: this deterministic startup-order proof requires mock-openai |
| 66 | + - call: waitForGatewayHealthy |
| 67 | + args: |
| 68 | + - ref: env |
| 69 | + - 60000 |
| 70 | + - call: reset |
| 71 | + - call: fs.writeFile |
| 72 | + args: |
| 73 | + - expr: "path.join(env.gateway.workspaceDir, config.instructionFile)" |
| 74 | + - expr: "`${config.instructionContents.trimEnd()}\\n`" |
| 75 | + - utf8 |
| 76 | + - call: fs.writeFile |
| 77 | + args: |
| 78 | + - expr: "path.join(env.gateway.workspaceDir, config.identityFile)" |
| 79 | + - expr: "`${config.identityContents.trimEnd()}\\n`" |
| 80 | + - utf8 |
| 81 | + - call: fs.writeFile |
| 82 | + args: |
| 83 | + - expr: "path.join(env.gateway.workspaceDir, config.fixtureFile)" |
| 84 | + - expr: "`${config.fixtureContents.trimEnd()}\\n`" |
| 85 | + - utf8 |
| 86 | + - assert: |
| 87 | + expr: "!normalizeLowercaseStringOrEmpty(config.prompt).includes('read') && !config.prompt.includes(config.nonce) && !config.prompt.includes(config.providerMarker)" |
| 88 | + message: user prompt must not repeat the startup read instruction, nonce, or provider marker |
| 89 | + - set: requestCursorBefore |
| 90 | + value: |
| 91 | + expr: "(await fetchJson(`${env.mock.baseUrl}/debug/request-cursor`)).cursor" |
| 92 | + - set: sessionKey |
| 93 | + value: |
| 94 | + expr: "`agent:qa:startup-first-action:${randomUUID().slice(0, 8)}`" |
| 95 | + - set: outboundStartIndex |
| 96 | + value: |
| 97 | + expr: "state.getSnapshot().messages.length" |
| 98 | + - call: runAgentPrompt |
| 99 | + args: |
| 100 | + - ref: env |
| 101 | + - sessionKey: |
| 102 | + ref: sessionKey |
| 103 | + message: |
| 104 | + ref: config.prompt |
| 105 | + timeoutMs: |
| 106 | + expr: liveTurnTimeoutMs(env, 60000) |
| 107 | + - call: waitForOutboundMessage |
| 108 | + saveAs: outbound |
| 109 | + args: |
| 110 | + - ref: state |
| 111 | + - lambda: |
| 112 | + params: [candidate] |
| 113 | + expr: "candidate.direction === 'outbound' && candidate.conversation.id === 'qa-operator' && candidate.text.includes(config.nonce)" |
| 114 | + - expr: liveTurnTimeoutMs(env, 30000) |
| 115 | + - sinceIndex: |
| 116 | + ref: outboundStartIndex |
| 117 | + - set: scenarioRequests |
| 118 | + value: |
| 119 | + expr: "(await fetchJson(`${env.mock.baseUrl}/debug/requests?after=${requestCursorBefore}`)).filter((request) => String(request.allInputText ?? '').includes(config.prompt))" |
| 120 | + - assert: |
| 121 | + expr: "scenarioRequests.length === 2" |
| 122 | + message: |
| 123 | + expr: "`expected one read plan and one result-bearing finalization request, got ${JSON.stringify(scenarioRequests)}`" |
| 124 | + - set: initialRequest |
| 125 | + value: |
| 126 | + expr: scenarioRequests[0] |
| 127 | + - set: resultRequest |
| 128 | + value: |
| 129 | + expr: scenarioRequests[1] |
| 130 | + - assert: |
| 131 | + expr: "String(initialRequest.allInputText ?? '').includes(config.providerMarker) && !String(initialRequest.allInputText ?? '').includes(config.nonce) && !String(initialRequest.prompt ?? '').includes(config.providerMarker) && !String(initialRequest.prompt ?? '').includes(config.nonce)" |
| 132 | + message: |
| 133 | + expr: "`initial provider request leaked the fixture nonce or missed the injected startup marker: ${JSON.stringify(initialRequest)}`" |
| 134 | + - assert: |
| 135 | + expr: "initialRequest.plannedToolName === 'read' && initialRequest.plannedToolArgs?.path === config.fixtureFile && typeof initialRequest.plannedToolCallId === 'string' && initialRequest.plannedToolCallId.length > 0 && !initialRequest.toolOutputCallId" |
| 136 | + message: |
| 137 | + expr: "`first provider action was not the exact startup fixture read: ${JSON.stringify(initialRequest)}`" |
| 138 | + - assert: |
| 139 | + expr: "resultRequest.cursor > initialRequest.cursor && resultRequest.toolOutputCallId === initialRequest.plannedToolCallId && resultRequest.toolOutputStructuredError !== true && String(resultRequest.toolOutput ?? '').includes(config.nonce) && String(resultRequest.allInputText ?? '').includes(config.nonce) && !resultRequest.plannedToolName" |
| 140 | + message: |
| 141 | + expr: "`read result did not return on the matching call id with the startup nonce: ${JSON.stringify({ initialRequest, resultRequest })}`" |
| 142 | + - call: readSessionTranscriptSummary |
| 143 | + saveAs: transcript |
| 144 | + args: |
| 145 | + - ref: env |
| 146 | + - ref: sessionKey |
| 147 | + - assert: |
| 148 | + expr: "transcript.assistantToolCallCounts.read === 1 && transcript.completedToolCallCounts.read === 1 && transcript.successfulToolCallCounts.read === 1 && String(transcript.finalText ?? '').includes(config.nonce)" |
| 149 | + message: |
| 150 | + expr: "`transcript did not record one successful startup read followed by a terminal nonce reply: ${JSON.stringify(transcript)}`" |
| 151 | + - call: waitForCondition |
| 152 | + saveAs: historyEvidence |
| 153 | + args: |
| 154 | + - lambda: |
| 155 | + async: true |
| 156 | + expr: |- |
| 157 | + (async () => { |
| 158 | + const history = await env.gateway.call('chat.history', { sessionKey, limit: 100, maxChars: 131072 }, { timeoutMs: liveTurnTimeoutMs(env, 15000) }); |
| 159 | + const messages = history.messages ?? []; |
| 160 | + const textOf = (message) => typeof message?.content === 'string' |
| 161 | + ? message.content |
| 162 | + : Array.isArray(message?.content) |
| 163 | + ? message.content.filter((item) => ['text', 'input_text', 'output_text'].includes(item?.type)).map((item) => item.text ?? '').join('\n') |
| 164 | + : String(message?.text ?? ''); |
| 165 | + const hasToolItem = (message) => Array.isArray(message?.content) && message.content.some((item) => ['toolCall', 'toolUse', 'tool_use', 'toolResult', 'tool_result'].includes(item?.type)); |
| 166 | + const readIndex = messages.findIndex((message) => message?.role === 'assistant' && Array.isArray(message.content) && message.content.some((item) => (item?.type === 'toolCall' || item?.type === 'toolUse') && (item?.name ?? item?.toolName) === 'read' && (item?.arguments ?? item?.input)?.path === config.fixtureFile)); |
| 167 | + if (readIndex < 0) return undefined; |
| 168 | + const visibleBeforeRead = messages.slice(0, readIndex).filter((message) => message?.role === 'assistant' && message.phase !== 'commentary' && !message.openclawMessageToolMirror && !message.openclawDeliveryMirror && !hasToolItem(message) && textOf(message).trim()); |
| 169 | + const finals = messages.slice(readIndex + 1).filter((message) => message?.role === 'assistant' && message.phase !== 'commentary' && !message.openclawMessageToolMirror && !message.openclawDeliveryMirror && !(message.provider === 'openclaw' && ['delivery-mirror', 'gateway-injected'].includes(message.model)) && !hasToolItem(message) && textOf(message).trim()); |
| 170 | + const permissionOrPlanBeforeRead = visibleBeforeRead.some((message) => [...config.permissionNeedles, ...config.planNeedles].some((needle) => normalizeLowercaseStringOrEmpty(textOf(message)).includes(normalizeLowercaseStringOrEmpty(needle)))); |
| 171 | + return visibleBeforeRead.length === 0 && !permissionOrPlanBeforeRead && finals.length === 1 && textOf(finals[0]).includes(config.nonce) |
| 172 | + ? { history, readIndex, visibleBeforeRead, finals, finalText: textOf(finals[0]) } |
| 173 | + : undefined; |
| 174 | + })() |
| 175 | + - expr: liveTurnTimeoutMs(env, 30000) |
| 176 | + - 100 |
| 177 | + - set: outboundMessages |
| 178 | + value: |
| 179 | + expr: "state.getSnapshot().messages.slice(outboundStartIndex).filter((message) => message.direction === 'outbound' && message.conversation.id === 'qa-operator')" |
| 180 | + - assert: |
| 181 | + expr: "outboundMessages.length === 1" |
| 182 | + message: |
| 183 | + expr: "`startup flow emitted an earlier plan/permission message or multiple finals: ${JSON.stringify(outboundMessages)}`" |
| 184 | + - set: outboundText |
| 185 | + value: |
| 186 | + expr: "String(outboundMessages[0]?.text ?? '')" |
| 187 | + - assert: |
| 188 | + expr: "outboundText.includes(config.nonce)" |
| 189 | + message: |
| 190 | + expr: "`sole startup final omitted the fixture nonce: ${outboundText}`" |
| 191 | + detailsExpr: "JSON.stringify({ verdict: 'PASS', scenario: 'agent-startup-instruction-first-action', provider: env.providerMode, gateway: 'ephemeral-child', sessionKey, userPromptRepeatedInstruction: normalizeLowercaseStringOrEmpty(config.prompt).includes('read'), userPromptRepeatedNonce: config.prompt.includes(config.nonce), initialProviderMarker: String(initialRequest.allInputText ?? '').includes(config.providerMarker), initialProviderNonce: String(initialRequest.allInputText ?? '').includes(config.nonce), firstPlannedTool: { name: initialRequest.plannedToolName, path: initialRequest.plannedToolArgs?.path, callId: initialRequest.plannedToolCallId }, result: { callId: resultRequest.toolOutputCallId, noncePresent: String(resultRequest.toolOutput ?? '').includes(config.nonce) }, transcript: { successfulReads: transcript.successfulToolCallCounts.read, finalHasNonce: String(transcript.finalText ?? '').includes(config.nonce) }, visibleBeforeRead: historyEvidence.visibleBeforeRead.length, finalRepliesAfterRead: historyEvidence.finals.length, outboundFinals: outboundMessages.length }, null, 2)" |
0 commit comments