#!/bin/sh
""":"
case "$0" in
    # Inside the app: always this bundle's Python, never a home inherited from another one.
    *.app/Contents/Resources/*) bundle=${0%%.app/Contents/Resources/*}.app
                                export PYTHONHOME="$bundle/Contents/Resources/python" ;;
esac
exec "$PYTHONHOME/bin/python3" -I -S -B "$0" "$@"
": """
__doc__ = """Summary: read the markdown once, write the one file every view reads.

    bin/summary               write .summary/summary.json, print its path and size
    bin/summary --no-words    the same, but never start the words-on-arrival run
                              (bin/check uses this)

The app is the door to the life folder. Every view (notch, window,
charts) reads .summary/summary.json, so the markdown format has exactly one
reader that knows it, and it lives here next to conventions.md. If the format
changes, this script is the one place to change; bin/check runs it so a
change that breaks it is caught the same day.

The file is rebuilt every run and is never authority (.summary/ is in
.gitignore). Frontmatter here is flat `key: value`, parsed by hand: standard
library only, nothing to install.

What it reads:

    now.md                    line count; the bullets or numbered lines under
                              the needs heading (config.json `needs_heading`),
                              "This week", "Not reached"; and the needs
                              heading again, split by its "Small:" and
                              "Decisions waiting in projects:" labels into
                              needs_you.todo and needs_you.waiting, each item with its project, the
                              pages it came from and, for "commit NNNN", the
                              commit line from that report's "## git add"
    projects/README.md        the table (state, last touched, next), joined with
                              each page's frontmatter and its # title
    decisions/NNNN-*.md       id, title, status, date, revisit, days to revisit
    work/achievements/*.md    top-level bullets per month (one bullet, one row)
    domains/people/*.md       slug and # name
    inbox/*.md                file and updated (raw files may have no
                              frontmatter; then updated is null)
    log/*.md                  date from the file name
    ideas/NNNN-slug/idea.md   id, # title, status, private, updated;
                              `version` stays 1: the app's decoder ignores keys
                              it does not know
    git                       short HEAD and whether the tree is dirty (read only)

Tasks and events (the Today view; read only, git included):

    tasks                     one per brief (with its report), project page,
                              idea and decision. id (brief-0019, project-app,
                              idea-0001, decision-0005), what it is (the title,
                              or the page's "What it is" first sentence), why
                              (a brief's first "## Why" sentence), its project,
                              state, status_text (the raw line state came from),
                              first and last event dates, sources, and its
                              events oldest first
    recent                    the events since local midnight, newest first

    An event is {date, time, at, kind, task, tasks, line, source}. Kinds:
    commit (the life folder, repo "yours"), repo-commit (another repo in
    config.json `repos`), brief-written, report-landed, read-back, brief-done,
    decision-accepted, idea-filed, log (one top-level bullet in log/, its time
    from git blame; null while uncommitted). Events reach back EVENT_DAYS.
    A commit or log line is tied to a task by the files it touches, a 4-digit
    number it names ("decision 0004", "idea 0001", else a brief) that exists,
    or, in another repo, its hash named in a brief's status line or its
    report's "## Read-back" (only for a commit after that brief was written).
    A bare number after another project's name is that repo's brief and is
    not tied. `task` is the first tie, `tasks` all of them.

    State is mapped only from what a file says, else null:
      brief    status "done…" done; "parked…" parked; a report with a
               "## Read-back" section but no "done" status: waiting for your
               commit; a report without one: built, waiting for your test;
               status with "running": running; "ready…": ready
      project  table state "active": running; "done…" or "abandoned": done;
               "on a break…": parked
      decision accepted or rejected: done; proposed: waiting for your word
      idea     parked: parked; dropped or started: done

`runs` stays empty; the app's scheduler fills it.

Then it runs `bin/memory update --budget 5`: the memory index's sweep (brief
0110), one line, never a failure of this script.

After summary.json it runs `bin/foryou build`, which writes
.summary/for-you.json: every text the app shows the user, written for them (needs,
decisions, files, projects, life, ledger), kept out of this file so this one
does not grow. `for_you` here only points at it.

When that build leaves a Needs you or decision item with no words, it says
how many in one line. It starts nothing: the `words` scheduled job writes
them, through the runner, when the user has it on.

It never guesses a field: anything missing is null. Anything it cannot parse
(a broken frontmatter block, a missing now.md section, a table row with the
wrong number of cells, a link to a page that does not exist) stops it with one
line naming the file.

Exit status:
    0   written
    1   a file could not be parsed; the line says which
"""
import datetime
import glob
import json
import os
import re
import subprocess
import sys

BIN = os.path.dirname(os.path.abspath(__file__))
sys.dont_write_bytecode = True  # no __pycache__ beside the engine's scripts
sys.path.insert(0, BIN)
import lifecfg  # noqa: E402

LIFE = lifecfg.root()
CFG = lifecfg.config(LIFE)
# The label of the life folder's own repo in sources and events: "yours" (brief
# 0104; it was "argoni", and "argus" before that). The app reads it as the user's own.
LIFE_REPO = lifecfg.OWN
OUT = os.path.join(LIFE, ".summary", "summary.json")
NOW_MAX_LINES = CFG["now_max_lines"]
_REPOS = []


def seen_repos(lines=False):
    """lifecfg.repos(), found once per run: one walk of the folders, not one per lookup.
    The paths, or with lines=True the plain lines (a folder skipped, none chosen yet)."""
    if not _REPOS:
        _REPOS.append(lifecfg.repos(LIFE))
    return _REPOS[0][1 if lines else 0]
NOW_SECTIONS = {"needs_you": CFG["needs_heading"], "this_week": "This week",
                "not_reached": "Not reached"}
DATED = re.compile(r"^(\d{4}-\d{2}-\d{2})")
LIST_ITEM = re.compile(r"^(?:- |\d+\. )(.*)$")
# A plain line ending in a colon inside a section: "Small:".
GROUP_LABEL = re.compile(r"^([A-Z][^:]*):\s*$")
# The needs heading's groups. No label at all means the user's to-dos,
# which is what the section held before it had labels; any other label stops.
NEEDS_GROUPS = {"Small": "todo", "Decisions waiting in projects": "waiting"}
# "app: …", "app#0008 (…) …"
NEED_PROJECT = re.compile(r"^([a-z0-9][a-z0-9-]*)(?:#(\d{4}))?(?::\s*|\s+)(.*)$")
NEED_COMMIT = re.compile(r"\bcommit (?:brief )?(\d{4})\b")
NEED_NUMBER = re.compile(r"(?<![\d#-])(\d{4})(?![\d-])")


class ParseError(Exception):
    pass


def warn(line):
    """A file left out of the summary, in one line; the summary is still written."""
    print("summary: %s" % line, file=sys.stderr)


def rel(path):
    return os.path.relpath(path, LIFE)


def read(path):
    with open(path, encoding="utf-8") as fh:
        return fh.read()


def frontmatter(path, required=True):
    """Return (fields, body). Flat `key: value` only; anything else is an error."""
    text = read(path)
    lines = text.splitlines()
    if not lines or lines[0].strip() != "---":
        if required:
            raise ParseError("%s: no frontmatter block" % rel(path))
        return None, text
    try:
        end = lines.index("---", 1)
    except ValueError:
        raise ParseError("%s: frontmatter block never closes" % rel(path))
    fields = {}
    for n, line in enumerate(lines[1:end], start=2):
        if not line.strip():
            continue
        key, sep, value = line.partition(":")
        if not sep or not re.match(r"^[a-z_]+$", key):
            raise ParseError("%s:%d: frontmatter line is not `key: value`" % (rel(path), n))
        fields[key] = value.strip() or None
    return fields, "\n".join(lines[end + 1:])


def h1(path, body):
    for line in body.splitlines():
        if line.startswith("# "):
            return line[2:].strip()
    raise ParseError("%s: no # title" % rel(path))


def git():
    def out(*args):
        code, text, _ = lifecfg.git(LIFE, *args)
        return text.strip() if code == 0 else None
    head = out("rev-parse", "--short", "HEAD")
    status = out("status", "--porcelain", "--ignore-submodules=all")
    return {"head": head, "dirty": None if status is None else bool(status)}


def now_md():
    """Return (now, needs_you): the `now` block, and the needs list in its two groups."""
    path = os.path.join(LIFE, "now.md")
    text = read(path)
    sections, current, group = {}, None, None
    needs = {"todo": [], "waiting": []}
    for n, line in enumerate(text.splitlines(), start=1):
        if line.startswith("## "):
            current = line[3:].strip()
            sections[current] = []
            group = None
        elif current is not None:
            m = LIST_ITEM.match(line)
            label = GROUP_LABEL.match(line)
            if m:
                sections[current].append(m.group(1).strip())
                if current == NOW_SECTIONS["needs_you"]:
                    needs[group or "todo"].append(m.group(1).strip())
            elif current == NOW_SECTIONS["needs_you"] and label:
                if label.group(1).strip() not in NEEDS_GROUPS:
                    raise ParseError("now.md:%d: '%s:' is not one of %s" % (
                        n, label.group(1).strip(), ", ".join("'%s:'" % k for k in NEEDS_GROUPS)))
                group = NEEDS_GROUPS[label.group(1).strip()]
    result = {"lines": len(text.splitlines()), "limit": NOW_MAX_LINES}
    for key, heading in NOW_SECTIONS.items():
        if heading not in sections:
            raise ParseError("now.md: no '## %s' section" % heading)
        result[key] = sections[heading]
    return result, {k: [need_item(t) for t in v] for k, v in needs.items()}


def repo_dir(name):
    """A repo of the user's on this Mac by its folder name, or None: one Argoni sees
    (config.json `repos_folders` and `repos`). Never the life folder."""
    for d in seen_repos():
        if os.path.basename(d.rstrip("/")) == name:
            return d
    return None


def commit_line(report):
    """The `cd … && git add …` line under a report's "## git add" heading, or None."""
    lines = read(os.path.join(LIFE, report)).splitlines()
    for i, line in enumerate(lines):
        if line.startswith("## git add"):
            for l in lines[i + 1:]:
                if l.startswith("## "):
                    return None
                if " git add " in l:
                    return l.strip()
    return None


def need_item(text):
    """One needs line: its project, the pages it came from, and the
    commit line when it says "commit NNNN". Only what the line names; never a guess."""
    item = {"text": text, "project": None, "what": text, "links": [], "commit": None}
    m = NEED_PROJECT.match(text)
    if not m or not (os.path.isfile(os.path.join(LIFE, "projects", m.group(1) + ".md"))
                     or repo_dir(m.group(1))):
        return item
    slug, number, item["what"] = m.group(1), m.group(2), m.group(3)
    item["project"] = slug
    if number:
        item["what"] = "decision %s %s" % (number, item["what"])
    # "app#0008": that decision, when the repo is on this Mac.
    if number and repo_dir(slug):
        found = sorted(glob.glob(os.path.join(repo_dir(slug), "docs", "decisions", number + "-*.md")))
        if found:
            item["links"].append({"label": "decision %s" % number, "repo": slug,
                                  "path": os.path.relpath(found[0], repo_dir(slug))})
    # "0016", "commit 0016": the life folder's report on that brief for this project.
    commit = NEED_COMMIT.search(text)
    for num in NEED_NUMBER.findall(item["what"]):
        found = sorted(glob.glob(os.path.join(LIFE, "reports", "%s-%s-*.md" % (num, slug))))
        if not found:
            continue
        report = rel(found[0])
        item["links"].append({"label": "report %s" % num, "repo": None, "path": report})
        if commit and commit.group(1) == num:
            item["commit"] = commit_line(report)
    if os.path.isfile(os.path.join(LIFE, "projects", slug + ".md")):
        item["links"].append({"label": "project page", "repo": None, "path": "projects/%s.md" % slug})
    return item


def projects():
    index = os.path.join(LIFE, "projects", "README.md")
    _, body = frontmatter(index)
    rows = {}
    for n, line in enumerate(body.splitlines(), start=1):
        if not line.startswith("| ["):
            continue
        cells = [c.strip() for c in line.strip().strip("|").split("|")]
        m = re.match(r"^\[[^\]]+\]\(([a-z0-9-]+)\.md\)$", cells[0])
        if len(cells) != 5 or not m:
            raise ParseError("projects/README.md: table row %r does not parse" % line[:60])
        slug = m.group(1)
        if not os.path.isfile(os.path.join(LIFE, "projects", slug + ".md")):
            raise ParseError("projects/README.md: row links %s.md, which does not exist" % slug)
        rows[slug] = {"state": cells[2] or None, "last_touched": cells[3] or None,
                      "next": cells[4] or None}
    # A fresh life folder has the table's header and no rows yet; that is not an error.
    if not rows and not re.search(r"^\| *Project *\|", body, re.M):
        raise ParseError("projects/README.md: no project table found")
    pages = sorted(p for p in glob.glob(os.path.join(LIFE, "projects", "*.md"))
                   if os.path.basename(p) != "README.md")
    by_slug = {}
    for page in pages:
        slug = os.path.basename(page)[:-3]
        fields, body = frontmatter(page)
        row = rows.get(slug, {})
        by_slug[slug] = {
            "slug": slug, "title": h1(page, body), "area": fields.get("area"),
            "state": row.get("state"), "last_touched": row.get("last_touched"),
            "next": row.get("next"), "confidence": fields.get("confidence"),
            "updated": fields.get("updated"),
        }
    # Table order is hot to cold; pages missing from the table go last.
    order = list(rows) + [s for s in sorted(by_slug) if s not in rows]
    return [by_slug[s] for s in order]


def decisions(today):
    out = []
    for path in sorted(glob.glob(os.path.join(LIFE, "decisions", "*.md"))):
        m = re.match(r"^(\d{4})-", os.path.basename(path))
        if not m:
            raise ParseError("%s: name is not NNNN-title.md" % rel(path))
        fields, body = frontmatter(path)
        title = re.sub(r"^\d{4}:\s*", "", h1(path, body))
        revisit, days = fields.get("revisit"), None
        if revisit:
            try:
                days = (datetime.date.fromisoformat(revisit) - today).days
            except ValueError:
                # One bad date never stops the whole summary (Today would go stale): said, left out.
                warn("%s: revisit %r is not a real YYYY-MM-DD date; left out" % (rel(path), revisit))
                revisit = None
        out.append({"id": m.group(1), "title": title, "status": fields.get("status"),
                    "date": fields.get("date"), "revisit": revisit,
                    "days_to_revisit": days})
    return out


def ledger():
    out = []
    for path in sorted(glob.glob(os.path.join(LIFE, "work", "achievements", "*.md"))):
        month = os.path.basename(path)[:-3]
        if not re.match(r"^\d{4}-\d{2}$", month):
            raise ParseError("%s: name is not YYYY-MM.md" % rel(path))
        _, body = frontmatter(path)
        out.append({"month": month,
                    "rows": sum(1 for l in body.splitlines() if l.startswith("- "))})
    return out


def people():
    out = []
    for path in sorted(glob.glob(os.path.join(LIFE, "domains", "people", "*.md"))):
        _, body = frontmatter(path)
        out.append({"slug": os.path.basename(path)[:-3], "name": h1(path, body)})
    return out


def inbox():
    out = []
    for path in sorted(glob.glob(os.path.join(LIFE, "inbox", "*.md"))):
        fields, _ = frontmatter(path, required=False)
        out.append({"file": rel(path), "updated": (fields or {}).get("updated")})
    return out


def ideas():
    out = []
    for path in sorted(glob.glob(os.path.join(LIFE, "ideas", "*", "idea.md"))):
        m = re.match(r"^(\d{4})-[a-z0-9-]+$", os.path.basename(os.path.dirname(path)))
        if not m:
            raise ParseError("%s: folder is not NNNN-slug" % rel(path))
        fields, body = frontmatter(path)
        private = fields.get("private")
        if private not in ("true", "false", None):
            raise ParseError("%s: private %r is not true or false" % (rel(path), private))
        out.append({"id": m.group(1), "title": re.sub(r"^\d{4}:\s*", "", h1(path, body)),
                    "status": fields.get("status"),
                    "private": None if private is None else private == "true",
                    "updated": fields.get("updated")})
    return out


WEEK_REVIEW = re.compile(r"^(\d{4})-W(\d{2})-review\.md$")


def logs():
    """Every log file: YYYY-MM-DD*.md, or a weekly review YYYY-Www-review.md, dated by
    its `updated:` line (the week's Monday when it has none) and carrying its week."""
    out = []
    for path in sorted(glob.glob(os.path.join(LIFE, "log", "*.md"))):
        name = os.path.basename(path)
        m, w = DATED.match(name), WEEK_REVIEW.match(name)
        if m:
            out.append({"date": m.group(1), "file": rel(path)})
        elif w:
            fields, _ = frontmatter(path, required=False)
            day = str((fields or {}).get("updated") or "")
            if not DATED.match(day):
                try:
                    day = datetime.date.fromisocalendar(int(w.group(1)), int(w.group(2)), 1).isoformat()
                except ValueError:
                    warn("%s: week %s of %s does not exist; left out" % (rel(path), w.group(2), w.group(1)))
                    continue
            out.append({"date": day[:10], "file": rel(path), "week": "%s-W%s" % w.groups()})
        else:
            raise ParseError("%s: name does not start with YYYY-MM-DD (or is not YYYY-Www-review.md)" % rel(path))
    return sorted(out, key=lambda l: (l["date"], l["file"]))


# Tasks and events for the Today view.
EVENT_DAYS = 30
NUMBER_REF = re.compile(r"(?:\b(decision|idea)s?[:\s]+)?(?<![\d#\w-])(\d{4})(?![\d-])", re.I)
HASH = re.compile(r"\b([0-9a-f]{7,12})\b")
FILE_TASK = [(re.compile(r"^briefs/(\d{4})-"), "brief-%s"),
             (re.compile(r"^reports/(\d{4})-"), "brief-%s"),
             (re.compile(r"^ideas/(\d{4})-"), "idea-%s"),
             (re.compile(r"^decisions/(\d{4})-"), "decision-%s"),
             (re.compile(r"^projects/([a-z0-9-]+)\.md$"), "project-%s")]


def git_out(repo, *args):
    code, out, _ = lifecfg.git(repo, *args)
    return out if code == 0 else ""


def section(body, heading):
    """The text under '## heading' up to the next '## ', or None."""
    lines, out = body.splitlines(), None
    for line in lines:
        if line.startswith("## "):
            if out is not None:
                break
            if line[3:].strip().lower().startswith(heading.lower()):
                out = []
        elif out is not None:
            out.append(line)
    return None if out is None else "\n".join(out).strip()


def first_sentence(text, limit=240):
    text = re.sub(r"\s+", " ", re.sub(r"\*\*|`", "", text or "")).strip()
    m = re.match(r"^(.+?[.!?])(?:\s|$)", text)
    text = m.group(1) if m else text
    return (text[:limit - 1] + "…") if len(text) > limit else (text or None)


def task_list(projects_list):
    tasks = {}
    for path in sorted(glob.glob(os.path.join(LIFE, "briefs", "[0-9][0-9][0-9][0-9]-*.md"))):
        num = os.path.basename(path)[:4]
        fields, body = frontmatter(path)
        status = fields.get("status") or ""
        report = sorted(glob.glob(os.path.join(LIFE, "reports", num + "-*.md")))
        report = rel(report[0]) if report else None
        readback = report and section(frontmatter(os.path.join(LIFE, report))[1], "Read-back")
        if status.startswith("done"):
            state = "done"
        elif status.startswith("parked"):
            state = "parked"
        elif report and readback:
            state = "waiting for your commit"
        elif report:
            state = "built, waiting for your test"
        elif "running" in status:
            state = "running"
        elif status.startswith("ready"):
            state = "ready"
        else:
            state = None
        subject = fields.get("subject")
        tasks["brief-" + num] = {
            "id": "brief-" + num, "kind": "brief",
            "what": re.sub(r"^Brief \d{4}:\s*", "", h1(path, body)),
            "why": first_sentence(section(body, "Why")),
            "project": LIFE_REPO if lifecfg.own(subject) else subject,
            "state": state, "status_text": status or None,
            "files": [rel(path)] + ([report] if report else []),
            "readback": readback or None}
    rows = {p["slug"]: p for p in projects_list}
    for slug, p in rows.items():
        page = os.path.join(LIFE, "projects", slug + ".md")
        _, body = frontmatter(page)
        m = re.search(r"^\*\*What it is:\*\*\s*(.*)$", body, re.M)
        raw = (p["state"] or "").lower()
        state = ("running" if raw == "active" else
                 "done" if raw.startswith("done") or raw == "abandoned" else
                 "parked" if raw.startswith("on a break") else None)
        tasks["project-" + slug] = {
            "id": "project-" + slug, "kind": "project",
            "what": first_sentence(m.group(1)) if m else p["title"], "why": None,
            "project": slug, "state": state, "status_text": p["state"],
            "files": [rel(page)], "next": p["next"]}
    for d in decisions(datetime.date.today()):
        path = sorted(glob.glob(os.path.join(LIFE, "decisions", d["id"] + "-*.md")))[0]
        tasks["decision-" + d["id"]] = {
            "id": "decision-" + d["id"], "kind": "decision", "what": d["title"], "why": None,
            "project": frontmatter(path)[0].get("subject"),
            "state": {"accepted": "done", "rejected": "done", "proposed": "waiting for your word"}.get(d["status"]),
            "status_text": d["status"], "files": [rel(path)]}
    for i in ideas():
        path = sorted(glob.glob(os.path.join(LIFE, "ideas", i["id"] + "-*", "idea.md")))[0]
        tasks["idea-" + i["id"]] = {
            "id": "idea-" + i["id"], "kind": "idea", "what": i["title"], "why": None,
            "project": None,
            "state": {"parked": "parked", "dropped": "done", "started": "done"}.get(i["status"]),
            "status_text": i["status"], "files": [rel(path)]}
    for t in tasks.values():
        if t["project"] == "self":
            t["project"] = None
    return tasks


def text_ties(text, tasks):
    """Tasks a line names by number. A number after another project's name
    ("platform 18 (brief 0017 review)") is that repo's own brief, not
    the life folder's, and is skipped."""
    slugs = [t["project"] for t in tasks.values() if t["kind"] == "project"]
    # No project pages yet: nothing can name another project (an empty alternation
    # would match everywhere and tie no number at all).
    named = re.compile(r"(?<![\w-])(%s)(?![\w-])" % "|".join(map(re.escape, slugs))) if slugs else None
    out = []
    for m in NUMBER_REF.finditer(text):
        kind, num = m.group(1), m.group(2)
        if kind is None and re.match(r"\s*§", text[m.end():]):
            kind = "decision"  # "0001 §4" is a decision's section
        tid = "%s-%s" % ((kind or "brief").lower(), num)
        if tid not in tasks or tid in out:
            continue
        before = named.findall(text[:m.start()]) if named else []
        if kind is None and before and before[-1] not in (LIFE_REPO, tasks[tid]["project"]):
            continue
        out.append(tid)
    return out


def event(at, kind, ties, line, repo, path=None, commit=None):
    dt = datetime.datetime.fromisoformat(at).astimezone() if at else None
    return {"date": dt.date().isoformat() if dt else None,
            "time": dt.strftime("%H:%M") if dt else None,
            "at": dt.isoformat(timespec="seconds") if dt else None,
            "kind": kind, "task": ties[0] if ties else None, "tasks": ties,
            "line": line, "source": {"repo": repo, "path": path, "commit": commit}}


def first_commit_matching(path, pattern, since):
    """(short hash, iso time) of the oldest commit whose diff of path adds or
    removes a line matching pattern, or None."""
    out = git_out(LIFE, "log", "--since", since, "--format=%h\t%aI", "-G", pattern, "--", path)
    rows = [l.split("\t") for l in out.splitlines() if l.strip()]
    return tuple(rows[-1]) if rows else None


def events_list(tasks, since):
    events = []
    # life folder commits, with the files each touched
    out = git_out(LIFE, "-c", "core.quotePath=false", "log", "--since", since, "--format=\x01%h\t%aI\t%s",
                  "--name-status")
    touched = set()  # every path a commit in the window changed: only those can have a dated change
    for block in out.split("\x01")[1:]:
        lines = block.strip().splitlines()
        short, at, subject = lines[0].split("\t", 2)
        ties = text_ties(subject, tasks)
        for l in lines[1:]:
            parts = l.split("\t")
            if len(parts) < 2:
                continue
            path = parts[-1]
            touched.add(path)
            for rx, fmt in FILE_TASK:
                m = rx.match(path)
                if m and fmt % m.group(1) in tasks and fmt % m.group(1) not in ties:
                    ties.append(fmt % m.group(1))
            if parts[0] == "A":
                m = re.match(r"^briefs/(\d{4})-", path)
                if m and "brief-" + m.group(1) in tasks:
                    t = tasks["brief-" + m.group(1)]
                    events.append(event(at, "brief-written", [t["id"]],
                                        "brief %s written: %s" % (m.group(1), t["what"]),
                                        LIFE_REPO, path, short))
                m = re.match(r"^reports/(\d{4})-[^/]*\.md$", path)
                if m and "brief-" + m.group(1) in tasks:
                    t = tasks["brief-" + m.group(1)]
                    events.append(event(at, "report-landed", [t["id"]],
                                        "report %s landed: %s" % (m.group(1), t["what"]),
                                        LIFE_REPO, path, short))
                m = re.match(r"^ideas/(\d{4})-[^/]*/idea\.md$", path)
                if m and "idea-" + m.group(1) in tasks:
                    t = tasks["idea-" + m.group(1)]
                    events.append(event(at, "idea-filed", [t["id"]],
                                        "idea %s filed: %s" % (m.group(1), t["what"]),
                                        LIFE_REPO, path, short))
        events.append(event(at, "commit", ties, subject, LIFE_REPO, None, short))
    # status changes that git can date: one git call per file the window's commits touched
    # (a file no commit touched since then has nothing to find; code review S-E13)
    for t in tasks.values():
        if t["kind"] not in ("brief", "decision") or not touched.intersection(t["files"]):
            continue
        if t["kind"] == "brief":
            hit = first_commit_matching(t["files"][0], r"^status: done", since)
            if hit:
                events.append(event(hit[1], "brief-done", [t["id"]],
                                    "brief %s marked done: %s" % (t["id"][-4:], t["status_text"]),
                                    LIFE_REPO, t["files"][0], hit[0]))
            if len(t["files"]) > 1 and t["readback"]:
                hit = first_commit_matching(t["files"][1], r"^## Read-back", since)
                if hit:
                    events.append(event(hit[1], "read-back", [t["id"]],
                                        "read-back filed: " + first_sentence(t["readback"]),
                                        LIFE_REPO, t["files"][1], hit[0]))
        elif t["kind"] == "decision":
            hit = first_commit_matching(t["files"][0], r"^status: accepted", since)
            if hit:
                events.append(event(hit[1], "decision-accepted", [t["id"]],
                                    "decision %s accepted: %s" % (t["id"][-4:], t["what"]),
                                    LIFE_REPO, t["files"][0], hit[0]))
    # log bullets, each timed by the commit that wrote it
    cutoff = since[:10]
    for path in sorted(glob.glob(os.path.join(LIFE, "log", "*.md"))):
        m = DATED.match(os.path.basename(path))
        if not m or m.group(1) < cutoff:
            continue
        blame = git_out(LIFE, "blame", "--line-porcelain", "--", rel(path))
        commit, at = None, None
        for l in blame.splitlines():
            if re.match(r"^[0-9a-f]{40} ", l):
                commit = l[:7]
            elif l.startswith("author-time "):
                at = int(l.split()[1])
            elif l.startswith("\t- "):
                text = l[3:].strip()
                uncommitted = commit == "0000000"
                when = None if uncommitted or at is None else \
                    datetime.datetime.fromtimestamp(at).astimezone().isoformat()
                e = event(when, "log", text_ties(text, tasks), first_sentence(text, 300),
                          LIFE_REPO, rel(path), None if uncommitted else commit)
                e["date"] = e["date"] or m.group(1)
                events.append(e)
        if not blame:  # not in git yet: bullets with the file's date only
            for l in read(path).splitlines():
                if l.startswith("- "):
                    e = event(None, "log", text_ties(l, tasks), first_sentence(l[2:], 300),
                              LIFE_REPO, rel(path))
                    e["date"] = m.group(1)
                    events.append(e)
    # commits in the user's other repos on this Mac, read only
    written = {e["task"]: e["at"] for e in events if e["kind"] == "brief-written"}
    hashes = {}
    for t in tasks.values():
        if t["kind"] == "brief":
            for text in (t["status_text"] or "", t["readback"] or ""):
                for h in HASH.findall(text):
                    if re.search(r"[a-f]", h) and re.search(r"\d", h):
                        hashes.setdefault(h, t["id"])
    for repo in seen_repos():
        name = os.path.basename(repo.rstrip("/"))
        if os.path.realpath(repo) == os.path.realpath(LIFE) or not os.path.isdir(os.path.join(repo, ".git")):
            continue
        out = git_out(repo, "log", "--since", since, "--format=%H\t%h\t%aI\t%s")
        for l in out.splitlines():
            full, short, at, subject = l.split("\t", 3)
            # A hash only ties to a brief written before the commit.
            ties = [tid for h, tid in hashes.items() if full.startswith(h)
                    and at >= written.get(tid, "")]
            if "project-" + name.lower() in tasks:
                ties.append("project-" + name.lower())
            events.append(event(at, "repo-commit", ties, subject, name.lower(), None, short))
    return events


def tasks_and_recent(projects_list, today):
    tasks = task_list(projects_list)
    since = (today - datetime.timedelta(days=EVENT_DAYS)).isoformat()
    events = events_list(tasks, since)
    key = lambda e: (e["date"] or "", e["time"] or "")
    for t in tasks.values():
        mine = sorted((e for e in events if t["id"] in e["tasks"]), key=key)
        t["first"] = mine[0]["date"] if mine else None
        t["last"] = mine[-1]["date"] if mine else None
        sources = []
        for f in t["files"]:
            sources.append({"repo": LIFE_REPO, "path": f, "commit": None})
        for e in mine:
            if e["source"] not in sources:
                sources.append(e["source"])
        t["sources"] = sources
        t["events"] = mine
        for k in ("files", "readback"):
            t.pop(k, None)
    recent = sorted((e for e in events if e["date"] == today.isoformat()), key=key, reverse=True)
    order = {"brief": 0, "project": 1, "decision": 2, "idea": 3}
    return sorted(tasks.values(), key=lambda t: (order[t["kind"]], t["id"])), recent


def main():
    stamp = datetime.datetime.now().astimezone()
    try:
        now, needs_you = now_md()
        projects_list = projects()
        tasks, recent = tasks_and_recent(projects_list, stamp.date())
        summary = {
            "version": 1,
            "generated_at": stamp.isoformat(timespec="seconds"),
            "git": git(),
            "now": now,
            # The same lines as now.needs_you, in their two groups.
            # now.needs_you stays the flat list for readers that predate this.
            "needs_you": needs_you,
            "projects": projects_list,
            # The repos Argoni sees, by folder name, and the lines a page shows when a
            # folder was skipped or none is chosen yet ("No project folders chosen yet ...").
            "repos_seen": {"repos": [os.path.basename(d.rstrip("/")) for d in seen_repos()],
                           "lines": seen_repos(lines=True)},
            "decisions": decisions(stamp.date()),
            "ledger": ledger(),
            "people": people(),
            "inbox": inbox(),
            "logs": logs(),
            "ideas": ideas(),
            "runs": [],
            # The Today view.
            "recent": recent,
            "tasks": tasks,
            # The words for the user live in their own file.
            "for_you": {"path": ".summary/for-you.json", "today": ".summary/today.json"},
        }
    except (ParseError, OSError, UnicodeDecodeError) as e:
        print("summary: FAIL, %s" % e, file=sys.stderr)
        return 1
    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    lifecfg.write_atomic(OUT, json.dumps(summary, indent=2, ensure_ascii=False) + "\n")
    print("%s (%d bytes)" % (rel(OUT), os.path.getsize(OUT)), flush=True)
    p = subprocess.run([sys.executable, "-I", os.path.join(BIN, "foryou"), "build"])
    if p.returncode:
        return 1
    memory_sweep()
    if "--no-words" not in sys.argv[1:]:
        words_on_arrival(stamp)
    return 0


def memory_sweep():
    """Keep the memory index fresh (bin/memory, brief 0110): a sweep of the files whose size
    or time moved, at most a few seconds, so a first build of a large folder never holds
    this run up; the next run goes on. The index is never authority, so a failure here is
    one line, never a failed summary."""
    p = subprocess.run([sys.executable, "-I", os.path.join(BIN, "memory"), "update", "--budget", "5"],
                       capture_output=True, text=True)
    line = (p.stdout or p.stderr).strip().splitlines()
    if p.returncode:
        print("summary: memory index not updated: %s" % (line[-1] if line else "exit %d" % p.returncode))
    elif line:
        print(line[-1])


def words_on_arrival(stamp):
    """Say how many items wait for words. The engine never starts a model itself
    (brief 0103): the app runs the `words` job through the runner when the user
    has it on, and that job finds these same items."""
    try:
        with open(os.path.join(LIFE, ".summary", "for-you.json"), encoding="utf-8") as fh:
            keys = json.load(fh).get("wanted") or []
    except (OSError, ValueError):
        return
    if keys:
        print("summary: %d item(s) wait for words; the words job writes them when it is on" % len(keys))

if __name__ == "__main__":
    try:
        sys.exit(main())
    except OSError as e:  # a write refused (lifecfg.inside) or the disk: one line, no traceback
        print("summary: FAIL, %s" % e, file=sys.stderr)
        sys.exit(1)
