fix(research): verify project updates and archive outcomes (RP32)

This commit is contained in:
Boki 2026-10-28 09:52:24 -04:00
parent 233178d4ad
commit f84fa82bba
5 changed files with 670 additions and 46 deletions

View file

@ -1,5 +1,6 @@
import { useCallback, useEffect, useRef, useState } from 'react';
import { Link, useParams, useSearchParams } from 'react-router-dom';
import { updateResearchProjectSchema } from '@stock-bot/stock-config/research';
import type {
ResearchArtifact,
ResearchHypothesis,
@ -157,7 +158,8 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
loading ||
pending.current ||
errors.project ||
(archived === undefined && (!editing || conflict))
conflict ||
(archived === undefined && !editing)
) {
return;
}
@ -177,7 +179,18 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
if (!body) {
return;
}
const { project: updated } = await researchApi.updateProject(projectId, body);
const input = updateResearchProjectSchema.parse(body);
const { project: updated } = await researchApi.updateProject(projectId, input);
if (
updated.id !== projectId ||
updated.ownerPrincipal !== project.ownerPrincipal ||
updated.revision !== input.expectedRevision + 1 ||
updated.title !== (input.title ?? project.title) ||
updated.description !== (input.description ?? project.description) ||
updated.archived !== (input.archived ?? project.archived)
) {
throw new ResearchApiError('The saved project update could not be verified.', null);
}
if (mounted.current) {
setProject(updated);
setEditing(null);
@ -213,7 +226,8 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
pending.current ||
editing ||
errors.project ||
errors.strategies
errors.strategies ||
conflict
) {
return;
}
@ -244,6 +258,20 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
}
}
};
const discardAndReloadProject = () => {
if (saving || !confirmDiscard(dirty)) {
return;
}
setLoading(true);
setEditing(null);
setCreator(null);
setCreateDirty(false);
setEntryDirty(false);
setSelectionEpoch(value => value + 1);
setConflict(false);
setMutationError('');
reload();
};
const hypothesisId = params.get('hypothesis');
const artifactId = params.get('artifact');
const selection =
@ -258,7 +286,17 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
const visibleArtifacts = artifacts.filter(
item => showArchived || project?.archived || !item.archived
);
const canWrite = project && !project.archived && !errors.project && !loading && !saving;
const canWrite =
project && !project.archived && !errors.project && !loading && !saving && !conflict;
const projectWriteBlocked = conflict
? 'Review the saved project before another update.'
: saving
? 'Updating project.'
: loading
? 'Refreshing project.'
: errors.project
? 'Project status could not be verified.'
: '';
const sectionError = (section: Section) =>
errors[section] && (
<div>
@ -333,7 +371,7 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
)}{' '}
<button
className={actionClass}
disabled={saving || loading || Boolean(errors.project)}
disabled={saving || loading || Boolean(errors.project) || conflict}
onClick={() => void mutateProject(!project.archived)}
>
{project.archived ? 'Restore project' : 'Archive project'}
@ -380,7 +418,9 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
className={actionClass}
disabled={saving}
onClick={() => {
if (confirmDiscard(true)) {
if (conflict) {
discardAndReloadProject();
} else if (confirmDiscard(true)) {
setEditing(null);
setMutationError('');
setConflict(false);
@ -401,18 +441,7 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
<p>
Review the saved project before another update. Your draft has been preserved.
</p>
<button
className={actionClass}
disabled={saving}
onClick={() => {
if (confirmDiscard(Boolean(editing))) {
setEditing(null);
setConflict(false);
setMutationError('');
reload();
}
}}
>
<button className={actionClass} disabled={saving} onClick={discardAndReloadProject}>
Discard edits and reload project
</button>
</div>
@ -583,15 +612,7 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
key={creator}
projectId={projectId}
kind={creator}
writeBlocked={
project.archived
? 'Archived project.'
: loading
? 'Refreshing project.'
: errors.project
? 'Project status could not be verified.'
: ''
}
writeBlocked={project.archived ? 'Archived project.' : projectWriteBlocked}
onDirty={setCreateDirty}
onClose={() => setCreator(null)}
onRefresh={reload}
@ -612,13 +633,7 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
kind={selection.kind}
revisionId={params.get('revision')}
archived={project.archived}
writeBlocked={
loading
? 'Refreshing project.'
: errors.project
? 'Project status could not be verified.'
: ''
}
writeBlocked={projectWriteBlocked}
hypotheses={hypotheses}
onDirty={setEntryDirty}
onRefresh={reload}

View file

@ -134,6 +134,7 @@ export function ResearchEntry({
const [saving, setSaving] = useState(false);
const [error, setError] = useState('');
const [blocked, setBlocked] = useState(false);
const [archiveUncertain, setArchiveUncertain] = useState(false);
const [downloadError, setDownloadError] = useState('');
useUnsavedWarning(editing);
useEffect(() => {
@ -241,7 +242,7 @@ export function ResearchEntry({
const readonly =
archived || Boolean(writeBlocked) || entry?.entity.archived || Boolean(revisionId);
const beginEdit = () => {
if (!revision || readonly) {
if (!revision || readonly || archiveUncertain) {
return;
}
setTitle(revision.title);
@ -260,6 +261,7 @@ export function ResearchEntry({
pending.current ||
readonly ||
blocked ||
archiveUncertain ||
!title.trim() ||
(kind === 'hypothesis' && !body.trim())
) {
@ -345,6 +347,7 @@ export function ResearchEntry({
archived ||
Boolean(writeBlocked) ||
pending.current ||
archiveUncertain ||
!confirmDiscard(editing) ||
!window.confirm(`${entry.entity.archived ? 'Restore' : 'Archive'} this research entry?`)
) {
@ -354,10 +357,29 @@ export function ResearchEntry({
setSaving(true);
setError('');
try {
if (kind === 'hypothesis') {
await researchApi.archiveHypothesis(id, !entry.entity.archived);
const targetArchived = !entry.entity.archived;
let saved: ResearchHypothesis | ResearchArtifact;
let matches: boolean;
if (entry.kind === 'hypothesis') {
({ hypothesis: saved } = await researchApi.archiveHypothesis(id, targetArchived));
matches = saved.latestRevision.hypothesisId === id;
} else {
await researchApi.archiveArtifact(id, !entry.entity.archived);
({ artifact: saved } = await researchApi.archiveArtifact(id, targetArchived));
matches =
saved.latestRevision.artifactId === id &&
saved.kind === entry.entity.kind &&
saved.hypothesisRevisionId === entry.entity.hypothesisRevisionId;
}
// Archive has no narrative revision precondition. A concurrent edit may be newer.
if (
!matches ||
saved.id !== id ||
saved.projectId !== projectId ||
saved.ownerPrincipal !== entry.entity.ownerPrincipal ||
saved.archived !== targetArchived ||
saved.latestRevision.validationState !== 'unvalidated'
) {
throw new ResearchApiError('The archive response could not be verified.', null);
}
if (mounted.current) {
setEditing(false);
@ -367,6 +389,7 @@ export function ResearchEntry({
} catch (cause) {
if (mounted.current) {
setError(cause instanceof Error ? cause.message : 'Could not update archive state');
setArchiveUncertain(!(cause instanceof ResearchApiError) || cause.ambiguous);
}
} finally {
pending.current = false;
@ -449,12 +472,20 @@ export function ResearchEntry({
)}
<div className="flex gap-2 flex-wrap">
{!readonly && !editing && (
<button className={actionClass} onClick={beginEdit} disabled={saving}>
<button
className={actionClass}
onClick={beginEdit}
disabled={saving || archiveUncertain}
>
Edit latest revision
</button>
)}
{!archived && !writeBlocked && (
<button className={actionClass} onClick={() => void archive()} disabled={saving}>
<button
className={actionClass}
onClick={() => void archive()}
disabled={saving || archiveUncertain}
>
{entry.entity.archived ? 'Restore entry' : 'Archive entry'}
</button>
)}
@ -464,7 +495,33 @@ export function ResearchEntry({
{error}
</p>
)}
{editing && !readonly && (
{archiveUncertain && (
<div role="status">
<p>
The archive request may have saved. Your draft is preserved. Reload saved history
before another write; this request will not be retried.
</p>
<button
className={actionClass}
type="button"
disabled={saving}
onClick={() => {
if (confirmDiscard(editing)) {
setLoading(true);
setEditing(false);
setArchiveUncertain(false);
setBlocked(false);
setError('');
setReload(value => value + 1);
onRefresh();
}
}}
>
Discard draft and reload archive state
</button>
</div>
)}
{editing && !archived && !entry.entity.archived && !revisionId && (
<form
aria-label="Revise research entry"
className="space-y-3"
@ -477,7 +534,7 @@ export function ResearchEntry({
className={fieldClass}
value={title}
onChange={event => setTitle(event.target.value)}
disabled={saving || blocked}
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
required
/>
</label>
@ -488,7 +545,7 @@ export function ResearchEntry({
className={fieldClass}
value={body}
onChange={event => setBody(event.target.value)}
disabled={saving || blocked}
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
required={kind === 'hypothesis'}
/>
</label>
@ -500,7 +557,7 @@ export function ResearchEntry({
className={fieldClass}
value={criteria}
onChange={event => setCriteria(event.target.value)}
disabled={saving || blocked}
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
/>
</label>
)}
@ -511,7 +568,7 @@ export function ResearchEntry({
className={fieldClass}
value={origin}
onChange={event => setOrigin(event.target.value as ResearchContentOrigin)}
disabled={saving || blocked}
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
>
<option value="human">Human</option>
<option value="ai">AI</option>
@ -545,7 +602,12 @@ export function ResearchEntry({
className={actionClass}
type="submit"
disabled={
saving || blocked || !title.trim() || (kind === 'hypothesis' && !body.trim())
saving ||
blocked ||
archiveUncertain ||
Boolean(writeBlocked) ||
!title.trim() ||
(kind === 'hypothesis' && !body.trim())
}
>
{saving ? 'Saving revision...' : 'Save new revision'}