Files
0d84c334ba feat(sdk): move the plugin SDK into the monorepo with independent releases (LANG-420) (#2993)
* chore(sdk): import langbot-plugin-sdk v0.7.24 snapshot into sdk/ (LANG-420)

Snapshot of langbot-app/langbot-plugin-sdk at 76443bb8 (tag v0.7.24),
without history. The repository's .github/ workflows are not imported;
they are replaced by sdk-*.yml workflows in the next commit. The root
.gitignore keeps the template .vscode/launch.json.example tracked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* feat(sdk): move the plugin SDK into the monorepo with independent releases (LANG-420)

- CI: sdk-tests.yml / sdk-box-docker.yml run the SDK suite from sdk/ in
  its own environment (Python 3.10-3.12); run-tests.yml gains a
  "Core with in-tree SDK" job that overlays sdk/ on Core's venv.
- Releases: sdk-publish.yml publishes langbot-plugin from sdk/vX.Y.Z tag
  pushes and never creates a GitHub Release. Core release workflows now
  only accept v* tags, and scripts/check_sdk_release_pin.py blocks a Core
  PyPI/Docker release whose langbot-plugin pin differs from the released,
  unchanged sdk/.
- Packaging: Core still installs the PyPI pin. Core wheel/sdist, git
  export, Docker context, release ZIP and fnOS package exclude sdk/.
- Generator/tests: generate_plugin_workspace_api.py writes sdk/ directly;
  the SDK's generator drift tests now run instead of skipping.
- Docs: AGENTS/ARCHITECTURE, sdk/ guides (+ RELEASING.md, SOURCE.json),
  agent skills and lbs probes, public docs (zh/en/ja) and dev-docs
  point at sdk/ instead of the sibling langbot-plugin-sdk checkout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-11-11 23:12:53 +08:00

115 lines
3.5 KiB
Python

"""Filesystem usage helpers shared by LangBot-managed runtimes.
The scanner deliberately does not follow symbolic links. Runtime storage roots
can contain user-controlled files, so following a link here could both escape
the managed directory and make a diagnostic request unexpectedly expensive.
"""
from __future__ import annotations
import os
import stat
from pathlib import Path
from typing import Any, Iterable
StorageRoot = tuple[str, Path, str, str | None]
def collect_storage_directories(roots: Iterable[StorageRoot]) -> list[dict[str, Any]]:
"""Collect size and file counts for logical runtime-owned directories.
Each root is ``(key, path, kind, parent_key)``. ``kind == "root"`` marks
independent storage included in the process total; ``detail`` entries are
useful subdirectories whose bytes are already included in their parent.
"""
return [
_collect_storage_directory(
key=key,
path=Path(path),
kind=kind,
parent_key=parent_key,
)
for key, path, kind, parent_key in roots
]
def storage_total_bytes(directories: Iterable[dict[str, Any]]) -> int:
return sum(
int(item.get("size_bytes") or 0)
for item in directories
if item.get("kind") == "root"
)
def _collect_storage_directory(
*,
key: str,
path: Path,
kind: str,
parent_key: str | None,
) -> dict[str, Any]:
size_bytes = 0
file_count = 0
error_count = 0
exists = False
try:
root_stat = path.lstat()
exists = True
except (FileNotFoundError, NotADirectoryError):
root_stat = None
except OSError:
root_stat = None
error_count = 1
if root_stat is not None:
if not _is_traversable_directory(root_stat):
size_bytes = int(root_stat.st_size)
file_count = 1
else:
stack = [path]
while stack:
current = stack.pop()
try:
with os.scandir(current) as entries:
for entry in entries:
try:
entry_stat = entry.stat(follow_symlinks=False)
except (FileNotFoundError, NotADirectoryError):
continue
except OSError:
error_count += 1
continue
if _is_traversable_directory(entry_stat):
stack.append(Path(entry.path))
continue
size_bytes += int(entry_stat.st_size)
file_count += 1
except (FileNotFoundError, NotADirectoryError):
continue
except OSError:
error_count += 1
result: dict[str, Any] = {
"key": key,
"path": str(path),
"kind": kind,
"exists": exists,
"size_bytes": size_bytes,
"file_count": file_count,
"error_count": error_count,
}
if parent_key is not None:
result["parent_key"] = parent_key
return result
def _is_traversable_directory(value: os.stat_result) -> bool:
reparse_flag = getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0)
file_attributes = int(getattr(value, "st_file_attributes", 0) or 0)
return stat.S_ISDIR(value.st_mode) and not (
reparse_flag and file_attributes & reparse_flag
)