type MemoryRecord {
  memoryRecordId: ID!
  content: MemoryContent
  createdAt: AWSDateTime
  updatedAt: AWSDateTime
  expiresAt: AWSDateTime
  namespace: String
  strategyId: String
  strategy: String
  score: Float
  userSlug: String
  agentSlug: String @deprecated(reason: "Use userSlug")
  factType: String
  confidence: Float
  eventDate: AWSDateTime
  occurredStart: AWSDateTime
  occurredEnd: AWSDateTime
  mentionedAt: AWSDateTime
  tags: [String!]
  accessCount: Int
  proofCount: Int
  context: String
  threadId: String
  """
  Compiled wiki pages (Compounding Memory) that cite this memory unit as
  a source. Populated from wiki_section_sources.source_ref. Returns pages
  scoped to the same user as this memory (there is no cross-user
  citation in v1). Returned pages have empty `sections`/`aliases` — fetch
  `wikiPage(tenantId, userId, type, slug)` for full detail.
  """
  wikiPages: [WikiPage!]!
}

type MemoryContent {
  text: String
}

enum MemoryStrategy {
  SEMANTIC
  PREFERENCES
  SUMMARIES
  EPISODES
  REFLECTIONS
}

type MemorySearchResult {
  records: [MemoryRecord!]!
  totalCount: Int!
}

type MemoryGraphNode {
  id: ID!
  label: String!
  type: String!
  strategy: String
  entityType: String
  edgeCount: Int!
  latestThreadId: String
}

type MemoryGraphEdge {
  source: String!
  target: String!
  type: String!
  label: String
  weight: Float!
}

type MemoryGraph {
  nodes: [MemoryGraphNode!]!
  edges: [MemoryGraphEdge!]!
}

"""
Runtime memory system configuration exposed to the admin UI.
Lets the UI decide which views to render (e.g. Knowledge Graph toggle is
only meaningful when Hindsight is deployed alongside managed memory).
"""
type MemorySystemConfig {
  """
  True when managed AgentCore Memory is provisioned and wired into the
  agent container. This is the always-on baseline — when false, memory
  features may be unavailable.
  """
  managedMemoryEnabled: Boolean!
  """
  True when the optional Hindsight add-on is deployed (ECS + ALB). Gates
  the Knowledge Graph / entity-graph views in the admin UI.
  """
  hindsightEnabled: Boolean!
}

type ThreadIdleLearningChangedFile {
  path: String!
  key: String
  beforeHash: String
  afterHash: String
  beforeBytes: Int
  afterBytes: Int
  snapshotKey: String
  hindsightDocumentId: String
  hindsightStatus: String
}

type ThreadIdleLearningRun {
  id: ID!
  tenantId: ID!
  threadId: ID!
  computerId: ID
  requesterUserId: ID
  scheduledJobId: ID
  activitySequence: Int!
  scheduledFor: AWSDateTime
  startedAt: AWSDateTime
  finishedAt: AWSDateTime
  status: String!
  changedFiles: [ThreadIdleLearningChangedFile!]!
  candidateSummary: AWSJSON
  reportS3Key: String
  reportMarkdown: String
  error: String
  budget: AWSJSON
  metadata: AWSJSON
  canRollback: Boolean!
  createdAt: AWSDateTime
  updatedAt: AWSDateTime
}

extend type Query {
  memoryRecords(
    tenantId: ID
    userId: ID
    assistantId: ID
    namespace: String!
  ): [MemoryRecord!]!
  memorySearch(
    tenantId: ID
    userId: ID
    assistantId: ID
    query: String!
    strategy: MemoryStrategy
    limit: Int
  ): MemorySearchResult!
  memoryGraph(tenantId: ID, userId: ID, assistantId: ID): MemoryGraph!
  memorySystemConfig: MemorySystemConfig!
  threadIdleLearningRuns(
    tenantId: ID
    userId: ID
    threadId: ID
    limit: Int = 25
  ): [ThreadIdleLearningRun!]!
  threadIdleLearningRun(
    tenantId: ID
    userId: ID
    runId: ID!
  ): ThreadIdleLearningRun
}

extend type Mutation {
  deleteMemoryRecord(
    tenantId: ID
    userId: ID
    assistantId: ID
    memoryRecordId: ID!
  ): Boolean!
  updateMemoryRecord(
    tenantId: ID
    userId: ID
    assistantId: ID
    memoryRecordId: ID!
    content: String!
  ): Boolean!
  rollbackThreadIdleLearningRun(
    tenantId: ID
    userId: ID
    runId: ID!
  ): ThreadIdleLearningRun!
  captureMobileMemory(
    tenantId: ID
    userId: ID
    agentId: ID
    content: String!
    factType: MobileCaptureFactType = FACT
    metadata: AWSJSON
    clientCaptureId: ID
  ): MobileMemoryCapture!
}

"""
Fact-type picker values exposed to the mobile quick-capture footer. Maps to
Hindsight's native fact_type via the resolver. FACT is the default when the
user doesn't override.
"""
enum MobileCaptureFactType {
  FACT
  PREFERENCE
  EXPERIENCE
  OBSERVATION
}

type MobileMemoryCapture {
  id: ID!
  tenantId: ID!
  userId: ID!
  agentId: ID @deprecated(reason: "Use userId")
  content: String!
  factType: MobileCaptureFactType!
  capturedAt: AWSDateTime!
  syncedAt: AWSDateTime
  metadata: AWSJSON
}

extend type Query {
  mobileMemoryCaptures(
    tenantId: ID
    userId: ID
    agentId: ID
    limit: Int = 50
  ): [MobileMemoryCapture!]!
  """
  Free-text search across the full Hindsight bank for the given user.
  Hits Hindsight's recall endpoint (semantic + rerank) and normalizes results
  back to MobileMemoryCapture so the Memories list can render search results
  with the same rows it uses for captures. Not filtered by capture_source —
  search is meant to answer "what does this user know?", including chat-
  derived observations.
  """
  mobileMemorySearch(
    tenantId: ID
    userId: ID
    agentId: ID
    query: String!
    limit: Int = 20
  ): [MobileMemoryCapture!]!

  """
  Ranked wiki-page search for mobile. Runs a Postgres full-text query
  (`plainto_tsquery('english', …)` + `ts_rank`) against the GIN-indexed
  `search_tsv` generated column on `wiki_pages` (title || summary ||
  body_md), scoped to one (tenant, user) pair. Returns results in
  `ts_rank` DESC order, tie-broken by `last_compiled_at` DESC.

  Previously routed through Hindsight semantic recall; on the compiled
  wiki corpus FTS is near-instant and matches the query shape mobile
  users actually type (page titles, keywords). `matchingMemoryIds` is
  retained for wire-format compatibility and is always [] on this path —
  pages match their own compiled text, not source memory units.
  """
  mobileWikiSearch(
    tenantId: ID
    userId: ID
    agentId: ID
    query: String!
    limit: Int = 20
  ): [MobileWikiSearchResult!]!

  """
  Newest compiled wiki pages for the given user, ordered by
  last_compiled_at DESC (falling back to updated_at when the page hasn't
  been recompiled yet). Intended as the default Memories-tab feed so
  the user sees fresh pages before they type a search query.
  """
  recentWikiPages(
    tenantId: ID
    userId: ID
    agentId: ID
    limit: Int = 50
  ): [WikiPage!]!
}

type MobileWikiSearchResult {
  page: WikiPage!
  """
  Postgres `ts_rank(search_tsv, plainto_tsquery('english', query))` on
  the page's compiled text. Higher is better. Not comparable across
  queries.
  """
  score: Float!
  """
  Retained for wire-format compatibility with older mobile clients.
  Always [] on the FTS path; pages match their own compiled text, not
  source memory units.
  """
  matchingMemoryIds: [ID!]!
}

extend type Mutation {
  deleteMobileMemoryCapture(
    tenantId: ID
    userId: ID
    agentId: ID
    captureId: ID!
  ): Boolean!
}
