feat: add audited async task reconciliation and frozen bindings
This commit is contained in:
1 parent
5be06a97c1
commit
c716ec7478
28 files changed
+1312
-76
No files matched your search
@@ -0,0 +1,2 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width,initial-scale=1" /><title>Isolated Task Reconciliation Preview</title></head><body><div id="app"></div><pre id="preview-observations" aria-label="测试回执状态"></pre><script type="module" src="/tests/reconciliation-preview.ts"></script></body></html>
|
||||
@@ -0,0 +1,36 @@
|
||||
// Dedicated UI fixture. No real backend, credentials, wallet or provider is contacted.
|
||||
import { createApp } from 'vue'
|
||||
import TaskReconciliation from '../src/components/TaskReconciliation.vue'
|
||||
import '../src/styles.css'
|
||||
import type { ReconciliationTask, ReconciliationCommand, ReconciliationReceipt } from '../src/services/task-reconciliation'
|
||||
|
||||
const task: ReconciliationTask = { id: 'ui-reconciliation-task', userId: 'isolated-test-user', modelId: 'isolated-test-model', taskType: '3d', status: 'reconciling', updatedAt: '2026-10-21T11:00:00Z', reservedAmount: '1.44000000', costAmount: null, providerId: 'isolated-test-provider', upstreamModel: 'isolated-test-upstream', providerTaskId: 'isolated-provider-task', credentialFingerprint: 'a'.repeat(64), completionStatus: null, requiresReconciliation: true, pricing: { unit: 'CNY / task', price: 1.44 }, error: { code: 'TEST_TIMEOUT', message: '隔离测试:上游结果待核账,额度仍冻结' } }
|
||||
const receipts: ReconciliationReceipt[] = []
|
||||
let accepted: ReconciliationCommand | null = null
|
||||
let attempts = 0
|
||||
const respond = (data: unknown, status = 200) => Promise.resolve(new Response(JSON.stringify(data), { status, headers: { 'Content-Type': 'application/json' } }))
|
||||
function observations() { const output = document.getElementById('preview-observations'); if (output) output.textContent = JSON.stringify({ testOnly: true, wireAttempts: attempts, uniqueReceipts: receipts.length, requestId: accepted?.requestId || null, status: task.status }, null, 2) }
|
||||
window.fetch = async (input, init) => {
|
||||
const url = new URL(typeof input === 'string' ? input : input instanceof URL ? input.href : input.url, location.origin)
|
||||
if (!url.pathname.startsWith('/api/admin/task-reconciliation')) return respond({ error: { message: '隔离测试禁止其他 API 请求' } }, 404)
|
||||
if (url.pathname.endsWith('/quote')) return respond({ expectedCost: '1.44', expectedUpdatedAt: task.updatedAt })
|
||||
if (init?.method === 'POST') {
|
||||
attempts++
|
||||
const command = JSON.parse(String(init.body)) as ReconciliationCommand
|
||||
if (!accepted) {
|
||||
accepted = command
|
||||
const before = { ...task }
|
||||
task.status = 'succeeded'; task.costAmount = '1.44000000'; task.requiresReconciliation = false; task.updatedAt = '2026-10-21T11:01:00Z'
|
||||
receipts.push({ request_id: command.requestId, actor_user_id: 'isolated-test-admin', action: command.action, evidence_reference: command.evidenceReference, note: command.note, created_at: task.updatedAt, before_state: before, after_state: { ...task } })
|
||||
observations()
|
||||
throw new TypeError('隔离测试:服务端已提交,但响应丢失,请原样重试')
|
||||
}
|
||||
observations()
|
||||
if (JSON.stringify(accepted) !== JSON.stringify(command)) return respond({ error: { message: '隔离测试发现重复提交没有保留原操作编号或请求内容' } }, 409)
|
||||
return respond({ task: { ...task }, requestId: command.requestId, replayed: true })
|
||||
}
|
||||
if (url.pathname.endsWith('/ui-reconciliation-task')) return respond({ task: { ...task }, history: receipts })
|
||||
return respond({ records: task.status === 'reconciling' ? [{ ...task }] : [], total: task.status === 'reconciling' ? 1 : 0, page: 1 })
|
||||
}
|
||||
observations()
|
||||
createApp(TaskReconciliation).mount('#app')
|
||||
@@ -0,0 +1,27 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { reconciliationPayload } from '../src/services/task-reconciliation-state.ts'
|
||||
|
||||
const task = { id: 'test-task', updatedAt: '2026-10-21T11:00:00Z' }
|
||||
test('success requires a server quote for the same frozen task version', () => {
|
||||
assert.throws(() => reconciliationPayload(task, 'confirm_succeeded', 'bill', 'verified note', '', '', null, {}))
|
||||
assert.throws(() => reconciliationPayload(task, 'confirm_succeeded', 'bill', 'verified note', '', '', { expectedCost: '1.44', expectedUpdatedAt: 'old' }, {}))
|
||||
const value = reconciliationPayload(task, 'confirm_succeeded', ' bill ', ' verified note ', 'unused', 'unused', { expectedCost: '0.00000001', expectedUpdatedAt: task.updatedAt }, { usage: { input_tokens: 1, output_tokens: 1 } })
|
||||
assert.equal(value.expectedCost, '0.00000001')
|
||||
assert.equal(value.expectedUpdatedAt, task.updatedAt)
|
||||
assert.equal(value.evidenceReference, 'bill')
|
||||
assert.equal('providerTaskId' in value, false)
|
||||
})
|
||||
test('a failure confirmation cannot accidentally include a charge or generated result', () => {
|
||||
const value = reconciliationPayload(task, 'confirm_failed', 'bill', 'verified note', 'unused', 'unused', { expectedCost: '100', expectedUpdatedAt: task.updatedAt }, { url: 'https://example.test/x' })
|
||||
assert.equal('expectedCost' in value, false)
|
||||
assert.equal('result' in value, false)
|
||||
assert.equal('credentialFingerprint' in value, false)
|
||||
})
|
||||
test('resume only carries the original task binding and never a financial adjustment', () => {
|
||||
const value = reconciliationPayload(task, 'resume_polling', 'bill', 'verified note', ' original-task ', ' fingerprint ', null, {})
|
||||
assert.equal(value.providerTaskId, 'original-task')
|
||||
assert.equal(value.credentialFingerprint, 'fingerprint')
|
||||
assert.equal('result' in value, false)
|
||||
assert.equal('expectedCost' in value, false)
|
||||
})
|
||||
Reference in new issue
Block a user