Merge branch 'codex/invariant-package-registration-gate' into codex/package-invariant-checks

# Conflicts:
#	.agents/notes/implemented/architecture/2026-07-19-package-invariant-runtime-contracts.i18n.yaml
#	.agents/notes/implemented/architecture/2026-07-19-package-invariant-runtime-contracts.md
#	.agents/notes/implemented/architecture/2026-07-19-package-invariant-runtime-contracts.zh.md
#	.agents/notes/implemented/architecture/2026-07-19-package-owned-invariant-service.i18n.yaml
#	docs/rfc/INDEX.md
#	packages/AGENTS.md
This commit is contained in:
Tianyi Cui
2026-07-20 00:54:28 +08:00
683 changed files with 7058 additions and 2430 deletions
+20 -4
View File
@@ -63,15 +63,31 @@ Unit tests use pi-ai catalog models redirected to local mock servers and cover p
### Provider request through pi-ai
**What the model sees**: The selected catalog model receives `GenerateOptions.system`, history, tools, and sampling fields supported by pi-ai's common streaming API. This package adds no prompt prose. Provider-native replay metadata is restored only when the adapter validates it for the historical content.
#### What the model sees
**Token effect**: Provider tokenization governs exact input. Conversion adds no model-visible text; replay metadata may let a native API reuse provider-side state.
The selected catalog model receives `GenerateOptions.system`, history, tools, and sampling fields supported by pi-ai's common streaming API. This package adds no prompt prose. Provider-native replay metadata is restored only when the adapter validates it for the historical content.
#### Token effect
Provider tokenization governs exact input. Conversion adds no model-visible text; replay metadata may let a native API reuse provider-side state.
#### KV Cache effect
Conversion preserves logical request order without adding text, while the selected provider's serialization and replay state determine reuse. Changing adapter instance, provider, model, or any upstream request token may prevent reuse from the first difference.
### Provider response
**What the model sees**: pi-ai events become harness reasoning, text, tool-call, usage, and finish chunks. Parsed tool arguments cross the harness boundary as raw JSON strings.
#### What the model sees
**Token effect**: Generated content affects later inputs only after the loop records it. pi-ai folds reasoning tokens into output usage when the provider does not report them separately.
pi-ai events become harness reasoning, text, tool-call, usage, and finish chunks. Parsed tool arguments cross the harness boundary as raw JSON strings.
#### Token effect
Generated content affects later inputs only after the loop records it. pi-ai folds reasoning tokens into output usage when the provider does not report them separately.
#### KV Cache effect
Recorded response content appends to the next request and does not invalidate its earlier reusable prefix. Unrecorded transport metadata and usage accounting do not affect cache identity.
## Known Limitations and Deferred Work
@@ -168,6 +168,26 @@ describe('PiAiAdapter provider routing', () => {
expect(server.paths).toEqual(['/v1/responses'])
})
it('uses OpenAI Responses against an Azure project v1 path with its API key header', async () => {
const server = await mockServer([{ status: 401, body: JSON.stringify({ error: { message: 'expected mock failure' } }) }])
const ctx = new Context()
await ctx.plugin(LlmService)
await ctx.plugin(LlmPiAi, {
providers: [{
provider: 'openai',
apiKey: 'test-key',
baseURL: `${server.url}/api/projects/openai/openai/v1`,
headers: { 'api-key': 'test-key', Authorization: '' },
maxRetries: 0,
}],
})
const result = await assemble(ctx, { provider: 'openai', model: 'gpt-5.5', messages: [] })
expect(result.finish.kind).toBe('error')
expect(server.paths).toEqual(['/api/projects/openai/openai/v1/responses'])
expect(server.headers[0]?.['api-key']).toBe('test-key')
expect(server.headers[0]?.authorization).toBe('')
})
it.each([
[401, 'AUTH'],
[400, 'INVALID_REQUEST'],
@@ -0,0 +1,163 @@
import { afterEach, describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import LlmService, { CallId } from '@deepseek-ai/dsh-llm'
import type { Message, ToolSchema } from '@deepseek-ai/dsh-llm'
import * as LlmPiAi from '@deepseek-ai/dsh-llm-pi-ai'
import type { PiAiReplayState } from '../src/replay.ts'
import { assemble, type AssembledResult } from './assemble.ts'
interface ProviderCase {
provider: 'openai' | 'anthropic'
api: 'openai-responses' | 'anthropic-messages'
model: string
apiKey?: string
baseURL?: string
headers?: Record<string, string>
}
const openAIBaseURL = process.env.DSH_PI_AI_OPENAI_BASE_URL
const azureOpenAIKey = process.env.AZURE_OPENAI_API_KEY
const providerCases: ProviderCase[] = [
{
provider: 'openai',
api: 'openai-responses',
model: process.env.DSH_PI_AI_OPENAI_MODEL ?? 'gpt-5.5',
...azureOpenAIKey
? { apiKey: azureOpenAIKey, headers: { 'api-key': azureOpenAIKey, Authorization: '' } }
: {},
...openAIBaseURL ? { baseURL: openAIBaseURL } : {},
},
{
provider: 'anthropic',
api: 'anthropic-messages',
model: process.env.DSH_PI_AI_ANTHROPIC_MODEL ?? 'claude-opus-4-8',
...process.env.ANTHROPIC_API_KEY ? { apiKey: process.env.ANTHROPIC_API_KEY } : {},
},
]
const contexts: Context[] = []
async function harness(): Promise<Context> {
const ctx = new Context()
contexts.push(ctx)
await ctx.plugin(LlmService)
await ctx.plugin(LlmPiAi, {
providers: providerCases.map(profile => ({
provider: profile.provider,
...profile.apiKey === undefined ? {} : { apiKey: profile.apiKey },
...profile.baseURL === undefined ? {} : { baseURL: profile.baseURL },
...profile.headers === undefined ? {} : { headers: profile.headers },
})),
})
return ctx
}
afterEach(async () => {
await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
})
function ask(text: string): Message[] {
return [{ role: 'user', content: [{ type: 'text', text }] }]
}
function textOf(result: AssembledResult): string {
return result.message.content
.filter(block => block.type === 'text')
.map(block => block.text)
.join('')
}
function expectFinish(result: AssembledResult, expected: 'stop' | 'tool-calls'): void {
if (result.finish.kind === 'error') {
throw new Error(`provider request failed (${result.finish.code ?? 'unknown'}): ${result.finish.message}`)
}
expect(result.finish.kind).toBe(expected)
}
function expectNativeReplay(result: AssembledResult, profile: ProviderCase): PiAiReplayState {
const replayState = result.message.provenance?.replayState
expect(replayState).toMatchObject({
kind: 'pi-ai',
version: 1,
api: profile.api,
provider: profile.provider,
model: profile.model,
})
return replayState as PiAiReplayState
}
const lookupTool: ToolSchema = {
name: 'lookup_code',
description: 'Look up the word represented by a short code.',
parameters: {
type: 'object',
properties: { code: { type: 'string', description: 'The code to look up.' } },
required: ['code'],
},
}
for (const profile of providerCases) {
describe.skipIf(profile.apiKey === undefined)(
`llm-pi-ai ${profile.provider} e2e (${profile.api})`,
() => {
it('streams text with usage and native replay metadata', async () => {
const ctx = await harness()
const result = await assemble(ctx, {
provider: profile.provider,
model: profile.model,
messages: ask('Reply with exactly the word: pong'),
maxTokens: 1024,
})
expectFinish(result, 'stop')
expect(textOf(result).toLowerCase()).toContain('pong')
expect(result.usage?.inputTokens).toBeGreaterThan(0)
expect(result.usage?.outputTokens).toBeGreaterThan(0)
expect(expectNativeReplay(result, profile).stopReason).toBe('stop')
})
it('round-trips a tool call with provider-native replay metadata', async () => {
const ctx = await harness()
const prompt = ask('Use lookup_code with code "blue". Do not answer without calling the tool.')
const first = await assemble(ctx, {
provider: profile.provider,
model: profile.model,
messages: prompt,
tools: [lookupTool],
maxTokens: 2048,
})
expectFinish(first, 'tool-calls')
const call = first.message.content.find(block => block.type === 'tool-call')
expect(call).toBeDefined()
expect(call!.name).toBe('lookup_code')
expect(JSON.parse(call!.arguments)).toMatchObject({ code: 'blue' })
expect(expectNativeReplay(first, profile).stopReason).toBe('toolUse')
const second = await assemble(ctx, {
provider: profile.provider,
model: profile.model,
messages: [
...prompt,
first.message,
{
role: 'user',
content: [{
type: 'tool-result',
toolCallId: CallId(call!.id),
content: [{ type: 'text', text: 'The code blue means ocean.' }],
}],
},
],
tools: [lookupTool],
maxTokens: 2048,
})
expectFinish(second, 'stop')
expect(textOf(second).toLowerCase()).toContain('ocean')
expect(expectNativeReplay(second, profile).stopReason).toBe('stop')
})
},
)
}