@dsh-ray/assistant-verifier 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +286 -2
- package/cordis.patch.yml +10 -0
- package/lib/config.d.ts +47 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +82 -0
- package/lib/config.js.map +1 -0
- package/lib/creation-review-native.d.ts +30 -0
- package/lib/creation-review-native.d.ts.map +1 -0
- package/lib/creation-review-native.js +137 -0
- package/lib/creation-review-native.js.map +1 -0
- package/lib/creation-review-store.d.ts +108 -0
- package/lib/creation-review-store.d.ts.map +1 -0
- package/lib/creation-review-store.js +715 -0
- package/lib/creation-review-store.js.map +1 -0
- package/lib/creation-review.d.ts +317 -0
- package/lib/creation-review.d.ts.map +1 -0
- package/lib/creation-review.js +842 -0
- package/lib/creation-review.js.map +1 -0
- package/lib/drivers.d.ts +153 -0
- package/lib/drivers.d.ts.map +1 -0
- package/lib/drivers.js +825 -0
- package/lib/drivers.js.map +1 -0
- package/lib/host.d.ts +93 -0
- package/lib/host.d.ts.map +1 -0
- package/lib/host.js +2 -0
- package/lib/host.js.map +1 -0
- package/lib/index.d.ts +26 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +18 -0
- package/lib/index.js.map +1 -0
- package/lib/linux-behavior-assets.d.ts +17 -0
- package/lib/linux-behavior-assets.d.ts.map +1 -0
- package/lib/linux-behavior-assets.js +73 -0
- package/lib/linux-behavior-assets.js.map +1 -0
- package/lib/linux-behavior-runner.d.ts +10 -0
- package/lib/linux-behavior-runner.d.ts.map +1 -0
- package/lib/linux-behavior-runner.js +69 -0
- package/lib/linux-behavior-runner.js.map +1 -0
- package/lib/linux-behavior-runtime.d.ts +67 -0
- package/lib/linux-behavior-runtime.d.ts.map +1 -0
- package/lib/linux-behavior-runtime.js +500 -0
- package/lib/linux-behavior-runtime.js.map +1 -0
- package/lib/memory-review-native.d.ts +50 -0
- package/lib/memory-review-native.d.ts.map +1 -0
- package/lib/memory-review-native.js +166 -0
- package/lib/memory-review-native.js.map +1 -0
- package/lib/memory-review.d.ts +50 -0
- package/lib/memory-review.d.ts.map +1 -0
- package/lib/memory-review.js +363 -0
- package/lib/memory-review.js.map +1 -0
- package/lib/native-behavior-artifact.d.ts +9 -0
- package/lib/native-behavior-artifact.d.ts.map +1 -0
- package/lib/native-behavior-artifact.js +144 -0
- package/lib/native-behavior-artifact.js.map +1 -0
- package/lib/native-behavior-worker.d.ts +3 -0
- package/lib/native-behavior-worker.d.ts.map +1 -0
- package/lib/native-behavior-worker.js +453 -0
- package/lib/native-behavior-worker.js.map +1 -0
- package/lib/plugin-behavior-config.d.ts +34 -0
- package/lib/plugin-behavior-config.d.ts.map +1 -0
- package/lib/plugin-behavior-config.js +170 -0
- package/lib/plugin-behavior-config.js.map +1 -0
- package/lib/plugin-behavior-runner.d.ts +57 -0
- package/lib/plugin-behavior-runner.d.ts.map +1 -0
- package/lib/plugin-behavior-runner.js +910 -0
- package/lib/plugin-behavior-runner.js.map +1 -0
- package/lib/review-authority-lineage.d.ts +109 -0
- package/lib/review-authority-lineage.d.ts.map +1 -0
- package/lib/review-authority-lineage.js +135 -0
- package/lib/review-authority-lineage.js.map +1 -0
- package/lib/review-maintenance.d.ts +20 -0
- package/lib/review-maintenance.d.ts.map +1 -0
- package/lib/review-maintenance.js +24 -0
- package/lib/review-maintenance.js.map +1 -0
- package/lib/revision-regression-review.d.ts +41 -0
- package/lib/revision-regression-review.d.ts.map +1 -0
- package/lib/revision-regression-review.js +379 -0
- package/lib/revision-regression-review.js.map +1 -0
- package/lib/revision-review.d.ts +242 -0
- package/lib/revision-review.d.ts.map +1 -0
- package/lib/revision-review.js +600 -0
- package/lib/revision-review.js.map +1 -0
- package/lib/service.d.ts +138 -0
- package/lib/service.d.ts.map +1 -0
- package/lib/service.js +878 -0
- package/lib/service.js.map +1 -0
- package/lib/source-review-git.d.ts +32 -0
- package/lib/source-review-git.d.ts.map +1 -0
- package/lib/source-review-git.js +241 -0
- package/lib/source-review-git.js.map +1 -0
- package/lib/source-review-native.d.ts +27 -0
- package/lib/source-review-native.d.ts.map +1 -0
- package/lib/source-review-native.js +149 -0
- package/lib/source-review-native.js.map +1 -0
- package/lib/source-review-store.d.ts +44 -0
- package/lib/source-review-store.d.ts.map +1 -0
- package/lib/source-review-store.js +212 -0
- package/lib/source-review-store.js.map +1 -0
- package/lib/source-review.d.ts +86 -0
- package/lib/source-review.d.ts.map +1 -0
- package/lib/source-review.js +255 -0
- package/lib/source-review.js.map +1 -0
- package/lib/store.d.ts +84 -0
- package/lib/store.d.ts.map +1 -0
- package/lib/store.js +386 -0
- package/lib/store.js.map +1 -0
- package/lib/version.d.ts +2 -0
- package/lib/version.d.ts.map +1 -0
- package/lib/version.js +2 -0
- package/lib/version.js.map +1 -0
- package/package.json +144 -3
- package/runtime/plugin-observer-launcher.c +246 -0
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
import { createHash, sign } from 'node:crypto';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { canonicalGrowthJson, growthObjectDigest, pluginRevisionVerificationSigningPayload, anySourceGrowthRunDigest, sourceGrowthEvidenceDigest, validatePluginRevisionParentBinding, validateRevisionAcceptanceAuthorityRef, validateSourceGrowthRunBinding, validateWorkItemRevisionSourceGrowthRunBinding, verifyPluginRevisionVerificationCertificate } from '@dsh-ray/assistant-growth-contract';
|
|
4
|
+
import { CreationReviewStore } from './creation-review-store.js';
|
|
5
|
+
import { CASE_RULES, COMPARISON_RULES, REVIEW_RULES, RULES_VERSION, SCHEMA_RULES, compareCase, compileCreationReviewConfig, linuxPreparedBuildEvidenceDigest, parseCases, parseReview, pluginKindContractAddendum, projectCreationSchemas } from './creation-review.js';
|
|
6
|
+
import { LINUX_BEHAVIOR_BOUNDARY, validateCreationBehaviorDeploymentConfig } from './plugin-behavior-config.js';
|
|
7
|
+
import { runNativeCreationTurn } from './creation-review-native.js';
|
|
8
|
+
const PLUGIN_KINDS = new Set(['tool', 'service', 'channel', 'ui']);
|
|
9
|
+
const SHA = /^[a-f0-9]{64}$/u;
|
|
10
|
+
const ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/u;
|
|
11
|
+
const REVISION_RULES = [
|
|
12
|
+
'Review this as a revision of the exact adopted parent identified by the Host binding.',
|
|
13
|
+
'Check the new authenticated task, feedback, exact candidate patch, scope and lifecycle ownership.',
|
|
14
|
+
'Parent identity is a Host-checked digest binding; do not infer prior behavior from its digest.',
|
|
15
|
+
'This review does not cover every prior behavior. Reject unsupported claims of complete regression safety.',
|
|
16
|
+
];
|
|
17
|
+
const REVISION_PURPOSE = 'assistant-growth/revision-review/v1';
|
|
18
|
+
export function validateRevisionReviewConfig(input) {
|
|
19
|
+
if (Object.hasOwn(input, 'workItemFailures') && typeof input.workItemFailures !== 'boolean')
|
|
20
|
+
throw new Error('revision review invalid workItemFailures');
|
|
21
|
+
if (Object.hasOwn(input, 'history'))
|
|
22
|
+
throw new Error('revision review history is not supported');
|
|
23
|
+
validateCreationBehaviorDeploymentConfig(Object.getOwnPropertyDescriptor(input, 'runner')?.value);
|
|
24
|
+
return compileCreationReviewConfig(input).config;
|
|
25
|
+
}
|
|
26
|
+
export function compileRevisionReviewConfig(input) {
|
|
27
|
+
if (Object.hasOwn(input, 'workItemFailures') && typeof input.workItemFailures !== 'boolean')
|
|
28
|
+
throw new Error('revision review invalid workItemFailures');
|
|
29
|
+
if (Object.hasOwn(input, 'history'))
|
|
30
|
+
throw new Error('revision review history is not supported');
|
|
31
|
+
validateCreationBehaviorDeploymentConfig(Object.getOwnPropertyDescriptor(input, 'runner')?.value);
|
|
32
|
+
const checked = compileCreationReviewConfig(input);
|
|
33
|
+
const authorityDigest = growthObjectDigest({ purpose: REVISION_PURPOSE,
|
|
34
|
+
baseRules: RULES_VERSION, caseRules: CASE_RULES, comparisonRules: COMPARISON_RULES,
|
|
35
|
+
schemaRules: SCHEMA_RULES, reviewRules: REVIEW_RULES, revisionRules: REVISION_RULES,
|
|
36
|
+
...(checked.config.workItemFailures === true ? { workItemFailures: true } : {}),
|
|
37
|
+
configurationDigest: checked.authority.authorityDigest });
|
|
38
|
+
const authority = {
|
|
39
|
+
protocol: 'assistant-growth/revision-acceptance-authority/v1', authorityId: checked.config.authorityId,
|
|
40
|
+
keyId: checked.config.keyId, authorityDigest, namePrefix: checked.config.namePrefix, expiresAt: checked.config.expiresAt,
|
|
41
|
+
};
|
|
42
|
+
validateRevisionAcceptanceAuthorityRef(authority);
|
|
43
|
+
return { config: checked.config, authority: Object.freeze(authority), publicKey: checked.publicKey, privateKey: checked.privateKey };
|
|
44
|
+
}
|
|
45
|
+
function requestValid(value) {
|
|
46
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.getPrototypeOf(value) !== Object.prototype
|
|
47
|
+
|| Reflect.ownKeys(value).some(key => typeof key !== 'string')
|
|
48
|
+
|| Object.getOwnPropertyNames(value).sort().join(',') !== 'planId,protocol'
|
|
49
|
+
|| Object.values(Object.getOwnPropertyDescriptors(value)).some(field => !field.enumerable || !('value' in field)))
|
|
50
|
+
return false;
|
|
51
|
+
const request = value;
|
|
52
|
+
return request.protocol === 'assistant-growth/revision-verification-request/v1'
|
|
53
|
+
&& typeof request.planId === 'string' && ID.test(request.planId) && request.planId.normalize('NFC') === request.planId;
|
|
54
|
+
}
|
|
55
|
+
/** Independent Host review of a new task's candidate revision. It does not grant execution or replacement. */
|
|
56
|
+
export class RevisionReviewRuntime {
|
|
57
|
+
ctx;
|
|
58
|
+
#compiled;
|
|
59
|
+
#store;
|
|
60
|
+
#runner;
|
|
61
|
+
#runnerLoading;
|
|
62
|
+
#abort = new AbortController();
|
|
63
|
+
#flights = new Map();
|
|
64
|
+
#closing;
|
|
65
|
+
constructor(ctx, input, databasePath) {
|
|
66
|
+
this.ctx = ctx;
|
|
67
|
+
this.#compiled = compileRevisionReviewConfig(input);
|
|
68
|
+
this.#store = new CreationReviewStore(databasePath + '.revision-reviews');
|
|
69
|
+
}
|
|
70
|
+
#loadRunner() {
|
|
71
|
+
if (this.#runner)
|
|
72
|
+
return Promise.resolve(this.#runner);
|
|
73
|
+
return this.#runnerLoading ??= import('./plugin-behavior-runner.js').then(({ PluginBehaviorRunner }) => {
|
|
74
|
+
this.#abort.signal.throwIfAborted();
|
|
75
|
+
const { config, authority } = this.#compiled;
|
|
76
|
+
if (Date.now() >= Math.min(config.expiresAt, config.runner.expiresAt))
|
|
77
|
+
throw new Error('revision observer authority expired');
|
|
78
|
+
const runner = new PluginBehaviorRunner({ ...config.runner, authorityDigest: authority.authorityDigest });
|
|
79
|
+
this.#runner = runner;
|
|
80
|
+
return runner;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The work-item revision uses the identical engineering task evidence as a
|
|
85
|
+
* work-item creation, only with a v3 revise-created run and a revision grant.
|
|
86
|
+
*/
|
|
87
|
+
#workItemRevisionTask(snapshot, run) {
|
|
88
|
+
const { config, authority } = this.#compiled;
|
|
89
|
+
if (config.workItemFailures !== true)
|
|
90
|
+
throw new Error('work-item revision review is not authorized');
|
|
91
|
+
const { source, reference, plan } = snapshot, { task, objective } = source;
|
|
92
|
+
const grant = plan.sourceRevision?.grant;
|
|
93
|
+
if (!grant || grant.workItemFailures !== true || grant.namePrefix !== config.namePrefix)
|
|
94
|
+
throw new Error('work-item revision grant invalid');
|
|
95
|
+
const pins = run.authorities;
|
|
96
|
+
if (task.protocol !== 'assistant-delivery/owner-work-item-learning/v1' || task.judgement !== 'owner-feedback'
|
|
97
|
+
|| task.canonical.objective?.status !== 'not-achieved'
|
|
98
|
+
|| task.canonical.projection.subjectKind !== 'work-item'
|
|
99
|
+
|| task.canonical.projection.subjectRef !== run.source.itemId
|
|
100
|
+
|| task.canonical.projection.disposition !== 'upsert'
|
|
101
|
+
|| task.proofIdentity.itemId !== run.source.itemId
|
|
102
|
+
|| task.sourceDigest !== run.source.sourceDigest || task.presentationDigest !== run.source.presentationDigest
|
|
103
|
+
|| reference.sourceDigest !== run.source.sourceDigest || reference.presentationDigest !== run.source.presentationDigest
|
|
104
|
+
|| reference.outcomeId !== run.source.outcomeId
|
|
105
|
+
|| canonicalGrowthJson(reference.projection) !== canonicalGrowthJson(run.source.projection)
|
|
106
|
+
|| reference.context.manifestDigest !== run.source.context.manifestDigest
|
|
107
|
+
|| reference.context.objectiveDigest !== run.source.context.objectiveDigest
|
|
108
|
+
|| reference.context.objectiveBytes !== run.source.context.objectiveBytes)
|
|
109
|
+
throw new Error('work-item revision engineering context changed');
|
|
110
|
+
for (const key of ['authorityId', 'authorityHash', 'principalId', 'principalRecordId', 'principalVersion', 'workspace', 'agentPreset']) {
|
|
111
|
+
if (reference.owner[key] !== config.owner[key]
|
|
112
|
+
|| task.owner[key] !== config.owner[key])
|
|
113
|
+
throw new Error('revision work-item owner changed');
|
|
114
|
+
}
|
|
115
|
+
if (pins.revisionGrant.id !== grant.id || sourceGrowthEvidenceDigest(grant) !== pins.revisionGrant.digest
|
|
116
|
+
|| grant.expiresAt !== pins.revisionGrant.expiresAt || pins.revisionGrant.workItemFailures !== true
|
|
117
|
+
|| canonicalGrowthJson(pins.revisionAcceptance) !== canonicalGrowthJson(authority)
|
|
118
|
+
|| canonicalGrowthJson(run.revisionAcceptance) !== canonicalGrowthJson(pins.revisionAcceptance))
|
|
119
|
+
throw new Error('work-item revision frozen grant changed');
|
|
120
|
+
if (run.modelOrigin === 'inherited-owner-task' && canonicalGrowthJson(run.model) !== canonicalGrowthJson(task.proofIdentity.supplier)) {
|
|
121
|
+
throw new Error('work-item revision inherited supplier changed');
|
|
122
|
+
}
|
|
123
|
+
if (typeof objective !== 'string' || !objective.isWellFormed() || !objective.trim()
|
|
124
|
+
|| Buffer.byteLength(objective) > 16_384)
|
|
125
|
+
throw new Error('work-item revision objective invalid');
|
|
126
|
+
return { objective, feedback: task.presentation.feedback.text, judgement: task.judgement, name: plan.name };
|
|
127
|
+
}
|
|
128
|
+
inspect(input) {
|
|
129
|
+
const { config, authority, publicKey } = this.#compiled;
|
|
130
|
+
if (this.#abort.signal.aborted || Date.now() >= config.expiresAt
|
|
131
|
+
|| canonicalGrowthJson(input.owner) !== canonicalGrowthJson(config.owner))
|
|
132
|
+
return undefined;
|
|
133
|
+
const remainingVerifications = this.#store.remaining(config.authorityId, authority.authorityDigest, config.maxVerifications);
|
|
134
|
+
return Object.freeze({ authority, publicKey, remainingVerifications, available: remainingVerifications > 0 });
|
|
135
|
+
}
|
|
136
|
+
/** Fail-closed work-item revision readiness; undefined until every peer seam exists. */
|
|
137
|
+
inspectWorkItemRevisionReviewReadiness() {
|
|
138
|
+
const { config, authority, publicKey } = this.#compiled;
|
|
139
|
+
if (this.#abort.signal.aborted || config.workItemFailures !== true)
|
|
140
|
+
return undefined;
|
|
141
|
+
try {
|
|
142
|
+
for (const name of ['agents', 'sessions', 'tools', 'llm', 'systemPrompt', 'assistantPolicy']) {
|
|
143
|
+
if (!this.ctx.get(name, false))
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
const port = this.ctx.get('pluginControlPlane', false);
|
|
147
|
+
if (typeof port?.inspectWorkItemPreparedRevisionProtocol !== 'function'
|
|
148
|
+
|| port.inspectWorkItemPreparedRevisionProtocol() !== 'dsh-prepared-revision/v2'
|
|
149
|
+
|| typeof port.inspectPreparedRevision !== 'function' || typeof port.withPreparedRevisionFence !== 'function'
|
|
150
|
+
|| typeof port.inspectPreparedRevisionReviewContext !== 'function')
|
|
151
|
+
return undefined;
|
|
152
|
+
const remainingVerifications = this.#store.remaining(config.authorityId, authority.authorityDigest, config.maxVerifications);
|
|
153
|
+
if (this.#abort.signal.aborted)
|
|
154
|
+
return undefined;
|
|
155
|
+
return Object.freeze({ protocol: 'assistant-growth/work-item-revision-review-readiness/v1',
|
|
156
|
+
sourceProtocol: 'assistant-delivery/work-item-engineering-context/v1', runProtocol: 'assistant-growth/source-run/v3',
|
|
157
|
+
workItemFailures: true, authority, publicKey, remainingVerifications,
|
|
158
|
+
available: remainingVerifications > 0 && Date.now() < Math.min(config.expiresAt, config.runner.expiresAt) });
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
run(request, external) {
|
|
165
|
+
if (!requestValid(request))
|
|
166
|
+
return Promise.resolve({ status: 'rejected', reason: 'request-invalid' });
|
|
167
|
+
if (this.#abort.signal.aborted)
|
|
168
|
+
return Promise.resolve({ status: 'unknown', reason: 'interrupted' });
|
|
169
|
+
const existing = this.#flights.get(request.planId);
|
|
170
|
+
if (existing)
|
|
171
|
+
return existing;
|
|
172
|
+
const flight = this.#run(request, external);
|
|
173
|
+
this.#flights.set(request.planId, flight);
|
|
174
|
+
void flight.finally(() => this.#flights.delete(request.planId)).catch(() => { });
|
|
175
|
+
return flight;
|
|
176
|
+
}
|
|
177
|
+
async #run(request, external) {
|
|
178
|
+
const { config, authority, privateKey, publicKey } = this.#compiled;
|
|
179
|
+
const signal = AbortSignal.any([this.#abort.signal, AbortSignal.timeout(config.maxDurationMs), ...(external ? [external] : [])]);
|
|
180
|
+
let admitted;
|
|
181
|
+
try {
|
|
182
|
+
signal.throwIfAborted();
|
|
183
|
+
const port = this.ctx.get('pluginControlPlane');
|
|
184
|
+
if (!port || typeof port.inspectPreparedRevision !== 'function' || typeof port.withPreparedRevisionFence !== 'function'
|
|
185
|
+
|| typeof port.inspectPreparedRevisionReviewContext !== 'function')
|
|
186
|
+
throw new Error('revision source port unavailable');
|
|
187
|
+
const snapshot = port.inspectPreparedRevision(request.planId);
|
|
188
|
+
const workItem = snapshot.protocol === 'dsh-prepared-revision/v2';
|
|
189
|
+
if (snapshot.protocol === 'dsh-prepared-revision/v3')
|
|
190
|
+
return this.#runInstruction(snapshot, port, signal, request);
|
|
191
|
+
if (!workItem && snapshot.protocol !== 'dsh-prepared-revision/v1')
|
|
192
|
+
throw new Error('revision source protocol changed');
|
|
193
|
+
const { plan, artifact } = snapshot;
|
|
194
|
+
const revision = plan.sourceRevision;
|
|
195
|
+
const intentRevision = snapshot.job.intent.revision;
|
|
196
|
+
const run = revision?.growthRun;
|
|
197
|
+
if (workItem)
|
|
198
|
+
validateWorkItemRevisionSourceGrowthRunBinding(run);
|
|
199
|
+
else
|
|
200
|
+
validateSourceGrowthRunBinding(run);
|
|
201
|
+
const frozen = run;
|
|
202
|
+
validateRevisionAcceptanceAuthorityRef(frozen.revisionAcceptance);
|
|
203
|
+
const parent = revision?.parent;
|
|
204
|
+
validatePluginRevisionParentBinding(parent);
|
|
205
|
+
validatePluginRevisionParentBinding(intentRevision?.parent);
|
|
206
|
+
if (workItem)
|
|
207
|
+
validateWorkItemRevisionSourceGrowthRunBinding(intentRevision?.growthRun);
|
|
208
|
+
else
|
|
209
|
+
validateSourceGrowthRunBinding(intentRevision?.growthRun);
|
|
210
|
+
const workItemTask = workItem
|
|
211
|
+
? this.#workItemRevisionTask(snapshot, frozen)
|
|
212
|
+
: undefined;
|
|
213
|
+
const ownerReceipt = workItem
|
|
214
|
+
? snapshot.source.task.owner
|
|
215
|
+
: snapshot.source.owner;
|
|
216
|
+
if (canonicalGrowthJson(frozen.revisionAcceptance) !== canonicalGrowthJson(authority)
|
|
217
|
+
|| canonicalGrowthJson(frozen) !== canonicalGrowthJson(intentRevision.growthRun)
|
|
218
|
+
|| canonicalGrowthJson(parent) !== canonicalGrowthJson(intentRevision.parent)
|
|
219
|
+
|| plan.mode !== 'prepared-revise' || plan.status !== 'pending-approval'
|
|
220
|
+
|| !plan.name.startsWith(config.namePrefix) || revision?.grant.namePrefix !== config.namePrefix
|
|
221
|
+
|| Date.now() >= Math.min(config.expiresAt, plan.expiresAt, revision.grant.expiresAt, frozen.expiresAt)
|
|
222
|
+
|| !Buffer.isBuffer(artifact)
|
|
223
|
+
|| artifact.length !== plan.preparedEvidence?.pack.sizeBytes || artifact.length > 512 * 1024
|
|
224
|
+
|| createHash('sha256').update(artifact).digest('hex') !== plan.preparedEvidence?.pack.sha256
|
|
225
|
+
|| !plan.sourceCheck || !SHA.test(plan.sourceCheck.treeDigest) || !SHA.test(plan.sourceCheck.patchDigest)
|
|
226
|
+
|| sourceGrowthEvidenceDigest(snapshot.reference.owner) !== frozen.ownerDigest) {
|
|
227
|
+
throw new Error('prepared revision authority or task evidence invalid');
|
|
228
|
+
}
|
|
229
|
+
const v1Source = workItem ? undefined : snapshot.source;
|
|
230
|
+
if (!workItem && (v1Source.source.truncated || !v1Source.source.quiescent || v1Source.feedback?.truncated
|
|
231
|
+
|| v1Source.judgement === 'unresolved' || !v1Source.source.objective.trim()
|
|
232
|
+
|| Buffer.byteLength(v1Source.source.objective) > 16_384))
|
|
233
|
+
throw new Error('prepared revision task evidence invalid');
|
|
234
|
+
for (const key of ['authorityId', 'authorityHash', 'principalId', 'principalRecordId', 'principalVersion', 'workspace', 'agentPreset']) {
|
|
235
|
+
if (snapshot.reference.owner[key] !== config.owner[key]
|
|
236
|
+
|| ownerReceipt[key] !== config.owner[key])
|
|
237
|
+
throw new Error('revision owner changed');
|
|
238
|
+
}
|
|
239
|
+
const sourceBinding = { referenceDigest: sourceGrowthEvidenceDigest(snapshot.reference), ownerDigest: sourceGrowthEvidenceDigest(snapshot.reference.owner),
|
|
240
|
+
growthRunDigest: anySourceGrowthRunDigest(frozen) };
|
|
241
|
+
const parentDigest = sourceGrowthEvidenceDigest(parent);
|
|
242
|
+
let linuxExecution;
|
|
243
|
+
if ('backend' in config.runner && config.runner.backend === 'linux-native') {
|
|
244
|
+
const buildEvidenceDigest = linuxPreparedBuildEvidenceDigest(plan.preparedEvidence);
|
|
245
|
+
if (typeof port.inspectPreparedRevisionLinuxBuild !== 'function')
|
|
246
|
+
throw new Error('Linux build inspection unavailable');
|
|
247
|
+
const build = await port.inspectPreparedRevisionLinuxBuild(plan.id, signal);
|
|
248
|
+
signal.throwIfAborted();
|
|
249
|
+
if (build.evidenceDigest !== buildEvidenceDigest)
|
|
250
|
+
throw new Error('Linux build evidence snapshot changed');
|
|
251
|
+
const [{ linuxRuntimeDigest }, { linuxPluginBehaviorRuntimeDigest, readLinuxPluginBehaviorPreparation }] = await Promise.all([
|
|
252
|
+
import('@dsh-ray/assistant-isolation'), import('./linux-behavior-runtime.js'),
|
|
253
|
+
]);
|
|
254
|
+
const preparation = await readLinuxPluginBehaviorPreparation({ runtime: build.runtime,
|
|
255
|
+
root: dirname(config.runner.runtime.runtime.root.path), signal });
|
|
256
|
+
const runtimeDigest = linuxPluginBehaviorRuntimeDigest(config.runner.runtime);
|
|
257
|
+
const buildRuntimeDigest = linuxRuntimeDigest(build.runtime);
|
|
258
|
+
if (plan.preparedEvidence?.runtimeDigest !== buildRuntimeDigest
|
|
259
|
+
|| preparation.baseRuntimeDigest !== buildRuntimeDigest
|
|
260
|
+
|| linuxPluginBehaviorRuntimeDigest(preparation.descriptor) !== runtimeDigest
|
|
261
|
+
|| !SHA.test(preparation.preparationDigest))
|
|
262
|
+
throw new Error('Linux build and observer preparation mismatch');
|
|
263
|
+
linuxExecution = { boundary: LINUX_BEHAVIOR_BOUNDARY, runtimeDigest, buildRuntimeDigest,
|
|
264
|
+
buildEvidenceDigest, observerPreparationDigest: preparation.preparationDigest };
|
|
265
|
+
}
|
|
266
|
+
const fenceInput = { planId: plan.id, planDigest: plan.digest, artifactSha256: plan.preparedEvidence.pack.sha256,
|
|
267
|
+
growthRunDigest: sourceBinding.growthRunDigest, referenceDigest: sourceBinding.referenceDigest, parentDigest };
|
|
268
|
+
const fence = (callback) => {
|
|
269
|
+
if (workItem) {
|
|
270
|
+
if (config.workItemFailures !== true || Date.now() >= Math.min(config.expiresAt, config.runner.expiresAt, frozen.authorities.sourceJobs.expiresAt, frozen.authorities.revisionGrant.expiresAt, frozen.authorities.revisionAcceptance.expiresAt))
|
|
271
|
+
throw new Error('work-item revision review authority expired');
|
|
272
|
+
}
|
|
273
|
+
return port.withPreparedRevisionFence(fenceInput, callback);
|
|
274
|
+
};
|
|
275
|
+
fence(() => { });
|
|
276
|
+
const model = frozen.model;
|
|
277
|
+
const binding = growthObjectDigest({ purpose: REVISION_PURPOSE, request, authority, parent, parentDigest,
|
|
278
|
+
plan: { id: plan.id, digest: plan.digest, tree: plan.sourceCheck.treeDigest, patch: plan.sourceCheck.patchDigest,
|
|
279
|
+
artifact: plan.preparedEvidence.pack }, source: sourceBinding, model,
|
|
280
|
+
budget: { maxOutputTokens: config.maxOutputTokens, maxDurationMs: config.maxDurationMs, maxCases: config.maxCases },
|
|
281
|
+
...(linuxExecution ? { execution: linuxExecution } : {}) });
|
|
282
|
+
const claim = fence(() => this.#store.claim(plan.id, binding, config.authorityId, authority.authorityDigest, config.maxVerifications));
|
|
283
|
+
if (claim.state === 'certificate')
|
|
284
|
+
return claim.certificate && claim.certificate.expiresAt > Date.now()
|
|
285
|
+
&& canonicalGrowthJson(claim.certificate.parent) === canonicalGrowthJson(parent)
|
|
286
|
+
&& verifyPluginRevisionVerificationCertificate(claim.certificate, authority, publicKey)
|
|
287
|
+
? { status: 'verified', certificate: claim.certificate } : { status: 'unknown', reason: 'stale-source' };
|
|
288
|
+
if (claim.state === 'rejected')
|
|
289
|
+
return { status: 'rejected', reason: claim.reason };
|
|
290
|
+
if (claim.state !== 'new')
|
|
291
|
+
return { status: 'unknown', reason: claim.reason ?? 'previous-unknown' };
|
|
292
|
+
admitted = { planId: plan.id, binding };
|
|
293
|
+
const planPluginKind = plan.pluginKind;
|
|
294
|
+
if (planPluginKind !== undefined && !PLUGIN_KINDS.has(planPluginKind)) {
|
|
295
|
+
throw new Error('prepared revision pluginKind invalid');
|
|
296
|
+
}
|
|
297
|
+
const observer = await this.#loadRunner();
|
|
298
|
+
signal.throwIfAborted();
|
|
299
|
+
fence(() => { });
|
|
300
|
+
const discovery = await observer.run({ key: growthObjectDigest([binding, 'discover']), artifact, operation: { kind: 'discover' },
|
|
301
|
+
...(planPluginKind !== undefined ? { pluginKind: planPluginKind } : {}), signal });
|
|
302
|
+
if (discovery.status !== 'observed' || !discovery.quiescent || !discovery.schemaDigest || !discovery.schemas
|
|
303
|
+
|| !discovery.environment || !discovery.jobId || discovery.artifactSha256 !== plan.preparedEvidence.pack.sha256
|
|
304
|
+
|| linuxExecution && (discovery.boundary !== linuxExecution.boundary
|
|
305
|
+
|| discovery.runtimeDigest !== linuxExecution.runtimeDigest)) {
|
|
306
|
+
this.#store.unknown(plan.id, binding, 'schema-observation-unknown');
|
|
307
|
+
return { status: 'unknown', reason: 'schema-observation-unknown' };
|
|
308
|
+
}
|
|
309
|
+
const effectiveKind = planPluginKind ?? discovery.pluginKind;
|
|
310
|
+
const projectedSchemas = projectCreationSchemas(discovery.schemas);
|
|
311
|
+
fence(() => this.#store.discovered(plan.id, binding, { jobId: discovery.jobId, schemaDigest: discovery.schemaDigest,
|
|
312
|
+
schemas: projectedSchemas, environment: discovery.environment }));
|
|
313
|
+
const task = workItem ? workItemTask : { objective: v1Source.source.objective, feedback: v1Source.feedback?.text ?? null,
|
|
314
|
+
judgement: v1Source.judgement, name: plan.name };
|
|
315
|
+
const contract = await runNativeCreationTurn(this.ctx, { sessionId: `revision-contract-${binding.slice(0, 40)}`,
|
|
316
|
+
owner: config.owner, model, maxInputBytes: config.maxInputBytes, maxOutputTokens: config.maxOutputTokens - 1,
|
|
317
|
+
prompt: [...CASE_RULES, ...pluginKindContractAddendum(effectiveKind), `Case limit: ${config.maxCases}.`, `Fixed owner acceptance policy: ${config.policy}`].join('\n'),
|
|
318
|
+
data: { task, schemas: projectedSchemas }, signal, assertCurrent: () => fence(() => { }) });
|
|
319
|
+
const cases = parseCases(contract.value, config.maxCases, projectedSchemas);
|
|
320
|
+
if (cases.status === 'insufficient') {
|
|
321
|
+
fence(() => this.#store.unknown(plan.id, binding, 'contract-insufficient'));
|
|
322
|
+
return { status: 'unknown', reason: 'contract-insufficient' };
|
|
323
|
+
}
|
|
324
|
+
fence(() => this.#store.contract(plan.id, binding, { cases: cases.cases, outputDigest: contract.outputDigest,
|
|
325
|
+
sessionId: contract.sessionId }));
|
|
326
|
+
const observations = [];
|
|
327
|
+
const jobs = new Set([discovery.jobId]);
|
|
328
|
+
for (const item of cases.cases) {
|
|
329
|
+
signal.throwIfAborted();
|
|
330
|
+
fence(() => { });
|
|
331
|
+
const operation = { kind: 'invoke', schemaDigest: discovery.schemaDigest,
|
|
332
|
+
calls: [{ id: item.id, toolName: item.toolName, arguments: item.arguments }] };
|
|
333
|
+
const operationDigest = growthObjectDigest(operation);
|
|
334
|
+
fence(() => this.#store.claimCase(plan.id, binding, item.id, operationDigest));
|
|
335
|
+
const observed = await observer.run({ key: growthObjectDigest([binding, 'invoke', operationDigest]), artifact, operation,
|
|
336
|
+
...(effectiveKind !== undefined ? { pluginKind: effectiveKind } : {}), signal });
|
|
337
|
+
if (observed.status !== 'observed' || !observed.quiescent || !observed.jobId
|
|
338
|
+
|| observed.schemaDigest !== discovery.schemaDigest || observed.artifactSha256 !== plan.preparedEvidence.pack.sha256
|
|
339
|
+
|| canonicalGrowthJson(observed.environment) !== canonicalGrowthJson(discovery.environment)
|
|
340
|
+
|| linuxExecution && (observed.boundary !== linuxExecution.boundary
|
|
341
|
+
|| observed.runtimeDigest !== linuxExecution.runtimeDigest)
|
|
342
|
+
|| observed.calls?.length !== 1 || observed.calls[0]?.id !== item.id || jobs.has(observed.jobId)) {
|
|
343
|
+
this.#store.unknown(plan.id, binding, 'case-observation-unknown');
|
|
344
|
+
return { status: 'unknown', reason: 'case-observation-unknown' };
|
|
345
|
+
}
|
|
346
|
+
const raw = observed.calls[0].result;
|
|
347
|
+
jobs.add(observed.jobId);
|
|
348
|
+
const observationDigest = growthObjectDigest(raw);
|
|
349
|
+
fence(() => this.#store.observation(plan.id, binding, item.id, { jobId: observed.jobId, operationDigest, observationDigest }));
|
|
350
|
+
observations.push({ caseId: item.id, jobId: observed.jobId, operationDigest, observationDigest });
|
|
351
|
+
if (!compareCase(item.expected, raw)) {
|
|
352
|
+
fence(() => this.#store.reject(plan.id, binding, 'case-mismatch'));
|
|
353
|
+
return { status: 'rejected', reason: 'case-mismatch' };
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const context = await port.inspectPreparedRevisionReviewContext(plan.id, signal);
|
|
357
|
+
fence(() => { });
|
|
358
|
+
if (Buffer.byteLength(context.patch) > config.maxInputBytes / 2 || context.changedPaths.length < 1
|
|
359
|
+
|| context.changedPaths.length > 32)
|
|
360
|
+
throw new Error('revision source review context exceeds bound');
|
|
361
|
+
fence(() => this.#store.claimReview(plan.id, binding));
|
|
362
|
+
const review = await runNativeCreationTurn(this.ctx, { sessionId: `revision-source-${binding.slice(0, 40)}`,
|
|
363
|
+
owner: config.owner, model, maxInputBytes: config.maxInputBytes,
|
|
364
|
+
maxOutputTokens: config.maxOutputTokens - contract.outputTokens,
|
|
365
|
+
prompt: [...REVIEW_RULES, ...REVISION_RULES,
|
|
366
|
+
...(effectiveKind && effectiveKind !== 'tool' ? [`Candidate pluginKind: ${effectiveKind}.`] : []),
|
|
367
|
+
`Fixed owner acceptance policy: ${config.policy}`].join('\n'),
|
|
368
|
+
data: { task, parent, cases: cases.cases, changedPaths: context.changedPaths, patch: context.patch },
|
|
369
|
+
signal, assertCurrent: () => fence(() => { }) });
|
|
370
|
+
const verdict = parseReview(review.value);
|
|
371
|
+
if (contract.outputTokens + review.outputTokens > config.maxOutputTokens)
|
|
372
|
+
throw new Error('revision model budget exceeded');
|
|
373
|
+
if (verdict.decision !== 'approved') {
|
|
374
|
+
fence(() => this.#store.reject(plan.id, binding, 'source-review-rejected'));
|
|
375
|
+
return { status: 'rejected', reason: 'source-review-rejected' };
|
|
376
|
+
}
|
|
377
|
+
const verifiedAt = Date.now();
|
|
378
|
+
const expiresAt = Math.min(verifiedAt + config.receiptTtlMs, config.expiresAt, config.runner.expiresAt, plan.expiresAt, revision.grant.expiresAt, frozen.expiresAt);
|
|
379
|
+
if (expiresAt <= verifiedAt)
|
|
380
|
+
throw new Error('revision verification windows closed');
|
|
381
|
+
const body = {
|
|
382
|
+
protocol: linuxExecution ? 'assistant-growth/revision-verification/v2'
|
|
383
|
+
: 'assistant-growth/revision-verification/v1',
|
|
384
|
+
verificationId: `revision-${binding.slice(0, 40)}`,
|
|
385
|
+
authority, plan: { id: plan.id, digest: plan.digest, name: plan.name,
|
|
386
|
+
sourceTreeDigest: plan.sourceCheck.treeDigest, sourcePatchDigest: plan.sourceCheck.patchDigest,
|
|
387
|
+
artifactSha256: plan.preparedEvidence.pack.sha256, artifactBytes: artifact.length,
|
|
388
|
+
generatorDigest: plan.generatorDigest }, parent, source: sourceBinding,
|
|
389
|
+
contractDigest: growthObjectDigest(cases.cases), schemaDigest: discovery.schemaDigest,
|
|
390
|
+
environment: discovery.environment, model,
|
|
391
|
+
budget: { modelCalls: 2, maxOutputTokens: config.maxOutputTokens, maxDurationMs: config.maxDurationMs,
|
|
392
|
+
maxCases: config.maxCases }, sessions: { contract: contract.sessionId, sourceReview: review.sessionId },
|
|
393
|
+
observations, reviewDigest: review.outputDigest, verifiedAt, expiresAt,
|
|
394
|
+
...(linuxExecution ? { execution: linuxExecution } : {}),
|
|
395
|
+
};
|
|
396
|
+
const signature = sign(null, Buffer.from(pluginRevisionVerificationSigningPayload(body)), privateKey).toString('base64url');
|
|
397
|
+
const certificate = { ...body, signature };
|
|
398
|
+
fence(() => this.#store.certificate(plan.id, binding, certificate));
|
|
399
|
+
admitted = undefined;
|
|
400
|
+
return { status: 'verified', certificate };
|
|
401
|
+
}
|
|
402
|
+
catch {
|
|
403
|
+
const reason = signal.aborted ? 'interrupted' : admitted ? 'verification-unknown' : 'stale-source';
|
|
404
|
+
if (admitted) {
|
|
405
|
+
try {
|
|
406
|
+
this.#store.unknown(admitted.planId, admitted.binding, reason);
|
|
407
|
+
}
|
|
408
|
+
catch { /* completed or closed ledger remains authoritative */ }
|
|
409
|
+
}
|
|
410
|
+
return { status: 'unknown', reason };
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Owner-instruction revision review: the same independent contract and
|
|
415
|
+
* behavior checks as a task-failure revision, but sourced from the frozen
|
|
416
|
+
* Host instruction instead of a failed task and growth run. No execution
|
|
417
|
+
* boundary evidence is claimed because no growth-run execution pin exists.
|
|
418
|
+
*/
|
|
419
|
+
async #runInstruction(snapshot, port, signal, request) {
|
|
420
|
+
const { config, authority, privateKey, publicKey } = this.#compiled;
|
|
421
|
+
let admitted;
|
|
422
|
+
try {
|
|
423
|
+
const { plan, artifact } = snapshot;
|
|
424
|
+
const instruction = snapshot.job.intent.ownerInstruction;
|
|
425
|
+
const revision = plan.sourceRevision;
|
|
426
|
+
const parent = revision?.parent;
|
|
427
|
+
validatePluginRevisionParentBinding(parent);
|
|
428
|
+
if (!instruction || instruction.digest !== snapshot.source.instruction.digest
|
|
429
|
+
|| instruction.instructionId !== snapshot.source.instruction.instructionId
|
|
430
|
+
|| Object.hasOwn(revision ?? {}, 'growthRun')
|
|
431
|
+
|| plan.mode !== 'prepared-revise' || plan.status !== 'pending-approval'
|
|
432
|
+
|| !plan.name.startsWith(config.namePrefix) || revision?.grant.namePrefix !== config.namePrefix
|
|
433
|
+
|| !Number.isSafeInteger(revision?.grant.expiresAt)
|
|
434
|
+
|| Date.now() >= Math.min(config.expiresAt, plan.expiresAt, revision.grant.expiresAt)
|
|
435
|
+
|| !Buffer.isBuffer(artifact) || !plan.preparedEvidence
|
|
436
|
+
|| artifact.length !== plan.preparedEvidence.pack.sizeBytes || artifact.length > 512 * 1024
|
|
437
|
+
|| createHash('sha256').update(artifact).digest('hex') !== plan.preparedEvidence.pack.sha256
|
|
438
|
+
|| !plan.sourceCheck || !SHA.test(plan.sourceCheck.treeDigest) || !SHA.test(plan.sourceCheck.patchDigest)) {
|
|
439
|
+
throw new Error('prepared instruction revision evidence invalid');
|
|
440
|
+
}
|
|
441
|
+
for (const key of ['authorityId', 'authorityHash', 'principalId', 'principalRecordId', 'principalVersion',
|
|
442
|
+
'workspace', 'agentPreset']) {
|
|
443
|
+
if (snapshot.source.owner[key] !== config.owner[key])
|
|
444
|
+
throw new Error('revision owner changed');
|
|
445
|
+
}
|
|
446
|
+
if (typeof port.withPreparedInstructionRevisionFence !== 'function') {
|
|
447
|
+
throw new Error('instruction revision fence unavailable');
|
|
448
|
+
}
|
|
449
|
+
const sourceBinding = { instructionDigest: instruction.digest,
|
|
450
|
+
ownerDigest: sourceGrowthEvidenceDigest(snapshot.source.owner) };
|
|
451
|
+
const parentDigest = sourceGrowthEvidenceDigest(parent);
|
|
452
|
+
const fenceInput = { planId: plan.id, planDigest: plan.digest,
|
|
453
|
+
artifactSha256: plan.preparedEvidence.pack.sha256, instructionDigest: instruction.digest, parentDigest };
|
|
454
|
+
const fence = (callback) => port.withPreparedInstructionRevisionFence(fenceInput, callback);
|
|
455
|
+
fence(() => { });
|
|
456
|
+
const model = snapshot.source.reviewModel;
|
|
457
|
+
const binding = growthObjectDigest({ purpose: REVISION_PURPOSE, request, authority, parent, parentDigest,
|
|
458
|
+
plan: { id: plan.id, digest: plan.digest, tree: plan.sourceCheck.treeDigest, patch: plan.sourceCheck.patchDigest,
|
|
459
|
+
artifact: plan.preparedEvidence.pack }, source: sourceBinding, model,
|
|
460
|
+
budget: { maxOutputTokens: config.maxOutputTokens, maxDurationMs: config.maxDurationMs, maxCases: config.maxCases } });
|
|
461
|
+
const claim = fence(() => this.#store.claim(plan.id, binding, config.authorityId, authority.authorityDigest, config.maxVerifications));
|
|
462
|
+
if (claim.state === 'certificate')
|
|
463
|
+
return claim.certificate && claim.certificate.expiresAt > Date.now()
|
|
464
|
+
&& canonicalGrowthJson(claim.certificate.parent) === canonicalGrowthJson(parent)
|
|
465
|
+
&& verifyPluginRevisionVerificationCertificate(claim.certificate, authority, publicKey)
|
|
466
|
+
? { status: 'verified', certificate: claim.certificate } : { status: 'unknown', reason: 'stale-source' };
|
|
467
|
+
if (claim.state === 'rejected')
|
|
468
|
+
return { status: 'rejected', reason: claim.reason };
|
|
469
|
+
if (claim.state !== 'new')
|
|
470
|
+
return { status: 'unknown', reason: claim.reason ?? 'previous-unknown' };
|
|
471
|
+
admitted = { planId: plan.id, binding };
|
|
472
|
+
const planPluginKind = plan.pluginKind;
|
|
473
|
+
if (planPluginKind !== undefined && !PLUGIN_KINDS.has(planPluginKind)) {
|
|
474
|
+
throw new Error('prepared instruction revision pluginKind invalid');
|
|
475
|
+
}
|
|
476
|
+
const observer = await this.#loadRunner();
|
|
477
|
+
signal.throwIfAborted();
|
|
478
|
+
fence(() => { });
|
|
479
|
+
const discovery = await observer.run({ key: growthObjectDigest([binding, 'discover']), artifact, operation: { kind: 'discover' },
|
|
480
|
+
...(planPluginKind !== undefined ? { pluginKind: planPluginKind } : {}), signal });
|
|
481
|
+
if (discovery.status !== 'observed' || !discovery.quiescent || !discovery.schemaDigest || !discovery.schemas
|
|
482
|
+
|| !discovery.environment || !discovery.jobId || discovery.artifactSha256 !== plan.preparedEvidence.pack.sha256) {
|
|
483
|
+
this.#store.unknown(plan.id, binding, 'schema-observation-unknown');
|
|
484
|
+
return { status: 'unknown', reason: 'schema-observation-unknown' };
|
|
485
|
+
}
|
|
486
|
+
const effectiveKind = planPluginKind ?? discovery.pluginKind;
|
|
487
|
+
const projectedSchemas = projectCreationSchemas(discovery.schemas);
|
|
488
|
+
fence(() => this.#store.discovered(plan.id, binding, { jobId: discovery.jobId, schemaDigest: discovery.schemaDigest,
|
|
489
|
+
schemas: projectedSchemas, environment: discovery.environment }));
|
|
490
|
+
const task = { objective: `Owner-requested revision of ${plan.name} under instruction ${instruction.instructionId}`,
|
|
491
|
+
feedback: null, judgement: 'owner-instruction', name: plan.name };
|
|
492
|
+
const contract = await runNativeCreationTurn(this.ctx, { sessionId: `revision-contract-${binding.slice(0, 40)}`,
|
|
493
|
+
owner: config.owner, model, maxInputBytes: config.maxInputBytes, maxOutputTokens: config.maxOutputTokens - 1,
|
|
494
|
+
prompt: [...CASE_RULES, ...pluginKindContractAddendum(effectiveKind), `Case limit: ${config.maxCases}.`, `Fixed owner acceptance policy: ${config.policy}`].join('\n'),
|
|
495
|
+
data: { task, schemas: projectedSchemas }, signal, assertCurrent: () => fence(() => { }) });
|
|
496
|
+
const cases = parseCases(contract.value, config.maxCases, projectedSchemas);
|
|
497
|
+
if (cases.status === 'insufficient') {
|
|
498
|
+
fence(() => this.#store.unknown(plan.id, binding, 'contract-insufficient'));
|
|
499
|
+
return { status: 'unknown', reason: 'contract-insufficient' };
|
|
500
|
+
}
|
|
501
|
+
fence(() => this.#store.contract(plan.id, binding, { cases: cases.cases, outputDigest: contract.outputDigest,
|
|
502
|
+
sessionId: contract.sessionId }));
|
|
503
|
+
const observations = [];
|
|
504
|
+
const jobs = new Set([discovery.jobId]);
|
|
505
|
+
for (const item of cases.cases) {
|
|
506
|
+
signal.throwIfAborted();
|
|
507
|
+
fence(() => { });
|
|
508
|
+
const operation = { kind: 'invoke', schemaDigest: discovery.schemaDigest,
|
|
509
|
+
calls: [{ id: item.id, toolName: item.toolName, arguments: item.arguments }] };
|
|
510
|
+
const operationDigest = growthObjectDigest(operation);
|
|
511
|
+
fence(() => this.#store.claimCase(plan.id, binding, item.id, operationDigest));
|
|
512
|
+
const observed = await observer.run({ key: growthObjectDigest([binding, 'invoke', operationDigest]), artifact, operation,
|
|
513
|
+
...(effectiveKind !== undefined ? { pluginKind: effectiveKind } : {}), signal });
|
|
514
|
+
if (observed.status !== 'observed' || !observed.quiescent || !observed.jobId
|
|
515
|
+
|| observed.schemaDigest !== discovery.schemaDigest || observed.artifactSha256 !== plan.preparedEvidence.pack.sha256
|
|
516
|
+
|| canonicalGrowthJson(observed.environment) !== canonicalGrowthJson(discovery.environment)
|
|
517
|
+
|| observed.calls?.length !== 1 || observed.calls[0]?.id !== item.id || jobs.has(observed.jobId)) {
|
|
518
|
+
this.#store.unknown(plan.id, binding, 'case-observation-unknown');
|
|
519
|
+
return { status: 'unknown', reason: 'case-observation-unknown' };
|
|
520
|
+
}
|
|
521
|
+
const raw = observed.calls[0].result;
|
|
522
|
+
jobs.add(observed.jobId);
|
|
523
|
+
const observationDigest = growthObjectDigest(raw);
|
|
524
|
+
fence(() => this.#store.observation(plan.id, binding, item.id, { jobId: observed.jobId, operationDigest, observationDigest }));
|
|
525
|
+
observations.push({ caseId: item.id, jobId: observed.jobId, operationDigest, observationDigest });
|
|
526
|
+
if (!compareCase(item.expected, raw)) {
|
|
527
|
+
fence(() => this.#store.reject(plan.id, binding, 'case-mismatch'));
|
|
528
|
+
return { status: 'rejected', reason: 'case-mismatch' };
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const context = await port.inspectPreparedRevisionReviewContext(plan.id, signal);
|
|
532
|
+
fence(() => { });
|
|
533
|
+
if (Buffer.byteLength(context.patch) > config.maxInputBytes / 2 || context.changedPaths.length < 1
|
|
534
|
+
|| context.changedPaths.length > 32)
|
|
535
|
+
throw new Error('revision source review context exceeds bound');
|
|
536
|
+
fence(() => this.#store.claimReview(plan.id, binding));
|
|
537
|
+
const review = await runNativeCreationTurn(this.ctx, { sessionId: `revision-source-${binding.slice(0, 40)}`,
|
|
538
|
+
owner: config.owner, model, maxInputBytes: config.maxInputBytes,
|
|
539
|
+
maxOutputTokens: config.maxOutputTokens - contract.outputTokens,
|
|
540
|
+
prompt: [...REVIEW_RULES, ...REVISION_RULES,
|
|
541
|
+
...(effectiveKind && effectiveKind !== 'tool' ? [`Candidate pluginKind: ${effectiveKind}.`] : []),
|
|
542
|
+
`Fixed owner acceptance policy: ${config.policy}`].join('\n'),
|
|
543
|
+
data: { task, parent, cases: cases.cases, changedPaths: context.changedPaths, patch: context.patch },
|
|
544
|
+
signal, assertCurrent: () => fence(() => { }) });
|
|
545
|
+
const verdict = parseReview(review.value);
|
|
546
|
+
if (contract.outputTokens + review.outputTokens > config.maxOutputTokens)
|
|
547
|
+
throw new Error('revision model budget exceeded');
|
|
548
|
+
if (verdict.decision !== 'approved') {
|
|
549
|
+
fence(() => this.#store.reject(plan.id, binding, 'source-review-rejected'));
|
|
550
|
+
return { status: 'rejected', reason: 'source-review-rejected' };
|
|
551
|
+
}
|
|
552
|
+
const verifiedAt = Date.now();
|
|
553
|
+
const expiresAt = Math.min(verifiedAt + config.receiptTtlMs, config.expiresAt, plan.expiresAt, revision.grant.expiresAt);
|
|
554
|
+
if (expiresAt <= verifiedAt)
|
|
555
|
+
throw new Error('revision verification windows closed');
|
|
556
|
+
const body = {
|
|
557
|
+
protocol: 'assistant-growth/revision-verification/v3',
|
|
558
|
+
verificationId: `revision-${binding.slice(0, 40)}`,
|
|
559
|
+
authority, plan: { id: plan.id, digest: plan.digest, name: plan.name,
|
|
560
|
+
sourceTreeDigest: plan.sourceCheck.treeDigest, sourcePatchDigest: plan.sourceCheck.patchDigest,
|
|
561
|
+
artifactSha256: plan.preparedEvidence.pack.sha256, artifactBytes: artifact.length,
|
|
562
|
+
generatorDigest: plan.generatorDigest }, parent, source: sourceBinding,
|
|
563
|
+
contractDigest: growthObjectDigest(cases.cases), schemaDigest: discovery.schemaDigest,
|
|
564
|
+
environment: discovery.environment, model,
|
|
565
|
+
budget: { modelCalls: 2, maxOutputTokens: config.maxOutputTokens, maxDurationMs: config.maxDurationMs,
|
|
566
|
+
maxCases: config.maxCases }, sessions: { contract: contract.sessionId, sourceReview: review.sessionId },
|
|
567
|
+
observations, reviewDigest: review.outputDigest, verifiedAt, expiresAt,
|
|
568
|
+
};
|
|
569
|
+
const signature = sign(null, Buffer.from(pluginRevisionVerificationSigningPayload(body)), privateKey).toString('base64url');
|
|
570
|
+
const certificate = { ...body, signature };
|
|
571
|
+
fence(() => this.#store.certificate(plan.id, binding, certificate));
|
|
572
|
+
admitted = undefined;
|
|
573
|
+
return { status: 'verified', certificate };
|
|
574
|
+
}
|
|
575
|
+
catch {
|
|
576
|
+
const reason = signal.aborted ? 'interrupted' : admitted ? 'verification-unknown' : 'stale-source';
|
|
577
|
+
if (admitted) {
|
|
578
|
+
try {
|
|
579
|
+
this.#store.unknown(admitted.planId, admitted.binding, reason);
|
|
580
|
+
}
|
|
581
|
+
catch { /* completed or closed ledger remains authoritative */ }
|
|
582
|
+
}
|
|
583
|
+
return { status: 'unknown', reason };
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
close() {
|
|
587
|
+
return this.#closing ??= (async () => {
|
|
588
|
+
this.#abort.abort(new Error('revision review disposed'));
|
|
589
|
+
try {
|
|
590
|
+
await Promise.allSettled(this.#flights.values());
|
|
591
|
+
await this.#runnerLoading?.catch(() => undefined);
|
|
592
|
+
await this.#runner?.close();
|
|
593
|
+
}
|
|
594
|
+
finally {
|
|
595
|
+
this.#store.close();
|
|
596
|
+
}
|
|
597
|
+
})();
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
//# sourceMappingURL=revision-review.js.map
|