opencode-setup-system 0.0.92

Managed ai-stp installation component for OpenCode, maintained by NDDev.
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
//! One bounded transaction database; completed records contain no file payloads.

use std::sync::Arc;

use cap_fs_ext::DirExt;
use cap_std::fs::Dir;
use rusqlite::{Connection, OpenFlags, OptionalExtension, config::DbConfig, limits::Limit, params};
use serde::{Deserialize, Serialize};

use super::{Admission, Change, Content, Result, files, invalid};
use crate::setup_core::{digest, sqlite as vfs};

const OWNER: &str = "CREATE TABLE owner (singleton INTEGER PRIMARY KEY CHECK(singleton=1), identity BLOB NOT NULL) STRICT";
const OPERATIONS: &str = "CREATE TABLE operations (id TEXT PRIMARY KEY, digest TEXT NOT NULL, admission BLOB NOT NULL, context BLOB NOT NULL, stage BLOB, phase TEXT NOT NULL CHECK(phase IN ('applying','finishing','completed')), active INTEGER CHECK(active=1), outcome BLOB, CHECK((active IS NULL)=(phase='completed'))) STRICT, WITHOUT ROWID";
const ACTIVE: &str =
    "CREATE UNIQUE INDEX one_active ON operations(active) WHERE active IS NOT NULL";
const CHANGES: &str = "CREATE TABLE changes (operation TEXT NOT NULL, sequence INTEGER NOT NULL, metadata BLOB NOT NULL, payload BLOB, PRIMARY KEY(operation,sequence)) STRICT, WITHOUT ROWID";
const APPLICATION: i64 = 0x4153_4657;
const CONTROL: &str = "forward";
const META_LIMIT: usize = 8 * 1024 * 1024;
// Reserve space above the 64 MiB effect bound for final provenance metadata.
const PAYLOAD_LIMIT: usize = 96 * 1024 * 1024;
pub(super) const CHANGE_LIMIT: usize = 8192;

fn sql<T>(value: rusqlite::Result<T>) -> Result<T> {
    value.map_err(|error| invalid().with_source(error))
}

fn encode<T: Serialize>(value: &T) -> Result<Vec<u8>> {
    let bytes = serde_json::to_vec(value).map_err(|_| invalid())?;
    if bytes.len() > META_LIMIT {
        return Err(invalid());
    }
    Ok(bytes)
}

fn decode<T: serde::de::DeserializeOwned>(bytes: &[u8]) -> Result<T> {
    if bytes.len() > META_LIMIT {
        return Err(invalid());
    }
    serde_json::from_slice(bytes).map_err(|_| invalid())
}

#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct Metadata {
    path: String,
    before: files::Observation,
    after: Option<(String, usize, u32)>,
}

pub(super) struct Record {
    pub(super) admission: Admission,
    pub(super) context: serde_json::Value,
    pub(super) stage: Option<files::Identity>,
    pub(super) phase: String,
    pub(super) outcome: Option<serde_json::Value>,
}

pub(super) struct Store {
    // SQLite must close its handles before the held VFS scope is released.
    db: Connection,
    scope: Arc<vfs::Scope>,
}

impl Store {
    pub(super) fn open(control: &Dir, owner: files::Identity) -> Result<Self> {
        match control.create_dir(CONTROL) {
            Ok(()) => files::sync(control)?,
            Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
            Err(error) => return Err(invalid().with_source(error)),
        }
        let directory = files::io(control.open_dir_nofollow(CONTROL))?;
        for entry in files::io(directory.entries())? {
            let entry = files::io(entry)?;
            let name = entry.file_name();
            let name = name.to_str().ok_or_else(invalid)?;
            if !matches!(name, vfs::DATABASE | vfs::JOURNAL | vfs::LOCK) {
                return Err(invalid());
            }
            files::io(vfs::inspect(&directory, name))?.ok_or_else(invalid)?;
        }
        let scope = vfs::Scope::open(control, CONTROL, directory, true)?;
        let db = sql(Connection::open_with_flags_and_vfs(
            scope.path(),
            OpenFlags::SQLITE_OPEN_READ_WRITE
                | OpenFlags::SQLITE_OPEN_CREATE
                | OpenFlags::SQLITE_OPEN_NO_MUTEX,
            vfs::NAME,
        ))?;
        sql(db.set_db_config(DbConfig::SQLITE_DBCONFIG_TRUSTED_SCHEMA, false))?;
        sql(db.set_db_config(DbConfig::SQLITE_DBCONFIG_DEFENSIVE, true))?;
        for (kind, limit) in [
            (Limit::SQLITE_LIMIT_LENGTH, 33 * 1024 * 1024),
            (Limit::SQLITE_LIMIT_SQL_LENGTH, 64 * 1024),
            (Limit::SQLITE_LIMIT_COLUMN, 16),
            (Limit::SQLITE_LIMIT_EXPR_DEPTH, 100),
            (Limit::SQLITE_LIMIT_ATTACHED, 0),
            (Limit::SQLITE_LIMIT_TRIGGER_DEPTH, 0),
            (Limit::SQLITE_LIMIT_WORKER_THREADS, 0),
        ] {
            sql(db.set_limit(kind, limit))?;
        }
        sql(db.execute_batch(
            "PRAGMA mmap_size=0; PRAGMA temp_store=MEMORY; PRAGMA cache_size=-2048;",
        ))?;
        let store = Self { db, scope };
        store.schema(owner)?;
        Ok(store)
    }

    fn schema(&self, owner: files::Identity) -> Result<()> {
        let application: i64 = sql(self.db.query_row("PRAGMA application_id", [], |r| r.get(0)))?;
        let version: i64 = sql(self.db.query_row("PRAGMA user_version", [], |r| r.get(0)))?;
        let mut statement = sql(self
            .db
            .prepare("SELECT sql FROM sqlite_schema ORDER BY name"))?;
        let schemas = sql(sql(statement.query_map([], |r| r.get::<_, String>(0)))?
            .collect::<rusqlite::Result<Vec<_>>>())?;
        let empty = application == 0 && version == 0 && schemas.is_empty();
        if !empty
            && (application != APPLICATION
                || version != 1
                || schemas != [CHANGES, ACTIVE, OPERATIONS, OWNER])
        {
            return Err(invalid());
        }
        if !empty {
            let recorded: Vec<u8> =
                sql(self
                    .db
                    .query_row("SELECT identity FROM owner WHERE singleton=1", [], |r| {
                        r.get(0)
                    }))?;
            if decode::<files::Identity>(&recorded)? != owner {
                return Err(invalid());
            }
        }
        let journal: String = sql(self
            .db
            .query_row("PRAGMA journal_mode=DELETE", [], |r| r.get(0)))?;
        if journal != "delete" {
            return Err(invalid());
        }
        sql(self.db.execute_batch(
            "PRAGMA synchronous=EXTRA; PRAGMA max_page_count=65536; PRAGMA secure_delete=ON;",
        ))?;
        if empty {
            sql(self.db.execute_batch("BEGIN IMMEDIATE"))?;
            for schema in [OWNER, OPERATIONS, ACTIVE, CHANGES] {
                sql(self.db.execute_batch(schema))?;
            }
            sql(self
                .db
                .execute("INSERT INTO owner VALUES (1,?1)", [encode(&owner)?]))?;
            sql(self.db.pragma_update(None, "application_id", APPLICATION))?;
            sql(self.db.pragma_update(None, "user_version", 1))?;
            sql(self.db.execute_batch("COMMIT"))?;
        }
        self.scope.validate()
    }

    pub(super) fn record(&self, admission: &Admission) -> Result<Option<Record>> {
        self.scope.validate()?;
        let row = sql(self
            .db
            .query_row(
                "SELECT digest,admission,context,stage,phase,outcome FROM operations WHERE id=?1",
                [&admission.operation_id],
                |r| {
                    Ok((
                        r.get::<_, String>(0)?,
                        r.get::<_, Vec<u8>>(1)?,
                        r.get::<_, Vec<u8>>(2)?,
                        r.get::<_, Option<Vec<u8>>>(3)?,
                        r.get::<_, String>(4)?,
                        r.get::<_, Option<Vec<u8>>>(5)?,
                    ))
                },
            )
            .optional())?;
        row.map(|(digest, bytes, context, stage, phase, outcome)| {
            if digest != admission.plan_digest || decode::<Admission>(&bytes)? != *admission {
                return Err(invalid());
            }
            let outcome = outcome.map(|v| decode(&v)).transpose()?;
            if phase == "completed" && outcome.is_none() {
                return Err(invalid());
            }
            Ok(Record {
                admission: admission.clone(),
                context: decode(&context)?,
                stage: stage.map(|v| decode(&v)).transpose()?,
                phase,
                outcome,
            })
        })
        .transpose()
    }

    pub(super) fn idle(&self) -> Result<()> {
        let count: i64 =
            sql(self
                .db
                .query_row("SELECT count(*) FROM operations WHERE active=1", [], |r| {
                    r.get(0)
                }))?;
        if count != 0 {
            return Err(invalid());
        }
        Ok(())
    }

    fn insert_changes(&self, id: &str, start: usize, changes: &[Change]) -> Result<()> {
        if changes.len() + start > CHANGE_LIMIT {
            return Err(invalid());
        }
        let mut total = 0_usize;
        let mut paths = std::collections::BTreeSet::new();
        for (index, change) in changes.iter().enumerate() {
            if !files::valid_path(&change.path) || !paths.insert(&change.path) {
                return Err(invalid());
            }
            let after = change.after.as_ref().map(|content| {
                (
                    digest::of_bytes(&content.bytes),
                    content.bytes.len(),
                    content.mode,
                )
            });
            if let Some(content) = &change.after {
                total = total.checked_add(content.bytes.len()).ok_or_else(invalid)?;
                content.validate()?;
            }
            if total > PAYLOAD_LIMIT {
                return Err(invalid());
            }
            let metadata = Metadata {
                path: change.path.clone(),
                before: change.before.clone(),
                after,
            };
            sql(self.db.execute(
                "INSERT INTO changes VALUES (?1,?2,?3,?4)",
                params![
                    id,
                    i64::try_from(index + start).map_err(|_| invalid())?,
                    encode(&metadata)?,
                    change.after.as_ref().map(|c| &c.bytes)
                ],
            ))?;
        }
        Ok(())
    }

    pub(super) fn admit(
        &mut self,
        admission: &Admission,
        context: &serde_json::Value,
        changes: &[Change],
    ) -> Result<()> {
        self.idle()?;
        self.scope.validate()?;
        sql(self.db.execute_batch("BEGIN IMMEDIATE"))?;
        let result = (|| {
            sql(self.db.execute(
                "INSERT INTO operations VALUES (?1,?2,?3,?4,NULL,'applying',1,NULL)",
                params![
                    admission.operation_id,
                    admission.plan_digest,
                    encode(admission)?,
                    encode(context)?
                ],
            ))?;
            self.insert_changes(&admission.operation_id, 0, changes)
        })();
        self.end(result)
    }

    pub(super) fn cancel(
        &mut self,
        admission: &Admission,
        outcome: &serde_json::Value,
        admitted: bool,
    ) -> Result<()> {
        self.scope.validate()?;
        sql(self.db.execute_batch("BEGIN IMMEDIATE"))?;
        let result = (|| {
            if admitted {
                if sql(self.db.execute(
                    "UPDATE operations SET phase='completed',active=NULL,context=x'7b7d',outcome=?1 WHERE id=?2 AND digest=?3 AND phase='applying' AND stage IS NULL AND active=1",
                    params![encode(outcome)?, admission.operation_id, admission.plan_digest],
                ))? != 1 {
                    return Err(invalid());
                }
                sql(self.db.execute(
                    "DELETE FROM changes WHERE operation=?1",
                    [&admission.operation_id],
                ))?;
            } else {
                sql(self.db.execute(
                    "INSERT INTO operations VALUES (?1,?2,?3,x'7b7d',NULL,'completed',NULL,?4)",
                    params![
                        admission.operation_id,
                        admission.plan_digest,
                        encode(admission)?,
                        encode(outcome)?
                    ],
                ))?;
            }
            Ok(())
        })();
        self.end(result)
    }

    fn end(&self, result: Result<()>) -> Result<()> {
        match result {
            Ok(()) => {
                sql(self.db.execute_batch("COMMIT"))?;
                self.scope.validate()
            }
            Err(error) => {
                let _ = self.db.execute_batch("ROLLBACK");
                Err(error)
            }
        }
    }

    pub(super) fn set_stage(&self, id: &str, identity: files::Identity) -> Result<()> {
        self.scope.validate()?;
        if sql(self.db.execute(
            "UPDATE operations SET stage=?1 WHERE id=?2 AND stage IS NULL AND phase='applying'",
            params![encode(&identity)?, id],
        ))? != 1
        {
            return Err(invalid());
        }
        Ok(())
    }

    pub(super) fn changes(&self, id: &str) -> Result<Vec<Change>> {
        self.scope.validate()?;
        let mut statement = sql(self.db.prepare("SELECT sequence,metadata,payload FROM changes WHERE operation=?1 ORDER BY sequence LIMIT 8193"))?;
        let mut rows = sql(statement.query([id]))?;
        let mut changes = Vec::new();
        let mut total = 0_usize;
        while let Some(row) = sql(rows.next())? {
            let sequence = usize::try_from(sql(row.get::<_, i64>(0))?).map_err(|_| invalid())?;
            if sequence != changes.len() || sequence >= CHANGE_LIMIT {
                return Err(invalid());
            }
            let metadata: Metadata = decode(&sql(row.get::<_, Vec<u8>>(1))?)?;
            let payload: Option<Vec<u8>> = sql(row.get(2))?;
            let after = match (metadata.after, payload) {
                (Some((hash, length, mode)), Some(bytes))
                    if bytes.len() == length && digest::of_bytes(&bytes) == hash =>
                {
                    total = total.checked_add(bytes.len()).ok_or_else(invalid)?;
                    let content = Content { bytes, mode };
                    content.validate()?;
                    Some(content)
                }
                (None, None) => None,
                _ => return Err(invalid()),
            };
            if total > PAYLOAD_LIMIT {
                return Err(invalid());
            }
            changes.push(Change {
                path: metadata.path,
                before: metadata.before,
                after,
            });
        }
        Ok(changes)
    }

    pub(super) fn finish(
        &mut self,
        id: &str,
        changes: &[Change],
        outcome: &serde_json::Value,
    ) -> Result<()> {
        let count: i64 = sql(self.db.query_row(
            "SELECT count(*) FROM changes WHERE operation=?1",
            [id],
            |r| r.get(0),
        ))?;
        sql(self.db.execute_batch("BEGIN IMMEDIATE"))?;
        let result = (|| {
            self.insert_changes(id, usize::try_from(count).map_err(|_| invalid())?, changes)?;
            if sql(self.db.execute("UPDATE operations SET phase='finishing',outcome=?1 WHERE id=?2 AND phase='applying'", params![encode(outcome)?, id]))? != 1 { return Err(invalid()); }
            Ok(())
        })();
        self.end(result)
    }

    pub(super) fn complete(&mut self, id: &str) -> Result<()> {
        sql(self.db.execute_batch("BEGIN IMMEDIATE"))?;
        let result = (|| {
            sql(self
                .db
                .execute("DELETE FROM changes WHERE operation=?1", [id]))?;
            if sql(self.db.execute("UPDATE operations SET phase='completed',active=NULL,context=x'7b7d' WHERE id=?1 AND phase='finishing' AND outcome IS NOT NULL", [id]))? != 1 { return Err(invalid()); }
            Ok(())
        })();
        self.end(result)
    }
}