@dsmm/dsmm 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import type { Context } from "@deepseek-ai/cordis";
2
2
  import { TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
3
- import type { ProfileReadResult, ProfileSaveRequest, ProfileSelectRequest, ProfileSnapshot, SessionProfileSelectRequest, SessionProfileSnapshot } from "./profile-types.js";
3
+ import type { ProfileReadResult, ProfileSaveRequest, ProfileSelectRequest, ProfileSnapshot, SessionModeSelectRequest, SessionProfileSelectRequest, SessionProfileSnapshot } from "./profile-types.js";
4
4
  import type { DshAgent } from "./dsh-types.js";
5
5
  export interface ProfilesBackend {
6
6
  describe(): Promise<ProfileSnapshot>;
@@ -9,6 +9,7 @@ export interface ProfilesBackend {
9
9
  select(request: ProfileSelectRequest): Promise<ProfileSnapshot>;
10
10
  getSession(agent: DshAgent): Promise<SessionProfileSnapshot>;
11
11
  selectSession(request: SessionProfileSelectRequest, capturedAgent: DshAgent, signal?: AbortSignal, assertAuthority?: () => void): Promise<SessionProfileSnapshot>;
12
+ selectMode?(request: SessionModeSelectRequest, capturedAgent: DshAgent, signal?: AbortSignal, assertAuthority?: () => void): Promise<SessionProfileSnapshot>;
12
13
  }
13
14
  /** Business service addressed only by native Typert Gateway invocations. */
14
15
  export declare class DsmmProfilesHost extends TypertRemoteService {
@@ -24,6 +25,7 @@ export declare class DsmmProfilesHost extends TypertRemoteService {
24
25
  private invokeSession;
25
26
  describeSession(sessionId: string): Promise<SessionProfileSnapshot>;
26
27
  selectSession(sessionId: string, request: SessionProfileSelectRequest): Promise<SessionProfileSnapshot>;
28
+ selectMode(sessionId: string, request: SessionModeSelectRequest): Promise<SessionProfileSnapshot>;
27
29
  }
28
30
  declare module "@deepseek-ai/cordis" {
29
31
  interface Context {
@@ -47,6 +47,7 @@ let DsmmProfilesHost = (() => {
47
47
  let _select_decorators;
48
48
  let _describeSession_decorators;
49
49
  let _selectSession_decorators;
50
+ let _selectMode_decorators;
50
51
  return class DsmmProfilesHost extends _classSuper {
51
52
  static {
52
53
  const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
@@ -56,12 +57,14 @@ let DsmmProfilesHost = (() => {
56
57
  _select_decorators = [Remote];
57
58
  _describeSession_decorators = [Remote];
58
59
  _selectSession_decorators = [Remote];
60
+ _selectMode_decorators = [Remote];
59
61
  __esDecorate(this, null, _describe_decorators, { kind: "method", name: "describe", static: false, private: false, access: { has: obj => "describe" in obj, get: obj => obj.describe }, metadata: _metadata }, null, _instanceExtraInitializers);
60
62
  __esDecorate(this, null, _read_decorators, { kind: "method", name: "read", static: false, private: false, access: { has: obj => "read" in obj, get: obj => obj.read }, metadata: _metadata }, null, _instanceExtraInitializers);
61
63
  __esDecorate(this, null, _save_decorators, { kind: "method", name: "save", static: false, private: false, access: { has: obj => "save" in obj, get: obj => obj.save }, metadata: _metadata }, null, _instanceExtraInitializers);
62
64
  __esDecorate(this, null, _select_decorators, { kind: "method", name: "select", static: false, private: false, access: { has: obj => "select" in obj, get: obj => obj.select }, metadata: _metadata }, null, _instanceExtraInitializers);
63
65
  __esDecorate(this, null, _describeSession_decorators, { kind: "method", name: "describeSession", static: false, private: false, access: { has: obj => "describeSession" in obj, get: obj => obj.describeSession }, metadata: _metadata }, null, _instanceExtraInitializers);
64
66
  __esDecorate(this, null, _selectSession_decorators, { kind: "method", name: "selectSession", static: false, private: false, access: { has: obj => "selectSession" in obj, get: obj => obj.selectSession }, metadata: _metadata }, null, _instanceExtraInitializers);
67
+ __esDecorate(this, null, _selectMode_decorators, { kind: "method", name: "selectMode", static: false, private: false, access: { has: obj => "selectMode" in obj, get: obj => obj.selectMode }, metadata: _metadata }, null, _instanceExtraInitializers);
65
68
  if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
66
69
  }
67
70
  backend = __runInitializers(this, _instanceExtraInitializers);
@@ -175,6 +178,16 @@ let DsmmProfilesHost = (() => {
175
178
  }
176
179
  return this.invokeSession(sessionId, (agent, signal, assertAuthority) => this.backend.selectSession(request, agent, signal, assertAuthority));
177
180
  }
181
+ selectMode(sessionId, request) {
182
+ if (request.sessionId !== sessionId) {
183
+ return this.invoke(async () => { throw new DsmmProfileError("validation", "The requested session identities must match.", "sessionId"); });
184
+ }
185
+ return this.invokeSession(sessionId, (agent, signal, assertAuthority) => {
186
+ if (this.backend.selectMode === undefined)
187
+ throw new DsmmProfileError("unavailable", "Session mode selection is unavailable.");
188
+ return this.backend.selectMode(request, agent, signal, assertAuthority);
189
+ });
190
+ }
178
191
  };
179
192
  })();
180
193
  export { DsmmProfilesHost };
@@ -1,8 +1,9 @@
1
1
  import type { DshAgent, DshContext } from "./dsh-types.js";
2
2
  import type { LoadedProfileSelection, LoadedSessionProfileSelection, SessionProfileCommit } from "./profile-store.js";
3
- import type { ProfileReadResult, ProfileSaveRequest, ProfileSelectRequest, ProfileSelectionState, ProfileSnapshot, SessionProfileSelectRequest, SessionProfileSnapshot } from "./profile-types.js";
3
+ import type { ProfileReadResult, ProfileSaveRequest, ProfileSelectRequest, ProfileSelectionState, ProfileSnapshot, SessionModeSelectRequest, SessionProfileSelectRequest, SessionProfileSnapshot } from "./profile-types.js";
4
4
  import type { DsmmProfileDocument } from "./profiles.js";
5
5
  import type { DsmmProfileAdmission, DsmmSettings, DsmmSettingsGetter } from "./settings.js";
6
+ import { DeepworkModeController } from "./state.js";
6
7
  export interface DsmmProfileRuntimeStore {
7
8
  loadSelection(): Promise<LoadedProfileSelection>;
8
9
  describe(): Promise<ProfileSnapshot>;
@@ -21,6 +22,7 @@ export interface DsmmProfileRuntimeStore {
21
22
  export interface DsmmProfileRuntimeOptions {
22
23
  /** Trusted test/integration seam, never a wire-supplied path or callback. */
23
24
  validateCandidate?: (settings: DsmmSettings) => void | Promise<void>;
25
+ modeController?: DeepworkModeController;
24
26
  }
25
27
  interface AdmittedProfile extends ProfileSelectionState, DsmmProfileAdmission {
26
28
  }
@@ -38,11 +40,13 @@ export declare class DsmmProfileRuntime {
38
40
  private readonly switching;
39
41
  private disposed;
40
42
  private selectionQueue;
43
+ private readonly mode;
41
44
  constructor(ctx: DshContext, baseline: DsmmSettings, store: DsmmProfileRuntimeStore, options?: DsmmProfileRuntimeOptions);
42
45
  initialize(): Promise<void>;
43
46
  admission(agent?: DshAgent): AdmittedProfile;
44
47
  getSession(agent: DshAgent): Promise<SessionProfileSnapshot>;
45
48
  /** The fourth argument is trusted native caller authority, never wire data. */
49
+ selectMode(request: SessionModeSelectRequest, agent: DshAgent, signal?: AbortSignal, assertAuthority?: () => void): Promise<SessionProfileSnapshot>;
46
50
  selectSession(request: SessionProfileSelectRequest, agent: DshAgent, signal?: AbortSignal, assertAuthority?: () => void): Promise<SessionProfileSnapshot>;
47
51
  describe(): Promise<ProfileSnapshot>;
48
52
  read(id: string): Promise<ProfileReadResult>;
@@ -59,6 +63,6 @@ export declare class DsmmProfileRuntime {
59
63
  private validate;
60
64
  }
61
65
  /** No guessed home fallback: native deployment context owns this directory. */
62
- export declare function createProfileRuntime(ctx: DshContext, baseline: DsmmSettings): Promise<DsmmProfileRuntime>;
66
+ export declare function createProfileRuntime(ctx: DshContext, baseline: DsmmSettings, options?: DsmmProfileRuntimeOptions): Promise<DsmmProfileRuntime>;
63
67
  export {};
64
68
  //# sourceMappingURL=profile-runtime.d.ts.map
@@ -5,7 +5,7 @@ import { DsmmProfileError, resolveProfileSettings } from "./profiles.js";
5
5
  import { DSMM_ROLES, isRootRole } from "./roles.js";
6
6
  import { roleRouteRuntimeState, selectInitialModelRoute } from "./role-routing.js";
7
7
  import { resolveEffectiveDsmmRole, sessionEvents } from "./session-scope.js";
8
- import { isDeepworkActive } from "./state.js";
8
+ import { DeepworkModeController, hasOpenTurn } from "./state.js";
9
9
  import { resolveRoleRuntimePolicy } from "./settings.js";
10
10
  function immutableSettings(settings) {
11
11
  const copy = structuredClone(settings);
@@ -33,11 +33,13 @@ export class DsmmProfileRuntime {
33
33
  switching = new WeakSet();
34
34
  disposed = false;
35
35
  selectionQueue = Promise.resolve();
36
+ mode;
36
37
  constructor(ctx, baseline, store, options = {}) {
37
38
  this.ctx = ctx;
38
39
  this.store = store;
39
40
  this.options = options;
40
41
  this.baseline = immutableSettings(baseline);
42
+ this.mode = options.modeController ?? new DeepworkModeController({ get: (name) => ctx.get?.(name) });
41
43
  this.current = this.prepare(null, { selectedId: null, appliedRevision: null, selectionRevision: "absent" });
42
44
  this.getSettings.admission = (agent) => this.admission(agent);
43
45
  }
@@ -68,14 +70,69 @@ export class DsmmProfileRuntime {
68
70
  throw new DsmmProfileError("conflict", "The session choice changed outside this Host. Its admitted policy was retained; refresh or resume explicitly.");
69
71
  }
70
72
  const reason = this.switching.has(agent) ? "maintenance" : agent.status === "running" ? "busy" : agent.runMaintenance === undefined ? "unavailable" : undefined;
71
- const role = resolveEffectiveDsmmRole(agent, admitted.settings, isDeepworkActive(sessionEvents(agent.session), admitted.settings.defaultActive));
73
+ const role = resolveEffectiveDsmmRole(agent, admitted.settings, this.mode.active(agent, admitted.settings.defaultActive));
72
74
  const profileModel = this.declaredProfileModel(admitted.settings, role);
73
75
  return { sessionId: agent.id, globalDefault: selectionState(this.current), selection: selectionState(disk), admittedSelection: selectionState(admitted), scope: admitted.scope,
74
- rolePolicy: roleRouteRuntimeState(agent, admitted.settings, role, admitted.epoch),
76
+ rolePolicy: roleRouteRuntimeState(agent, admitted.settings, role, admitted.epoch), deepwork: this.mode.describe(agent, admitted.settings.defaultActive),
75
77
  ...(profileModel === undefined ? {} : { profileModel }),
76
78
  admissionEpoch: admitted.epoch, switchAllowed: reason === undefined, ...(reason === undefined ? {} : { switchUnavailableReason: reason }) };
77
79
  }
78
80
  /** The fourth argument is trusted native caller authority, never wire data. */
81
+ async selectMode(request, agent, signal, assertAuthority) {
82
+ this.assertRoot(agent);
83
+ if (request.sessionId !== agent.id)
84
+ throw new DsmmProfileError("not-owned", "The native session does not match the captured Agent.");
85
+ const admitted = this.bind(agent);
86
+ const assertCurrent = (maintenanceSignal) => {
87
+ this.assertRoot(agent);
88
+ if (signal?.aborted || maintenanceSignal?.aborted)
89
+ throw new DsmmProfileError("cancelled", "The Deepwork change was cancelled.");
90
+ const mode = this.mode.describe(agent, admitted.settings.defaultActive);
91
+ if (this.bind(agent) !== admitted || admitted.epoch !== request.expectedAdmissionEpoch || mode.revision !== request.expectedModeRevision) {
92
+ throw new DsmmProfileError("conflict", "The session profile, preset or mode changed. Refresh before retrying.");
93
+ }
94
+ if (mode.locked)
95
+ throw new DsmmProfileError("validation", "A DW preset owns its Deepwork composition.");
96
+ assertAuthority?.();
97
+ };
98
+ assertCurrent();
99
+ if (agent.status === "running")
100
+ throw new DsmmProfileError("busy", "Wait for the session to become idle.");
101
+ if (agent.runMaintenance === undefined)
102
+ throw new DsmmProfileError("unavailable", "Native idle maintenance is unavailable.");
103
+ // Read before the write, so a later read failure cannot misreport a commit.
104
+ const before = await this.getSession(agent);
105
+ assertCurrent();
106
+ try {
107
+ return await agent.runMaintenance(async (maintenanceSignal) => {
108
+ this.switching.add(agent);
109
+ try {
110
+ assertCurrent(maintenanceSignal);
111
+ if (hasOpenTurn(sessionEvents(agent.session)))
112
+ throw new DsmmProfileError("busy", "A session turn is still open.");
113
+ try {
114
+ await this.mode.selectIdle(agent, request.active, admitted.settings.defaultActive);
115
+ }
116
+ catch {
117
+ throw new DsmmProfileError("io", "The session mode could not be saved. Refresh before retrying.");
118
+ }
119
+ const role = resolveEffectiveDsmmRole(agent, admitted.settings, this.mode.active(agent, admitted.settings.defaultActive));
120
+ const profileModel = this.declaredProfileModel(admitted.settings, role);
121
+ const { profileModel: _previousModel, rolePolicy: _previousPolicy, ...snapshot } = before;
122
+ return { ...snapshot, deepwork: this.mode.describe(agent, admitted.settings.defaultActive),
123
+ rolePolicy: roleRouteRuntimeState(agent, admitted.settings, role, admitted.epoch), ...(profileModel === undefined ? {} : { profileModel }) };
124
+ }
125
+ finally {
126
+ this.switching.delete(agent);
127
+ }
128
+ });
129
+ }
130
+ catch (error) {
131
+ if (error instanceof DsmmProfileError)
132
+ throw error;
133
+ throw new DsmmProfileError("maintenance", "The native session refused the Deepwork change. Refresh before retrying.");
134
+ }
135
+ }
79
136
  selectSession(request, agent, signal, assertAuthority) {
80
137
  this.assertRoot(agent);
81
138
  if (request.sessionId !== agent.id)
@@ -105,7 +162,7 @@ export class DsmmProfileRuntime {
105
162
  const result = await this.store.selectSession(request, epoch, async (document) => {
106
163
  const prepared = immutableSettings(resolveProfileSettings(this.baseline, document?.settings ?? {}));
107
164
  await this.validate(prepared, agent, maintenanceSignal);
108
- const role = resolveEffectiveDsmmRole(agent, prepared, isDeepworkActive(sessionEvents(agent.session), prepared.defaultActive));
165
+ const role = resolveEffectiveDsmmRole(agent, prepared, this.mode.active(agent, prepared.defaultActive));
109
166
  profileModel = this.declaredProfileModel(prepared, role);
110
167
  assertCurrent(maintenanceSignal);
111
168
  return prepared;
@@ -118,6 +175,7 @@ export class DsmmProfileRuntime {
118
175
  // A committed selection remains a successful transaction even if a
119
176
  // queued wake or disposal wins immediately when maintenance releases.
120
177
  return { sessionId: request.sessionId, globalDefault: selectionState(this.current), selection: selectionState(result.selection), admittedSelection: selectionState(result.selection),
178
+ deepwork: this.mode.describe(agent, result.prepared.defaultActive),
121
179
  ...(profileModel === undefined ? {} : { profileModel }),
122
180
  scope: request.id === null ? "deployment-baseline" : "session-override", admissionEpoch: epoch, switchAllowed: true };
123
181
  }
@@ -310,11 +368,11 @@ function selectionState(selection) {
310
368
  return { selectedId: selection.selectedId, appliedRevision: selection.appliedRevision, selectionRevision: selection.selectionRevision };
311
369
  }
312
370
  /** No guessed home fallback: native deployment context owns this directory. */
313
- export async function createProfileRuntime(ctx, baseline) {
371
+ export async function createProfileRuntime(ctx, baseline, options = {}) {
314
372
  const profile = ctx.get?.("profileContext");
315
373
  if (typeof profile?.dir !== "string" || profile.dir.trim() === "")
316
374
  throw new DsmmProfileError("activation", "Deepwork profiles require the native deployment profile directory.");
317
- const runtime = new DsmmProfileRuntime(ctx, baseline, new ProfileStore(join(profile.dir, "dsmm-profiles")));
375
+ const runtime = new DsmmProfileRuntime(ctx, baseline, new ProfileStore(join(profile.dir, "dsmm-profiles")), options);
318
376
  await runtime.initialize();
319
377
  return runtime;
320
378
  }
@@ -73,6 +73,21 @@ export interface SessionProfileSnapshot {
73
73
  rolePolicy?: DsmmRoleRuntimeState;
74
74
  /** Declared profile primary for the current ordinary root role, not an applied native selection. */
75
75
  profileModel?: DsmmModelRoute;
76
+ /** Optional for older Hosts; explicit session intent always beats defaults. */
77
+ deepwork?: SessionDeepworkState;
78
+ }
79
+ export interface SessionDeepworkState {
80
+ active: boolean;
81
+ explicit: boolean;
82
+ /** Standing DW presets own their composition; only ordinary presets toggle. */
83
+ locked: boolean;
84
+ revision: string;
85
+ }
86
+ export interface SessionModeSelectRequest {
87
+ sessionId: string;
88
+ active: boolean;
89
+ expectedModeRevision: string;
90
+ expectedAdmissionEpoch: string;
76
91
  }
77
92
  export interface SessionProfileSelectRequest extends ProfileSelectRequest {
78
93
  sessionId: string;
package/lib/state.d.ts CHANGED
@@ -1,14 +1,18 @@
1
1
  import type { DshAgent, DshContext, DshSessionEvent } from "./dsh-types.js";
2
+ import type { SessionDeepworkState } from "./profile-types.js";
2
3
  export declare const DEEPWORK_MODE_EVENT = "deepwork/mode";
3
4
  export type DeepworkSelectionOutcome = "committed" | "pending" | "unchanged";
4
- export declare function isDeepworkActive(events?: readonly DshSessionEvent[], defaultActive?: boolean): boolean;
5
+ export declare function isDeepworkActive(events?: readonly DshSessionEvent[], defaultActive?: boolean, selectedPreset?: string): boolean;
5
6
  export declare function hasOpenTurn(events?: readonly DshSessionEvent[]): boolean;
6
7
  export declare class DeepworkModeController {
7
8
  private readonly ctx;
8
9
  private readonly pending;
9
10
  constructor(ctx: DshContext);
10
11
  active(agent: DshAgent | undefined, defaultActive: boolean): boolean;
12
+ describe(agent: DshAgent, defaultActive: boolean): SessionDeepworkState;
11
13
  select(agent: DshAgent, active: boolean, defaultActive?: boolean): Promise<DeepworkSelectionOutcome>;
14
+ /** Native idle maintenance owns this write; never stage an uncommitted UI intent. */
15
+ selectIdle(agent: DshAgent, active: boolean, defaultActive: boolean): Promise<void>;
12
16
  private commit;
13
17
  }
14
18
  //# sourceMappingURL=state.d.ts.map
package/lib/state.js CHANGED
@@ -1,5 +1,7 @@
1
- import { sessionEvents } from "./session-scope.js";
1
+ import { resolveSelectedAgentPreset, sessionEvents } from "./session-scope.js";
2
2
  import { assertDsmmMetadataPersistence } from "./session-metadata.js";
3
+ import { createHash } from "node:crypto";
4
+ import { isDsmmRoleId } from "./roles.js";
3
5
  export const DEEPWORK_MODE_EVENT = "deepwork/mode";
4
6
  function activeFromEvent(event) {
5
7
  if (event.type !== DEEPWORK_MODE_EVENT)
@@ -9,8 +11,8 @@ function activeFromEvent(event) {
9
11
  ? data.active
10
12
  : undefined;
11
13
  }
12
- export function isDeepworkActive(events = [], defaultActive = false) {
13
- let active = defaultActive;
14
+ export function isDeepworkActive(events = [], defaultActive = false, selectedPreset) {
15
+ let active = selectedPreset === "minimal" ? false : defaultActive;
14
16
  for (const event of events)
15
17
  active = activeFromEvent(event) ?? active;
16
18
  return active;
@@ -47,22 +49,45 @@ export class DeepworkModeController {
47
49
  active(agent, defaultActive) {
48
50
  if (agent === undefined)
49
51
  return false;
50
- return this.pending.get(agent.session)?.active ?? isDeepworkActive(sessionEvents(agent.session), defaultActive);
52
+ return this.pending.get(agent.session)?.active ?? isDeepworkActive(sessionEvents(agent.session), defaultActive, resolveSelectedAgentPreset(agent.session));
53
+ }
54
+ describe(agent, defaultActive) {
55
+ const events = sessionEvents(agent.session);
56
+ const preset = resolveSelectedAgentPreset(agent.session);
57
+ const intents = events.map(activeFromEvent).filter((value) => value !== undefined);
58
+ const pending = this.pending.get(agent.session);
59
+ const locked = isDsmmRoleId(preset);
60
+ return { active: locked || this.active(agent, defaultActive), explicit: intents.length > 0 || pending !== undefined, locked,
61
+ revision: createHash("sha256").update(JSON.stringify({ preset, defaultActive, intents,
62
+ presetChanges: events.filter((event) => event.type === "agent-preset/selected").length, pending: pending?.active })).digest("hex") };
51
63
  }
52
64
  async select(agent, active, defaultActive = false) {
53
65
  const current = this.active(agent, defaultActive);
54
- if (current === active && !this.pending.has(agent.session))
66
+ const explicit = sessionEvents(agent.session).some((event) => activeFromEvent(event) !== undefined);
67
+ if (current === active && explicit && !this.pending.has(agent.session))
55
68
  return "unchanged";
56
- this.pending.set(agent.session, { active });
57
- if (hasOpenTurn(sessionEvents(agent.session)))
69
+ if (hasOpenTurn(sessionEvents(agent.session))) {
70
+ this.pending.set(agent.session, { active });
58
71
  return "pending";
72
+ }
59
73
  await this.commit(agent.session, active);
60
74
  return "committed";
61
75
  }
76
+ /** Native idle maintenance owns this write; never stage an uncommitted UI intent. */
77
+ async selectIdle(agent, active, defaultActive) {
78
+ if (hasOpenTurn(sessionEvents(agent.session)))
79
+ throw new Error("Deepwork mode requires an idle session");
80
+ const mode = this.describe(agent, defaultActive);
81
+ if (mode.explicit && mode.active === active && !this.pending.has(agent.session))
82
+ return;
83
+ await this.commit(agent.session, active);
84
+ }
62
85
  async commit(session, active) {
86
+ const pending = this.pending.get(session);
63
87
  assertDsmmMetadataPersistence(this.ctx);
64
88
  await session.append(DEEPWORK_MODE_EVENT, { active });
65
- this.pending.delete(session);
89
+ if (this.pending.get(session) === pending)
90
+ this.pending.delete(session);
66
91
  }
67
92
  }
68
93
  //# sourceMappingURL=state.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dsmm/dsmm",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "DeepSeek Harness deepwork mode bundle for ocmm-style agentic workflows.",
5
5
  "author": "Hugefiver",
6
6
  "license": "LicenseRef-AAAPL",