remem-ai 0.6.16

Local-first coding agent memory for Claude Code and OpenAI Codex
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/env python3
"""Validate declared PR tier claims against the actual diff.

PR tiers (`Tier: fastlane|standard|heavy` in the PR body) let small changes
skip the separate-spec-PR process. The tier is only trustworthy if a
machine, not the PR author, verifies the claim. PRs without a `Tier:` line
remain outside tier classification, but every PR must declare
`enforcement_sensitive: true|false` and match the sensitive registry.
"""

from __future__ import annotations

import argparse
import re
import subprocess
import sys
from pathlib import Path
from typing import Any

from check_spec_lifecycle import pr_body_from_env

ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "checks"))

from sensitive_enforcement import (  # noqa: E402
    classify_sensitive_changes,
    sensitive_registry,
)
from specrail_lib import PackConfig, SpecRailError, load_pack, parse_yaml_subset  # noqa: E402

TIER_RE = re.compile(r"^\s*Tier:\s*(\S+)\s*$", re.I | re.M)
SENSITIVE_RE = re.compile(
    r"^\s*enforcement_sensitive\s*:\s*(true|false)\s*$", re.I | re.M
)
KNOWN_TIERS = ("fastlane", "standard", "heavy")

FASTLANE_MAX_LINES = 50
MAX_GITHUB_CHANGED_FILES = 3000
# Paths where a change is never fastlane: API surface, CI definitions,
# install/config plumbing, and schema/migration files.
PROTECTED_PREFIXES = (
    "src/api/",
    "src/install/",
    ".github/workflows/",
)
PROTECTED_NAME_RE = re.compile(r"(schema|migration)", re.I)
# Docs and specs never count against the fastlane line budget.
DOC_EXEMPT_RE = re.compile(r"^docs/|\.md$")


def diff_numstat(base: str, head: str) -> list[tuple[str, str, str]]:
    result = subprocess.run(
        ["git", "diff", "--numstat", "--no-renames", f"{base}..{head}"],
        stdout=subprocess.PIPE,
        check=True,
        text=True,
    )
    rows = []
    for line in result.stdout.splitlines():
        parts = line.split("\t")
        if len(parts) == 3:
            rows.append((parts[0], parts[1], parts[2]))
    return rows


def diff_changed_paths(base: str, head: str) -> list[str]:
    """Return every normalized old/new path, including both sides of renames."""
    result = subprocess.run(
        ["git", "diff", "--name-status", "-z", f"{base}..{head}"],
        stdout=subprocess.PIPE,
        check=True,
    )
    return parse_name_status(result.stdout)


def parse_name_status(raw: bytes) -> list[str]:
    fields = raw.decode("utf-8", errors="strict").split("\0")
    if fields and fields[-1] == "":
        fields.pop()
    paths: list[str] = []
    index = 0
    while index < len(fields):
        status = fields[index]
        index += 1
        count = 2 if status.startswith(("R", "C")) else 1
        if not status or index + count > len(fields):
            raise SpecRailError("git diff --name-status returned malformed rename data")
        paths.extend(fields[index : index + count])
        index += count
    return sorted(set(paths))


def normalize_github_changed_file_pages(
    pages: Any, expected_count: int
) -> dict[str, Any]:
    """Validate a complete paginated GitHub PR-file response."""
    if expected_count <= 0 or expected_count > MAX_GITHUB_CHANGED_FILES:
        raise SpecRailError("live changed_files total is outside the GitHub API limit")
    if not isinstance(pages, list) or any(not isinstance(page, list) for page in pages):
        raise SpecRailError("PR changed-files query must return a page list")
    entries: list[dict[str, str]] = []
    paths: set[str] = set()
    for page in pages:
        for item in page:
            if not isinstance(item, dict):
                raise SpecRailError("PR changed-file entry must be an object")
            filename = item.get("filename")
            status = item.get("status")
            if not isinstance(filename, str) or not filename.strip():
                raise SpecRailError("PR changed-file entry filename is missing")
            if not isinstance(status, str) or not status.strip():
                raise SpecRailError("PR changed-file entry status is missing")
            entry = {"filename": filename.strip(), "status": status.strip()}
            paths.add(entry["filename"])
            previous = item.get("previous_filename")
            if status == "renamed":
                if not isinstance(previous, str) or not previous.strip():
                    raise SpecRailError("renamed PR changed-file entry lacks previous_filename")
                entry["previous_filename"] = previous.strip()
                paths.add(previous.strip())
            elif previous is not None:
                raise SpecRailError(
                    "non-renamed PR changed-file entry unexpectedly has previous_filename"
                )
            entries.append(entry)
    if len(entries) != expected_count:
        raise SpecRailError(
            "PR changed-files collection count does not match live changed_files total"
        )
    return {
        "api_limit": MAX_GITHUB_CHANGED_FILES,
        "expected_count": expected_count,
        "collected_count": len(entries),
        "complete": True,
        "entries": entries,
        "classification_paths": sorted(paths),
    }


def declared_tier(body: str) -> str | None:
    match = TIER_RE.search(body)
    return match.group(1).lower() if match else None


def declared_sensitive(body: str) -> tuple[bool | None, list[str]]:
    values = [match.lower() == "true" for match in SENSITIVE_RE.findall(body)]
    if not values:
        return None, ["PR body must declare `enforcement_sensitive: true|false` exactly once."]
    if len(values) != 1:
        return None, ["PR body contains multiple enforcement_sensitive declarations."]
    return values[0], []


def pack_at_revision(revision: str, head_config: PackConfig) -> PackConfig:
    completed = subprocess.run(
        ["git", "show", f"{revision}:workflow.yaml"],
        check=False,
        capture_output=True,
        text=True,
    )
    if completed.returncode != 0:
        detail = completed.stderr.strip() or "git show failed"
        raise SpecRailError(f"cannot load trusted base workflow.yaml: {detail}")
    workflow = parse_yaml_subset(completed.stdout)
    if not isinstance(workflow, dict):
        raise SpecRailError("trusted base workflow.yaml must be a mapping")
    return PackConfig(
        repo=ROOT,
        workflow=workflow,
        states=head_config.states,
        labels=head_config.labels,
    )


def check_sensitive(
    body: str,
    changed_paths: list[str],
    spec_refs: list[str] | None = None,
    *,
    head_config: PackConfig | None = None,
    base_config: PackConfig | None = None,
) -> list[str]:
    declaration, failures = declared_sensitive(body)
    if failures:
        return failures
    spec_refs = spec_refs or []
    try:
        head_config = head_config or load_pack(ROOT)
        base_config = base_config or head_config
        head_registry = sensitive_registry(head_config)
        base_registry = sensitive_registry(base_config)
        removed = {
            key: sorted(set(base_registry[key]) - set(head_registry[key]))
            for key in ("paths", "specs")
        }
        if any(removed.values()):
            details = [
                f"{key}: {', '.join(values)}"
                for key, values in removed.items()
                if values
            ]
            return [
                "Sensitive registry must not remove trusted base entries: "
                + "; ".join(details)
            ]
        classifications = [
            classify_sensitive_changes(
                config,
                ROOT,
                changed_paths,
                spec_refs,
                source="github_changed_files",
            )
            for config in (base_config, head_config)
        ]
    except SpecRailError as exc:
        return [f"Sensitive classification failed closed: {exc}"]
    computed = any(item["enforcement_sensitive"] for item in classifications)
    if computed and declaration is not True:
        matched = sorted(
            {
                path
                for item in classifications
                for path in item["matched_paths"] + item["matched_specs"]
            }
        )
        return [
            "Sensitive registry matched but PR declares false: " + ", ".join(matched)
        ]
    return []


def check_tier(body: str, numstat: list[tuple[str, str, str]]) -> list[str]:
    tier = declared_tier(body)
    if tier is None:
        return []
    if tier not in KNOWN_TIERS:
        return [f"Unknown tier `{tier}`; use one of: {', '.join(KNOWN_TIERS)}."]
    if tier != "fastlane":
        return []

    failures: list[str] = []
    code_lines = 0
    for added, deleted, path in numstat:
        if any(path.startswith(p) for p in PROTECTED_PREFIXES) or (
            path.startswith("src/") and PROTECTED_NAME_RE.search(path)
        ):
            failures.append(
                f"Fastlane PRs must not touch protected path `{path}`; "
                "use Tier: standard or heavy."
            )
        if DOC_EXEMPT_RE.search(path):
            continue
        if added == "-" or deleted == "-":
            failures.append(
                f"Fastlane PRs must not change binary file `{path}`."
            )
            continue
        code_lines += int(added) + int(deleted)

    if code_lines > FASTLANE_MAX_LINES:
        failures.append(
            f"Fastlane PRs are limited to {FASTLANE_MAX_LINES} changed "
            f"non-doc lines; this PR changes {code_lines}."
        )
    return failures


def check_pr(
    body: str,
    numstat: list[tuple[str, str, str]],
    changed_paths: list[str] | None = None,
    spec_refs: list[str] | None = None,
    *,
    head_config: PackConfig | None = None,
    base_config: PackConfig | None = None,
) -> list[str]:
    normalized_paths = changed_paths or [path for _added, _deleted, path in numstat]
    return check_tier(body, numstat) + check_sensitive(
        body,
        normalized_paths,
        spec_refs,
        head_config=head_config,
        base_config=base_config,
    )


def classification_spec_refs(
    changed_paths: list[str], issue: int | None = None
) -> list[str]:
    """Return complete classifier inputs for sensitive spec patterns."""
    refs = set(changed_paths)
    if issue is not None:
        if issue <= 0:
            raise SpecRailError("classification issue must be positive")
        refs.update(
            [
                f"specs/GH{issue}/product.md",
                f"specs/GH{issue}/tech.md",
            ]
        )
    return sorted(refs)


def effective_sensitive(declaration: bool, classification: dict[str, Any]) -> bool:
    """Combine the explicit declaration with trusted registry classification."""
    computed = classification.get("enforcement_sensitive")
    if not isinstance(declaration, bool) or not isinstance(computed, bool):
        raise SpecRailError("sensitive declaration and classification must be boolean")
    return declaration or computed


def self_test() -> int:
    cases = [
        (
            "no tier line passes",
            "Closes #1\nenforcement_sensitive: false",
            [("100", "50", "src/big.rs")],
            set(),
        ),
        (
            "unknown tier fails",
            "Tier: turbo\nenforcement_sensitive: false",
            [("1", "1", "src/lib.rs")],
            {"Unknown tier"},
        ),
        (
            "fastlane small change passes",
            "Tier: fastlane\nCloses #1\nenforcement_sensitive: false",
            [("20", "10", "src/context.rs"), ("300", "0", "docs/notes.md")],
            set(),
        ),
        (
            "fastlane over budget fails",
            "Tier: fastlane\nenforcement_sensitive: false",
            [("40", "20", "src/context.rs")],
            {"limited to 50"},
        ),
        (
            "fastlane protected api path fails",
            "Tier: fastlane\nenforcement_sensitive: false",
            [("5", "5", "src/api/handlers.rs")],
            {"protected path"},
        ),
        (
            "fastlane workflow path fails",
            "Tier: fastlane\nenforcement_sensitive: true",
            [("2", "2", ".github/workflows/ci.yml")],
            {"protected path"},
        ),
        (
            "fastlane schema name fails",
            "Tier: fastlane\nenforcement_sensitive: false",
            [("3", "3", "src/store/schema_v70.rs")],
            {"protected path"},
        ),
        (
            "fastlane binary fails",
            "Tier: fastlane\nenforcement_sensitive: false",
            [("-", "-", "assets/logo.png")],
            {"binary file"},
        ),
        (
            "standard tier passes untouched",
            "Tier: standard\nenforcement_sensitive: true",
            [("500", "200", "src/api/handlers.rs")],
            set(),
        ),
        (
            "case-insensitive tier line",
            "tier: FASTLANE\nenforcement_sensitive: false",
            [("10", "0", "src/lib.rs")],
            set(),
        ),
        (
            "missing sensitive declaration fails",
            "Tier: standard",
            [("1", "0", "src/lib.rs")],
            {"must declare"},
        ),
        (
            "sensitive registry conflict fails",
            "Tier: standard\nenforcement_sensitive: false",
            [("1", "0", "src/rules/compiler.rs")],
            {"Sensitive registry matched"},
        ),
        (
            "sensitive registry match passes with true",
            "Tier: standard\nenforcement_sensitive: true",
            [("1", "0", "src/rules/compiler.rs")],
            set(),
        ),
        (
            "normalized sensitive declaration passes",
            "Tier: standard\n  ENFORCEMENT_SENSITIVE : TRUE  ",
            [("1", "0", "src/rules/compiler.rs")],
            set(),
        ),
    ]
    for name, body, numstat, expected in cases:
        failures = check_pr(body, numstat)
        text = "\n".join(failures)
        if expected:
            for fragment in expected:
                if fragment not in text:
                    print(
                        f"self-test failed: {name}: missing `{fragment}`",
                        file=sys.stderr,
                    )
                    print(text, file=sys.stderr)
                    return 1
        elif failures:
            print(f"self-test failed: {name}: unexpected failures", file=sys.stderr)
            print(text, file=sys.stderr)
            return 1

    head_config = load_pack(ROOT)
    weakened_workflow = dict(head_config.workflow)
    weakened_workflow["enforcement"] = {
        "sensitive_registry": {"paths": [], "specs": []}
    }
    weakened = PackConfig(
        repo=ROOT,
        workflow=weakened_workflow,
        states=head_config.states,
        labels=head_config.labels,
    )
    failures = check_sensitive(
        "enforcement_sensitive: true",
        ["workflow.yaml"],
        head_config=weakened,
        base_config=head_config,
    )
    if not any("must not remove trusted base entries" in item for item in failures):
        print("self-test failed: registry shrinkage did not fail closed", file=sys.stderr)
        return 1

    spec_failures = check_sensitive(
        "enforcement_sensitive: false",
        ["src/lib.rs"],
        ["specs/GH813/product.md", "specs/GH813/tech.md"],
    )
    if not any("Sensitive registry matched" in item for item in spec_failures):
        print("self-test failed: linked sensitive spec did not fail closed", file=sys.stderr)
        return 1

    changed_spec_refs = classification_spec_refs(
        ["specs/GH813/product.md"], issue=910
    )
    changed_spec_failures = check_sensitive(
        "enforcement_sensitive: false",
        ["specs/GH813/product.md"],
        changed_spec_refs,
    )
    if not any("Sensitive registry matched" in item for item in changed_spec_failures):
        print("self-test failed: changed sensitive spec was omitted", file=sys.stderr)
        return 1
    if not effective_sensitive(True, {"enforcement_sensitive": False}):
        print("self-test failed: explicit sensitive declaration was ignored", file=sys.stderr)
        return 1

    rename_paths = parse_name_status(
        b"R100\0src/rules/compiler.rs\0src/rules/compiler_v2.rs\0"
        b"M\0workflow.yaml\0"
    )
    if rename_paths != [
        "src/rules/compiler.rs",
        "src/rules/compiler_v2.rs",
        "workflow.yaml",
    ]:
        print("self-test failed: rename paths were not normalized", file=sys.stderr)
        return 1

    print("check_pr_tier self-test passed")
    return 0


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("base", nargs="?")
    parser.add_argument("head", nargs="?", default="HEAD")
    parser.add_argument("--self-test", action="store_true")
    parser.add_argument("--issue", type=int)
    parser.add_argument("--github-output", type=Path)
    args = parser.parse_args()

    if args.self_test:
        return self_test()
    if not args.base:
        parser.error("base revision is required unless --self-test is used")

    try:
        head_config = load_pack(ROOT)
        base_config = pack_at_revision(args.base, head_config)
    except SpecRailError as exc:
        print(f"PR tier check failed closed: {exc}", file=sys.stderr)
        return 1
    body = pr_body_from_env()
    declaration, declaration_failures = declared_sensitive(body)
    changed_paths = diff_changed_paths(args.base, args.head)
    spec_refs = classification_spec_refs(changed_paths, args.issue)
    if args.issue is not None:
        if args.issue <= 0:
            parser.error("--issue must be positive")
    failures = check_pr(
        body,
        diff_numstat(args.base, args.head),
        changed_paths,
        spec_refs,
        head_config=head_config,
        base_config=base_config,
    )
    if failures:
        print("PR tier check failed:", file=sys.stderr)
        for failure in failures:
            print(f"- {failure}", file=sys.stderr)
        return 1

    if declaration_failures or declaration is None:
        print("PR tier check failed closed: sensitive declaration unavailable", file=sys.stderr)
        return 1
    if args.github_output is not None:
        with args.github_output.open("a", encoding="utf-8") as output:
            output.write(
                "enforcement_sensitive=" + ("true" if declaration else "false") + "\n"
            )

    print("PR tier check passed")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())