Skip to content

fix(ai): correct mock array result indexing#15154

Merged
lgrammel merged 1 commit into
vercel:mainfrom
Kage18:fix-mock-array-result-index
May 13, 2026
Merged

fix(ai): correct mock array result indexing#15154
lgrammel merged 1 commit into
vercel:mainfrom
Kage18:fix-mock-array-result-index

Conversation

@Kage18

@Kage18 Kage18 commented May 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #15141.

  • corrects array-backed indexing in MockLanguageModelV3 / MockLanguageModelV4 for doGenerate and doStream
  • corrects the same off-by-one in MockEmbeddingModelV3 / MockEmbeddingModelV4 for doEmbed
  • adds focused tests that verify the first call returns the first scripted result and the second call returns the second scripted result
  • adds a patch changeset for ai

Verification

  • pnpm --filter ai exec vitest --config vitest.node.config.js --run src/test/mock-language-model.test.ts src/test/mock-embedding-model.test.ts
  • pnpm --filter ai type-check
  • pnpm type-check:full
  • pnpm exec ultracite check packages/ai/src/test/mock-language-model.test.ts packages/ai/src/test/mock-embedding-model.test.ts packages/ai/src/test/mock-language-model-v3.ts packages/ai/src/test/mock-language-model-v4.ts packages/ai/src/test/mock-embedding-model-v3.ts packages/ai/src/test/mock-embedding-model-v4.ts .changeset/slow-mocks-learn.md

Note: local verification ran on Node v25.9.0, so pnpm emitted the repo's unsupported-engine warning. The commands still passed.

@Kage18

Kage18 commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

Kage usage for this PR:

This PR fixed #15141 by correcting array-backed mock result indexing across the AI SDK mock language and embedding models.

How Kage helped:

  • Recalled the packages/ai test-helper area instead of making me rediscover the mock-model layout from the whole monorepo.
  • Kept the affected implementation/test paths together: mock-language-model-v2.ts, mock-language-model-v3.ts, mock-language-model-v4.ts, mock-embedding-model-v3.ts, mock-embedding-model-v4.ts, and the focused mock tests.
  • Made the reusable lesson explicit: array-backed mocked results should return the first scripted result on the first call and advance after the result is consumed, not before.
  • Captured the verification path so a future agent touching these mocks knows to run the mock language-model and embedding-model tests, plus type-checking.

Why this matters for future sessions:
A fresh agent working in the mock test helpers can recall that this was an off-by-one state-advance bug, see which model versions were affected, and avoid reintroducing different V2/V3/V4 behavior.

Full contribution summary and Kage metrics: kage-core/Kage#8

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

MockLanguageModelV2 has an off-by-one bug when indexing into doGenerate/doStream arrays, causing the first element to be skipped and eventual out-of-bounds access.

Fix on Vercel

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

MockLanguageModelV2 has an off-by-one bug when indexing into doGenerate/doStream arrays, causing the first element to be skipped and eventual out-of-bounds access.

Fix on Vercel

@Kage18
Kage18 force-pushed the fix-mock-array-result-index branch from 6bb0deb to 3234eb5 Compare May 11, 2026 06:49
@lgrammel

Copy link
Copy Markdown
Collaborator

Please sign your commits, otherwise I cannot merge the PR https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits

@Kage18
Kage18 force-pushed the fix-mock-array-result-index branch from 3234eb5 to 287b5d4 Compare May 11, 2026 16:26
@Kage18

Kage18 commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

@lgrammel
Done, I amended the PR commit with SSH signing and force-pushed the branch. GitHub now reports the commit as verified.

@lgrammel

Copy link
Copy Markdown
Collaborator

tests are now failing

@lgrammel
lgrammel self-requested a review May 12, 2026 10:42
@Kage18
Kage18 force-pushed the fix-mock-array-result-index branch from 9a47ab8 to 56a0115 Compare May 12, 2026 13:48
@Kage18

Kage18 commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

I checked the failed run. The failing job was @ai-sdk/codemod#test, specifically packages/codemod/src/test/remove-await-fn.test.ts timing out after 15s on Node 20. The touched packages/ai mock tests passed in that same run, so this does not look related to this PR's diff.\n\nI rebased the branch onto latest main and force-pushed a clean signed commit: 56a0115d25f0cdb06afc9f3f3489652d5f70164d. GitHub reports the commit signature as verified.\n\nThe new CI / changeset workflow runs are currently marked action_required with no jobs created, so they likely need maintainer approval to run from the fork.

@Kage18
Kage18 force-pushed the fix-mock-array-result-index branch from 56a0115 to ace15da Compare May 12, 2026 15:17
@lgrammel
lgrammel merged commit 2605e5f into vercel:main May 13, 2026
18 of 19 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Published in:

Package Version
ai 7.0.0-canary.134
@ai-sdk/angular 3.0.0-canary.134
@ai-sdk/devtools 1.0.0-canary.22
@ai-sdk/langchain 3.0.0-canary.134
@ai-sdk/llamaindex 3.0.0-canary.134
@ai-sdk/otel 1.0.0-canary.80
@ai-sdk/react 4.0.0-canary.135
@ai-sdk/rsc 3.0.0-canary.135
@ai-sdk/svelte 5.0.0-canary.134
@ai-sdk/vue 4.0.0-canary.134
@ai-sdk/workflow 1.0.0-canary.51

gr2m pushed a commit that referenced this pull request Jun 5, 2026
## Summary

- corrects array-backed indexing in `MockLanguageModelV3` /
`MockLanguageModelV4` for `doGenerate` and `doStream`
- corrects the same off-by-one in `MockEmbeddingModelV3` /
`MockEmbeddingModelV4` for `doEmbed`
- adds focused tests that verify the first call returns the first
scripted result and the second call returns the second scripted result
- adds a patch changeset for `ai`

## Verification

- `pnpm --filter ai exec vitest --config vitest.node.config.js --run
src/test/mock-language-model.test.ts
src/test/mock-embedding-model.test.ts`
- `pnpm --filter ai type-check`
- `pnpm type-check:full`
- `pnpm exec ultracite check
packages/ai/src/test/mock-language-model.test.ts
packages/ai/src/test/mock-embedding-model.test.ts
packages/ai/src/test/mock-language-model-v3.ts
packages/ai/src/test/mock-language-model-v4.ts
packages/ai/src/test/mock-embedding-model-v3.ts
packages/ai/src/test/mock-embedding-model-v4.ts
.changeset/slow-mocks-learn.md`

Note: local verification ran on Node `v25.9.0`, so pnpm emitted the
repo's unsupported-engine warning. The commands still passed.

## Related Issues
Fixes #15141
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: MockLanguageModelV3 / V4 array form for doGenerate/doStream skips first entry (off-by-one)

2 participants