interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / tensor / omega_tensor_store.h

123 lines · 6603 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef OMEGA_TENSOR_STORE_H
18#define OMEGA_TENSOR_STORE_H
19
20/*
21 * M20 OMEGA_TENSOR crash-safe storage lifetime (docs/tensor/M20_OMEGA_TENSOR.md,
22 * row "Crash-safe storage lifetime"). Owned by src/tensor; does not link or
23 * call src/runtime (the commit pattern is copied from rx_generation.c).
24 *
25 * A store is a directory that keeps generation-checked tensor storage across
26 * process exits and crashes:
27 * <dir>/<value-id-hex>.bin content-addressed payload: the tensor's elements
28 * in logical row-major order, dtype width,
29 * little-endian, every NaN stored as the canonical
30 * quiet NaN (NaN payload is not semantic in M20, so
31 * a value id names exactly one byte string).
32 * <dir>/journal header + fixed-size records (PUT / RELEASE),
33 * each with slot, generation, sequence number,
34 * dtype, shape, value id and payload SHA-256, and
35 * its own SHA-256 checksum.
36 *
37 * Commit (put and release): the file is written to a temp name, fsynced,
38 * renamed over the final name, then the directory is fsynced. A put commits
39 * its payload first and its journal record second; the journal rename is the
40 * commit point. A crash anywhere leaves either the old or the new state.
41 *
42 * Recovery (open) replays the journal and refuses, with a typed error and
43 * without returning any data, when:
44 * - the journal is shorter or longer than its header says, a record or the
45 * header fails its checksum, or a record does not follow from the state
46 * before it (wrong sequence, wrong generation, release of a free slot,
47 * put on a live slot, slot or shape out of range): OMEGA_TSTORE_ERR_TORN;
48 * - a live record's payload is missing, short, long, or does not hash to
49 * the recorded payload SHA-256 and value id: OMEGA_TSTORE_ERR_CORRUPT;
50 * - the journal is missing while payload files exist (a lost journal would
51 * reset generations): OMEGA_TSTORE_ERR_TORN.
52 * Leftover temp files and payloads no live record refers to are removed.
53 *
54 * Generations: every slot starts at generation 1. A release bumps it, and the
55 * bump is journaled, so after any recovery a released handle stays released:
56 * its generation never comes back and it gets OMEGA_TENSOR_ERR_STALE. A slot
57 * whose generation reaches UINT64_MAX is retired, never wrapped.
58 *
59 * One opener at a time (flock on the directory; a second open gets
60 * OMEGA_TSTORE_ERR_BUSY). Not thread safe. The journal grows by one record
61 * per put/release and is rewritten whole on each commit (no compaction yet).
62 * Not covered: rollback of the whole directory to an older valid copy.
63 *
64 * Status codes: OMEGA_TENSOR_* from omega_tensor.h (OK, BAD_ARGS, STALE,
65 * CAPACITY, and any code of the tensor layer) plus the codes below.
66 */
67
68#include <stdbool.h>
69#include <stdint.h>
70
71#include "omega_tensor.h"
72
73#define OMEGA_TSTORE_ERR_IO (-20) /* a file operation failed */
74#define OMEGA_TSTORE_ERR_TORN (-21) /* journal short/torn/inconsistent */
75#define OMEGA_TSTORE_ERR_CORRUPT (-22) /* payload missing or hash mismatch */
76#define OMEGA_TSTORE_ERR_BUSY (-23) /* store already open elsewhere */
77
78#define OMEGA_TSTORE_MAX_CAPACITY (1u << 20)
79
80typedef struct OmegaTensorStore OmegaTensorStore;
81
82/* Opens <dir> (created if absent, with `capacity` slots), or recovers an
83 * existing store, whose recorded capacity must equal `capacity`. */
84int omega_tensor_store_open(const char *dir, uint32_t capacity, OmegaTensorStore **out);
85void omega_tensor_store_close(OmegaTensorStore *st);
86
87/* Persists the value of t (any tensor or view of ctx) into a free slot and
88 * returns its handle once the commit is durable. */
89int omega_tensor_store_put(OmegaTensorStore *st, const OmegaTensorCtx *ctx, OmegaTensor t,
90 OmegaStorageHandle *out);
91/* Loads the stored value into a new tensor of ctx. The payload hash and value
92 * id are checked again on every load. Stale handle: OMEGA_TENSOR_ERR_STALE. */
93int omega_tensor_store_get(OmegaTensorStore *st, OmegaStorageHandle h, OmegaTensorCtx *ctx,
94 OmegaTensor *out);
95/* Releases the slot durably; every handle of it turns stale. Releasing twice,
96 * or through a stale handle, returns OMEGA_TENSOR_ERR_STALE and changes nothing. */
97int omega_tensor_store_release(OmegaTensorStore *st, OmegaStorageHandle h);
98bool omega_tensor_store_valid(const OmegaTensorStore *st, OmegaStorageHandle h);
99int omega_tensor_store_value_id(const OmegaTensorStore *st, OmegaStorageHandle h, uint8_t id[32]);
100uint32_t omega_tensor_store_live_count(const OmegaTensorStore *st);
101
102/* Crash points, in commit order. A put passes 1..8, a release 5..8. */
103enum {
104 OMEGA_TSTORE_CRASH_NONE = 0,
105 OMEGA_TSTORE_CRASH_PAYLOAD_PARTIAL, /* half the payload temp written */
106 OMEGA_TSTORE_CRASH_PAYLOAD_WRITTEN, /* payload temp fsynced, not renamed */
107 OMEGA_TSTORE_CRASH_PAYLOAD_RENAMED, /* renamed, directory not fsynced */
108 OMEGA_TSTORE_CRASH_PAYLOAD_SYNCED, /* payload durable, journal untouched */
109 OMEGA_TSTORE_CRASH_JOURNAL_PARTIAL, /* half the journal temp written */
110 OMEGA_TSTORE_CRASH_JOURNAL_WRITTEN, /* journal temp fsynced, not renamed */
111 OMEGA_TSTORE_CRASH_JOURNAL_RENAMED, /* commit point passed */
112 OMEGA_TSTORE_CRASH_JOURNAL_SYNCED, /* directory fsynced */
113 OMEGA_TSTORE_CRASH_COUNT
114};
115
116#ifdef OMEGA_TENSOR_TEST_HOOKS
117/* Test-only: the next commit that reaches `step` calls _exit(86) there.
118 * Compiled only with OMEGA_TENSOR_TEST_HOOKS; test-tensor-no-hooks checks with
119 * nm that the default build has neither this symbol nor a reference to _exit. */
120int omega_tensor_store_test_set_crash_step(OmegaTensorStore *st, int step);
121#endif
122
123#endif /* OMEGA_TENSOR_STORE_H */