feat(memory): release Mnemo project bridge and schema2 recovery
Official contracts and Windows source CI / windows-source (push) Has been cancelled
Official contracts and Windows source CI / windows-source (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import type { FormEvent } from 'react';
|
||||
import type { ProjectMemory } from './types';
|
||||
import { api } from './api';
|
||||
|
||||
type Review = Pick<ProjectMemory, 'id' | 'title' | 'content' | 'source' | 'sourceFingerprint' | 'expiresAt' | 'experience'>;
|
||||
type Source = { memoryId: string; title: string; state: string; sequence: number; version: string | null; review: Review; confirmationDigest: string; syncStatus: string;
|
||||
requestKey: string | null; confirmation: { by: string; at: string } | null; receipt: { receiptId: string; currentState: string } | null; issue: { message: string } | null };
|
||||
type View = { producerId: string; projectId: string; connection: { enabled: boolean; baseUrl: string; bindingId: string; revision: number; hasToken: boolean; selectionLimit: number }; sources: Source[]; rule: string };
|
||||
const states: Record<string, string> = { not_shared: '未共享', current: '已确认', candidate: '候选 · 未确认', unverified: '需重新核对', expired: '已到期', archived: '已归档', withdrawn: '已撤回', deleted: '永久删除' };
|
||||
const syncStates: Record<string, string> = { prepared: '待核对原键', unknown: '同步结果未知', committed: '已收到提交回执', rejected: '服务未接受', not_sent: '未发送' };
|
||||
|
||||
export function MnemoBridgePanel({ projectId, memories }: { projectId: string; memories: ProjectMemory[] }) {
|
||||
const [view, setView] = useState<View | null>(null), [error, setError] = useState(''), [notice, setNotice] = useState(''), [busy, setBusy] = useState(false);
|
||||
const [draft, setDraft] = useState({ baseUrl: '', bindingId: '', token: '', enabled: false, selectionLimit: 50 });
|
||||
const [selected, setSelected] = useState(''), [confirmed, setConfirmed] = useState(false);
|
||||
const route = '/projects/' + projectId + '/memory-bridge';
|
||||
const accept = (value: View) => { setView(value); setDraft({ baseUrl: value.connection.baseUrl, bindingId: value.connection.bindingId, token: '', enabled: value.connection.enabled, selectionLimit: value.connection.selectionLimit }); };
|
||||
useEffect(() => { let alive = true; setView(null); setSelected(''); setConfirmed(false); setError('');
|
||||
api<View>(route).then(value => { if (alive) accept(value); }).catch(cause => { if (alive) setError(cause.message); }); return () => { alive = false; };
|
||||
}, [route, memories]);
|
||||
const perform = async (path: string, input: unknown, message: string) => {
|
||||
setBusy(true); setError(''); setNotice('');
|
||||
try { const value = await api<View>(route + path, input); accept(value); setNotice(message); setConfirmed(false); }
|
||||
catch (cause) { setError(cause instanceof Error ? cause.message : '操作未完成,请核对原回执。'); }
|
||||
finally { setBusy(false); }
|
||||
};
|
||||
const save = (event: FormEvent) => { event.preventDefault(); if (!view) return;
|
||||
void perform('/configure', { enabled: draft.enabled, expectedRevision: view.connection.revision, selectionLimit: draft.selectionLimit,
|
||||
...(draft.baseUrl ? { baseUrl: draft.baseUrl } : {}), ...(draft.bindingId ? { bindingId: draft.bindingId } : {}), ...(draft.token ? { token: draft.token } : {}) }, '设置已保存;只会共享逐条确认的来源。');
|
||||
};
|
||||
// The review and digest come from one server snapshot. A separately loaded
|
||||
// local-memory list can be older and must never supply the confirmed text.
|
||||
const source = view?.sources.find(row => row.memoryId === selected), memory = source?.review;
|
||||
return <section className="mnemo-bridge" aria-label="Mnemo 项目记忆桥">
|
||||
<details><summary>Mnemo 项目记忆桥 <span>{view?.connection.enabled ? view.connection.hasToken ? '本次已连接' : '需重新授权' : '默认关闭'}</span></summary>
|
||||
<p>让本项目已确认的记忆通过 Mnemo 用于后续请求。仅共享选中的完整修订;候选不会进入模型请求。</p>
|
||||
{error && <p className="memory-error" role="alert">{error}</p>}{notice && <p className="memory-notice" role="status">{notice}</p>}
|
||||
{view && <><p className="mnemo-identity">生产者 <code>{view.producerId}</code><br />项目 <code>{view.projectId}</code></p>
|
||||
<form onSubmit={save}><div className="memory-form-grid">
|
||||
<label>Mnemo 根地址<input type="url" value={draft.baseUrl} onChange={e => setDraft({ ...draft, baseUrl: e.target.value })} placeholder="https://memory.example.com" autoComplete="off" /></label>
|
||||
<label>项目绑定 ID<input value={draft.bindingId} onChange={e => setDraft({ ...draft, bindingId: e.target.value })} autoComplete="off" /></label>
|
||||
<label>每次选入的桥记忆条数<input type="number" min={1} max={50} value={draft.selectionLimit} onChange={e => setDraft({ ...draft, selectionLimit: Number(e.target.value) })} /><small>仅用于可选桥;不改变模型额度和本地全文。</small></label>
|
||||
<label className="memory-full">本次项目凭据<input type="password" value={draft.token} onChange={e => setDraft({ ...draft, token: e.target.value })} autoComplete="new-password" placeholder={view.connection.hasToken ? '当前宿主已有凭据;留空保持' : '填写新授权返回的项目凭据'} /></label>
|
||||
</div><div className="memory-editor-footer"><label><input type="checkbox" checked={draft.enabled} onChange={e => setDraft({ ...draft, enabled: e.target.checked })} /> 启用本项目记忆桥</label><button className="button secondary" disabled={busy}>保存连接</button></div></form>
|
||||
<p className="experience-hint">凭据只留在本次宿主内存中;重启或恢复后需重新授权。连接失败不改变模型额度、历史或工程权限。</p>
|
||||
<div className="mnemo-sources">{view.sources.map(row => <button type="button" key={row.memoryId} className={'mnemo-source' + (selected === row.memoryId ? ' is-selected' : '')} onClick={() => { setSelected(row.memoryId); setConfirmed(false); }}>
|
||||
<strong>{row.title}</strong><span>{states[row.state] ?? row.state} · 修订 {row.sequence} · {syncStates[row.syncStatus] ?? row.syncStatus}</span></button>)}</div>
|
||||
{source && memory && <div className="mnemo-review" aria-label="核对完整来源修订"><h4>{source.title}</h4><p className="memory-content">{memory.content}</p>
|
||||
<p className="experience-hint">来源 <code>{source.memoryId}</code> · 工程 <code>{memory.sourceFingerprint}</code></p>
|
||||
<p>来源:{memory.source.path || memory.source.label || memory.source.kind} · 到期:{memory.expiresAt ? new Date(memory.expiresAt).toLocaleString('zh-CN') : '未设置'}</p>
|
||||
<details><summary>查看完整来源修订与摘要</summary><p>修订摘要 <code>{source.confirmationDigest}</code></p><pre>{JSON.stringify(memory, null, 2)}</pre></details>
|
||||
{source.confirmation && <p>确认时间:{new Date(source.confirmation.at).toLocaleString('zh-CN')}</p>}
|
||||
{source.issue && <p className="memory-error" role="alert">{source.issue.message}</p>}
|
||||
{source.requestKey && <details><summary>原同步回执</summary><code>{source.requestKey}</code><p>{syncStates[source.syncStatus] ?? source.syncStatus} · {source.receipt?.currentState ?? '当前状态未知'}</p></details>}
|
||||
<label className="mnemo-confirm"><input type="checkbox" checked={confirmed} onChange={e => setConfirmed(e.target.checked)} /> 我已核对以上完整来源修订</label>
|
||||
<div className="experience-actions">{(['candidate', 'confirm', 'withdraw', 'delete'] as const).map(action => <button type="button" className={action === 'confirm' ? 'button primary' : 'button secondary'} key={action}
|
||||
disabled={busy || !confirmed || !view.connection.hasToken || source.state === 'deleted'} onClick={() => void perform('/sources/' + source.memoryId,
|
||||
{ action, confirmed: true, expectedDigest: source.confirmationDigest }, '操作已记录,请查看该来源的提交回执。')}>{({ candidate: '提交候选', confirm: '确认并共享', withdraw: '撤回共享', delete: '永久删除桥来源' })[action]}</button>)}
|
||||
{source.requestKey && <button type="button" className="text-button" disabled={busy || !view.connection.hasToken} onClick={() => void perform('/lookup', { memoryId: source.memoryId }, '已核对原键;未重送同步或模型请求。')}>查询原键</button>}</div>
|
||||
<p className="experience-hint">永久删除后,该来源不能通过续期或恢复重新共享。本地原记忆与旧请求工件保留。</p>
|
||||
</div>}</>}
|
||||
</details>
|
||||
</section>;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ContextPlan } from './types';
|
||||
export type MnemoReceipt = Pick<ContextPlan, 'id' | 'runId' | 'mnemoRecallArtifactId' | 'mnemoMemoryArtifactId' | 'mnemoRecallContextDigest' | 'mnemoSelectedContextDigest' | 'mnemoSelectedRefs' | 'mnemoIssue' | 'mnemoDispatches'>;
|
||||
const status: Record<string, string> = { not_dispatched: '未发送远端记忆', validated_not_submitted: '已核对,尚未提交', submitted_unconfirmed: '已提交,响应待核对', response_received: '已收到响应', unknown: '结果未知', failed: '请求失败' };
|
||||
export function MnemoPlanReceipt({ plan, onOpenArtifact }: { plan: MnemoReceipt; onOpenArtifact?: (id: string) => void }) {
|
||||
if (!plan.mnemoRecallArtifactId && !plan.mnemoIssue) return null;
|
||||
return <details className="mnemo-plan-receipt"><summary>Mnemo 记忆来源 · {plan.mnemoSelectedRefs?.length ?? 0} 条选入</summary>
|
||||
<p>Run <code>{plan.runId}</code><br />请求计划 <code>{plan.id}</code></p>
|
||||
{plan.mnemoIssue && <p role="status">{plan.mnemoIssue.message}</p>}
|
||||
<p>召回摘要 <code>{plan.mnemoRecallContextDigest ?? '无召回'}</code><br />选入摘要 <code>{plan.mnemoSelectedContextDigest ?? '无选入'}</code></p>
|
||||
{plan.mnemoSelectedRefs?.map(ref => <p key={ref.sourceId}><code>{ref.sourceId}</code> · 来源序号 {ref.sequence} · 远端修订 {ref.revision}</p>)}
|
||||
{plan.mnemoDispatches?.map(attempt => <div key={attempt.wireAttemptId}><p>{status[attempt.status] ?? attempt.status} · <code>{attempt.wireAttemptId}</code></p>
|
||||
{attempt.issue && <p>{attempt.issue.message}</p>}{attempt.wireRequestSha256 && <p>实际发送 SHA <code>{attempt.wireRequestSha256}</code></p>}
|
||||
{onOpenArtifact && <button type="button" className="text-button" onClick={() => onOpenArtifact(attempt.validationArtifactId)}>查看本次发送前核对</button>}</div>)}
|
||||
{!!onOpenArtifact && plan.mnemoRecallArtifactId && <button type="button" className="text-button" onClick={() => onOpenArtifact(plan.mnemoRecallArtifactId!)}>查看完整召回回执</button>}
|
||||
{!!onOpenArtifact && plan.mnemoMemoryArtifactId && <button type="button" className="text-button" onClick={() => onOpenArtifact(plan.mnemoMemoryArtifactId!)}>查看整条选入记忆</button>}
|
||||
<p>核对仅证明当时时刻匹配,不授予执行权限。跨系统校验与模型发送不保证原子性。</p>
|
||||
</details>;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
.project-memory{display:flex;flex-direction:column;min-height:0;background:#f8fafc;color:#15283f;padding:24px 28px;overflow:auto}.memory-overview{display:flex;align-items:flex-start;justify-content:space-between;gap:18px;padding-bottom:18px;border-bottom:1px solid #cbd5df}.memory-eyebrow{display:flex;align-items:center;gap:7px;color:#2560a3;letter-spacing:.13em;font-size:10px;font-weight:700}.memory-overview h3{font-size:22px;margin:8px 0 5px}.memory-overview p{color:#5b6d83;font-size:12px;line-height:1.8}.memory-summary{display:flex;gap:16px;flex-wrap:wrap;padding:13px 0;color:#5d7088;font-size:12px}.memory-summary strong{color:#15283f;font-size:16px;margin-right:4px}.memory-summary code{color:#33577f}.memory-list{display:grid;gap:11px}.memory-card{padding:17px 18px;background:#fff;border:1px solid #cbd6e2;border-left:3px solid #2f806e;border-radius:6px}.memory-card.memory-expired{border-left-color:#b4772b;background:#fffaf0}.memory-card.memory-archived{border-left-color:#8b9aaa;background:#f4f7fa;opacity:.82}.memory-card-heading{display:flex;align-items:center;gap:10px;flex-wrap:wrap}.memory-card-heading h4{font-size:14px;flex:1;min-width:130px}.memory-card-heading time{font-size:11px;color:#74859a}.memory-state{padding:3px 7px;border:1px solid #b9d9cb;border-radius:4px;color:#216d5f;background:#e6f3ee;font-size:10px;font-weight:650}.memory-expired .memory-state{color:#8b5c12;background:#fff4d9;border-color:#e6d09d}.memory-archived .memory-state{color:#67788d;background:#e9eef3;border-color:#d1dbe4}.memory-content{font-size:13px;line-height:1.8;white-space:pre-wrap;overflow-wrap:anywhere;margin-top:11px;color:#233b55}.memory-meta{display:flex;gap:8px;align-items:center;flex-wrap:wrap;margin-top:12px;color:#6a7d92;font-size:11px}.memory-meta code{font-size:10px;color:#7b8ca1}.memory-tag{padding:3px 6px;background:#edf3f8;color:#4e6884;border-radius:3px}.memory-warning{display:flex;align-items:flex-start;gap:6px;color:#8b5c12;font-size:11px;line-height:1.7;margin-top:11px}.memory-card>.text-button{padding:8px 0;margin-top:8px}.memory-notice,.memory-error{display:flex;gap:7px;align-items:flex-start;padding:11px 13px;margin:15px 0 0;border-radius:5px;font-size:12px;line-height:1.7}.memory-notice{color:#216d5f;background:#e6f3ee;border:1px solid #b9d9cb}.memory-error{color:#a7353e;background:#fff0f0;border:1px solid #e8bac0}.memory-empty{display:grid;place-items:center;padding:38px 20px;margin:7px 0 17px;background:#fff;border:1px dashed #b5c6d7;border-radius:7px;color:#61738a;text-align:center}.memory-empty svg{color:#6c87a5;margin-bottom:10px}.memory-empty strong{color:#314b68;font-size:14px}.memory-empty p{font-size:12px;line-height:1.8;margin-top:7px;max-width:480px}.memory-editor{padding:19px 20px;margin-top:18px;border:1px solid #b2c6da;border-top:3px solid #2560a3;border-radius:7px;background:#fff}.memory-editor-heading{display:flex;align-items:flex-start;justify-content:space-between;color:#2560a3}.memory-editor-heading h4{font-size:15px;color:#15283f}.memory-editor-heading p{font-size:11px;color:#687a90;margin-top:5px;line-height:1.7}.memory-form-grid{display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-top:16px}.memory-form-grid label{display:flex;flex-direction:column;gap:7px;color:#4e637d;font-size:11px}.memory-form-grid input,.memory-form-grid textarea{border:1px solid #b9c8d6;border-radius:5px;padding:9px 10px;min-height:38px;font:inherit;color:#15283f;resize:vertical}.memory-form-grid input:focus,.memory-form-grid textarea:focus{outline:2px solid #2560a333;border-color:#2560a3}.memory-full{grid-column:1/-1}.memory-editor-footer{display:flex;align-items:center;justify-content:space-between;gap:15px;flex-wrap:wrap;margin-top:14px}.memory-editor-footer>span{display:flex;align-items:center;gap:6px;color:#67798e;font-size:11px}.memory-editor-footer svg{color:#2f806e}.project-memory button{min-height:36px}
|
||||
@media(max-width:620px){.project-memory{padding:18px 15px}.memory-overview{flex-direction:column}.memory-form-grid{grid-template-columns:1fr}.memory-full{grid-column:auto}.memory-editor{padding:17px 14px}.memory-card{padding:15px 13px}.memory-card-heading time{width:100%}}
|
||||
.memory-unverified .memory-state{color:#8a5b16;background:#fff7df;border-color:#ead39a}.memory-scope-warning{margin:14px 0;padding:12px 14px;line-height:1.7}
|
||||
.mnemo-bridge{padding:16px 0;margin-bottom:14px;border-bottom:1px solid var(--orven-border,#cbd6e2);font-size:12px;line-height:1.8}.mnemo-bridge summary{cursor:pointer;min-height:40px;font-weight:600}.mnemo-bridge summary>span{margin-left:12px;font-weight:400;color:var(--orven-muted,#5b6d83)}.mnemo-bridge code,.mnemo-plan-receipt code{overflow-wrap:anywhere;font-size:11px}.mnemo-identity{margin:10px 0;color:var(--orven-muted,#5b6d83)}.mnemo-sources{margin-top:18px;border-top:1px solid var(--orven-border,#cbd6e2)}.mnemo-source{display:flex;flex-direction:column;width:100%;text-align:left;gap:4px;padding:12px 10px;border:0;border-bottom:1px solid var(--orven-border,#cbd6e2);background:transparent;color:inherit}.mnemo-source>span{font-size:11px;color:var(--orven-muted,#5b6d83)}.mnemo-source.is-selected{background:var(--orven-surface-faint,#edf3f8);box-shadow:inset 3px 0 var(--accent,#456652)}.mnemo-review{padding:16px 10px}.mnemo-confirm{display:flex;align-items:center;gap:8px;margin-top:16px;min-height:44px}.mnemo-review .memory-content{max-height:360px;overflow:auto}.mnemo-plan-receipt{flex-basis:100%;min-width:0;max-width:100%;padding:12px 0;font-size:12px;line-height:1.8}.mnemo-plan-receipt summary{min-height:40px;cursor:pointer}.mnemo-plan-receipt p{overflow-wrap:anywhere}
|
||||
.experience-source{padding:18px 0 22px;margin-bottom:18px;border-bottom:1px solid var(--orven-border,#cbd6e2)}
|
||||
.experience-source h4{font-size:14px;font-weight:600;color:var(--orven-ink,#15283f)}
|
||||
.experience-source p,.experience-boundary{font-size:12px;line-height:1.8;color:var(--orven-text,#4e637d)}
|
||||
@@ -31,3 +32,4 @@
|
||||
.experience-counterexamples{border-left:2px solid var(--orven-border-strong,#b9c8d6);padding-left:12px;font-size:12px;line-height:1.8;margin-top:14px}
|
||||
.experience-counterexamples small{display:block;color:var(--orven-muted,#5b6d83)}
|
||||
@media(max-width:620px){.experience-source-picker{align-items:stretch;flex-direction:column}.experience-source-picker .button{justify-content:center}.experience-provenance li{align-items:flex-start;flex-wrap:wrap}.experience-scope>div,.experience-provenance dl>div{grid-template-columns:1fr;gap:2px}.experience-actions{gap:10px}.experience-actions .button,.experience-actions .text-button{min-height:40px}}
|
||||
.mnemo-review pre{white-space:pre-wrap;overflow-wrap:anywhere;max-height:360px;overflow:auto;font:11px/1.8 var(--font-mono,monospace);padding:10px;background:var(--orven-surface-faint,#edf3f8)}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Archive, Brain, CircleHelp, LoaderCircle, Plus, RefreshCw, ShieldCheck
|
||||
import type { ExperienceSourceView, Project, ProjectMemory, ProjectMemoryView, Session } from './types';
|
||||
import { api } from './api';
|
||||
import { ExperienceDraftControls } from './ExperienceDraftControls';
|
||||
import { MnemoBridgePanel } from './MnemoBridgePanel';
|
||||
import './ProjectMemory.css';
|
||||
|
||||
const statusLabel: Record<string, string> = { current: '当前', unverified: '扫描不完整', expired: '已过期', archived: '已归档' };
|
||||
@@ -64,6 +65,7 @@ export function ProjectMemoryPanel({ project, session, onSelectSession }: { proj
|
||||
{notice && <p className="memory-notice" role="status"><ShieldCheck size={14} />{notice}</p>}
|
||||
{error && <p className="memory-error" role="alert">{error}</p>}
|
||||
{view && <div className="memory-summary"><span><strong>{view.summary.current}</strong> 条当前可用</span><span><strong>{view.summary.drafts ?? 0}</strong> 条经验草稿</span>{(view.summary.unverified ?? 0) > 0 && <span><strong>{view.summary.unverified}</strong> 条未能验证</span>}<span><strong>{view.summary.expired}</strong> 条需重新核对</span><span>源码 <code>{view.sourceFingerprint.slice(0, 12)}</code></span></div>}
|
||||
{view && <MnemoBridgePanel key={project.id} projectId={project.id} memories={memories} />}
|
||||
<section className="experience-source" aria-label="从工程证据生成草稿"><div><h4>从工程证据生成草稿</h4><p>选择当前会话已有的交付报告、检查或工具结果。保留原失败和未知结果,不调用模型。</p></div>
|
||||
{sourceError && <p className="memory-error" role="alert">{sourceError}</p>}
|
||||
<div className="experience-source-picker"><label>经验来源<select aria-label="经验来源" value={sourceId} onChange={event => setSourceId(event.target.value)} disabled={loading || working || !sources}><option value="">选择已有来源…</option>{sources?.sources.map(source => <option key={source.kind + ':' + source.id} value={source.kind + ':' + source.id}>{source.label} · {source.id.slice(0, 8)}</option>)}</select></label><button type="button" className="button secondary" disabled={working || loading || !sourceId || !sources} onClick={() => void createExperience()}><Plus size={14} />生成经验草稿</button></div>
|
||||
|
||||
@@ -4,6 +4,7 @@ import { api } from './api';
|
||||
import { Modal } from './components';
|
||||
import { ModelUsageDetails } from './ModelUsageDetails';
|
||||
import type { ContextHistory, ContextHistoryGroup, ContextPlan, Session } from './types';
|
||||
import { MnemoPlanReceipt } from './MnemoPlanReceipt';
|
||||
import { ModelRequestDetails } from './ModelRequestDetails';
|
||||
import { ModelProviderStateDetails } from './ModelProviderStateDetails';
|
||||
|
||||
@@ -74,6 +75,7 @@ export function RequestContextPanel({ session, runId, openArtifact, onError }: {
|
||||
{plan.workingSetArtifactId && <button type="button" className="text-button" onClick={() => openArtifact(plan.workingSetArtifactId!)}><BrainCircuit size={13} />查看工作依据</button>}
|
||||
{plan.latestToolsArtifactId && <button type="button" className="text-button" onClick={() => openArtifact(plan.latestToolsArtifactId!)}><FileText size={13} />查看最近工具反馈</button>}
|
||||
{plan.projectMemoryArtifactId && <button type="button" className="text-button" onClick={() => openArtifact(plan.projectMemoryArtifactId!)}><BrainCircuit size={13} />查看本次项目记忆</button>}
|
||||
<MnemoPlanReceipt plan={plan} onOpenArtifact={openArtifact} />
|
||||
{plan.skillCatalogArtifactId && <button type="button" className="text-button" onClick={() => openArtifact(plan.skillCatalogArtifactId!)}><FileText size={13} />查看本次技能目录</button>}
|
||||
{!!plan.archivedGroups && <button type="button" className="text-button" onClick={() => setHistoryId(plan.id)}><Archive size={13} />检索完整历史</button>}</div>
|
||||
{historyId && <HistoryModal key={session.id + ':' + historyId} sessionId={session.id} planId={historyId} onError={onError}
|
||||
|
||||
@@ -12,6 +12,7 @@ export interface CompanionStatus {
|
||||
export interface MobileBrief { id: string; projectId: string; taskId?: string; runId?: string; title: string; status: string; revision: number; eventSeq: number; updatedAt: string }
|
||||
export interface MobileUsage { totalTokens: number; unknownRequests: number; estimatedUnknownTokens: number }
|
||||
export interface MobileTask extends MobileBrief {
|
||||
memoryReceipts?: import('./MnemoPlanReceipt').MnemoReceipt[];
|
||||
browserObservations?:{id:string;tabId:string;url:string;title:string;createdAt:string;revision:string;scope:{complete:boolean};screenshotSha256:string}[];
|
||||
inputAttachments?: import('./types').InputAttachment[];
|
||||
budgetPause?: import('./types').BudgetPause|null;
|
||||
|
||||
@@ -7,6 +7,7 @@ import { useRecordPages } from '../useRecordPages';
|
||||
import type { RecordPage } from '../types';
|
||||
import { mobileApi, MobileError, verifiedText, verifyDigest } from './api';
|
||||
import { MobileInputImage } from './MobileInputImage';
|
||||
import { MnemoPlanReceipt } from '../MnemoPlanReceipt';
|
||||
import './file-results.css';
|
||||
import { BRAND, OrvenMark } from '../brand';
|
||||
import { AppearancePanel } from '../AppearancePanel';
|
||||
@@ -133,6 +134,7 @@ export function MobileApp({ initialToken }: { initialToken: string }) {
|
||||
<div className="mobile-task-title"><span className="mobile-eyebrow">{projectName(selectedTask.projectId)}</span><h1>{selectedTask.title}</h1><div><span className={'mobile-state state-' + selectedTask.status}>{stateLabel(selectedTask.status)}</span><span>修订 {selectedTask.revision}</span></div><details className="mobile-goal"><summary>完整任务目标</summary><p>{selectedTask.task?.goal ?? '此会话尚未创建执行任务。'}</p></details><details><summary>原任务与运行</summary><code>Task {selectedTask.taskId ?? '未开始'}<br />Run {selectedTask.runId ?? '未开始'}</code></details></div>
|
||||
{receipt && <p className="mobile-receipt" role="status">{receipt}</p>}
|
||||
{selectedTask.inputAttachments?.map(image=><MobileInputImage key={image.id} sessionId={selectedTask.id} image={image} onError={cause=>{if(cause instanceof MobileError&&[401,403].includes(cause.status))revoke(cause.message);else setError(cause instanceof Error?cause.message:'截图读取失败。');}}/>)}
|
||||
{selectedTask.memoryReceipts?.map(plan => <MnemoPlanReceipt key={plan.id} plan={plan} />)}
|
||||
{selectedTask.budgetPause&&<aside className="mobile-unknown"><strong>原任务因预算暂停</strong><p>必需输入 {selectedTask.budgetPause.requiredInputTokens?.toLocaleString()??'旧记录未分解'};可用输入 {selectedTask.budgetPause.inputBudget?.toLocaleString()??'未记录'} tokens。请在电脑核对预算并预览续接,原工具结果保留。</p></aside>}
|
||||
{!!selectedTask.progress.unknown.length && <div className="mobile-unknown"><strong>{selectedTask.progress.unknown.length} 项工具结果需要核对</strong><p>请在电脑核对原输入和实际结果。刷新、重连和手机审批均不会自动重新执行。</p>{selectedTask.progress.unknown.map(item => <code key={item.id}>{item.tool} · {stateLabel(item.state)}</code>)}</div>}
|
||||
{selectedTask.browserObservations?.slice(-1).map(observation=><MobileBrowserObservation key={observation.id} sessionId={selectedTask.id} observation={observation} onError={cause=>{if(cause instanceof MobileError&&[401,403].includes(cause.status))revoke(cause.message);else setError(cause instanceof Error?cause.message:'原网页截图读取失败。');}}/>)}
|
||||
|
||||
@@ -4,3 +4,4 @@
|
||||
.mobile-file-results p { margin: 8px 0; }
|
||||
.mobile-file-results code { font-size: 11px; overflow-wrap: anywhere; word-break: break-word; }
|
||||
.mobile-file-results details summary { min-height: 42px; display: flex; align-items: center; cursor: pointer; }
|
||||
.mnemo-plan-receipt{font-size:13px;line-height:1.8;max-width:100%;min-width:0;padding:12px 0;border-bottom:1px solid var(--orven-border,#cbd6e2)}.mnemo-plan-receipt summary{cursor:pointer;min-height:44px;font-weight:600}.mnemo-plan-receipt code,.mnemo-plan-receipt p{overflow-wrap:anywhere;font-size:12px}
|
||||
|
||||
@@ -202,6 +202,10 @@ export interface ContextPlan {
|
||||
workingSetViewVersion?: number; workingRetentionTokens?: number;
|
||||
latestToolsArtifactId?: string | null; latestToolsSha256?: string | null; latestToolsEntries?: number;
|
||||
projectMemoryArtifactId?: string | null; projectMemorySha256?: string | null; projectMemoryEntries?: number;
|
||||
mnemoRecallArtifactId?: string | null; mnemoMemoryArtifactId?: string | null; mnemoRecallContextDigest?: string | null; mnemoSelectedContextDigest?: string;
|
||||
mnemoSelectedRefs?: { sourceId: string; sequence: number; sourceDigest: string; memoryId: string; revision: number }[];
|
||||
mnemoIssue?: { code: string; message: string } | null;
|
||||
mnemoDispatches?: { wireAttemptId: string; validationArtifactId: string; validationSha256: string; status: string; wireRequestSha256?: string; modelAttemptId?: string; issue: { code: string; message: string } | null }[];
|
||||
skillCatalogArtifactId?: string | null; skillCatalogSha256?: string | null; skillCatalogEntries?: number;
|
||||
actualUsage: TokenUsage | null; requestAttempts: number; error?: string;
|
||||
historyPolicy?: 'preserve' | 'compact'; contextLayout?: string;
|
||||
|
||||
@@ -34,6 +34,10 @@ export function mobileSession(store, runtime, session, redact = String) {
|
||||
.slice(-10).map(value => pick(value, ['id', 'kind', 'sha256', 'bytes', 'createdAt', 'taskId', 'runId'])),
|
||||
};
|
||||
if (task?.delivery) result.task.delivery = pick(task.delivery, ['status', 'paths', 'at', 'checks']);
|
||||
const mnemoPlans = [...store.core.context_plan.values()].filter(plan => plan.sessionId === session.id && plan.taskId === session.taskId
|
||||
&& (plan.mnemoRecallArtifactId || plan.mnemoIssue));
|
||||
if (mnemoPlans.length) result.memoryReceipts = mnemoPlans.map(plan => pick(plan, ['id', 'runId', 'mnemoRecallArtifactId', 'mnemoMemoryArtifactId',
|
||||
'mnemoRecallContextDigest', 'mnemoSelectedContextDigest', 'mnemoSelectedRefs', 'mnemoIssue', 'mnemoDispatches']));
|
||||
if ([...store.core.file_result.values()].some(row => row.sessionId === session.id && row.taskId === session.taskId)) {
|
||||
result.fileResults = new FileResults(store).inspect(session).results.slice(0, 8).map(row => ({
|
||||
...pick(row, ['id', 'taskId', 'runId', 'createdAt', 'verification', 'contractSha256']), reason: row.reason,
|
||||
|
||||
@@ -4,6 +4,8 @@ import { completionBody, requestMetrics, requestVariants, requestHistory, output
|
||||
import { workingCandidates, workingDocument, workingEntry, workingMessage, verifyWorkingDocument } from './working-set.mjs';
|
||||
import { latestToolDocument, latestToolMessage, verifyLatestToolDocument } from './tool-feedback.mjs';
|
||||
import { projectMemoryMessage, MEMORY_CONTEXT_LIMITS } from '../memory/project-memory.mjs';
|
||||
import { mnemoMemoryMessage } from '../memory/mnemo-bridge.mjs';
|
||||
import { contextDigest as mnemoContextDigest, entryRef as mnemoEntryRef, digest as mnemoDigest } from '../memory/mnemo-protocol.mjs';
|
||||
import { skillCatalogMessage, SKILL_LIMITS } from '../skills/skill-registry.mjs';
|
||||
import { mcpCatalogMessage, MCP_LIMITS } from '../tools/mcp-contract.mjs';
|
||||
import { modelReservation,taskConsumption,verifyModelReservation } from '../../shared/contracts/model-reservation.mjs';
|
||||
@@ -193,7 +195,7 @@ export class RequestPlanner {
|
||||
return { artifact_id: artifact.id, sha256: artifact.sha256, group_sha256: group.sha256, bytes: Buffer.byteLength(text, 'utf8'), chunks: chunks.length };
|
||||
}
|
||||
|
||||
prepare(session, run, config, { system, contextTail = null, tools, toolSurface = null, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null, progressReminder = null, transportReservationTokens = 0,engineStepOnly=false }) {
|
||||
prepare(session, run, config, { system, contextTail = null, tools, toolSurface = null, stepId, projectMemory = null, mnemoMemory = null, skillCatalog = null, mcpCatalog = null, progressReminder = null, transportReservationTokens = 0,engineStepOnly=false }) {
|
||||
const task = this.store.owned('task', run.taskId, session.id);
|
||||
this.store.owned('run', run.id, session.id);
|
||||
const history = providerHistory(session, run);
|
||||
@@ -401,6 +403,26 @@ export class RequestPlanner {
|
||||
}
|
||||
}
|
||||
}
|
||||
let mnemoReference = null, mnemoRecallReference = null, mnemoDocument = null, mnemoIssue = mnemoMemory?.issue ?? null;
|
||||
if (mnemoMemory?.projectId === session.projectId) {
|
||||
try {
|
||||
mnemoRecallReference = this.artifact(session, run, { ...mnemoMemory, sessionId: session.id, taskId: run.taskId, runId: run.id }, 'context-mnemo-recall');
|
||||
if (mnemoMemory.entries?.length && !mnemoIssue) {
|
||||
const { trace, ...source } = mnemoMemory;
|
||||
mnemoDocument = { ...source, sessionId: session.id, taskId: run.taskId, runId: run.id, entries: [] };
|
||||
for (const entry of mnemoMemory.entries) {
|
||||
if (mnemoDocument.entries.length >= (mnemoMemory.selectionLimit ?? 50)) break;
|
||||
mnemoDocument.entries.push(entry);
|
||||
const candidate = addContext(messages, mnemoMemoryMessage({ id: PLACEHOLDER_ID, sha256: PLACEHOLDER_SHA }, mnemoDocument));
|
||||
if (requestMetrics(completionBody(config, candidate, tools)).estimatedInputTokens > Math.min(inputBudget, targetInputTokens)) mnemoDocument.entries.pop();
|
||||
}
|
||||
if (mnemoDocument.entries.length) {
|
||||
mnemoReference = this.artifact(session, run, mnemoDocument, 'context-mnemo-memory');
|
||||
messages.splice(0, messages.length, ...addContext(messages, mnemoMemoryMessage(mnemoReference, mnemoDocument)));
|
||||
}
|
||||
}
|
||||
} catch { mnemoIssue = { code: 'PROJECT_SNAPSHOT_UNAVAILABLE', message: '可选远端记忆未能保存,本次原任务继续。' }; mnemoReference = null; }
|
||||
}
|
||||
const body = completionBody(config, messages, tools), metrics = requestMetrics(body);
|
||||
if (metrics.estimatedInputTokens > inputBudget) throw pause(budget, metrics, breakdown);
|
||||
const tokenReservation=engineStepOnly?null:modelReservation(config,body,taskConsumption(this.store,task.id));
|
||||
@@ -446,7 +468,7 @@ export class RequestPlanner {
|
||||
// sizing reserves that observed framing growth before returning bytes;
|
||||
// the next complete request is still checked against its original limit.
|
||||
readStateGrowthTokens: 1536,
|
||||
originalMessages: history.length, retainedMessages: messages.length - 1 - tail.length - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 1 : 0) - (memoryReference ? 1 : 0) - (skillReference ? 1 : 0) - (mcpReference ? 1 : 0),
|
||||
originalMessages: history.length, retainedMessages: messages.length - 1 - tail.length - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 1 : 0) - (memoryReference ? 1 : 0) - (skillReference ? 1 : 0) - (mcpReference ? 1 : 0) - (mnemoReference ? 1 : 0),
|
||||
archivedGroups: omitted.length, retainedGroups: groups.filter(group => selected.has(group.index)).map(group => group.index),
|
||||
historyIndexArtifactId: reference?.id ?? null, historyIndexSha256: reference?.sha256 ?? null, actualUsage: null,
|
||||
workingSetArtifactId: workingReference?.id ?? null, workingSetSha256: workingReference?.sha256 ?? null, workingSetEntries: entries.length,
|
||||
@@ -456,6 +478,10 @@ export class RequestPlanner {
|
||||
projectMemoryArtifactId: memoryReference?.id ?? null, projectMemorySha256: memoryReference?.sha256 ?? null,
|
||||
projectMemoryEntries: memoryReference ? memoryDocument.entries.length : 0,
|
||||
...(memoryIssue ? { projectMemoryIssue: memoryIssue } : {}),
|
||||
...(mnemoMemory ? { mnemoRecallArtifactId: mnemoRecallReference?.id ?? null, mnemoRecallSha256: mnemoRecallReference?.sha256 ?? null,
|
||||
mnemoMemoryArtifactId: mnemoReference?.id ?? null, mnemoMemorySha256: mnemoReference?.sha256 ?? null,
|
||||
mnemoRecallContextDigest: mnemoMemory.recallContextDigest ?? null, mnemoSelectedContextDigest: mnemoContextDigest(mnemoReference ? mnemoDocument.entries : []),
|
||||
mnemoSelectedRefs: mnemoReference ? mnemoDocument.entries.map(mnemoEntryRef) : [], mnemoIssue } : {}),
|
||||
skillCatalogArtifactId: skillReference?.id ?? null, skillCatalogSha256: skillReference?.sha256 ?? null,
|
||||
skillCatalogEntries: skillReference ? skillDocument.entries.length : 0,
|
||||
mcpCatalogArtifactId: mcpReference?.id ?? null, mcpCatalogSha256: mcpReference?.sha256 ?? null,
|
||||
@@ -593,6 +619,25 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
const message = projectMemoryMessage(artifact, document);
|
||||
if (!carriesContext(message.content)) throw new Error('项目记忆与实际发送内容不匹配。');
|
||||
}
|
||||
for (const key of ['mnemoRecallArtifactId', 'mnemoRecallSha256', 'mnemoMemoryArtifactId', 'mnemoMemorySha256', 'mnemoRecallContextDigest', 'mnemoSelectedContextDigest', 'mnemoSelectedRefs', 'mnemoIssue']) {
|
||||
if (mnemoDigest(frozen[key] ?? null) !== mnemoDigest(plan[key] ?? null)) throw new Error('Mnemo选择与发送前封存记录不匹配。');
|
||||
}
|
||||
if (plan.mnemoRecallArtifactId) {
|
||||
const recallArtifact = get(plan.mnemoRecallArtifactId, 'context-mnemo-recall', plan.mnemoRecallSha256);
|
||||
const recall = JSON.parse(store.artifactStore.read(recallArtifact));
|
||||
if (recallArtifact.taskId !== plan.taskId || recallArtifact.runId !== plan.runId || recall.projectId !== session.projectId || recall.sessionId !== session.id
|
||||
|| recall.taskId !== plan.taskId || recall.runId !== plan.runId || recall.kind !== 'mnemo-authorized-project-memory') throw new Error('Mnemo召回工件归属不匹配。');
|
||||
if (plan.mnemoMemoryArtifactId) {
|
||||
const artifact = get(plan.mnemoMemoryArtifactId, 'context-mnemo-memory', plan.mnemoMemorySha256), document = JSON.parse(store.artifactStore.read(artifact));
|
||||
if (artifact.taskId !== plan.taskId || artifact.runId !== plan.runId || document.projectId !== session.projectId || document.sessionId !== session.id
|
||||
|| document.taskId !== plan.taskId || document.runId !== plan.runId || document.kind !== 'mnemo-authorized-project-memory'
|
||||
|| document.bindingId !== recall.bindingId || document.producerId !== recall.producerId || document.connectionRevision !== recall.connectionRevision
|
||||
|| mnemoContextDigest(recall.entries) !== plan.mnemoRecallContextDigest || mnemoDigest(document.snapshot) !== mnemoDigest(recall.snapshot)
|
||||
|| mnemoContextDigest(document.entries) !== plan.mnemoSelectedContextDigest || mnemoDigest(document.entries.map(mnemoEntryRef)) !== mnemoDigest(plan.mnemoSelectedRefs)
|
||||
|| document.entries.some(e => !recall.entries.some(original => mnemoDigest(original) === mnemoDigest(e)))
|
||||
|| !carriesContext(mnemoMemoryMessage(artifact, document).content)) throw new Error('Mnemo整条选择与实际发送内容不匹配。');
|
||||
}
|
||||
}
|
||||
if ((frozen.skillCatalogArtifactId ?? null) !== (plan.skillCatalogArtifactId ?? null)
|
||||
|| (frozen.skillCatalogSha256 ?? null) !== (plan.skillCatalogSha256 ?? null)
|
||||
|| (frozen.skillCatalogEntries ?? 0) !== (plan.skillCatalogEntries ?? 0)) throw new Error('技能目录与发送前的封存记录不匹配。');
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { isDeepStrictEqual } from 'node:util';
|
||||
import { canonicalHost, hostError } from '../../shared/contracts/desktop-host.mjs';
|
||||
import { assertLocalTree, ownedChild, readState } from './file-state.mjs';
|
||||
import { REQUEST_LEDGER_MIGRATION, REQUEST_LEDGER_OBJECTS, databaseSchemaObjects, legacyLedgerRow } from '../storage/request-ledger-schema.mjs';
|
||||
|
||||
const digest = value => createHash('sha256').update(value).digest('hex');
|
||||
const rowHash = row => digest(JSON.stringify(canonicalHost(row)));
|
||||
const fail = message => hostError(message, 'INSTALLATION_REJECTED', 409);
|
||||
const same = (a, b, message) => { if (!isDeepStrictEqual(a, b)) throw fail(message); };
|
||||
const shaPattern = /^[a-f0-9]{64}$/;
|
||||
const contractFile = 'dist-server/data-compatibility.json';
|
||||
|
||||
// The declaration is generated after compilation and binds both executable
|
||||
// bundles. An earlier, undeclared program is never inferred to support schema2.
|
||||
export function readDataCompatibility(appRoot) {
|
||||
if (!fs.existsSync(path.join(appRoot, contractFile))) return { source: 'legacy-undeclared', readWriteSchemas: [1], ledgerMigration: null };
|
||||
const declared = readState(ownedChild(appRoot, contractFile));
|
||||
same(Object.keys(declared).sort(), ['application', 'bundles', 'ledgerMigration', 'readWriteSchemas', 'schemaVersion', 'sourceInputs'].sort(), '候选数据库能力声明含未知字段。');
|
||||
if (declared.schemaVersion !== 1 || declared.application !== 'agent-workbench-database') throw fail('候选数据库能力声明版本无效。');
|
||||
same(declared.readWriteSchemas, [1, 2], '候选数据库读写范围未经核验。');
|
||||
same(declared.ledgerMigration, REQUEST_LEDGER_MIGRATION, '候选请求账本迁移身份与已核验定义不同。');
|
||||
const expectedBundles = ['dist-server/server.mjs', 'dist-server/hot-upgrade.mjs'];
|
||||
if (!Array.isArray(declared.bundles) || !Array.isArray(declared.sourceInputs)) throw fail('候选数据库声明输入必须为完整清单。');
|
||||
same(declared.bundles?.map(row => row.path).sort(), expectedBundles.slice().sort(), '候选数据库声明未绑定完整处理器。');
|
||||
for (const row of declared.bundles) {
|
||||
same(Object.keys(row).sort(), ['bytes', 'path', 'sha256'], '候选数据库处理器绑定字段无效。');
|
||||
const bytes = fs.readFileSync(ownedChild(appRoot, row.path));
|
||||
if (row.bytes !== bytes.length || !shaPattern.test(row.sha256) || digest(bytes) !== row.sha256) throw fail('候选数据库声明与实际处理器原字节不同。');
|
||||
}
|
||||
const sourceNames = ['src/core/models/request-ledger.mjs', 'src/core/storage/state-database.mjs', 'src/core/storage/request-ledger-schema.mjs'];
|
||||
same(declared.sourceInputs?.map(row => row.path).sort(), sourceNames.slice().sort(), '候选数据库声明来源不完整。');
|
||||
if (declared.sourceInputs.some(row => Object.keys(row).sort().join(',') !== 'bytes,path,sha256' || !shaPattern.test(row.sha256) || !Number.isSafeInteger(row.bytes) || row.bytes < 1)) throw fail('候选数据库声明来源摘要无效。');
|
||||
return { source: contractFile, readWriteSchemas: declared.readWriteSchemas, ledgerMigration: declared.ledgerMigration,
|
||||
declarationSha256: digest(fs.readFileSync(ownedChild(appRoot, contractFile))), bundles: declared.bundles, sourceInputs: declared.sourceInputs };
|
||||
}
|
||||
|
||||
export function assertDataCompatibility(snapshot, compatibility) {
|
||||
const version = snapshot.database?.userVersion;
|
||||
if (version !== undefined && !compatibility?.readWriteSchemas?.includes(version)) throw fail(`候选程序未声明支持数据库 schema${version},请选用已核验的兼容恢复载荷;原账本保持。`);
|
||||
}
|
||||
|
||||
export function snapshotPersistentData(dataRoot) {
|
||||
assertLocalTree(dataRoot);
|
||||
const databaseFile = path.join(dataRoot, 'workbench.sqlite'), tables = {};
|
||||
let databaseState = null;
|
||||
if (fs.existsSync(databaseFile)) {
|
||||
ownedChild(dataRoot, 'workbench.sqlite');
|
||||
const database = new DatabaseSync(databaseFile, { readOnly: true });
|
||||
try {
|
||||
database.exec('PRAGMA busy_timeout=5000; BEGIN;');
|
||||
const objects = databaseSchemaObjects(database);
|
||||
for (const object of objects.filter(row => row.type === 'table')) {
|
||||
if (!/^[a-z_]+$/.test(object.name)) throw fail('原数据库包含无法核对的表名。');
|
||||
const rows = database.prepare('SELECT * FROM "' + object.name + '"').all().filter(row => !(object.name === 'metadata' && row.key === 'runtimeOwner'));
|
||||
const hashes = rows.map(rowHash).sort(); tables[object.name] = { count: hashes.length, hashes };
|
||||
}
|
||||
const raw = tables.metadata ? database.prepare("SELECT value FROM metadata WHERE key='modelAttempts'").get()?.value : undefined;
|
||||
const markerRow = tables.metadata ? database.prepare("SELECT key,value FROM metadata WHERE key='modelAttemptLedger'").get() : undefined;
|
||||
let entries;
|
||||
try { entries = raw === undefined ? [] : JSON.parse(raw); } catch { throw fail('原请求账本 metadata 无法核对,原字节保持。'); }
|
||||
if (!Array.isArray(entries) || entries.some(entry => !entry || typeof entry !== 'object' || Array.isArray(entry) || typeof entry.id !== 'string' || !entry.id)) throw fail('原请求账本 metadata 无法完整核对,原字节保持。');
|
||||
const expectedRows = entries.map((entry, index) => {
|
||||
const row = legacyLedgerRow(entry, index);
|
||||
return { legacyPosition: index, sequence: row.seq, rowSha256: rowHash(row), payloadSha256: row.payload_sha256, initialSha256: row.initial_sha256 };
|
||||
});
|
||||
databaseState = { userVersion: database.prepare('PRAGMA user_version').get().user_version, objects,
|
||||
sequences: database.prepare("SELECT name FROM sqlite_schema WHERE name='sqlite_sequence'").get()
|
||||
? database.prepare('SELECT name,seq FROM sqlite_sequence ORDER BY name').all().map(row => ({ name: row.name, seq: row.seq })) : [],
|
||||
legacyMetadata: raw === undefined ? null : { bytes: Buffer.byteLength(raw), sha256: digest(raw) }, expectedLegacyRows: expectedRows,
|
||||
ledgerMarker: markerRow ? { value: JSON.parse(markerRow.value), rowSha256: rowHash(markerRow) } : null };
|
||||
database.exec('ROLLBACK');
|
||||
} finally { database.close(); }
|
||||
}
|
||||
const artifacts = [], artifactRoot = path.join(dataRoot, 'artifacts');
|
||||
if (fs.existsSync(artifactRoot)) {
|
||||
const walk = relative => { for (const entry of fs.readdirSync(path.join(artifactRoot, relative), { withFileTypes: true })) {
|
||||
const next = path.posix.join(relative, entry.name), file = ownedChild(dataRoot, 'artifacts/' + next);
|
||||
if (entry.isDirectory()) walk(next);
|
||||
else if (entry.isFile()) { const bytes = fs.readFileSync(file); artifacts.push({ path: next, bytes: bytes.length, sha256: digest(bytes) }); }
|
||||
else throw fail('原工件目录包含非常规文件。');
|
||||
} }; walk('');
|
||||
}
|
||||
const files = ['config.json', 'projects.json'].filter(relative => fs.existsSync(path.join(dataRoot, relative)))
|
||||
.map(relative => ({ path: relative, sha256: digest(fs.readFileSync(ownedChild(dataRoot, relative))) }));
|
||||
assertLocalTree(dataRoot);
|
||||
return { tables, database: databaseState, artifacts: artifacts.sort((a, b) => a.path.localeCompare(b.path)), files,
|
||||
scope: 'Every persistent row except runtimeOwner, schema objects/user_version/sequences, every original artifact byte and public config/project files; no credential is decrypted or returned.' };
|
||||
}
|
||||
|
||||
export function verifyPersistentDataTransition(before, after, compatibility) {
|
||||
assertDataCompatibility(after, compatibility);
|
||||
if (isDeepStrictEqual(before, after)) return { kind: 'unchanged', databaseSchema: after.database?.userVersion ?? null };
|
||||
same(before.artifacts, after.artifacts, '安装更新改变了原工件字节。');
|
||||
same(before.files, after.files, '安装更新改变了原公开配置或项目字节。');
|
||||
const old = before.database, next = after.database;
|
||||
if (!old || !next || old.userVersion !== 1 || next.userVersion !== 2 || old.ledgerMarker
|
||||
|| Object.hasOwn(before.tables, 'model_attempts') || !isDeepStrictEqual(compatibility?.ledgerMigration, REQUEST_LEDGER_MIGRATION)) throw fail('安装更新不是已核验的精确请求账本迁移,原数据仍需核对。');
|
||||
same(old.legacyMetadata, next.legacyMetadata, '迁移改变了原请求 metadata 字符串。');
|
||||
same(old.expectedLegacyRows, next.expectedLegacyRows, '迁移原请求条目或顺序变化。');
|
||||
const marker = next.ledgerMarker?.value;
|
||||
if (!marker || Object.keys(marker).sort().join(',') !== 'id,legacyEntries,legacyMetadataSha256,migratedAt,originalMetadataPreserved,schemaVersion'
|
||||
|| marker.schemaVersion !== 1 || !/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(marker.id)
|
||||
|| marker.legacyEntries !== old.expectedLegacyRows.length || marker.legacyMetadataSha256 !== (old.legacyMetadata?.sha256 ?? null)
|
||||
|| marker.originalMetadataPreserved !== true || typeof marker.migratedAt !== 'string'
|
||||
|| !Number.isFinite(Date.parse(marker.migratedAt)) || new Date(marker.migratedAt).toISOString() !== marker.migratedAt) throw fail('请求账本迁移标记与原来源不同或含未知字段。');
|
||||
const expectedObjects = [...old.objects, ...REQUEST_LEDGER_OBJECTS].sort((a, b) => a.name.localeCompare(b.name));
|
||||
same(expectedObjects, next.objects.slice().sort((a, b) => a.name.localeCompare(b.name)), '迁移产生了未知表、索引、视图、触发器或改变了原 schema。');
|
||||
const expectedSequences = old.sequences.concat(old.expectedLegacyRows.length ? [{ name: 'model_attempts', seq: old.expectedLegacyRows.length }] : []).sort((a, b) => a.name.localeCompare(b.name));
|
||||
same(expectedSequences, next.sequences.slice().sort((a, b) => a.name.localeCompare(b.name)), '迁移改变了原数据库序号或账本序号。');
|
||||
const expectedTables = structuredClone(before.tables);
|
||||
if (!expectedTables.metadata) throw fail('原数据库缺少可核验 metadata 表。');
|
||||
expectedTables.metadata = { count: before.tables.metadata.count + 1, hashes: before.tables.metadata.hashes.concat(next.ledgerMarker.rowSha256).sort() };
|
||||
expectedTables.model_attempts = { count: old.expectedLegacyRows.length, hashes: old.expectedLegacyRows.map(row => row.rowSha256).sort() };
|
||||
same(expectedTables, after.tables, '请求账本迁移少行、重复归属、记录/初始来源/搜索字段变化,或原持久行改变。');
|
||||
return { kind: REQUEST_LEDGER_MIGRATION.id, migration: REQUEST_LEDGER_MIGRATION, legacyEntries: old.expectedLegacyRows.length,
|
||||
originalMetadata: old.legacyMetadata, legacyRows: old.expectedLegacyRows, markerSha256: next.ledgerMarker.rowSha256,
|
||||
originalRowsPreserved: true, originalArtifactsPreserved: true, originalConfigPreserved: true };
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import net from 'node:net';
|
||||
import { isDeepStrictEqual } from 'node:util';
|
||||
import { HostController } from './host-controller.mjs';
|
||||
@@ -9,10 +8,11 @@ import { inspectHostInstallation } from './app-installation.mjs';
|
||||
import { observeWindowsProcess, protectUserDirectory, binaryIdentity } from './process-identity.mjs';
|
||||
import { ownedChild, treeFile, readState, writeState, samePath, assertLocalTree } from './file-state.mjs';
|
||||
import { upgradeRunning } from '../../../scripts/upgrade-running.mjs';
|
||||
import { canonicalHost, hostError } from '../../shared/contracts/desktop-host.mjs';
|
||||
import { hostError } from '../../shared/contracts/desktop-host.mjs';
|
||||
import { InstallationProgress } from './installation-progress.mjs';
|
||||
import { snapshotPersistentData, readDataCompatibility, assertDataCompatibility, verifyPersistentDataTransition } from './data-retention.mjs';
|
||||
export { snapshotPersistentData } from './data-retention.mjs';
|
||||
|
||||
const digest = value => createHash('sha256').update(value).digest('hex');
|
||||
const inside = (root, value) => samePath(root, value) || path.resolve(value).toLowerCase().startsWith(path.resolve(root).toLowerCase() + path.sep);
|
||||
const fail = message => hostError(message, 'INSTALLATION_REJECTED', 409);
|
||||
|
||||
@@ -38,29 +38,6 @@ export function validateInstalledVersion(directory, { onProgress = null } = {})
|
||||
return inspectHostInstallation(directory, { onProgress });
|
||||
}
|
||||
|
||||
export function snapshotPersistentData(dataRoot) {
|
||||
assertLocalTree(dataRoot); const databaseFile = path.join(dataRoot, 'workbench.sqlite'), tables = {};
|
||||
if (fs.existsSync(databaseFile)) {
|
||||
ownedChild(dataRoot, 'workbench.sqlite'); const database = new DatabaseSync(databaseFile, { readOnly: true });
|
||||
try {
|
||||
for (const { name } of database.prepare("SELECT name FROM sqlite_schema WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name").all()) {
|
||||
if (!/^[a-z_]+$/.test(name)) throw fail('原数据库包含无法核对的表名。');
|
||||
const rows = database.prepare('SELECT * FROM "' + name + '"').all().filter(row => !(name === 'metadata' && row.key === 'runtimeOwner'));
|
||||
const hashes = rows.map(row => digest(JSON.stringify(canonicalHost(row)))).sort(); tables[name] = { count: hashes.length, hashes };
|
||||
}
|
||||
} finally { database.close(); }
|
||||
}
|
||||
const artifacts = [], artifactRoot = path.join(dataRoot, 'artifacts');
|
||||
if (fs.existsSync(artifactRoot)) {
|
||||
const walk = relative => { for (const entry of fs.readdirSync(path.join(artifactRoot, relative), { withFileTypes: true })) {
|
||||
const next = path.posix.join(relative, entry.name), file = ownedChild(dataRoot, 'artifacts/' + next);
|
||||
if (entry.isDirectory()) walk(next); else if (entry.isFile()) { const bytes = fs.readFileSync(file); artifacts.push({ path: next, bytes: bytes.length, sha256: digest(bytes) }); } else throw fail('原工件目录包含非常规文件。');
|
||||
} }; walk('');
|
||||
}
|
||||
const files = ['config.json', 'projects.json'].filter(relative => fs.existsSync(path.join(dataRoot, relative))).map(relative => ({ path: relative, sha256: digest(fs.readFileSync(ownedChild(dataRoot, relative))) }));
|
||||
return { tables, artifacts: artifacts.sort((a, b) => a.path.localeCompare(b.path)), files, scope: 'Every persistent database row except runtimeOwner, every original artifact byte and existing public config/project files; no credential is decrypted or returned.' };
|
||||
}
|
||||
|
||||
export class InstallationManager {
|
||||
constructor({ installRoot, dataRoot, port = 4318, candidateRoot = null, timeoutMs = 60000, operationId = null, expectedManifestSha256 = null, onProgress = null }) {
|
||||
for (const value of [installRoot, dataRoot]) if (typeof value !== 'string' || !path.isAbsolute(value)) throw fail('安装和数据目录必须为本机绝对路径。');
|
||||
@@ -173,16 +150,28 @@ export class InstallationManager {
|
||||
if (!root || binaryIdentity(bootstrap).sha256 !== binaryIdentity(ownedChild(root, 'AgentWorkbench.exe')).sha256) throw fail('原根启动器与所属发行不一致,不复用或覆盖未知程序。');
|
||||
}
|
||||
}
|
||||
async activate({ stageOnly = false, beforePointer = null, afterPointer = null } = {}) {
|
||||
async rollback({ recoveryRoot = null } = {}) {
|
||||
const original = this.current();
|
||||
if (!original) throw fail('没有可核验的活动安装,不能选择恢复载荷。');
|
||||
const target = recoveryRoot ?? (original.previous ? this.versionRoot(original.previous) : null);
|
||||
if (!target) throw fail('没有已登记的前版,请指定已核验的兼容恢复载荷。');
|
||||
this.candidateRoot = fs.realpathSync.native(target);
|
||||
const result = await this.activate({ expectedCurrent: original });
|
||||
return { ...result, operation: 'rollback', recoveryTarget: result.active };
|
||||
}
|
||||
async activate({ stageOnly = false, beforePointer = null, afterPointer = null, expectedCurrent = null } = {}) {
|
||||
return this.operation('activate', async () => {
|
||||
const candidate = this.candidate(), old = this.current(); this.inspectRoot(old); const priorHost = stageOnly ? null : await this.host();
|
||||
if (expectedCurrent && !isDeepStrictEqual(expectedCurrent, old)) throw fail('选择恢复载荷后安装指针已变化,保留原记录。');
|
||||
this.phase('snapshot-data');
|
||||
const snapshot = snapshotPersistentData(this.dataRoot), id = randomUUID(), receiptFile = path.join(this.stateRoot, 'operation-' + id + '.json');
|
||||
const compatibility = readDataCompatibility(this.candidateRoot);
|
||||
if (!stageOnly) assertDataCompatibility(snapshot, compatibility);
|
||||
this.receiptFile = receiptFile;
|
||||
const receipt = { schema: 1, id, operationId: this.progress?.value.id, state: 'prepared', candidate, original: old?.active ?? null, dataRoot: this.dataRoot, snapshot, stageOnly, startedAt: new Date().toISOString() };
|
||||
const receipt = { schema: 1, id, operationId: this.progress?.value.id, state: 'prepared', candidate, original: old?.active ?? null, dataRoot: this.dataRoot, snapshot, compatibility, stageOnly, startedAt: new Date().toISOString() };
|
||||
writeState(receiptFile, receipt);
|
||||
if (stageOnly) { writeState(receiptFile, { ...receipt, state: 'staged' }); return { phase: 'staged', candidate, receiptFile, activated: false }; }
|
||||
let upgraded = false, next = null, pointerPublished = false;
|
||||
let upgraded = false, next = null, pointerPublished = false, dataVerification = null;
|
||||
const originalPointer = old ? fs.readFileSync(this.recordFile) : null;
|
||||
const bootstrapFile = ownedChild(this.installRoot, 'AgentWorkbench.exe', { missing: true });
|
||||
const originalBootstrap = fs.existsSync(bootstrapFile) ? fs.readFileSync(bootstrapFile) : null;
|
||||
@@ -202,13 +191,15 @@ export class InstallationManager {
|
||||
if (!nextHost || nextHost.identity.pid !== priorHost.identity.pid || nextHost.identity.instanceId !== priorHost.identity.instanceId || nextHost.identity.runtimeIdentity !== candidate.runtimeIdentity) throw fail('新处理器没有保持原宿主实例。');
|
||||
}
|
||||
this.phase('verify-data');
|
||||
const after = snapshotPersistentData(this.dataRoot); if (!isDeepStrictEqual(snapshot, after)) throw fail('安装更新改变了原持久行或工件。');
|
||||
const after = snapshotPersistentData(this.dataRoot);
|
||||
dataVerification = verifyPersistentDataTransition(snapshot, after, compatibility);
|
||||
if (beforePointer) await beforePointer();
|
||||
if (!isDeepStrictEqual(old, this.current())) throw fail('原安装指针已发生变化,停止发布候选。');
|
||||
if (!isDeepStrictEqual(candidate, this.candidate())) throw fail('新候选在预检后发生变化,停止发布候选。');
|
||||
this.phase('publish-pointer', { interruptible: false });
|
||||
const actualBootstrap = fs.existsSync(bootstrapFile) ? fs.readFileSync(bootstrapFile) : null;
|
||||
if (!isDeepStrictEqual(actualBootstrap, originalBootstrap)) throw fail('原启动器在预检后发生变化,停止发布候选。');
|
||||
if (!isDeepStrictEqual(after, snapshotPersistentData(this.dataRoot))) throw fail('安装发布前原数据再次变化,停止发布候选并保留现场。');
|
||||
if (!isDeepStrictEqual(originalBootstrap, candidateBootstrap)) { replaceBootstrap(candidateBootstrap); bootstrapPublished = true; }
|
||||
next = { schema: 1, app: 'agent-workbench-installation', installId: old?.installId ?? randomUUID(), revision: (old?.revision ?? 0) + 1,
|
||||
dataRoot: this.dataRoot, port: this.port, active: candidate, previous: old?.active ?? null, activatedAt: new Date().toISOString() };
|
||||
@@ -217,12 +208,19 @@ export class InstallationManager {
|
||||
this.phase('verify-publication', { interruptible: false });
|
||||
if (afterPointer) await afterPointer(); // Explicit in-process QA fault fixture, never exposed by the CLI.
|
||||
this.inspectRoot(next);
|
||||
writeState(receiptFile, { ...receipt, state: 'activated', hostRetained: upgraded, finishedAt: new Date().toISOString() });
|
||||
return { phase: 'activated', active: candidate, receiptFile, hostRetained: upgraded, dataPreserved: true };
|
||||
if (!isDeepStrictEqual(after, snapshotPersistentData(this.dataRoot))) throw fail('安装发布后原数据变化,保持当前记录并核对兼容恢复载荷。');
|
||||
writeState(receiptFile, { ...receipt, state: 'activated', hostRetained: upgraded, dataVerification, finishedAt: new Date().toISOString() });
|
||||
return { phase: 'activated', active: candidate, receiptFile, hostRetained: upgraded, dataPreserved: true, dataVerification };
|
||||
} catch (error) {
|
||||
try { this.phase('rollback', { interruptible: false, originalError: error.message }); } catch {}
|
||||
let rollback = 'pointer-preserved';
|
||||
try {
|
||||
// Decide before restoring a pointer: a schema1 pointer is not a valid
|
||||
// recovery target after new results. Keep the live data and receipt.
|
||||
const atFailure = snapshotPersistentData(this.dataRoot);
|
||||
const rollbackTransition = verifyPersistentDataTransition(snapshot, atFailure, compatibility);
|
||||
const rollbackInitialization = upgraded && rollbackTransition.kind === 'model-attempt-ledger-v1';
|
||||
if (upgraded && !rollbackInitialization) assertDataCompatibility(atFailure, readDataCompatibility(priorHost.identity.runtimeRoot));
|
||||
if (pointerPublished && fs.existsSync(this.recordFile)) {
|
||||
if (!isDeepStrictEqual(readState(this.recordFile), next)) throw fail('发布后的指针被其他操作改变,不能覆盖未知记录。');
|
||||
if (originalPointer) {
|
||||
@@ -235,17 +233,17 @@ export class InstallationManager {
|
||||
if (originalBootstrap) replaceBootstrap(originalBootstrap); else fs.unlinkSync(bootstrapFile);
|
||||
}
|
||||
if (upgraded) {
|
||||
await upgradeRunning({ url: priorHost.controller.url, pid: priorHost.identity.pid, currentRoot: priorHost.identity.appRoot, handlerRoot: this.candidateRoot, newRoot: priorHost.identity.runtimeRoot });
|
||||
await upgradeRunning({ url: priorHost.controller.url, pid: priorHost.identity.pid, currentRoot: priorHost.identity.appRoot, handlerRoot: this.candidateRoot, newRoot: priorHost.identity.runtimeRoot, rollbackInitialization });
|
||||
const restored = await priorHost.controller.connect(); if (!restored || restored.identity.instanceId !== priorHost.identity.instanceId || restored.identity.pid !== priorHost.identity.pid) throw fail('原宿主回退仍需核对。');
|
||||
rollback = 'original-host-and-pointer-restored';
|
||||
}
|
||||
if (!isDeepStrictEqual(snapshot, snapshotPersistentData(this.dataRoot)) || (old && !fs.readFileSync(this.recordFile).equals(originalPointer))) throw fail('原持久数据或安装指针回退仍需核对。');
|
||||
if (!isDeepStrictEqual(fs.existsSync(bootstrapFile) ? fs.readFileSync(bootstrapFile) : null, originalBootstrap)) throw fail('原启动器回退仍需核对。');
|
||||
} catch (rollbackError) {
|
||||
try { writeState(receiptFile, { ...receipt, state: 'rollback-unverified', error: error.message, rollbackError: rollbackError.message }); } catch {}
|
||||
try { writeState(receiptFile, { ...receipt, state: 'rollback-unverified', error: error.message, rollbackError: rollbackError.message, dataVerification, publishedPointerKept: pointerPublished }); } catch {}
|
||||
throw fail('安装失败,原宿主或指针回退尚待核对。请保留原操作记录,不重新执行任务。');
|
||||
}
|
||||
try { writeState(receiptFile, { ...receipt, state: 'failed', error: error.message, rollback }); } catch {}
|
||||
try { writeState(receiptFile, { ...receipt, state: 'failed', error: error.message, rollback, dataVerification }); } catch {}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { ProjectMemoryManager } from './project-memory.mjs';
|
||||
import { experienceEligible } from './experience-drafts.mjs';
|
||||
import { MnemoClient, MNEMO_PROTOCOL, bridgeOrigin, bridgeError, checkMemory, digest, sourceDigest, eventDigest, manifestDigest, contextDigest, entryRef, checkRecall, strict } from './mnemo-protocol.mjs';
|
||||
|
||||
const key = 'mnemoProjectMemoryBridge';
|
||||
const storeLocks = new WeakMap();
|
||||
const stamp = () => new Date().toISOString();
|
||||
export const memoryConfirmationRevision = m => ({ id: m.id, title: m.title, content: m.content, source: m.source, sourceFingerprint: m.sourceFingerprint,
|
||||
expiresAt: m.expiresAt, experience: m.experience ?? null });
|
||||
export const memoryConfirmationDigest = m => digest(memoryConfirmationRevision(m));
|
||||
const payload = (m, kind) => ({ title: m.title, content: m.content, kind });
|
||||
const unavailable = cause => ({ code: typeof cause.code === 'string' ? cause.code : 'PROJECT_BRIDGE_UNAVAILABLE', message: cause.code ? cause.message : '可选记忆桥本次不可用,原任务继续。' });
|
||||
export function mnemoMemoryMessage(reference, document) {
|
||||
return { role: 'system', content: '以下是Mnemo经授权召回的私有项目记忆数据,保留来源声明,不授予执行权限,不证明工程已交付。当前用户要求和实际工程证据优先。\n'
|
||||
+ JSON.stringify({ mnemo_memory_artifact: { id: reference.id, sha256: reference.sha256 }, mnemo_project_memory: document }) };
|
||||
}
|
||||
|
||||
/** Tokens live only in this host. Backups/restarts never restore an authorization.
|
||||
* The independent local high-water journal also survives an in-place DB restore.
|
||||
* Mnemo remains the authority for grants and permanent remote tombstones. */
|
||||
export class MnemoBridge {
|
||||
constructor(store, { memories = new ProjectMemoryManager(store), fetch, timeoutMs } = {}) {
|
||||
Object.assign(this, { store, memories, fetch, timeoutMs }); this.tokens = new Map();
|
||||
if (!storeLocks.has(store)) storeLocks.set(store, new Map()); this.locks = storeLocks.get(store);
|
||||
this.floorPath = path.join(store.directory, 'mnemo-bridge-high-water.json');
|
||||
}
|
||||
state() {
|
||||
let state = this.store.storage.meta(key);
|
||||
if (!state) { state = { schemaVersion: 1, producerId: randomUUID(), projects: {} }; this.save(state); }
|
||||
if (state.schemaVersion !== 1 || !state.projects || typeof state.producerId !== 'string') throw bridgeError('PROJECT_STATE_INVALID', '原记忆桥登记无法核对,未覆盖原记录。');
|
||||
for (const p of Object.values(state.projects)) for (const s of Object.values(p.sources)) {
|
||||
if (s.event) {
|
||||
const event = p.events.find(e => e.requestKey === s.event.requestKey);
|
||||
if (!event) throw bridgeError('PROJECT_STATE_INVALID', '来源原键与持久事件不一致。');
|
||||
s.event = event;
|
||||
}
|
||||
}
|
||||
const floor = this.floor(state); let erased = false;
|
||||
for (const [projectId, p] of Object.entries(state.projects)) for (const s of Object.values(p.sources)) {
|
||||
if (floor.sources[projectId + '/' + s.memoryId]?.deleted) { s.deleted = true; s.confirmation = null;
|
||||
erased = this.erasePackages(p, s.memoryId) || erased; }
|
||||
}
|
||||
if (erased) this.save(state);
|
||||
return state;
|
||||
}
|
||||
save(state) { this.store.storage.transaction(() => this.store.storage.setMeta(key, state)); }
|
||||
project(state, projectId) {
|
||||
this.store.project(projectId);
|
||||
return state.projects[projectId] ??= { enabled: false, baseUrl: '', bindingId: '', revision: 0, sources: {}, events: [] };
|
||||
}
|
||||
async locked(projectId, action) {
|
||||
// All projects share one metadata document. Serialize the whole read/await/
|
||||
// write transaction, including distinct manager instances of this Store.
|
||||
projectId = 'all-projects';
|
||||
const previous = this.locks.get(projectId) ?? Promise.resolve();
|
||||
let release; const current = new Promise(resolve => { release = resolve; }); this.locks.set(projectId, current);
|
||||
await previous;
|
||||
try { return await action(); } finally { release(); if (this.locks.get(projectId) === current) this.locks.delete(projectId); }
|
||||
}
|
||||
floor(state) {
|
||||
let value = { schemaVersion: 1, producerId: state.producerId, sources: {} };
|
||||
if (fs.existsSync(this.floorPath)) {
|
||||
if (fs.lstatSync(this.floorPath).isSymbolicLink()) throw bridgeError('PROJECT_STATE_INVALID', '记忆桥高水位来源不能是链接。');
|
||||
try { value = JSON.parse(fs.readFileSync(this.floorPath, 'utf8')); } catch { throw bridgeError('PROJECT_STATE_INVALID', '记忆桥高水位无法核对。'); }
|
||||
if (value.schemaVersion !== 1 || value.producerId !== state.producerId || !value.sources) throw bridgeError('PROJECT_STATE_INVALID', '生产者身份与本机高水位不一致,需重新核对。');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
advance(state, projectId, sourceId, sequence, tombstone) {
|
||||
const floor = this.floor(state), id = projectId + '/' + sourceId, old = floor.sources[id];
|
||||
if (old?.deleted && !tombstone) throw bridgeError('PROJECT_SOURCE_DELETED', '该来源有永久删除墓碑,不能恢复或重新确认。');
|
||||
floor.sources[id] = { sequence: Math.max(old?.sequence ?? 0, sequence), deleted: !!(old?.deleted || tombstone) };
|
||||
const temporary = this.floorPath + '.' + randomUUID() + '.tmp';
|
||||
fs.writeFileSync(temporary, JSON.stringify(floor), { mode: 0o600, flag: 'wx' }); fs.renameSync(temporary, this.floorPath);
|
||||
}
|
||||
client(state, projectId, trace = []) {
|
||||
const p = this.project(state, projectId), token = this.tokens.get(projectId);
|
||||
return new MnemoClient({ enabled: p.enabled, baseUrl: p.baseUrl, bindingId: p.bindingId, producerId: state.producerId, projectId,
|
||||
token: token?.revision === p.revision ? token.value : null, fetch: this.fetch, timeoutMs: this.timeoutMs,
|
||||
observe: value => trace.push(value) });
|
||||
}
|
||||
async view(projectId) {
|
||||
const state = this.state(), p = this.project(state, projectId), local = await this.memories.list(projectId), floor = this.floor(state);
|
||||
return { schemaVersion: 1, protocol: MNEMO_PROTOCOL, producerId: state.producerId, projectId,
|
||||
connection: { enabled: p.enabled, baseUrl: p.baseUrl, bindingId: p.bindingId, revision: p.revision, selectionLimit: p.selectionLimit ?? 50,
|
||||
hasToken: this.tokens.get(projectId)?.revision === p.revision, credentialStorage: 'host-memory', restartRequiresAuthorization: true },
|
||||
sources: local.memories.map(m => { const s = p.sources[m.id], deleted = s?.deleted || floor.sources[projectId + '/' + m.id]?.deleted;
|
||||
return { memoryId: m.id, title: m.title, localStatus: m.status, review: memoryConfirmationRevision(m), confirmationDigest: memoryConfirmationDigest(m),
|
||||
state: deleted ? 'deleted' : s?.state ?? 'not_shared', sequence: s?.sequence ?? 0, version: s?.version ?? null,
|
||||
confirmation: s?.confirmation ?? null, requestKey: s?.event?.requestKey ?? null, syncStatus: s?.event?.status ?? 'not_sent',
|
||||
receipt: s?.event?.receipt ?? null, issue: s?.issue ?? null }; }),
|
||||
events: p.events.map(e => ({ requestKey: e.requestKey, sourceId: e.input.source.id, sequence: e.input.source.sequence, state: e.input.source.state, status: e.status,
|
||||
createdAt: e.createdAt, receipt: e.receipt ?? null, issue: e.issue ?? null })),
|
||||
rule: '默认关闭。逐条人工确认后才共享;本次凭据仅在当前宿主内存中。重启/恢复须重新授权。桥不可用时原任务继续。' };
|
||||
}
|
||||
async configure(projectId, input) {
|
||||
strict(input, ['enabled', 'baseUrl', 'bindingId', 'token', 'expectedRevision', 'selectionLimit'], ['enabled', 'expectedRevision']);
|
||||
return this.locked(projectId, async () => {
|
||||
const state = this.state(), p = this.project(state, projectId); this.floor(state);
|
||||
if (input.expectedRevision !== p.revision || typeof input.enabled !== 'boolean') throw bridgeError('PROJECT_CONFIG_CHANGED', '记忆桥设置已变化,请刷新后保存。');
|
||||
const baseUrl = input.baseUrl === undefined ? p.baseUrl : bridgeOrigin(input.baseUrl), bindingId = input.bindingId ?? p.bindingId;
|
||||
const same = baseUrl === p.baseUrl && bindingId === p.bindingId;
|
||||
const token = input.token === undefined ? same ? this.tokens.get(projectId)?.value : null : input.token;
|
||||
if (token !== null && token !== undefined && (typeof token !== 'string' || !token || token.length > 16000 || /[\r\n]/.test(token))) throw bridgeError('PROJECT_TOKEN_INVALID', '请填写本项目新授权返回的凭据。');
|
||||
const selectionLimit = input.selectionLimit ?? p.selectionLimit ?? 50;
|
||||
if (!Number.isInteger(selectionLimit) || selectionLimit < 1 || selectionLimit > 50) throw bridgeError('PROJECT_SELECTION_INVALID', '可选记忆桥每次选入条数需要1至50,原任务全文与额度保持。');
|
||||
const revision = p.revision + 1, next = { ...p, enabled: input.enabled, baseUrl, bindingId, revision, selectionLimit };
|
||||
if (input.enabled) {
|
||||
const client = new MnemoClient({ enabled: true, baseUrl, bindingId, producerId: state.producerId, projectId, token, fetch: this.fetch, timeoutMs: this.timeoutMs });
|
||||
if (!token) throw bridgeError('PROJECT_TOKEN_REQUIRED', '重启或恢复后需填写新项目凭据,不能从备份恢复授权。');
|
||||
await client.capabilities();
|
||||
}
|
||||
state.projects[projectId] = next; this.save(state);
|
||||
if (input.enabled) this.tokens.set(projectId, { revision, value: token }); else this.tokens.delete(projectId);
|
||||
return this.view(projectId);
|
||||
});
|
||||
}
|
||||
event(state, projectId, s, source, memory) {
|
||||
const p = this.project(state, projectId), prior = this.floor(state).sources[projectId + '/' + source.id];
|
||||
const sequence = Math.max(s.sequence ?? 0, prior?.sequence ?? 0) + 1;
|
||||
if (!Number.isSafeInteger(sequence)) throw bridgeError('PROJECT_SEQUENCE_INVALID', '来源序号无法继续,原记录保持。');
|
||||
const normalized = { ...source, sequence, version: 'rev-' + sequence }, input = { protocol: MNEMO_PROTOCOL, bindingId: p.bindingId, requestKey: randomUUID(), source: normalized, memory };
|
||||
this.advance(state, projectId, source.id, sequence, source.state === 'deleted');
|
||||
const e = { requestKey: input.requestKey, input, expectedDigests: { sourceDigest: sourceDigest(normalized, memory), eventDigest: eventDigest(input) }, status: 'prepared', createdAt: stamp(), attempts: [], receipt: null };
|
||||
Object.assign(s, { sequence, version: normalized.version, state: normalized.state, digest: sourceDigest(normalized, memory), event: e });
|
||||
p.events.push(e); if (source.state === 'deleted') this.erasePackages(p, source.id); this.save(state); return e;
|
||||
}
|
||||
async settle(state, projectId, s, e, trace, isNew = false) {
|
||||
const client = this.client(state, projectId, trace);
|
||||
try {
|
||||
let receipt;
|
||||
if (isNew) {
|
||||
// Persist intent first. A restart can only lookup this exact package.
|
||||
e.status = 'unknown'; this.save(state); receipt = await client.sync(e.input);
|
||||
} else {
|
||||
const result = await client.lookup(e.input, e.expectedDigests); if (!result.found) return;
|
||||
receipt = result.receipt;
|
||||
}
|
||||
e.status = 'committed'; e.receipt = receipt; e.issue = null;
|
||||
if (['deleted', 'locally_deleted'].includes(receipt.currentState)) { s.deleted = true; s.confirmation = null; s.state = 'deleted'; this.advance(state, projectId, s.memoryId, s.sequence, true); this.erasePackages(this.project(state, projectId), s.memoryId); }
|
||||
} catch (cause) {
|
||||
e.status = !isNew || cause.code === 'PROJECT_SYNC_UNKNOWN' ? 'unknown' : 'rejected'; e.issue = unavailable(cause);
|
||||
} finally { e.attempts.push(...trace.splice(0)); if (s.deleted) this.erasePackages(this.project(state, projectId), s.memoryId); this.save(state); }
|
||||
}
|
||||
erasePackages(p, sourceId) {
|
||||
let changed = false;
|
||||
for (const e of p.events.filter(value => value.input.source.id === sourceId)) {
|
||||
if (e.input.memory !== null) { e.expectedDigests ??= { sourceDigest: sourceDigest(e.input.source, e.input.memory), eventDigest: eventDigest(e.input) }; e.input.memory = null; changed = true; }
|
||||
e.bodyErased = true;
|
||||
for (const attempt of e.attempts) {
|
||||
if (attempt.request?.memory) { attempt.requestSha256 ??= digest(attempt.request); attempt.request.memory = null; changed = true; }
|
||||
if (attempt.responseText !== null) { attempt.responseTextSha256 ??= digest(attempt.responseText); attempt.responseText = null; changed = true; }
|
||||
if (attempt.response !== null) { attempt.responseSha256 ??= digest(attempt.response); attempt.response = null; changed = true; }
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
async changeSource(projectId, memoryId, input) {
|
||||
strict(input, ['action', 'confirmed', 'expectedDigest', 'kind'], ['action', 'confirmed', 'expectedDigest']);
|
||||
return this.locked(projectId, async () => {
|
||||
const state = this.state(), p = this.project(state, projectId), view = await this.memories.list(projectId), m = view.memories.find(v => v.id === memoryId);
|
||||
if (!p.enabled || this.tokens.get(projectId)?.revision !== p.revision) throw bridgeError('PROJECT_BRIDGE_DISABLED', '请先显式连接本项目记忆桥。');
|
||||
if (!m) throw bridgeError('PROJECT_SOURCE_MISSING', '来源记忆不存在。', 404);
|
||||
if (input.confirmed !== true || input.expectedDigest !== memoryConfirmationDigest(m) || !['candidate', 'confirm', 'withdraw', 'delete'].includes(input.action)) throw bridgeError('PROJECT_CONFIRMATION_CHANGED', '请核对当前完整正文后明确确认,旧页面不能确认新修订。');
|
||||
const s = p.sources[m.id] ??= { memoryId: m.id, sequence: 0, confirmation: null, state: 'unverified' };
|
||||
if (s.deleted || this.floor(state).sources[projectId + '/' + m.id]?.deleted) throw bridgeError('PROJECT_SOURCE_DELETED', '该来源已永久删除,不能复活。');
|
||||
if (s.event && ['unknown', 'prepared'].includes(s.event.status)) { await this.settle(state, projectId, s, s.event, []); if (s.event.status !== 'committed') throw bridgeError('PROJECT_SYNC_UNKNOWN', '前次同步仍未知,只允许查询原键,不能换键重送。'); }
|
||||
if (s.deleted) throw bridgeError('PROJECT_SOURCE_DELETED', '该来源已永久删除,不能复活。');
|
||||
const kind = input.kind ?? (m.experience ? 'experience' : 'fact');
|
||||
const sharing = ['candidate', 'confirm'].includes(input.action);
|
||||
if (sharing) {
|
||||
checkMemory(payload(m, kind));
|
||||
if (!view.scope.complete || m.status !== 'current' || input.action === 'confirm' && !experienceEligible(m)) throw bridgeError('PROJECT_SOURCE_UNVERIFIED', '请先核对当前来源;经验草稿须先在来源产品明确采用。');
|
||||
}
|
||||
s.kind = kind; s.signature = memoryConfirmationDigest(m);
|
||||
s.confirmation = input.action === 'confirm' ? { by: 'user', at: stamp() } : null;
|
||||
s.deleted = input.action === 'delete';
|
||||
const sourceState = { confirm: 'current', candidate: 'candidate', withdraw: 'withdrawn', delete: 'deleted' }[input.action];
|
||||
const e = this.event(state, projectId, s, { id: m.id, state: sourceState, fingerprint: m.sourceFingerprint, expiresAt: m.expiresAt ?? null, confirmation: s.confirmation }, sharing ? payload(m, kind) : null);
|
||||
await this.settle(state, projectId, s, e, [], true); return this.view(projectId);
|
||||
});
|
||||
}
|
||||
async lookupSource(projectId, memoryId) {
|
||||
return this.locked(projectId, async () => {
|
||||
const state = this.state(), p = this.project(state, projectId), s = p.sources[memoryId];
|
||||
if (!s?.event) throw bridgeError('PROJECT_SOURCE_MISSING', '没有可核对的原同步请求。', 404);
|
||||
await this.settle(state, projectId, s, s.event, []); return this.view(projectId);
|
||||
});
|
||||
}
|
||||
async fresh(state, projectId, trace) {
|
||||
const p = this.project(state, projectId), view = await this.memories.list(projectId), sources = [], floor = this.floor(state);
|
||||
if (!view.scope.complete) throw bridgeError('PROJECT_MANIFEST_INCOMPLETE', '本地项目核对不完整,本次省略远端记忆。');
|
||||
const byId = new Map(view.memories.map(m => [m.id, m]));
|
||||
for (const s of Object.values(p.sources)) {
|
||||
if (s.event && ['unknown', 'prepared'].includes(s.event.status)) await this.settle(state, projectId, s, s.event, trace);
|
||||
const m = byId.get(s.memoryId), high = floor.sources[projectId + '/' + s.memoryId];
|
||||
let desired = s.deleted || high?.deleted || !m ? 'deleted' : m.status === 'archived' ? 'archived' : m.status === 'expired' ? 'expired'
|
||||
: m.status !== 'current' || !experienceEligible(m) && s.state !== 'candidate' || m.sourceFingerprint !== view.sourceFingerprint ? 'unverified'
|
||||
: memoryConfirmationDigest(m) !== s.signature ? 'unverified' : s.confirmation ? 'current' : s.state === 'candidate' ? 'candidate' : s.state === 'withdrawn' ? 'withdrawn' : 'unverified';
|
||||
if (desired === 'deleted') { s.deleted = true; s.confirmation = null; }
|
||||
if (s.event && ['unknown', 'prepared'].includes(s.event.status)) continue;
|
||||
if (s.event?.status === 'rejected') continue;
|
||||
if (desired !== s.state || high?.sequence > s.sequence) {
|
||||
if (desired !== 'current') s.confirmation = null;
|
||||
const e = this.event(state, projectId, s, { id: s.memoryId, state: desired, fingerprint: m?.sourceFingerprint ?? null,
|
||||
expiresAt: m?.expiresAt ?? null, confirmation: desired === 'current' ? s.confirmation : null }, ['current', 'candidate'].includes(desired) ? payload(m, s.kind) : null);
|
||||
await this.settle(state, projectId, s, e, trace, true);
|
||||
}
|
||||
if (desired === 'current' && s.event?.status === 'committed' && s.event.receipt.currentState === 'current') sources.push({ id: s.memoryId, sequence: s.sequence, digest: s.digest });
|
||||
}
|
||||
if (sources.length > 500) throw bridgeError('PROJECT_MANIFEST_INCOMPLETE', '可选桥清单超过500条,本次省略远端记忆;本地全文保持。');
|
||||
// checkedAt belongs to this complete scan, never to a cached snapshot.
|
||||
return { projectId, projectFingerprint: view.sourceFingerprint, checkedAt: view.inspectedAt, complete: true, sources };
|
||||
}
|
||||
async context(session, query) {
|
||||
if (!this.store.storage.meta(key)) return null;
|
||||
return this.locked(session.projectId, async () => {
|
||||
const state = this.state(), p = this.project(state, session.projectId);
|
||||
if (!p.enabled) return null;
|
||||
const trace = [], base = { schemaVersion: 1, kind: 'mnemo-authorized-project-memory', projectId: session.projectId, producerId: state.producerId,
|
||||
bindingId: p.bindingId, connectionRevision: p.revision, selectionLimit: p.selectionLimit ?? 50, checkedAt: stamp(), entries: [] };
|
||||
try {
|
||||
const client = this.client(state, session.projectId, trace); await client.capabilities();
|
||||
const manifest = await this.fresh(state, session.projectId, trace);
|
||||
const recall = await client.recall(query, manifest);
|
||||
return { ...base, manifest, snapshot: recall.snapshot, recallContextDigest: recall.snapshot.contextDigest, entries: recall.entries, trace };
|
||||
} catch (cause) { return { ...base, issue: unavailable(cause), trace }; }
|
||||
}).catch(cause => ({ schemaVersion: 1, kind: 'mnemo-authorized-project-memory', projectId: session.projectId, entries: [], issue: unavailable(cause), trace: [] }));
|
||||
}
|
||||
artifact(session, run, value, kind) {
|
||||
const artifact = this.store.artifactStore.write(session, run, value, { kind }); this.store.core.artifact.set(artifact.id, artifact); this.store.save(session); return artifact;
|
||||
}
|
||||
async beforePhysical(session, run, plan, attemptId) {
|
||||
if (!plan.mnemoMemoryArtifactId) return;
|
||||
return this.locked(session.projectId, async () => {
|
||||
const artifact = this.store.owned('artifact', plan.mnemoMemoryArtifactId, session.id), document = JSON.parse(this.store.artifactStore.read(artifact));
|
||||
const trace = []; let manifest = null, validation = null, issue = null;
|
||||
try {
|
||||
const state = this.state(), p = this.project(state, session.projectId);
|
||||
if (!p.enabled || p.revision !== document.connectionRevision || p.bindingId !== document.bindingId || state.producerId !== document.producerId) throw bridgeError('PROJECT_CONNECTION_CHANGED', '本次连接或授权已变化。');
|
||||
const recallArtifact = this.store.owned('artifact', plan.mnemoRecallArtifactId, session.id);
|
||||
const recall = JSON.parse(this.store.artifactStore.read(recallArtifact));
|
||||
checkRecall({ protocol: MNEMO_PROTOCOL, entries: recall.entries, snapshot: recall.snapshot }, p.bindingId, recall.manifest);
|
||||
if (contextDigest(document.entries) !== plan.mnemoSelectedContextDigest || digest(document.entries.map(entryRef)) !== digest(plan.mnemoSelectedRefs)) throw bridgeError('PROJECT_RESPONSE_INVALID', '选入的记忆正文与封存摘要不符。');
|
||||
manifest = await this.fresh(state, session.projectId, trace);
|
||||
if (manifestDigest(manifest) !== document.snapshot.manifestDigest) throw bridgeError('PROJECT_MANIFEST_CHANGED', '本地来源在召回后已变化。');
|
||||
validation = await this.client(state, session.projectId, trace).validate(document.snapshot, manifest);
|
||||
} catch (cause) { issue = unavailable(cause); }
|
||||
const proof = this.artifact(session, run, { schemaVersion: 1, kind: 'mnemo-physical-validation', projectId: session.projectId, sessionId: session.id,
|
||||
taskId: run.taskId, runId: run.id, contextPlanId: plan.id, wireAttemptId: attemptId, checkedAt: stamp(), manifest, validation, issue,
|
||||
recallContextDigest: document.recallContextDigest, selectedContextDigest: plan.mnemoSelectedContextDigest, selectedRefs: plan.mnemoSelectedRefs, trace,
|
||||
executionAuthorized: false, atomicDispatchGuaranteed: false }, 'mnemo-physical-validation');
|
||||
(plan.mnemoDispatches ??= []).push({ wireAttemptId: attemptId, validationArtifactId: proof.id, validationSha256: proof.sha256,
|
||||
status: issue ? 'not_dispatched' : 'validated_not_submitted', issue }); this.store.save(session);
|
||||
if (issue) throw bridgeError('PROJECT_SELECTION_STALE', '远端记忆发送前核对失效;重新规划原任务并省略远端记忆。');
|
||||
});
|
||||
}
|
||||
submitted(session, plan, submission) {
|
||||
const dispatch = plan.mnemoDispatches?.find(value => value.wireAttemptId === submission.attemptId);
|
||||
if (dispatch) { dispatch.status = 'submitted_unconfirmed'; dispatch.wireRequestSha256 = submission.wireRequestSha256; this.store.save(session); }
|
||||
}
|
||||
observePhysical(session, plan, consumption) {
|
||||
const dispatch = plan.mnemoDispatches?.find(value => value.wireAttemptId === (consumption.wireAttemptId ?? consumption.attemptId));
|
||||
if (dispatch) { dispatch.status = consumption.outcome === 'completed' ? 'response_received' : consumption.outcome === 'unknown' ? 'unknown' : 'failed';
|
||||
dispatch.modelAttemptId = consumption.attemptId; dispatch.outcome = consumption.outcome; this.store.save(session); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
export const MNEMO_PROTOCOL = 'mnemo.project-memory.v1';
|
||||
class ProjectBridgeError extends Error {
|
||||
constructor(code, message, status) { super(message); this.code = code; this.status = status; }
|
||||
}
|
||||
export const bridgeError = (code, message, status = 409) => new ProjectBridgeError(code, message, status);
|
||||
export const digest = value => createHash('sha256').update(JSON.stringify(value), 'utf8').digest('hex');
|
||||
const uuid = value => typeof value === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
|
||||
const id = value => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9:_.-]{0,99}$/.test(value);
|
||||
const sha = value => typeof value === 'string' && /^[a-f0-9]{64}$/.test(value);
|
||||
const time = value => typeof value === 'string' && /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?Z$/.test(value) && Number.isFinite(Date.parse(value));
|
||||
const positive = value => Number.isSafeInteger(value) && value > 0;
|
||||
const states = ['current', 'candidate', 'unverified', 'expired', 'archived', 'withdrawn', 'deleted'];
|
||||
function assert(ok, message = '项目记忆响应的结构、来源或摘要不符。') { if (!ok) throw bridgeError('PROJECT_RESPONSE_INVALID', message); }
|
||||
export function strict(value, keys, required = keys) {
|
||||
assert(value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).every(key => keys.includes(key)) && required.every(key => Object.hasOwn(value, key)));
|
||||
return value;
|
||||
}
|
||||
export function sourcePayload(source, memory) {
|
||||
return { source: { id: source.id, sequence: source.sequence, version: source.version, state: source.state,
|
||||
fingerprint: source.fingerprint ?? null, expiresAt: source.expiresAt ?? null,
|
||||
confirmation: source.confirmation ? { by: source.confirmation.by, at: source.confirmation.at } : null },
|
||||
memory: memory ? { title: memory.title, content: memory.content, kind: memory.kind } : null };
|
||||
}
|
||||
export const sourceDigest = (source, memory) => digest(sourcePayload(source, memory));
|
||||
export const eventDigest = input => digest({ protocol: input.protocol, bindingId: input.bindingId, requestKey: input.requestKey, ...sourcePayload(input.source, input.memory) });
|
||||
export const manifestDigest = manifest => digest({ projectId: manifest.projectId, projectFingerprint: manifest.projectFingerprint, complete: manifest.complete,
|
||||
sources: [...manifest.sources].sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0).map(s => ({ id: s.id, sequence: s.sequence, digest: s.digest })) });
|
||||
export const contextDigest = entries => digest(entries.map(e => ({ origin: e.origin, bindingId: e.bindingId, projectId: e.projectId, memoryId: e.memoryId,
|
||||
revision: e.revision, source: sourcePayload(e.source, null).source, sourceDigest: e.sourceDigest, title: e.title, content: e.content, kind: e.kind, visibility: e.visibility })));
|
||||
export const entryRef = e => ({ sourceId: e.source.id, sequence: e.source.sequence, sourceDigest: e.sourceDigest, memoryId: e.memoryId, revision: e.revision });
|
||||
export function checkSource(s) {
|
||||
strict(s, ['id', 'sequence', 'version', 'state', 'fingerprint', 'expiresAt', 'confirmation']);
|
||||
assert(id(s.id) && positive(s.sequence) && id(s.version) && states.includes(s.state) && (s.fingerprint === null || sha(s.fingerprint)) && (s.expiresAt === null || time(s.expiresAt)));
|
||||
if (s.confirmation !== null) { strict(s.confirmation, ['by', 'at']); assert(s.confirmation.by === 'user' && time(s.confirmation.at)); }
|
||||
assert(s.state === 'current' ? s.fingerprint && s.confirmation : s.confirmation === null);
|
||||
}
|
||||
export function checkMemory(m) {
|
||||
strict(m, ['title', 'content', 'kind']);
|
||||
assert(typeof m.title === 'string' && m.title.length >= 1 && m.title.length <= 100 && typeof m.content === 'string' && m.content.length >= 1 && m.content.length <= 12000
|
||||
&& ['decision', 'preference', 'fact', 'experience'].includes(m.kind), '材料超出可选记忆桥格式;本地原文保持。');
|
||||
}
|
||||
export function checkSync(input) {
|
||||
strict(input, ['protocol', 'bindingId', 'requestKey', 'source', 'memory']);
|
||||
assert(input.protocol === MNEMO_PROTOCOL && uuid(input.bindingId) && uuid(input.requestKey)); checkSource(input.source);
|
||||
assert(['current', 'candidate'].includes(input.source.state) === (input.memory !== null)); if (input.memory) checkMemory(input.memory);
|
||||
assert(Buffer.byteLength(JSON.stringify(input)) <= 128 * 1024, '材料超过可选记忆桥128KB请求上限;本地原文保持。'); return input;
|
||||
}
|
||||
export function checkManifest(m, now = Date.now()) {
|
||||
strict(m, ['projectId', 'projectFingerprint', 'checkedAt', 'complete', 'sources']);
|
||||
assert(id(m.projectId) && sha(m.projectFingerprint) && time(m.checkedAt) && m.complete === true && Array.isArray(m.sources) && m.sources.length <= 500
|
||||
&& now - Date.parse(m.checkedAt) <= 30000 && Date.parse(m.checkedAt) - now <= 5000, '本次来源清单不完整或已到期,不能使用远端记忆。');
|
||||
for (const s of m.sources) { strict(s, ['id', 'sequence', 'digest']); assert(id(s.id) && positive(s.sequence) && sha(s.digest)); }
|
||||
assert(new Set(m.sources.map(s => s.id)).size === m.sources.length); return m;
|
||||
}
|
||||
function checkSnapshot(s) {
|
||||
strict(s, ['id', 'bindingId', 'grantId', 'manifestDigest', 'contextDigest', 'issuedAt', 'expiresAt', 'refs']);
|
||||
assert(uuid(s.id) && uuid(s.bindingId) && uuid(s.grantId) && sha(s.manifestDigest) && sha(s.contextDigest) && time(s.issuedAt) && time(s.expiresAt) && Array.isArray(s.refs) && s.refs.length <= 50);
|
||||
for (const r of s.refs) { strict(r, ['sourceId', 'sequence', 'sourceDigest', 'memoryId', 'revision']); assert(id(r.sourceId) && positive(r.sequence) && sha(r.sourceDigest) && uuid(r.memoryId) && positive(r.revision)); }
|
||||
}
|
||||
export function checkRecall(result, bindingId, manifest) {
|
||||
strict(result, ['protocol', 'entries', 'snapshot']); const { entries, snapshot } = result; checkManifest(manifest); checkSnapshot(snapshot);
|
||||
assert(result.protocol === MNEMO_PROTOCOL && Array.isArray(entries) && entries.length <= 50 && snapshot.bindingId === bindingId
|
||||
&& snapshot.manifestDigest === manifestDigest(manifest) && Date.parse(snapshot.expiresAt) > Date.now()
|
||||
&& Date.parse(snapshot.issuedAt) <= Date.now() + 5000 && Date.parse(snapshot.expiresAt) <= Date.parse(manifest.checkedAt) + 30000);
|
||||
for (const e of entries) {
|
||||
strict(e, ['origin', 'bindingId', 'projectId', 'memoryId', 'revision', 'source', 'sourceDigest', 'title', 'content', 'kind', 'visibility']);
|
||||
checkSource(e.source); checkMemory({ title: e.title, content: e.content, kind: e.kind });
|
||||
assert(e.origin === 'authorized_project_mirror' && e.bindingId === bindingId && e.projectId === manifest.projectId && uuid(e.memoryId) && positive(e.revision)
|
||||
&& e.visibility === 'private' && e.source.state === 'current' && sourceDigest(e.source, e) === e.sourceDigest
|
||||
&& manifest.sources.some(s => s.id === e.source.id && s.sequence === e.source.sequence && s.digest === e.sourceDigest)
|
||||
&& (!e.source.expiresAt || Date.parse(snapshot.expiresAt) <= Date.parse(e.source.expiresAt)));
|
||||
}
|
||||
assert(new Set(entries.map(e => e.source.id)).size === entries.length && contextDigest(entries) === snapshot.contextDigest && digest(entries.map(entryRef)) === digest(snapshot.refs));
|
||||
return result;
|
||||
}
|
||||
function checkReceipt(r, input, expected = null) {
|
||||
strict(r, ['protocol', 'bindingId', 'requestKey', 'receiptId', 'sourceId', 'sequence', 'sourceDigest', 'eventDigest', 'state', 'memoryId', 'revision', 'committedAt', 'replayed', 'currentState', 'requiresFreshManifest']);
|
||||
assert(r.protocol === MNEMO_PROTOCOL && r.bindingId === input.bindingId && r.requestKey === input.requestKey && uuid(r.receiptId) && r.sourceId === input.source.id
|
||||
&& r.sequence === input.source.sequence && r.sourceDigest === (expected?.sourceDigest ?? sourceDigest(input.source, input.memory)) && r.eventDigest === (expected?.eventDigest ?? eventDigest(input)) && r.state === input.source.state
|
||||
&& (r.memoryId === null || uuid(r.memoryId)) && (r.revision === null || positive(r.revision)) && time(r.committedAt) && typeof r.replayed === 'boolean'
|
||||
&& [...states, 'locally_deleted'].includes(r.currentState) && r.requiresFreshManifest === true);
|
||||
assert(r.state !== 'current' || r.memoryId && r.revision); return r;
|
||||
}
|
||||
export function bridgeOrigin(baseUrl) {
|
||||
let u; try { u = new URL(baseUrl); } catch { throw bridgeError('PROJECT_ENDPOINT_INVALID', '请填写HTTPS或本机HTTP根地址。', 400); }
|
||||
const local = ['127.0.0.1', 'localhost', '[::1]'].includes(u.hostname);
|
||||
if (!(u.protocol === 'https:' || local && u.protocol === 'http:') || u.username || u.password || u.search || u.hash || u.pathname !== '/') throw bridgeError('PROJECT_ENDPOINT_INVALID', '请填写无凭据、路径和查询的HTTPS或本机HTTP根地址。', 400);
|
||||
return u.origin;
|
||||
}
|
||||
|
||||
/** Bridge-specific protocol bounds never change model input, history or budgets. */
|
||||
export class MnemoClient {
|
||||
constructor({ enabled = false, baseUrl, bindingId, token, producerId, projectId, timeoutMs = 10000, fetch: transport = globalThis.fetch, observe = () => {} }) {
|
||||
this.origin = bridgeOrigin(baseUrl); assert(uuid(bindingId) && uuid(producerId) && id(projectId));
|
||||
Object.assign(this, { enabled, bindingId, token, producerId, projectId, timeoutMs, transport, observe });
|
||||
}
|
||||
async request(route, body) {
|
||||
if (!this.enabled || !this.token) throw bridgeError('PROJECT_BRIDGE_DISABLED', '桥默认关闭;需要显式启用并填写本次项目凭据。');
|
||||
const request = body === undefined ? null : structuredClone(body), startedAt = new Date().toISOString();
|
||||
let response, raw = '', result;
|
||||
try {
|
||||
response = await this.transport(this.origin + '/api/service/project-memory/v1/' + route, { method: body === undefined ? 'GET' : 'POST', redirect: 'error',
|
||||
headers: { Authorization: 'Bearer ' + this.token, ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) },
|
||||
...(body === undefined ? {} : { body: JSON.stringify(body) }), signal: AbortSignal.timeout(this.timeoutMs) });
|
||||
if (response.status >= 300 && response.status < 400) throw new Error('Redirect refused');
|
||||
const chunks = []; let bytes = 0;
|
||||
for await (const chunk of response.body ?? []) { bytes += chunk.length; if (bytes > 2 * 1024 * 1024) throw new Error('Bridge response too large'); chunks.push(chunk); }
|
||||
raw = Buffer.concat(chunks).toString('utf8');
|
||||
try { result = JSON.parse(raw); } catch { throw bridgeError('PROJECT_RESPONSE_INVALID', '桥返回不完整JSON,未使用远端记忆。'); }
|
||||
if (!response.ok) throw bridgeError(typeof result?.code === 'string' ? result.code : 'PROJECT_HTTP_ERROR', '项目记忆服务拒绝本次操作(HTTP ' + response.status + ')。', response.status);
|
||||
return result;
|
||||
} catch (cause) {
|
||||
// Upstream codes use their own namespace. A received HTTP rejection is
|
||||
// distinct from a transport failure, regardless of the code's spelling.
|
||||
throw cause instanceof ProjectBridgeError ? cause : bridgeError('PROJECT_TRANSPORT_UNAVAILABLE', '项目记忆暂不可用;不使用旧远端缓存。', 0);
|
||||
} finally {
|
||||
this.observe({ route, startedAt, finishedAt: new Date().toISOString(), request, httpStatus: response?.status ?? null, responseText: raw.split(this.token).join('[PROJECT_TOKEN]'),
|
||||
response: result === undefined ? null : JSON.parse(JSON.stringify(result).split(this.token).join('[PROJECT_TOKEN]')) });
|
||||
}
|
||||
}
|
||||
async capabilities() {
|
||||
const r = await this.request('capabilities?bindingId=' + encodeURIComponent(this.bindingId));
|
||||
strict(r, ['protocol', 'bindingId', 'producerId', 'projectId', 'grantId', 'available', 'manifestLifetimeMs', 'executionAuthorized', 'privateOnly']);
|
||||
assert(r.protocol === MNEMO_PROTOCOL && r.bindingId === this.bindingId && r.producerId === this.producerId && r.projectId === this.projectId && uuid(r.grantId)
|
||||
&& r.available === true && r.manifestLifetimeMs === 30000 && r.executionAuthorized === false && r.privateOnly === true); return r;
|
||||
}
|
||||
async sync(input) {
|
||||
checkSync(input);
|
||||
try { return checkReceipt(await this.request('sync', input), input); }
|
||||
catch (cause) {
|
||||
if (cause.status >= 400 && cause.status < 500 && cause.code !== 'PROJECT_RESPONSE_INVALID') throw cause;
|
||||
throw bridgeError('PROJECT_SYNC_UNKNOWN', '同步结果未知;保留原包原键,只查询原回执。');
|
||||
}
|
||||
}
|
||||
async lookup(input, expected = null) {
|
||||
const r = await this.request('lookup', { protocol: MNEMO_PROTOCOL, bindingId: this.bindingId, requestKey: input.requestKey });
|
||||
strict(r, ['found', 'receipt']); assert(typeof r.found === 'boolean');
|
||||
if (r.found) checkReceipt(r.receipt, input, expected); else assert(r.receipt === null); return r;
|
||||
}
|
||||
async recall(query, manifest, limit = 20) {
|
||||
checkManifest(manifest); assert(typeof query === 'string' && query.trim().length > 0 && query.trim().length <= 4000 && Number.isInteger(limit) && limit >= 1 && limit <= 50);
|
||||
return checkRecall(await this.request('recall', { protocol: MNEMO_PROTOCOL, bindingId: this.bindingId, query: query.trim(), manifest, limit }), this.bindingId, manifest);
|
||||
}
|
||||
async validate(snapshot, manifest) {
|
||||
checkManifest(manifest); checkSnapshot(snapshot);
|
||||
assert(snapshot.bindingId === this.bindingId && snapshot.manifestDigest === manifestDigest(manifest) && Date.parse(snapshot.expiresAt) > Date.now());
|
||||
const r = await this.request('validate', { protocol: MNEMO_PROTOCOL, bindingId: this.bindingId, snapshot, manifest });
|
||||
strict(r, ['protocol', 'valid', 'bindingId', 'snapshotId', 'contextDigest', 'executionAuthorized']);
|
||||
assert(r.protocol === MNEMO_PROTOCOL && r.valid === true && r.bindingId === this.bindingId && r.snapshotId === snapshot.id && r.contextDigest === snapshot.contextDigest && r.executionAuthorized === false); return r;
|
||||
}
|
||||
}
|
||||
@@ -507,6 +507,10 @@ export class ModelHub {
|
||||
}
|
||||
return result;
|
||||
} catch (cause) {
|
||||
if (cause.code === 'PROJECT_SELECTION_STALE') {
|
||||
attempt.status = 'rejected'; attempt.usageStatus = 'not_dispatched'; attempt.reason = cause.message;
|
||||
throw cause;
|
||||
}
|
||||
const diagnostic = cause.diagnostic;
|
||||
if (diagnostic) attempt.diagnostic = diagnostic;
|
||||
attempt.status = signal?.aborted ? 'interrupted' : diagnostic?.outcome === 'unknown' ? 'unknown' : 'failed';
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { REQUEST_LEDGER_SQL } from '../storage/request-ledger-schema.mjs';
|
||||
|
||||
const metadataKey = 'modelAttemptLedger';
|
||||
const sha = value => createHash('sha256').update(value).digest('hex');
|
||||
@@ -47,15 +48,7 @@ export class ModelRequestLedger {
|
||||
this.state = { schemaVersion: 1, id: randomUUID(), legacyEntries: entries.length, legacyMetadataSha256: original ? sha(original) : null,
|
||||
originalMetadataPreserved: true, migratedAt: new Date().toISOString() };
|
||||
storage.transaction(() => {
|
||||
this.database.exec(`CREATE TABLE IF NOT EXISTS model_attempts (
|
||||
seq INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT NOT NULL, connection_id TEXT, session_id TEXT, task_id TEXT, run_id TEXT,
|
||||
status TEXT, model TEXT, protocol TEXT, legacy_position INTEGER UNIQUE,
|
||||
payload TEXT NOT NULL CHECK(json_valid(payload)), payload_sha256 TEXT NOT NULL,
|
||||
initial_payload TEXT NOT NULL CHECK(json_valid(initial_payload)), initial_sha256 TEXT NOT NULL);
|
||||
CREATE INDEX IF NOT EXISTS model_attempts_id ON model_attempts(id);
|
||||
CREATE INDEX IF NOT EXISTS model_attempts_connection ON model_attempts(connection_id,seq);
|
||||
CREATE INDEX IF NOT EXISTS model_attempts_session ON model_attempts(session_id,task_id,run_id,seq);
|
||||
CREATE INDEX IF NOT EXISTS model_attempts_status ON model_attempts(status,seq);`);
|
||||
this.database.exec(REQUEST_LEDGER_SQL);
|
||||
for (const [index, entry] of entries.entries()) this.insert(entry, JSON.stringify(entry), index);
|
||||
storage.setMeta(metadataKey, this.state);
|
||||
this.database.exec('PRAGMA user_version=2');
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
|
||||
export const REQUEST_LEDGER_SQL = `CREATE TABLE IF NOT EXISTS model_attempts (
|
||||
seq INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT NOT NULL, connection_id TEXT, session_id TEXT, task_id TEXT, run_id TEXT,
|
||||
status TEXT, model TEXT, protocol TEXT, legacy_position INTEGER UNIQUE,
|
||||
payload TEXT NOT NULL CHECK(json_valid(payload)), payload_sha256 TEXT NOT NULL,
|
||||
initial_payload TEXT NOT NULL CHECK(json_valid(initial_payload)), initial_sha256 TEXT NOT NULL);
|
||||
CREATE INDEX IF NOT EXISTS model_attempts_id ON model_attempts(id);
|
||||
CREATE INDEX IF NOT EXISTS model_attempts_connection ON model_attempts(connection_id,seq);
|
||||
CREATE INDEX IF NOT EXISTS model_attempts_session ON model_attempts(session_id,task_id,run_id,seq);
|
||||
CREATE INDEX IF NOT EXISTS model_attempts_status ON model_attempts(status,seq);`;
|
||||
|
||||
export const normalizedSchemaSql = sql => sql === null ? null : sql.replace(/\s+/g, ' ').trim();
|
||||
export function databaseSchemaObjects(database) {
|
||||
return database.prepare("SELECT type,name,tbl_name,sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%' ORDER BY name").all()
|
||||
.map(row => ({ type: row.type, name: row.name, table: row.tbl_name, sql: normalizedSchemaSql(row.sql) }));
|
||||
}
|
||||
const reference = new DatabaseSync(':memory:');
|
||||
let objects;
|
||||
try { reference.exec(REQUEST_LEDGER_SQL); objects = databaseSchemaObjects(reference); }
|
||||
finally { reference.close(); }
|
||||
export const REQUEST_LEDGER_OBJECTS = Object.freeze(objects.map(row => Object.freeze(row)));
|
||||
export const REQUEST_LEDGER_MIGRATION = Object.freeze({
|
||||
id: 'model-attempt-ledger-v1', from: 1, to: 2,
|
||||
definitionSha256: createHash('sha256').update(JSON.stringify({ objects, marker: 'modelAttemptLedger',
|
||||
legacyProjection: 'array-order/seq=index+1/legacy_position=index/current=initial=JSON.stringify(entry)/sha256-utf8/v1' })).digest('hex'),
|
||||
});
|
||||
|
||||
// These are the exact columns written by the original migration, including
|
||||
// search projections. Hashing only payloads would miss altered ownership.
|
||||
export function legacyLedgerRow(entry, index) {
|
||||
const payload = JSON.stringify(entry), sha = createHash('sha256').update(payload).digest('hex');
|
||||
return { seq: index + 1, id: entry.id, connection_id: entry.connectionId ?? null, session_id: entry.sessionId ?? null,
|
||||
task_id: entry.taskId ?? null, run_id: entry.runId ?? null, status: entry.status ?? null, model: entry.model ?? null,
|
||||
protocol: entry.protocol ?? null, legacy_position: index, payload, payload_sha256: sha, initial_payload: payload, initial_sha256: sha };
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { InstallationManager } from '../core/desktop/install-manager.mjs';
|
||||
const args = process.argv.slice(2), command = args.shift(), options = {};
|
||||
if (!['preflight', 'activate', 'uninstall', 'cancel'].includes(command)) throw new Error('Unsupported installation operation.');
|
||||
if (!['preflight', 'activate', 'rollback', 'uninstall', 'cancel'].includes(command)) throw new Error('Unsupported installation operation.');
|
||||
while (args.length) {
|
||||
const flag = args.shift();
|
||||
if (flag === '--stage-only' && !options.stageOnly) { options.stageOnly = true; continue; }
|
||||
@@ -8,12 +8,13 @@ while (args.length) {
|
||||
options[flag] = args.shift();
|
||||
}
|
||||
try {
|
||||
if (command === 'rollback' && options.stageOnly) throw new Error('Rollback selects a verified recovery payload; stage-only is not a rollback operation.');
|
||||
const manager = new InstallationManager({ installRoot: options['--install-root'], dataRoot: options['--data-root'], candidateRoot: options['--candidate-root'], port: Number(options['--port'] || 4318), operationId: options['--operation-id'] ?? null, expectedManifestSha256: options['--expected-manifest-sha256'] ?? null,
|
||||
onProgress: value => {
|
||||
const counts = value.counts;
|
||||
console.log('AWBPROGRESS ' + (value.label + (counts ? ` · ${counts.completedFiles}/${counts.totalFiles} 文件` + (counts.completedBytes !== null ? ` · ${counts.completedBytes}/${counts.totalBytes} 字节` : '') + (counts.currentPath ? ' · ' + counts.currentPath : '') : '')).replace(/[\r\n]/g, ' '));
|
||||
console.log('AWBFILES ' + (counts ? counts.completedFiles + '/' + counts.totalFiles : '-'));
|
||||
} });
|
||||
const result = command === 'preflight' ? await manager.preflight({ stageOnly: !!options.stageOnly }) : command === 'activate' ? await manager.activate({ stageOnly: !!options.stageOnly }) : command === 'cancel' ? await manager.cancelPreparation() : await manager.prepareUninstall();
|
||||
const result = command === 'preflight' ? await manager.preflight({ stageOnly: !!options.stageOnly }) : command === 'activate' ? await manager.activate({ stageOnly: !!options.stageOnly }) : command === 'rollback' ? await manager.rollback({ recoveryRoot: options['--candidate-root'] ?? null }) : command === 'cancel' ? await manager.cancelPreparation() : await manager.prepareUninstall();
|
||||
console.log(JSON.stringify(result));
|
||||
} catch (error) { console.error('AWBERROR ' + error.message); console.error(JSON.stringify({ success: false, code: error.code || 'INSTALLATION_FAILED', message: error.message })); process.exitCode = 1; }
|
||||
|
||||
@@ -165,6 +165,18 @@ return async (request, response) => {
|
||||
if (method === 'POST') { if (workerForProject(store, projectId)) throw workerConflict('受限子项目不能修改MCP登记,请返回父项目。'); const input = await body(request);
|
||||
return json(response, mcpMatch[2] ? registry.configure(projectId, input) : registry.register(projectId, input), mcpMatch[2] ? 200 : 201); }
|
||||
}
|
||||
const mnemoBridgeMatch = route.match(/^\/api\/projects\/([a-f0-9-]+)\/memory-bridge(?:\/(configure|sources|lookup)(?:\/([a-f0-9-]+))?)?$/i);
|
||||
if (mnemoBridgeMatch) {
|
||||
const projectId = mnemoBridgeMatch[1], bridge = runtime.mnemoBridge();
|
||||
if (method === 'GET' && !mnemoBridgeMatch[2]) return json(response, await bridge.view(projectId));
|
||||
if (method === 'POST') {
|
||||
if (workerForProject(store, projectId)) throw workerConflict('请从来源项目管理可选记忆桥。');
|
||||
const input = await body(request);
|
||||
if (mnemoBridgeMatch[2] === 'configure' && !mnemoBridgeMatch[3]) return json(response, await bridge.configure(projectId, input));
|
||||
if (mnemoBridgeMatch[2] === 'lookup') { if (typeof input.memoryId !== 'string' || Object.keys(input).length !== 1) throw new Error('需要原来源ID。'); return json(response, await bridge.lookupSource(projectId, input.memoryId)); }
|
||||
if (mnemoBridgeMatch[2] === 'sources' && mnemoBridgeMatch[3]) return json(response, await bridge.changeSource(projectId, mnemoBridgeMatch[3], input));
|
||||
}
|
||||
}
|
||||
const projectMemoryMatch = route.match(/^\/api\/projects\/([a-f0-9-]+)\/memories$/i);
|
||||
if (projectMemoryMatch && method === 'GET') {
|
||||
const includeExpired = url.searchParams.get('include_expired') !== '0';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
|
||||
export const APP_VERSION = '1.25.12';
|
||||
export const APP_VERSION = '1.25.13';
|
||||
|
||||
export function executionEnvironment() {
|
||||
const windows = process.platform === 'win32';
|
||||
|
||||
+73
-33
@@ -13,6 +13,18 @@ import { hostAuthority } from '../core/desktop/host-authority.mjs';
|
||||
import { CredentialVault } from '../core/models/credential-vault.mjs';
|
||||
import { NetworkDiagnostics } from '../core/models/network-diagnostics.mjs';
|
||||
import { StateDatabase } from '../core/storage/state-database.mjs';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { ownedChild } from '../core/desktop/file-state.mjs';
|
||||
|
||||
// Only the installing bundle owns this synchronous undo closure. Its ledger
|
||||
// guard refuses any new/changed outcome before restoring the previous objects.
|
||||
export function rollbackUnchangedUpgrade(context, { appDirectory } = {}) {
|
||||
const adoption = context.server?.workbenchLedgerAdoption;
|
||||
if (!adoption || adoption.root !== fs.realpathSync.native(path.resolve(appDirectory))
|
||||
|| adoption.store !== context.store || adoption.provider !== context.provider || adoption.runtime !== context.runtime) throw new Error('No source-bound unchanged ledger adoption is available for rollback.');
|
||||
adoption.restore();
|
||||
return { restored: true, newRequestsDispatched: false };
|
||||
}
|
||||
|
||||
// Keep the existing objects and their in-memory connection. No credential is
|
||||
// extracted, transferred, returned, or persisted by this operation.
|
||||
@@ -30,7 +42,11 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
|
||||
const root = fs.realpathSync.native(path.resolve(appDirectory));
|
||||
if (!fs.existsSync(path.join(root, 'dist-client', 'index.html'))) throw new Error('The new client bundle is missing.');
|
||||
const oldHandlers = server.listeners('request');
|
||||
const serviceInfoFile = ownedChild(store.directory, 'service-info.json', { missing: true });
|
||||
const originalServiceInfo = fs.existsSync(serviceInfoFile) ? fs.readFileSync(serviceInfoFile) : null;
|
||||
let publishedServiceInfo = null;
|
||||
const oldCompanion = server.workbenchCompanion;
|
||||
const oldLedgerAdoption = server.workbenchLedgerAdoption;
|
||||
const oldAuthority = server.workbenchHostAuthority, oldAuthorityProperties = oldAuthority ? { ...oldAuthority } : null;
|
||||
const oldAuthorityPrototype = oldAuthority ? Object.getPrototypeOf(oldAuthority) : null;
|
||||
const oldPrototypes = [Object.getPrototypeOf(store), Object.getPrototypeOf(provider), Object.getPrototypeOf(runtime)];
|
||||
@@ -41,9 +57,10 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
|
||||
network: provider.hub?.networkChecks ? Object.getPrototypeOf(provider.hub.networkChecks) : null,
|
||||
catalog: provider.hub?.catalog ? Object.getPrototypeOf(provider.hub.catalog) : null };
|
||||
const oldStoreProperties = { ...store };
|
||||
const oldHubProperties = provider.hub ? { ...provider.hub } : null, oldStoragePrototype = store.storage ? Object.getPrototypeOf(store.storage) : null;
|
||||
const oldHubProperties = provider.hub ? { ...provider.hub } : null, oldHubDescriptors = provider.hub ? Object.getOwnPropertyDescriptors(provider.hub) : null;
|
||||
const oldStoragePrototype = store.storage ? Object.getPrototypeOf(store.storage) : null;
|
||||
let initializedLedger = null;
|
||||
const oldRuntimeProperties = { filePreviews: runtime.filePreviews, browsing: runtime.browsing, previews: runtime.previews, evaluations:runtime.evaluations, environments: runtime.environments, appDirectory: runtime.appDirectory, workers: runtime.workers, controlledEngines: runtime.controlledEngines, checks: runtime.checks, deliveries: runtime.deliveries, codeIndex: runtime.codeIndex, changeSets: runtime.changeSets, contextPlans: runtime.contextPlans, workspaces: runtime.workspaces, research: runtime.research, engines: runtime.engines, browser: runtime.browser, taskPlans: runtime.taskPlans, projectMemories: runtime.projectMemories, taskCheckpoints: runtime.taskCheckpoints, skills: runtime.skills, mcp: runtime.mcp };
|
||||
const oldRuntimeProperties = { results: runtime.results, filePreviews: runtime.filePreviews, browsing: runtime.browsing, previews: runtime.previews, evaluations:runtime.evaluations, environments: runtime.environments, appDirectory: runtime.appDirectory, workers: runtime.workers, controlledEngines: runtime.controlledEngines, checks: runtime.checks, deliveries: runtime.deliveries, codeIndex: runtime.codeIndex, changeSets: runtime.changeSets, contextPlans: runtime.contextPlans, workspaces: runtime.workspaces, research: runtime.research, engines: runtime.engines, browser: runtime.browser, taskPlans: runtime.taskPlans, projectMemories: runtime.projectMemories, taskCheckpoints: runtime.taskCheckpoints, skills: runtime.skills, mcp: runtime.mcp };
|
||||
const oldUpdates = store.listeners('update');
|
||||
const oldInputs = runtime.inputs;
|
||||
const oldResumes = runtime.resumes;
|
||||
@@ -51,6 +68,55 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
|
||||
if (oldUpdates.length > 1) throw new Error('Unexpected Store observers; preserve their ownership before upgrading.');
|
||||
const handler = createRequestHandler({ appDirectory: root, server, store, provider, runtime, token, clients, started,
|
||||
exampleProject, shutdown, getPort: () => server.address().port });
|
||||
const restoreOriginal = () => {
|
||||
const readServiceInfo = () => fs.existsSync(serviceInfoFile) ? fs.readFileSync(ownedChild(store.directory, 'service-info.json')) : null;
|
||||
const expectedServiceInfo = publishedServiceInfo ?? originalServiceInfo;
|
||||
const currentServiceInfo = readServiceInfo();
|
||||
if (expectedServiceInfo ? !currentServiceInfo?.equals(expectedServiceInfo) : currentServiceInfo !== null) throw new Error('The host index changed outside this adoption; preserve its current bytes before rollback.');
|
||||
initializedLedger?.rollbackInitialization();
|
||||
if (oldCompanion) server.workbenchCompanion = oldCompanion; else delete server.workbenchCompanion;
|
||||
if (oldLedgerAdoption) server.workbenchLedgerAdoption = oldLedgerAdoption; else delete server.workbenchLedgerAdoption;
|
||||
if (oldAuthority) {
|
||||
for (const key of Object.keys(oldAuthority)) if (!Object.hasOwn(oldAuthorityProperties, key)) delete oldAuthority[key];
|
||||
Object.assign(oldAuthority, oldAuthorityProperties); Object.setPrototypeOf(oldAuthority, oldAuthorityPrototype);
|
||||
server.workbenchHostAuthority = oldAuthority;
|
||||
} else delete server.workbenchHostAuthority;
|
||||
server.removeListener('close', closeStorage);
|
||||
if (store.storage && store.storage !== oldStoreProperties.storage) {
|
||||
const filename = store.storage.filename;
|
||||
store.close();
|
||||
if (path.resolve(filename) !== path.join(path.resolve(oldStoreProperties.directory), 'workbench.sqlite')) throw new Error('Upgrade rollback database path is invalid.');
|
||||
for (const suffix of ['', '-wal', '-shm']) if (fs.existsSync(filename + suffix)) fs.renameSync(filename + suffix, filename + '.rolledback-' + Date.now() + suffix);
|
||||
}
|
||||
for (const key of Object.keys(store)) if (!Object.hasOwn(oldStoreProperties, key)) delete store[key];
|
||||
Object.assign(store, oldStoreProperties);
|
||||
Object.assign(runtime, oldRuntimeProperties);
|
||||
if (oldInputs === undefined) delete runtime.inputs; else runtime.inputs = oldInputs;
|
||||
if (oldResumes === undefined) delete runtime.resumes; else runtime.resumes = oldResumes;
|
||||
Object.setPrototypeOf(store, oldPrototypes[0]); Object.setPrototypeOf(provider, oldPrototypes[1]); Object.setPrototypeOf(runtime, oldPrototypes[2]);
|
||||
if (oldStoragePrototype && store.storage === oldStoreProperties.storage) Object.setPrototypeOf(store.storage, oldStoragePrototype);
|
||||
if (oldHubProperties) { for (const key of Object.keys(provider.hub)) if (!Object.hasOwn(oldHubProperties, key)) delete provider.hub[key]; Object.defineProperties(provider.hub, oldHubDescriptors); }
|
||||
if (oldModelPrototypes.hub) Object.setPrototypeOf(provider.hub, oldModelPrototypes.hub);
|
||||
if (oldModelPrototypes.ledger) Object.setPrototypeOf(provider.hub.ledger, oldModelPrototypes.ledger);
|
||||
if (oldModelPrototypes.importer) Object.setPrototypeOf(provider.hub.importer, oldModelPrototypes.importer);
|
||||
if (oldModelPrototypes.vault) Object.setPrototypeOf(provider.hub.credentialVault, oldModelPrototypes.vault);
|
||||
if (oldModelPrototypes.network) Object.setPrototypeOf(provider.hub.networkChecks, oldModelPrototypes.network);
|
||||
if (oldModelPrototypes.catalog) Object.setPrototypeOf(provider.hub.catalog, oldModelPrototypes.catalog);
|
||||
store.removeAllListeners('update'); for (const observer of oldUpdates) store.on('update', observer);
|
||||
server.removeAllListeners('request'); for (const previous of oldHandlers) server.on('request', previous);
|
||||
// Restoring an already-bound authority makes refreshInstallation a no-op.
|
||||
// Restore its public index explicitly, after proving our published bytes
|
||||
// are still current. Never re-create registration secrets or credentials.
|
||||
if (publishedServiceInfo && !publishedServiceInfo.equals(originalServiceInfo ?? Buffer.alloc(0))) {
|
||||
const latest = readServiceInfo();
|
||||
if (!latest?.equals(publishedServiceInfo)) throw new Error('The published host index changed while restoring the original handler.');
|
||||
if (originalServiceInfo) {
|
||||
const temporary = ownedChild(store.directory, 'service-info.rollback-' + randomUUID() + '.json', { missing: true });
|
||||
fs.writeFileSync(temporary, originalServiceInfo, { flag: 'wx', mode: 0o600 });
|
||||
try { fs.renameSync(temporary, serviceInfoFile); } finally { if (fs.existsSync(temporary)) fs.unlinkSync(temporary); }
|
||||
} else fs.unlinkSync(serviceInfoFile);
|
||||
}
|
||||
};
|
||||
try {
|
||||
Object.setPrototypeOf(store, Store.prototype);
|
||||
Object.setPrototypeOf(provider, Provider.prototype);
|
||||
@@ -80,41 +146,15 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
|
||||
server.prependOnceListener('close', closeStorage);
|
||||
if (updateServiceInfo) {
|
||||
const authority = hostAuthority({ appDirectory: root, server, store, provider, runtime });
|
||||
authority.initialize(); authority.refreshInstallation();
|
||||
authority.initialize(); publishedServiceInfo = fs.readFileSync(serviceInfoFile);
|
||||
authority.refreshInstallation(); publishedServiceInfo = fs.readFileSync(serviceInfoFile);
|
||||
}
|
||||
if (initializedLedger) server.workbenchLedgerAdoption = { root, store, provider, runtime, restore: restoreOriginal };
|
||||
else delete server.workbenchLedgerAdoption;
|
||||
void oldRuntimeProperties.filePreviews?.stopAll();
|
||||
return { version: APP_VERSION, pid: process.pid, hasKey: provider.hasKey(), projects: store.projects.length, sessions: store.sessions.size, appDirectory: root };
|
||||
} catch (error) {
|
||||
initializedLedger?.rollbackInitialization();
|
||||
if (oldCompanion) server.workbenchCompanion = oldCompanion; else delete server.workbenchCompanion;
|
||||
if (oldAuthority) {
|
||||
for (const key of Object.keys(oldAuthority)) if (!Object.hasOwn(oldAuthorityProperties, key)) delete oldAuthority[key];
|
||||
Object.assign(oldAuthority, oldAuthorityProperties); Object.setPrototypeOf(oldAuthority, oldAuthorityPrototype);
|
||||
server.workbenchHostAuthority = oldAuthority;
|
||||
} else delete server.workbenchHostAuthority;
|
||||
server.removeListener('close', closeStorage);
|
||||
if (store.storage && store.storage !== oldStoreProperties.storage) {
|
||||
const filename = store.storage.filename;
|
||||
store.close();
|
||||
if (path.resolve(filename) !== path.join(path.resolve(oldStoreProperties.directory), 'workbench.sqlite')) throw new Error('Upgrade rollback database path is invalid.');
|
||||
for (const suffix of ['', '-wal', '-shm']) if (fs.existsSync(filename + suffix)) fs.renameSync(filename + suffix, filename + '.rolledback-' + Date.now() + suffix);
|
||||
}
|
||||
for (const key of Object.keys(store)) if (!Object.hasOwn(oldStoreProperties, key)) delete store[key];
|
||||
Object.assign(store, oldStoreProperties);
|
||||
Object.assign(runtime, oldRuntimeProperties);
|
||||
if (oldInputs === undefined) delete runtime.inputs; else runtime.inputs = oldInputs;
|
||||
if (oldResumes === undefined) delete runtime.resumes; else runtime.resumes = oldResumes;
|
||||
Object.setPrototypeOf(store, oldPrototypes[0]); Object.setPrototypeOf(provider, oldPrototypes[1]); Object.setPrototypeOf(runtime, oldPrototypes[2]);
|
||||
if (oldStoragePrototype && store.storage === oldStoreProperties.storage) Object.setPrototypeOf(store.storage, oldStoragePrototype);
|
||||
if (oldHubProperties) { for (const key of Object.keys(provider.hub)) if (!Object.hasOwn(oldHubProperties, key)) delete provider.hub[key]; Object.assign(provider.hub, oldHubProperties); }
|
||||
if (oldModelPrototypes.hub) Object.setPrototypeOf(provider.hub, oldModelPrototypes.hub);
|
||||
if (oldModelPrototypes.ledger) Object.setPrototypeOf(provider.hub.ledger, oldModelPrototypes.ledger);
|
||||
if (oldModelPrototypes.importer) Object.setPrototypeOf(provider.hub.importer, oldModelPrototypes.importer);
|
||||
if (oldModelPrototypes.vault) Object.setPrototypeOf(provider.hub.credentialVault, oldModelPrototypes.vault);
|
||||
if (oldModelPrototypes.network) Object.setPrototypeOf(provider.hub.networkChecks, oldModelPrototypes.network);
|
||||
if (oldModelPrototypes.catalog) Object.setPrototypeOf(provider.hub.catalog, oldModelPrototypes.catalog);
|
||||
store.removeAllListeners('update'); for (const observer of oldUpdates) store.on('update', observer);
|
||||
server.removeAllListeners('request'); for (const previous of oldHandlers) server.on('request', previous);
|
||||
restoreOriginal();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,6 +228,11 @@ export class Provider {
|
||||
return responseDetails;
|
||||
};
|
||||
try {
|
||||
if (body && options.beforePhysicalDispatch) {
|
||||
await options.beforePhysicalDispatch({ attemptId: receipt.attemptId, body });
|
||||
signal?.throwIfAborted();
|
||||
options.beforeDispatch?.({ previousFailure });
|
||||
}
|
||||
dispatched = true;
|
||||
const transport=this.fetcher?null:(this.transport??=new ModelTransport());
|
||||
if(transport)onProgress?.({phase:'running',transport:transport.profile,attempt,maxAttempts:policy.attempts,wireAttemptId:receipt.attemptId});
|
||||
@@ -315,6 +320,9 @@ export class Provider {
|
||||
onProgress?.({ phase: 'completed', attempt, maxAttempts: policy.attempts, durationMs: Date.now() - startedAt, modelIdentity: receipt.modelIdentity });
|
||||
return result;
|
||||
} catch (error) {
|
||||
// No HTTP has been submitted. The runtime can prepare the original
|
||||
// task without its now-invalid optional remote memory, once.
|
||||
if (!dispatched && error.code === 'PROJECT_SELECTION_STALE') throw error;
|
||||
outcome = signal?.aborted ? 'interrupted' : readingStream && !streamTerminalSeen ? 'unknown' : 'failed';
|
||||
signal?.throwIfAborted();
|
||||
let diagnostic = connectionFailure(error, { ...context, method:body?'POST':'GET',responseReceived:receipt.httpStatus!==undefined,
|
||||
|
||||
+27
-6
@@ -59,6 +59,7 @@ import { currentBrowserVerification } from '../core/browser/browser-manager.mjs'
|
||||
import { taskPlanInput, taskProofInput, PLAN_READ_TOOLS } from '../shared/contracts/task-plans.mjs';
|
||||
import { taskPlanToolDefinitions } from '../shared/contracts/task-plan-tools.mjs';
|
||||
import { ProjectMemoryManager } from '../core/memory/project-memory.mjs';
|
||||
import { MnemoBridge } from '../core/memory/mnemo-bridge.mjs';
|
||||
import { TaskCheckpointManager } from '../core/tasks/task-checkpoint-manager.mjs';
|
||||
import { SkillRegistry, skillToolDefinitions, skillReadInput } from '../core/skills/skill-registry.mjs';
|
||||
import { McpRegistry } from '../core/tools/mcp-registry.mjs';
|
||||
@@ -236,6 +237,7 @@ export class Runtime {
|
||||
resumeBudget(id,input){const session=this.store.session(id),plan=this.budgetResumes().verify(session,input);return this.start(id,'',true,{resumePlan:plan});}
|
||||
engineRunner() { return this.controlledEngines ??= new EngineRunner(this.store, this.provider, { appDirectory: this.appDirectory }); }
|
||||
memoryManager() { return this.projectMemories ??= new ProjectMemoryManager(this.store, { redact: value => this.provider.redact(value) }); }
|
||||
mnemoBridge() { return this.mnemoMemories ??= new MnemoBridge(this.store, { memories: this.memoryManager() }); }
|
||||
checkpointManager() { return this.taskCheckpoints ??= new TaskCheckpointManager(this.store, {
|
||||
taskPlans: this.taskPlanner(), redact: value => this.provider.redact(value) }); }
|
||||
skillRegistry() { return this.skills ??= new SkillRegistry(this.store, {
|
||||
@@ -495,14 +497,17 @@ export class Runtime {
|
||||
purpose: run.record.purpose ?? 'execution', workerId: run.worker?.id ?? null },
|
||||
reason: name => run.worker && !unrestricted(config) && !WORKER_TOOLS.has(name) ? 'disabled_by_worker_contract'
|
||||
: !taskToolAllowed(task, run.record, name) ? 'disabled_by_task_contract' : 'disabled_by_execution_mode' });
|
||||
const projectMemory = await this.memoryManager().context(session, { redact: value => this.provider.redact(value) });
|
||||
let projectMemory = await this.memoryManager().context(session, { redact: value => this.provider.redact(value) });
|
||||
const lastInput = providerHistory(session, run.record).findLast(message => message.role === 'user')?.content;
|
||||
let mnemoMemory = config.mode === 'compatible' && !run.worker?.engine
|
||||
? await this.mnemoBridge().context(session, (typeof lastInput === 'string' ? lastInput : JSON.stringify(lastInput ?? '当前项目')).slice(0, 4000) || '当前项目') : null;
|
||||
signal.throwIfAborted();
|
||||
let reminder = config.mode === 'compatible' && !run.worker && (config.capacityMode === 'passthrough' || config.historyPolicy === 'preserve')
|
||||
? this.reminderManager().prepare(session, run.record, toolSurface) : null;
|
||||
const prepare = selected => this.requestPlanner().prepare(session, run.record, permissionConfig, {
|
||||
system: stablePrefix ? this.provider.redact(fixedContext) : context + (selected ? '\n' + selected.message.content : ''),
|
||||
contextTail: stablePrefix ? context + (selected ? '\n' + selected.message.content : '') : null,
|
||||
progressReminder: selected?.ref ?? null, tools, toolSurface, stepId: stepRecord.id, projectMemory, skillCatalog, mcpCatalog,
|
||||
progressReminder: selected?.ref ?? null, tools, toolSurface, stepId: stepRecord.id, projectMemory, mnemoMemory, skillCatalog, mcpCatalog,
|
||||
transportReservationTokens: run.worker?.engine ? ENGINE_SEED_RESERVATION : 0, engineStepOnly: !!run.worker?.engine });
|
||||
let planned;
|
||||
try { planned = prepare(reminder); }
|
||||
@@ -530,6 +535,7 @@ export class Runtime {
|
||||
requestStarted = true;
|
||||
const progressHandler = progress => {
|
||||
if (progress.consumption) {
|
||||
this.mnemoMemories?.observePhysical(session, planned.record, progress.consumption);
|
||||
finalAttemptUsage = progress.consumption.usage ?? null;
|
||||
this.store.recordConsumption(session, run.record, { ...progress.consumption, requestId: planned.record.id }, planned.record.tokenReservation?.reservedTokens??planned.record.estimatedInputTokens + config.maxOutputTokens);
|
||||
responseAccounted = true;
|
||||
@@ -574,12 +580,27 @@ export class Runtime {
|
||||
this.store.save(session);
|
||||
};
|
||||
const requestConfig=config.modelRouting&&!externalEngine?{...config,modelRouting:{...config.modelRouting,maxAttempts:Math.min(config.modelRouting.maxAttempts,planned.record.maxBudgetAttempts)}}:config;
|
||||
const dispatchModel = () => this.provider.complete(requestConfig, planned.messages, run.project, signal, progressHandler, planned.record, tools,
|
||||
{ ...(config.mode === 'demo' ? { demoHistory: providerHistory(session, run.record) } : {}),
|
||||
onSubmitted: submission => {
|
||||
try { this.mnemoMemories?.submitted(session, planned.record, submission); }
|
||||
finally { if (reminder) this.reminderManager().dispatch(session, run.record, planned.record, submission); }
|
||||
},
|
||||
...(planned.record.mnemoMemoryArtifactId ? { beforePhysicalDispatch: ({ attemptId }) => this.mnemoBridge().beforePhysical(session, run.record, planned.record, attemptId) } : {}),
|
||||
maxBudgetAttempts: planned.record.maxBudgetAttempts, beforeDispatch: ({ previousFailure } = {}) => checkModelDispatchBudget(this.store, session, run.record, requestConfig, planned.body, 0, previousFailure) });
|
||||
response = externalEngine
|
||||
? await this.engineRunner().complete(session, run, planned.messages, progressHandler, planned.record, tools)
|
||||
: await this.provider.complete(requestConfig, planned.messages, run.project, signal, progressHandler, planned.record, tools,
|
||||
{ ...(config.mode === 'demo' ? { demoHistory: providerHistory(session, run.record) } : {}),
|
||||
...(reminder ? { onSubmitted: submission => this.reminderManager().dispatch(session, run.record, planned.record, submission) } : {}),
|
||||
maxBudgetAttempts:planned.record.maxBudgetAttempts,beforeDispatch:({previousFailure}={})=>checkModelDispatchBudget(this.store,session,run.record,requestConfig,planned.body,0,previousFailure)});
|
||||
: await dispatchModel().catch(async cause => {
|
||||
if (cause.code !== 'PROJECT_SELECTION_STALE' || !planned.record.mnemoMemoryArtifactId) throw cause;
|
||||
const previousPlan = planned.record;
|
||||
this.requestPlanner().finish(session, previousPlan, { error: cause, attempts: previousPlan.requestAttempts });
|
||||
mnemoMemory = { schemaVersion: 1, kind: 'mnemo-authorized-project-memory', projectId: session.projectId, entries: [], omittedPlanId: previousPlan.id,
|
||||
issue: { code: cause.code, message: cause.message } };
|
||||
if (requestEvent) { requestEvent.phase = 'interrupted'; requestEvent.finishedAt = now(); requestEvent.output = cause.message; this.store.save(session); requestEvent = null; }
|
||||
projectMemory = await this.memoryManager().context(session, { redact: value => this.provider.redact(value) });
|
||||
planned = prepare(reminder);
|
||||
return dispatchModel();
|
||||
});
|
||||
if (response.compatibility?.length && requestEvent) { requestEvent.compatibility = response.compatibility; this.store.save(session); }
|
||||
this.requestPlanner().finish(session, planned.record, { response, attempts: planned.record.requestAttempts });
|
||||
if (run.worker && !externalEngine && !responseAccounted) {
|
||||
|
||||
Reference in New Issue
Block a user