export type ReflectionOp = 'add' | 'update' | 'merge_into' | 'remove';
export type ReflectionMemoryType = 'user' | 'feedback' | 'project' | 'reference';

export interface MemoryChange {
  op: ReflectionOp;
  name: string;
  type: ReflectionMemoryType;
  description: string;
  body: string;
  merge_target?: string;
}

export interface PieceChanges {
  should_edit: boolean;
  target_piece?: string;
  diff_summary?: string;
  new_yaml?: string | null;
}

export interface ReflectionResult {
  memory_changes: MemoryChange[];
  piece_changes: PieceChanges;
  reasoning: string;
  abstain_reason?: string;
}

export interface ReflectionInput {
  originalJobId: string;
  userId: string;
  pieceName: string;
  pieceSource: 'builtin' | 'custom';
  outcome: 'succeeded' | 'failed' | 'aborted';
  taskTitle: string;
  taskBody: string;
  activityLogSummary: string;             // already compressed
  postCompletionComments: Array<{ author: string; body: string; createdAt: string }>;
  feedback: { rating: 'good' | 'bad' | null; comment: string | null; tags: string[] };
  resultText: string;                     // complete.result / abort_reason / missing_info
  observedRevisions: Record<string, string>; // entryName -> sha1(body) at prompt-build time
  memoryIndex: string;                    // MEMORY.md raw
  memoryEntries: Array<{ name: string; description: string; type: string; body: string }>;
  pieceYaml: string;                      // current piece (custom if forked, else builtin)
}

export type ReflectionRejectionCode =
  | 'rejected_unknown_type'
  | 'rejected_bad_name'
  | 'rejected_body_too_large'
  | 'rejected_missing_target'
  | 'rejected_stale_target'
  | 'rejected_name_collision'
  | 'rejected_target_piece_mismatch'
  | 'rejected_invalid_yaml'
  | 'rejected_invalid_piece'
  | 'rejected_dangerous_piece'
  | 'rejected_injected_directive';   // body reads as an injected agent-steering / exfiltration directive

export type ReflectionOutcome =
  | 'applied'                             // memory and/or piece changes applied
  | 'partial'                             // some changes applied, others rejected
  | 'abstained'                           // LLM said "nothing to learn"
  | 'rejected'                            // every change was rejected
  | 'failed';                             // LLM error, lock timeout, schema invalid
