Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions group-creation-tool/spec-review-round-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Specification review – round 1
==============================

Prompting Claude Opus 5.5, high effort, with `Review group-creation-tool/spec.md
against the project description in the course notes. Report contradictions,
undefined cases, and statements an implementer could read two ways. Keep the
spec short; do not add features.` produced the following findings.
Strikethrough marks findings we judged incorrect or irrelevant.

1. **HIGH**: `T` (mean score of the other group members) is undefined for a
group of one, which occurs when `m = 2` and `n` is odd (`n = 5` gives sizes
2, 2, 1). *Fixed: `T = 0` in a group of one.*
2. **HIGH**: `w` and `c` could only be changed until the survey closed, but the
organizer normally closes the survey first and forms groups afterwards, so
the settings could never be tuned on final data. *Fixed: `m`, `w` and `c`
can change at any time; closing only stops submissions.*
3. **MEDIUM**: Step 2 said "maximize quality", but the teammate part of
satisfaction does not depend on projects and the tie-break cannot be
expressed as an assignment problem. *Fixed: step 2 maximizes the sum of
`w · P`.*
4. **MEDIUM**: CSV group names collide when the project list holds both
`htmd fuzzer` and `htmd fuzzer 2`. *Fixed: such project lists are rejected.*
5. **MEDIUM**: The numbering of a project's extra groups depended on "the order
the algorithm produced them", which is not defined. *Fixed: numbered by each
group's alphabetically first userid.*
6. **MEDIUM**: "Only the organizer can view the survey" contradicts students
opening it through the link. *Fixed: only the organizer can change settings,
close the survey, or see submissions and results.*
7. **LOW**: The organizer has no way to see who has not answered yet before
closing. *Fixed: the organizer sees which students have submitted.*
8. **LOW**: The quality test required the heuristic's result to "equal the
optimum", although several assignments can share the optimal quality.
*Fixed: compare quality only.*
9. ~~**MEDIUM**: Replace the local search with an integer linear program (PuLP)
to guarantee the optimum.~~ Rejected: adds a solver dependency and
overspecifies; local search is adequate for class-sized rosters (design
decision 4).
10. ~~**LOW**: Notify students by email when a survey opens or closes.~~
Rejected: new feature, out of scope.

8 findings applied, 2 rejected.
19 changes: 19 additions & 0 deletions group-creation-tool/spec-review-round-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Specification review – round 2
==============================

Same prompt and model as round 1. Strikethrough marks findings we judged
incorrect or irrelevant.

1. **MEDIUM**: Steps 2 and 3 could cycle. A swap in step 3 may improve only the
tie-break (the least satisfied student), after which step 2 may pick another
project assignment with the same sum but a worse tie-break, which the next
swap undoes, and so on. *Fixed: step 2 replaces the current project
assignment only if the result is better, so every change strictly improves
the assignment and the search terminates.*
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
2. ~~**LOW**: Specify that a swap is evaluated from the change in the two
affected groups only, to keep a pass fast.~~ Rejected: an implementation
detail that does not change behavior.
3. ~~**LOW**: Let students mark another student as a required teammate.~~
Rejected: new feature, out of scope.

1 finding applied, 2 rejected.
15 changes: 15 additions & 0 deletions group-creation-tool/spec-review-round-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Specification review – round 3
==============================

Same prompt and model as round 1. Strikethrough marks findings we judged
incorrect or irrelevant.

1. ~~**LOW**: Naming SortableJS in Implementation overspecifies the user
interface.~~ Rejected: the section only records the chosen stack, which the
team agreed on; behavior is specified elsewhere.
2. ~~**LOW**: The 10⁻⁹ comparison tolerance is unnecessary because scores are
rational.~~ Rejected: they are computed as floating-point numbers, so the
tolerance keeps comparisons (and therefore results) stable.

No findings applied. Only trivial or contradictory findings remain, so the
review stopped here.
29 changes: 29 additions & 0 deletions group-creation-tool/spec-review-round-4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Specification review – round 4
==============================

Findings from the automated CodeRabbit review on pull request
[#162](https://github.com/bjones1/CodeChat_Editor/pull/162), each verified by
prompting Claude Opus 5.5, high effort, with `Check each of these review
comments against group-creation-tool/spec.md; say whether it is correct and
propose the smallest fix.` Strikethrough marks findings we judged incorrect or
irrelevant.

1. **HIGH**: With a 10⁻⁹ tolerance, "better" is not transitive: totals that tie
pairwise within the tolerance can still form a cycle A > B > C > A, so the
claim that the search terminates did not hold. *Fixed: quality and the least
satisfaction are rounded to 9 decimal places before comparing, which makes
"better" a strict order.*
2. **HIGH**: A student whose sign-in name changed was rejected as "not on the
roster" before the `tid`/`oid` match could recognize them. *Fixed: the app
looks for an entry bound to the `tid`/`oid` first, and matches the userid
only for unbound entries.*
3. **MEDIUM**: "Every student gets the same total influence" is false: one
preferred item totals 1, two total 1.5. *Fixed: the text now says scores
share the same scale, from 1 for the top item to −1 for the least wanted.*
4. **MEDIUM**: Step 2 maximizes only the sum, so among equal-sum project
assignments it may miss the one with the better tie-break. *Fixed by stating
it: the solver's choice is used, since the search is a heuristic and does not
promise the best possible result. Making step 2 tie-break-aware would need a
different solver for little benefit.*

4 findings applied.
16 changes: 16 additions & 0 deletions group-creation-tool/spec-review-round-5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Specification review – round 5
==============================

The automated CodeRabbit review of round 4's changes on pull request
[#162](https://github.com/bjones1/CodeChat_Editor/pull/162) reported one
finding, verified with the same prompt and model as round 4.

1. **LOW**: "Floating-point noise never decides a comparison" overstated the
rounding rule: two nearly equal values on either side of a rounding boundary
still round differently. *Fixed: the spec now says rounding removes almost
all noise and that a boundary case can only tip the choice between two nearly
equal assignments; the strict order, and therefore termination, is
unaffected.*

1 finding applied. Only a trivial wording issue remained, so the review stopped
here.
217 changes: 217 additions & 0 deletions group-creation-tool/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
Project group creation tool
===========================

A web app that collects students' project and teammate preferences and forms
project groups that satisfy them as well as possible. The result is a `.csv`
file used as input by the team evaluation tool.

Roles and sign-in
-----------------

There are two roles. An **organizer** creates and manages a survey. A
**student** submits preferences for a survey.

Everyone signs in with a Microsoft Entra ID (Teams) account using the OpenID
Connect authorization-code flow (`msal.ConfidentialClientApplication`), as
described in [Signing in with Microsoft Teams accounts](https://github.com/bjones1/literate-programming-book/blob/main/project_notes/teams_sign_in.md).
A signed-in user is identified by the pair `tid`/`oid` from the ID token. The
`name` claim is used only for display.

Any signed-in user may create a survey and becomes its organizer. Only the
organizer of a survey can change its settings, close it, or see submissions and
results.

Surveys
-------

To create a survey, the organizer provides:

* **Roster**: the students' userids, one per line. A userid is the account's
sign-in name (`preferred_username`, such as `jdoe@school.edu`) and is compared
case-insensitively. Blank lines and duplicates are dropped.
* **Projects**: project names, one per line. Blank lines and duplicates are
dropped. A name equal to another project's name followed by a space and a
number (`htmd fuzzer 2` beside `htmd fuzzer`) is rejected, since the CSV uses
such names for extra groups.
* **Group size** `m`: an integer of at least 2.
* **Project weight** `w`: a number from 0 to 1, default 0.5. The teammate
weight is `1 − w`.
* **Groups per project** `c`: the most groups one project may have. It defaults
to the smallest value that makes grouping possible (see Group sizes).

The app rejects a survey with fewer than 2 students or fewer than 1 project.
After creation, the roster and project list cannot be changed; the organizer
creates a new survey instead. `m`, `w` and `c` can be changed at any time.

Creating a survey produces a link containing a random, unguessable token. The
organizer posts this link and sees which students have submitted so far.

A survey is **open** until the organizer closes it. Closing is final and only
stops students from submitting; the organizer can still form groups.

Student flow
------------

A student follows the link and signs in. The app first looks for a roster entry
already bound to the account's `tid`/`oid`. If there is none, it looks for an
unbound entry whose userid matches the account's sign-in name and binds that
entry to the `tid`/`oid`. If neither exists, the app shows an error and stores
nothing. Because bound entries are found first, a later change of the sign-in
name does not lock the student out.

The student then sees two lists: the survey's projects, and the other students
on the roster (by display name if they have signed in, otherwise by userid).
For each list, every item is in exactly one of three categories:

* **Preferred**, ranked from most to least wanted.
* **Unspecified**, not ranked. All items start here.
* **Not preferred**, ranked from least to most unwanted (the last item is the
least wanted).

The student clicks **Submit** to save both lists. While the survey is open, the
student can return and change them; each submission replaces the previous one.
After the survey closes, the student can only view what was submitted. A
student never sees another student's preferences.

Forming groups
--------------

The organizer can form groups at any time from the submissions saved so far.
Students who have not submitted are treated as having every item unspecified.

### Group sizes

With `n` students and group size `m`, the app forms `g = ⌈n / m⌉` groups. Their
sizes differ by at most one: `n mod g` groups have `⌈n / g⌉` students and the
rest have `⌊n / g⌋`. Each group is assigned one project, and no project is
assigned to more than `c` groups. Grouping requires `k · c ≥ g` for `k`
projects, so `c` defaults to `⌈g / k⌉`; a smaller value is rejected. Projects
may end up with no group.

### Score

A student's ranking of a list is turned into a score for each item in the
list:

* the item at position `i` (from 1) of `a` preferred items scores
`(a − i + 1) / a`, from 1 for the top item down to `1 / a`;
* an unspecified item scores 0;
* the item at position `j` (from 1) of `b` not-preferred items scores `−j / b`,
from `−1 / b` down to −1 for the least wanted item.

Dividing by the list length puts every student's scores on the same scale: the
top preferred item always scores 1 and the least wanted item always scores −1,
however many items the student ranks.

A student's **satisfaction** with an assignment is

`w · P + (1 − w) · T`

where `P` is the student's score for the group's project and `T` is the mean of
the student's scores for the other members of the group, or 0 in a group of one
(possible when `m = 2` and `n` is odd).

The **quality** of an assignment is the sum of all students' satisfaction. One
assignment is **better** than another if its quality is higher, or if the
qualities are equal and its least satisfied student is more satisfied. Both
values are rounded to 9 decimal places before comparing, so "better" stays a
strict order: no chain of better assignments can lead back to where it started.
Rounding removes almost all floating-point noise; a value lying on a rounding
boundary can still tip the choice between two nearly equal assignments, but
never prevents the search from ending.

### Algorithm

Finding the best assignment is NP-hard, so the app uses a deterministic local
search:

1. **Start.** Shuffle the roster with a pseudo-random generator seeded from the
survey id and the restart number, then fill the groups in that order, largest
groups first.
2. **Assign projects.** Given the groups, assign projects to maximize the sum of
`w · P` over all students; the teammate part does not depend on projects.
This is a weighted assignment problem with `c` copies of each project, solved
exactly with `scipy.optimize.linear_sum_assignment`. When several project
assignments reach the same sum, the solver's choice is used without applying
the tie-break; the search is a heuristic and does not promise the best
possible result. The new project assignment replaces the current one only if
it makes the assignment better, so steps 2 and 3 cannot undo each other.
3. **Improve members.** With projects fixed, consider every swap of two
students in different groups, in roster order. Apply a swap whenever it
makes the assignment better, and repeat until a full pass makes no swap.
4. Repeat steps 2–3 until neither changes the assignment.
5. Run steps 1–4 for 20 restarts and keep the best result.

The same inputs (submissions, `m`, `w`, `c`) always produce the same groups.

### Result

The organizer sees each group with its project and members, each student's
satisfaction, and how many of each student's not-preferred items (projects or
teammates) they received. The organizer can download the result as a `.csv`
file.

CSV format
----------

UTF-8, comma-separated, quoted as in RFC 4180, with a header row:

```
userid,team
jdoe@school.edu,YAML support
asmith@school.edu,htmd fuzzer 2
```

There is one row per student, sorted by team and then userid. `team` is the
project name for the project's first group; further groups of the same project
are named `<project> 2`, `<project> 3`, and so on. A project's groups are
numbered in order of the alphabetically first userid in each group.

Design decisions
----------------

1. **The organizer sets the group size.** The assignment's "≈ 1/n" group size
cannot be meant literally. A target size, rather than one group per project,
keeps groups at a workable size when there are many projects, and allows a
popular project to have several groups, as in past courses.
2. **"Not preferred" is a penalty, not a ban.** The assignment calls it a
preference. A ban could make grouping impossible (for example, when most
students reject one project and every project must be used); a penalty
always produces a result, and the result view shows each violation.
3. **Sum of satisfaction, ties by the worst-off student.** Maximizing the sum is
simple and predictable; the tie-break prevents needlessly sacrificing one
student when it costs nothing.
4. **Local search instead of an exact solver.** The problem is NP-hard. Swaps
plus an exact project assignment give good results for class-sized rosters
(up to a few hundred students) in seconds, with no solver dependency beyond
SciPy.
5. **Determinism.** Seeding from the survey id makes results reproducible, so
forming groups twice from the same data never surprises the organizer.
6. **Identity by `tid`/`oid`.** Names and emails can change; `tid`/`oid` cannot.
The roster uses sign-in names only because that is what an organizer knows.
7. **Fixed roster and projects.** Editing them after students have answered
would invalidate rankings; creating a new survey is simpler and safe.

Implementation
--------------

Python 3.12 with FastAPI, server-rendered Jinja2 pages, SortableJS for
drag-and-drop ranking, `msal` for sign-in, SciPy for the assignment step, and
SQLite (through the standard `sqlite3` module) for storage. The code lives in
`group-creation-tool/`; the Entra client id, secret and redirect URI come from
environment variables.

Testing
-------

1. **Score**: unit tests for each category, list lengths 1 to 5, and the
satisfaction formula, including `w = 0`, `w = 1` and a group of one.
2. **Group sizes**: for all `n ≤ 30` and `m ≤ 6`, sizes sum to `n`, differ by at
most one, and none exceeds `m`.
3. **Algorithm invariants**: every student is in exactly one group, no project
exceeds `c` groups, and repeated runs give identical output.
4. **Algorithm quality**: on fixed random instances with `n ≤ 8`, the result's
quality equals the optimum found by exhaustive search.
5. **Sign-in**: with MSAL mocked, an unlisted userid is rejected, a listed one
is bound to its `tid`/`oid`, and a renamed account is still recognized.
6. **CSV**: the export of a known result matches an expected file exactly.
Loading