@modelprofile.com/flexharness 18.4.0 → 18.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ts/utils.json.ts CHANGED
@@ -771,7 +771,7 @@ function validateUsage(value: unknown, path: string): void {
771
771
  requireNonNegativeNumber(usage.cacheWriteTokens, `${path}.cacheWriteTokens`);
772
772
  }
773
773
 
774
- interface IValidatedMessageIdentity {
774
+ export interface IValidatedMessageIdentity {
775
775
  messageId: string;
776
776
  sessionId: string;
777
777
  runId: string;
@@ -792,7 +792,7 @@ interface IValidatedSessionIdentity {
792
792
  depth?: number;
793
793
  }
794
794
 
795
- function validateMessage(value: unknown, path: string): IValidatedMessageIdentity {
795
+ export function validateMessage(value: unknown, path: string): IValidatedMessageIdentity {
796
796
  const message = requireRecord(value, path);
797
797
  requireOnlyKeys(
798
798
  message,
@@ -1246,7 +1246,7 @@ interface IValidatedProjectionBody {
1246
1246
  stagedMessages: IValidatedMessageIdentity[];
1247
1247
  }
1248
1248
 
1249
- interface IValidatedStagedTerminal {
1249
+ export interface IValidatedStagedTerminal {
1250
1250
  runId: string;
1251
1251
  user?: IValidatedMessageIdentity;
1252
1252
  assistant: IValidatedMessageIdentity;
@@ -1264,7 +1264,7 @@ const stagedTerminalKeys = [
1264
1264
  ];
1265
1265
 
1266
1266
  /** One staged terminal by itself: its fields, and messages that belong to its run and status. */
1267
- function validateStagedTerminal(value: unknown, path: string): IValidatedStagedTerminal {
1267
+ export function validateStagedTerminal(value: unknown, path: string): IValidatedStagedTerminal {
1268
1268
  const terminal = requireRecord(value, path);
1269
1269
  requireOnlyKeys(terminal, stagedTerminalKeys, path);
1270
1270
  const runId = requireString(terminal.runId, `${path}.runId`);
@@ -1648,7 +1648,7 @@ function validateAffectedWorkspaces(value: unknown, path: string): void {
1648
1648
  }
1649
1649
  }
1650
1650
 
1651
- function assertSessionMetrics(value: unknown): void {
1651
+ export function assertSessionMetrics(value: unknown): void {
1652
1652
  const path = '$snapshot.metrics';
1653
1653
  const metrics = requireRecord(value, path);
1654
1654
  requireOnlyKeys(metrics, [
@@ -1740,7 +1740,7 @@ const reversionSegmentKeys = [
1740
1740
  * One schema-3 reversion segment by itself: its fields and the workspace ownership its
1741
1741
  * provenance, status and disposition allow.
1742
1742
  */
1743
- function validateReversionSegment(value: unknown, path: string): void {
1743
+ export function validateReversionSegment(value: unknown, path: string): void {
1744
1744
  const segment = requireRecord(value, path);
1745
1745
  requireOnlyKeys(segment, reversionSegmentKeys, path);
1746
1746
  requireString(segment.runId, `${path}.runId`);
@@ -1954,6 +1954,30 @@ interface IReversionSegmentFacts {
1954
1954
  capturingRunIds: string[];
1955
1955
  }
1956
1956
 
1957
+ /** Relates one segment to its visible and staged turn facts, independent of their storage layout. */
1958
+ export function assertProjectionSegmentTurnRelation(
1959
+ segment: Pick<IFlexReversionSegment, 'userMessageId'>,
1960
+ facts: {
1961
+ readonly visiblePrompt: boolean;
1962
+ readonly stagedPrompt: boolean;
1963
+ readonly assistant: boolean;
1964
+ readonly matchingVisibleUser: boolean;
1965
+ readonly matchingStagedUser: boolean;
1966
+ },
1967
+ path: string,
1968
+ ): void {
1969
+ if (segment.userMessageId === undefined) {
1970
+ if (facts.visiblePrompt || facts.stagedPrompt) {
1971
+ throw new FlexHarnessStoreFormatError(`${path} omits its projected prompt identity.`);
1972
+ }
1973
+ if (!facts.assistant) {
1974
+ throw new FlexHarnessStoreFormatError(`${path} does not match its projected turn messages.`);
1975
+ }
1976
+ } else if (!facts.matchingVisibleUser && !facts.matchingStagedUser) {
1977
+ throw new FlexHarnessStoreFormatError(`${path} does not match its projected turn messages.`);
1978
+ }
1979
+ }
1980
+
1957
1981
  /**
1958
1982
  * Validates the segments after the `kept` first in full, then reads every segment's identity
1959
1983
  * once: run, event and capture IDs are unique, and each segment matches its run's messages.
@@ -2000,35 +2024,44 @@ function relateReversionSegments(
2000
2024
  if (segment.status === 'capturing') facts.capturingRunIds.push(runId);
2001
2025
  }
2002
2026
  if (segment.status === 'completed') facts.completed.push(index);
2003
- if (userMessageId === undefined) {
2004
- // An assistant-only continuation: its run has no prompt and an assistant message.
2005
- if (messages.visiblePromptRunIds.has(runId) || messages.stagedPromptRunIds.has(runId)) {
2006
- throw new FlexHarnessStoreFormatError(
2007
- `$snapshot.reversionSegments[${index}] omits its projected prompt identity.`,
2008
- );
2009
- }
2010
- if (!messages.assistantRunIds.has(runId)) {
2011
- throw new FlexHarnessStoreFormatError(
2012
- `$snapshot.reversionSegments[${index}] does not match its projected turn messages.`,
2013
- );
2014
- }
2015
- } else {
2016
- const visible = messages.visible.get(userMessageId);
2017
- const staged = messages.staged.get(userMessageId);
2018
- if (
2019
- !(visible?.runId === runId && visible.role === 'user')
2020
- && !(staged?.runId === runId && staged.role === 'user')
2021
- ) {
2022
- throw new FlexHarnessStoreFormatError(
2023
- `$snapshot.reversionSegments[${index}] does not match its projected turn messages.`,
2024
- );
2025
- }
2026
- }
2027
+ const visible = userMessageId === undefined ? undefined : messages.visible.get(userMessageId);
2028
+ const staged = userMessageId === undefined ? undefined : messages.staged.get(userMessageId);
2029
+ assertProjectionSegmentTurnRelation(segment, {
2030
+ visiblePrompt: messages.visiblePromptRunIds.has(runId),
2031
+ stagedPrompt: messages.stagedPromptRunIds.has(runId),
2032
+ assistant: messages.assistantRunIds.has(runId),
2033
+ matchingVisibleUser: visible?.runId === runId && visible.role === 'user',
2034
+ matchingStagedUser: staged?.runId === runId && staged.role === 'user',
2035
+ }, `$snapshot.reversionSegments[${index}]`);
2027
2036
  }
2028
2037
  return facts;
2029
2038
  }
2030
2039
 
2031
2040
  /** Checks a pending apply against the segments: it moves the cursor by one unit, applied in order. */
2041
+ export function assertPendingApplyRunIds(pendingRuns: readonly string[], appliedRuns: readonly string[]): void {
2042
+ if (new Set(pendingRuns).size !== pendingRuns.length || new Set(appliedRuns).size !== appliedRuns.length) {
2043
+ throw new FlexHarnessStoreFormatError('Snapshot pending apply run IDs are invalid.');
2044
+ }
2045
+ if (appliedRuns.some((runId) => !pendingRuns.includes(runId))) {
2046
+ throw new FlexHarnessStoreFormatError('Snapshot pending apply progress is invalid.');
2047
+ }
2048
+ }
2049
+
2050
+ export function assertPendingApplyRunUnit(
2051
+ pendingRuns: readonly string[],
2052
+ appliedRuns: readonly string[],
2053
+ exactRuns: readonly string[],
2054
+ direction: 'undo' | 'redo',
2055
+ ): void {
2056
+ if (pendingRuns.length !== exactRuns.length || pendingRuns.some((runId, index) => runId !== exactRuns[index])) {
2057
+ throw new FlexHarnessStoreFormatError('Snapshot pending apply segment unit is invalid.');
2058
+ }
2059
+ const applyOrder = direction === 'undo' ? [...exactRuns].reverse() : exactRuns;
2060
+ if (appliedRuns.some((runId, index) => runId !== applyOrder[index])) {
2061
+ throw new FlexHarnessStoreFormatError('Snapshot pending apply progress is out of order.');
2062
+ }
2063
+ }
2064
+
2032
2065
  function validatePendingApply(
2033
2066
  pending: Record<string, unknown>,
2034
2067
  revertCursor: number,
@@ -2054,12 +2087,7 @@ function validatePendingApply(
2054
2087
  requireNonNegativeInteger(pending.toCursor, '$snapshot.pendingReversion.toCursor');
2055
2088
  const pendingRuns = requireStringArray(pending.segmentRunIds, '$snapshot.pendingReversion.segmentRunIds');
2056
2089
  const appliedRuns = requireStringArray(pending.appliedRunIds, '$snapshot.pendingReversion.appliedRunIds');
2057
- if (new Set(pendingRuns).size !== pendingRuns.length || new Set(appliedRuns).size !== appliedRuns.length) {
2058
- throw new FlexHarnessStoreFormatError('Snapshot pending apply run IDs are invalid.');
2059
- }
2060
- if (appliedRuns.some((runId) => !pendingRuns.includes(runId))) {
2061
- throw new FlexHarnessStoreFormatError('Snapshot pending apply progress is invalid.');
2062
- }
2090
+ assertPendingApplyRunIds(pendingRuns, appliedRuns);
2063
2091
  const fromCursor = Number(pending.fromCursor);
2064
2092
  const toCursor = Number(pending.toCursor);
2065
2093
  const direction = pending.direction as 'undo' | 'redo';
@@ -2081,13 +2109,7 @@ function validatePendingApply(
2081
2109
  const start = rangeStart === 0 ? 0 : first;
2082
2110
  const end = facts.completed[rangeEnd] ?? segments.length;
2083
2111
  const exactRuns = segments.slice(start, end).map((segment) => segment.runId);
2084
- if (pendingRuns.length !== exactRuns.length || pendingRuns.some((runId, index) => runId !== exactRuns[index])) {
2085
- throw new FlexHarnessStoreFormatError('Snapshot pending apply segment unit is invalid.');
2086
- }
2087
- const applyOrder = direction === 'undo' ? [...exactRuns].reverse() : exactRuns;
2088
- if (appliedRuns.some((runId, index) => runId !== applyOrder[index])) {
2089
- throw new FlexHarnessStoreFormatError('Snapshot pending apply progress is out of order.');
2090
- }
2112
+ assertPendingApplyRunUnit(pendingRuns, appliedRuns, exactRuns, direction);
2091
2113
  }
2092
2114
 
2093
2115
  /**