type-bridge-workspace 2.1.0

Validated programmatic workspace configuration for type-bridge
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
//! Offline migration authoring and planning over the source workspace.
//!
//! These are the library equivalents of `type-bridge migration make` and
//! `type-bridge migration plan`: both run without any provider connection.
//! The desired schema is always the workspace's compiled schema sources, the
//! source is the committed migration head, and workspace policy is carried by
//! the validated config — never inferred.

use std::collections::BTreeSet;
use std::io::Read as _;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use type_bridge_contract::capability::CapabilitySet;
use type_bridge_contract::diagnostic::Diagnostic;
use type_bridge_contract::migration::MigrationId;
use type_bridge_contract::schema::{DeclaredSchema, DocumentId};
use type_bridge_schema::SafetyClass;
use type_bridge_schema_compat::{ADOPTED_GENESIS_FILE_NAME, parse_adopted_genesis};
use type_bridge_schema_migration::{
    GeneratedMigration, MigrationDirectory, MigrationGenerationOutcome, MigrationGenerationRequest,
    MigrationHistoryGraph, MigrationPreviewError,
    canonical_history_declared_legacy_bridge_count_in, discover_verified_migration_chain_in,
    generate_next_migration, render_migration_preview, require_adoption_authority_pair,
    require_adoption_authority_pair_state, try_acquire_migration_authoring_lock,
    write_generated_migration_under_lock,
};

use crate::{
    TypeBridgeWorkspace, TypeBridgeWorkspaceError, WorkspaceConfigError, WorkspaceConfigErrorCode,
};

fn migration_directory_escape() -> TypeBridgeWorkspaceError {
    TypeBridgeWorkspaceError::Config(
        WorkspaceConfigError::new(
            WorkspaceConfigErrorCode::PathNotConfined,
            "the migration directory must remain beneath the workspace without symbolic links",
        )
        .with_detail("migration_v2_directory"),
    )
}

/// An open, workspace-bound migration-directory authority.
///
/// The directory descriptor remains live for the authority's lifetime. Its
/// operational path resolves through that descriptor rather than through the
/// mutable workspace pathname, so replacing a checked component cannot
/// redirect discovery or publication.
///
#[derive(Debug)]
pub struct MigrationDirectoryAuthority {
    directory: MigrationDirectory,
    configured_path: PathBuf,
    owner: Arc<()>,
}

impl MigrationDirectoryAuthority {
    /// Return the retained capability used for filesystem operations.
    #[must_use]
    pub const fn directory(&self) -> &MigrationDirectory {
        &self.directory
    }

    /// Return the configured path for diagnostics and operator output only.
    #[must_use]
    pub fn display_path(&self) -> &Path {
        &self.configured_path
    }
}

/// One dependency-ordered entry of an offline apply plan.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MigrationPlanEntry {
    id: MigrationId,
    safety: SafetyClass,
    reversible: bool,
}

impl MigrationPlanEntry {
    /// Return the compound migration identity.
    pub const fn id(&self) -> &MigrationId {
        &self.id
    }

    /// Return the verified safety classification the policy will meet.
    pub const fn safety(&self) -> SafetyClass {
        self.safety
    }

    /// Return whether the manifest carries a verified reverse program.
    pub const fn reversible(&self) -> bool {
        self.reversible
    }
}

impl TypeBridgeWorkspace {
    /// Open the existing migration directory as retained authority.
    pub fn open_migration_directory(
        &self,
    ) -> Result<MigrationDirectoryAuthority, TypeBridgeWorkspaceError> {
        self.open_migration_directory_impl(false)
    }

    /// Create missing real directory components and retain the result as authority.
    ///
    /// Creation is component-relative to already opened parent descriptors;
    /// symbolic links and non-directories are rejected at every step.
    pub fn ensure_migration_directory(
        &self,
    ) -> Result<MigrationDirectoryAuthority, TypeBridgeWorkspaceError> {
        self.open_migration_directory_impl(true)
    }

    fn open_migration_directory_impl(
        &self,
        create: bool,
    ) -> Result<MigrationDirectoryAuthority, TypeBridgeWorkspaceError> {
        let root = self.config().workspace_root().as_path();
        let directory = MigrationDirectory::open_beneath_directory(
            self.root_directory(),
            self.config().migration_v2_directory().as_path(),
            create,
        )
        .map_err(|_| migration_directory_escape())?;
        Ok(MigrationDirectoryAuthority {
            directory,
            configured_path: root.join(self.config().migration_v2_directory().as_path()),
            owner: Arc::clone(self.authority_owner()),
        })
    }

    fn require_migration_directory(
        &self,
        directory: &MigrationDirectoryAuthority,
    ) -> Result<(), TypeBridgeWorkspaceError> {
        let expected = self
            .config()
            .workspace_root()
            .as_path()
            .join(self.config().migration_v2_directory().as_path());
        if !Arc::ptr_eq(&directory.owner, self.authority_owner())
            || directory.configured_path != expected
        {
            return Err(migration_directory_escape());
        }
        Ok(())
    }

    /// Discover and replay-verify the workspace's canonical migration chain.
    pub fn discover_migrations(&self) -> Result<MigrationHistoryGraph, TypeBridgeWorkspaceError> {
        let directory = self.open_migration_directory()?;
        self.discover_migrations_in(&directory)
    }

    /// Discover from one retained directory authority.
    pub fn discover_migrations_in(
        &self,
        directory: &MigrationDirectoryAuthority,
    ) -> Result<MigrationHistoryGraph, TypeBridgeWorkspaceError> {
        self.discover_migrations_with_genesis_in(directory)
            .map(|(graph, _)| graph)
    }

    fn discover_migrations_with_genesis_in(
        &self,
        directory: &MigrationDirectoryAuthority,
    ) -> Result<(MigrationHistoryGraph, DeclaredSchema), TypeBridgeWorkspaceError> {
        self.require_migration_directory(directory)?;
        let adopted_source = read_adopted_genesis_bounded(directory.directory())?;
        let adopted_genesis_present = adopted_source.is_some();
        let declared_bridge_count =
            canonical_history_declared_legacy_bridge_count_in(directory.directory())?;
        require_adoption_authority_pair_state(adopted_genesis_present, declared_bridge_count)?;
        let genesis = self.parse_migration_genesis(adopted_source)?;
        let graph = discover_verified_migration_chain_in(
            directory.directory(),
            &genesis,
            self.delta_context(),
        )?;
        require_adoption_authority_pair(&graph, adopted_genesis_present)?;
        Ok((graph, genesis))
    }

    /// Author the next canonical migration toward the compiled schema sources.
    ///
    /// Offline by default: the source is the committed head's verified target
    /// (or the genesis for an empty history) and the target is the workspace's
    /// desired declared schema. Nothing is written; pair the outcome with
    /// [`Self::write_generated_migration`].
    pub fn migration_make(
        &self,
        base_name: &str,
    ) -> Result<MigrationGenerationOutcome, TypeBridgeWorkspaceError> {
        let directory = self.open_migration_directory()?;
        self.migration_make_in(&directory, base_name)
    }

    /// Author against one retained directory authority.
    pub fn migration_make_in(
        &self,
        directory: &MigrationDirectoryAuthority,
        base_name: &str,
    ) -> Result<MigrationGenerationOutcome, TypeBridgeWorkspaceError> {
        let (graph, genesis) = self.discover_migrations_with_genesis_in(directory)?;
        let request = MigrationGenerationRequest {
            app_label: self.config().app_label().as_str(),
            base_name,
            genesis_source: &genesis,
            desired: self.declared_schema(),
            context: self.delta_context(),
        };
        Ok(generate_next_migration(&graph, &request)?)
    }

    /// Persist one generated manifest and its review-only TypeQL preview.
    pub fn write_generated_migration(
        &self,
        generated: &GeneratedMigration,
    ) -> Result<PathBuf, TypeBridgeWorkspaceError> {
        let directory = self.open_migration_directory()?;
        self.write_generated_migration_in(&directory, generated)?;
        Ok(directory.display_path().join(generated.file_name()))
    }

    /// Persist through one retained directory authority.
    pub fn write_generated_migration_in(
        &self,
        directory: &MigrationDirectoryAuthority,
        generated: &GeneratedMigration,
    ) -> Result<PathBuf, TypeBridgeWorkspaceError> {
        self.require_migration_directory(directory)?;
        // Discovery, deterministic regeneration, and publication are one
        // serialized authority operation. Locking only the final filenames
        // permits two differently named candidates derived from one stale
        // head to publish as ambiguous siblings.
        let authoring_lock = try_acquire_migration_authoring_lock(directory.directory())?;
        let base_name = generated
            .manifest()
            .id()
            .name()
            .as_str()
            .split_once('_')
            .map(|(_, base_name)| base_name)
            .filter(|base_name| !base_name.is_empty())
            .ok_or_else(generated_migration_stale)?;
        let regenerated = self.migration_make_in(directory, base_name)?;
        let MigrationGenerationOutcome::Generated(regenerated) = regenerated else {
            return Err(generated_migration_stale());
        };
        if regenerated.canonical_bytes() != generated.canonical_bytes() {
            return Err(generated_migration_stale());
        }
        let preview = render_migration_preview(generated.manifest(), self.delta_context())
            .map_err(preview_diagnostic)?;
        Ok(write_generated_migration_under_lock(
            &authoring_lock,
            generated,
            &preview,
        )?)
    }

    /// Plan the dependency-ordered chain from an explicit applied basis.
    ///
    /// The plan is offline: it names what would apply and each manifest's
    /// verified safety classification, so an operator can see which entries
    /// the workspace policy will gate before connecting to any database.
    pub fn migration_plan(
        &self,
        applied: &BTreeSet<MigrationId>,
    ) -> Result<Vec<MigrationPlanEntry>, TypeBridgeWorkspaceError> {
        let directory = self.open_migration_directory()?;
        self.migration_plan_in(&directory, applied)
    }

    /// Plan from one retained directory authority.
    pub fn migration_plan_in(
        &self,
        directory: &MigrationDirectoryAuthority,
        applied: &BTreeSet<MigrationId>,
    ) -> Result<Vec<MigrationPlanEntry>, TypeBridgeWorkspaceError> {
        let graph = self.discover_migrations_in(directory)?;
        let order = graph.plan_apply_to_default_head(applied)?;
        order
            .into_iter()
            .map(|id| {
                let manifest = graph.manifest(&id).ok_or_else(|| {
                    TypeBridgeWorkspaceError::Contract(Diagnostic::new(
                        type_bridge_contract::diagnostic::DiagnosticCategory::Integrity,
                        type_bridge_contract::diagnostic::DiagnosticCode::new(
                            "workspace_migration_plan_missing_manifest",
                        )
                        .expect("static workspace diagnostic code"),
                        "planned identity has no verified manifest",
                    ))
                })?;
                Ok(MigrationPlanEntry {
                    id,
                    safety: manifest.safety(),
                    reversible: manifest.reversible(),
                })
            })
            .collect()
    }

    /// Return the genesis source every parentless manifest verifies against.
    ///
    /// A workspace that never adopted a legacy (v1) database starts managed
    /// history from the empty schema. An adopted workspace carries the
    /// `adopted-genesis.typeql` artifact beside its canonical manifests; the
    /// reconstructed legacy head parsed from those bytes is the genesis on
    /// every offline and connected operation, so the legacy-frontier bridge
    /// and everything chained onto it replay-verify against the exact head
    /// the adoption recorded.
    pub fn migration_genesis(&self) -> Result<DeclaredSchema, TypeBridgeWorkspaceError> {
        let directory = self.open_migration_directory()?;
        self.migration_genesis_in(&directory)
    }

    /// Read the genesis from one retained directory authority.
    pub fn migration_genesis_in(
        &self,
        directory: &MigrationDirectoryAuthority,
    ) -> Result<DeclaredSchema, TypeBridgeWorkspaceError> {
        self.require_migration_directory(directory)?;
        self.parse_migration_genesis(read_adopted_genesis_bounded(directory.directory())?)
    }

    fn parse_migration_genesis(
        &self,
        source: Option<String>,
    ) -> Result<DeclaredSchema, TypeBridgeWorkspaceError> {
        let Some(source) = source else {
            return Ok(DeclaredSchema::from_facts(
                self.declared_schema().format(),
                CapabilitySet::new(),
                std::iter::empty(),
            )?);
        };
        let document = DocumentId::new(ADOPTED_GENESIS_FILE_NAME)?;
        parse_adopted_genesis(document, &source).map_err(TypeBridgeWorkspaceError::Contract)
    }
}

fn read_adopted_genesis_bounded(
    directory: &MigrationDirectory,
) -> Result<Option<String>, TypeBridgeWorkspaceError> {
    let limit = type_bridge_schema_compat::MAX_TYPEQL_SCHEMA_BYTES;
    let file = match directory.open_regular_readonly(ADOPTED_GENESIS_FILE_NAME.as_ref()) {
        Ok(file) => file,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(_) => {
            return Err(adopted_genesis_read_error(
                type_bridge_contract::diagnostic::DiagnosticCategory::Integrity,
                "workspace_adopted_genesis_unreadable",
                "adopted-genesis artifact exists but cannot be read as a regular file",
            ));
        }
    };
    if !file.metadata().is_ok_and(|metadata| metadata.is_file()) {
        return Err(adopted_genesis_read_error(
            type_bridge_contract::diagnostic::DiagnosticCategory::Integrity,
            "workspace_adopted_genesis_not_regular",
            "adopted-genesis artifact must be a regular file",
        ));
    }
    let mut bytes = Vec::new();
    file.take(u64::try_from(limit).unwrap_or(u64::MAX).saturating_add(1))
        .read_to_end(&mut bytes)
        .map_err(|_| {
            adopted_genesis_read_error(
                type_bridge_contract::diagnostic::DiagnosticCategory::Integrity,
                "workspace_adopted_genesis_unreadable",
                "adopted-genesis artifact exists but cannot be read",
            )
        })?;
    if bytes.len() > limit {
        return Err(adopted_genesis_read_error(
            type_bridge_contract::diagnostic::DiagnosticCategory::ResourceLimit,
            "workspace_adopted_genesis_oversized",
            "adopted-genesis artifact exceeds the schema byte ceiling",
        ));
    }
    String::from_utf8(bytes).map(Some).map_err(|_| {
        adopted_genesis_read_error(
            type_bridge_contract::diagnostic::DiagnosticCategory::InvalidContract,
            "workspace_adopted_genesis_not_utf8",
            "adopted-genesis artifact is not valid UTF-8",
        )
    })
}

fn generated_migration_stale() -> TypeBridgeWorkspaceError {
    TypeBridgeWorkspaceError::Contract(Diagnostic::new(
        type_bridge_contract::diagnostic::DiagnosticCategory::Integrity,
        type_bridge_contract::diagnostic::DiagnosticCode::new(
            "workspace_generated_migration_stale",
        )
        .expect("static workspace diagnostic code"),
        "generated migration no longer matches this workspace directory authority",
    ))
}

fn adopted_genesis_read_error(
    category: type_bridge_contract::diagnostic::DiagnosticCategory,
    code: &'static str,
    message: &'static str,
) -> TypeBridgeWorkspaceError {
    TypeBridgeWorkspaceError::Contract(Diagnostic::new(
        category,
        type_bridge_contract::diagnostic::DiagnosticCode::new(code)
            .expect("static workspace diagnostic code"),
        message,
    ))
}

fn preview_diagnostic(error: MigrationPreviewError) -> TypeBridgeWorkspaceError {
    match error {
        MigrationPreviewError::Diagnostic(diagnostic) => {
            TypeBridgeWorkspaceError::Contract(diagnostic)
        }
        MigrationPreviewError::Lowering(lowering) => {
            TypeBridgeWorkspaceError::Contract(Diagnostic::new(
                type_bridge_contract::diagnostic::DiagnosticCategory::InvalidContract,
                type_bridge_contract::diagnostic::DiagnosticCode::new(lowering.code())
                    .expect("lowering diagnostic codes are canonical"),
                "migration preview lowering failed",
            ))
        }
    }
}