diff --git a/tests/runner-e2e/product-journeys/saved-skill-use-evidence.test.ts b/tests/runner-e2e/product-journeys/saved-skill-use-evidence.test.ts index 7db2180219a..ed6009dde54 100644 --- a/tests/runner-e2e/product-journeys/saved-skill-use-evidence.test.ts +++ b/tests/runner-e2e/product-journeys/saved-skill-use-evidence.test.ts @@ -136,6 +136,17 @@ describe("saved skill use evidence", () => { expect(savedSkillReadReceipts(assignedReadEvents(), binding, fixture, runtimeName, { ...assignment, key: "foreign" })).toEqual([]); expect(savedSkillReadReceipts(assignedReadEvents(), binding, fixture, runtimeName, { ...assignment, versionId: "foreign" })).toEqual([]); }); + it.each(["1: ", "2|", "3→"])("preserves literal assigned-source prefixes %s", prefix => { + const procedure = `${prefix}${fixture.procedure}`; + const sourceFixture = { ...fixture, procedure, markdown: fixture.markdown.replace(fixture.procedure, procedure) }; + const events = assignedReadEvents(); + changeAssignedResult(events, r => { + r.text = sourceFixture.markdown; r.byteSize = Buffer.byteLength(r.text); + r.sha256 = createHash("sha256").update(r.text).digest("hex"); + }); + const payload = events[1]!.payload.prpEvent.payload; payload.outputBytes = Buffer.byteLength(payload.output); + expect(savedSkillReadReceipts(events, binding, sourceFixture, runtimeName, assignment)).toHaveLength(1); + }); it.each([{ kind: "codex" }, { kind: "acpx", agent: "cursor" }, { kind: "cursor_cloud" }])( "binds the semantic source to common native skill entries for %j while preserving the OpenCode file guard", provider => { const { skill } = source(); @@ -271,6 +282,16 @@ describe("saved skill use evidence", () => { expect(savedSkillReadReceipts(wrong, binding, fixture)).toEqual([]); } }); + it.each(["1: ", "2|", "3→"])("preserves literal provider skill procedure prefixes %s", prefix => { + const procedure = `${prefix}${fixture.procedure}`; + const sourceFixture = { ...fixture, procedure, markdown: fixture.markdown.replace(fixture.procedure, procedure) }; + const events = ["pending", "completed"].map((status, index) => { + const event = canonicalProviderEventsFromOpenCodePart({ id: "load-skill", type: "tool", tool: "skill", + state: { status, output: `\n${procedure}\n` } })[0]!; + return native(index + 1, event.eventType, event.payload); + }); + expect(savedSkillReadReceipts(events, binding, sourceFixture)).toHaveLength(1); + }); it("allows known file-reader line prefixes without treating partial output as a complete read", () => { const events = readEvents(); events[1]!.payload.prpEvent.payload.output = fixture.markdown.split("\n").map((line, i) => `${i + 1}: ${line}`).join("\n"); diff --git a/tests/runner-e2e/product-journeys/saved-skill-use-evidence.ts b/tests/runner-e2e/product-journeys/saved-skill-use-evidence.ts index 1ef4ce9ce1f..1075c0bb3a8 100644 --- a/tests/runner-e2e/product-journeys/saved-skill-use-evidence.ts +++ b/tests/runner-e2e/product-journeys/saved-skill-use-evidence.ts @@ -97,29 +97,55 @@ function assignedSkillReadText(output: string, binding: NativePrpRunBinding, fix } catch { return undefined; } } +/** Shared run admission; provider-specific receipts validate their own payloads. */ +function admittedNativeSkill(binding: NativePrpRunBinding, fixture: SavedSkillFixture, + assignment: SavedSkillReadAssignment | undefined, run: Row | undefined): Row | undefined { + const input = run?.runnerProfileJson?.nativeExecutionInput; + const skills = Array.isArray(input?.runtimeContext?.skills) + ? input.runtimeContext.skills.filter((skill: Row) => skill.key === assignment?.key) : []; + if (!run || run.runtimeMode !== "native" + || run.id !== binding.runId || run.companyId !== binding.companyId || run.agentId !== binding.agentId + || typeof run.runnerInstanceId !== "string" || !run.runnerInstanceId.trim() + || typeof run.nativeSessionId !== "string" || !run.nativeSessionId.trim() + || !assignment || assignment.key !== `company/${binding.companyId}/${fixture.slug}` + || typeof assignment.versionId !== "string" || !assignment.versionId.trim() + || skills.length !== 1 || skills[0].versionId !== assignment.versionId + || run.contextSnapshot?.skillSelectionSnapshot !== true + || run.contextSnapshot?.skillVersions?.[assignment.key] !== assignment.versionId) return undefined; + return skills[0]; +} + +/** Unknown-operation OpenCode loaders require one complete, ordered native call. */ +function nativeOpenCodeSkillCall(row: Row, bound: Row[], binding: NativePrpRunBinding, fixture: SavedSkillFixture, + assignment: SavedSkillReadAssignment | undefined, run: Row | undefined): Row | undefined { + const skill = admittedNativeSkill(binding, fixture, assignment, run); + const event = row.payload.prpEvent, payload = event.payload; + if (!skill || run?.runnerProfileJson?.nativeExecutionInput?.provider?.kind !== "opencode" + || event.sourceInstanceId !== run.runnerInstanceId || event.normalizedSessionId !== run.nativeSessionId + || typeof event.itemId !== "string" || !event.itemId.trim() || !Number.isSafeInteger(row.seq) + || payload.operation !== "unknown" || payload.readOnly !== false || payload.target != null) return undefined; + const pair = bound.filter(candidate => ["tool.execution.started", "tool.execution.completed"].includes(candidate.payload.prpEvent.eventType) + && candidate.payload.prpEvent.payload?.executionId === payload.executionId); + const start = pair.find(candidate => candidate.payload.prpEvent.eventType === "tool.execution.started"); + const initial = start?.payload.prpEvent, p = initial?.payload; + if (pair.length !== 2 || !start || !Number.isSafeInteger(start.seq) || start.seq >= row.seq + || initial.sourceSeq >= event.sourceSeq || initial.sourceInstanceId !== event.sourceInstanceId + || initial.normalizedSessionId !== event.normalizedSessionId || initial.turnId !== event.turnId || initial.itemId !== event.itemId + || p?.schema !== payload.schema || p.name !== payload.name || p.transport !== payload.transport + || p.operation !== payload.operation || p.status !== "running" || p.readOnly !== false || p.target != null) return undefined; + return skill; +} + /** The native library loader returns the immutable source through Core's read API. * OpenCode's proxy reports unknown/false for this tool too. Those hints alone * prove nothing: require the admitted version, exact saved source and paired call. */ function nativeLibrarySkillLoad(row: Row, bound: Row[], binding: NativePrpRunBinding, fixture: SavedSkillFixture, assignment: SavedSkillReadAssignment | undefined, run: Row | undefined): string | undefined { - const event = row.payload.prpEvent, payload = event.payload, input = run?.runnerProfileJson?.nativeExecutionInput; - const skills = Array.isArray(input?.runtimeContext?.skills) - ? input.runtimeContext.skills.filter((skill: Row) => skill.key === assignment?.key) : []; - if (!run || run.runtimeMode !== "native" || input?.provider?.kind !== "opencode" - || run.id !== binding.runId || run.companyId !== binding.companyId || run.agentId !== binding.agentId - || typeof run.runnerInstanceId !== "string" || !run.runnerInstanceId.trim() - || typeof run.nativeSessionId !== "string" || !run.nativeSessionId.trim() - || event.sourceInstanceId !== run.runnerInstanceId || event.normalizedSessionId !== run.nativeSessionId - || typeof event.itemId !== "string" || !event.itemId.trim() || !Number.isSafeInteger(row.seq) - || !assignment || assignment.key !== `company/${binding.companyId}/${fixture.slug}` - || typeof assignment.versionId !== "string" || !assignment.versionId.trim() - || typeof assignment.skillId !== "string" || !assignment.skillId.trim() - || skills.length !== 1 || skills[0].versionId !== assignment.versionId - || run.contextSnapshot?.skillSelectionSnapshot !== true - || run.contextSnapshot?.skillVersions?.[assignment.key] !== assignment.versionId - || payload.name !== "paperclip_load_skill" || payload.operation !== "unknown" - || payload.readOnly !== false || payload.target != null) return undefined; + const payload = row.payload.prpEvent.payload; + if (!assignment || typeof assignment.skillId !== "string" || !assignment.skillId.trim() + || payload.name !== "paperclip_load_skill" + || !nativeOpenCodeSkillCall(row, bound, binding, fixture, assignment, run)) return undefined; try { const wrapper = JSON.parse(payload.output); if (wrapper?.type !== "inputText" || typeof wrapper.text !== "string" @@ -133,15 +159,6 @@ function nativeLibrarySkillLoad(row: Row, bound: Row[], binding: NativePrpRunBin || result.outcome !== undefined || result.fileCount !== 1 || result.nextFileOffset !== null || !Array.isArray(result.files) || result.files.length !== 1 || result.files[0]?.path !== "SKILL.md" || result.files[0]?.kind !== "skill") return undefined; - const pair = bound.filter(candidate => ["tool.execution.started", "tool.execution.completed"].includes(candidate.payload.prpEvent.eventType) - && candidate.payload.prpEvent.payload?.executionId === payload.executionId); - const start = pair.find(candidate => candidate.payload.prpEvent.eventType === "tool.execution.started"); - const initial = start?.payload.prpEvent, p = initial?.payload; - if (pair.length !== 2 || !start || !Number.isSafeInteger(start.seq) || start.seq >= row.seq - || initial.sourceSeq >= event.sourceSeq || initial.sourceInstanceId !== event.sourceInstanceId - || initial.normalizedSessionId !== event.normalizedSessionId || initial.turnId !== event.turnId || initial.itemId !== event.itemId - || p?.schema !== payload.schema || p.name !== payload.name || p.transport !== payload.transport - || p.operation !== payload.operation || p.status !== "running" || p.readOnly !== false || p.target != null) return undefined; return result.text; } catch { return undefined; } } @@ -152,22 +169,11 @@ function nativeLibrarySkillLoad(row: Row, bound: Row[], binding: NativePrpRunBin */ function nativeOpenCodeSkillLoad(row: Row, bound: Row[], binding: NativePrpRunBinding, fixture: SavedSkillFixture, runtimeName: string | undefined, assignment: SavedSkillReadAssignment | undefined, run: Row | undefined): boolean { - const event = row.payload.prpEvent, payload = event.payload, input = run?.runnerProfileJson?.nativeExecutionInput; - const skills = Array.isArray(input?.runtimeContext?.skills) - ? input.runtimeContext.skills.filter((skill: Row) => skill.key === assignment?.key) : []; - if (!run || run.runtimeMode !== "native" || input?.provider?.kind !== "opencode" - || run.id !== binding.runId || run.companyId !== binding.companyId || run.agentId !== binding.agentId - || typeof run.runnerInstanceId !== "string" || !run.runnerInstanceId.trim() - || typeof run.nativeSessionId !== "string" || !run.nativeSessionId.trim() - || event.sourceInstanceId !== run.runnerInstanceId || event.normalizedSessionId !== run.nativeSessionId - || typeof event.itemId !== "string" || !event.itemId.trim() || !Number.isSafeInteger(row.seq) - || !assignment || assignment.key !== `company/${binding.companyId}/${fixture.slug}` - || typeof assignment.versionId !== "string" || !assignment.versionId.trim() - || skills.length !== 1 || skills[0].versionId !== assignment.versionId || skills[0].runtimeName !== runtimeName - || run.contextSnapshot?.skillSelectionSnapshot !== true - || run.contextSnapshot?.skillVersions?.[assignment.key] !== assignment.versionId + const payload = row.payload.prpEvent.payload; + const skill = nativeOpenCodeSkillCall(row, bound, binding, fixture, assignment, run); + if (!skill || skill.runtimeName !== runtimeName || typeof runtimeName !== "string" || !/^[a-z0-9][a-z0-9_-]*$/i.test(runtimeName) - || payload.name !== "skill" || payload.operation !== "unknown" || payload.readOnly !== false || payload.target !== null) return false; + || payload.name !== "skill") return false; const wrapper = /^\r\n]+)">\n# Skill: ([^\r\n]+)\n\n([\s\S]*?)\n\nBase directory for this skill: ([^\r\n]+)\n([\s\S]*)\n<\/skill_content>\s*$/.exec(payload.output); if (!wrapper || wrapper[1] !== fixture.slug || wrapper[2] !== fixture.slug || wrapper[3] !== fixture.procedure.trim() || !/\s*<\/skill_files>/.test(wrapper[5]!) @@ -180,13 +186,7 @@ function nativeOpenCodeSkillLoad(row: Row, bound: Row[], binding: NativePrpRunBi if (!path.startsWith("/") || /[\\\u0000-\u001f\u007f?#]/.test(path) || /%[a-f0-9]{2}/i.test(path) || path.split("/").some(part => part === "." || part === "..") || !path.replace(/\/$/, "").endsWith(`/.claude/skills/${runtimeName}`)) return false; - const pair = bound.filter(candidate => ["tool.execution.started", "tool.execution.completed"].includes(candidate.payload.prpEvent.eventType) - && candidate.payload.prpEvent.payload?.executionId === payload.executionId); - const start = pair.find(candidate => candidate.payload.prpEvent.eventType === "tool.execution.started"); - const initial = start?.payload.prpEvent, initialPayload = initial?.payload; - return Boolean(pair.length === 2 && start && Number.isSafeInteger(start.seq) && start.seq < row.seq - && initial.sourceSeq < event.sourceSeq && initial.normalizedSessionId === event.normalizedSessionId && initial.itemId === event.itemId - && initialPayload?.readOnly === false && initialPayload.target === null); + return true; } // Core091 provider_bridge.rs hashes UTF-8 JSON with object keys ordered by UTF-16. @@ -204,17 +204,8 @@ function semanticDigest(value: unknown): string { */ function codexAssignedSkillReceipts(bound: Row[], binding: NativePrpRunBinding, fixture: SavedSkillFixture, assignment: SavedSkillReadAssignment | undefined, run: Row | undefined) { - const input = run?.runnerProfileJson?.nativeExecutionInput; - const skills = Array.isArray(input?.runtimeContext?.skills) - ? input.runtimeContext.skills.filter((skill: Row) => skill.key === assignment?.key) : []; - if (!run || run.runtimeMode !== "native" || input?.provider?.kind !== "codex" - || run.id !== binding.runId || run.companyId !== binding.companyId || run.agentId !== binding.agentId - || typeof run.runnerInstanceId !== "string" || !run.runnerInstanceId.trim() - || typeof run.nativeSessionId !== "string" || !run.nativeSessionId.trim() - || !assignment || assignment.key !== `company/${binding.companyId}/${fixture.slug}` || !assignment.versionId - || skills.length !== 1 || skills[0].versionId !== assignment.versionId - || run.contextSnapshot?.skillSelectionSnapshot !== true - || run.contextSnapshot?.skillVersions?.[assignment.key] !== assignment.versionId + const skill = admittedNativeSkill(binding, fixture, assignment, run); + if (!skill || !run || !assignment || run.runnerProfileJson?.nativeExecutionInput?.provider?.kind !== "codex" || !fixture.markdown.includes(fixture.procedure.trim()) || !fixture.markdown.includes(fixture.expected.reference)) return []; const expected = { skill: assignment.key, versionId: assignment.versionId, path: "SKILL.md", text: fixture.markdown, sha256: createHash("sha256").update(fixture.markdown, "utf8").digest("hex"), @@ -244,8 +235,8 @@ function codexAssignedSkillReceipts(bound: Row[], binding: NativePrpRunBinding, const initial = semantic(start)!, args = initial.input; if (!args || typeof args !== "object" || Array.isArray(args) || Object.keys(args).some(key => !["skill", "path", "offset"].includes(key)) - || !(args.skill === assignment.key || typeof skills[0].runtimeName === "string" - && skills[0].runtimeName.trim() && args.skill === skills[0].runtimeName) + || !(args.skill === assignment.key || typeof skill.runtimeName === "string" + && skill.runtimeName.trim() && args.skill === skill.runtimeName) || Object.hasOwn(args, "path") && args.path !== "SKILL.md" || Object.hasOwn(args, "offset") && args.offset !== 0 || !content(initial, semanticDigest(args))) return []; // Duplicates, reconciled alternatives and foreign same-call carriers cannot supply a second interpretation. @@ -304,7 +295,10 @@ export function savedSkillReadReceipts(events: Row[], binding: NativePrpRunBindi const libraryText = nativeLibrarySkillLoad(row, bound, binding, fixture, assignment, run); if (!reading && !loading && !nativeLoading && !wrappedRead && assignedText === undefined && libraryText === undefined) return false; // File readers may prefix each source line with its line number. - const output = (libraryText ?? assignedText ?? (wrappedRead ? wrapped![2]! : payload.output)).split("\n").map((line: string) => line.replace(/^\s*\d+(?:[:|]|→)\s?/, "")).join("\n"); + const readerOutput = wrappedRead ? wrapped![2]! : payload.output; + const output = libraryText ?? assignedText ?? ((reading || wrappedRead) + ? readerOutput.split("\n").map((line: string) => line.replace(/^\s*\d+(?:[:|]|→)\s?/, "")).join("\n") + : payload.output); if (!output.includes(fixture.procedure.trim())) return false; return bound.some(start => { const initial = start.payload.prpEvent, p = initial.payload; diff --git a/tests/runner-e2e/product-journeys/saved-skill-use-library-loader.test.ts b/tests/runner-e2e/product-journeys/saved-skill-use-library-loader.test.ts index a016095df1d..a27eea60eed 100644 --- a/tests/runner-e2e/product-journeys/saved-skill-use-library-loader.test.ts +++ b/tests/runner-e2e/product-journeys/saved-skill-use-library-loader.test.ts @@ -7,18 +7,18 @@ const fixture = savedSkillFixture("library-loader", "62ff548dfec748da8fef6d5fbf6 const binding = { companyId: "company", agentId: "agent", runId: "run" }; const key = `company/${binding.companyId}/${fixture.slug}`; -function observations() { +function observations(sourceFixture = fixture) { const skill = { id: "skill", key, currentVersionId: "version" }; const assignments = { entries: [{ key, versionId: "version" }] }; const run: Row = { ...binding, id: binding.runId, runtimeMode: "native", runnerInstanceId: "runner", nativeSessionId: "session", contextSnapshot: { skillSelectionSnapshot: true, skillVersions: { [key]: "version" } }, runnerProfileJson: { nativeExecutionInput: { provider: { kind: "opencode" }, runtimeContext: { skills: assignments.entries } } } }; // Shape of a retained native library load. All identities and source bytes are synthetic. - const result: Row = { id: skill.id, skillId: skill.id, key, name: fixture.name, slug: fixture.slug, + const result: Row = { id: skill.id, skillId: skill.id, key, name: sourceFixture.name, slug: sourceFixture.slug, versionId: "version", sourceType: "managed", trustLevel: "markdown_only", compatibility: "compatible", description: "Make a receipt using this saved procedure.", instruction: "Use this versionId for further pages and referenced files.", - path: "SKILL.md", text: fixture.markdown, byteSize: Buffer.byteLength(fixture.markdown), - sha256: createHash("sha256").update(fixture.markdown).digest("hex"), nextOffset: null, + path: "SKILL.md", text: sourceFixture.markdown, byteSize: Buffer.byteLength(sourceFixture.markdown), + sha256: createHash("sha256").update(sourceFixture.markdown).digest("hex"), nextOffset: null, files: [{ kind: "skill", path: "SKILL.md" }], fileCount: 1, nextFileOffset: null }; const output = JSON.stringify({ type: "inputText", text: JSON.stringify(result) }); const events: Row[] = ["started", "progressed", "completed"].map((phase, index) => { @@ -37,7 +37,7 @@ function observations() { } type Observations = ReturnType; const completed = (v: Observations) => v.events.find(row => row.eventType === "tool.execution.completed")!.payload.prpEvent.payload; -const read = (v: Observations) => savedSkillReadReceipts(v.events, binding, fixture, undefined, +const read = (v: Observations, sourceFixture = fixture) => savedSkillReadReceipts(v.events, binding, sourceFixture, undefined, savedSkillReadAssignment(v.skill, v.assignments, v.run), v.run); function result(v: Observations, change: (r: Row) => void) { const p = completed(v), wrapper = JSON.parse(p.output), value = JSON.parse(wrapper.text); @@ -53,6 +53,11 @@ describe("saved-skill native library load", () => { for (const row of v.events) delete row.payload.prpEvent.payload.target; expect(read(v)).toHaveLength(1); }); + it.each(["1: ", "2|", "3→"])("preserves literal source prefixes %s", prefix => { + const procedure = `${prefix}${fixture.procedure}`; + const sourceFixture = { ...fixture, procedure, markdown: fixture.markdown.replace(fixture.procedure, procedure) }; + expect(read(observations(sourceFixture), sourceFixture)).toHaveLength(1); + }); it.each([ ["skill id", (v: Observations) => result(v, r => { r.skillId = "foreign"; })], ["id alias", (v: Observations) => result(v, r => { r.id = "foreign"; })], diff --git a/tests/runner-e2e/product-journeys/saved-skill-use-native-loader.test.ts b/tests/runner-e2e/product-journeys/saved-skill-use-native-loader.test.ts index 925ad58b5d9..fc1f4700a42 100644 --- a/tests/runner-e2e/product-journeys/saved-skill-use-native-loader.test.ts +++ b/tests/runner-e2e/product-journeys/saved-skill-use-native-loader.test.ts @@ -42,7 +42,7 @@ function observations() { return { skill, assignments, run, events, notifications }; } type Observations = ReturnType; -const read = (value: Observations) => savedSkillReadReceipts(value.events, binding, fixture, +const read = (value: Observations, sourceFixture = fixture) => savedSkillReadReceipts(value.events, binding, sourceFixture, savedSkillReadRuntimeName(value.skill, value.assignments, value.run), savedSkillReadAssignment(value.skill, value.assignments, value.run), value.run); const completed = (v: Observations) => v.events[1]!.payload.prpEvent.payload; @@ -63,6 +63,18 @@ describe("saved-skill native OpenCode builtin loader", () => { const value = observations(); output(value, text => text.replace("/workspace/", "/work space/") .replace(`${runtimeName}\n`, `${runtimeName}/\n`)); expect(read(value)).toHaveLength(1); }); + it.each(["1: ", "2|", "3→"])("preserves literal native skill procedure prefixes %s", prefix => { + const procedure = `${prefix}${fixture.procedure}`; + const sourceFixture = { ...fixture, procedure, markdown: fixture.markdown.replace(fixture.procedure, procedure) }; + const value = observations(); + output(value, text => text.replace(fixture.procedure.trim(), procedure.trim())); + expect(read(value, sourceFixture)).toHaveLength(1); + }); + it("accepts omitted null targets in retained native loader events", () => { + const value = observations(); + for (const row of value.events) delete row.payload.prpEvent.payload.target; + expect(read(value)).toHaveLength(1); + }); it.each([ ["missing start", (v: Observations) => { v.events.shift(); }], ["missing completion", (v: Observations) => { v.events.pop(); }], diff --git a/tests/runner-e2e/product-journeys/saved-skill-use.md b/tests/runner-e2e/product-journeys/saved-skill-use.md index c18bf45e696..8c8ac48dd03 100644 --- a/tests/runner-e2e/product-journeys/saved-skill-use.md +++ b/tests/runner-e2e/product-journeys/saved-skill-use.md @@ -89,6 +89,10 @@ must be null. The actual run's admitted assignment, runner, session, turn, item and one ordered start/completion pair must agree. Progress events can occur between the pair. Duplicate start/completion events fail. This path does not change the independent saved-output, revision authorship, reload or source checks. +Verified library and assigned-skill text retains literal numeric line prefixes. +Only file-reader output has presentation line numbers removed. Native loaders +share run admission and ordered call checks; Codex uses the same run admission +with its own semantic receipt checks. Codex's dynamic tool display is not first-party authority. Its raw name, optional namespace or read-only hint cannot qualify a read. For Codex, the