Contributor Architecture Guide#

This page describes how the tree, entry, and client internals of labapi currently behave, including where the design is intentionally incomplete.

Subsystem Map#

A call passes through these layers:

  1. Client

  2. User

  3. Tree model (Notebooks -> Notebook -> NotebookDirectory / NotebookPage)

  4. Entry model (Entries and Entry subclasses)

  5. Utility layer (labapi.util helpers such as NotebookPath, indexing types, and XML extraction)

Client and User Boundary#

Client handles the connection, authentication, and request signing. It appends the AKID, expiry, and HMAC-SHA512 signature to each request URL.

User wraps an authenticated session and provides api_get()/api_post() that always add uid. Most internal modules call the API through User rather than directly through Client.

Tree Model Boundary#

The tree model is split between mapping-style collections and node/container mixins:

Most operations that mutate the notebook hierarchy are implemented in tree/mixins.py and then reused by concrete node types.

Entry Model Boundary#

entries lazily fetches page entries and materializes them as Entry subclasses.

The Entries collection owns page-level entry creation methods and appends newly created entries to local state after successful API calls.

Utility Layer Boundary#

The labapi.util package provides primitives that are shared by tree, entry, and client modules:

  • NotebookPath path normalization/resolution

  • typed index markers (Index.Id/Index.Name)

  • XML extraction and conversion helpers

  • environment and browser-detection helpers (getenv, detect_default_browser)

Cache model and invariants#

Container Population Cache (_populated)#

AbstractTreeContainer lazily loads children on first access:

  • children and mapping access call the internal _ensure_populated() helper.

  • _ensure_populated() fetches tree_tools/get_tree_level once and marks the container as populated.

  • refresh() clears _children and resets _populated=False.

Page Entries Cache (_entries)#

entries is also lazy:

  • _entries is None means “not loaded yet”.

  • first access fetches from tree_tools/get_entries_for_page and stores an Entries object.

  • refresh() resets _entries back to None.

Path Cache (_has_path)#

Every tree node memoizes its NotebookPath:

  • first path access computes and caches a NotebookPath for the node.

  • rename and move operations clear the cached path for the current node and any loaded descendants.

Path Stability and Traversal Expectations#

NotebookPath normalizes path input. A path can be absolute or relative, joined to another with /, resolved against a parent path, and made relative with relative_to().

traverse() resolves relative paths against self.path and then walks segments from self.root.

Enumeration helpers build on the same traversal model:

Mutation Invariants and Refresh Expectations#

General Rule#

Mutating methods first call the API and then update local in-memory state when the API call succeeds.

Create Operations#

create() inserts new nodes and appends them to self._children. For container nodes, the new node starts with _populated=True and empty children.

When if_exists=InsertBehavior.Replace, existing matching nodes are deleted before creating a replacement.

Move Operations#

move_to() updates the server parent, then mutates both local parents:

  • removes the node from old parent _children

  • switches _parent

  • appends to destination _children if the destination is already populated

  • invalidates cached paths for the moved node and any loaded descendants

Delete Operations#

delete() moves the node to a trash folder:

  • ensure/create an API Deleted Items directory under notebook root

  • rename node to include deletion timestamp

  • call move_to() with the trash folder as destination

The Python object stays the same, with its new parent and new name.

When to Call Refresh#

Use refresh() (or refresh() for page entries) before reading changes made by another user, the web UI, or another API session, or when you need to force a re-fetch.

By design, refresh() only clears the container’s child cache or the page’s entries cache. Child and entry objects you already hold are not updated.

Other Entry Types#

When page entries are loaded, unrecognized part types are wrapped as UnknownEntry and recognized-but-unimplemented types as UnimplementedEntry, with warnings in entries.

Known Limitations and TODOs#

  • individual entry deletion is not implemented

  • create_json_entry() creates an AttachmentEntry and a TextEntry; no wrapper object represents the pair

Contributor Checklist for Internal Changes#

Before changing tree/entry/client internals:

  1. Identify which cache invariants your change touches (_populated, _entries, _has_path).

  2. Decide whether in-memory objects must be mutated immediately or whether refresh() / refresh() should be required.

  3. Ensure parent/child bookkeeping stays symmetric for moves/deletes.

  4. If you add or change entry types, confirm entry registration plus from_part_type() dispatch behavior.