@evomap/evolver-proxy 2.0.48 → 2.0.49

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.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env -S node --
2
2
  import { createHash, randomBytes } from 'node:crypto';
3
3
  import { readFileSync, realpathSync, rmSync } from 'node:fs';
4
- import { homedir } from 'node:os';
4
+ import { homedir, userInfo } from 'node:os';
5
5
  import { join, resolve } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
7
7
  import { bootstrap as coreBootstrap, daemon, events, exec as execCore, hub as hubNs, mailbox, util, verify } from '@evomap/evolver-core';
@@ -388,7 +388,7 @@ export function proxyUsage(command = 'evolver-proxy') {
388
388
  '',
389
389
  'Useful options are configured through env or EVOLVER_ENV_FILE:',
390
390
  ' EVOLVER_IPC_PORT, EVOLVER_IPC_TOKEN, EVOLVER_PROXY_SETTINGS_FILE',
391
- ' EVOLVER_NATIVE_PUBLISH_VERIFIER=1, EVOLVER_PUBLISH_VALIDATION_ROOT=<dir> (必须显式设置)',
391
+ ' EVOLVER_PUBLISH_VALIDATION_ROOT=<dir> (HTTP 发布验证根目录;隔离可用时后台验证按 Gene 自建副本)',
392
392
  ' EVOLVER_SELF_UPDATE, EVOLVER_LLM_TRACE_CAPTURE_BODIES',
393
393
  '',
394
394
  ].join('\n');
@@ -698,7 +698,8 @@ export function createProxyDaemonDeps(options) {
698
698
  const env = options.env ?? process.env;
699
699
  const traceBackfill = resolveTraceBackfillConfig(env);
700
700
  const heartbeatIntervalMs = positiveIntegerEnv(env['HEARTBEAT_INTERVAL_MS']);
701
- const publishExecutionVerifier = resolveNativePublishExecutionVerifier(env);
701
+ const verifierFactory = nativeVerifierFactory();
702
+ const publishExecutionVerifier = configuredRootVerifier(env, verifierFactory);
702
703
  const hubAuthFailurePolicy = resolveHubAuthFailurePolicy(env);
703
704
  const triggerFeedback = options.triggerFeedback ?? execCore.createTriggerFeedbackLocalFacade(env);
704
705
  return {
@@ -726,69 +727,104 @@ export function createProxyDaemonDeps(options) {
726
727
  ...(selfUpdate ? { selfUpdate } : {}),
727
728
  ...(traceBackfill ? { traceBackfill } : {}),
728
729
  ...(publishExecutionVerifier ? { publishExecutionVerifier } : {}),
730
+ ...(verifierFactory ? { publishVerifierForRoot: verifierFactory } : {}),
729
731
  };
730
732
  }
731
- /**
732
- * 仅在显式启用且具备完整 OS 隔离时接入本地发布验证器;默认仍保持 draft-only。
733
- * 这样不会把普通桌面进程意外变成可发布执行器,也不会在 Windows/macOS 上静默降级为弱隔离。
734
- */
735
- function resolveNativePublishExecutionVerifier(env) {
736
- if (env['EVOLVER_NATIVE_PUBLISH_VERIFIER']?.trim() !== '1')
737
- return undefined;
738
- // 原生发布验证器只能在完整的 OS 隔离可用时装配。Windows/macOS 目前没有
739
- // 与 Linux namespace、只读文件系统和 cgroup 等价的实现,必须保持 draft-only,
740
- // 不能先暴露一个运行时必然失败的“验证器”能力。
733
+ function nativeValidationChecks(commands, plan) {
734
+ if (!plan)
735
+ return commands.map((command) => ({ command }));
736
+ return plan.checks.length === commands.length && plan.checks.every((check, index) => check.command === commands[index])
737
+ ? plan.checks : undefined;
738
+ }
739
+ function nativeVerifierFactory() {
741
740
  try {
742
741
  if (!verify.readOnlyIsolationAvailable())
743
742
  return undefined;
744
743
  }
745
744
  catch {
746
- // 隔离探测本身失败也必须保持 fail-closed,而不能阻止代理启动。
747
745
  return undefined;
748
746
  }
749
- // 发布验证必须绑定到调用方明确配置的项目根目录;回退到 daemon 当前目录会让验证对象与发布对象脱钩。
747
+ return (validationRoot, plan) => {
748
+ const rootFingerprint = hubNs.publishBindingDigest('publish-validation-root.v1', validationRoot);
749
+ const scrubLocalPaths = localPathScrubber([
750
+ [validationRoot, '.'],
751
+ ...(plan ? [[plan.dependencySourceRoot, '.']] : []),
752
+ [homedir(), '~'],
753
+ [userInfo().homedir, '~'],
754
+ ]);
755
+ return async (input, signal) => {
756
+ const commands = Array.isArray(input.validation)
757
+ ? input.validation.filter((value) => typeof value === 'string').map((value) => value.trim()).filter(Boolean)
758
+ : [];
759
+ if (commands.length === 0
760
+ || commands.length > 8
761
+ || !Array.isArray(input.validation)
762
+ || input.validation.length !== commands.length
763
+ || commands.some((command) => command.length > 180
764
+ || !verify.isValidationCommandAllowed(command)
765
+ || verify.sanitizeExecutionCommand(command).blocked)
766
+ || signal.aborted
767
+ || !hubNs.isPublishVerificationRequest(input['verification_request'])
768
+ || input['verification_request'].validationDigest !== hubNs.publishBindingDigest('publish-validation.v1', commands))
769
+ return null;
770
+ const checks = nativeValidationChecks(commands, plan);
771
+ if (!checks)
772
+ return null;
773
+ const result = await verify.runSandboxedChecks(checks, validationRoot, {
774
+ requireIsolation: true,
775
+ signal,
776
+ resourceBudget: VALIDATION_RESOURCE_BUDGET,
777
+ ...(plan ? { dependencySourceRoot: plan.dependencySourceRoot, writableCheckout: true } : {}),
778
+ });
779
+ if (signal.aborted || !result.passed || result.results.length !== commands.length)
780
+ return null;
781
+ return {
782
+ binding: { request: structuredClone(input['verification_request']), rootFingerprint },
783
+ validation: commands,
784
+ trace: result.results.map((row) => ({
785
+ command: row.cmd,
786
+ exit: row.exitCode ?? 1,
787
+ ...(row.stdoutSummary ? { summary: scrubLocalPaths(row.stdoutSummary) } : {}),
788
+ })),
789
+ };
790
+ };
791
+ };
792
+ }
793
+ // Real check commands run whole test suites: vitest forks a worker per core and needs well over the 1 GiB default.
794
+ const VALIDATION_RESOURCE_BUDGET = { memoryBytes: 4 * 1024 * 1024 * 1024, processes: 256 };
795
+ /**
796
+ * The trace is published, local paths are not: tools print their working directory (vitest's "RUN <root>").
797
+ * Each path is matched as given, canonicalized, and without macOS's /private prefix, longest first so a checkout
798
+ * under HOME becomes "." rather than "~/...".
799
+ */
800
+ function localPathScrubber(entries) {
801
+ const spellings = entries.flatMap(([path, replacement]) => {
802
+ let canonical = path;
803
+ try {
804
+ canonical = realpathSync(path);
805
+ }
806
+ catch { /* keep the given spelling */ }
807
+ return [path, resolve(path), canonical, canonical.replace(/^\/private(?=\/)/, '')]
808
+ .filter((spelling) => spelling.length > 1)
809
+ .map((spelling) => [spelling, replacement]);
810
+ });
811
+ const ordered = [...new Map(spellings).entries()].sort(([a], [b]) => b.length - a.length);
812
+ return (text) => ordered.reduce((out, [spelling, replacement]) => out.split(spelling).join(replacement), text);
813
+ }
814
+ /**
815
+ * HTTP callers verify against the root the deployer configured; falling back to the daemon's cwd would detach
816
+ * the verified tree from the published one.
817
+ */
818
+ function configuredRootVerifier(env, factory) {
750
819
  const configuredRoot = env['EVOLVER_PUBLISH_VALIDATION_ROOT']?.trim();
751
- if (!configuredRoot)
820
+ if (!factory || !configuredRoot)
752
821
  return undefined;
753
- let validationRoot;
754
822
  try {
755
- validationRoot = realpathSync(resolve(configuredRoot));
823
+ return factory(realpathSync(resolve(configuredRoot)));
756
824
  }
757
825
  catch {
758
826
  return undefined;
759
827
  }
760
- const rootFingerprint = hubNs.publishBindingDigest('publish-validation-root.v1', validationRoot);
761
- return async (input, signal) => {
762
- const commands = Array.isArray(input.validation)
763
- ? input.validation.filter((value) => typeof value === 'string').map((value) => value.trim()).filter(Boolean)
764
- : [];
765
- if (commands.length === 0
766
- || commands.length > 8
767
- || !Array.isArray(input.validation)
768
- || input.validation.length !== commands.length
769
- || commands.some((command) => command.length > 180
770
- || !verify.isValidationCommandAllowed(command)
771
- || verify.sanitizeExecutionCommand(command).blocked)
772
- || signal.aborted
773
- || !hubNs.isPublishVerificationRequest(input['verification_request'])
774
- || input['verification_request'].validationDigest !== hubNs.publishBindingDigest('publish-validation.v1', commands))
775
- return null;
776
- const result = await verify.runSandboxedValidation(commands, validationRoot, {
777
- requireIsolation: true,
778
- signal,
779
- });
780
- if (signal.aborted || !result.passed || result.results.length !== commands.length)
781
- return null;
782
- return {
783
- binding: { request: structuredClone(input['verification_request']), rootFingerprint },
784
- validation: commands,
785
- trace: result.results.map((row) => ({
786
- command: row.cmd,
787
- exit: row.exitCode ?? 1,
788
- ...(row.stdoutSummary ? { summary: row.stdoutSummary } : {}),
789
- })),
790
- };
791
- };
792
828
  }
793
829
  function positiveIntegerEnv(value) {
794
830
  const trimmed = value?.trim();
@@ -0,0 +1,53 @@
1
+ import { assetstore, type hub as hubNs, type verify } from '@evomap/evolver-core';
2
+ import { type VerificationContextStore } from './observedEvidence.js';
3
+ export declare const CURATION_INTERVAL_MS: number;
4
+ export declare const CURATION_IDLE_MS: number;
5
+ export type ExecutionVerifier = (input: hubNs.ConversationDistillInput, signal: AbortSignal) => Promise<hubNs.ConversationDistillVerifiedExecution | null>;
6
+ export interface CuratedPublication {
7
+ queued: boolean;
8
+ reason?: string;
9
+ envelopeId?: string;
10
+ assetIds?: string[];
11
+ }
12
+ export type CuratedMailboxCommit = (send: () => boolean) => 'stored' | 'not_stored' | 'review_not_approved' | 'verification_plan_changed';
13
+ /** Where each check runs inside the copy, and which project lends the copy its installed dependencies. */
14
+ export interface VerificationPlan {
15
+ checks: readonly verify.ObservedCheck[];
16
+ dependencySourceRoot: string;
17
+ }
18
+ export interface CurationDeps {
19
+ store: assetstore.AssetStoreProvider;
20
+ review: assetstore.ReviewLedger;
21
+ contexts: VerificationContextStore;
22
+ /** Absent where read-only isolation is unavailable: genes then wait, never published unverified. */
23
+ verifierForRoot: ((root: string, plan?: VerificationPlan) => ExecutionVerifier) | undefined;
24
+ verify: (body: hubNs.ConversationDistillInput, verifier: ExecutionVerifier, planDigest: string) => Promise<hubNs.ConversationDistillVerifiedExecution | undefined>;
25
+ enqueue: (body: hubNs.ConversationDistillInput, verified: hubNs.ConversationDistillVerifiedExecution, planDigest: string, commit: CuratedMailboxCommit) => Promise<CuratedPublication>;
26
+ now: () => Date;
27
+ scratchDir?: string;
28
+ }
29
+ export interface CurationReport {
30
+ promoted: string[];
31
+ attempted?: {
32
+ geneAssetId: string;
33
+ result: string;
34
+ };
35
+ }
36
+ /**
37
+ * One idle pass: promote probation drafts that earned it, then verify and publish at most one promoted Gene.
38
+ * Every refusal leaves the Gene local and is retried on a later pass.
39
+ */
40
+ export declare function runCurationTick(deps: CurationDeps): Promise<CurationReport>;
41
+ export interface CurationStatus {
42
+ publish_verifier: 'available' | 'unavailable';
43
+ publish_verifier_reason?: 'read_only_isolation_unavailable';
44
+ awaiting_verification: number;
45
+ published: number;
46
+ last_attempt?: {
47
+ gene_asset_id: string;
48
+ result: string;
49
+ at: string;
50
+ };
51
+ }
52
+ /** Why promoted genes are or are not leaving this machine, for /proxy/status and the plugin's status command. */
53
+ export declare function curationStatus(contexts: VerificationContextStore, review: assetstore.ReviewLedger, verifierAvailable: boolean): CurationStatus;
@@ -0,0 +1,156 @@
1
+ import { mkdtempSync, rmSync } from 'node:fs';
2
+ import { tmpdir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ import { algo, assetstore, wire } from '@evomap/evolver-core';
5
+ import { createRecordedCheckout, RecordedTreeMismatch } from './curationGit.js';
6
+ import { hasVerificationPlan } from './observedEvidence.js';
7
+ export const CURATION_INTERVAL_MS = 10 * 60_000;
8
+ export const CURATION_IDLE_MS = 2 * 60_000;
9
+ function geneDigest(gene) {
10
+ return wire.canonicalize(gene);
11
+ }
12
+ async function withValidationCopy(context, scratchDir, use) {
13
+ let parent;
14
+ try {
15
+ let copy;
16
+ try {
17
+ parent = mkdtempSync(join(scratchDir, 'evolver-validation-'));
18
+ copy = await createRecordedCheckout(context, parent);
19
+ }
20
+ catch (error) {
21
+ return error instanceof RecordedTreeMismatch ? 'not_reproducible' : 'git_setup_failed';
22
+ }
23
+ return await use(copy);
24
+ }
25
+ finally {
26
+ if (parent)
27
+ rmSync(parent, { recursive: true, force: true });
28
+ }
29
+ }
30
+ function verifiedCapsule(context, gene, calls) {
31
+ const { capsule } = algo.solidify({
32
+ geneId: context.geneId,
33
+ trigger: Array.isArray(gene['signals_match']) ? gene['signals_match'].map(String).slice(0, 8) : [],
34
+ summary: `Host-verified validation of ${context.geneId}`,
35
+ confidence: 0.9,
36
+ outcome: { status: 'success', score: 0.9 },
37
+ proofOfWork: { kind: 'tool_call_trace', tool_call_trace: { calls, ref: 'host-verified' } },
38
+ strongEvidence: true,
39
+ });
40
+ return capsule;
41
+ }
42
+ const TERMINAL_RESULTS = new Set(['not_reproducible', 'new_failures', 'verification_plan_unavailable']);
43
+ const awaitsVerification = (context) => !context.published && !TERMINAL_RESULTS.has(context.lastAttempt?.result ?? '');
44
+ async function pendingCandidate(deps) {
45
+ const queue = deps.contexts.list()
46
+ .filter(awaitsVerification)
47
+ .sort((a, b) => (a.lastAttempt?.at ?? '').localeCompare(b.lastAttempt?.at ?? ''));
48
+ for (const context of queue) {
49
+ if (deps.review.get(context.geneAssetId)?.state !== 'approved')
50
+ continue;
51
+ const gene = await deps.store.get(context.geneAssetId);
52
+ if (gene?.type === 'Gene')
53
+ return { context, gene };
54
+ }
55
+ return undefined;
56
+ }
57
+ function currentObservation(contexts, attempted) {
58
+ const current = contexts.get(attempted.geneAssetId);
59
+ return current && current.recordedAt === attempted.recordedAt && current.planDigest === attempted.planDigest ? current : undefined;
60
+ }
61
+ function saveCurrentObservation(contexts, attempted, update) {
62
+ const current = currentObservation(contexts, attempted);
63
+ if (!current || (update.verifiedAt !== undefined && !hasVerificationPlan(current)))
64
+ return false;
65
+ contexts.put({ ...current, ...update });
66
+ return true;
67
+ }
68
+ function isCurrentPlan(contexts, attempted) {
69
+ const current = currentObservation(contexts, attempted);
70
+ return current !== undefined && !current.published && hasVerificationPlan(current);
71
+ }
72
+ async function enqueueCurrentVerification(deps, context, body, verified, geneBefore) {
73
+ if (!algo.isGenePublishEligible(await assetstore.aggregateLearningHistory(deps.store, context.geneId)))
74
+ return 'not_publish_eligible';
75
+ const current = await deps.store.get(context.geneAssetId);
76
+ if (!current || geneDigest(current) !== geneBefore)
77
+ return 'gene_changed';
78
+ const isCurrent = () => isCurrentPlan(deps.contexts, context);
79
+ if (!isCurrent())
80
+ return 'verification_plan_changed';
81
+ const at = deps.now().toISOString();
82
+ // The mark is written in the same synchronous step as the send, so no later plan can find the Gene unpublished.
83
+ const commit = (send) => deps.review.withExplicitApproval(context.geneAssetId, () => {
84
+ if (!isCurrent())
85
+ return 'verification_plan_changed';
86
+ if (!send())
87
+ return 'not_stored';
88
+ saveCurrentObservation(deps.contexts, context, { published: { at, envelopeId: '', assetIds: [] } });
89
+ return 'stored';
90
+ }) ?? 'review_not_approved';
91
+ const publication = await deps.enqueue(body, verified, context.planDigest, commit);
92
+ if (!publication.queued)
93
+ return publication.reason ?? 'publish_blocked';
94
+ const latest = deps.contexts.get(context.geneAssetId);
95
+ if (latest) {
96
+ deps.contexts.put({ ...latest, published: { at, envelopeId: publication.envelopeId ?? '', assetIds: publication.assetIds ?? [] } });
97
+ }
98
+ return 'published';
99
+ }
100
+ async function curateOne(deps, context, gene) {
101
+ if (!hasVerificationPlan(context))
102
+ return 'verification_plan_unavailable';
103
+ const verifierForRoot = deps.verifierForRoot;
104
+ const history = await assetstore.aggregateLearningHistory(deps.store, context.geneId);
105
+ if (context.failedAtVerification !== undefined && history.failed !== context.failedAtVerification)
106
+ return 'new_failures';
107
+ const before = geneDigest(gene);
108
+ const checks = Object.freeze(context.checks.map((check) => Object.freeze({ ...check })));
109
+ const validation = Object.freeze(checks.map(({ command }) => command));
110
+ const body = { ...context.distillInput, validation, publish: true, persist: true };
111
+ // The Linux verifier mounts scratch over /tmp before entering the checkout.
112
+ const scratch = deps.scratchDir ?? (process.platform === 'linux' ? '/var/tmp' : tmpdir());
113
+ const verified = await withValidationCopy(context, scratch, (root) => deps.verify(body, verifierForRoot(root, { checks, dependencySourceRoot: context.root }), context.planDigest));
114
+ if (typeof verified === 'string')
115
+ return verified;
116
+ if (!verified)
117
+ return 'verification_failed';
118
+ if (!isCurrentPlan(deps.contexts, context))
119
+ return 'verification_plan_changed';
120
+ if (!deps.review.isExplicitlyApproved(context.geneAssetId))
121
+ return 'review_not_approved';
122
+ await deps.store.put(verifiedCapsule(context, gene, verified.trace.length));
123
+ if (!saveCurrentObservation(deps.contexts, context, {
124
+ verifiedAt: deps.now().toISOString(), failedAtVerification: history.failed,
125
+ }))
126
+ return 'verification_plan_changed';
127
+ return enqueueCurrentVerification(deps, context, body, verified, before);
128
+ }
129
+ /**
130
+ * One idle pass: promote probation drafts that earned it, then verify and publish at most one promoted Gene.
131
+ * Every refusal leaves the Gene local and is retried on a later pass.
132
+ */
133
+ export async function runCurationTick(deps) {
134
+ const promoted = await algo.autoPromoteProbationGenes(deps.store, deps.review, { by: 'proxy-auto-promote' });
135
+ if (!deps.verifierForRoot)
136
+ return { promoted };
137
+ const candidate = await pendingCandidate(deps);
138
+ if (!candidate)
139
+ return { promoted };
140
+ const result = await curateOne(deps, candidate.context, candidate.gene);
141
+ saveCurrentObservation(deps.contexts, candidate.context, { lastAttempt: { at: deps.now().toISOString(), result } });
142
+ return { promoted, attempted: { geneAssetId: candidate.context.geneAssetId, result } };
143
+ }
144
+ /** Why promoted genes are or are not leaving this machine, for /proxy/status and the plugin's status command. */
145
+ export function curationStatus(contexts, review, verifierAvailable) {
146
+ const all = contexts.list();
147
+ const latest = all.filter((context) => context.lastAttempt)
148
+ .sort((a, b) => (b.lastAttempt.at).localeCompare(a.lastAttempt.at))[0];
149
+ return {
150
+ publish_verifier: verifierAvailable ? 'available' : 'unavailable',
151
+ ...(verifierAvailable ? {} : { publish_verifier_reason: 'read_only_isolation_unavailable' }),
152
+ awaiting_verification: all.filter((context) => awaitsVerification(context) && review.get(context.geneAssetId)?.state === 'approved').length,
153
+ published: all.filter((context) => context.published).length,
154
+ ...(latest ? { last_attempt: { gene_asset_id: latest.geneAssetId, result: latest.lastAttempt.result, at: latest.lastAttempt.at } } : {}),
155
+ };
156
+ }
@@ -0,0 +1,5 @@
1
+ import type { VerificationContext } from './observedEvidence.js';
2
+ export declare class RecordedTreeMismatch extends Error {
3
+ }
4
+ /** All checkout and patch operations use our own config and index, even for a linked source worktree. */
5
+ export declare function createRecordedCheckout(context: VerificationContext, parent: string): Promise<string>;
@@ -0,0 +1,140 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { accessSync, closeSync, constants, fstatSync, lstatSync, openSync, readSync, realpathSync, statSync, writeFileSync } from 'node:fs';
3
+ import { delimiter, isAbsolute, join, relative, resolve, sep } from 'node:path';
4
+ import { exec } from '@evomap/evolver-core';
5
+ const GIT_TIMEOUT_MS = 60_000;
6
+ const GIT_POINTER_MAX_BYTES = 8192;
7
+ const GIT_CONFIG_MAX_BYTES = 1024 * 1024;
8
+ const PATCH_REJECTION = /^error: (?:patch failed: .+|.+: patch does not apply|binary patch does not apply to '.+'|corrupt patch at line \d+|No valid patches in input \(allow with "--allow-empty"\))$/;
9
+ const SAFE_GIT_OPTIONS = [
10
+ '--no-pager', '-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false',
11
+ '-c', 'core.attributesFile=/dev/null', '-c', 'core.autocrlf=false',
12
+ '-c', 'submodule.recurse=false', '-c', 'protocol.allow=never',
13
+ '-c', 'gc.auto=0', '-c', 'maintenance.auto=false',
14
+ ];
15
+ export class RecordedTreeMismatch extends Error {
16
+ }
17
+ function inside(root, path) {
18
+ const name = relative(root, path);
19
+ return name !== '..' && !name.startsWith(`..${sep}`) && !isAbsolute(name);
20
+ }
21
+ function hostGit(root, env) {
22
+ const executable = process.platform === 'win32' ? 'git.exe' : 'git';
23
+ for (const directory of env['PATH']?.split(delimiter) ?? []) {
24
+ if (!isAbsolute(directory))
25
+ continue;
26
+ try {
27
+ const file = realpathSync.native(join(directory, executable));
28
+ if (inside(root, file) || !statSync(file).isFile())
29
+ continue;
30
+ accessSync(file, constants.X_OK);
31
+ return file;
32
+ }
33
+ catch { /* Look for the next native Git on the daemon PATH. */ }
34
+ }
35
+ throw new Error('curation Git executable unavailable outside source checkout');
36
+ }
37
+ function readGitFile(path, limit) {
38
+ let original;
39
+ try {
40
+ original = lstatSync(path);
41
+ }
42
+ catch (error) {
43
+ if (error.code === 'ENOENT')
44
+ return undefined;
45
+ throw error;
46
+ }
47
+ if (!original.isFile() || original.size >= limit)
48
+ throw new Error('invalid curation Git metadata');
49
+ const fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
50
+ try {
51
+ const stat = fstatSync(fd);
52
+ if (!stat.isFile() || stat.dev !== original.dev || stat.ino !== original.ino || stat.size !== original.size) {
53
+ throw new Error('curation Git metadata changed');
54
+ }
55
+ const bytes = Buffer.alloc(stat.size + 1);
56
+ const count = readSync(fd, bytes, 0, bytes.length, 0);
57
+ if (count !== stat.size)
58
+ throw new Error('curation Git metadata changed');
59
+ return bytes.subarray(0, count);
60
+ }
61
+ finally {
62
+ closeSync(fd);
63
+ }
64
+ }
65
+ function readGitPointer(path, prefix) {
66
+ const bytes = readGitFile(path, GIT_POINTER_MAX_BYTES);
67
+ if (bytes === undefined)
68
+ return undefined;
69
+ const line = new TextDecoder('utf-8', { fatal: true }).decode(bytes).replace(/\r?\n$/, '');
70
+ if (!line.startsWith(prefix) || line.length <= prefix.length || /[\0\r\n]/.test(line)) {
71
+ throw new Error('invalid curation Git pointer');
72
+ }
73
+ return line.slice(prefix.length);
74
+ }
75
+ function sourceGitDirectory(root) {
76
+ const dotGit = join(root, '.git');
77
+ const gitDir = lstatSync(dotGit).isDirectory() ? dotGit : resolve(root, readGitPointer(dotGit, 'gitdir: ') ?? '');
78
+ if (gitDir === root)
79
+ throw new Error('curation Git directory unavailable');
80
+ const common = readGitPointer(join(gitDir, 'commondir'), '');
81
+ return realpathSync.native(common === undefined ? gitDir : resolve(gitDir, common));
82
+ }
83
+ function rejectsRecordedTree(args, diagnostic) {
84
+ if (args[0] === 'rev-parse')
85
+ return diagnostic === 'fatal: Needed a single revision';
86
+ return args[0] === 'apply' && diagnostic.split(/\r?\n/).every((line) => PATCH_REJECTION.test(line));
87
+ }
88
+ function gitRunner(bin, env) {
89
+ return (args, cwd, input) => new Promise((resolve, reject) => {
90
+ const child = execFile(bin, [...SAFE_GIT_OPTIONS, ...args], {
91
+ cwd, timeout: GIT_TIMEOUT_MS, encoding: 'utf8', env,
92
+ }, (error, stdout, stderr) => {
93
+ const diagnostic = stderr.trim();
94
+ if (!error || (args[0] === 'config' && error.code === 1 && !diagnostic)) {
95
+ resolve(stdout.trim());
96
+ return;
97
+ }
98
+ const mismatch = typeof error.code === 'number' && !error.killed && !error.signal && rejectsRecordedTree(args, diagnostic);
99
+ reject(mismatch ? new RecordedTreeMismatch('recorded Git tree mismatch') : error);
100
+ });
101
+ if (input !== undefined)
102
+ child.stdin?.on('error', () => { }).end(input);
103
+ });
104
+ }
105
+ /** All checkout and patch operations use our own config and index, even for a linked source worktree. */
106
+ export async function createRecordedCheckout(context, parent) {
107
+ const root = realpathSync.native(context.root);
108
+ const gitDir = sourceGitDirectory(root);
109
+ const objects = realpathSync.native(join(gitDir, 'objects'));
110
+ if (!statSync(objects).isDirectory() || /[\0\r\n]/.test(objects))
111
+ throw new Error('invalid curation Git object directory');
112
+ const nullDevice = '/dev/null';
113
+ const env = {
114
+ ...exec.scrubAgentEnv(process.env),
115
+ LC_ALL: 'C', LANGUAGE: 'C',
116
+ GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_SYSTEM: nullDevice, GIT_CONFIG_GLOBAL: nullDevice,
117
+ GIT_ATTR_NOSYSTEM: '1', GIT_TERMINAL_PROMPT: '0', GIT_NO_LAZY_FETCH: '1', GIT_NO_REPLACE_OBJECTS: '1',
118
+ // An explicit ceiling keeps the metadata-only config command outside any ancestor repository.
119
+ GIT_CEILING_DIRECTORIES: parent,
120
+ };
121
+ const git = gitRunner(hostGit(root, env), env);
122
+ const sourceConfig = readGitFile(join(gitDir, 'config'), GIT_CONFIG_MAX_BYTES);
123
+ if (!sourceConfig)
124
+ throw new Error('curation Git config unavailable');
125
+ const configured = await git(['config', '--file', '-', '--no-includes', '--get', 'extensions.objectFormat'], parent, sourceConfig);
126
+ const format = configured || 'sha1';
127
+ if (format !== 'sha1' && format !== 'sha256')
128
+ throw new Error('unsupported curation Git object format');
129
+ const copy = join(parent, 'tree');
130
+ await git(['init', '--quiet', '--template=', `--object-format=${format}`, '--', copy], parent);
131
+ // Git reads alternate objects but writes all local metadata and new objects to the disposable repository.
132
+ // Only tracked attributes can select built-in checkout conversions; the source's filter config is absent.
133
+ const objectPath = process.platform === 'win32' ? objects.replaceAll('\\', '/') : objects;
134
+ writeFileSync(join(copy, '.git', 'objects', 'info', 'alternates'), `${objectPath}\n`, { flag: 'wx', mode: 0o600 });
135
+ const commit = await git(['rev-parse', '--verify', '--end-of-options', `${context.head}^{commit}`], copy);
136
+ await git(['checkout', '--quiet', '--detach', commit], copy);
137
+ if (context.diff)
138
+ await git(['apply', '--whitespace=nowarn', '-'], copy, context.diff);
139
+ return copy;
140
+ }
@@ -49,11 +49,14 @@ export function rootEventReuseTallies(path = events.rootEventsPath()) {
49
49
  }
50
50
  };
51
51
  }
52
- function recalledAsset(hit) {
52
+ const TRIAL_NOTICE = '[On trial, not yet verified] ';
53
+ const TRIAL_BAN_FAILURES = 2;
54
+ function recalledAsset(hit, trial) {
53
55
  // Only notes recall matched to this project leave the store; a record's own fields cannot carry any.
54
56
  const { project_notes: _unvouched, recall: _claimed, ...gene } = hit.gene;
55
57
  return {
56
58
  ...gene,
59
+ ...(trial && typeof gene['summary'] === 'string' ? { summary: `${TRIAL_NOTICE}${gene['summary']}` } : {}),
57
60
  similarity: LOCAL_SIMILARITY_FLOOR + (1 - LOCAL_SIMILARITY_FLOOR) * hit.relevance,
58
61
  ...(hit.projectNotes.length > 0 ? { project_notes: hit.projectNotes } : {}),
59
62
  recall: {
@@ -64,9 +67,32 @@ function recalledAsset(hit) {
64
67
  project_match: hit.projectMatch,
65
68
  reuse: hit.reuse,
66
69
  merged_asset_ids: hit.mergedAssetIds,
70
+ ...(trial ? { trial: true } : {}),
67
71
  },
68
72
  };
69
73
  }
74
+ /** Quarantined drafts that may be tried once: every other gate still applies, and two failures end the trial. */
75
+ function trialPool(genes, capsules, eligibility) {
76
+ const onTrial = genes.filter((gene) => eligibility.review.get(gene.asset_id)?.state === 'quarantined');
77
+ if (onTrial.length === 0)
78
+ return [];
79
+ const review = new Map(eligibility.review);
80
+ for (const gene of onTrial)
81
+ review.delete(gene.asset_id);
82
+ const relaxed = { ...eligibility, review };
83
+ const failures = new Map();
84
+ for (const capsule of capsules) {
85
+ if (capsule['outcome']?.status !== 'failed' || capsule['resolution_status'] === 'pending')
86
+ continue;
87
+ const geneId = String(capsule['gene'] ?? '');
88
+ failures.set(geneId, (failures.get(geneId) ?? 0) + 1);
89
+ }
90
+ const trialGenes = onTrial.filter((gene) => (failures.get(String(gene['id'] ?? gene.asset_id)) ?? 0) < TRIAL_BAN_FAILURES
91
+ && assetstore.assetContextEligibilityReason(gene, relaxed) === undefined);
92
+ const ids = new Set(trialGenes.map((gene) => String(gene['id'] ?? gene.asset_id)));
93
+ return [...trialGenes, ...capsules.filter((capsule) => ids.has(String(capsule['gene'] ?? ''))
94
+ && assetstore.assetContextEligibilityReason(capsule, relaxed) === undefined)];
95
+ }
70
96
  function diagnosticsOf(result) {
71
97
  return {
72
98
  considered: result.considered,
@@ -103,10 +129,19 @@ export async function recallFromLocalStore(store, text, projectDir) {
103
129
  const contexts = typeof store.readLocalContextsV1 === 'function' && capsules.length > 0
104
130
  ? await store.readLocalContextsV1(capsules.map((capsule) => capsule.asset_id)) : [];
105
131
  const query = { text, projectFingerprint: await assetstore.projectFingerprint(projectDir) };
132
+ const outcomeOf = (result, trial) => {
133
+ const rawAssets = new Map(result.hits.map((hit) => [recalledAsset(hit, trial), hit.gene]));
134
+ return { assets: [...rawAssets.keys()], rawAssets, diagnostics: { ...diagnosticsOf(result), ...(truncated ? { truncated } : {}) } };
135
+ };
106
136
  return { finish: (eligibility, reuse) => {
107
137
  const qualified = [...genes, ...capsules].filter((asset) => assetstore.assetContextEligibilityReason(asset, eligibility) === undefined);
108
138
  const result = assetstore.recallLocalAssets(qualified, query, reuse, contexts);
109
- const rawAssets = new Map(result.hits.map((hit) => [recalledAsset(hit), hit.gene]));
110
- return { assets: [...rawAssets.keys()], rawAssets, diagnostics: { ...diagnosticsOf(result), ...(truncated ? { truncated } : {}) } };
139
+ if (result.hits.length > 0)
140
+ return outcomeOf(result, false);
141
+ const trial = trialPool(genes, capsules, eligibility);
142
+ if (trial.length === 0)
143
+ return outcomeOf(result, false);
144
+ const trialResult = assetstore.recallLocalAssets(trial, { ...query, limit: 1 }, reuse, contexts);
145
+ return trialResult.hits.length > 0 ? outcomeOf(trialResult, true) : outcomeOf(result, false);
111
146
  } };
112
147
  }
@@ -0,0 +1,43 @@
1
+ import { assetstore, hub as hubNs, verify } from '@evomap/evolver-core';
2
+ export interface VerificationContext {
3
+ geneAssetId: string;
4
+ geneId: string;
5
+ root: string;
6
+ head: string;
7
+ diff?: string;
8
+ commands: string[];
9
+ checks: readonly verify.ObservedCheck[];
10
+ planDigest?: string;
11
+ distillInput: Record<string, unknown>;
12
+ recordedAt: string;
13
+ verifiedAt?: string;
14
+ failedAtVerification?: number;
15
+ lastAttempt?: {
16
+ at: string;
17
+ result: string;
18
+ };
19
+ published?: {
20
+ at: string;
21
+ envelopeId: string;
22
+ assetIds: string[];
23
+ };
24
+ }
25
+ export declare class VerificationContextStore {
26
+ private readonly dir;
27
+ constructor(assetStoreDir: string);
28
+ private pathOf;
29
+ get(geneAssetId: string): VerificationContext | undefined;
30
+ /** A Gene is addressed by its content, so once it is published a later plan for it keeps that mark. */
31
+ put(context: VerificationContext): void;
32
+ list(): VerificationContext[];
33
+ }
34
+ /** Old sidecars may already have lost cwd occurrences and must be observed again. */
35
+ export declare function hasVerificationPlan(context: VerificationContext): context is VerificationContext & {
36
+ planDigest: string;
37
+ };
38
+ /** The project state a host hook recorded with an observed distillation, or none when it cannot be replayed. */
39
+ export declare function verificationContextOf(body: hubNs.ConversationDistillInput, gene: assetstore.AssetRecord, now: Date, checks: readonly verify.ObservedCheck[] | undefined): VerificationContext | undefined;
40
+ /** Whether a host hook observed the reused turn's last check and its result; agent-reported evidence never qualifies. */
41
+ export declare function isObservedCheckEvidence(evidence: Record<string, unknown> | undefined): boolean;
42
+ export type ObservedReuseRecording = 'recorded' | 'duplicate' | 'not_local' | 'no_task';
43
+ export declare function recordObservedReuse(store: assetstore.AssetStoreProvider, assetId: string, taskId: string | undefined, passed: boolean): Promise<ObservedReuseRecording>;