diff --git a/apps/stock/web-app/src/features/research/ResearchProjectPage.tsx b/apps/stock/web-app/src/features/research/ResearchProjectPage.tsx
index 04931006..bd2618b7 100644
--- a/apps/stock/web-app/src/features/research/ResearchProjectPage.tsx
+++ b/apps/stock/web-app/src/features/research/ResearchProjectPage.tsx
@@ -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] && (
@@ -333,7 +371,7 @@ function ProjectWorkspace({ projectId }: { projectId: string }) {
)}{' '}
@@ -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}
diff --git a/apps/stock/web-app/src/features/research/components/ResearchEntry.tsx b/apps/stock/web-app/src/features/research/components/ResearchEntry.tsx
index df1f39bf..692e3644 100644
--- a/apps/stock/web-app/src/features/research/components/ResearchEntry.tsx
+++ b/apps/stock/web-app/src/features/research/components/ResearchEntry.tsx
@@ -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({
)}
{!readonly && !editing && (
-
+
Edit latest revision
)}
{!archived && !writeBlocked && (
- void archive()} disabled={saving}>
+ void archive()}
+ disabled={saving || archiveUncertain}
+ >
{entry.entity.archived ? 'Restore entry' : 'Archive entry'}
)}
@@ -464,7 +495,33 @@ export function ResearchEntry({
{error}
)}
- {editing && !readonly && (
+ {archiveUncertain && (
+
+
+ The archive request may have saved. Your draft is preserved. Reload saved history
+ before another write; this request will not be retried.
+
+
{
+ if (confirmDiscard(editing)) {
+ setLoading(true);
+ setEditing(false);
+ setArchiveUncertain(false);
+ setBlocked(false);
+ setError('');
+ setReload(value => value + 1);
+ onRefresh();
+ }
+ }}
+ >
+ Discard draft and reload archive state
+
+
+ )}
+ {editing && !archived && !entry.entity.archived && !revisionId && (