microscope-memory 1.2.0

Pure binary cognitive memory engine with REST API bridge for LLM integration. Zero-JSON core, mmap-based, hierarchical, sub-microsecond search.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
openapi: "3.1.0"

info:
  title: Microscope Memory — Spine Bridge API
  description: |
    AI-native cognitive memory interface for ChatGPT, Claude, and other LLMs.

    Microscope Memory is a **sub-microsecond**, zoom-based hierarchical memory engine.
    This API lets AI models **store** and **recall** memories using natural language.

    ## How it works
    - Memories are stored in a 3D coordinate space using semantic hashing
    - Recall uses SIMD-accelerated radial search at the correct depth layer
    - Layers: identity, long_term, short_term, associative, emotional, relational,
      reflections, crypto_chain, echo_cache, rust_state

    ## Usage tips
    - Use `recall` before answering to check if relevant context exists
    - Use `remember` to commit important facts for future sessions
    - Set `layer` to `associative` for links between concepts
    - Set `layer` to `emotional` for emotional context
    - Higher `importance` (1–10) = prioritized in future recall

    ## Namespacing
    Most endpoints accept optional `user_id`, `memory_backend` (local|cloud), and
    `memory_scope` (personal|shared|both) parameters for multi-user isolation.
  version: "0.8.0"
  contact:
    name: Microscope Memory
    url: https://github.com/silentnoisehun/microscope-memory

servers:
  - url: http://localhost:6060
    description: Local Spine Bridge (default)
  - url: http://localhost:6060/v1
    description: Local Spine Bridge v1 prefix
  - url: https://your-deployment.railway.app
    description: Cloud deployment (configure your URL)

paths:
  /status:
    get:
      operationId: getMemoryStatus
      summary: Get memory engine status
      description: Returns block count, append log size, and available layers.
      responses:
        "200":
          description: Engine status
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/StatusResponse"
        "500":
          $ref: "#/components/responses/EngineError"

  /session:
    get:
      operationId: getSession
      summary: Resolve namespace paths for a user/backend/scope combination
      parameters:
        - $ref: "#/components/parameters/UserId"
        - $ref: "#/components/parameters/MemoryBackend"
        - $ref: "#/components/parameters/MemoryScope"
      responses:
        "200":
          description: Resolved namespace info
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SessionResponse"
        "500":
          $ref: "#/components/responses/EngineError"

  /recall:
    get:
      operationId: recallMemory
      summary: Recall relevant memories by natural language query
      description: |
        Searches the memory engine using semantic hashing + SIMD radial search.
        Use this before answering any question to check for relevant context.
      parameters:
        - name: q
          in: query
          required: true
          description: Natural language query
          schema:
            type: string
            minLength: 1
            maxLength: 500
        - name: k
          in: query
          required: false
          description: Max results (default 10, max 50)
          schema:
            type: integer
            minimum: 1
            maximum: 50
            default: 10
        - $ref: "#/components/parameters/UserId"
        - $ref: "#/components/parameters/MemoryBackend"
        - $ref: "#/components/parameters/MemoryScope"
      responses:
        "200":
          description: Relevant memory blocks ordered by distance
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/MemoryResult"
        "400":
          $ref: "#/components/responses/BadRequest"
        "500":
          $ref: "#/components/responses/EngineError"

  /remember:
    post:
      operationId: rememberMemory
      summary: Store a new memory
      description: |
        Commits a new memory to the append log. Immediately available for recall.
        Use `layer` to categorize and `importance` (1–10) to weight future recall.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/RememberRequest"
      responses:
        "200":
          description: Memory committed
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/RememberResponse"
        "400":
          $ref: "#/components/responses/BadRequest"
        "500":
          $ref: "#/components/responses/EngineError"

  /find:
    get:
      operationId: findMemory
      summary: Find memories by text substring / keyword
      description: |
        Searches memory blocks by text match (not semantic). Faster than recall for
        exact keyword lookups. Returns text, depth, and layer — no distance score.
      parameters:
        - name: q
          in: query
          required: true
          description: Keyword or substring to search
          schema:
            type: string
            minLength: 1
            maxLength: 500
        - name: k
          in: query
          required: false
          description: Max results (default 10)
          schema:
            type: integer
            minimum: 1
            default: 10
      responses:
        "200":
          description: Matching memory blocks
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/FindResult"
        "400":
          $ref: "#/components/responses/BadRequest"
        "500":
          $ref: "#/components/responses/EngineError"

  /look:
    get:
      operationId: lookMemory
      summary: Look at a specific 3D coordinate in memory space
      description: |
        Returns memory blocks closest to the given (x, y, z) coordinate at a specific
        zoom level (D0–D8). Useful for spatial exploration of the memory index.
      parameters:
        - name: x
          in: query
          required: true
          schema:
            type: number
            format: float
        - name: "y"
          in: query
          required: true
          schema:
            type: number
            format: float
        - name: z
          in: query
          required: true
          schema:
            type: number
            format: float
        - name: zoom
          in: query
          required: true
          description: Depth level 0–8
          schema:
            type: integer
            minimum: 0
            maximum: 8
        - name: k
          in: query
          required: false
          description: Max results (default 10)
          schema:
            type: integer
            minimum: 1
            default: 10
      responses:
        "200":
          description: Nearby memory blocks
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/LookResult"
        "400":
          $ref: "#/components/responses/BadRequest"
        "500":
          $ref: "#/components/responses/EngineError"

  /mql:
    get:
      operationId: mqlQuery
      summary: Query memory using MQL (Microscope Query Language)
      description: |
        Executes a structured MQL expression against the memory index and append log.
        Returns blocks with a relevance score.
      parameters:
        - name: mql
          in: query
          required: true
          description: MQL expression string
          schema:
            type: string
            minLength: 1
      responses:
        "200":
          description: Query results with scores
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/MqlResult"
        "400":
          $ref: "#/components/responses/BadRequest"
        "500":
          $ref: "#/components/responses/EngineError"

  /build:
    post:
      operationId: buildIndex
      summary: Rebuild the memory index from the append log
      description: |
        Merges the append log into the main binary index, clears the log, and
        returns updated block count. Use `force: true` to rebuild even if up-to-date.
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: object
              properties:
                force:
                  type: boolean
                  default: false
                  description: Force full rebuild even if index is current
      responses:
        "200":
          description: Build result
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: ok
                  blocks:
                    type: integer
                    example: 20323
                  depths:
                    type: integer
                    description: Number of non-empty depth levels
                    example: 9
        "500":
          $ref: "#/components/responses/EngineError"

  /session_log:
    get:
      operationId: getSessionLog
      summary: Get recent session layer entries
      description: Returns the last N entries from the session.txt layer file.
      parameters:
        - name: n
          in: query
          required: false
          description: Number of recent entries to return (default 50)
          schema:
            type: integer
            minimum: 1
            default: 50
      responses:
        "200":
          description: Recent session entries
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
        "500":
          $ref: "#/components/responses/EngineError"

  /consolidate:
    post:
      operationId: consolidateSessions
      summary: Consolidate session entries into long-term memory
      description: |
        Groups session.txt entries by session ID, summarizes each group, and
        stores summaries as long_term memories (importance 8).
      responses:
        "200":
          description: Consolidation result
          content:
            application/json:
              schema:
                type: object
                properties:
                  consolidated:
                    type: integer
                    description: Number of session groups consolidated
                  groups:
                    type: array
                    items:
                      type: string
                    description: Summary strings that were stored
        "500":
          $ref: "#/components/responses/EngineError"

  /dream:
    post:
      operationId: dreamConsolidation
      summary: Run a dream consolidation cycle
      description: |
        Triggers the offline consolidation process: replays fingerprints, strengthens
        co-activated pairs, prunes weak associations, and consolidates patterns.
        Analogous to sleep-based memory consolidation.
      responses:
        "200":
          description: Dream cycle result
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DreamResult"
        "500":
          $ref: "#/components/responses/EngineError"

  /mobile/recall:
    post:
      operationId: mobileRecall
      summary: Recall memories (mobile POST variant)
      description: POST-based recall for mobile clients where GET with body is inconvenient.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [user_id, query]
              properties:
                user_id:
                  type: string
                query:
                  type: string
                k:
                  type: integer
                  default: 10
      responses:
        "200":
          description: Relevant memory blocks
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/MemoryResult"
        "500":
          $ref: "#/components/responses/EngineError"

  /mobile/remember:
    post:
      operationId: mobileRemember
      summary: Store a memory (mobile variant, user-scoped)
      description: Stores a memory prefixed with the user_id scope tag.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [user_id, text]
              properties:
                user_id:
                  type: string
                text:
                  type: string
                layer:
                  type: string
                  default: long_term
                importance:
                  type: integer
                  minimum: 1
                  maximum: 10
                  default: 7
      responses:
        "200":
          description: Memory committed
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: committed
                  message:
                    type: string
        "500":
          $ref: "#/components/responses/EngineError"

  /mobile/chat:
    post:
      operationId: mobileChat
      summary: Provider-agnostic chat with automatic memory recall + store
      description: |
        Recalls relevant memories, sends the message + context to an LLM provider
        (Ollama, OpenAI, or Gemini), then optionally stores both user message and
        assistant reply back into memory.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/MobileChatRequest"
      responses:
        "200":
          description: Chat response with recalled memories and stored items
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/MobileChatResponse"
        "400":
          $ref: "#/components/responses/BadRequest"
        "502":
          description: LLM provider error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "500":
          $ref: "#/components/responses/EngineError"

  /openapi.json:
    get:
      operationId: getOpenApiSpec
      summary: OpenAPI specification (JSON)
      responses:
        "200":
          description: OpenAPI spec document
          content:
            application/json:
              schema:
                type: object

components:
  parameters:
    UserId:
      name: user_id
      in: query
      required: false
      description: User identifier for namespace isolation (default "guest")
      schema:
        type: string
        maxLength: 64
    MemoryBackend:
      name: memory_backend
      in: query
      required: false
      description: Storage backend — local or cloud (default "local")
      schema:
        type: string
        enum: [local, cloud]
        default: local
    MemoryScope:
      name: memory_scope
      in: query
      required: false
      description: Which namespace to search — personal, shared, or both (default "both")
      schema:
        type: string
        enum: [personal, shared, both]
        default: both

  responses:
    BadRequest:
      description: Invalid request parameters
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"
    EngineError:
      description: Internal engine error
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ErrorResponse"

  schemas:
    StatusResponse:
      type: object
      required: [version, blocks, append_log, layers]
      properties:
        version:
          type: string
          example: "0.8.0"
        blocks:
          type: integer
          example: 20323
        append_log:
          type: integer
          example: 12
        layers:
          type: array
          items:
            type: string
          example: [identity, long_term, short_term, associative, emotional]

    SessionResponse:
      type: object
      required: [user_id, memory_backend, memory_scope, namespace_dir, personal_namespace_dir, shared_namespace_dir]
      properties:
        user_id:
          type: string
        memory_backend:
          type: string
        memory_scope:
          type: string
        namespace_dir:
          type: string
        personal_namespace_dir:
          type: string
        shared_namespace_dir:
          type: string

    MemoryResult:
      type: object
      required: [text, depth, layer, distance, memory_scope]
      properties:
        text:
          type: string
        depth:
          type: integer
          minimum: 0
          maximum: 8
          description: "Depth level D0–D8 (D0=identity, D8=raw bytes)"
        layer:
          type: string
        distance:
          type: number
          format: float
          description: Euclidean distance in semantic space (lower = more relevant)
        memory_scope:
          type: string

    FindResult:
      type: object
      required: [text, depth, layer]
      properties:
        text:
          type: string
        depth:
          type: integer
        layer:
          type: string

    LookResult:
      type: object
      required: [text, depth, layer, distance]
      properties:
        text:
          type: string
        depth:
          type: integer
        layer:
          type: string
        distance:
          type: number
          format: float

    MqlResult:
      type: object
      required: [text, depth, layer, score]
      properties:
        text:
          type: string
        depth:
          type: integer
        layer:
          type: string
        score:
          type: number
          format: float

    RememberRequest:
      type: object
      required: [text]
      properties:
        text:
          type: string
          maxLength: 256
        layer:
          type: string
          default: long_term
          enum: [identity, long_term, short_term, associative, emotional, relational, reflections, crypto_chain, echo_cache, rust_state]
        importance:
          type: integer
          minimum: 1
          maximum: 10
          default: 5
        user_id:
          type: string
        memory_backend:
          type: string
          enum: [local, cloud]
          default: local
        memory_scope:
          type: string
          enum: [personal, shared, both]
          default: both

    RememberResponse:
      type: object
      properties:
        status:
          type: string
          example: committed
        message:
          type: string
        user_id:
          type: string
        memory_backend:
          type: string
        memory_scope:
          type: string
        written_scopes:
          type: array
          items:
            type: string
        namespace_dir:
          type: string
        personal_namespace_dir:
          type: string
        shared_namespace_dir:
          type: string

    DreamResult:
      type: object
      properties:
        status:
          type: string
          example: ok
        duration_ms:
          type: integer
        replayed_fingerprints:
          type: integer
        strengthened_pairs:
          type: integer
        pruned_pairs:
          type: integer
        pruned_activations:
          type: integer
        consolidated_patterns:
          type: integer
        energy_before:
          type: number
          format: float
        energy_after:
          type: number
          format: float

    MobileChatRequest:
      type: object
      required: [user_id, message, provider, model]
      properties:
        user_id:
          type: string
        message:
          type: string
        provider:
          type: string
          enum: [ollama, openai, gemini]
        model:
          type: string
          example: llama3
        api_base:
          type: string
          description: Override the provider's default base URL
        recall_k:
          type: integer
          default: 8
          description: Number of memories to recall for context
        system_prompt:
          type: string
        layer:
          type: string
          default: long_term
        importance:
          type: integer
          minimum: 1
          maximum: 10
          default: 7
        remember_user:
          type: boolean
          default: true
          description: Store the user's message in memory
        remember_assistant:
          type: boolean
          default: true
          description: Store the assistant's reply in memory

    MobileChatResponse:
      type: object
      required: [provider, model, reply, recalled, stored]
      properties:
        provider:
          type: string
        model:
          type: string
        reply:
          type: string
        recalled:
          type: array
          items:
            $ref: "#/components/schemas/MemoryResult"
        stored:
          type: array
          items:
            type: string
          description: Which items were stored (e.g. ["user_message", "assistant_reply"])

    ErrorResponse:
      type: object
      properties:
        error:
          type: string