diff --git a/.claude-config/bin/README.md b/.claude-config/bin/README.md index 2041343..10953d6 100644 --- a/.claude-config/bin/README.md +++ b/.claude-config/bin/README.md @@ -287,15 +287,25 @@ transcript, before that time is billed to a ticket. Python standard library only - **The rule.** A silent stretch of 30 minutes or less between events is working time. A longer gap is classified from its evidence and printed with its cause, COUNTED or EXCLUDED: an - attended pause (the Operator typed next) counts up to `--attended-max` hours (default 4); - unattended agent time (a background task that ran through the gap) counts to the same - limit; a usage-limit or provider-error hold, a stated break, a wait ended by a harness message - (scheduled check, context handoff, resume message, `/compact`), a gap with no prompt - evidence and a gap past the limit are excluded. `--keep ISO` and `--drop ISO` settle one - gap on evidence the transcript cannot show; `--also FILE...` adds lane transcripts as work + attended pause (the Operator typed next) counts when it lies wholly inside work hours + (Monday to Friday 07:00 to 18:00 in the display zone, start and end on the same date, both + ends inclusive) and lasts no longer than `--attended-max` hours (default 4), and is assumed + idle when any part falls outside them (a weekend, before or after the window, across + midnight). A session transcript cannot tell a person working the task outside the chat from + an agent parked on an approval, so the hour of day only sets the default: the agent decides + from what the pause was waiting on and overturns it with `--keep` or `--drop`, and each + printed cause names the flag. Unattended agent time (a background task that ran through the + gap) counts to the same `--attended-max` limit; a usage-limit or provider-error hold, a + stated break, a wait ended by a harness message (scheduled check, context handoff, resume + message, `/compact`), a gap with no prompt evidence and a gap past the limit are excluded + whatever the hour. `--keep ISO` and `--drop ISO` settle one gap on evidence the + transcript's timing cannot show; `--also FILE...` adds lane transcripts as work trace; `--strict` excludes every gap over `--gap` seconds (default 1800). The figure to bill is the `ACTIVE (bill this)` line, decimal hours to two places, never rounded. -- **Options.** `--gap SECONDS`, `--attended-max HOURS`, `--since ISO`, `--until ISO` (no offset +- **Options.** `--gap SECONDS`, `--attended-max HOURS`, `--work-hours HH:MM-HH:MM` (default + `07:00-18:00`, Monday to Friday only, read in the display zone one timestamp at a time so a + daylight-saving change needs no special case; a value that is not a start before an end + exits 2), `--since ISO`, `--until ISO` (no offset means UTC), `--zone NAME`, `--tz-offset H`, `--also FILE...`, `--keep ISO`, `--drop ISO`, `--strict`, `--time-tool NAME`, and the hook mode `--since-last-entry [--ticket T] [--now ISO] --brief`, which prints one `key=value` line (unlogged minutes since the transcript's last diff --git a/.claude-config/bin/active-minutes b/.claude-config/bin/active-minutes index 96bacc9..3cffa3e 100755 --- a/.claude-config/bin/active-minutes +++ b/.claude-config/bin/active-minutes @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """Billable working minutes from a Claude session transcript. -Two rules set the measure, and the code follows both. +Three rules set the measure, and the code follows all three. A silent stretch of 30 minutes or less between transcript events is working time (the operator reads, takes a portal step, works the same task outside @@ -14,14 +14,25 @@ Two rules set the measure, and the code follows both. (a usage limit, an overnight stop) is not work and needs evidence too: counting a limit hold as work would bill hours in which nothing happened. + A pause is judged by the hour of day. A session transcript cannot tell a person + working the task outside the chat from an agent parked on an approval, so a long + pause that ends with a prompt the operator typed counts when it lies wholly + inside work hours and is assumed idle when any part falls outside them. Both + sides are defaults: the agent decides from what the pause was waiting on. + So every gap over --gap seconds (default 1800) is classified by its evidence and printed with its cause, COUNTED or EXCLUDED: - COUNTED attended pause: the gap ends with a prompt the operator typed, and - lasted no longer than --attended-max hours (default 4). + COUNTED attended pause in work hours: the gap ends with a prompt the operator + typed, lies wholly inside work hours (start and end on the same + weekday date, both inside the window), and lasted no longer than + --attended-max hours (default 4). COUNTED background task: the gap ends with a task notification whose task was launched before the gap began, so agent work ran through it, and the gap lasted no longer than --attended-max hours (default 4). + EXCLUDED pause outside work hours: the same typed-prompt gap, but it starts or + ends outside the window, falls on a weekend, or crosses midnight. The + cause names which end fell outside. Assumed idle. EXCLUDED usage-limit hold or other provider error: an API error record sits just before the gap. EXCLUDED operator-stated break: the last prompt before the gap, or the one @@ -30,12 +41,11 @@ printed with its cause, COUNTED or EXCLUDED: EXCLUDED idle until a harness message: a scheduled check, fleet notice, context handoff, resume message, compact command or cross-thread message ended the gap, so nobody is shown present. - EXCLUDED long attended pause: over --attended-max hours with no other - evidence (overnight, away). The 4 hours sit between the pauses + EXCLUDED long attended pause: inside work hours but over --attended-max hours + with no other evidence (away). The 4 hours sit between the pauses seen in practice (an hour or two while the operator works a console - or a report) and the stops that mean overnight or a weekend. The - line only decides counted versus listed; --keep overrides it with - evidence. + or a report) and the stops that mean away for the day. The line only + decides counted versus listed; --keep overrides it with evidence. EXCLUDED long background-task gap: the same task notification, but the gap is over --attended-max hours and nothing shows work inside it. A launch record is not work: a watcher or an idle poll that exits the @@ -49,11 +59,23 @@ printed with its cause, COUNTED or EXCLUDED: listed (with that said in its cause) until --also lane transcripts or --keep give evidence. +Work hours default to Monday to Friday 07:00 to 18:00, both ends inside (07:00 +and 18:00 exactly are inside), read in the display zone (see below) one timestamp +at a time, so a daylight-saving change moves the window against UTC with no +special case. --work-hours HH:MM-HH:MM changes the hours; the weekdays stay fixed, +and a value that is not a start before an end exits 2. The hour of day only sets +the default for a typed-prompt pause: --keep ISO and --drop ISO are how the agent +records its own call from the record (what the pause was waiting on, what the +operator said on returning), and each printed cause says which flag to use. +Gaps of 30 minutes or less, background-task gaps, holds, stated breaks and +harness-ended gaps do not depend on the hour. + --also FILE... adds other transcripts (a thread's subagent and workflow lane files) as work trace: an event in any of them inside a gap means work was running, so the gap is not one. --keep ISO / --drop ISO force one gap (matched by its UTC start, any prefix such as 2026-11-06T22:10) to COUNTED or EXCLUDED -when the operator gives evidence the transcript cannot see. --strict drops every +when the record or the operator gives evidence the transcript's timing cannot +show. --strict drops every gap over --gap with no classification, for a session with no operator in it. Bill to the minute as decimal hours to the nearest hundredth; never round to a block. @@ -85,7 +107,7 @@ naming a ticket in the other form, ticket number versus numeric id Usage: active-minutes [--gap SECONDS] [--attended-max HOURS] - [--since ISO] [--until ISO] [--zone NAME | --tz-offset H] [--also FILE ...] + [--work-hours HH:MM-HH:MM] [--since ISO] [--until ISO] [--zone NAME | --tz-offset H] [--also FILE ...] [--keep ISO ...] [--drop ISO ...] [--strict] [--time-tool NAME] [--since-last-entry [--ticket T] [--now ISO] --brief] @@ -98,7 +120,8 @@ from zoneinfo import ZoneInfo, ZoneInfoNotFoundError p = argparse.ArgumentParser() p.add_argument("transcript") p.add_argument("--gap", type=float, default=1800.0, help="seconds of silence above which a gap is classified instead of counted (default 1800 = 30 min)") -p.add_argument("--attended-max", type=float, default=4.0, help="hours an attended pause may last and still count without other evidence (default 4)") +p.add_argument("--attended-max", type=float, default=4.0, help="hours an attended pause or a background-task gap may last and still count without other evidence (default 4)") +p.add_argument("--work-hours", default="07:00-18:00", metavar="HH:MM-HH:MM", help="work hours, Monday to Friday, both ends inside, read in the display zone (default 07:00-18:00): a pause a typed prompt ends counts inside them and is assumed idle outside them") p.add_argument("--since", default=None, help="ignore events before this ISO time (no offset = UTC)") p.add_argument("--until", default=None, help="ignore events after this ISO time (no offset = UTC)") zone_group = p.add_mutually_exclusive_group() @@ -126,6 +149,19 @@ if not a.time_tool.strip(): p.error("--time-tool: need a tool name, got an empty one") TIME_TOOL = a.time_tool.strip() +def work_window(text): + # An unusable window must stop the run: guessing one would count or drop + # pauses by hours nobody asked for. Checked before the transcript is read, + # like the zone options, so a bad value exits 2 whatever the transcript holds. + m = re.fullmatch(r"(\d{1,2}):(\d\d)-(\d{1,2}):(\d\d)", text.strip()) + if m: + h1, m1, h2, m2 = map(int, m.groups()) + if h1 < 24 and h2 < 24 and m1 < 60 and m2 < 60 and (h1, m1) < (h2, m2): + return datetime.time(h1, m1), datetime.time(h2, m2) + p.error("--work-hours: need HH:MM-HH:MM with the start before the end, got %r" % text) + +work_start, work_end = work_window(a.work_hours) + def parse(t): # Transcript stamps are UTC. A naive time is read as UTC too, because # comparing naive with aware datetimes raises TypeError. @@ -402,6 +438,11 @@ def api_error_before(i): if ev[j].kind in ("human", "harness", "assistant"): return None return None +def in_work_hours(local): + """True when a local timestamp is on a weekday and inside the window, both + ends inclusive (07:00 and 18:00 exactly are inside).""" + return local.weekday() < 5 and work_start <= local.time() <= work_end + def classify(i): """(counted, cause) for the gap between ev[i] and ev[i+1].""" x, y = ev[i], ev[i + 1] @@ -449,9 +490,27 @@ def classify(i): return True, "background task: launched %s, still running through the gap, finished at its end (unattended agent work counts)" % loc(launched) return False, "task notification ended the gap but its launch is not in this transcript: pass the lane transcripts with --also, or --keep with evidence" return False, "idle until %s (nobody shown present): \"%s\"" % (end.label, one_line(end.text, 50)) + # Only a typed prompt is left here: the assistant and harness ends returned + # above. A transcript cannot tell a person working the task outside the chat + # from an agent parked on an approval, so the hour of day sets the default: + # wholly inside work hours the pause is assumed worked, any part outside them + # (a weekend, before or after the window, across midnight) it is assumed + # idle. Both are defaults, not findings, so each cause names the flag that + # overturns it from the record. Start and end are read in the display zone, + # one timestamp at a time, so a daylight-saving change needs no special case. + quote = one_line(end.text, 50) + ls, le = x.ts.astimezone(zone), y.ts.astimezone(zone) + outside = [(what, t_) for what, t_, ok in (("starts", ls, in_work_hours(ls)), ("ends", le, in_work_hours(le))) if not ok] + if outside or ls.date() != le.date(): + if outside: + parts = ["it %s at %s %s" % (what, t_.strftime("%a %m-%d %H:%M"), t_.strftime("%Z") or zone_label) for what, t_ in outside] + where = parts[0] + ", outside work hours" if len(parts) == 1 else "both ends fall outside work hours (%s)" % ", ".join(parts) + else: + where = "it crosses midnight (%s to %s)" % (loc(x.ts), loc(y.ts)) + return False, 'pause outside work hours, assumed idle: %s; the operator typed next ("%s"); --keep if the record shows the task was being worked (what the pause waited on, what the operator said on returning)' % (where, quote) if d > a.attended_max * 3600: - return False, 'attended pause over %g h with no other evidence (overnight or away): next prompt "%s"' % (a.attended_max, one_line(end.text, 50)) - return True, 'attended pause: the operator typed next ("%s"), assumed working' % one_line(end.text, 50) + return False, 'attended pause in work hours but over %g h with no other evidence: the operator typed next ("%s"); --keep if the record shows the task was being worked' % (a.attended_max, quote) + return True, 'attended pause in work hours, assumed working: the operator typed next ("%s"); --drop if the record shows the time was idle (what the pause waited on, what the operator said on returning)' % quote active = 0.0 gaps = [] # (start, end, seconds, counted, cause) diff --git a/.claude-config/bin/tests/active-minutes-test.py b/.claude-config/bin/tests/active-minutes-test.py index d76003a..195beec 100644 --- a/.claude-config/bin/tests/active-minutes-test.py +++ b/.claude-config/bin/tests/active-minutes-test.py @@ -11,15 +11,20 @@ gets TZ=UTC unless a case says otherwise, so no result depends on the box's own zone. -Three groups. The display zone of the contiguous work blocks: the blocks print on +Four groups. The display zone of the contiguous work blocks: the blocks print on a time entry's Time line, so a wrong offset puts a wrong window on a billed entry. The defect these tests pin: a fixed UTC-4 display read every block an hour late once US Eastern went back to UTC-5 (an 18:05 ET start printed as 19:05). The ACTIVE total never depended on the display zone, and one case holds that so a later change cannot make it start to. The gap rule: every silent stretch over the threshold is classified and printed with its cause, because a billed -figure that silently drops or silently adds an hour is a wrong bill. And the -hook mode (--since-last-entry --brief) with its --time-tool option. +figure that silently drops or silently adds an hour is a wrong bill. The work +hours default for a long pause the operator's prompt ends: a transcript cannot +tell a person working the task outside the chat from an agent parked on an +approval, so the hour of day sets the default (counts wholly inside Monday to +Friday 07:00 to 18:00, assumed idle otherwise) and the agent overturns it with +--keep or --drop. And the hook mode (--since-last-entry --brief) with its +--time-tool option. The two Eastern changes the zone cases straddle (UTC instants): fall back 2026-11-01 06:00Z 02:00 EDT -> 01:00 EST @@ -28,6 +33,7 @@ import json import os import re +import datetime import subprocess import sys import tempfile @@ -135,6 +141,21 @@ def overnight_task_records(): queued("11-06 10:00", notice("toolu_NIGHT")), assistant("11-06 10:01", "It exited.")] +def at_utc(stamp, kind="assistant", text="ok"): + """A record at a full UTC stamp (the helpers above pin 2026 and a month-day).""" + if kind == "human": + return {"timestamp": stamp, "type": "user", "promptSource": "sdk", "message": {"role": "user", "content": text}} + return {"timestamp": stamp, "type": "assistant", "message": {"content": [{"type": "text", "text": text}]}} + + +def pause_records(start, end, text="the portal shows two failures"): + """The agent goes quiet at START and the operator types TEXT at END (both full + UTC stamps), then the agent answers five minutes later. Nothing else sits in + the gap, so it is a typed-prompt pause with no other evidence.""" + reply = datetime.datetime.fromisoformat(end.replace("Z", "+00:00")) + datetime.timedelta(minutes=5) + return [at_utc(start), at_utc(end, "human", text), at_utc(reply.strftime("%Y-%m-%dT%H:%M:%SZ"))] + + class ActiveMinutesTest(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory(prefix="active-minutes-test.") @@ -397,6 +418,8 @@ def test_help_and_docstring_describe_the_zone_default(self): self.assertIn("$TZ", flat) self.assertIn("daylight saving", flat) self.assertIn("--time-tool NAME", flat) + self.assertIn("--work-hours HH:MM-HH:MM", flat) + self.assertIn("default 07:00-18:00", flat) self.assertNotIn("default -4", flat) # the old fixed default self.assertNotIn("US Eastern DST", flat) doc = " ".join(SCRIPT.read_text().split('"""')[1].split()) @@ -461,7 +484,7 @@ def test_an_overnight_gap_over_the_attended_ceiling_is_listed_and_excluded(self) proc = self.run_records(records) [(verdict, mins, _, _, key, cause)] = self.gaps_of(proc) self.assertEqual(verdict, "EXCLUDED") - self.assertIn("over 4 h", cause) + self.assertIn("outside work hours", cause) # 21:00 is after the window; 08:10 the next day is not the cause self.assertIn("Morning, next one", cause) self.assertEqual(self.active_minutes(proc), 30.0) # --keep, given by the operator's evidence, counts exactly that gap. @@ -469,14 +492,16 @@ def test_an_overnight_gap_over_the_attended_ceiling_is_listed_and_excluded(self) self.assertEqual([g[0] for g in self.gaps_of(kept)], ["COUNTED"]) self.assertEqual(self.active_minutes(kept), 30.0 + float(mins)) self.assertIn("kept", self.gaps_of(kept)[0][5]) - # So does a higher ceiling. - self.assertEqual([g[0] for g in self.gaps_of(self.run_records(records, "--attended-max", "12"))], ["COUNTED"]) + # A higher ceiling does not: the hour of day decides this gap, not its length. + self.assertEqual([g[0] for g in self.gaps_of(self.run_records(records, "--attended-max", "12"))], ["EXCLUDED"]) def test_the_ceiling_is_inclusive_at_exactly_four_hours(self): records = [assistant("11-06 10:00"), human("11-06 14:00", "back at it"), assistant("11-06 14:05")] self.assertEqual([g[0] for g in self.gaps_of(self.run_records(records))], ["COUNTED"]) records = [assistant("11-06 10:00"), human("11-06 14:01", "back at it"), assistant("11-06 14:05")] - self.assertEqual([g[0] for g in self.gaps_of(self.run_records(records))], ["EXCLUDED"]) + [(verdict, _, _, _, _, cause)] = self.gaps_of(self.run_records(records)) + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("in work hours but over 4 h", cause) def test_a_stated_break_before_the_gap_is_excluded_and_quoted(self): records = [human("11-06 12:00", "Going to lunch, back in an hour"), assistant("11-06 12:05", "Understood."), @@ -668,6 +693,208 @@ def test_an_old_api_error_does_not_excuse_a_later_gap(self): [(verdict, _, _, _, _, cause)] = self.gaps_of(self.run_records(records)) self.assertTrue(verdict == "COUNTED" and cause.startswith("attended pause")) + # --- work hours: the hour of day sets the default for a typed-prompt pause --- + # + # 2026-11-02 is a Monday, 11-06 a Friday, 11-07 a Saturday, 11-08 a Sunday. + # The runs use TZ=UTC, so the window reads in UTC unless a case names a zone. + + def one_gap(self, start, end, *args, env=None): + proc = self.run_records(pause_records(start, end), *args, env=env) + [gap] = self.gaps_of(proc) + return proc, gap + + def test_a_pause_wholly_inside_work_hours_counts_and_says_how_to_overturn_it(self): + proc, (verdict, mins, _, _, _, cause) = self.one_gap("2026-11-02T10:00:00Z", "2026-11-02T10:50:00Z") + self.assertEqual((verdict, mins), ("COUNTED", "50.0")) + self.assertIn("in work hours", cause) + self.assertIn("--drop", cause) + self.assertIn("the portal shows two failures", cause) + self.assertEqual(self.active_minutes(proc), 55.0) + + def test_a_weekend_pause_is_excluded_and_the_cause_says_why(self): + for day, name in (("2026-11-07", "Sat"), ("2026-11-08", "Sun")): + with self.subTest(day=name): + proc, (verdict, _, _, _, _, cause) = self.one_gap(f"{day}T10:00:00Z", f"{day}T10:50:00Z") + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("outside work hours", cause) + self.assertIn(f"{name} ", cause) + self.assertIn("--keep", cause) + self.assertEqual(self.active_minutes(proc), 5.0) # only the closing reply's own five minutes + + def test_a_pause_that_ends_after_the_window_is_excluded_and_names_the_end(self): + _, (verdict, _, _, _, _, cause) = self.one_gap("2026-11-06T17:40:00Z", "2026-11-06T18:20:00Z") + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("it ends at Fri 11-06 18:20", cause) + self.assertIn("outside work hours", cause) + self.assertNotIn("it starts", cause) + + def test_a_pause_that_starts_before_the_window_is_excluded_and_names_the_start(self): + _, (verdict, _, _, _, _, cause) = self.one_gap("2026-11-06T06:30:00Z", "2026-11-06T07:20:00Z") + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("it starts at Fri 11-06 06:30", cause) + self.assertNotIn("it ends", cause) + + def test_a_pause_with_both_ends_outside_the_window_names_both(self): + _, (verdict, _, _, _, _, cause) = self.one_gap("2026-11-06T05:00:00Z", "2026-11-06T06:00:00Z") + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("both ends fall outside work hours", cause) + self.assertIn("05:00", cause) + self.assertIn("06:00", cause) + + def test_a_pause_that_crosses_midnight_is_excluded_even_when_the_window_covers_both_ends(self): + # Default hours: 23:30 and 00:10 are both outside, so the ends say so. A + # window wide enough to hold both ends still must not count a pause across + # midnight: it is not one working stretch of one day. + _, (verdict, _, _, _, _, cause) = self.one_gap("2026-11-02T23:30:00Z", "2026-11-03T00:10:00Z") + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("outside work hours", cause) + _, (verdict, _, _, _, _, cause) = self.one_gap("2026-11-02T23:30:00Z", "2026-11-03T00:10:00Z", "--work-hours", "00:00-23:59") + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("crosses midnight", cause) + self.assertIn("--keep", cause) + + def test_a_friday_to_monday_pause_is_excluded_even_when_both_ends_are_in_hours(self): + _, (verdict, _, _, _, _, cause) = self.one_gap("2026-11-06T17:00:00Z", "2026-11-09T08:00:00Z", "--attended-max", "100") + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("crosses midnight", cause) + + def test_the_window_boundaries_are_inclusive_at_0700_and_1800(self): + # (start, end, counted): exactly 07:00 and exactly 18:00 are inside; a + # minute (or a second) beyond either is outside. + cases = [("2026-11-06T07:00:00Z", "2026-11-06T07:40:00Z", "COUNTED"), + ("2026-11-06T06:59:00Z", "2026-11-06T07:40:00Z", "EXCLUDED"), + ("2026-11-06T17:20:00Z", "2026-11-06T18:00:00Z", "COUNTED"), + ("2026-11-06T17:20:00Z", "2026-11-06T18:01:00Z", "EXCLUDED"), + ("2026-11-06T17:20:00Z", "2026-11-06T18:00:30Z", "EXCLUDED")] + for start, end, want in cases: + with self.subTest(start=start, end=end): + _, gap = self.one_gap(start, end) + self.assertEqual(gap[0], want) + + def test_the_attended_max_ceiling_still_applies_inside_work_hours(self): + # 07:30 to 11:30 is exactly 4 h and counts; a minute more is listed with the + # ceiling named; a higher --attended-max counts it again. + _, gap = self.one_gap("2026-11-02T07:30:00Z", "2026-11-02T11:30:00Z") + self.assertEqual(gap[0], "COUNTED") + _, (verdict, _, _, _, key, cause) = self.one_gap("2026-11-02T07:30:00Z", "2026-11-02T11:31:00Z") + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("in work hours but over 4 h", cause) + self.assertNotIn("outside work hours", cause) + _, gap = self.one_gap("2026-11-02T07:30:00Z", "2026-11-02T11:31:00Z", "--attended-max", "5") + self.assertEqual(gap[0], "COUNTED") + + def test_keep_counts_an_out_of_hours_pause_and_drop_excludes_an_in_hours_one(self): + out = pause_records("2026-11-07T10:00:00Z", "2026-11-07T10:50:00Z") + [(verdict, _, _, _, key, cause)] = self.gaps_of(self.run_records(out)) + self.assertEqual(verdict, "EXCLUDED") # the default the agent overturns + self.assertIn("outside work hours", cause) + kept = self.run_records(out, "--keep", key) + [(verdict, _, _, _, _, cause)] = self.gaps_of(kept) + self.assertEqual(verdict, "COUNTED") + self.assertIn("--keep", cause) + self.assertEqual(self.active_minutes(kept), 55.0) + inside = pause_records("2026-11-02T10:00:00Z", "2026-11-02T10:50:00Z") + [(verdict, _, _, _, key, cause)] = self.gaps_of(self.run_records(inside)) + self.assertEqual(verdict, "COUNTED") # the default the agent overturns + self.assertIn("in work hours", cause) + dropped = self.run_records(inside, "--drop", key) + [(verdict, _, _, _, _, cause)] = self.gaps_of(dropped) + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("--drop", cause) + self.assertEqual(self.active_minutes(dropped), 5.0) + + def test_work_hours_option_moves_the_window(self): + early = ("2026-11-02T08:00:00Z", "2026-11-02T08:50:00Z") # inside 07:00-18:00 + self.assertEqual(self.one_gap(*early)[1][0], "COUNTED") + self.assertEqual(self.one_gap(*early, "--work-hours", "09:00-17:00")[1][0], "EXCLUDED") + before = ("2026-11-02T06:00:00Z", "2026-11-02T06:50:00Z") # outside 07:00-18:00 + self.assertEqual(self.one_gap(*before)[1][0], "EXCLUDED") + self.assertEqual(self.one_gap(*before, "--work-hours", "05:00-23:00")[1][0], "COUNTED") + # A one-digit hour reads the same as its padded form. + self.assertEqual(self.one_gap("2026-11-02T07:00:00Z", "2026-11-02T07:40:00Z", "--work-hours", "7:00-18:00")[1][0], "COUNTED") + + def test_work_hours_never_reach_the_weekdays(self): + # The option sets hours only: a wide window does not make a Saturday a work day. + _, (verdict, _, _, _, _, cause) = self.one_gap("2026-11-07T10:00:00Z", "2026-11-07T10:50:00Z", "--work-hours", "00:00-23:59") + self.assertEqual(verdict, "EXCLUDED") + self.assertIn("Sat", cause) + + def test_a_bad_work_hours_value_exits_2_with_a_clear_message_even_on_a_short_transcript(self): + for bad in ["25:00-26:00", "18:00-07:00", "07:00-07:00", "07:00", "7-18", "07:00-18:60", "07:00-24:00", "abc", " ", "07:00-18:00-19:00"]: + with self.subTest(bad=bad): + proc = self.run_records(pause_records("2026-11-02T10:00:00Z", "2026-11-02T10:50:00Z"), f"--work-hours={bad}") + self.assertEqual(proc.returncode, 2, proc.stderr) + self.assertIn("--work-hours: need HH:MM-HH:MM with the start before the end", proc.stderr) + self.assertNotIn("ACTIVE", proc.stdout) + # Checked before the transcript is read, like the zone options: exit 2, not the short-transcript exit 1. + short = self.run_records([assistant("11-06 09:00")], "--work-hours=18:00-07:00") + self.assertEqual(short.returncode, 2) + self.assertIn("--work-hours", short.stderr) + + def test_brief_follows_the_same_line(self): + # The hook mode measures with the same classifier, so a weekend pause is + # not counted into the unlogged minutes and a weekday one is. + brief = ("--since-last-entry", "--brief") + inside = self.run_records(pause_records("2026-11-02T10:00:00Z", "2026-11-02T10:50:00Z"), *brief) + self.assertIn("unlogged_min=55.0", inside.stdout) + weekend = self.run_records(pause_records("2026-11-07T10:00:00Z", "2026-11-07T10:50:00Z"), *brief) + self.assertIn("unlogged_min=5.0", weekend.stdout) + moved = self.run_records(pause_records("2026-11-07T10:00:00Z", "2026-11-07T10:50:00Z"), *brief, "--keep", "2026-11-07T10:00") + self.assertIn("unlogged_min=55.0", moved.stdout) + late = self.run_records(pause_records("2026-11-02T17:40:00Z", "2026-11-02T18:20:00Z"), *brief, "--work-hours", "07:00-19:00") + self.assertIn("unlogged_min=45.0", late.stdout) + + def test_other_gap_causes_do_not_depend_on_the_hour_of_day(self): + # A background task that ran, a limit hold, a stated break, a harness-ended + # gap and --strict behave the same on a Saturday night as on a Monday morning. + sat = "2026-11-07T" + task = [at_utc(sat + "02:00:00Z", "human", "run the sweep"), launch("11-07 02:05", "toolu_SAT"), at_utc(sat + "02:06:00Z"), + queued("11-07 03:10", notice("toolu_SAT")), at_utc(sat + "03:11:00Z")] + [(verdict, _, _, _, _, cause)] = self.gaps_of(self.run_records(task)) + self.assertTrue(verdict == "COUNTED" and cause.startswith("background task"), cause) + hold = [at_utc(sat + "02:00:00Z"), api_error("11-07 02:05"), at_utc(sat + "04:00:00Z", "human", "continue"), at_utc(sat + "04:05:00Z")] + [(verdict, _, _, _, _, cause)] = self.gaps_of(self.run_records(hold)) + self.assertTrue(verdict == "EXCLUDED" and cause.startswith("usage-limit hold"), cause) + stated = [at_utc(sat + "02:00:00Z", "human", "going to lunch"), at_utc(sat + "02:05:00Z"), at_utc(sat + "03:20:00Z", "human", "ok"), at_utc(sat + "03:25:00Z")] + [(verdict, _, _, _, _, cause)] = self.gaps_of(self.run_records(stated)) + self.assertTrue(verdict == "EXCLUDED" and "operator-stated break" in cause, cause) + strict = self.run_records(pause_records("2026-11-02T10:00:00Z", "2026-11-02T10:50:00Z"), "--strict") + [(verdict, _, _, _, _, cause)] = self.gaps_of(strict) + self.assertTrue(verdict == "EXCLUDED" and "--strict" in cause, cause) + + # --- work hours and daylight saving: the window is read per timestamp -------- + + def test_the_window_follows_daylight_saving_either_side_of_the_fall_change(self): + # After the change (EST, UTC-5): Monday 21:50Z to 22:50Z is 16:50 to 17:50, + # inside. Read at the old EDT offset it would end at 18:50, outside. + _, (verdict, _, _, _, _, cause) = self.one_gap("2026-11-02T21:50:00Z", "2026-11-02T22:50:00Z", *NY) + self.assertEqual(verdict, "COUNTED", cause) + # Before the change (EDT, UTC-4): Friday 21:40Z to 22:20Z is 17:40 to 18:20, + # so it ends outside. Read at EST it would be 16:40 to 17:20, inside. + _, (verdict, _, _, _, _, cause) = self.one_gap("2026-10-30T21:40:00Z", "2026-10-30T22:20:00Z", *NY) + self.assertEqual(verdict, "EXCLUDED", cause) + self.assertIn("it ends at Fri 10-30 18:20 EDT", cause) + + def test_the_window_follows_daylight_saving_across_the_spring_change(self): + # After the change (EDT): Monday 11:00Z is 07:00, the window's first minute. + # Read at the old EST offset it would be 06:00, outside. + _, (verdict, _, _, _, _, cause) = self.one_gap("2027-03-15T11:00:00Z", "2027-03-15T11:40:00Z", *NY) + self.assertEqual(verdict, "COUNTED", cause) + # Before the change (EST): Friday 11:00Z is 06:00, outside. + _, (verdict, _, _, _, _, cause) = self.one_gap("2027-03-12T11:00:00Z", "2027-03-12T11:40:00Z", *NY) + self.assertEqual(verdict, "EXCLUDED", cause) + self.assertIn("it starts at Fri 03-12 06:00 EST", cause) + + def test_the_window_is_read_in_the_default_zone_and_in_a_fixed_offset(self): + # Same instants as the first fall-change case, reached through TZ and through --tz-offset. + start, end = "2026-11-02T21:50:00Z", "2026-11-02T22:50:00Z" + self.assertEqual(self.one_gap(start, end, env=base_env(TZ="America/New_York"))[1][0], "COUNTED") + self.assertEqual(self.one_gap(start, end, "--tz-offset", "-5")[1][0], "COUNTED") + # A fixed -4 is a deliberate override and gives the old EDT reading: 17:50 to 18:50. + self.assertEqual(self.one_gap(start, end, "--tz-offset", "-4")[1][0], "EXCLUDED") + # With no zone given at all the window reads in UTC (TZ=UTC): 21:50 is after 18:00. + self.assertEqual(self.one_gap(start, end)[1][0], "EXCLUDED") + # --- hook mode: --since-last-entry --brief --------------------------------- def test_brief_reports_unlogged_minutes_since_the_last_time_create(self): @@ -808,7 +1035,7 @@ def test_docstring_names_the_rules_and_the_hold_rule(self): text = SCRIPT.read_text() doc = " ".join(text.split('"""')[1].split()) for needle in ("30 minutes or less", "Unattended agent time counts like attended time", "usage-limit hold", - "--keep", "--also", "--strict"): + "--keep", "--also", "--strict", "work hours", "--work-hours", "inside work hours"): self.assertIn(needle, doc, needle) self.assertNotIn(chr(0x2014), text) # no em or en dashes self.assertNotIn(chr(0x2013), text) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90071db..bf2f651 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,26 @@ Notable changes to OPS, newest first. Format: the date, then what changed and wh matters. This file starts fresh at the public release; the harness's private prehistory is deliberately not part of it. +## 2026-11-27: `active-minutes` judges a long pause by the hour of day + +- **A pause over 30 minutes that ends with a prompt the Operator typed now counts only inside + work hours.** A session transcript cannot tell a person working the task outside the chat + from an agent parked on an approval, and counting every such pause up to `--attended-max` + billed approval waits as work. Now a pause that lies wholly inside work hours (Monday to + Friday 07:00 to 18:00, both ends inclusive, start and end on the same date) counts, and one + with any part outside them (a weekend, before or after the window, across midnight) is + assumed idle. Both are defaults the agent overturns from the record with `--keep` and + `--drop`, and each printed cause names the flag. New `--work-hours HH:MM-HH:MM` moves the + hours (weekdays stay fixed; a bad value exits 2); the window is read in the display zone per + timestamp, so a daylight-saving change needs no special case. Gaps of 30 minutes or less, + background-task gaps, holds, stated breaks, harness-ended gaps, `--also`, `--strict` and + `--brief` behave as before. +- **Tests**: 18 new cases in `bin/tests/active-minutes-test.py` (in-hours, weekend, before and + after the window, midnight, the 07:00 and 18:00 boundaries, the ceiling, `--keep` and `--drop`, + `--work-hours`, `--brief`, daylight saving); two existing cases changed because the rule + changes their cause and what a higher `--attended-max` does. `bin/README.md` and + `CONTEXT/work-tracking.md` state the rule. + ## 2026-11-24: shared systems answer to their owners - **`core.md` says a teammate's go is not a design ruling on a system they do not own.** On a team diff --git a/CONTEXT/work-tracking.md b/CONTEXT/work-tracking.md index 8f36dda..2ad81cd 100644 --- a/CONTEXT/work-tracking.md +++ b/CONTEXT/work-tracking.md @@ -45,6 +45,12 @@ - **"Already logged today" check:** before proposing anything, ask my time system what's already recorded for today (tool `psa__time_overview` for my user id) so a day captured elsewhere never gets double-logged. +- **Measuring the time:** `active-minutes` computes it from the session transcript. A pause + over 30 minutes that ends with a prompt the Operator typed counts when it lies wholly inside + work hours (Monday to Friday 07:00 to 18:00, `--work-hours` changes the hours) and is assumed + idle when any part falls outside them. Both are defaults, because a transcript cannot tell a + person working the task outside the chat from an agent parked on an approval: the agent + decides from what the pause was waiting on and records the call with `--keep` or `--drop`. - **Billable vs not:** client work is billable; internal/overhead work is logged for tracking but not billed; personal-side work takes no entry, so note "personal, skipped" and move on. Some work is genuinely non-trackable, so ask,